From 85c101979a181a76e6250fb1abb75216e08384a3 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 23 May 2022 23:15:34 -0700 Subject: move more match logic from web to lib --- web/web_control.c | 57 +++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'web/web_control.c') diff --git a/web/web_control.c b/web/web_control.c index 96c455e..a7a53cf 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -28,9 +28,9 @@ void set_active_player(int pn) void update_user_rem_from_pts(int pts) { - update_player_rem(state->m->active_player, - state_active_leg()->rem - pts); - scoreboard_flush_player_info(state->m->active_player); + int pn = state->m->active_player; + update_player_rem(pn, state->m->legs[pn - 1]->rem - pts); + scoreboard_flush_player_info(pn); } EMSCRIPTEN_KEEPALIVE @@ -58,17 +58,18 @@ void draw_boat_throwing(int pts, char *str, double x, double y, int match_id) char pts_str[10]; sprintf(pts_str, "%d", pts); - struct leg *l = state_active_leg(); + int pn = state->m->active_player; + struct leg *l = state->m->legs[pn - 1]; int rem = l->n_visits > 1 ? l->visits[l->n_visits-2].rem : l->start; svg_draw_point(x, y); - update_player_rem(state->m->active_player, rem - pts); + update_player_rem(pn, rem - pts); prompt_set_input(pts_str); prompt_set_msgr(str); free(str); prompt_flush(); - scoreboard_flush_player_info(state->m->active_player); + scoreboard_flush_player_info(pn); } static void schedule_boat_visit_draws(struct leg *l, struct visit *v, @@ -103,13 +104,9 @@ static void schedule_boat_visit_draws(struct leg *l, struct visit *v, void boat_visit() { - struct leg *l = state_active_leg(); - if (l->undone_visits) - leg_redo_visit(l); - else - comp_visit(l); + struct leg *l = state->m->legs[state->m->active_player - 1]; + struct visit *v = leg_comp_visit(l, true); - 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; @@ -147,22 +144,18 @@ void handle_next() void user_visit(int points) { - if (!is_points_valid(points, state_active_leg()->rem)) { + int pn = state->m->active_player; + struct leg *l = state->m->legs[pn - 1]; + struct visit *v = leg_pts_visit(l, points); + + if (!v) { oi(); return; } - struct leg *l = state_active_leg(); - if (l->n_visits == l->size_visits) - leg_grow_visits(l); - struct visit *v = l->visits + l->n_visits++; - v->points = points; - l->rem -= points; - v->rem = l->rem; - update_player_rem(state->m->active_player, l->rem); - + update_player_rem(pn, v->rem); if (v->rem > 0) - update_player_avg(state->m->active_player, 0); + update_player_avg(pn, 0); draw_visits(); handle_next(); @@ -170,34 +163,36 @@ void user_visit(int points) void user_visit_to_rem(int rem) { - user_visit(state_active_leg()->rem - rem); + user_visit(state->m->legs[state->m->active_player - 1]->rem - rem); } static void undo_active() { - struct leg *l = state_active_leg(); + int pn = state->m->active_player; + struct leg *l = state->m->legs[pn - 1]; leg_undo_visit(l); - update_player_avg(state->m->active_player, 0); - update_player_rem(state->m->active_player, l->rem); + update_player_avg(pn, 0); + update_player_rem(pn, l->rem); state->m->active_player = match_prev_throw_player(state->m); } void user_undo() { - if (!match_first_user_has_thrown()) { + if (!match_first_user_has_thrown(state->m)) { oi(); return; } if (state->num_darts) { state->num_darts = 0; - struct leg *l = state_active_leg(); - scoreboard_set_player_avg(state->m->active_player, + int pn = state->m->active_player; + struct leg *l = state->m->legs[pn - 1]; + scoreboard_set_player_avg(pn, ((double)(l->start - l->visits[l->n_visits-2].rem) / (l->n_visits - 1))); - scoreboard_set_player_active(state->m->active_player); + scoreboard_set_player_active(pn); handle_next(); return; } -- cgit v1.2.3-70-g09d2