From 90b5ca5e47f0fda21f8da6cb9857a35e0f4730a3 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 9 May 2022 23:39:12 -0700 Subject: web: allow selection of starting points and user to throw first This new option selection code is a bit messy for now. It requires the options and their order to be in sync between the prompt functions and the corresponding submit functions. There's also a bit too much repetition of generic code that's applicable to all options. --- web/web_control.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'web/web_control.c') diff --git a/web/web_control.c b/web/web_control.c index 0c220a5..2890308 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -30,7 +30,6 @@ void toggle_active_player() set_active_player(3 - state->active_player); } -EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) { update_player_rem(state->active_player, state->active_leg->rem - pts); @@ -70,7 +69,6 @@ void draw_boat_throwing(int pts, char *str, double x, double y) scoreboard_flush_player_info(2); // FIXME bot hard-coded as player 2 } -EMSCRIPTEN_KEEPALIVE void boat_visit() { struct leg *l = state->legs[1]; @@ -131,7 +129,7 @@ void handle_next() if (state->active_player) toggle_active_player(); else - set_active_player(1); + set_active_player(match_opts->throws_first); if (state->mode == M_PVC && state->active_player == 2) boat_visit(); @@ -142,7 +140,6 @@ void handle_next() scoreboard_flush(); } -EMSCRIPTEN_KEEPALIVE void user_visit(int points) { if (!is_points_valid(points, state->active_leg->rem)) { @@ -166,13 +163,11 @@ void user_visit(int points) handle_next(); } -EMSCRIPTEN_KEEPALIVE void user_visit_to_rem(int rem) { user_visit(state->legs[0]->rem - rem); } -EMSCRIPTEN_KEEPALIVE void user_undo() { if (!state->legs[0]->n_visits) { @@ -225,7 +220,6 @@ void user_undo() handle_next(); } -EMSCRIPTEN_KEEPALIVE void user_num_darts(int n) { if (n < 1 || n > 3) { @@ -239,26 +233,49 @@ void user_num_darts(int n) handle_next(); } -EMSCRIPTEN_KEEPALIVE -void start_match(int mode) +void match_mode_selected(int mode) { if (mode < M_FIRST || mode > M_LAST) { oi(); return; } + if (match_opts) match_opts_free(); + match_opts = calloc(1, sizeof(*match_opts)); + match_opts->mode = mode; + match_opts->start_pts = 501; + match_opts->throws_first = 1; + // names need to be freed if we stop using string literals + if (mode == M_PVC) { + match_opts->p1_name = "User"; + match_opts->p2_name = "Bot"; + } else if (mode == M_P) { + match_opts->p1_name = "Player 1"; + match_opts->p2_name = NULL; + } else if (mode == M_PVP) { + match_opts->p1_name = "Player 1"; + match_opts->p2_name = "Player 2"; + } + + prompt_match_opts(); + prompt_flush(); +} + +void start_match() +{ if (state) free_state(); // rematch gets us here state = calloc(1, sizeof(*state)); - state->mode = mode; - state->legs[0] = leg_init(501, mode == M_PVP ? "Player 1" : "User"); - state->legs[1] = leg_init(501, mode == M_PVC ? "Bot" : "Player 2"); + state->mode = match_opts->mode; + state->legs[0] = leg_init(match_opts->start_pts, match_opts->p1_name); + state->legs[1] = leg_init(match_opts->start_pts, + match_opts->p2_name ? match_opts->p2_name : "oi"); show_player_info(1); update_player_name(1, state->legs[0]->name); update_player_rem(1, state->legs[0]->rem); update_player_avg(1, 0); - if (mode != M_P) { + if (state->mode != M_P) { show_player_info(2); update_player_name(2, state->legs[1]->name); update_player_rem(2, state->legs[1]->rem); @@ -269,7 +286,6 @@ void start_match(int mode) handle_next(); } -EMSCRIPTEN_KEEPALIVE void end_match() { if (state) free_state(); -- cgit v1.2.3-70-g09d2