diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-23 15:23:48 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-23 15:25:07 -0700 |
commit | 15ff00e056a98ab4210dde3d4461a88849d6c1d4 (patch) | |
tree | 280b8d248baee5d2ae0dcc2117e3f2b309d128ec /web/web_control.c | |
parent | c93897ed2013f20d6ece33815971d206fd0d056e (diff) | |
download | dartboat-15ff00e056a98ab4210dde3d4461a88849d6c1d4.tar.gz dartboat-15ff00e056a98ab4210dde3d4461a88849d6c1d4.tar.xz |
move a bunch of general match logic from web to lib
Diffstat (limited to 'web/web_control.c')
-rw-r--r-- | web/web_control.c | 79 |
1 files changed, 36 insertions, 43 deletions
diff --git a/web/web_control.c b/web/web_control.c index c561250..d06e17e 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -17,7 +17,7 @@ void set_active_player(int pn) { - state->active_player = pn; + state->m->active_player = pn; scoreboard_set_player_active(pn); if (match_player_is_comp(pn)) @@ -28,8 +28,9 @@ void set_active_player(int pn) void update_user_rem_from_pts(int pts) { - update_player_rem(state->active_player, state_active_leg()->rem - pts); - scoreboard_flush_player_info(state->active_player); + update_player_rem(state->m->active_player, + state_active_leg()->rem - pts); + scoreboard_flush_player_info(state->m->active_player); } EMSCRIPTEN_KEEPALIVE @@ -39,8 +40,8 @@ void end_boat_visit(int rem, double avg, int match_id) return; svg_clear_points(); - update_player_rem(state->active_player, rem); - scoreboard_set_player_avg(state->active_player, avg); + update_player_rem(state->m->active_player, rem); + scoreboard_set_player_avg(state->m->active_player, avg); prompt_set_input(NULL); prompt_set_msgr(NULL); @@ -61,13 +62,13 @@ void draw_boat_throwing(int pts, char *str, double x, double y, int match_id) int rem = l->n_visits > 1 ? l->visits[l->n_visits-2].rem : l->start; svg_draw_point(x, y); - update_player_rem(state->active_player, rem - pts); + update_player_rem(state->m->active_player, rem - pts); prompt_set_input(pts_str); prompt_set_msgr(str); free(str); prompt_flush(); - scoreboard_flush_player_info(state->active_player); + scoreboard_flush_player_info(state->m->active_player); } static void schedule_boat_visit_draws(struct leg *l, struct visit *v, @@ -103,12 +104,10 @@ static void schedule_boat_visit_draws(struct leg *l, struct visit *v, void boat_visit() { struct leg *l = state_active_leg(); - if (state->comp_undone[state->active_player - 1]) { - --state->comp_undone[state->active_player - 1]; - l->rem = l->visits[l->n_visits++].rem; - } else { + if (l->undone_visits) + leg_redo_visit(l); + else comp_visit(l); - } struct visit *v = l->visits + l->n_visits - 1; double avg = v->rem > 0 ? @@ -123,21 +122,22 @@ void boat_visit() void handle_next() { + int wp; + if (!state) { prompt_main_menu(); - } else if (match_is_over()) { - if (state->num_darts || - match_player_is_comp(match_winning_player())) + } else if ((wp = match_winning_player(state->m))) { + if (state->num_darts || match_player_is_comp(wp)) prompt_end_match(); else prompt_num_darts(); } else { - if (state->active_player) - set_active_player(match_next_player()); + if (state->m->active_player) + set_active_player(match_next_player(state->m)); else set_active_player(match_opts->throws_first); - if (match_player_is_comp(state->active_player)) + if (match_player_is_comp(state->m->active_player)) boat_visit(); } @@ -159,10 +159,10 @@ void user_visit(int points) v->points = points; l->rem -= points; v->rem = l->rem; - update_player_rem(state->active_player, l->rem); + update_player_rem(state->m->active_player, l->rem); if (v->rem > 0) - update_player_avg(state->active_player, 0); + update_player_avg(state->m->active_player, 0); draw_visits(); handle_next(); @@ -176,20 +176,12 @@ void user_visit_to_rem(int rem) static void undo_active() { struct leg *l = state_active_leg(); + leg_undo_visit(l); - if (match_player_is_comp(state->active_player)) { - l->rem += l->visits[--l->n_visits].points; - ++state->comp_undone[state->active_player - 1]; - } else { - struct visit *v = l->visits + --l->n_visits; - l->rem += v->points; - memset(v, 0, sizeof(*v)); - } - - update_player_avg(state->active_player, 0); - update_player_rem(state->active_player, l->rem); + update_player_avg(state->m->active_player, 0); + update_player_rem(state->m->active_player, l->rem); - state->active_player = match_prev_throw_player(); + state->m->active_player = match_last_player_to_throw(state->m); } void user_undo() @@ -202,16 +194,16 @@ void user_undo() if (state->num_darts) { state->num_darts = 0; struct leg *l = state_active_leg(); - scoreboard_set_player_avg(state->active_player, + scoreboard_set_player_avg(state->m->active_player, ((double)(l->start - l->visits[l->n_visits-2].rem) / (l->n_visits - 1))); - scoreboard_set_player_active(state->active_player); + scoreboard_set_player_active(state->m->active_player); handle_next(); return; } - state->active_player = match_prev_throw_player(); - while (match_player_is_comp(state->active_player)) + state->m->active_player = match_last_player_to_throw(state->m); + while (match_player_is_comp(state->m->active_player)) undo_active(); undo_active(); @@ -227,7 +219,7 @@ void user_num_darts(int n) } state->num_darts = n; - update_player_avg(state->active_player, n); + update_player_avg(state->m->active_player, n); handle_next(); } @@ -237,14 +229,15 @@ void start_match() match_new(); for (int i = 0; i < match_opts->num_players; ++i) - match_add_player(match_opts->start_pts, + match_add_player(state->m, match_opts->players[i].type, - match_opts->players[i].name); + match_opts->players[i].name, + match_opts->start_pts); - scoreboard_show_info(match_num_players()); - for (int i = 1; i <= match_num_players(); ++i) { - update_player_name(i, state->legs[i - 1]->name); - update_player_rem(i, state->legs[i - 1]->rem); + scoreboard_show_info(state->m->n_players); + for (int i = 1; i <= state->m->n_players; ++i) { + update_player_name(i, state->m->players[i - 1].name); + update_player_rem(i, state->m->legs[i - 1]->rem); update_player_avg(i, 0); } |