diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-06 01:01:53 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-06 01:01:53 -0700 |
commit | 923b945be41739c109b5452567a521fa03c44498 (patch) | |
tree | 54296ec9727e5b8e1da669faf99514a6ebf0781e /web/web_scoreboard.c | |
parent | f2923a29cffcfd866ef929518f753f4cd4dee9ec (diff) | |
download | dartboat-923b945be41739c109b5452567a521fa03c44498.tar.gz dartboat-923b945be41739c109b5452567a521fa03c44498.tar.xz |
web: simplify buffering of display data
Diffstat (limited to 'web/web_scoreboard.c')
-rw-r--r-- | web/web_scoreboard.c | 69 |
1 files changed, 17 insertions, 52 deletions
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c index 71b239d..d0a5947 100644 --- a/web/web_scoreboard.c +++ b/web/web_scoreboard.c @@ -1,5 +1,6 @@ #include "web_dom.h" #include "web_match.h" +#include "web_misc.h" #include "web_scoreboard.h" #include "checkouts.h" @@ -12,11 +13,9 @@ #include <emscripten/emscripten.h> -struct player_info { - char *name, *rem, *sugg, *avg; -}; - -struct player_info buffered_info[2], flushed_info[2]; +static struct { + bufstr name, rem, sugg, avg; +} bufs[2]; void scoreboard_set_player_active(int pn) { @@ -30,86 +29,52 @@ void scoreboard_set_player_active(int pn) dom_set_uniq_class(psel, "active", "[id$=-info]"); } -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); + bufstr_buf(&bufs[pn-1].name, str); } void scoreboard_set_player_rem(int pn, char *str) { - buffer_str(str, &buffered_info[pn-1].rem, flushed_info[pn-1].rem); + bufstr_buf(&bufs[pn-1].rem, str); } void scoreboard_set_player_sugg(int pn, char *str) { - buffer_str(str, &buffered_info[pn-1].sugg, flushed_info[pn-1].sugg); + bufstr_buf(&bufs[pn-1].sugg, str); } void scoreboard_set_player_avg(int pn, double avg) { char *str = malloc(8); sprintf(str, "%.2f", avg); - buffer_str(str, &buffered_info[pn-1].avg, flushed_info[pn-1].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); + bufstr_buf(&bufs[pn-1].avg, str); } void scoreboard_flush_player_info(int pn) { - char sel[64]; + char sel[32]; int len = sprintf(sel, "#p%d-", pn); - if (buffered_str_changed(buffered_info[pn-1].name, - flushed_info[pn-1].name)) { + if (bufstr_changed(&bufs[pn-1].name)) { strcpy(sel + len, "name"); - dom_set_content(sel, buffered_info[pn-1].name); + dom_set_content(sel, bufstr_flush(&bufs[pn-1].name)); } - if (buffered_str_changed(buffered_info[pn-1].rem, - flushed_info[pn-1].rem)) { + if (bufstr_changed(&bufs[pn-1].rem)) { strcpy(sel + len, "rem"); - dom_set_content(sel, buffered_info[pn-1].rem); + dom_set_content(sel, bufstr_flush(&bufs[pn-1].rem)); } - if (buffered_str_changed(buffered_info[pn-1].sugg, - flushed_info[pn-1].sugg)) { + if (bufstr_changed(&bufs[pn-1].sugg)) { strcpy(sel + len, "sugg"); - dom_set_content(sel, buffered_info[pn-1].sugg); + dom_set_content(sel, bufstr_flush(&bufs[pn-1].sugg)); } - if (buffered_str_changed(buffered_info[pn-1].avg, - flushed_info[pn-1].avg)) { + if (bufstr_changed(&bufs[pn-1].avg)) { strcpy(sel + len, "avg"); - dom_set_content(sel, buffered_info[pn-1].avg); + dom_set_content(sel, bufstr_flush(&bufs[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); - free_flushed_str(flushed_info[pn-1].avg, buffered_info[pn-1].avg); - - flushed_info[pn-1] = buffered_info[pn-1]; } void scoreboard_flush() |