diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-03 15:27:10 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-03 15:27:10 -0700 |
commit | 12e0680e13e3b00fe085ff419abcef88dd512b23 (patch) | |
tree | 0676ff27c3d2351da182bad19f7ead6747397b8e /web/web_scoreboard.c | |
parent | 874b94aaeb51ffa80f19b7c9535fbc84af32609e (diff) | |
download | dartboat-12e0680e13e3b00fe085ff419abcef88dd512b23.tar.gz dartboat-12e0680e13e3b00fe085ff419abcef88dd512b23.tar.xz |
web: reuse elem array appending for drawing visits
Diffstat (limited to 'web/web_scoreboard.c')
-rw-r--r-- | web/web_scoreboard.c | 91 |
1 files changed, 69 insertions, 22 deletions
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c index b9b4688..18835eb 100644 --- a/web/web_scoreboard.c +++ b/web/web_scoreboard.c @@ -176,44 +176,91 @@ void clear_player_info(int pn) scoreboard_set_player_avg(pn, 0); } -void draw_visits() + +// FIXME move html/svg elem stuff to common file +struct elem { + char *ns, *name, *content; + int n_attrs, size_attrs; + char **attr_names, **attr_vals; +}; +struct elem *elem_init(char *ns, char *name, int size_attrs); +void elem_free(struct elem *e); +void elem_add_attr(struct elem *e, char *name, char *val); +void elem_add_attr_double(struct elem *e, char *name, double val); +void append_elemv(char *sel, int elemc, struct elem **elemv); + +struct elem *gen_div(char *str, char *class) { - EM_ASM({elemSetContent($0, $1)}, "#visits", NULL); + struct elem *e = elem_init(NULL, "div", 1); + e->content = strdup(str); + elem_add_attr(e, "class", class); + return e; +} - char visit_no[10], p1_pts[10], p1_rem[10], - p2_pts[10], p2_rem[10], p2_darts[100]; +void add_colour_class(char *buf, int pts) +{ + if (pts >= 180) + strcat(buf, " p180"); + else if (pts >= 140) + strcat(buf, " p140"); + else if (pts >= 100) + strcat(buf, " p100"); + else if (pts >= 60) + strcat(buf, " p60"); + else if (pts >= 40) + strcat(buf, " p40"); + else if (pts >= 20) + strcat(buf, " p20"); + else + strcat(buf, " p0"); +} - sprintf(p1_rem, "%d", state->legs[0]->start); - sprintf(p2_rem, "%d", state->legs[1]->start); - EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)}, - "0", "", p1_rem, "", state->mode == M_P ? "" : p2_rem, ""); +void draw_visits() +{ + EM_ASM({elemSetContent($0, $1)}, "#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; - for (int i = 0; i < n_visits; ++i) { - visit_no[0] = p1_pts[0] = p1_rem[0] = - p2_pts[0] = p2_rem[0] = p2_darts[0] = 0; - sprintf(visit_no, "%d", i + 1); + struct elem **elemv = malloc(6 * n_visits * sizeof(*elemv)); + int elemc = 0; + for (int i = 0; i < n_visits; ++i) { + char buf[32], buf2[32]; struct visit *v = state->legs[0]->visits + i; - sprintf(p1_pts, "%d", v->points); - sprintf(p1_rem, "%d", v->rem); + sprintf(buf, "%d", v->points); + strcpy(buf2, "visit-col1"); + add_colour_class(buf2, v->points); + elemv[elemc++] = gen_div(buf, buf2); + sprintf(buf, "%d", v->rem); + elemv[elemc++] = gen_div(buf, "visit-col2"); + + sprintf(buf, "%d", i + 1); + elemv[elemc++] = gen_div(buf, "visit-col3"); if (i < state->legs[1]->n_visits) { v = state->legs[1]->visits + i; - sprintf(p2_pts, "%d", v->points); - sprintf(p2_rem, "%d", v->rem); - + sprintf(buf, "%d", v->rem); + elemv[elemc++] = gen_div(buf, "visit-col4"); + sprintf(buf, "%d", v->points); + strcpy(buf2, "visit-col5"); + add_colour_class(buf2, v->points); + elemv[elemc++] = gen_div(buf, buf2); + + strcpy(buf, "… "); for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); - sprintf(p2_darts + (j ? (j*5 - 1) : 0), - j == 0 ? "%4s" : " %4s", n); + sprintf(buf + strlen(buf), j == 0 ? "%4s" : " %4s", n); // FIXME free(n); } + elemv[elemc++] = gen_div(buf, "visit-col6"); + } else { + elemv[elemc++] = gen_div("", "visit-col4"); + elemv[elemc++] = gen_div("", "visit-col5"); + elemv[elemc++] = gen_div("", "visit-col6"); } - - EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)}, - visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts); } + + append_elemv("#visits", elemc, elemv); + EM_ASM({elemScrollToBottom($0)}, "#visits"); } |