diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-25 18:12:37 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-25 18:12:37 -0700 |
commit | ea49bdfd2c83a0fced5a43015f5c1ce4f5dbce65 (patch) | |
tree | bfadc5d352adb0d4c7926c22826f67268b6145f0 /web | |
parent | 69325ef5bbd7f34e894222b9c416c94f4236ac2c (diff) | |
download | dartboat-ea49bdfd2c83a0fced5a43015f5c1ce4f5dbce65.tar.gz dartboat-ea49bdfd2c83a0fced5a43015f5c1ce4f5dbce65.tar.xz |
web: use a common function to determine the next action
Diffstat (limited to 'web')
-rw-r--r-- | web/dartboat_wasm.c | 90 | ||||
-rw-r--r-- | web/static/dartboat.js | 8 | ||||
-rw-r--r-- | web/static/index.html | 2 | ||||
-rw-r--r-- | web/static/style.css | 2 |
4 files changed, 62 insertions, 40 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index 936af7d..6580716 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -23,6 +23,7 @@ struct match_state { struct leg *l1, *l2, *active_l; int active_p; int undone_count; + bool num_darts; }; struct match_state *state; @@ -69,10 +70,10 @@ bool is_match_over() return state->l1->rem <= 0 || state->l2->rem <= 0; } -void prompt_match_over() +void prompt_end_match() { EM_ASM(setPlayerActive()); // sets all inactive - EM_ASM({setPromptHandler($0)}, "match_over"); + EM_ASM({setPromptHandler($0)}, "end_match"); EM_ASM({promptMsgR($0)}, "Press OK to end match."); } @@ -149,7 +150,7 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) EM_ASM({promptMsgR($0)}, str); } -void post_visit_checks(); +void handle_next(); EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, double avg) { update_player_rem(2, rem); @@ -158,7 +159,7 @@ EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, double avg) EM_ASM({promptMsgR($0)}, ""); draw_visits(); - post_visit_checks(); + handle_next(); } EMSCRIPTEN_KEEPALIVE void boat_visit() @@ -200,18 +201,46 @@ EMSCRIPTEN_KEEPALIVE void boat_visit() "number", l->rem, "number", avg); } -void post_visit_checks() +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); +} + +EMSCRIPTEN_KEEPALIVE void prompt_match_mode() +{ + for (int pn = 1; pn < 3; ++pn) { + EM_ASM({hidePlayerInfo($0)}, pn); + clear_player_info(pn); + } + EM_ASM(clearVisits()); + + EM_ASM({setPromptHandler($0)}, "match_mode"); + EM_ASM({promptMsgR($0)}, ""); + EM_ASM({promptMsgL($0)}, "Select match mode:"); +} + +void handle_next() { - if (is_match_over()) { + if (!state) { + prompt_match_mode(); + } else if (is_match_over()) { if (state->mode == M_PVC && state->l2->rem <= 0) { EM_ASM({promptMsgL($0)}, "Bot wins. :("); - prompt_match_over(); - } else { + prompt_end_match(); + } else if (!state->num_darts) { EM_ASM({setPromptHandler($0)}, "num_darts"); EM_ASM({promptMsgL($0)}, "Darts needed?"); + } else { + EM_ASM({promptMsgL($0)}, "You win! :)"); + prompt_end_match(); } } else { - if (state->mode != M_P) { + if (state->mode == M_P) { + set_active_player(1); + } else { toggle_active_player(); if (state->mode == M_PVC && state->active_p == 2) @@ -246,7 +275,7 @@ EMSCRIPTEN_KEEPALIVE void user_visit(int points) (double)(l->start-l->rem)/l->n_visits); draw_visits(); - post_visit_checks(); + handle_next(); } EMSCRIPTEN_KEEPALIVE void user_visit_to_rem(int rem) @@ -274,6 +303,7 @@ EMSCRIPTEN_KEEPALIVE void user_undo() struct visit *v = l->visits + --l->n_visits; l->rem += v->points; memcpy(v, 0, sizeof(*v)); + state->num_darts = 0; if (state->mode == M_PVC && state->l2->n_visits > l->n_visits) { state->l2->rem += state->l2->visits[--state->l2->n_visits].points; @@ -281,7 +311,9 @@ EMSCRIPTEN_KEEPALIVE void user_undo() } EM_ASM({updatePlayerAvg($0, $1)}, 1, - l->n_visits ? (double)(l->start - l->rem) / l->n_visits : 0); + state->l1->n_visits ? + (double)(state->l1->start - state->l1->rem) / state->l1->n_visits : + 0); EM_ASM({updatePlayerAvg($0, $1)}, 2, state->l2->n_visits ? (double)(state->l2->start - state->l2->rem) / state->l2->n_visits : @@ -291,6 +323,10 @@ EMSCRIPTEN_KEEPALIVE void user_undo() update_player_rem(2, state->l2->rem); draw_visits(); + + if (state->mode != M_P) // FIXME avoid double toggle? + toggle_active_player(); + handle_next(); } EMSCRIPTEN_KEEPALIVE void user_num_darts(int n) @@ -300,12 +336,13 @@ EMSCRIPTEN_KEEPALIVE void user_num_darts(int n) return; } - struct leg *l = state->active_l; + state->num_darts = n; + struct leg *l = state->active_l; EM_ASM({updatePlayerAvg($0, $1)}, state->active_p, ((double)l->start / (((l->n_visits - 1) * 3) + n)) * 3); - EM_ASM({promptMsgL($0)}, "You win! :)"); - prompt_match_over(); + + handle_next(); } void free_state() @@ -324,7 +361,7 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode) return; } - if (state) free_state(); + if (state) free_state(); // FIXME state = calloc(1, sizeof(*state)); state->mode = mode; state->l1 = leg_init(501, mode == M_PVP ? "Player 1" : "User"); @@ -346,25 +383,10 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode) draw_visits(); } -void clear_player_info(int pn) +EMSCRIPTEN_KEEPALIVE void end_match() { - 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); -} - -EMSCRIPTEN_KEEPALIVE void prompt_match_mode() -{ - for (int pn = 1; pn < 3; ++pn) { - EM_ASM({hidePlayerInfo($0)}, pn); - clear_player_info(pn); - } - EM_ASM(clearVisits()); - - EM_ASM({setPromptHandler($0)}, "init"); - EM_ASM({promptMsgR($0)}, ""); - EM_ASM({promptMsgL($0)}, "Select match mode:"); + if (state) free_state(); + handle_next(); } EMSCRIPTEN_KEEPALIVE void set_delay(int delay) @@ -391,5 +413,5 @@ void init_boat() int main() { init_boat(); - prompt_match_mode(); + handle_next(); } diff --git a/web/static/dartboat.js b/web/static/dartboat.js index cb84fe3..cb8e9dd 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -113,19 +113,19 @@ prompt_handlers.num_darts = { } }; -prompt_handlers.match_over = { +prompt_handlers.end_match = { __proto__: prompt_handlers.default_with_undo, append() {}, submit() { - Module.ccall('prompt_match_mode'); + Module.ccall('end_match'); } }; -prompt_handlers.init = { +prompt_handlers.match_mode = { __proto__: prompt_handlers.default, - _keypad: 'init', + _keypad: 'match_mode', submit() { let v = this._get_and_clear(); diff --git a/web/static/index.html b/web/static/index.html index 5fb11a8..e1384bb 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -47,7 +47,7 @@ <div onclick="promptHandle('undo')" class="key" id="key_undo">UNDO</div> <div onclick="promptHandle('submit_rem')" class="key">REMAINING</div> </div> - <div id="keypad-init" class="keypad"> + <div id="keypad-match_mode" class="keypad"> <div onclick="promptHandle('append', 1); promptHandle('submit')" class="key"><span class="keyboard-val">[1]</span> Play against bot</div> <div onclick="promptHandle('append', 2); promptHandle('submit')" class="key"><span class="keyboard-val">[2]</span> One-player scoreboard</div> <div onclick="promptHandle('append', 3); promptHandle('submit')" class="key"><span class="keyboard-val">[3]</span> Two-player scoreboard</div> diff --git a/web/static/style.css b/web/static/style.css index 2a3d60b..519a437 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -42,7 +42,7 @@ div#controls .keypad { display: contents; } -div#controls #keypad-init .key { +div#controls #keypad-match_mode .key { grid-column: 1 / -1; padding-left: 0.4rem; |