Skip to content

Commit

Permalink
feat(kazu/game): Add ability to start game from a custom number
Browse files Browse the repository at this point in the history
  • Loading branch information
jurienhamaker committed Jul 14, 2024
1 parent 7e6c391 commit f27c33e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
39 changes: 35 additions & 4 deletions apps/kazu/src/modules/game/commands/start.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ForbiddenExceptionFilter, GuildModeratorGuard } from '@yugen/shared';
import { Client, CommandInteraction } from 'discord.js';
import {
Context,
NumberOption,
Options,
SlashCommandContext,
StringOption,
Expand All @@ -28,6 +29,22 @@ class GameStartOptions {
choices: GameStartOptionsChoices,
})
type: GameType;

@NumberOption({
name: 'starting-number',
description: 'The number to start the game at',
required: false,
})
startingNumber: number;
}

class GameResetOptions {
@NumberOption({
name: 'starting-number',
description: 'The number to start the game at',
required: false,
})
startingNumber: number;
}

@UseGuards(GuildModeratorGuard)
Expand All @@ -47,25 +64,38 @@ export class GameStartCommands {
})
public async start(
@Context() [interaction]: SlashCommandContext,
@Options() { type }: GameStartOptions,
@Options() { type, startingNumber }: GameStartOptions,
) {
return this._startGame(interaction, type ?? GameType.NORMAL);
return this._startGame(
interaction,
type ?? GameType.NORMAL,
startingNumber ?? 1,
);
}

@Subcommand({
name: 'reset',
description: 'Reset the current game and any points earned.',
})
public async reset(@Context() [interaction]: SlashCommandContext) {
public async reset(
@Context() [interaction]: SlashCommandContext,
@Options() { startingNumber }: GameResetOptions,
) {
const currentGame = await this._game.getCurrentGame(
interaction.guildId,
);
return this._startGame(interaction, currentGame.type, true);
return this._startGame(
interaction,
currentGame.type,
startingNumber ?? 1,
true,
);
}

private async _startGame(
interaction: CommandInteraction,
type: GameType = GameType.NORMAL,
startingNumber: number = 0,
recreate: boolean = false,
) {
const settings = await this._settings.getSettings(interaction.guildId);
Expand All @@ -77,6 +107,7 @@ export class GameStartCommands {
const started = await this._game.start(
interaction.guildId,
type,
startingNumber,
recreate,
);

Expand Down
8 changes: 5 additions & 3 deletions apps/kazu/src/modules/game/services/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class GameService {
async start(
guildId: string,
type: GameType = GameType.NORMAL,
startingNumber: number = 1,
recreate = false,
shamedData?: {
message: Message;
Expand Down Expand Up @@ -48,13 +49,14 @@ export class GameService {
await this.endGame(currentGame.id, GameStatus.FAILED, shamedData);
}

const number = startingNumber - 1;
await this._prisma.game.create({
data: {
guildId,
type,
history: {
create: {
number: 0,
number: number >= 0 ? number : 0,
userId: this._client.user.id,
},
},
Expand All @@ -63,7 +65,7 @@ export class GameService {

if (channel.type === ChannelType.GuildText) {
channel.send(`**A new game has started!**
Start the count from **1**`);
Start the count from **${startingNumber}**`);
}

return true;
Expand Down Expand Up @@ -145,7 +147,7 @@ Used **1 server** save, There are **${saves}/${maxSaves}** server saves left.`);
**Want to save the game?** Make sure to **/vote** for Kazu and earn yourself saves to save the game!`,
);

return this.start(guildId, game.type, true, {
return this.start(guildId, game.type, 1, true, {
message,
lastShameUserId: settings.lastShameUserId,
roleId: settings.shameRoleId,
Expand Down

0 comments on commit f27c33e

Please sign in to comment.