Skip to content

Commit

Permalink
implement singleplayer features
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Oct 4, 2024
1 parent b60120c commit d005308
Show file tree
Hide file tree
Showing 14 changed files with 1,191 additions and 658 deletions.
2 changes: 1 addition & 1 deletion client/database/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DifficultyDropdown from '../scripts/components/DifficultyDropdown.min.js'
import Star from '../scripts/components/Star.min.js';
import CategoryManager from '../scripts/utilities/category-manager.js';
import { getDropdownValues } from '../scripts/utilities/dropdown-checklist.js';
import { insertTokensIntoHTML } from '../scripts/utilities/insert-tokens-into-html.js';
import { insertTokensIntoHTML } from '../insert-tokens-into-html.js';

const starredTossupIds = new Set(await star.getStarredTossupIds());
const starredBonusIds = new Set(await star.getStarredBonusIds());
Expand Down
2 changes: 1 addition & 1 deletion client/database/index.min.js

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions client/multiplayer/room.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ function lostBuzzerRace ({ username, userId }) {
if (userId === USER_ID) { document.getElementById('answer-input-group').classList.add('d-none'); }
}

function next ({ tossup: nextTossup, type, username }) {
function next ({ oldTossup, tossup: nextTossup, type, username }) {
switch (type) {
case 'next':
logEvent(username, 'went to the next question');
Expand All @@ -468,9 +468,7 @@ function next ({ tossup: nextTossup, type, username }) {
}

if (type === 'next' || type === 'skip') {
tossup.question = document.getElementById('question').innerHTML;
tossup.answer = document.getElementById('answer').innerHTML.replace('ANSWER: ', '');
createTossupCard(tossup);
createTossupCard(oldTossup);
} else if (type === 'start') {
document.getElementById('next').classList.add('btn-primary');
document.getElementById('next').classList.remove('btn-success');
Expand Down
2 changes: 1 addition & 1 deletion client/multiplayer/room.min.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions client/scripts/utilities/category-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ export default class CategoryManager {
return {
categories: this.categories,
subcategories: this.subcategories,
alternateSubcategories: this.alternateSubcategories
alternateSubcategories: this.alternateSubcategories,
percentView: this.percentView,
categoryPercents: this.categoryPercents
};
}

import (categories = [], subcategories = [], alternateSubcategories = []) {
import (categories = [], subcategories = [], alternateSubcategories = [], percentView = false, categoryPercents = []) {
if (categories.length > 0 && subcategories.length === 0) {
categories.forEach(category => {
SUBCATEGORIES[category].forEach(subcategory => {
Expand Down
77 changes: 77 additions & 0 deletions client/singleplayer/ClientTossupRoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import api from '../scripts/api/index.js';
// import TossupRoom from '../../quizbowl/TossupRoom.js';
import TossupRoom from '../TossupRoom.js';

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

this.previous = {
celerity: 0,
endOfQuestion: false,
isCorrect: true,
inPower: false,
negValue: -5,
powerValue: 15,
tossup: {}
};
this.settings.skip = true;

this.checkAnswer = api.checkAnswer;
this.getRandomTossups = async (args) => await api.getRandomTossup({ number: 20, ...args });
this.getSet = async ({ setName, packetNumbers }) => setName ? await api.getPacketTossups(setName, packetNumbers[0] ?? 1) : [];
this.getSetList = api.getSetList;
this.getNumPackets = api.getNumPackets;

this.setList = this.getSetList().concat('');
}

async message (userId, message) {
switch (message.type) {
case 'toggle-correct': return this.toggleCorrect(userId, message);
default: super.message(userId, message);
}
}

get liveAnswer () {
return document.getElementById('answer-input').value;
}

set liveAnswer (value) {
document.getElementById('answer-input').value = value;
}

async scoreTossup ({ givenAnswer }) {
const { celerity, directive, directedPrompt, endOfQuestion, inPower, points } = await super.scoreTossup({ givenAnswer });
this.previous.celerity = celerity;
this.previous.endOfQuestion = endOfQuestion;
this.previous.isCorrect = points > 0;
this.previous.inPower = inPower;
this.previous.tossup = this.tossup;
return { celerity, directive, directedPrompt, points };
}

toggleCorrect (userId, { correct }) {
const multiplier = correct ? 1 : -1;

if (this.previous.inPower) {
this.players[userId].powers += multiplier * 1;
this.players[userId].points += multiplier * this.previous.powerValue;
} else {
this.players[userId].tens += multiplier * 1;
this.players[userId].points += multiplier * 10;
}

if (this.previous.endOfQuestion) {
this.players[userId].dead += multiplier * -1;
} else {
this.players[userId].negs += multiplier * -1;
this.players[userId].points += multiplier * -this.previous.negValue;
}

this.players[userId].celerity.correct.total += multiplier * this.previous.celerity;
this.players[userId].celerity.correct.average = this.players[userId].celerity.correct.total / (this.players[userId].powers + this.players[userId].tens);

this.emitMessage({ type: 'toggle-correct', correct, userId });
}
}
Loading

0 comments on commit d005308

Please sign in to comment.