From f817c51ade43b05887a098182abc00b76e2615cd Mon Sep 17 00:00:00 2001 From: Geoffrey Wu Date: Fri, 23 Dec 2022 12:38:14 -0600 Subject: [PATCH] fix issue with 0 length words --- client/singleplayer/tossups.js | 13 ++++++++----- server/Room.js | 23 +++++++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/client/singleplayer/tossups.js b/client/singleplayer/tossups.js index 53e50ef9c..6ed95338d 100644 --- a/client/singleplayer/tossups.js +++ b/client/singleplayer/tossups.js @@ -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 { @@ -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 { @@ -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') @@ -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; } diff --git a/server/Room.js b/server/Room.js index 17ab17788..f69e96711 100644 --- a/server/Room.js +++ b/server/Room.js @@ -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) { @@ -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; } @@ -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; @@ -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) @@ -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() {