-
Notifications
You must be signed in to change notification settings - Fork 3
/
kcapp-colors.js
181 lines (151 loc) · 4.94 KB
/
kcapp-colors.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const debug = require('debug')('kcapp-color-switcher:main');
const VENUE = process.env.VENUE;
const RED = process.env.RED || 17;
const GREEN = process.env.GREEN || 22;
const BLUE = process.env.BLUE || 24;
const pwm = require("./pwm-util")(RED, GREEN, BLUE);
const USB_PORT = process.env.SERIAL || "/dev/tty.usbserial-0200FE39";
const wled = require("./wled-util")(USB_PORT);
const host = process.env.KCAPP_API || "localhost";
const port = process.env.PORT || 3000;
const kcapp = require('kcapp-sio-client/kcapp')(host, port, 'kcapp-colors', "http");
// Disable lights when we start
pwm.turnOff();
function controlPWMLights(leg) {
const setLightsToCurrentPlayer = () => {
const player = leg.currentPlayer.player;
debug(`Setting color for ${player.name} = ${player.color}`);
pwm.setColor(player.color);
}
leg.on('score_update', (data) => {
if (data.leg.is_finished) {
return;
}
setLightsToCurrentPlayer();
});
leg.on('leg_finished', (data) => {
const match = data.match;
debug("Blinking lights for 4s");
pwm.blink('#00ff00', 4000, () => {
if (!match.is_finished) {
setLightsToCurrentPlayer();
}
});
if (match.is_finished) {
debug("Disabling lights in 6s");
setTimeout(() => {
pwm.turnOff();
}, 6000);
}
});
leg.on('order_changed', (data) => {
setLightsToCurrentPlayer();
});
leg.on('cancelled', (data) => {
debug("Leg cancelled, disabling lights");
pwm.turnOff();
});
if (leg.leg && !leg.leg.is_finished) {
setLightsToCurrentPlayer();
}
}
function controlWLEDLights(leg) {
let throwTimeout = undefined;
function clearThrowTimeout() {
if (throwTimeout) {
clearTimeout(throwTimeout);
}
}
const setLightsToCurrentPlayer = () => {
const player = leg.currentPlayer.player;
debug(`Setting color for ${player.name} = ${player.color}`);
wled.setColor(player.color);
}
leg.on('score_update', (data) => {
clearThrowTimeout();
if (data.leg.is_finished) {
return;
}
setLightsToCurrentPlayer();
});
leg.on('possible_throw', (data) => {
clearThrowTimeout();
wled.effect(wled.EFFECTS.SOLID);
throwTimeout = setTimeout(() => {
debug("No dart thrown for 10s");
wled.effect(wled.EFFECTS.HEARTBEAT, null, null, 50, 240, 5000, null);
}, 10000);
});
leg.on('leg_finished', (data) => {
const match = data.match;
clearThrowTimeout();
debug("Blinking lights for 6s");
wled.blink('#00ff00', 6000, () => {
if (!match.is_finished) {
setLightsToCurrentPlayer();
}
});
if (match.is_finished) {
debug("Disabling lights in 6s");
setTimeout(() => {
wled.turnOff();
}, 6000);
}
});
leg.on('order_changed', (data) => {
clearThrowTimeout();
setLightsToCurrentPlayer();
});
leg.on('cancelled', (data) => {
clearThrowTimeout();
debug("Leg cancelled, disabling lights");
wled.turnOff();
});
if (leg.leg && !leg.leg.is_finished) {
setLightsToCurrentPlayer();
}
}
function connectToMatch(data) {
const match = data.match;
if (match.venue && match.venue.config && VENUE && VENUE == match.venue.id) {
const config = match.venue.config;
if (config.has_led_lights) {
const legId = match.current_leg_id;
debug(`Connected to match ${match.id}`);
kcapp.connectLegNamespace(legId, (leg) => {
controlPWMLights(leg);
});
} else if (config.has_wled_lights) {
const legId = match.current_leg_id;
debug(`Connected to match ${match.id} via WLED`);
kcapp.connectLegNamespace(legId, (leg) => {
controlWLEDLights(leg);
});
}
}
}
kcapp.connect(() => {
kcapp.on('new_match', (data) => {
connectToMatch(data);
});
kcapp.on('warmup_started', (data) => {
connectToMatch(data);
});
kcapp.on('demo', (data) => {
debug(`Enabling Demo Mode`);
const venue = data.venue;
if (VENUE && VENUE == venue.id) {
if (venue && venue.config) {
const config = venue.config;
if (config.has_led_lights) {
// TODO
} else if (config.has_wled_lights) {
let length = 0;
data.audios.forEach(audio => length += audio.length ? audio.length : 0);
wled.effect(wled.EFFECTS.BPM, null, wled.PALLETE.RAINBOW, 140, 240, length * 1000, null);
}
}
}
});
});
debug("Waiting for events to change colors...");