From 58b01dc12adc1217e509ff66e7aba199517bf00a Mon Sep 17 00:00:00 2001 From: Thord Setsaas Date: Sat, 5 Oct 2019 11:46:52 +0200 Subject: [PATCH] Add support for 9 darts shootout --- main.js => kcapp-smartboard.js | 44 +++++++++++++++++++++------------- package.json | 2 +- smartboard-mock.js | 3 +++ 3 files changed, 32 insertions(+), 17 deletions(-) rename main.js => kcapp-smartboard.js (67%) diff --git a/main.js b/kcapp-smartboard.js similarity index 67% rename from main.js rename to kcapp-smartboard.js index c3a65df..245cf0b 100644 --- a/main.js +++ b/kcapp-smartboard.js @@ -1,6 +1,8 @@ var debug = require('debug')('kcapp-smartboard:main'); var smartboard = require('./smartboard')("5cf8218da78e", 15); -var sensor = require('./movement-sensor')(40); +//var sensor = require('./movement-sensor')(40); + +const SHOOTOUT = 2; this.connected = false; this.peripheral = {}; @@ -19,10 +21,8 @@ function disconnectListener(data) { function connectToMatch(data) { var match = data.match; var legId = match.current_leg_id; - if (match.venue && match.venue.id === kcapp.DART_REIDAR_VENUE_ID) { + if (match.venue && match.venue.config.has_smartboard) { debug(`Connected to match ${match.id}`); - // TODO add a generic "venue_configuration" to avoid hardcoding this here - kcapp.connectLegNamespace(legId, (leg) => { debug(`Connected to leg ${legId}`); if (!this.connected) { @@ -45,16 +45,28 @@ function connectToMatch(data) { } debug(`Got throw ${JSON.stringify(dart)} for ${player.player.id}`); leg.emitThrow(dart); - player.current_score -= dart.score * dart.multiplier; - if (player.current_score === 0 && dart.multiplier === 2) { - debug("Player Checkout! sending visit"); - leg.emit('announce', { type: 'confirm_checkout', message: "" }); - } else if (player.current_score <= 1) { - debug("Player busted, sending visit"); - leg.emitVisit(); - } else if (leg.dartsThrown == 3) { - leg.emitVisit(); + if (match.match_type.id == SHOOTOUT) { + player.current_score += dart.score * dart.multiplier; + var visits = leg.leg.visits.length; + if (visits > 0 && ((visits * 3 + leg.dartsThrown) % (9 * leg.leg.players.length) === 0)) { + debug("Match finished! sending visit"); + leg.emitVisit(); + } else if (leg.dartsThrown == 3) { + leg.emitVisit(); + } + } else { + player.current_score -= dart.score * dart.multiplier; + + if (player.current_score === 0 && dart.multiplier === 2) { + debug("Player Checkout! sending visit"); + leg.emit('announce', { type: 'confirm_checkout', message: "" }); + } else if (player.current_score <= 1) { + debug("Player busted, sending visit"); + leg.emitVisit(); + } else if (leg.dartsThrown == 3) { + leg.emitVisit(); + } } }, () => { @@ -64,14 +76,14 @@ function connectToMatch(data) { } ); - sensor.initialize(() => { + /*sensor.initialize(() => { var last = Date.now() - lastBoardData; debug(`Got movement. Last dart ${last}ms ago`); if (last > 200) { // TODO Send miss debug("Movement sensor: Miss"); } - }); + });*/ leg.on('leg_finished', (data) => { debug(`Got leg_finished event!`); @@ -99,7 +111,7 @@ function connectToMatch(data) { } -var kcapp = require('kcapp-sio-client/kcapp')("10.12.141.230", 3000); +var kcapp = require('kcapp-sio-client/kcapp')("localhost", 3000); kcapp.connect(() => { kcapp.on('new_match', (data) => { connectToMatch(data); diff --git a/package.json b/package.json index f10e0be..54dd9d8 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.0", "private": true, "scripts": { - "start": "node main" + "start": "node kcapp-smartboard" }, "dependencies": { "debug": "^4.1.1", diff --git a/smartboard-mock.js b/smartboard-mock.js index 8a62783..d19f851 100644 --- a/smartboard-mock.js +++ b/smartboard-mock.js @@ -26,6 +26,9 @@ exports.initialize = (peripheral, throwCallback, playerChangeCallback) => { debug('Connected to ' + peripheral.advertisement.localName + " (" + peripheral.uuid + ")"); debug('Enabled listening'); debug('Subscribed to throw noftifications!'); + setInterval(() => { + throwCallback({ score: 20, multiplier: 1}); + }, 1500); } exports.disconnect = (peripheral, callback) => {