From ea49bdfd2c83a0fced5a43015f5c1ce4f5dbce65 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 25 Apr 2022 18:12:37 -0700 Subject: web: use a common function to determine the next action --- web/dartboat_wasm.c | 90 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 34 deletions(-) (limited to 'web/dartboat_wasm.c') 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(); } -- cgit v1.2.3-70-g09d2