#include "web_dom.h" #include "web_match.h" #include "web_misc.h" #include "web_scoreboard.h" #include "checkouts.h" #include "match.h" #include #include #include #include #include static struct { bufstr name, rem, sugg, avg; } bufs[2]; void scoreboard_set_player_active(int pn) { char sel[64], *psel = sel; if (pn == -1) psel = NULL; else sprintf(sel, "#p%d-info", pn); dom_set_uniq_class(psel, "active", "[id$=-info]"); } 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 = malloc(8); sprintf(str, "%.2f", avg); bufstr_buf(&bufs[pn-1].avg, str); } void scoreboard_flush_player_info(int pn) { char sel[32]; int len = sprintf(sel, "#p%d-", pn); 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() { scoreboard_flush_player_info(1); scoreboard_flush_player_info(2); } 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 (pn == 1 || state->mode != M_PVC) update_player_sugg(pn, rem); } void show_player_info(int pn) { char sel[64]; sprintf(sel, "#p%d-info-inner", pn); dom_add_class(sel, "visible"); } void clear_player_info(int pn) { scoreboard_set_player_name(pn, NULL); scoreboard_set_player_rem(pn, NULL); scoreboard_set_player_sugg(pn, NULL); scoreboard_set_player_avg(pn, 0); } 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"; } void draw_visits() { dom_set_content("#visits", NULL); 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(6 * (n_visits + 1) * sizeof(*elemv)); int elemc = 0; char buf[32], buf2[32]; snprintf(buf, sizeof(buf), "%d", state->legs[0]->start); elemv[elemc++] = create_div(buf, "visit-col2"); elemv[elemc++] = create_div("0", "visit-col3"); if (state->mode != M_P) { snprintf(buf, sizeof(buf), "%d", state->legs[1]->start); elemv[elemc++] = create_div(buf, "visit-col4"); } for (int i = 0; i < n_visits; ++i) { struct visit *v = state->legs[0]->visits + i; snprintf(buf, sizeof(buf), "%d", v->points); snprintf(buf2, sizeof(buf2), "%s %s", "visit-col1", points_class(v->points)); elemv[elemc++] = create_div(buf, buf2); snprintf(buf, sizeof(buf), "%d", v->rem); elemv[elemc++] = create_div(buf, "visit-col2"); snprintf(buf, sizeof(buf), "%d", i + 1); elemv[elemc++] = create_div(buf, "visit-col3"); if (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-col4"); snprintf(buf, sizeof(buf), "%d", v->points); snprintf(buf2, sizeof(buf2), "%s %s", "visit-col5", points_class(v->points)); elemv[elemc++] = create_div(buf, buf2); char *pbuf = stpcpy(buf, "…"); for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); snprintf(pbuf + (j * 5), sizeof(buf) - (pbuf + (j * 5) - buf), " %4s", n); free(n); } elemv[elemc++] = create_div(buf, "visit-col6"); } 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"); }