diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-26 02:42:42 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-26 02:42:42 -0700 |
commit | dca2e78c27b1d73dfbecdeb1a697f6785981db91 (patch) | |
tree | 48bba021096c57905848db7f6160ffde15f4e8f7 /web/dartboat_wasm.c | |
parent | fb4b69761ea08abdce119a0a2962ed475a348f42 (diff) | |
download | dartboat-dca2e78c27b1d73dfbecdeb1a697f6785981db91.tar.gz dartboat-dca2e78c27b1d73dfbecdeb1a697f6785981db91.tar.xz |
web: rewrite prompt handlers in c
Diffstat (limited to 'web/dartboat_wasm.c')
-rw-r--r-- | web/dartboat_wasm.c | 166 |
1 files changed, 161 insertions, 5 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index d9c3540..322127a 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -10,6 +10,28 @@ int delay_ms = 500; +enum prompt_mode { + PM_NONE, + 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; + + if (pm != PM_NONE) + EM_ASM(setPromptActive()); + else + EM_ASM(setPromptInactive()); + + EM_ASM({setKeypad($0)}, pm == PM_SELECT_MODE ? "select_mode" : "default"); +} + enum match_mode { M_FIRST = 1, M_PVC = M_FIRST, @@ -37,10 +59,10 @@ void set_active_player(int pn) if (state->mode == M_PVC && pn == 2) { EM_ASM({promptMsgL($0)}, "Bot is throwing…"); - EM_ASM({setPromptHandler($0)}, ""); + set_prompt_mode(PM_NONE); } else { EM_ASM({promptMsgL($0)}, "Enter points:"); - EM_ASM({setPromptHandler($0)}, "visit"); + set_prompt_mode(PM_VISIT); } EM_ASM({promptMsgR($0)}, ""); } @@ -206,7 +228,7 @@ void clear_player_info(int pn) void prompt_num_darts() { - EM_ASM({setPromptHandler($0)}, "num_darts"); + set_prompt_mode(PM_NUM_DARTS); EM_ASM({promptMsgL($0)}, "Darts needed?"); EM_ASM({promptMsgR($0)}, ""); } @@ -214,7 +236,7 @@ void prompt_num_darts() void prompt_end_match() { EM_ASM(setPlayerActive()); // sets all inactive - EM_ASM({setPromptHandler($0)}, "end_match"); + set_prompt_mode(PM_END_MATCH); EM_ASM({promptMsgL($0)}, state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" : "You win! :)"); @@ -229,7 +251,7 @@ EMSCRIPTEN_KEEPALIVE void prompt_select_mode() } EM_ASM(clearVisits()); - EM_ASM({setPromptHandler($0)}, "select_mode"); + set_prompt_mode(PM_SELECT_MODE); EM_ASM({promptMsgL($0)}, "Select match mode:"); EM_ASM({promptMsgR($0)}, ""); } @@ -432,8 +454,142 @@ void init_boat() EM_ASM({updateStdev($0)}, horizontal_stdev); } +char *prompt_get() +{ + return (char *)EM_ASM_INT({return promptGet()}); +} + +void prompt_handle_pre(char *action) +{ + if (pm == PM_NONE) + return; + + EM_ASM(clearOi()); + + if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) && + strcmp(action, "undo")) + EM_ASM(deactivateUndo()); +} + +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_NONE) + 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_NONE) + return; + + EM_ASM(promptClear()); + prompt_handle_on_change(); +} + +void prompt_handle_submit() +{ + if (pm == PM_END_MATCH) { + end_match(); + return; + } + + if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_SELECT_MODE) + return; + + char *str = prompt_get(); + 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); + prompt_handle_clear(); +} + +void prompt_handle_submit_rem() +{ + if (pm != PM_VISIT) + return; + + char *str = prompt_get(); + if (*str) + user_visit_to_rem(atoi(str)); + free(str); + prompt_handle_clear(); +} + +void prompt_handle_undo() +{ + if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH) + return; + + prompt_handle_clear(); + if (EM_ASM_INT({return isUndoActive()})) + user_undo(); + EM_ASM(toggleUndo()); +} + +EMSCRIPTEN_KEEPALIVE void prompt_handle(char *action, char *data) +{ + prompt_handle_pre(action); + + if (!strcmp(action, "append")) + prompt_handle_append(data); + else if (!strcmp(action, "backspace")) + prompt_handle_backspace(); + else if (!strcmp(action, "clear")) + prompt_handle_clear(); + else if (!strcmp(action, "submit")) + prompt_handle_submit(); + else if (!strcmp(action, "submit_rem")) + prompt_handle_submit_rem(); + else if (!strcmp(action, "undo")) + prompt_handle_undo(); +} + int main() { init_boat(); + set_prompt_mode(PM_VISIT); handle_next(); } |