Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 9858f5b

Browse files
committed
prepare for seven and zero
1 parent f8755c7 commit 9858f5b

File tree

6 files changed

+87
-58
lines changed

6 files changed

+87
-58
lines changed

src/commands/eval.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { inspect } from "util";
33

44
import { client } from "../client.js";
55
import * as clientUtils from "../client.js";
6+
import * as components from "../components.js";
67
import * as constants from "../constants.js";
78
import database from "../database.js";
89
import { games } from "../gameLogic/index.js";
910
import * as gameLogic from "../gameLogic/index.js";
1011
import { config } from "../index.js";
1112
import { Command } from "../types";
1213
import * as utils from "../utils.js";
13-
client; gameLogic; database; constants; utils;
14+
client; components; gameLogic; database; constants; utils;
1415

1516
const MAX_RESPONSE_LENGTH = 1950;
1617

src/components.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ComponentBuilder } from "@oceanicjs/builders";
22
import { ButtonStyles, ComponentTypes, MessageActionRow } from "oceanic.js";
33

44
import { client, sendMessage } from "./client.js";
5-
import { ButtonIDs, cardEmotes, defaultSettings, maxRejoinableTurnCount, SelectIDs, SettingsIDs } from "./constants.js";
5+
import { ButtonIDs, cardEmotes, colors, defaultSettings, maxRejoinableTurnCount, SelectIDs, SettingsIDs, uniqueVariants } from "./constants.js";
66
import { sendGameMessage } from "./gameLogic/index.js";
77
import { Card, UnoGame } from "./types.js";
88
import { cardArrayToCount, getUsername, next, toHumanReadableTime, toTitleCase } from "./utils.js";
@@ -130,25 +130,54 @@ export function PickCardSelect(game: UnoGame<true>, id: string, canSkip = false)
130130
return row.toJSON();
131131
}
132132

