From bcaa60602159a870e539540c96bab0c88a1d9f9a Mon Sep 17 00:00:00 2001 From: Tissevert Date: Wed, 21 Aug 2019 22:13:44 +0200 Subject: [PATCH] Make sure game states are rendered sequentially to fix race condition bug caused by playing a very fast opponent (like the bot Hannah) --- www/game.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/www/game.js b/www/game.js index 2e89dd8..f56079d 100644 --- a/www/game.js +++ b/www/game.js @@ -11,16 +11,6 @@ function Game(modules) { var selected = null; var queue = []; - window.addEventListener('focus', runQueue); - modules.messaging.addEventListener(["Game"], function(o) { - if(document.hasFocus()) { - modules.async.run(handleGameMessage(o)); - } else { - modules.statusHandler.set("♪"); - queue.push(handleGameMessage(o)); - } - }); - function buildSets() { var sets = {}; ['river', 'you', 'them'].forEach(function(id) { @@ -39,10 +29,24 @@ function Game(modules) { return sets; } + window.addEventListener('focus', runQueue); + modules.messaging.addEventListener(["Game"], function(o) { + queue.push(handleGameMessage(o)); + if(document.hasFocus() && queue.length == 1) { + runQueue(); + } else { + modules.statusHandler.set("♪"); + } + }); + function runQueue() { if(queue.length > 0) { + var length = queue.length; modules.async.run.apply(null, queue.concat( - modules.async.apply(function() {queue = [];}) + modules.async.apply(function() { + queue = queue.slice(length); + runQueue(); + }) )); } }