From f4c0da6517e3a5bf4e87d2ac0acf474fbc75d62c Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 2 May 2022 20:03:01 -0700 Subject: web: more renaming --- web/static/dartboat.js | 4 +- web/static/index.html | 6 +- web/static/style.css | 2 +- web/web_control.c | 16 ++--- web/web_prompt.c | 2 +- web/web_scoreboard.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++++ web/web_scoreboard.h | 19 +++++ web/web_ui.c | 185 ------------------------------------------------- web/web_ui.h | 19 ----- 9 files changed, 219 insertions(+), 219 deletions(-) create mode 100644 web/web_scoreboard.c create mode 100644 web/web_scoreboard.h delete mode 100644 web/web_ui.c delete mode 100644 web/web_ui.h diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 13a5da4..719bd8d 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -156,12 +156,12 @@ function svgDrawElemv(layer, elemc, elemv, off_name, off_n_attrs, off_attr_names const content = HEAP32[(struct + off_content)>>2]; if (content) e.textContent = UTF8ToString(content); - $(`#dartboard${layer ? '-points' : ''}`).appendChild(e); + $(`#dartboard .${layer ? 'overlay' : 'base'}`).appendChild(e); } } function svgClearPoints() { - $('#dartboard-points').textContent = ''; + $('#dartboard .overlay').textContent = ''; } function updateDelay(val) { diff --git a/web/static/index.html b/web/static/index.html index f583449..1a8d1b5 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -58,9 +58,9 @@ diff --git a/web/static/style.css b/web/static/style.css index e306177..a7ad76a 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -64,7 +64,7 @@ div#controls #keypad-dartboard #dartboard-container { justify-content: center; } -div#controls #keypad-dartboard #dartboard-container svg { +div#controls #keypad-dartboard #dartboard { font-family: 'Inter'; height: 100%; } diff --git a/web/web_control.c b/web/web_control.c index 6c51167..3891750 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -2,8 +2,8 @@ #include "web_match.h" #include "web_opts.h" #include "web_prompt.h" +#include "web_scoreboard.h" #include "web_svg.h" -#include "web_ui.h" #include "comp.h" @@ -34,7 +34,7 @@ EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) { update_player_rem(state->active_player, state->active_leg->rem - pts); - ui_flush_player_info(state->active_player); + scoreboard_flush_player_info(state->active_player); } EMSCRIPTEN_KEEPALIVE @@ -42,7 +42,7 @@ void end_boat_visit(int rem, double avg) { EM_ASM(svgClearPoints()); update_player_rem(2, rem); - ui_set_player_avg(2, avg); + scoreboard_set_player_avg(2, avg); EM_ASM({setPromptInput($0)}, ""); EM_ASM({promptMsgR($0)}, ""); @@ -66,7 +66,7 @@ void draw_boat_throwing(int pts, char *str, double x, double y) EM_ASM({promptMsgR($0)}, str); free(str); - ui_flush_player_info(2); // FIXME bot hard-coded as player 2 + scoreboard_flush_player_info(2); // FIXME bot hard-coded as player 2 } EMSCRIPTEN_KEEPALIVE @@ -136,7 +136,7 @@ void handle_next() } } - ui_flush(); + scoreboard_flush(); } EMSCRIPTEN_KEEPALIVE @@ -179,7 +179,7 @@ void user_undo() if (state->num_darts) { state->num_darts = 0; - ui_set_player_avg(state->active_player, + scoreboard_set_player_avg(state->active_player, ((double)(state->active_leg->start - state->active_leg->visits[ state->active_leg->n_visits-2].rem) / @@ -209,10 +209,10 @@ void user_undo() ++state->boat_undone; } - ui_set_player_avg(state->active_player, 0); + scoreboard_set_player_avg(state->active_player, 0); update_player_rem(state->active_player, l->rem); if (state->mode == M_PVC) { - ui_set_player_avg(2, 0); + scoreboard_set_player_avg(2, 0); update_player_rem(2, state->legs[1]->rem); } diff --git a/web/web_prompt.c b/web/web_prompt.c index 4a2b829..69f4705 100644 --- a/web/web_prompt.c +++ b/web/web_prompt.c @@ -1,7 +1,7 @@ #include "web_control.h" #include "web_prompt.h" #include "web_match.h" -#include "web_ui.h" +#include "web_scoreboard.h" #include #include diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c new file mode 100644 index 0000000..6be1cd4 --- /dev/null +++ b/web/web_scoreboard.c @@ -0,0 +1,185 @@ +#include "web_match.h" +#include "web_scoreboard.h" + +#include "checkouts.h" +#include "match.h" + +#include +#include +#include +#include + +#include + +struct player_info { + char *name, *rem, *sugg; + double avg; +}; + +struct player_info buffered_info[2], flushed_info[2]; + +static inline void buffer_str(char *str, char **buffer, char *flushed) +{ + if (str == *buffer || (str && *buffer && !strcmp(str, *buffer))) + return; + + if (*buffer && *buffer != flushed) + free(*buffer); + + *buffer = str ? strdup(str) : NULL; +} + +void scoreboard_set_player_name(int pn, char *str) +{ + buffer_str(str, &buffered_info[pn-1].name, flushed_info[pn-1].name); +} + +void scoreboard_set_player_rem(int pn, char *str) +{ + buffer_str(str, &buffered_info[pn-1].rem, flushed_info[pn-1].rem); +} + +void scoreboard_set_player_sugg(int pn, char *str) +{ + buffer_str(str, &buffered_info[pn-1].sugg, flushed_info[pn-1].sugg); +} + +void scoreboard_set_player_avg(int pn, double avg) +{ + buffered_info[pn-1].avg = avg; +} + +static inline bool buffered_str_changed(char *buffered, char *flushed) +{ + return (!buffered != !flushed) || + (buffered && flushed && strcmp(buffered, flushed)); +} + +static inline void free_flushed_str(char *flushed, char *buffered) +{ + if (flushed && flushed != buffered) + free(flushed); +} + +void scoreboard_flush_player_info(int pn) +{ + if (buffered_str_changed(buffered_info[pn-1].name, + flushed_info[pn-1].name)) + EM_ASM({updatePlayerName($0, $1)}, pn, buffered_info[pn-1].name); + + if (buffered_str_changed(buffered_info[pn-1].rem, + flushed_info[pn-1].rem)) + EM_ASM({updatePlayerRem($0, $1)}, pn, buffered_info[pn-1].rem); + + if (buffered_str_changed(buffered_info[pn-1].sugg, + flushed_info[pn-1].sugg)) + EM_ASM({updatePlayerSugg($0, $1)}, pn, buffered_info[pn-1].sugg); + + if (buffered_info[pn-1].avg != flushed_info[pn-1].avg) + EM_ASM({updatePlayerAvg($0, $1)}, pn, buffered_info[pn-1].avg); + + free_flushed_str(flushed_info[pn-1].name, buffered_info[pn-1].name); + free_flushed_str(flushed_info[pn-1].rem, buffered_info[pn-1].rem); + free_flushed_str(flushed_info[pn-1].sugg, buffered_info[pn-1].sugg); + flushed_info[pn-1] = buffered_info[pn-1]; +} + +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); +} + +void update_player_avg(int pn, int n_darts) +{ + struct leg *l = state->legs[pn-1]; + scoreboard_set_player_avg(pn, + l->n_visits ? + (l->rem > 0 ? ((double)(l->start - l->rem) / l->n_visits) : + ((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3)) : + 0); +} + +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 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); +} + +void draw_visits() +{ + EM_ASM({clearVisits()}); + + char visit_no[10], p1_pts[10], p1_rem[10], + p2_pts[10], p2_rem[10], p2_darts[100]; + + 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, ""); + + 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 visit *v = state->legs[0]->visits + i; + sprintf(p1_pts, "%d", v->points); + sprintf(p1_rem, "%d", v->rem); + + 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); + + 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); + free(n); + } + } + + EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)}, + visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts); + } +} diff --git a/web/web_scoreboard.h b/web/web_scoreboard.h new file mode 100644 index 0000000..76ba1a9 --- /dev/null +++ b/web/web_scoreboard.h @@ -0,0 +1,19 @@ +#ifndef WEB_SCOREBOARD_H +#define WEB_SCOREBOARD_H + +#include + +void scoreboard_flush(); + +void scoreboard_flush_player_info(int pn); +void scoreboard_set_player_avg(int pn, double avg); + +void update_player_name(int pn, char *str); +void update_player_sugg(int pn, int rem); +void update_player_avg(int pn, int n_darts); +void update_player_rem(int pn, int rem); +void clear_player_info(int pn); + +void draw_visits(); + +#endif diff --git a/web/web_ui.c b/web/web_ui.c deleted file mode 100644 index 14b0ee6..0000000 --- a/web/web_ui.c +++ /dev/null @@ -1,185 +0,0 @@ -#include "web_match.h" -#include "web_ui.h" - -#include "checkouts.h" -#include "match.h" - -#include -#include -#include -#include - -#include - -struct player_info { - char *name, *rem, *sugg; - double avg; -}; - -struct player_info buffered_info[2], flushed_info[2]; - -static inline void buffer_str(char *str, char **buffer, char *flushed) -{ - if (str == *buffer || (str && *buffer && !strcmp(str, *buffer))) - return; - - if (*buffer && *buffer != flushed) - free(*buffer); - - *buffer = str ? strdup(str) : NULL; -} - -void ui_set_player_name(int pn, char *str) -{ - buffer_str(str, &buffered_info[pn-1].name, flushed_info[pn-1].name); -} - -void ui_set_player_rem(int pn, char *str) -{ - buffer_str(str, &buffered_info[pn-1].rem, flushed_info[pn-1].rem); -} - -void ui_set_player_sugg(int pn, char *str) -{ - buffer_str(str, &buffered_info[pn-1].sugg, flushed_info[pn-1].sugg); -} - -void ui_set_player_avg(int pn, double avg) -{ - buffered_info[pn-1].avg = avg; -} - -static inline bool buffered_str_changed(char *buffered, char *flushed) -{ - return (!buffered != !flushed) || - (buffered && flushed && strcmp(buffered, flushed)); -} - -static inline void free_flushed_str(char *flushed, char *buffered) -{ - if (flushed && flushed != buffered) - free(flushed); -} - -void ui_flush_player_info(int pn) -{ - if (buffered_str_changed(buffered_info[pn-1].name, - flushed_info[pn-1].name)) - EM_ASM({updatePlayerName($0, $1)}, pn, buffered_info[pn-1].name); - - if (buffered_str_changed(buffered_info[pn-1].rem, - flushed_info[pn-1].rem)) - EM_ASM({updatePlayerRem($0, $1)}, pn, buffered_info[pn-1].rem); - - if (buffered_str_changed(buffered_info[pn-1].sugg, - flushed_info[pn-1].sugg)) - EM_ASM({updatePlayerSugg($0, $1)}, pn, buffered_info[pn-1].sugg); - - if (buffered_info[pn-1].avg != flushed_info[pn-1].avg) - EM_ASM({updatePlayerAvg($0, $1)}, pn, buffered_info[pn-1].avg); - - free_flushed_str(flushed_info[pn-1].name, buffered_info[pn-1].name); - free_flushed_str(flushed_info[pn-1].rem, buffered_info[pn-1].rem); - free_flushed_str(flushed_info[pn-1].sugg, buffered_info[pn-1].sugg); - flushed_info[pn-1] = buffered_info[pn-1]; -} - -void ui_flush() -{ - ui_flush_player_info(1); - ui_flush_player_info(2); -} - -void update_player_name(int pn, char *str) -{ - ui_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)); - } - } - - ui_set_player_sugg(pn, str); -} - -void update_player_avg(int pn, int n_darts) -{ - struct leg *l = state->legs[pn-1]; - ui_set_player_avg(pn, - l->n_visits ? - (l->rem > 0 ? ((double)(l->start - l->rem) / l->n_visits) : - ((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3)) : - 0); -} - -void update_player_rem(int pn, int rem) -{ - char str[5]; - if (rem < 0 || rem == 1) - strcpy(str, "BUST"); - else - sprintf(str, "%d", rem); - - ui_set_player_rem(pn, str); - if (pn == 1 || state->mode != M_PVC) - update_player_sugg(pn, rem); -} - -void clear_player_info(int pn) -{ - ui_set_player_name(pn, NULL); - ui_set_player_rem(pn, NULL); - ui_set_player_sugg(pn, NULL); - ui_set_player_avg(pn, 0); -} - -void draw_visits() -{ - EM_ASM({clearVisits()}); - - char visit_no[10], p1_pts[10], p1_rem[10], - p2_pts[10], p2_rem[10], p2_darts[100]; - - 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, ""); - - 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 visit *v = state->legs[0]->visits + i; - sprintf(p1_pts, "%d", v->points); - sprintf(p1_rem, "%d", v->rem); - - 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); - - 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); - free(n); - } - } - - EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)}, - visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts); - } -} diff --git a/web/web_ui.h b/web/web_ui.h deleted file mode 100644 index 831e3dd..0000000 --- a/web/web_ui.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef WEB_UI_H -#define WEB_UI_H - -#include - -void ui_flush(); - -void ui_flush_player_info(int pn); -void ui_set_player_avg(int pn, double avg); - -void update_player_name(int pn, char *str); -void update_player_sugg(int pn, int rem); -void update_player_avg(int pn, int n_darts); -void update_player_rem(int pn, int rem); -void clear_player_info(int pn); - -void draw_visits(); - -#endif -- cgit v1.2.3-70-g09d2