Skip to content

Commit

Permalink
feat(kusari): Add highscore emoji when game has reached a highscore
Browse files Browse the repository at this point in the history
  • Loading branch information
jurienhamaker committed Jul 15, 2024
1 parent 7589099 commit 7b8d90b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
37 changes: 31 additions & 6 deletions apps/kusari/src/modules/game/services/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,16 @@ Used **1 of your own** saves, You have **${saves}/2** saves left.`);
Used **1 server** save, There are **${saves}/${maxSaves}** server saves left.`);
}

const highscore = await this._checkStreak(settings, count);
const { isHighscore } = await this._checkStreak(
settings,
game,
count,
);

await message.reply(
`${failReason}
**The game has ended on a streak of ${count}!**${
highscore
isHighscore
? `
**A new highscore has been set! 🎉**`
: ''
Expand Down Expand Up @@ -204,8 +208,16 @@ Used **1 server** save, There are **${saves}/${maxSaves}** server saves left.`);
});

const count = await this._getCount(game.id);
const highscore = await this._checkStreak(settings, count);
await message.react(highscore ? '☑️' : '✅').catch(() => null);
const { isHighscore, isGameHighscored } = await this._checkStreak(
settings,
game,
count,
);
if (isGameHighscored) {
await message.react('🎉').catch(() => null);
}

await message.react(isHighscore ? '☑️' : '✅').catch(() => null);

this._setNumber(message, count);
}
Expand Down Expand Up @@ -268,8 +280,9 @@ Used **1 server** save, There are **${saves}/${maxSaves}** server saves left.`);
});
}

private async _checkStreak(settings: Settings, count: number) {
private async _checkStreak(settings: Settings, game: Game, count: number) {
let isHighscore = false;
let isGameHighscored = false;
if (count > settings.highscore) {
isHighscore = true;
await this._settings.set(settings.guildId, 'highscore', count);
Expand All @@ -278,9 +291,21 @@ Used **1 server** save, There are **${saves}/${maxSaves}** server saves left.`);
'highscoreDate',
new Date(),
);

if (!game.isHighscored) {
isGameHighscored = true;
await this._prisma.game.update({
where: {
id: game.id,
},
data: {
isHighscored: true,
},
});
}
}

return isHighscore;
return { isHighscore, isGameHighscored };
}

private async _setNumber(message: Message, count: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Game" ADD COLUMN "isHighscored" BOOLEAN NOT NULL DEFAULT false;
6 changes: 4 additions & 2 deletions libs/prisma/kusari/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ model Game {
id Int @id @default(autoincrement())
guildId String
lastMessageId String?
settings Settings @relation(fields: [guildId], references: [guildId], onDelete: Cascade, onUpdate: Cascade)
history History[]
status GameStatus @default(IN_PROGRESS)
type GameType @default(NORMAL)
isHighscored Boolean @default(false)
// timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -85,4 +87,4 @@ enum GameStatus {

enum GameType {
NORMAL
}
}

0 comments on commit 7b8d90b

Please sign in to comment.