summaryrefslogtreecommitdiff
path: root/web/web_control.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-18 20:29:25 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-18 20:30:17 -0700
commit5a1032e3e4b3479c84807d5610335c7e4f850158 (patch)
tree045b26df714e44c270f428daea2e83bc4b2e3b1a /web/web_control.c
parentd4a44af5efa5d3847a4ec4c545d585770e92e4b9 (diff)
downloaddartboat-5a1032e3e4b3479c84807d5610335c7e4f850158.tar.gz
dartboat-5a1032e3e4b3479c84807d5610335c7e4f850158.tar.xz
web: store comp info as part of match player info instead of globally
This allows us things like two computer players against one another, which I've also added for testing.
Diffstat (limited to 'web/web_control.c')
-rw-r--r--web/web_control.c35
1 files changed, 18 insertions, 17 deletions
diff --git a/web/web_control.c b/web/web_control.c
index 0c08b40..e89cc8f 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -15,8 +15,6 @@
#include <emscripten/emscripten.h>
-static int curr_match_id = 0;
-
void set_active_player(int pn)
{
state->active_player = pn;
@@ -37,7 +35,7 @@ void update_user_rem_from_pts(int pts)
EMSCRIPTEN_KEEPALIVE
void end_boat_visit(int rem, double avg, int match_id)
{
- if (!state || match_id != curr_match_id)
+ if (!state || match_id != state->id)
return;
svg_clear_points();
@@ -53,7 +51,7 @@ void end_boat_visit(int rem, double avg, int match_id)
EMSCRIPTEN_KEEPALIVE
void draw_boat_throwing(int pts, char *str, double x, double y, int match_id)
{
- if (!state || match_id != curr_match_id)
+ if (!state || match_id != state->id)
return;
char pts_str[10];
@@ -95,18 +93,18 @@ static void schedule_boat_visit_draws(struct leg *l, struct visit *v,
memcpy(tmp, str, len_str + 1);
EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5, $6)},
"draw_boat_throwing", delay,
- pts, tmp, c.x, c.y, curr_match_id);
+ pts, tmp, c.x, c.y, state->id);
}
EM_ASM({scheduleCCall($0, $1, $2, $3, $4)}, "end_boat_visit",
- delay_ms * (v->n_darts + 1), l->rem, avg, curr_match_id);
+ delay_ms * (v->n_darts + 1), l->rem, avg, state->id);
}
void boat_visit()
{
struct leg *l = state_active_leg();
- if (state->boat_undone) {
- --state->boat_undone;
+ if (state->comp_undone[state->active_player - 1]) {
+ --state->comp_undone[state->active_player - 1];
l->rem = l->visits[l->n_visits++].rem;
} else {
comp_visit(l);
@@ -120,7 +118,7 @@ void boat_visit()
if (delay_ms)
schedule_boat_visit_draws(l, v, avg);
else
- end_boat_visit(l->rem, avg, curr_match_id);
+ end_boat_visit(l->rem, avg, state->id);
}
void handle_next()
@@ -181,7 +179,7 @@ static void undo_active()
if (match_player_is_comp(state->active_player)) {
l->rem += l->visits[--l->n_visits].points;
- ++state->boat_undone;
+ ++state->comp_undone[state->active_player - 1];
} else {
struct visit *v = l->visits + --l->n_visits;
l->rem += v->points;
@@ -250,12 +248,18 @@ void match_mode_selected(int mode)
if (mode == M_PVC) {
match_opts->p1_name = "User";
match_opts->p2_name = "Computer";
+ match_opts->p2_type = PT_COMP;
} 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";
+ } else if (mode == M_CVC) {
+ match_opts->p1_name = "Computer 1";
+ match_opts->p1_type = PT_COMP;
+ match_opts->p2_name = "Computer 2";
+ match_opts->p2_type = PT_COMP;
}
prompt_match_opts();
@@ -264,13 +268,10 @@ void match_mode_selected(int mode)
void start_match()
{
- if (state) free_state(); // rematch gets us here
- ++curr_match_id;
-
- state = calloc(1, sizeof(*state));
- 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_new();
+ match_add_player(match_opts->start_pts, match_opts->p1_type,
+ match_opts->p1_name);
+ match_add_player(match_opts->start_pts, match_opts->p2_type,
match_opts->p2_name ? match_opts->p2_name : "oi");
show_player_info(1);