diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-18 20:29:25 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-18 20:30:17 -0700 |
commit | 5a1032e3e4b3479c84807d5610335c7e4f850158 (patch) | |
tree | 045b26df714e44c270f428daea2e83bc4b2e3b1a /web/web_match.c | |
parent | d4a44af5efa5d3847a4ec4c545d585770e92e4b9 (diff) | |
download | dartboat-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_match.c')
-rw-r--r-- | web/web_match.c | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/web/web_match.c b/web/web_match.c index aabea4d..bdd049d 100644 --- a/web/web_match.c +++ b/web/web_match.c @@ -8,12 +8,50 @@ struct match_state *state; struct match_opts *match_opts; +static int curr_id = 0; + +void match_new() +{ + if (state) free_state(); + + state = calloc(1, sizeof(*state)); + state->id = ++curr_id; + state->mode = match_opts->mode; + + state->size_players = 2; + state->legs = malloc(state->size_players * sizeof(*(state->legs))); + state->player_types = malloc(state->size_players * + sizeof(*(state->player_types))); + state->comp_undone = malloc(state->size_players * + sizeof(*(state->comp_undone))); +} + +void match_add_player(int start_pts, enum player_type type, char *name) +{ + if (state->num_players == state->size_players) { + state->size_players += + state->size_players ? state->size_players : 1; + state->legs = realloc(state->legs, + state->size_players * sizeof(*(state->legs))); + state->player_types = realloc(state->player_types, + state->size_players * sizeof(*(state->player_types))); + state->comp_undone = realloc(state->comp_undone, + state->size_players * sizeof(*(state->comp_undone))); + } + + state->legs[state->num_players] = leg_init(start_pts, name); + state->player_types[state->num_players] = type; + state->comp_undone[state->num_players++] = 0; +} void free_state() { - state->legs[1]->n_visits += state->boat_undone; // avoid memory leak - leg_free(state->legs[0]); - leg_free(state->legs[1]); + for (int i = 0; i < state->num_players; ++i) { + // undone visits readded before free to avoid memory leak + state->legs[i]->n_visits += state->comp_undone[i]; + leg_free(state->legs[i]); + } + free(state->legs); free(state); state = NULL; } @@ -78,7 +116,7 @@ int match_prev_throw_player() bool match_player_is_comp(int pn) { - return state->mode == M_PVC && pn == 2; + return state->player_types[pn - 1] == PT_COMP; } bool match_first_user_has_thrown() |