Ensure we only process 1 line at a time

Also debounce unknown cards, rather than processing them as fast as
possible.
This commit is contained in:
Annika Backstrom 2017-04-08 02:02:07 +00:00
parent ab89453d76
commit 1636bffd71

View File

@ -85,20 +85,32 @@ function appendLog(message) {
wss.broadcast(JSON.stringify(data)); wss.broadcast(JSON.stringify(data));
} }
tag_reader.stdout.on('data', (data) => { function processLine(data) {
if (Date.now() / 1000 < debounce_until) { if (Date.now() / 1000 < debounce_until) {
return; return;
} }
last_tag = String(data).trim(); last_tag = String(data).trim();
console.log("[tag] " + last_tag); console.log("[tag] " + last_tag);
// min 1 sec debounce
debounce_until = Date.now() / 1000 + 1;
lookup(last_tag, function(action, wait_sec) { lookup(last_tag, function(action, wait_sec) {
wait_sec = wait_sec || 1; wait_sec = wait_sec || 1;
debounce_until = Date.now() / 1000 + wait_sec;
if (action) { if (action) {
debounce_until = Date.now() / 1000 + wait_sec;
console.log(`[action] [${action}] for [${last_tag}]`); console.log(`[action] [${action}] for [${last_tag}]`);
player.stdin.write(action + "\n"); 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) => { tag_reader.stderr.on('data', (data) => {