|
| 1 | +import { ComponentBuilder } from "@oceanicjs/builders"; |
| 2 | +import { ButtonStyles, ComponentTypes, MessageActionRow } from "oceanic.js"; |
| 3 | + |
| 4 | +import { client, sendMessage } from "./client.js"; |
| 5 | +import { ButtonIDs, cardEmotes, defaultSettings, SelectIDs, SettingsIDs } from "./constants.js"; |
| 6 | +import { sendGameMessage } from "./gameLogic/index.js"; |
| 7 | +import { Card, UnoGame } from "./types.js"; |
| 8 | +import { cardArrayToCount, getUsername, next, toHumanReadableTime, toTitleCase } from "./utils.js"; |
| 9 | + |
| 10 | +export const GameButtons = ((canRejoin: boolean) => { |
| 11 | + const components = new ComponentBuilder<MessageActionRow>() |
| 12 | + .addInteractionButton({ |
| 13 | + style: ButtonStyles.SECONDARY, |
| 14 | + customID: ButtonIDs.VIEW_CARDS, |
| 15 | + label: "View cards", |
| 16 | + emoji: ComponentBuilder.emojiToPartial("🔍", "default") |
| 17 | + }) |
| 18 | + .addInteractionButton({ |
| 19 | + style: ButtonStyles.SUCCESS, |
| 20 | + customID: ButtonIDs.PLAY_CARD, |
| 21 | + label: "Play", |
| 22 | + emoji: ComponentBuilder.emojiToPartial("🃏", "default") |
| 23 | + }) |
| 24 | + .addRow() |
| 25 | + .addInteractionButton({ |
| 26 | + style: ButtonStyles.SECONDARY, |
| 27 | + customID: ButtonIDs.VIEW_GAME_SETTINGS, |
| 28 | + emoji: ComponentBuilder.emojiToPartial("⚙", "default") |
| 29 | + }) |
| 30 | + .addInteractionButton({ |
| 31 | + style: ButtonStyles.DANGER, |
| 32 | + customID: ButtonIDs.LEAVE_GAME, |
| 33 | + emoji: ComponentBuilder.emojiToPartial("🚪", "default") |
| 34 | + }) |
| 35 | + .addInteractionButton({ |
| 36 | + style: ButtonStyles.PRIMARY, |
| 37 | + customID: ButtonIDs.JOIN_MID_GAME, |
| 38 | + label: "Join", |
| 39 | + disabled: !canRejoin, |
| 40 | + emoji: ComponentBuilder.emojiToPartial("➡️", "default") |
| 41 | + }); |
| 42 | + return components.toJSON(); |
| 43 | +}); |
| 44 | + |
| 45 | +export function PickCardSelect(game: UnoGame<true>, id: string, canSkip = false): MessageActionRow[] | false { |
| 46 | + if (!game.players.includes(id)) |
| 47 | + throw new Error(`Player ${id} not in game ${game.channelID}`); |
| 48 | + |
| 49 | + const cards = cardArrayToCount(game.cards[id]); |
| 50 | + const entries = [ |
| 51 | + ...Object.keys(cards).map(c => { |
| 52 | + return { |
| 53 | + label: `${toTitleCase(c)}${cards[c] >= 2 ? ` x${cards[c]}` : ""}`, |
| 54 | + value: c, |
| 55 | + emoji: ComponentBuilder.emojiToPartial(cardEmotes[c]) |
| 56 | + }; |
| 57 | + }), |
| 58 | + { |
| 59 | + label: "Draw a card", |
| 60 | + value: "draw", |
| 61 | + emoji: ComponentBuilder.emojiToPartial("🃏") |
| 62 | + } |
| 63 | + ]; |
| 64 | + |
| 65 | + if (game.settings.allowSkipping && canSkip) |
| 66 | + entries.push({ |
| 67 | + label: "Skip your turn", |
| 68 | + value: "skip", |
| 69 | + emoji: ComponentBuilder.emojiToPartial("➡") |
| 70 | + }); |
| 71 | + |
| 72 | + if (entries.length > 50) { |
| 73 | + game.players.splice(game.players.indexOf(id), 1); |
| 74 | + sendMessage(game.channelID, `Removed **${getUsername(id, true, client.guilds.get(game.guildID))}**`); |
| 75 | + if (game.players.length <= 1) return; |
| 76 | + if (game.currentPlayer === id) { |
| 77 | + game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer)); |
| 78 | + game.lastPlayer.duration = 0; |
| 79 | + } |
| 80 | + sendGameMessage(game); |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + const row = new ComponentBuilder<MessageActionRow>(); |
| 85 | + row.addSelectMenu({ |
| 86 | + customID: SelectIDs.CHOOSE_CARD, |
| 87 | + placeholder: "Choose a card", |
| 88 | + type: ComponentTypes.STRING_SELECT, |
| 89 | + options: entries.slice(0, 25) |
| 90 | + }); |
| 91 | + if (entries.length > 25) row.addSelectMenu({ |
| 92 | + customID: SelectIDs.CHOOSE_CARD_ABOVE_25, |
| 93 | + placeholder: "Choose a card", |
| 94 | + type: ComponentTypes.STRING_SELECT, |
| 95 | + options: entries.slice(25) |
| 96 | + }); |
| 97 | + |
| 98 | + return row.toJSON(); |
| 99 | +} |
| 100 | + |
| 101 | +export const DrawStackedCardSelect = (game: UnoGame<true>, cards: { [k in Card]?: number }) => new ComponentBuilder<MessageActionRow>() |
| 102 | + .addSelectMenu({ |
| 103 | + customID: SelectIDs.FORCEFUL_DRAW, |
| 104 | + options: [{ |
| 105 | + label: `Draw ${game.drawStackCounter} cards`, |
| 106 | + value: "draw-forceful", |
| 107 | + emoji: ComponentBuilder.emojiToPartial("🃏") |
| 108 | + }, |
| 109 | + ...Object.keys(cards).map(c => { |
| 110 | + if (c === "+4" || c.split("-")[1] === "+2") return { |
| 111 | + label: `${toTitleCase(c)}`, |
| 112 | + value: c, |
| 113 | + emoji: ComponentBuilder.emojiToPartial(cardEmotes[c], "custom") |
| 114 | + }; |
| 115 | + })].filter(Boolean), |
| 116 | + type: ComponentTypes.STRING_SELECT |
| 117 | + }) |
| 118 | + .toJSON(); |
| 119 | + |
| 120 | +export const SettingsSelectMenu = (game: UnoGame<false>) => new ComponentBuilder<MessageActionRow>() |
| 121 | + .addSelectMenu({ |
| 122 | + customID: SelectIDs.EDIT_GAME_SETTINGS, |
| 123 | + type: ComponentTypes.STRING_SELECT, |
| 124 | + options: [{ |
| 125 | + label: "Turn duration", |
| 126 | + value: SettingsIDs.TIMEOUT_DURATION, |
| 127 | + description: `${toHumanReadableTime(game.settings.timeoutDuration ?? defaultSettings.timeoutDuration)}` |
| 128 | + }, |
| 129 | + { |
| 130 | + label: "Kick on timeout", |
| 131 | + value: SettingsIDs.KICK_ON_TIMEOUT, |
| 132 | + description: game.settings.kickOnTimeout ? "Enabled" : "Disabled" |
| 133 | + }, |
| 134 | + { |
| 135 | + label: "Allow skipping turns", |
| 136 | + value: SettingsIDs.ALLOW_SKIPPING, |
| 137 | + description: game.settings.allowSkipping ? "Enabled" : "Disabled" |
| 138 | + }, |
| 139 | + { |
| 140 | + label: "Anti sabotage", |
| 141 | + value: SettingsIDs.ANTI_SABOTAGE, |
| 142 | + description: `Don't allow drawing too many cards at once. ${game.settings.antiSabotage ? "Enabled" : "Disabled"}` |
| 143 | + }, |
| 144 | + { |
| 145 | + label: "Stack +2's and +4's", |
| 146 | + value: SettingsIDs.ALLOW_CARD_STACKING, |
| 147 | + description: game.settings.allowStacking ? "Enabled" : "Disabled" |
| 148 | + }, |
| 149 | + { |
| 150 | + label: "Randomize order of players", |
| 151 | + value: SettingsIDs.RANDOMIZE_PLAYER_LIST, |
| 152 | + description: game.settings.randomizePlayerList ? "Enabled" : "Disabled" |
| 153 | + }, |
| 154 | + { |
| 155 | + label: "Resend game message", |
| 156 | + value: SettingsIDs.RESEND_GAME_MESSAGE, |
| 157 | + description: `if it gets sent too far up because of chat. ${game.settings.resendGameMessage ? "Enabled" : "Disabled"}` |
| 158 | + }, |
| 159 | + { |
| 160 | + label: "Allow joining mid game", |
| 161 | + value: SettingsIDs.ALLOW_REJOINING, |
| 162 | + description: game.settings.canRejoin ? "Enabled" : "Disabled" |
| 163 | + }] |
| 164 | + }) |
| 165 | + .toJSON(); |
0 commit comments