diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-02 15:20:34 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-02 15:20:34 -0700 |
commit | 237359d107c5f8fb3e1f7a3532696eb19a6c56fc (patch) | |
tree | 192f6007e9a59c334477dd6ab6ce209b067614ba /web/web_ui.c | |
parent | 09c23f60189223db58d98e49ee94df947667662d (diff) | |
download | dartboat-237359d107c5f8fb3e1f7a3532696eb19a6c56fc.tar.gz dartboat-237359d107c5f8fb3e1f7a3532696eb19a6c56fc.tar.xz |
web: buffer player info and flush on state change
Constant calls from Wasm to JS to replace unchanged elements in the DOM
is not very efficient.
Diffstat (limited to 'web/web_ui.c')
-rw-r--r-- | web/web_ui.c | 100 |
1 files changed, 93 insertions, 7 deletions
diff --git a/web/web_ui.c b/web/web_ui.c index 7ac4182..070b898 100644 --- a/web/web_ui.c +++ b/web/web_ui.c @@ -12,6 +12,90 @@ #include <emscripten/emscripten.h> +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; @@ -25,13 +109,13 @@ void update_player_sugg(int pn, int rem) } } - EM_ASM({updatePlayerSugg($0, $1)}, pn, str); + ui_set_player_sugg(pn, str); } void update_player_avg(int pn, int n_darts) { struct leg *l = state->legs[pn-1]; - EM_ASM({updatePlayerAvg($0, $1)}, pn, + 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)) : @@ -88,17 +172,17 @@ void update_player_rem(int pn, int rem) else sprintf(str, "%d", rem); - EM_ASM({updatePlayerRem($0, $1)}, pn, str); + ui_set_player_rem(pn, str); if (pn == 1 || state->mode != M_PVC) update_player_sugg(pn, rem); } void clear_player_info(int pn) { - EM_ASM({updatePlayerName($0, $1)}, pn, ""); - EM_ASM({updatePlayerRem($0, $1)}, pn, ""); - EM_ASM({updatePlayerSugg($0, $1)}, pn, ""); - EM_ASM({updatePlayerAvg($0, $1)}, pn, 0); + ui_set_player_name(pn, ""); + ui_set_player_rem(pn, ""); + ui_set_player_sugg(pn, ""); + ui_set_player_avg(pn, 0); } EMSCRIPTEN_KEEPALIVE @@ -116,6 +200,8 @@ void draw_boat_throwing(int pts, char *str, double x, double y) EM_ASM({setPromptInput($0)}, pts_str); EM_ASM({promptMsgR($0)}, str); free(str); + + ui_flush_player_info(2); // FIXME bot hard-coded as player 2 } bool is_key_active(char *k) |