-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkcapp-smartboard.js
122 lines (112 loc) · 5.13 KB
/
kcapp-smartboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const debug = require('debug')('kcapp-smartboard:main');
const smartboard = process.env.NODE_ENV === "prod" ? require('./smartboard')() : require("./smartboard-mock")();
const host = process.env.KCAPP_API || "localhost";
const port = process.env.PORT || 3000;
const kcapp = require('kcapp-sio-client/kcapp')(host, port, 'smartboard', "http");
const X01 = 1;
const SHOOTOUT = 2;
this.connected = false;
this.peripheral = {};
/**
* Disconnect from the smartboard
* @param {object} data
*/
function disconnectListener(data) {
smartboard.disconnect(this.peripheral, () => {
debug("Disconnected");
this.connected = false;
});
}
function connectToMatch(data) {
const match = data.match;
const legId = match.current_leg_id;
const config = match.venue.config;
if (match.venue && config.has_smartboard) {
debug(`Connected to match ${match.id}`);
kcapp.connectLegNamespace(legId, (leg) => {
debug(`Connected to leg ${legId}`);
if (!this.connected) {
leg.emit('announce', { type: 'notify', message: 'Searching for smartboard ...' });
smartboard.startScan();
smartboard.connect(config.smartboard_uuid, (peripheral) => {
this.connected = true;
this.peripheral = peripheral;
smartboard.initialize(peripheral, config.smartboard_button_number,
(dart) => {
const player = leg.currentPlayer;
if (dart.multiplier == 0) {
dart.multiplier = 1;
dart.zone = 'inner';
} else if (dart.multiplier == 1) {
dart.zone = 'outer';
}
debug(`Got throw ${JSON.stringify(dart)} for ${player.player.id}`);
leg.emitThrow(dart);
if (match.match_type.id == SHOOTOUT) {
player.current_score += dart.score * dart.multiplier;
const 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 if (match.match_type.id == X01) {
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();
}
} else {
if (leg.dartsThrown == 3) {
leg.emitVisit();
}
}
},
() => {
debug("Button pressed, sending visit");
leg.emitVisit();
}
);
leg.on('leg_finished', (data) => {
debug(`Got leg_finished event!`);
const match = data.match;
if (match.is_finished) {
debug("Match is finished, disconnecting from board");
disconnectListener.bind(this)(data);
} else {
debug("Leg is finished");
}
});
leg.emit('announce', { type: 'success', message: 'Connected to smartboard' });
leg.on('cancelled', (data) => {
debug("Leg cancelled, disconnecting from board");
disconnectListener.bind(this)();
});
});
} else {
debug("Already connected to board...");
leg.emit('announce', { type: 'success', message: 'Already Connected' });
leg.on('leg_finished', disconnectListener.bind(this));
}
});
}
}
kcapp.connect(() => {
kcapp.on('new_match', (data) => {
connectToMatch(data);
});
kcapp.on('warmup_started', (data) => {
connectToMatch(data);
});
kcapp.on('reconnect_smartboard', (data) => {
connectToMatch(data);
});
});
debug("Waiting for matches to start...");