Skip to content

Commit

Permalink
fix issue with 0 length words
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Dec 23, 2022
1 parent 5720881 commit f817c51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
13 changes: 8 additions & 5 deletions client/singleplayer/tossups.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function advanceQuestion() {

if (questions.length > 0) {
questionText = questions[questionNumber]['question'];
questionTextSplit = questionText.split(' ');
questionTextSplit = questionText.split(' ').filter(word => word !== '');
document.getElementById('question-number-info').innerHTML = questionNumber + 1;
}
} else {
Expand All @@ -66,7 +66,7 @@ async function advanceQuestion() {

if (questions.length > 0) {
questionText = questions[0]['question'];
questionTextSplit = questionText.split(' ');
questionTextSplit = questionText.split(' ').filter(word => word !== '');
document.getElementById('question-number-info').innerHTML = questionNumber;
questionNumber = 0;
} else {
Expand Down Expand Up @@ -203,7 +203,7 @@ function readQuestion(expectedReadTime) {
const word = questionTextSplit.shift();
document.getElementById('question').innerHTML += word + ' ';

//calculate time needed before reading next word
// calculate time needed before reading next word
let time = Math.log(word.length) + 1;
if ((word.endsWith('.') && word.charCodeAt(word.length - 2) > 96 && word.charCodeAt(word.length - 2) < 123)
|| word.slice(-2) === '.\u201d' || word.slice(-2) === '!\u201d' || word.slice(-2) === '?\u201d')
Expand All @@ -213,9 +213,12 @@ function readQuestion(expectedReadTime) {
else if (word === '(*)')
time = 0;

time = time * 0.9 * (125 - document.getElementById('reading-speed').value);
const delay = time - new Date().getTime() + expectedReadTime;

timeoutID = window.setTimeout(() => {
readQuestion(time * 0.9 * (125 - document.getElementById('reading-speed').value) + expectedReadTime);
}, time * 0.9 * (125 - document.getElementById('reading-speed').value) - new Date().getTime() + expectedReadTime);
readQuestion(time + expectedReadTime);
}, delay);
} else {
document.getElementById('pause').disabled = true;
}
Expand Down
23 changes: 13 additions & 10 deletions server/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ class Room {
}

async advanceQuestion() {
this.queryingQuestion = true;
this.wordIndex = 0;
this.buzzedIn = null;
this.paused = false;
this.queryingQuestion = true;
this.wordIndex = 0;

if (this.settings.selectBySetName) {
if (this.setCache.length === 0) {
Expand Down Expand Up @@ -307,7 +307,7 @@ class Room {
}

this.questionProgress = 1;
this.questionSplit = this.tossup.question.split(' ');
this.questionSplit = this.tossup.question.split(' ').filter(word => word !== '');
return true;
}

Expand Down Expand Up @@ -421,7 +421,7 @@ class Room {
});
}

readQuestion(expectedReadTime) {
async readQuestion(expectedReadTime) {
if (Object.keys(this.tossup).length === 0) return;
if (this.wordIndex >= this.questionSplit.length) {
return;
Expand All @@ -430,6 +430,11 @@ class Room {
const word = this.questionSplit[this.wordIndex];
this.wordIndex++;

this.sendSocketMessage({
type: 'update-question',
word: word
});

// calculate time needed before reading next word
let time = Math.log(word.length) + 1;
if ((word.endsWith('.') && word.charCodeAt(word.length - 2) > 96 && word.charCodeAt(word.length - 2) < 123)
Expand All @@ -440,14 +445,12 @@ class Room {
else if (word === '(*)')
time = 0;

this.sendSocketMessage({
type: 'update-question',
word: word
});
time = time * 0.9 * (125 - this.settings.readingSpeed);
const delay = time - new Date().getTime() + expectedReadTime;

this.timeoutID = setTimeout(() => {
this.readQuestion(time * 0.9 * (125 - this.settings.readingSpeed) + expectedReadTime);
}, time * 0.9 * (125 - this.settings.readingSpeed) - new Date().getTime() + expectedReadTime);
this.readQuestion(time + expectedReadTime);
}, delay);
}

revealQuestion() {
Expand Down

0 comments on commit f817c51

Please sign in to comment.