Skip to content

Commit

Permalink
Fix bug where correct guess count wasn't correctly tracked
Browse files Browse the repository at this point in the history
  • Loading branch information
taahamahdi committed Dec 1, 2023
1 parent 233b1c1 commit 3a88899
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/structures/game_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,13 @@ export default class GameSession extends Session {
this.scoreboard.getPlayerIDs().map(async (participant) => {
await this.ensurePlayerStat(participant);
await GameSession.incrementPlayerGamesPlayed(participant);
const playerScore = this.scoreboard.getPlayerScore(participant);
if (playerScore > 0) {
const playerCorrectGuessCount =
this.scoreboard.getPlayerCorrectGuessCount(participant);

if (playerCorrectGuessCount > 0) {
await GameSession.incrementPlayerSongsGuessed(
participant,
1,
playerCorrectGuessCount,
);
}

Expand All @@ -527,6 +529,8 @@ export default class GameSession extends Session {
}
}

const playerScore = this.scoreboard.getPlayerScore(participant);

await GameSession.insertPerSessionStats(
participant,
playerScore,
Expand Down
16 changes: 16 additions & 0 deletions src/structures/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default class Player {
/** The player's current score */
protected score: number;

/** The number of correct guesses the player has made */
private correctGuessCount: number;

/** The player's avatar URL */
private readonly avatarURL: string | null;

Expand All @@ -48,6 +51,7 @@ export default class Player {
this.guildID = guildID;
this.avatarURL = avatarURL;
this.score = points;
this.correctGuessCount = 0;
this.username = username;
this.firstGameOfTheDay = firstGameOfTheDay;
this.premium = premium;
Expand Down Expand Up @@ -120,6 +124,11 @@ export default class Player {
return this.score;
}

/** @returns how many correct guesses the player has made */
getCorrectGuessCount(): number {
return this.correctGuessCount;
}

/*
* @param boldScore - whether to display the score in bold
* @returns what to display as the score in the scoreboard for the player
Expand Down Expand Up @@ -152,6 +161,13 @@ export default class Player {
this.score += pointsEarned;
}

/**
* Increments the player's correct guess count by 1
*/
incrementCorrectGuessCount(): void {
this.correctGuessCount += 1;
}

/**
* Increment the player's EXP gain by the specified amount
* @param expGain - The amount of EXP that was gained
Expand Down
11 changes: 10 additions & 1 deletion src/structures/scoreboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export default class Scoreboard {
guessResult.pointsEarned,
);

this.players[guessResult.userID].incrementCorrectGuessCount();
this.players[guessResult.userID].incrementExp(guessResult.expGain);
const winnerScore = this.players[guessResult.userID].getScore();

const winnerScore = this.players[guessResult.userID].getScore();
if (winnerScore === this.highestScore) {
// If user is tied for first, add them to the first place array
this.firstPlace.push(this.players[guessResult.userID]);
Expand Down Expand Up @@ -158,6 +159,14 @@ export default class Scoreboard {
return this.players[userID]?.getExpGain() ?? 0;
}

/**
* @param userID - The Discord user ID of the player to check
* @returns the number of correct guesses by the player
*/
getPlayerCorrectGuessCount(userID: string): number {
return this.players[userID]?.getCorrectGuessCount() ?? 0;
}

/**
* @param guildPreference - The GuildPreference
* @returns whether the game has completed
Expand Down

0 comments on commit 3a88899

Please sign in to comment.