Skip to content

Commit

Permalink
Add support for 9 darts shootout
Browse files Browse the repository at this point in the history
  • Loading branch information
thordy committed Oct 5, 2019
1 parent 15e6a18 commit 58b01dc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
44 changes: 28 additions & 16 deletions main.js → kcapp-smartboard.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand All @@ -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) {
Expand All @@ -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();
}
}
},
() => {
Expand All @@ -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!`);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node main"
"start": "node kcapp-smartboard"
},
"dependencies": {
"debug": "^4.1.1",
Expand Down
3 changes: 3 additions & 0 deletions smartboard-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 58b01dc

Please sign in to comment.