Skip to content

Commit

Permalink
Merge pull request #472 from uwcsc/320-connect-four
Browse files Browse the repository at this point in the history
Connect 4!
  • Loading branch information
SaurontheMighty authored Apr 14, 2023
2 parents 505592f + 30b1d36 commit 8b98cc5
Show file tree
Hide file tree
Showing 8 changed files with 648 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"axios": "^0.27.2",
"concurrently": "^6.1.0",
"cron": "^1.8.2",
"discord.js": "^13.6.0",
"discord.js": "^13.15.0",
"dotenv": "^8.2.0",
"emoji-regex": "^10.2.1",
"engine-blackjack-ts": "^0.9.11",
Expand Down
54 changes: 54 additions & 0 deletions src/commandDetails/games/connectFour.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { container } from '@sapphire/framework';
import {
CodeyCommandDetails,
getUserFromMessage,
SapphireAfterReplyType,
SapphireMessageExecuteType,
SapphireMessageResponseWithMetadata,
} from '../../codeyCommand';
import { connectFourGameTracker } from '../../components/games/connectFour';

const connectFourExecuteCommand: SapphireMessageExecuteType = async (
_client,
messageFromUser,
_args,
): Promise<SapphireMessageResponseWithMetadata> => {
/*
executeCommand sends the initial connectFour embed;
the subsequent interactionHandlers handle the rest of the logic
*/

const game = await connectFourGameTracker.startGame(
messageFromUser.channelId,
getUserFromMessage(messageFromUser),
);

// Return initial response
return new SapphireMessageResponseWithMetadata(game.getGameResponse(), {
gameId: game.id,
});
};

const connectFourAfterMessageReply: SapphireAfterReplyType = async (result, sentMessage) => {
if (typeof result.metadata.gameId === 'undefined') return;
// Store the message which the game takes place in the game object
connectFourGameTracker.runFuncOnGame(<number>result.metadata.gameId, (game) => {
game.gameMessage = sentMessage;
});
};

export const connectFourCommandDetails: CodeyCommandDetails = {
name: 'connect4',
aliases: [],
description: 'Play Connect 4!',
detailedDescription: `**Examples:**
\`${container.botPrefix}connect4\`
\`${container.botPrefix}connect 4 @user\``,

isCommandResponseEphemeral: false,
messageWhenExecutingCommand: 'Setting up your Connect 4 game...',
executeCommand: connectFourExecuteCommand,
afterMessageReply: connectFourAfterMessageReply,
options: [],
subcommandDetails: {},
};
16 changes: 16 additions & 0 deletions src/commands/games/connectFour.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Command } from '@sapphire/framework';
import { CodeyCommand } from '../../codeyCommand';
import { connectFourCommandDetails } from '../../commandDetails/games/connectFour';

export class GamesConnectFourCommand extends CodeyCommand {
details = connectFourCommandDetails;

public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
aliases: connectFourCommandDetails.aliases,
description: connectFourCommandDetails.description,
detailedDescription: connectFourCommandDetails.detailedDescription,
});
}
}
18 changes: 18 additions & 0 deletions src/components/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ const initRpsGameInfo = async (db: Database): Promise<void> => {
);
};

const initConnectFourGameInfo = async (db: Database): Promise<void> => {
// If player 2 ID is null, the game was against Codey
await db.run(
`
CREATE TABLE IF NOT EXISTS connect_four_game_info (
id INTEGER PRIMARY KEY NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
player1_id VARCHAR(30) NOT NULL,
player2_id VARCHAR(30),
player1_sign INTEGER NOT NULL DEFAULT 0,
player2_sign INTEGER NOT NULL DEFAULT 0,
status INTEGER NOT NULL DEFAULT 0
)
`,
);
};

const initResumePreview = async (db: Database): Promise<void> => {
await db.run(
`
Expand Down Expand Up @@ -215,6 +232,7 @@ const initTables = async (db: Database): Promise<void> => {
await initUserCoinTable(db);
await initUserProfileTable(db);
await initRpsGameInfo(db);
await initConnectFourGameInfo(db);
await initResumePreview(db);
await initCompaniesTable(db);
await initPeopleCompaniesTable(db);
Expand Down
Loading

0 comments on commit 8b98cc5

Please sign in to comment.