summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
Diffstat (limited to 'web')
-rw-r--r--web/web_control.c15
-rw-r--r--web/web_match.c34
-rw-r--r--web/web_match.h24
-rw-r--r--web/web_prompt.c70
4 files changed, 77 insertions, 66 deletions
diff --git a/web/web_control.c b/web/web_control.c
index a5edb82..c561250 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -235,16 +235,11 @@ void user_num_darts(int n)
void start_match()
{
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");
- if (match_opts->mode == M_PVPVP)
- match_add_player(match_opts->start_pts, PT_USER, "Player 3");
- else if (match_opts->mode == M_PVCVCVC) {
- match_add_player(match_opts->start_pts, PT_COMP, "Computer 2");
- match_add_player(match_opts->start_pts, PT_COMP, "Computer 3");
- }
+
+ for (int i = 0; i < match_opts->num_players; ++i)
+ match_add_player(match_opts->start_pts,
+ match_opts->players[i].type,
+ match_opts->players[i].name);
scoreboard_show_info(match_num_players());
for (int i = 1; i <= match_num_players(); ++i) {
diff --git a/web/web_match.c b/web/web_match.c
index f200f11..0ebc56e 100644
--- a/web/web_match.c
+++ b/web/web_match.c
@@ -16,7 +16,6 @@ void match_new()
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)));
@@ -56,8 +55,38 @@ void free_state()
state = NULL;
}
+void match_opts_new()
+{
+ if (match_opts) match_opts_free();
+
+ match_opts = calloc(1, sizeof(*match_opts));
+
+ match_opts->size_players = 2;
+ match_opts->players = malloc(
+ match_opts->size_players * sizeof(*(match_opts->players)));
+}
+
+void match_opts_add_player(enum player_type type, char *name)
+{
+ if (match_opts->num_players == match_opts->size_players) {
+ match_opts->size_players +=
+ match_opts->size_players ?
+ match_opts->size_players : 1;
+ match_opts->players = realloc(match_opts->players,
+ match_opts->size_players *
+ sizeof(*(match_opts->players)));
+ }
+
+ match_opts->players[match_opts->num_players].type = type;
+ match_opts->players[match_opts->num_players++].name = name;
+}
+
void match_opts_free()
{
+ if (!match_opts)
+ return;
+
+ free(match_opts->players);
free(match_opts);
match_opts = NULL;
}
@@ -69,9 +98,6 @@ struct leg *state_active_leg()
int match_num_players()
{
- if (state->mode == M_P)
- return 1;
-
return state->num_players;
}
diff --git a/web/web_match.h b/web/web_match.h
index fc2dabe..0a5a71d 100644
--- a/web/web_match.h
+++ b/web/web_match.h
@@ -3,17 +3,6 @@
#include "match.h"
-enum match_mode {
- M_FIRST = 1,
- M_PVC = M_FIRST,
- M_P,
- M_PVP,
- M_CVC,
- M_PVPVP,
- M_PVCVCVC,
- M_LAST = M_PVCVCVC
-};
-
enum player_type {
PT_USER,
PT_COMP
@@ -21,7 +10,6 @@ enum player_type {
struct match_state {
int id;
- enum match_mode mode;
struct leg **legs;
enum player_type *player_types;
@@ -32,10 +20,14 @@ struct match_state {
int num_darts;
};
+struct match_opts_player {
+ char *name;
+ enum player_type type;
+};
+
struct match_opts {
- enum match_mode mode;
- enum player_type p1_type, p2_type;
- char *p1_name, *p2_name;
+ struct match_opts_player *players;
+ int num_players, size_players;
int start_pts;
int throws_first;
};
@@ -46,6 +38,8 @@ extern struct match_opts *match_opts;
void match_new();
void match_add_player(int start_pts, enum player_type type, char *name);
void free_state();
+void match_opts_new();
+void match_opts_add_player(enum player_type type, char *name);
void match_opts_free();
struct leg *state_active_leg();
diff --git a/web/web_prompt.c b/web/web_prompt.c
index 1e2e1a1..dc21d62 100644
--- a/web/web_prompt.c
+++ b/web/web_prompt.c
@@ -163,9 +163,7 @@ void menu_display_match_opts()
sprintf(buf, "Starting points: %d", match_opts->start_pts);
add_list_opt(buf);
sprintf(buf, "Throws first: %s",
- match_opts->throws_first == 1 ?
- match_opts->p1_name :
- match_opts->p2_name);
+ match_opts->players[match_opts->throws_first - 1].name);
add_list_opt(buf);
list_back = true;
@@ -187,9 +185,8 @@ void menu_display_throws_first()
{
prompt_set_msgl("Throws first:");
- add_list_opt(match_opts->p1_name);
- if (match_opts->p2_name)
- add_list_opt(match_opts->p2_name);
+ for (int i = 0; i < match_opts->num_players; ++i)
+ add_list_opt(match_opts->players[i].name);
list_back = true;
}
@@ -237,41 +234,40 @@ static void menu_push(enum menu m)
menu_display();
}
-void menu_submit_main(int mode)
+void menu_submit_main(int val)
{
- if (mode < M_FIRST || mode > M_LAST) {
- oi();
- return;
- }
-
- if (match_opts) match_opts_free();
- match_opts = calloc(1, sizeof(*match_opts));
- match_opts->mode = mode;
+ match_opts_new();
match_opts->start_pts = 501;
match_opts->throws_first = 1;
+
// names need to be freed if we stop using string literals
- 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;
- } else if (mode == M_PVPVP) {
- match_opts->p1_name = "Player 1";
- match_opts->p2_name = "Player 2";
- } else if (mode == M_PVCVCVC) {
- match_opts->p1_name = "User";
- match_opts->p2_type = PT_COMP;
- match_opts->p2_name = "Computer 1";
+ switch(val) {
+ case 1:
+ match_opts_add_player(PT_USER, "User");
+ match_opts_add_player(PT_COMP, "Computer");
+ break;
+ case 2:
+ match_opts_add_player(PT_USER, "Player 1");
+ break;
+ case 3:
+ match_opts_add_player(PT_USER, "Player 1");
+ match_opts_add_player(PT_USER, "Player 2");
+ break;
+ case 4:
+ match_opts_add_player(PT_COMP, "Computer 1");
+ match_opts_add_player(PT_COMP, "Computer 2");
+ break;
+ case 5:
+ match_opts_add_player(PT_USER, "Player 1");
+ match_opts_add_player(PT_USER, "Player 2");
+ match_opts_add_player(PT_USER, "Player 3");
+ break;
+ case 6:
+ match_opts_add_player(PT_USER, "Player 1");
+ match_opts_add_player(PT_COMP, "Computer 1");
+ match_opts_add_player(PT_COMP, "Computer 2");
+ match_opts_add_player(PT_COMP, "Computer 3");
+ break;
}
menu_push(MENU_MATCH_OPTS);