133-
export const DrawStackedCardSelect = (game: UnoGame<true>, cards: { [k in Card]?: number }) => new ComponentBuilder<MessageActionRow>()
133+
export const DrawStackedCardSelect = (game: UnoGame<true>, cards: { [k in Card]?: number }) =>
134+
new ComponentBuilder<MessageActionRow>()
135+
.addSelectMenu({
136+
customID: SelectIDs.FORCEFUL_DRAW,
137+
options: [{
138+
label: `Draw ${game.drawStackCounter} cards`,
139+
value: "draw-forceful",
140+
emoji: ComponentBuilder.emojiToPartial("🃏")
141+
},
142+
...Object.keys(cards).map(c => {
143+
if (c === "+4" || c.split("-")[1] === "+2") return {
144+
label: `${toTitleCase(c)}`,
145+
value: c,
146+
emoji: ComponentBuilder.emojiToPartial(cardEmotes[c])
147+
};
148+
})].filter(Boolean),
149+
type: ComponentTypes.STRING_SELECT
150+
})
151+
.toJSON();
152+
153+
export const CardColorSelect = (cardType: typeof uniqueVariants[number]) => new ComponentBuilder<MessageActionRow>()
134154
.addSelectMenu({
135-
customID: SelectIDs.FORCEFUL_DRAW,
136-
options: [{
137-
label: `Draw ${game.drawStackCounter} cards`,
138-
value: "draw-forceful",
139-
emoji: ComponentBuilder.emojiToPartial("🃏")
140-
},
141-
...Object.keys(cards).map(c => {
142-
if (c === "+4" || c.split("-")[1] === "+2") return {
143-
label: `${toTitleCase(c)}`,
144-
value: c,
145-
emoji: ComponentBuilder.emojiToPartial(cardEmotes[c], "custom")
155+
customID: SelectIDs.CHOOSE_COLOR,
156+
options: Object.values(colors).map(c => {
157+
return {
158+
label: toTitleCase(c),
159+
value: `${c}-${cardType}`
146160
};
147-
})].filter(Boolean),
148-
type: ComponentTypes.STRING_SELECT
161+
}),
162+
type: ComponentTypes.STRING_SELECT,
149163
})
150164
.toJSON();
151165

166+
export function PlayerUserSelect(game: UnoGame<true>) {
167+
const guild = client.guilds.get(game.guildID);
168+
return new ComponentBuilder<MessageActionRow>()
169+
.addSelectMenu({
170+
customID: SelectIDs.PLAYER_USER_SELECT,
171+
placeholder: "Choose a player",
172+
options: game.players.filter(id => id !== game.currentPlayer).map(id => ({
173+
label: getUsername(id, true, guild, "none"),
174+
value: id
175+
})),
176+
type: ComponentTypes.STRING_SELECT
177+
})
178+
.toJSON();
179+
}
180+
152181
export const SettingsSelectMenu = (game: UnoGame<false>) => new ComponentBuilder<MessageActionRow>()
153182
.addSelectMenu({
154183
customID: SelectIDs.EDIT_GAME_SETTINGS,

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const SelectIDs = Object.freeze({
111111
CHOOSE_CARD_ABOVE_25: "choose-card-2",
112112
CHOOSE_COLOR: "choose-color",
113113
FORCEFUL_DRAW: "draw-or-stack",
114+
PLAYER_USER_SELECT: "select-player-user",
114115
EDIT_GAME_SETTINGS: "change-settings",
115116
EDIT_GAME_SETTINGS_RULES: "change-game-rules",
116117
});

src/gameLogic/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function makeStartMessage(game: UnoGame<false>, guild?: Guild) {
5050
**Game will start <t:${game.starting}:R>**
5151
Current game host: ${getUsername(game.host, true, guild ?? game.message?.channel?.guild)}
5252
\`\`\`
53-
${game.players.map(p => getUsername(p, true, guild ?? game.message?.channel?.guild, true) ?? `Unknown [${p}]`).join("\n")}
53+
${game.players.map(p => getUsername(p, true, guild ?? game.message?.channel?.guild, "codeblock") ?? `Unknown [${p}]`).join("\n")}
5454
\`\`\`
5555
`)
5656
.setColor(defaultColor)
@@ -59,7 +59,7 @@ ${game.players.map(p => getUsername(p, true, guild ?? game.message?.channel?.gui
5959

6060
const makeGameLine = (game: UnoGame<true>, playerID: string, i: number) =>
6161
`${game.players.indexOf(game.currentPlayer) === i ? "+ " : "\u200b "}\
62-
${getUsername(playerID, true, game.message.channel.guild, true) ?? `Unknown [${playerID}]`}: \
62+
${getUsername(playerID, true, game.message.channel.guild, "codeblock") ?? `Unknown [${playerID}]`}: \
6363
${game.cards[playerID].length} card${game.cards[playerID].length === 1 ? "" : "s"}`;
6464

6565
export function sendGameMessage(game: UnoGame<true>, keepTimeout = false) {

src/gameLogic/playedCards.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { ComponentBuilder } from "@oceanicjs/builders";
2-
import { ComponentInteraction, ComponentTypes, MessageActionRow, MessageFlags } from "oceanic.js";
1+
import { ComponentInteraction, ComponentTypes, MessageFlags } from "oceanic.js";
32

43
import { deleteMessage, sendMessage } from "../client.js";
5-
import { PickCardSelect } from "../components.js";
6-
import { cardEmotes, cards, colors, SelectIDs, uniqueVariants, variants } from "../constants.js";
4+
import { CardColorSelect, PickCardSelect } from "../components.js";
5+
import { cardEmotes, cards, colors, uniqueVariants, variants } from "../constants.js";
76
import database from "../database.js";
87
import { config } from "../index.js";
98
import timeouts from "../timeouts.js";
@@ -124,19 +123,8 @@ export function onCardPlayed(ctx: ComponentInteraction<ComponentTypes.STRING_SEL
124123
if (uniqueVariants.includes(color)) {
125124
return ctx.createFollowup({
126125
content: "Choose a color",
127-
components: new ComponentBuilder<MessageActionRow>()
128-
.addSelectMenu({
129-
customID: SelectIDs.CHOOSE_COLOR,
130-
options: Object.values(colors).map(c => {
131-
return {
132-
label: toTitleCase(c),
133-
value: `${c}-${color}`
134-
};
135-
}),
136-
type: ComponentTypes.STRING_SELECT,
137-
})
138-
.toJSON()
139-
});
126+
components: CardColorSelect(color as typeof uniqueVariants[number])
127+
}).then(() => ctx.deleteOriginal());
140128
}
141129

142130
if (cardPlayed === "skip" && (!game.settings.allowSkipping || (game.lastPlayer.id !== game.currentPlayer && !wasLastTurnBlocked(game))))
@@ -183,33 +171,41 @@ You drew ${cardEmotes[newCards[0]]}`,
183171
game.cards[ctx.member.id].splice(game.cards[ctx.member.id].indexOf(cardPlayed), 1);
184172
delete game.saboteurs[game.lastPlayer.id];
185173

186-
if (variant === "reverse") {
187-
game.players = game.players.reverse();
188-
if (game.players.length === 2) {
189-
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
190-
extraInfo = `**${getUsername(game.currentPlayer, true, ctx.guild)}** was skipped`;
174+
switch (variant) {
175+
case "reverse": {
176+
game.players = game.players.reverse();
177+
if (game.players.length === 2) {
178+
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
179+
extraInfo = `**${getUsername(game.currentPlayer, true, ctx.guild)}** was skipped`;
180+
}
181+
break;
191182
}
192-
}
193-
194-
if (variant === "+2") {
195-
const nextPlayer = next(game.players, game.players.indexOf(ctx.member.id));
196-
if (game.settings.allowStacking && game.cards[nextPlayer].some(c => c === "+4" || c.endsWith("+2"))) {
197-
game.drawStackCounter += 2;
183+
case "+2": {
184+
const nextPlayer = next(game.players, game.players.indexOf(ctx.member.id));
185+
if (game.settings.allowStacking && game.cards[nextPlayer].some(c => c === "+4" || c.endsWith("+2"))) {
186+
game.drawStackCounter += 2;
187+
}
188+
else if (game.cards[ctx.member.id].length > 0) {
189+
const { cards, newDeck } = game.draw(2 + game.drawStackCounter);
190+
game.cards[nextPlayer].push(...cards);
191+
game.deck = newDeck;
192+
extraInfo = `**${getUsername(nextPlayer, true, ctx.guild)}** drew **${2 + game.drawStackCounter}** cards and was skipped`;
193+
game.drawStackCounter = 0;
194+
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
195+
}
196+
break;
198197
}
199-
else if (game.cards[ctx.member.id].length > 0) {
200-
const { cards, newDeck } = game.draw(2 + game.drawStackCounter);
201-
game.cards[nextPlayer].push(...cards);
202-
game.deck = newDeck;
203-
extraInfo = `**${getUsername(nextPlayer, true, ctx.guild)}** drew **${2 + game.drawStackCounter}** cards and was skipped`;
204-
game.drawStackCounter = 0;
198+
case "block": {
205199
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
200+
extraInfo = `**${getUsername(game.currentPlayer, true, ctx.guild)}** was skipped`;
201+
break;
202+
}
203+
case "7":
204+
case "0": {
205+
// TODO
206206
}
207207
}
208208

209-
if (variant === "block") {
210-
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
211-
extraInfo = `**${getUsername(game.currentPlayer, true, ctx.guild)}** was skipped`;
212-
}
213209
if (game.settings.allowSkipping)
214210
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
215211
ctx.deleteOriginal();

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ export function updateStats(game: UnoGame<true>, winner: string) {
8181
database.setBulk(game.guildID, newStats);
8282
}
8383

84-
export function getUsername(id: string, nick: boolean, guild: Guild, inCodeblock = false) {
84+
export function getUsername(id: string, nick: boolean, guild: Guild, charEscape: "none" | "markdown" | "codeblock" = "markdown") {
8585
// Member.displayName is not actually the user's display name
8686
const name = (nick && guild?.members.get(id)?.nick)
8787
|| guild?.members.get(id)?.username
8888
|| client.users.get(id)?.username
8989
|| id;
9090
if (!name) return "[this shouldn't be here]";
91-
if (inCodeblock) return name.replace(/`/g, "`\u200b");
91+
92+
if (charEscape === "none") return name;
93+
else if (charEscape === "codeblock") return name.replace(/`/g, "`\u200b");
9294
return name.replace(/([*_~`|])/g, "\\$1");
9395
}

0 commit comments

Comments
 (0)