summaryrefslogtreecommitdiff
path: root/web/web_scoreboard.c
blob: 62b4c0f1f80fb56e3558f8135ac8f88dd70aa2da (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "web_dom.h"
#include "web_match.h"
#include "web_misc.h"
#include "web_scoreboard.h"

#include "checkouts.h"
#include "match.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <emscripten/emscripten.h>

static struct {
	bufstr name, rem, sugg, avg;
} *bufs;

#define NUM_SLOTS 2
static int slots[NUM_SLOTS];
static int active_pn;

static void flush_slot(int slot)
{
	int pn = slots[slot - 1];
	if (!pn) return;

	// FIXME: should be combined with flushing player info
	char sel[32];
	int len = sprintf(sel, "#info-slot-%d .", slot);
	strcpy(sel + len, "name");
	dom_set_content(sel, bufstr_flush(&bufs[pn-1].name));
	strcpy(sel + len, "rem");
	dom_set_content(sel, bufstr_flush(&bufs[pn-1].rem));
	strcpy(sel + len, "sugg");
	dom_set_content(sel, bufstr_flush(&bufs[pn-1].sugg));
	strcpy(sel + len, "avg");
	dom_set_content(sel, bufstr_flush(&bufs[pn-1].avg));
}

static int player_slot(int pn)
{
	for (int i = 0; i < NUM_SLOTS; ++i) {
		if (slots[i] == pn)
			return i + 1;
	}

	return 0;
}

static void set_slot_player(int slot, int pn)
{
	if (slots[slot - 1] != pn) {
		slots[slot - 1] = pn;
		flush_slot(slot);
	}
}

static void set_slot_active(int slot)
{
	char buf[64], *sel = buf;

	if (slot)
		sprintf(sel, "#info-slot-%d", slot);
	else
		sel = NULL;

	dom_set_uniq_class(sel, "active", ".info-slot");
}

EMSCRIPTEN_KEEPALIVE
void scoreboard_prev_slot()
{
	for (int i = NUM_SLOTS; i > 1; --i)
		set_slot_player(i, slots[i - 2]);
	int prev = slots[0];
	set_slot_player(1, prev == 1 ? match_num_players() : prev - 1);
	set_slot_active(player_slot(active_pn));
}

EMSCRIPTEN_KEEPALIVE
void scoreboard_next_slot()
{
	for (int i = 1; i < NUM_SLOTS; ++i)
		set_slot_player(i, slots[i]);
	int prev = slots[NUM_SLOTS - 1];
	set_slot_player(NUM_SLOTS, prev == match_num_players() ? 1 : prev + 1);
	set_slot_active(player_slot(active_pn));
}

void scoreboard_set_player_active(int pn)
{
	active_pn = pn;

	if (!pn) {
		set_slot_active(0);
		return;
	}

	int np = match_num_players();
	if (np > NUM_SLOTS) {
		for (int i = 0; i < NUM_SLOTS; ++i)
			set_slot_player(i + 1, ((pn + i - 1) % np) + 1);
		set_slot_active(1);
	} else {
		set_slot_active(pn);
	}
}

void scoreboard_set_player_name(int pn, char *str)
{
	bufstr_buf(&bufs[pn-1].name, str);
}

void scoreboard_set_player_rem(int pn, char *str)
{
	bufstr_buf(&bufs[pn-1].rem, str);
}

void scoreboard_set_player_sugg(int pn, char *str)
{
	bufstr_buf(&bufs[pn-1].sugg, str);
}

void scoreboard_set_player_avg(int pn, double avg)
{
	char str[64];
	sprintf(str, "%.2f", avg);
	bufstr_buf(&bufs[pn-1].avg, str);
}

void scoreboard_flush_player_info(int pn)
{
	int slot = player_slot(pn);
	if (!slot) return;

	char sel[32];
	int len = sprintf(sel, "#info-slot-%d .", slot);

	if (bufstr_changed(&bufs[pn-1].name)) {
		strcpy(sel + len, "name");
		dom_set_content(sel, bufstr_flush(&bufs[pn-1].name));
	}

	if (bufstr_changed(&bufs[pn-1].rem)) {
		strcpy(sel + len, "rem");
		dom_set_content(sel, bufstr_flush(&bufs[pn-1].rem));
	}

	if (bufstr_changed(&bufs[pn-1].sugg)) {
		strcpy(sel + len, "sugg");
		dom_set_content(sel, bufstr_flush(&bufs[pn-1].sugg));
	}

	if (bufstr_changed(&bufs[pn-1].avg)) {
		strcpy(sel + len, "avg");
		dom_set_content(sel, bufstr_flush(&bufs[pn-1].avg));
	}
}

void scoreboard_flush()
{
	// FIXME unnecessary slot lookups
	for (int i = 0; i < NUM_SLOTS; ++i) {
		int pn = slots[i];
		if (pn)
			scoreboard_flush_player_info(pn);
	}
}

void update_player_name(int pn, char *str)
{
	scoreboard_set_player_name(pn, str);
}

void update_player_sugg(int pn, int rem)
{
	char str[15] = {0}, *p = str;

	if (rem > 1 && rem <= 170) {
		char *target;
		int i = 3, trem = rem;
		while (i && trem && (target = CHECKOUTS[--i][trem-1])) {
			p += sprintf(p, i == 2 ? "%s" : "-%s", target);
			trem -= segment_points(segment_from_name(target));
		}
	}

	scoreboard_set_player_sugg(pn, str);
}

static double calc_avg(struct leg *l, int n_darts) {
	if (!l->n_visits)
		return 0;

	if (l->rem > 0)
		return ((double)(l->start - l->rem) / l->n_visits);

	return ((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3);
}

void update_player_avg(int pn, int n_darts)
{
	struct leg *l = state->legs[pn-1];
	scoreboard_set_player_avg(pn, calc_avg(l, n_darts));
}

void update_player_rem(int pn, int rem)
{
	char str[5];
	if (rem < 0 || rem == 1)
		strcpy(str, "BUST");
	else
		sprintf(str, "%d", rem);

	scoreboard_set_player_rem(pn, str);
	if (!match_player_is_comp(pn))
		update_player_sugg(pn, rem);
}

void scoreboard_show_info(int num_players)
{
	if (bufs) free(bufs);
	bufs = calloc(num_players, sizeof(*bufs));

	for (int i = 1; i <= num_players && i <= NUM_SLOTS; ++i) {
		set_slot_player(i, i);

		char buf[64];
		sprintf(buf, "#info-slot-%d .inner", i);
		dom_add_class(buf, "visible");
	}

	if (num_players > NUM_SLOTS) {
		dom_add_class("#info-slot-prev", "visible");
		dom_add_class("#info-slot-next", "visible");
	}
}

void scoreboard_hide_info()
{
	free(bufs);
	bufs = NULL;
	memset(slots, 0, sizeof(slots));

	dom_set_uniq_class(NULL, "visible", ".info-slot .inner");
	dom_remove_class("#info-slot-prev", "visible");
	dom_remove_class("#info-slot-next", "visible");
}

static struct dom_elem *create_div(char *str, char *class)
{
	struct dom_elem *e = dom_elem_init(NULL, "div", 1);
	e->content = strdup(str);
	dom_elem_add_attr(e, "class", class);
	return e;
}

static const char *points_class(int pts)
{
	return (pts >= 180) ? "p180" :
		(pts >= 140) ? "p140" :
		(pts >= 100) ? "p100" :
		(pts >= 60) ? "p60" :
		(pts >= 40) ? "p40" :
		(pts >= 20) ? "p20" :
		"p0";
}

static void buf_darts(char *buf, size_t size, struct visit *v)
{
	char *ptr = buf;
	for (int j = 0; j < v->n_darts; ++j) {
		char *n = segment_name(v->darts[j]);
		ptr += snprintf(ptr, size - (ptr - buf),
			j == 0 ? "%s" : "-%s", n);
		free(n);
	}
}

void draw_visits_dense()
{
	dom_set_content("#visits", NULL);
	dom_add_class("#visits", "dense");

	int np = match_num_players();
	dom_set_style_property_int("--num-players", np);

	// FIXME
	int n_visits = state->legs[0]->n_visits > state->legs[1]->n_visits ?
		state->legs[0]->n_visits : state->legs[1]->n_visits;

	struct dom_elem **elemv = malloc(
		(np * 2 + 1) * (2 * n_visits + 2) * sizeof(*elemv));
	int elemc = 0;

	elemv[elemc++] = create_div("", "");
	for (int i = 0; i < np; ++i) {
		elemv[elemc++] = create_div(state->legs[i]->name,
			"visit-name");
	}

	char buf[32], buf2[32];
	elemv[elemc++] = create_div("0", "visit-n");
	for (int i = 0; i < np; ++i) {
		if (i + 1 == match_opts->throws_first)
			elemv[elemc++] = create_div("", "throws-first");
		else
			elemv[elemc++] = create_div("", "");
		snprintf(buf, sizeof(buf), "%d", state->legs[i]->start);
		elemv[elemc++] = create_div(buf, "");
	}

	for (int i = 0; i < n_visits; ++i) {
		snprintf(buf, sizeof(buf), "%d", i + 1);
		elemv[elemc++] = create_div(buf, "visit-n");

		for (int j = 0; j < np; ++j) {
			if (i >= state->legs[j]->n_visits) {
				elemv[elemc++] = create_div("", "");
				elemv[elemc++] = create_div("", "");
				continue;
			}

			struct visit *v = state->legs[j]->visits + i;

			snprintf(buf, sizeof(buf), "%d", v->points);
			snprintf(buf2, sizeof(buf2), "%s",
				points_class(v->points));
			elemv[elemc++] = create_div(buf, buf2);

			snprintf(buf, sizeof(buf), "%d", v->rem);
			elemv[elemc++] = create_div(buf, "");
		}

		bool n_darts = false;
		for (int j = 0; j < np; ++j) {
			if (i < state->legs[j]->n_visits &&
				state->legs[j]->visits[i].n_darts)
				n_darts = true;
		}
		if (!n_darts) continue;

		elemv[elemc++] = create_div("", "");
		for (int j = 0; j < np; ++j) {
			if (i >= state->legs[j]->n_visits) {
				elemv[elemc++] = create_div("",
					"visit-dense-darts");
				continue;
			}

			struct visit *v = state->legs[j]->visits + i;

			if (v->n_darts) {
				buf_darts(buf, sizeof(buf), v);
				elemv[elemc++] = create_div(buf,
					"visit-dense-darts");
			} else {
				elemv[elemc++] = create_div("",
					"visit-dense-darts");
			}
		}
	}

	dom_append_elemv("#visits", elemc, elemv);
	for (int i = 0; i < elemc; ++i)
		dom_elem_free(elemv[i]);
	free(elemv);

	dom_scroll_to_bottom("#visits");
	dom_scroll_to_center_child("#visits", ".visit-n");
}

void draw_visits()
{
	if (match_num_players() > 2) {
		draw_visits_dense();
		return;
	}

	dom_set_content("#visits", NULL);
	dom_remove_class("#visits", "dense");

	int n_visits = state->legs[0]->n_visits;
	if (match_num_players() != 1 && state->legs[1]->n_visits > n_visits)
		n_visits = state->legs[1]->n_visits;

	struct dom_elem **elemv = malloc(7 * (n_visits + 1) * sizeof(*elemv));
	int elemc = 0;

	elemv[elemc++] = create_div("", "visit-p1-spacer");
	elemv[elemc++] = create_div(state->legs[0]->name, "visit-p1-name");
	if (match_num_players() != 1)
		elemv[elemc++] = create_div(state->legs[1]->name,
			"visit-p2-name");
	elemv[elemc++] = create_div("", "visit-p2-spacer");

	char buf[32], buf2[32];
	snprintf(buf, sizeof(buf), "%d", state->legs[0]->start);
	if (match_opts->throws_first == 1)
		elemv[elemc++] = create_div("", // content added via CSS
			"visit-p1-pts throws-first");
	elemv[elemc++] = create_div(buf, "visit-p1-rem");
	elemv[elemc++] = create_div("0", "visit-n");
	if (match_num_players() != 1) {
		snprintf(buf, sizeof(buf), "%d", state->legs[1]->start);
		elemv[elemc++] = create_div(buf, "visit-p2-rem");
		if (match_opts->throws_first == 2)
			elemv[elemc++] = create_div("",
				"visit-p2-pts throws-first");
	}

	for (int i = 0; i < n_visits; ++i) {
		struct visit *v;

		if (i >= state->legs[0]->n_visits)
			goto p2;
		v = state->legs[0]->visits + i;

		if (v->n_darts) {
			buf_darts(buf, sizeof(buf), v);
			elemv[elemc++] = create_div(buf, "visit-p1-darts");
		}

		snprintf(buf, sizeof(buf), "%d", v->points);
		snprintf(buf2, sizeof(buf2), "%s %s",
			"visit-p1-pts", points_class(v->points));
		elemv[elemc++] = create_div(buf, buf2);

		snprintf(buf, sizeof(buf), "%d", v->rem);
		elemv[elemc++] = create_div(buf, "visit-p1-rem");

p2:
		snprintf(buf, sizeof(buf), "%d", i + 1);
		elemv[elemc++] = create_div(buf, "visit-n");

		if (match_num_players() == 1 || i >= state->legs[1]->n_visits)
			continue;
		v = state->legs[1]->visits + i;

		snprintf(buf, sizeof(buf), "%d", v->rem);
		elemv[elemc++] = create_div(buf, "visit-p2-rem");

		snprintf(buf, sizeof(buf), "%d", v->points);
		snprintf(buf2, sizeof(buf2), "%s %s",
			"visit-p2-pts", points_class(v->points));
		elemv[elemc++] = create_div(buf, buf2);

		if (v->n_darts) {
			buf_darts(buf, sizeof(buf), v);
			elemv[elemc++] = create_div(buf, "visit-p2-darts");
		}
	}

	dom_append_elemv("#visits", elemc, elemv);
	for (int i = 0; i < elemc; ++i)
		dom_elem_free(elemv[i]);
	free(elemv);

	dom_scroll_to_bottom("#visits");
	dom_scroll_to_center_child("#visits", ".visit-n");
}