Skip to content

Commit 8b98cc5

Browse files
authoredApr 14, 2023
Merge pull request #472 from uwcsc/320-connect-four
Connect 4!
2 parents 505592f + 30b1d36 commit 8b98cc5

File tree

8 files changed

+648
-17
lines changed

8 files changed

+648
-17
lines changed
 

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"axios": "^0.27.2",
2929
"concurrently": "^6.1.0",
3030
"cron": "^1.8.2",
31-
"discord.js": "^13.6.0",
31+
"discord.js": "^13.15.0",
3232
"dotenv": "^8.2.0",
3333
"emoji-regex": "^10.2.1",
3434
"engine-blackjack-ts": "^0.9.11",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { container } from '@sapphire/framework';
2+
import {
3+
CodeyCommandDetails,
4+
getUserFromMessage,
5+
SapphireAfterReplyType,
6+
SapphireMessageExecuteType,
7+
SapphireMessageResponseWithMetadata,
8+
} from '../../codeyCommand';
9+
import { connectFourGameTracker } from '../../components/games/connectFour';
10+
11+
const connectFourExecuteCommand: SapphireMessageExecuteType = async (
12+
_client,
13+
messageFromUser,
14+
_args,
15+
): Promise<SapphireMessageResponseWithMetadata> => {
16+
/*
17+
executeCommand sends the initial connectFour embed;
18+
the subsequent interactionHandlers handle the rest of the logic
19+
*/
20+
21+
const game = await connectFourGameTracker.startGame(
22+
messageFromUser.channelId,
23+
getUserFromMessage(messageFromUser),
24+
);
25+
26+
// Return initial response
27+
return new SapphireMessageResponseWithMetadata(game.getGameResponse(), {
28+
gameId: game.id,
29+
});
30+
};
31+
32+
const connectFourAfterMessageReply: SapphireAfterReplyType = async (result, sentMessage) => {
33+
if (typeof result.metadata.gameId === 'undefined') return;
34+
// Store the message which the game takes place in the game object
35+
connectFourGameTracker.runFuncOnGame(<number>result.metadata.gameId, (game) => {
36+
game.gameMessage = sentMessage;
37+
});
38+
};
39+
40+
export const connectFourCommandDetails: CodeyCommandDetails = {
41+
name: 'connect4',
42+
aliases: [],
43+
description: 'Play Connect 4!',
44+
detailedDescription: `**Examples:**
45+
\`${container.botPrefix}connect4\`
46+
\`${container.botPrefix}connect 4 @user\``,
47+
48+
isCommandResponseEphemeral: false,
49+
messageWhenExecutingCommand: 'Setting up your Connect 4 game...',
50+
executeCommand: connectFourExecuteCommand,
51+
afterMessageReply: connectFourAfterMessageReply,
52+
options: [],
53+
subcommandDetails: {},
54+
};

‎src/commands/games/connectFour.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Command } from '@sapphire/framework';
2+
import { CodeyCommand } from '../../codeyCommand';
3+
import { connectFourCommandDetails } from '../../commandDetails/games/connectFour';
4+
5+
export class GamesConnectFourCommand extends CodeyCommand {
6+
details = connectFourCommandDetails;
7+
8+
public constructor(context: Command.Context, options: Command.Options) {
9+
super(context, {
10+
...options,
11+
aliases: connectFourCommandDetails.aliases,
12+
description: connectFourCommandDetails.description,
13+
detailedDescription: connectFourCommandDetails.detailedDescription,
14+
});
15+
}
16+
}

‎src/components/db.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ const initRpsGameInfo = async (db: Database): Promise<void> => {
140140
);
141141
};
142142

143+
const initConnectFourGameInfo = async (db: Database): Promise<void> => {
144+
// If player 2 ID is null, the game was against Codey
145+
await db.run(
146+
`
147+
CREATE TABLE IF NOT EXISTS connect_four_game_info (
148+
id INTEGER PRIMARY KEY NOT NULL,
149+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
150+
player1_id VARCHAR(30) NOT NULL,
151+
player2_id VARCHAR(30),
152+
player1_sign INTEGER NOT NULL DEFAULT 0,
153+
player2_sign INTEGER NOT NULL DEFAULT 0,
154+
status INTEGER NOT NULL DEFAULT 0
155+
)
156+
`,
157+
);
158+
};
159+
143160
const initResumePreview = async (db: Database): Promise<void> => {
144161
await db.run(
145162
`
@@ -215,6 +232,7 @@ const initTables = async (db: Database): Promise<void> => {
215232
await initUserCoinTable(db);
216233
await initUserProfileTable(db);
217234
await initRpsGameInfo(db);
235+
await initConnectFourGameInfo(db);
218236
await initResumePreview(db);
219237
await initCompaniesTable(db);
220238
await initPeopleCompaniesTable(db);

0 commit comments

Comments
 (0)