Skip to content

Commit

Permalink
feat(kusari/game): Add ability to start game from a custom word/letter
Browse files Browse the repository at this point in the history
  • Loading branch information
jurienhamaker committed Jul 14, 2024
1 parent f27c33e commit 47193c7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
48 changes: 42 additions & 6 deletions apps/kusari/src/modules/game/commands/start.commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable, UseFilters, UseGuards } from '@nestjs/common';
import { GameType } from '@prisma/kusari';
import { noSettingsReply } from '../../../util/no-settings-reply';
import { ForbiddenExceptionFilter, GuildModeratorGuard } from '@yugen/shared';
import { Client, CommandInteraction } from 'discord.js';
import {
Expand All @@ -10,9 +9,10 @@ import {
StringOption,
Subcommand,
} from 'necord';
import { noSettingsReply } from '../../../util/no-settings-reply';
import { SettingsService } from '../../settings';
import { GameCommandDecorator } from '../game.decorator';
import { GameService } from '../services/game.service';
import { SettingsService } from '../../settings';

const GameStartOptionsChoices = [
{
Expand All @@ -28,6 +28,22 @@ class GameStartOptions {
choices: GameStartOptionsChoices,
})
type: GameType;

@StringOption({
name: 'starting-word',
required: false,
description: 'The word the game starts with.',
})
startingWord: string;
}

class GameResetOptions {
@StringOption({
name: 'starting-word',
required: false,
description: 'The word the game starts with.',
})
startingWord: string;
}

@UseGuards(GuildModeratorGuard)
Expand All @@ -47,37 +63,57 @@ export class GameStartCommands {
})
public async start(
@Context() [interaction]: SlashCommandContext,
@Options() { type }: GameStartOptions,
@Options() { type, startingWord }: GameStartOptions,
) {
return this._startGame(interaction, type ?? GameType.NORMAL);
return this._startGame(
interaction,
type ?? GameType.NORMAL,
false,
startingWord,
);
}

@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() { startingWord }: GameResetOptions,
) {
const currentGame = await this._game.getCurrentGame(
interaction.guildId,
);
return this._startGame(interaction, currentGame.type, true);
return this._startGame(
interaction,
currentGame.type,
true,
startingWord,
);
}

private async _startGame(
interaction: CommandInteraction,
type: GameType = GameType.NORMAL,
recreate: boolean = false,
startingWord: string | undefined,
) {
const settings = await this._settings.getSettings(interaction.guildId);

if (!settings.channelId) {
return noSettingsReply(interaction, this._client);
}

let letter: string;
if (startingWord) {
letter = startingWord[startingWord.length - 1];
}

const started = await this._game.start(
interaction.guildId,
type,
recreate,
letter,
);

return interaction.reply({
Expand Down
5 changes: 4 additions & 1 deletion apps/kusari/src/modules/game/services/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class GameService {
guildId: string,
type: GameType = GameType.NORMAL,
recreate = false,
letter?: string,
) {
this._logger.log(`Trying to start a game for ${guildId}`);

Expand All @@ -45,7 +46,9 @@ export class GameService {
await this.endGame(currentGame.id, GameStatus.FAILED);
}

const letter = this._getRandomLetter();
if (!letter) {
letter = this._getRandomLetter();
}

await this._prisma.game.create({
data: {
Expand Down

0 comments on commit 47193c7

Please sign in to comment.