|
1 | 1 | import { ComponentBuilder } from "@oceanicjs/builders"; |
2 | 2 | import { AnyGuildTextChannel, ButtonStyles, ComponentTypes, Guild, MessageActionRow } from "oceanic.js"; |
3 | 3 |
|
4 | | -import { client } from "./client.js"; |
| 4 | +import { client, sendMessage } from "./client.js"; |
5 | 5 | import { ButtonIDs, cardEmotes, cards, defaultSettings, SelectIDs, SettingsIDs } from "./constants.js"; |
6 | 6 | import database from "./database.js"; |
7 | | -import { games } from "./gameLogic/index.js"; |
| 7 | +import { games, sendGameMessage } from "./gameLogic/index.js"; |
8 | 8 | import { Card, PlayerStorage, UnoGame } from "./types.js"; |
9 | 9 |
|
10 | 10 | export const GameButtons = (() => { |
@@ -42,35 +42,64 @@ export function onMsgError(e: Error, ctx: { channelID: string }) { |
42 | 42 | console.log(e); |
43 | 43 | } |
44 | 44 |
|
45 | | -export const PickCardSelect = (game: UnoGame<true>, cards: { [k in Card]?: number }) => |
46 | | - new ComponentBuilder<MessageActionRow>() |
47 | | - .addSelectMenu({ |
48 | | - customID: SelectIDs.CHOOSE_CARD, |
49 | | - placeholder: "Choose a card", |
50 | | - options: [ |
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 | | - ].concat(game.lastPlayer.id === game.currentPlayer && game.settings.allowSkipping && |
64 | | - (game.players.length === 2 ? (wasLastTurnBlocked(game) ? game.lastPlayer.duration >= 1 : true) : true) |
65 | | - ? [{ |
66 | | - label: "Skip your turn", |
67 | | - value: "skip", |
68 | | - emoji: ComponentBuilder.emojiToPartial("➡") |
69 | | - }] |
70 | | - : []), |
71 | | - type: ComponentTypes.STRING_SELECT |
72 | | - }) |
73 | | - .toJSON(); |
| 45 | +export function PickCardSelect(game: UnoGame<true>, id: string): 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.lastPlayer.id === game.currentPlayer |
| 66 | + && game.settings.allowSkipping |
| 67 | + && (game.players.length !== 2 || !wasLastTurnBlocked(game) || game.lastPlayer.duration >= 1) |
| 68 | + ) |
| 69 | + entries.push({ |
| 70 | + label: "Skip your turn", |
| 71 | + value: "skip", |
| 72 | + emoji: ComponentBuilder.emojiToPartial("➡") |
| 73 | + }); |
| 74 | + |
| 75 | + if (entries.length > 50) { |
| 76 | + game.players.splice(game.players.indexOf(id), 1); |
| 77 | + sendMessage(game.channelID, `Removed **${getUsername(id, true, client.guilds.get(game.guildID))}**`); |
| 78 | + if (game.players.length <= 1) return; |
| 79 | + if (game.currentPlayer === id) { |
| 80 | + game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer)); |
| 81 | + game.lastPlayer.duration = 0; |
| 82 | + } |
| 83 | + sendGameMessage(game); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + const row = new ComponentBuilder<MessageActionRow>(); |
| 88 | + row.addSelectMenu({ |
| 89 | + customID: SelectIDs.CHOOSE_CARD, |
| 90 | + placeholder: "Choose a card", |
| 91 | + type: ComponentTypes.STRING_SELECT, |
| 92 | + options: entries.slice(0, 25) |
| 93 | + }); |
| 94 | + if (entries.length > 25) row.addSelectMenu({ |
| 95 | + customID: SelectIDs.CHOOSE_CARD_ABOVE_25, |
| 96 | + placeholder: "Choose a card", |
| 97 | + type: ComponentTypes.STRING_SELECT, |
| 98 | + options: entries.slice(25) |
| 99 | + }); |
| 100 | + |
| 101 | + return row.toJSON(); |
| 102 | +} |
74 | 103 |
|
75 | 104 | export const DrawStackedCardSelect = (game: UnoGame<true>, cards: { [k in Card]?: number }) => new ComponentBuilder<MessageActionRow>() |
76 | 105 | .addSelectMenu({ |
@@ -177,7 +206,7 @@ export const cardArrayToCount = (a: Card[]) => a |
177 | 206 | .sort((a, b) => cards.indexOf(a) - cards.indexOf(b)) |
178 | 207 | .reduce((obj, c) => { |
179 | 208 | obj[c] = (obj[c] + 1) || 1; return obj; |
180 | | - }, {} as { [k in Card]: number; }); |
| 209 | + }, {} as { [k in Card]?: number; }); |
181 | 210 |
|
182 | 211 | export const getPlayerMember = (game: UnoGame<boolean>, player: string) => game.message.channel.guild.members.get(player); |
183 | 212 |
|
|
0 commit comments