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

Commit 6934926

Browse files
committed
move components to separate file
1 parent 0f77529 commit 6934926

File tree

6 files changed

+177
-166
lines changed

6 files changed

+177
-166
lines changed

src/components.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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();

src/gameLogic/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { EmbedBuilder } from "@oceanicjs/builders";
22
import { AnyGuildTextChannel, ComponentInteraction, ComponentTypes, Guild, Message, ModalSubmitInteraction, TypedCollection } from "oceanic.js";
33

44
import { client, deleteMessage, sendMessage } from "../client.js";
5+
import { GameButtons, SettingsSelectMenu } from "../components.js";
56
import { ButtonIDs, cardEmojis, cardEmotes, coloredUniqueCards, defaultColor, defaultSettings, maxRejoinableTurnCount, rainbowColors, SelectIDs, SettingsIDs, uniqueVariants, veryLongTime } from "../constants.js";
67
import database from "../database.js";
78
import { config } from "../index.js";
89
import timeouts from "../timeouts.js";
910
import { UnoGame } from "../types.js";
10-
import { cancelGameMessageFail, GameButtons, getPlayerMember, getUsername, hasStarted, next, SettingsSelectMenu, toHumanReadableTime, toTitleCase } from "../utils.js";
11+
import { cancelGameMessageFail, getPlayerMember, getUsername, hasStarted, next, toHumanReadableTime, toTitleCase } from "../utils.js";
1112
import { makeSettingsModal, onGameJoin, onSettingsChange } from "./notStarted.js";
1213
import { onCardPlayed, onColorPlayed, onForceDrawPlayed } from "./playedCards.js";
1314
import { leaveGame, onGameButtonPress } from "./started.js";

src/gameLogic/notStarted.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { ComponentBuilder } from "@oceanicjs/builders";
22
import { ButtonStyles, ComponentInteraction, ComponentTypes, MessageActionRow, MessageFlags, ModalActionRow, TextInputStyles } from "oceanic.js";
33

44
import { deleteMessage, respond, sendMessage } from "../client.js";
5+
import { SettingsSelectMenu } from "../components.js";
56
import { ButtonIDs, cardEmotes, cards, defaultSettings, SettingsIDs, uniqueVariants, veryLongTime } from "../constants.js";
67
import database from "../database.js";
78
import timeouts from "../timeouts.js";
89
import { Card, DebugState, DebugStateType, UnoGame } from "../types.js";
9-
import { getPlayerMember, hasStarted, SettingsSelectMenu, shuffle, toTitleCase, updateStats, without } from "../utils.js";
10+
import { getPlayerMember, hasStarted, shuffle, toTitleCase, updateStats, without } from "../utils.js";
1011
import { games, makeStartMessage, sendGameMessage } from "./index.js";
1112

1213
export function makeDrawCardProxy(startedGame: UnoGame<true>, userId: string, t, p, n) {

src/gameLogic/playedCards.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { ComponentBuilder } from "@oceanicjs/builders";
22
import { ComponentInteraction, ComponentTypes, MessageActionRow, MessageFlags } from "oceanic.js";
33

44
import { deleteMessage, sendMessage } from "../client.js";
5+
import { PickCardSelect } from "../components.js";
56
import { cardEmotes, cards, colors, SelectIDs, uniqueVariants, variants } from "../constants.js";
67
import database from "../database.js";
78
import { config } from "../index.js";
89
import timeouts from "../timeouts.js";
910
import { Card, UnoGame } from "../types.js";
10-
import { getUsername, next, PickCardSelect, toTitleCase, updateStats, wasLastTurnBlocked } from "../utils.js";
11+
import { getUsername, next, toTitleCase, updateStats, wasLastTurnBlocked } from "../utils.js";
1112
import { games, onTimeout, sendGameMessage } from "./index.js";
1213

1314
function isSabotage(ctx: ComponentInteraction<ComponentTypes.STRING_SELECT>, game: UnoGame<true>): boolean {

src/gameLogic/started.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { ComponentBuilder } from "@oceanicjs/builders";
22
import { ButtonStyles, ComponentInteraction, ComponentTypes, MessageActionRow, MessageFlags } from "oceanic.js";
33

44
import { sendMessage } from "../client.js";
5+
import { DrawStackedCardSelect, PickCardSelect } from "../components.js";
56
import { ButtonIDs, cardEmotes } from "../constants.js";
67
import { config } from "../index.js";
78
import { UnoGame } from "../types.js";
8-
import { cardArrayToCount, DrawStackedCardSelect, getUsername, next, PickCardSelect, toTitleCase } from "../utils.js";
9+
import { cardArrayToCount, getUsername, next, toTitleCase } from "../utils.js";
910
import { sendGameMessage } from "./index.js";
1011
import { makeDrawCardProxy } from "./notStarted.js";
1112

0 commit comments

Comments
 (0)