diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-18 01:33:15 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-18 01:33:15 -0700 |
commit | 07478abb0796219ecd5c36a75c1fbb292909d089 (patch) | |
tree | fe4941c2d08e683f699219dc9edf6a3c65655685 | |
parent | 9072b791c461863aedc85e070a59259f1d15bdaa (diff) | |
download | dartboat-07478abb0796219ecd5c36a75c1fbb292909d089.tar.gz dartboat-07478abb0796219ecd5c36a75c1fbb292909d089.tar.xz |
web: refactor undo logic
-rw-r--r-- | web/static/sw.js | 2 | ||||
-rw-r--r-- | web/web_control.c | 125 | ||||
-rw-r--r-- | web/web_match.c | 1 |
3 files changed, 75 insertions, 53 deletions
diff --git a/web/static/sw.js b/web/static/sw.js index a5fe6fc..1844274 100644 --- a/web/static/sw.js +++ b/web/static/sw.js @@ -1,5 +1,5 @@ const CACHE_PREFIX = 'dartboat-' -const CACHE_VERSION = '13'; +const CACHE_VERSION = '14'; const CACHE_NAME = `${CACHE_PREFIX}${CACHE_VERSION}`; const CACHE_FILES = [ diff --git a/web/web_control.c b/web/web_control.c index 0d8cd46..fac1d8b 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -79,26 +79,9 @@ void draw_boat_throwing(int pts, char *str, double x, double y, int match_id) scoreboard_flush_player_info(2); } -void boat_visit() +static void schedule_boat_visit_draws(struct leg *l, struct visit *v, + double avg) { - struct leg *l = state->legs[1]; - if (state->boat_undone) { - --state->boat_undone; - l->rem = l->visits[l->n_visits++].rem; - } else { - comp_visit(l); - } - - struct visit *v = l->visits + l->n_visits - 1; - double avg = v->rem > 0 ? - (double)(l->start - l->rem) / l->n_visits : - (double)l->start / (((l->n_visits - 1) * 3) + v->n_darts) * 3; - - if (!delay_ms) { - end_boat_visit(l->rem, avg, curr_match_id); - return; - } - int pts = 0; char str[15] = {0}; size_t len_str = 0; @@ -126,6 +109,27 @@ void boat_visit() delay_ms * (v->n_darts + 1), l->rem, avg, curr_match_id); } +void boat_visit() +{ + struct leg *l = state->legs[1]; + if (state->boat_undone) { + --state->boat_undone; + l->rem = l->visits[l->n_visits++].rem; + } else { + comp_visit(l); + } + + struct visit *v = l->visits + l->n_visits - 1; + double avg = v->rem > 0 ? + (double)(l->start - l->rem) / l->n_visits : + (double)l->start / (((l->n_visits - 1) * 3) + v->n_darts) * 3; + + if (delay_ms) + schedule_boat_visit_draws(l, v, avg); + else + end_boat_visit(l->rem, avg, curr_match_id); +} + void handle_next() { if (!state) { @@ -182,8 +186,53 @@ void user_visit_to_rem(int rem) user_visit(state->legs[0]->rem - rem); } +static int num_players() +{ + if (state->mode == M_P) + return 1; + + return 2; +} + +static int prev_throw_player() +{ + if (match_is_over()) + return state->active_player; + + if (state->active_player == 1) + return num_players(); + else + return state->active_player - 1; +} + +static bool player_is_comp(int pn) +{ + return state->mode == M_PVC && pn == 2; +} + +static void undo_active() +{ + struct leg *l = state->active_leg; + + if (player_is_comp(state->active_player)) { + l->rem += l->visits[--l->n_visits].points; + ++state->boat_undone; + } 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); + + state->active_player = prev_throw_player(); + state->active_leg = state->legs[state->active_player - 1]; +} + void user_undo() { + // FIXME: undo doesn't work for first visit when player 2 throws first if (!state->legs[0]->n_visits) { oi(); return; @@ -200,41 +249,13 @@ void user_undo() return; } - if (state->mode == M_PVC) { - set_active_player(1); - } else if (state->mode == M_PVP) { - if (match_is_over()) - set_active_player(state->active_player); - else - toggle_active_player(); - } - - struct leg *l = state->active_leg; - struct visit *v = l->visits + --l->n_visits; - l->rem += v->points; - memset(v, 0, sizeof(*v)); - - if (state->mode == M_PVC && - ((match_opts->throws_first == 1 && - state->legs[1]->n_visits > l->n_visits) || - (match_opts->throws_first == 2 && - state->legs[1]->n_visits > l->n_visits + 1))) { - struct leg *bl = state->legs[1]; - bl->rem += bl->visits[--bl->n_visits].points; - ++state->boat_undone; - } - - update_player_avg(state->active_player, 0); - update_player_rem(state->active_player, l->rem); - if (state->mode == M_PVC) { - update_player_avg(2, 0); - update_player_rem(2, state->legs[1]->rem); - } + state->active_player = prev_throw_player(); + state->active_leg = state->legs[state->active_player - 1]; + while (player_is_comp(state->active_player)) + undo_active(); + undo_active(); draw_visits(); - - if (state->mode != M_P) // FIXME avoid double toggle? - toggle_active_player(); handle_next(); } diff --git a/web/web_match.c b/web/web_match.c index d05efa5..b3e8243 100644 --- a/web/web_match.c +++ b/web/web_match.c @@ -1,5 +1,6 @@ #include "web_match.h" +#include <stdbool.h> #include <stddef.h> #include <stdlib.h> |