Skip to content

Commit

Permalink
Merge pull request #116 from BLCRAFT210/main
Browse files Browse the repository at this point in the history
Compensate for setTimeout lag
  • Loading branch information
geoffrey-wu authored Dec 22, 2022
2 parents 28f49bf + e18ac96 commit 5720881
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions client/singleplayer/tossups.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async function next() {
document.getElementById('set-name-info').innerHTML = setName;

paused = false;
readQuestion();
readQuestion(new Date().getTime());
}


Expand All @@ -184,7 +184,7 @@ function pause() {
if (paused) {
document.getElementById('buzz').removeAttribute('disabled');
document.getElementById('pause').innerHTML = 'Pause';
readQuestion();
readQuestion(new Date().getTime());
}
else {
document.getElementById('buzz').setAttribute('disabled', 'disabled');
Expand All @@ -198,7 +198,7 @@ function pause() {
/**
* Recursively reads the question based on the reading speed.
*/
function readQuestion() {
function readQuestion(expectedReadTime) {
if (!currentlyBuzzing && questionTextSplit.length > 0) {
const word = questionTextSplit.shift();
document.getElementById('question').innerHTML += word + ' ';
Expand All @@ -214,8 +214,8 @@ function readQuestion() {
time = 0;

timeoutID = window.setTimeout(() => {
readQuestion();
}, time * 0.9 * (125 - document.getElementById('reading-speed').value));
readQuestion(time * 0.9 * (125 - document.getElementById('reading-speed').value) + expectedReadTime);
}, time * 0.9 * (125 - document.getElementById('reading-speed').value) - new Date().getTime() + expectedReadTime);
} else {
document.getElementById('pause').disabled = true;
}
Expand Down
14 changes: 7 additions & 7 deletions server/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Room {
if (this.buzzedIn === userId) {
this.buzzedIn = null;
this.players[userId].updateStats(-5, 0);
this.readQuestion();
this.readQuestion(new Date().getTime());
this.sendSocketMessage({
type: 'give-answer',
userId: userId,
Expand Down Expand Up @@ -367,7 +367,7 @@ class Room {
this.players[userId].updateStats(points, celerity);
Object.values(this.players).forEach(player => { player.tuh++; });
} else if (directive === 'reject') {
this.readQuestion();
this.readQuestion(new Date().getTime());
this.players[userId].updateStats(points, celerity);
}

Expand Down Expand Up @@ -402,7 +402,7 @@ class Room {
username: this.players[userId].username,
tossup: this.tossup
});
this.readQuestion();
this.readQuestion(new Date().getTime());
}

pause(userId) {
Expand All @@ -411,7 +411,7 @@ class Room {
if (this.paused) {
clearTimeout(this.timeoutID);
} else {
this.readQuestion();
this.readQuestion(new Date().getTime());
}

this.sendSocketMessage({
Expand All @@ -421,7 +421,7 @@ class Room {
});
}

readQuestion() {
readQuestion(expectedReadTime) {
if (Object.keys(this.tossup).length === 0) return;
if (this.wordIndex >= this.questionSplit.length) {
return;
Expand All @@ -446,8 +446,8 @@ class Room {
});

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

revealQuestion() {
Expand Down

0 comments on commit 5720881

Please sign in to comment.