-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkcapp-colors.js
More file actions
163 lines (136 loc) · 5.04 KB
/
kcapp-colors.js
File metadata and controls
163 lines (136 loc) · 5.04 KB
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
const debug = require('debug')('kcapp-colors:main');
const VENUE = process.env.VENUE;
const KCAPP_HOST = process.env.KCAPP_HOST || "localhost";
const KCAPP_PORT = process.env.KCAPP_PORT || 3000;
const KCAPP_PROTO = process.env.KCAPP_PROTO || "http";
const kcapp = require('kcapp-sio-client/kcapp')(KCAPP_HOST, KCAPP_PORT, "kcapp-colors", KCAPP_PROTO);
let lights = null;
function getLights(config) {
if (lights) return lights;
if (config.has_led_lights) {
const RED = process.env.RED || 17;
const GREEN = process.env.GREEN || 22;
const BLUE = process.env.BLUE || 24;
lights = require('./pwm-util')(RED, GREEN, BLUE);
lights.turnOff();
} else if (config.has_wled_lights) {
if (config.wled_url) {
lights = require('./wled-http')(config.wled_url);
} else {
const USB_PORT = process.env.SERIAL || "/dev/tty.usbserial-0200FE39";
lights = require('./wled-serial')(USB_PORT);
}
}
return lights;
}
function controlPWMLights(leg, lights) {
const setLightsToCurrentPlayer = () => {
const player = leg.currentPlayer.player;
debug(`Setting color for ${player.name} = ${player.color}`);
lights.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");
lights.blink('#00ff00', 4000, () => {
if (!match.is_finished) setLightsToCurrentPlayer();
});
if (match.is_finished) {
debug("Disabling lights in 6s");
setTimeout(() => lights.turnOff(), 6000);
}
});
leg.on('order_changed', () => setLightsToCurrentPlayer());
leg.on('cancelled', () => {
debug("Leg cancelled, disabling lights");
lights.turnOff();
});
if (leg.leg && !leg.leg.is_finished) setLightsToCurrentPlayer();
}
function controlWLEDLights(leg, lights) {
let throwTimeout = undefined;
function clearThrowTimeout() {
if (throwTimeout) {
clearTimeout(throwTimeout);
throwTimeout = undefined;
}
}
const setLightsToCurrentPlayer = () => {
const player = leg.currentPlayer.player;
debug(`Setting color for ${player.name} = ${player.color}`);
lights.setColor(player.color);
}
leg.on('score_update', (data) => {
clearThrowTimeout();
if (data.leg.is_finished) return;
setLightsToCurrentPlayer();
});
leg.on('possible_throw', () => {
clearThrowTimeout();
lights.effect(lights.EFFECTS.SOLID);
throwTimeout = setTimeout(() => {
debug("No dart thrown for 30s");
lights.effect(lights.EFFECTS.HEARTBEAT, null, null, 50, 50, null, null);
}, 30000);
});
leg.on('leg_finished', (data) => {
const match = data.match;
clearThrowTimeout();
debug("Blinking lights for 6s");
lights.blink('#00ff00', 6000, () => {
if (!match.is_finished) setLightsToCurrentPlayer();
});
if (match.is_finished) {
debug("Disabling lights in 6s");
setTimeout(() => lights.turnOff(), 6000);
}
});
leg.on('order_changed', () => {
clearThrowTimeout();
setLightsToCurrentPlayer();
});
leg.on('cancelled', () => {
clearThrowTimeout();
debug("Leg cancelled, disabling lights");
lights.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)) return;
const config = match.venue.config;
const lights = getLights(config);
if (!lights) return;
const legId = match.current_leg_id;
if (config.has_led_lights) {
debug(`Connected to match ${match.id} via PWM`);
kcapp.connectLegNamespace(legId, (leg) => controlPWMLights(leg, lights));
} else if (config.has_wled_lights) {
const mode = config.wled_url ? 'http' : 'serial';
debug(`Connected to match ${match.id} via WLED (${mode})`);
kcapp.connectLegNamespace(legId, (leg) => controlWLEDLights(leg, lights));
}
}
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 && venue.config)) return;
const config = venue.config;
const lights = getLights(config);
if (!lights || !config.has_wled_lights) return;
let length = 0;
data.audios.forEach(audio => length += audio.length ? audio.length : 0);
lights.effect(lights.EFFECTS.BPM, null, lights.PALLETE.RAINBOW, 140, 240, length * 1000, null);
});
});
debug("Waiting for events to change colors...");