Skip to content

Commit

Permalink
Trivia: Fix crash and Number mode game cap (#10753)
Browse files Browse the repository at this point in the history
* Trivia: Fix crash and Number mode game cap

Discussed with Trivia auth.

Fixes an issue in Number mode where the game fails to end after a specified amount of questions, and fixes a crash caused by ending a game with no participation

* Update server/chat-plugins/trivia/trivia.ts

Co-authored-by: Kris Johnson <[email protected]>

---------

Co-authored-by: Kris Johnson <[email protected]>
  • Loading branch information
dot-Comfey and KrisXV authored Jan 4, 2025
1 parent b790eb8 commit 69eac38
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions server/chat-plugins/trivia/trivia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ export class Trivia extends Rooms.RoomGame<TriviaPlayer> {
const prizes = this.getPrizes();
const [p1, p2, p3] = winners;

if (!p1) return `No winners this game!`;
let initialPart = this.room.tr`${Utils.escapeHTML(p1.name)} won the game with a final score of <strong>${p1.player.points}</strong>`;
if (!this.game.givesPoints) {
return `${initialPart}.`;
Expand All @@ -847,18 +848,25 @@ export class Trivia extends Rooms.RoomGame<TriviaPlayer> {

getStaffEndMessage(winners: TopPlayer[], mapper: (k: TopPlayer) => string) {
let message = "";
const winnerParts: ((k: TopPlayer) => string)[] = [
winner => this.room.tr`User ${mapper(winner)} won the game of ` +
if (winners.length) {
const winnerParts: ((k: TopPlayer) => string)[] = [
winner => this.room.tr`User ${mapper(winner)} won the game of ` +
(this.game.givesPoints ? this.room.tr`ranked ` : this.room.tr`unranked `) +
this.room.tr`${this.game.mode} mode trivia under the ${this.game.category} category with ` +
this.room.tr`a cap of ${this.getDisplayableCap()} ` +
this.room.tr`with ${winner.player.points} points and ` +
this.room.tr`${winner.player.correctAnswers} correct answers`,
winner => this.room.tr` Second place: ${mapper(winner)} (${winner.player.points} points)`,
winner => this.room.tr`, third place: ${mapper(winner)} (${winner.player.points} points)`,
];
for (const [i, winner] of winners.entries()) {
message += winnerParts[i](winner);
}
} else {
message = `No participants in the game of ` +
(this.game.givesPoints ? this.room.tr`ranked ` : this.room.tr`unranked `) +
this.room.tr`${this.game.mode} mode trivia under the ${this.game.category} category with ` +
this.room.tr`a cap of ${this.getDisplayableCap()} ` +
this.room.tr`with ${winner.player.points} points and ` +
this.room.tr`${winner.player.correctAnswers} correct answers`,
winner => this.room.tr` Second place: ${mapper(winner)} (${winner.player.points} points)`,
winner => this.room.tr`, third place: ${mapper(winner)} (${winner.player.points} points)`,
];
for (let i = 0; i < winners.length; i++) {
message += winnerParts[i](winners[i]);
this.room.tr`a cap of ${this.getDisplayableCap()}`;
}
return `${message}`;
}
Expand Down Expand Up @@ -1088,11 +1096,9 @@ export class NumberModeTrivia extends Trivia {
);

const points = this.calculatePoints(innerBuffer.length);
let winner = false;
const cap = this.getCap();
let winner = cap.questions && this.questionNumber >= cap.questions;
if (points) {
const cap = this.getCap();
// We add 1 questionNumber because it starts at 0
winner = !!cap.questions && this.questionNumber >= cap.questions;
for (const userid in this.playerTable) {
const player = this.playerTable[userid];
if (player.isCorrect) player.incrementPoints(points, this.questionNumber);
Expand Down

0 comments on commit 69eac38

Please sign in to comment.