diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-02 03:29:51 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-02 03:29:51 -0700 |
commit | e462a15fb8b1f5ff505153f1a3e1097a4fbf0e80 (patch) | |
tree | 4ec12048ddd90042556878a7ce0857904bdd651a | |
parent | 22646d963e2b0c9b5ef629aa7704158026cfa6d1 (diff) | |
download | dartboat-e462a15fb8b1f5ff505153f1a3e1097a4fbf0e80.tar.gz dartboat-e462a15fb8b1f5ff505153f1a3e1097a4fbf0e80.tar.xz |
web: split opts and prompt handlers into separate files
Currently using a bunch of forward declarations in prompt.c that should
be cleaned up later.
-rw-r--r-- | web/dartboat_wasm.c | 208 | ||||
-rw-r--r-- | web/web_opts.c | 22 | ||||
-rw-r--r-- | web/web_opts.h | 9 | ||||
-rw-r--r-- | web/web_prompt.c | 168 | ||||
-rw-r--r-- | web/web_prompt.h | 16 |
5 files changed, 238 insertions, 185 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index c43e73d..c245771 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -1,3 +1,6 @@ +#include "web_match.h" +#include "web_opts.h" +#include "web_prompt.h" #include "web_svg.h" #include "web_ui.h" @@ -14,18 +17,6 @@ #include "web_match.h" -int delay_ms = 1000; - -enum prompt_mode { - PM_DARTBOARD, - PM_VISIT, - PM_NUM_DARTS, - PM_END_MATCH, - PM_SELECT_MODE -}; - -enum prompt_mode pm; - void set_prompt_mode(enum prompt_mode mode) { pm = mode; @@ -62,13 +53,15 @@ void toggle_active_player() set_active_player(3 - state->active_player); } -EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) +EMSCRIPTEN_KEEPALIVE +void update_user_rem_from_pts(int pts) { update_player_rem(state->active_player, state->active_leg->rem - pts); } void handle_next(); -EMSCRIPTEN_KEEPALIVE void end_boat_visit(int rem, double avg) +EMSCRIPTEN_KEEPALIVE +void end_boat_visit(int rem, double avg) { EM_ASM(svgClearPoints()); update_player_rem(2, rem); @@ -80,7 +73,8 @@ EMSCRIPTEN_KEEPALIVE void end_boat_visit(int rem, double avg) handle_next(); } -EMSCRIPTEN_KEEPALIVE void boat_visit() +EMSCRIPTEN_KEEPALIVE +void boat_visit() { struct leg *l = state->legs[1]; if (state->boat_undone) { @@ -181,7 +175,8 @@ void handle_next() } } -EMSCRIPTEN_KEEPALIVE void user_visit(int points) +EMSCRIPTEN_KEEPALIVE +void user_visit(int points) { if (!is_points_valid(points, state->active_leg->rem)) { EM_ASM(oi()); @@ -204,12 +199,14 @@ EMSCRIPTEN_KEEPALIVE void user_visit(int points) handle_next(); } -EMSCRIPTEN_KEEPALIVE void user_visit_to_rem(int rem) +EMSCRIPTEN_KEEPALIVE +void user_visit_to_rem(int rem) { user_visit(state->legs[0]->rem - rem); } -EMSCRIPTEN_KEEPALIVE void user_undo() +EMSCRIPTEN_KEEPALIVE +void user_undo() { if (!state->legs[0]->n_visits) { EM_ASM(oi()); @@ -261,7 +258,8 @@ EMSCRIPTEN_KEEPALIVE void user_undo() handle_next(); } -EMSCRIPTEN_KEEPALIVE void user_num_darts(int n) +EMSCRIPTEN_KEEPALIVE +void user_num_darts(int n) { if (n < 1 || n > 3) { EM_ASM(oi()); @@ -274,7 +272,8 @@ EMSCRIPTEN_KEEPALIVE void user_num_darts(int n) handle_next(); } -EMSCRIPTEN_KEEPALIVE void start_match(int mode) +EMSCRIPTEN_KEEPALIVE +void start_match(int mode) { if (mode < M_FIRST || mode > M_LAST) { EM_ASM(oi()); @@ -303,24 +302,15 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode) handle_next(); } -EMSCRIPTEN_KEEPALIVE void end_match() +EMSCRIPTEN_KEEPALIVE +void end_match() { if (state) free_state(); handle_next(); } -EMSCRIPTEN_KEEPALIVE void set_delay(int delay) -{ - delay_ms = delay; -} - -EMSCRIPTEN_KEEPALIVE void set_stdev(float stdev) -{ - if (!isnan(stdev)) - horizontal_stdev = vertical_stdev = stdev; -} - -void init_boat() +EMSCRIPTEN_KEEPALIVE +void init() { srand(time(NULL)); init_board(); @@ -329,159 +319,7 @@ void init_boat() EM_ASM(readOpts()); EM_ASM({updateDelay($0)}, delay_ms); EM_ASM({updateStdev($0)}, horizontal_stdev); -} - -char *prompt_get() -{ - return (char *)EM_ASM_INT({return promptGet()}); -} - -void prompt_handle_pre(char *command) -{ - if (pm == PM_DARTBOARD) - return; - - EM_ASM(clearOi()); - - if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) && - strcmp(command, "undo")) - deactivate_key("undo"); - - if (pm == PM_END_MATCH && strcmp(command, "rem")) - deactivate_key("rem"); - - if (pm == PM_END_MATCH && strcmp(command, "submit")) - deactivate_key("submit"); -} - -void prompt_handle_on_change() -{ - if (pm != PM_VISIT) - return; - - char *str = prompt_get(); - update_user_rem_from_pts(atoi(str)); - free(str); -} - -void prompt_handle_append(char *data) -{ - if (pm != PM_SELECT_MODE && pm != PM_VISIT && pm != PM_NUM_DARTS) - return; - - char *str = prompt_get(); - size_t len_str = strlen(str); - size_t len_data = strlen(data); - if (len_str < 3) { - str = realloc(str, len_str + len_data + 1); - memcpy(str + len_str, data, len_data + 1); - EM_ASM({setPromptInput($0)}, str); - prompt_handle_on_change(); - } - free(str); -} - -void prompt_handle_backspace() -{ - if (pm == PM_DARTBOARD) - return; - char *str = prompt_get(); - size_t len_str = strlen(str); - if (len_str > 0) { - str[len_str-1] = 0; - EM_ASM({setPromptInput($0)}, str); - prompt_handle_on_change(); - } - free(str); -} - -void prompt_handle_clear() -{ - if (pm == PM_DARTBOARD) - return; - - EM_ASM({setPromptInput($0)}, ""); - prompt_handle_on_change(); -} - -void prompt_handle_submit() -{ - if (pm == PM_END_MATCH) { - if (is_key_active("submit")) - end_match(); - toggle_key("submit"); - return; - } - - if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_SELECT_MODE) - return; - - char *str = prompt_get(); - prompt_handle_clear(); - if (*str) { - if (pm == PM_VISIT) - user_visit(atoi(str)); - else if (pm == PM_NUM_DARTS) - user_num_darts(atoi(str)); - else if (pm == PM_SELECT_MODE) - start_match(atoi(str)); - } - - free(str); -} - -void prompt_handle_rem() -{ - if (pm == PM_END_MATCH) { - if (is_key_active("rem")) - start_match(state->mode); - toggle_key("rem"); - return; - } - - if (pm != PM_VISIT) - return; - - char *str = prompt_get(); - prompt_handle_clear(); - if (*str) - user_visit_to_rem(atoi(str)); - free(str); -} - -void prompt_handle_undo() -{ - if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH) - return; - - prompt_handle_clear(); - if (is_key_active("undo")) - user_undo(); - toggle_key("undo"); -} - -EMSCRIPTEN_KEEPALIVE void prompt_handle(char *command, char *data) -{ - prompt_handle_pre(command); - - if (!strcmp(command, "append")) - prompt_handle_append(data); - else if (!strcmp(command, "backspace")) - prompt_handle_backspace(); - else if (!strcmp(command, "clear")) - prompt_handle_clear(); - else if (!strcmp(command, "submit")) - prompt_handle_submit(); - else if (!strcmp(command, "rem")) - prompt_handle_rem(); - else if (!strcmp(command, "undo")) - prompt_handle_undo(); -} - -EMSCRIPTEN_KEEPALIVE void init() -{ - init_boat(); set_prompt_mode(PM_VISIT); handle_next(); } diff --git a/web/web_opts.c b/web/web_opts.c new file mode 100644 index 0000000..1188232 --- /dev/null +++ b/web/web_opts.c @@ -0,0 +1,22 @@ +#include "web_opts.h" + +#include "comp.h" + +#include <math.h> + +#include <emscripten/emscripten.h> + +int delay_ms = 1000; + +EMSCRIPTEN_KEEPALIVE +void set_delay(int delay) +{ + delay_ms = delay; +} + +EMSCRIPTEN_KEEPALIVE +void set_stdev(float stdev) +{ + if (!isnan(stdev)) + horizontal_stdev = vertical_stdev = stdev; +} diff --git a/web/web_opts.h b/web/web_opts.h new file mode 100644 index 0000000..e016373 --- /dev/null +++ b/web/web_opts.h @@ -0,0 +1,9 @@ +#ifndef WEB_OPTS_H +#define WEB_OPTS_H + +extern int delay_ms; + +void set_delay(int delay); +void set_stdev(float stdev); + +#endif diff --git a/web/web_prompt.c b/web/web_prompt.c new file mode 100644 index 0000000..64a23fd --- /dev/null +++ b/web/web_prompt.c @@ -0,0 +1,168 @@ +#include "web_prompt.h" +#include "web_match.h" +#include "web_ui.h" + +#include <stdlib.h> +#include <string.h> + +#include <emscripten/emscripten.h> + +// FIXME forward declaring these until the code is better modularised +void start_match(int mode); +void user_num_darts(int n); +void user_visit(int points); +void user_visit_to_rem(int rem); +void end_match(); +void update_user_rem_from_pts(int pts); +void user_undo(); + +enum prompt_mode pm; + +char *prompt_get() +{ + return (char *)EM_ASM_INT({return promptGet()}); +} + +void prompt_handle_pre(char *command) +{ + if (pm == PM_DARTBOARD) + return; + + EM_ASM(clearOi()); + + if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) && + strcmp(command, "undo")) + deactivate_key("undo"); + + if (pm == PM_END_MATCH && strcmp(command, "rem")) + deactivate_key("rem"); + + if (pm == PM_END_MATCH && strcmp(command, "submit")) + deactivate_key("submit"); +} + +void prompt_handle_on_change() +{ + if (pm != PM_VISIT) + return; + + char *str = prompt_get(); + update_user_rem_from_pts(atoi(str)); + free(str); +} + +void prompt_handle_append(char *data) +{ + if (pm != PM_SELECT_MODE && pm != PM_VISIT && pm != PM_NUM_DARTS) + return; + + char *str = prompt_get(); + size_t len_str = strlen(str); + size_t len_data = strlen(data); + if (len_str < 3) { + str = realloc(str, len_str + len_data + 1); + memcpy(str + len_str, data, len_data + 1); + EM_ASM({setPromptInput($0)}, str); + prompt_handle_on_change(); + } + free(str); +} + +void prompt_handle_backspace() +{ + if (pm == PM_DARTBOARD) + return; + + char *str = prompt_get(); + size_t len_str = strlen(str); + if (len_str > 0) { + str[len_str-1] = 0; + EM_ASM({setPromptInput($0)}, str); + prompt_handle_on_change(); + } + free(str); +} + +void prompt_handle_clear() +{ + if (pm == PM_DARTBOARD) + return; + + EM_ASM({setPromptInput($0)}, ""); + prompt_handle_on_change(); +} + +void prompt_handle_submit() +{ + if (pm == PM_END_MATCH) { + if (is_key_active("submit")) + end_match(); + toggle_key("submit"); + return; + } + + if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_SELECT_MODE) + return; + + char *str = prompt_get(); + prompt_handle_clear(); + if (*str) { + if (pm == PM_VISIT) + user_visit(atoi(str)); + else if (pm == PM_NUM_DARTS) + user_num_darts(atoi(str)); + else if (pm == PM_SELECT_MODE) + start_match(atoi(str)); + } + + free(str); +} + +void prompt_handle_rem() +{ + if (pm == PM_END_MATCH) { + if (is_key_active("rem")) + start_match(state->mode); + toggle_key("rem"); + return; + } + + if (pm != PM_VISIT) + return; + + char *str = prompt_get(); + prompt_handle_clear(); + if (*str) + user_visit_to_rem(atoi(str)); + free(str); +} + +void prompt_handle_undo() +{ + if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH) + return; + + prompt_handle_clear(); + if (is_key_active("undo")) + user_undo(); + toggle_key("undo"); +} + +EMSCRIPTEN_KEEPALIVE +void prompt_handle(char *command, char *data) +{ + prompt_handle_pre(command); + + if (!strcmp(command, "append")) + prompt_handle_append(data); + else if (!strcmp(command, "backspace")) + prompt_handle_backspace(); + else if (!strcmp(command, "clear")) + prompt_handle_clear(); + else if (!strcmp(command, "submit")) + prompt_handle_submit(); + else if (!strcmp(command, "rem")) + prompt_handle_rem(); + else if (!strcmp(command, "undo")) + prompt_handle_undo(); +} diff --git a/web/web_prompt.h b/web/web_prompt.h new file mode 100644 index 0000000..b416f2a --- /dev/null +++ b/web/web_prompt.h @@ -0,0 +1,16 @@ +#ifndef WEB_PROMPT_H +#define WEB_PROMPT_H + +enum prompt_mode { + PM_DARTBOARD, + PM_VISIT, + PM_NUM_DARTS, + PM_END_MATCH, + PM_SELECT_MODE +}; + +extern enum prompt_mode pm; + +void prompt_handle(char *command, char *data); + +#endif |