From 1636bffd7169478066fe13fd1736b63caf1dbf7e Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sat, 8 Apr 2017 02:02:07 +0000 Subject: [PATCH] Ensure we only process 1 line at a time Also debounce unknown cards, rather than processing them as fast as possible. --- player.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/player.js b/player.js index be56e4b..f8a5432 100644 --- a/player.js +++ b/player.js @@ -85,20 +85,32 @@ function appendLog(message) { wss.broadcast(JSON.stringify(data)); } -tag_reader.stdout.on('data', (data) => { +function processLine(data) { if (Date.now() / 1000 < debounce_until) { return; } + last_tag = String(data).trim(); console.log("[tag] " + last_tag); + + // min 1 sec debounce + debounce_until = Date.now() / 1000 + 1; + lookup(last_tag, function(action, wait_sec) { wait_sec = wait_sec || 1; + debounce_until = Date.now() / 1000 + wait_sec; if (action) { - debounce_until = Date.now() / 1000 + wait_sec; console.log(`[action] [${action}] for [${last_tag}]`); player.stdin.write(action + "\n"); } }); +} + +tag_reader.stdout.on('data', (data) => { + var s = data.toString(), lines = s.split(/\n/g); + for (var i = 0, l = lines.length; i < l; i++) { + processLine(lines[i]); + } }); tag_reader.stderr.on('data', (data) => {