summaryrefslogtreecommitdiff
path: root/web/static/dartboat.js
blob: a47b84565e3f13850b7945404d71741a92f93e10 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
function _promptGet() {
	return document.getElementById('prompt').textContent;
}

function _promptGetAndClear() {
	let val = _promptGet();
	promptClear();
	return val;
}

function _promptDeleteLast() {
	let elem = document.getElementById('prompt');
	elem.textContent = elem.textContent.slice(0, -1);
}

function _promptClear() {
	document.getElementById('prompt').textContent = '';
}

let prompt_handler;
let prompt_handlers = {
	visit: {
		append(val) {
			let elem = document.getElementById('prompt');
			if (elem.textContent.length < 3) {
				elem.textContent += val;
				promptUpdateRem();
			}
		},

		backspace() {
			_promptDeleteLast();
			promptUpdateRem();
		},

		clear() {
			_promptClear();
			promptUpdateRem();
		},

		submit() {
			let pts = _promptGetAndClear();
			if (pts)
				Module.ccall('user_visit', 'number', ['number'], [pts]);
		},

		submit_rem() {
			let rem = _promptGetAndClear();
			if (rem)
				Module.ccall('user_visit_to_rem', 'number', ['number'], [rem]);
		}
	},
	num_darts: {
		append(val) {
			let elem = document.getElementById('prompt');
			if (elem.textContent.length < 3)
				elem.textContent += val;
		},

		backspace() {
			_promptDeleteLast();
		},

		clear() {
			_promptClear();
		},

		submit() {
			let n = _promptGetAndClear();
			if (n)
				Module.ccall('resp_numdarts', null, ['number'], [n]);
		}
	},
	init: {
		submit() {
			Module.ccall('start_match');
		}
	}
}

function setPromptHandler(ptr) {
	prompt_handler = prompt_handlers[UTF8ToString(ptr)];
}

function promptAppend(...args) {
	clearOi();
	prompt_handler?.append?.(...args);
}

function promptClear(...args) {
	clearOi();
	prompt_handler?.clear?.(...args);
}

function promptBackspace(...args) {
	clearOi();
	prompt_handler?.backspace?.(...args);
}

function promptSubmit(...args) {
	clearOi();
	prompt_handler?.submit?.(...args);
}

function promptSubmitRem(...args) {
	clearOi();
	prompt_handler?.submit_rem?.(...args);
}

function clearMatchLog() {
	document.getElementById('match').textContent = '';
}

let oi_timeout;
function oi() {
	document.getElementById('oi').style.visibility = 'visible';

	oi_timeout = setTimeout(function() { oi_timeout = null; clearOi() }, 3000);
}

function clearOi() {
	document.getElementById('oi').style.visibility = 'hidden';

	if (oi_timeout) {
		clearTimeout(oi_timeout);
		oi_timeout = null;
	}
}

function setPlayerActive(n) {
	document.querySelectorAll('[id$=-state]').forEach(e =>
		e.classList[n && e.id == `p${n}-state` ? 'add' : 'remove']('active'));
}

function updatePlayerRem(n, ptr) {
	document.getElementById(`p${n}-rem`).textContent = UTF8ToString(ptr);
}

function updatePlayerName(n, ptr) {
	document.getElementById(`p${n}-name`).textContent = UTF8ToString(ptr);
}

function updatePlayerSugg(n, ptr) {
	document.getElementById(`p${n}-sugg`).textContent = UTF8ToString(ptr);
}

function updatePlayerAvg(n, avg) {
	document.getElementById(`p${n}-avg`).textContent = avg.toFixed(2);
}

function setPromptText(ptr) {
	document.getElementById('prompt').textContent = UTF8ToString(ptr);
}

function promptMsg(p) {
	document.getElementById('prompt-msg').textContent = UTF8ToString(p);
}

function promptSugg(p) {
	document.getElementById('prompt-sugg').textContent = UTF8ToString(p);
}

function promptUpdateRem() {
	Module.ccall('update_user_rem_from_pts', null, ['number'], [_promptGet()]);
}

function scheduleCCall(f, ms, ...args) {
	let types = [];
	let vals = [];

	for (let i = 0; i < args.length/2; i++)
		vals[i] = (types[i] = UTF8ToString(args[i*2])) == 'string' ?
			UTF8ToString(args[i*2+1]) : args[i*2+1];

	let func = UTF8ToString(f);
	setTimeout(function() { Module.ccall(func, null, types, vals) }, ms);
}

const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0];
function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) {
	let elem = document.getElementById('match');

	for (let [i, ptr] of [u_pts, u_rem, visit_no, b_rem, b_pts, b_darts].entries()) {
		let div = document.createElement('div');
		let v = div.textContent = UTF8ToString(ptr);
		if (i == 0 || i == 4)
			div.className = `p${POINT_CLASSES.find(x => x <= v)}`;
		else if (i == 5 && v)
			div.textContent = `… ${v}`;
		div.className += ` visit-col${i+1}`;
		elem.append(div);
	}

	elem.scrollTop = elem.scrollHeight;
}

function setStdev(val) {
	Module.ccall('change_stdev', null, ['number', 'number'], [val, val]);
}

function updateStdev(val) {
	document.getElementById('stdev').value = val;
}

function setDelay(val) {
	Module.ccall('change_delay', null, ['number'], [val]);
}

function updateDelay(val) {
	document.getElementById('delay').value = val;
}

function processKey(data) {
	if (data.altKey || data.ctrlKey || data.metaKey || data.target.type == 'text')
		return;
	let key = data.key;

	if (isFinite(key))
		promptAppend(key);
	else if (key == 'Enter')
		promptSubmit();
	else if (key == 'Backspace')
		promptBackspace();
	else if (key == 'r')
		promptSubmitRem();
}

function modal(id) {
	document.getElementById(id).style.display = 'block';
}

window.onclick = function(e) {
	if (e.target.classList.contains('modal')) e.target.style.display = 'none';
}

document.addEventListener('keydown', processKey);