Skip to content

Commit

Permalink
refactor QuestionProgressEnum into a shared constant
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Jan 15, 2025
1 parent d06a48c commit 1a1ffde
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
20 changes: 7 additions & 13 deletions quizbowl/BonusRoom.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { ANSWER_TIME_LIMIT } from './constants.js';
import { ANSWER_TIME_LIMIT, BONUS_PROGRESS_ENUM } from './constants.js';
import QuestionRoom from './QuestionRoom.js';

export default class BonusRoom extends QuestionRoom {
constructor (name, categories = [], subcategories = [], alternateSubcategories = []) {
super(name, categories, subcategories, alternateSubcategories);

this.QuestionProgressEnum = Object.freeze({
NOT_STARTED: 0,
READING: 1,
LAST_PART_REVEALED: 2
});

this.bonus = {};
this.bonusProgress = BONUS_PROGRESS_ENUM.NOT_STARTED;
/**
* 0-indexed variable that tracks current part of the bonus being read
*/
Expand All @@ -21,7 +16,6 @@ export default class BonusRoom extends QuestionRoom {
* @type {number[]}
*/
this.pointsPerPart = [];
this.queryingQuestion = false;

this.query = {
threePartBonuses: true,
Expand Down Expand Up @@ -78,7 +72,7 @@ export default class BonusRoom extends QuestionRoom {

async next (userId, { type }) {
if (this.queryingQuestion) { return false; }
if (this.questionProgress === this.QuestionProgressEnum.READING && !this.settings.skip) { return false; }
if (this.bonusProgress === BONUS_PROGRESS_ENUM.READING && !this.settings.skip) { return false; }

clearInterval(this.timer.interval);
this.emitMessage({ type: 'timer-update', timeRemaining: 0 });
Expand All @@ -97,7 +91,7 @@ export default class BonusRoom extends QuestionRoom {
this.emitMessage({
type,
bonus: this.bonus,
lastPartRevealed: this.questionProgress === this.QuestionProgressEnum.LAST_PART_REVEALED,
lastPartRevealed: this.bonusProgress === BONUS_PROGRESS_ENUM.LAST_PART_REVEALED,
oldBonus,
packetLength: this.packetLength,
pointsPerPart: this.pointsPerPart,
Expand All @@ -107,7 +101,7 @@ export default class BonusRoom extends QuestionRoom {

this.currentPartNumber = -1;
this.pointsPerPart = [];
this.questionProgress = this.QuestionProgressEnum.READING;
this.bonusProgress = BONUS_PROGRESS_ENUM.READING;
this.revealLeadin();
this.revealNextPart();
}
Expand All @@ -119,7 +113,7 @@ export default class BonusRoom extends QuestionRoom {
revealNextAnswer () {
const lastPartRevealed = this.currentPartNumber === this.bonus.parts.length - 1;
if (lastPartRevealed) {
this.questionProgress = this.QuestionProgressEnum.LAST_PART_REVEALED;
this.bonusProgress = BONUS_PROGRESS_ENUM.LAST_PART_REVEALED;
}
this.emitMessage({
type: 'reveal-next-answer',
Expand All @@ -130,7 +124,7 @@ export default class BonusRoom extends QuestionRoom {
}

revealNextPart () {
if (this.questionProgress === this.QuestionProgressEnum.LAST_PART_REVEALED) { return; }
if (this.bonusProgress === BONUS_PROGRESS_ENUM.LAST_PART_REVEALED) { return; }

this.currentPartNumber++;
this.emitMessage({
Expand Down
22 changes: 8 additions & 14 deletions quizbowl/TossupRoom.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { ANSWER_TIME_LIMIT, DEAD_TIME_LIMIT } from './constants.js';
import { ANSWER_TIME_LIMIT, DEAD_TIME_LIMIT, TOSSUP_PROGRESS_ENUM } from './constants.js';
import insertTokensIntoHTML from './insert-tokens-into-html.js';
import QuestionRoom from './QuestionRoom.js';

export default class TossupRoom extends QuestionRoom {
constructor (name, categories = [], subcategories = [], alternateSubcategories = []) {
super(name, categories, subcategories, alternateSubcategories);

this.QuestionProgressEnum = Object.freeze({
NOT_STARTED: 0,
READING: 1,
ANSWER_REVEALED: 2
});

this.timeoutID = null;
/**
* @type {string | null}
Expand All @@ -23,9 +17,9 @@ export default class TossupRoom extends QuestionRoom {
this.buzzpointIndices = [];
this.liveAnswer = '';
this.paused = false;
this.questionProgress = this.QuestionProgressEnum.NOT_STARTED;
this.questionSplit = [];
this.tossup = {};
this.tossupProgress = TOSSUP_PROGRESS_ENUM.NOT_STARTED;
this.wordIndex = 0;

this.query = {
Expand Down Expand Up @@ -71,7 +65,7 @@ export default class TossupRoom extends QuestionRoom {

buzz (userId) {
if (!this.settings.rebuzz && this.buzzes.includes(userId)) { return; }
if (this.questionProgress !== this.QuestionProgressEnum.READING) { return; }
if (this.tossupProgress !== TOSSUP_PROGRESS_ENUM.READING) { return; }

const username = this.players[userId].username;
if (this.buzzedIn) {
Expand Down Expand Up @@ -161,7 +155,7 @@ export default class TossupRoom extends QuestionRoom {
async next (userId, { type }) {
if (this.buzzedIn) { return false; } // prevents skipping when someone has buzzed in
if (this.queryingQuestion) { return false; }
if (this.questionProgress === this.QuestionProgressEnum.READING && !this.settings.skip) { return false; }
if (this.tossupProgress === TOSSUP_PROGRESS_ENUM.READING && !this.settings.skip) { return false; }

clearInterval(this.timer.interval);
this.emitMessage({ type: 'timer-update', timeRemaining: 0 });
Expand All @@ -172,7 +166,7 @@ export default class TossupRoom extends QuestionRoom {
this.buzzpointIndices = [];
this.paused = false;

if (this.questionProgress !== this.QuestionProgressEnum.ANSWER_REVEALED) { this.revealQuestion(); }
if (this.tossupProgress !== TOSSUP_PROGRESS_ENUM.ANSWER_REVEALED) { this.revealQuestion(); }

const oldTossup = this.tossup;
this.tossup = await this.advanceQuestion({ lastSeenQuestion: oldTossup });
Expand All @@ -184,13 +178,13 @@ export default class TossupRoom extends QuestionRoom {
this.emitMessage({ type, packetLength: this.packetLength, userId, username, oldTossup, tossup: this.tossup });

this.wordIndex = 0;
this.questionProgress = this.QuestionProgressEnum.READING;
this.tossupProgress = TOSSUP_PROGRESS_ENUM.READING;
this.readQuestion(Date.now());
}

pause (userId) {
if (this.buzzedIn) { return false; }
if (this.questionProgress === this.QuestionProgressEnum.ANSWER_REVEALED) { return false; }
if (this.tossupProgress === TOSSUP_PROGRESS_ENUM.ANSWER_REVEALED) { return false; }

this.paused = !this.paused;
if (this.paused) {
Expand Down Expand Up @@ -277,7 +271,7 @@ export default class TossupRoom extends QuestionRoom {
revealQuestion () {
if (Object.keys(this.tossup || {}).length === 0) return;

this.questionProgress = this.QuestionProgressEnum.ANSWER_REVEALED;
this.tossupProgress = TOSSUP_PROGRESS_ENUM.ANSWER_REVEALED;
this.emitMessage({
type: 'reveal-answer',
question: insertTokensIntoHTML(this.tossup.question, this.tossup.question_sanitized, [this.buzzpointIndices], [' (#) ']),
Expand Down
12 changes: 12 additions & 0 deletions quizbowl/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export const MODE_ENUM = Object.freeze({
RANDOM: 'random questions',
STARRED: 'starred questions'
});

export const BONUS_PROGRESS_ENUM = Object.freeze({
NOT_STARTED: 0,
READING: 1,
LAST_PART_REVEALED: 2
});

export const TOSSUP_PROGRESS_ENUM = Object.freeze({
NOT_STARTED: 0,
READING: 1,
ANSWER_REVEALED: 2
});
8 changes: 4 additions & 4 deletions server/multiplayer/ServerTossupRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ServerPlayer from './ServerPlayer.js';
import Votekick from './VoteKick.js';
import { HEADER, ENDC, OKBLUE, OKGREEN } from '../bcolors.js';
import isAppropriateString from '../moderation/is-appropriate-string.js';
import { MODE_ENUM, TOSSUP_PROGRESS_ENUM } from '../../quizbowl/constants.js';
import insertTokensIntoHTML from '../../quizbowl/insert-tokens-into-html.js';
import TossupRoom from '../../quizbowl/TossupRoom.js';
import RateLimit from '../RateLimit.js';
Expand All @@ -12,7 +13,6 @@ import getSetList from '../../database/qbreader/get-set-list.js';
import getNumPackets from '../../database/qbreader/get-num-packets.js';

import checkAnswer from 'qb-answer-checker';
import { MODE_ENUM } from '../../quizbowl/constants.js';

const BAN_DURATION = 1000 * 60 * 30; // 30 minutes

Expand Down Expand Up @@ -119,22 +119,22 @@ export default class ServerTossupRoom extends TossupRoom {
buzzedIn: this.buzzedIn,
canBuzz: this.settings.rebuzz || !this.buzzes.includes(userId),
mode: this.mode,
questionProgress: this.questionProgress,
questionProgress: this.tossupProgress,

settings: this.settings
}));

socket.send(JSON.stringify({ type: 'connection-acknowledged-query', ...this.query, ...this.categoryManager.export() }));
socket.send(JSON.stringify({ type: 'connection-acknowledged-tossup', tossup: this.tossup }));

if (this.questionProgress === this.QuestionProgressEnum.READING) {
if (this.tossupProgress === TOSSUP_PROGRESS_ENUM.READING) {
socket.send(JSON.stringify({
type: 'update-question',
word: this.questionSplit.slice(0, this.wordIndex).join(' ')
}));
}

if (this.questionProgress === this.QuestionProgressEnum.ANSWER_REVEALED && this.tossup?.answer) {
if (this.tossupProgress === TOSSUP_PROGRESS_ENUM.ANSWER_REVEALED && this.tossup?.answer) {
socket.send(JSON.stringify({
type: 'reveal-answer',
question: insertTokensIntoHTML(this.tossup.question, this.tossup.question_sanitized, [this.buzzpointIndices], [' (#) ']),
Expand Down

0 comments on commit 1a1ffde

Please sign in to comment.