From dc7c6379f0ef9db382a5445954a4be37e2ec5640 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sat, 21 May 2022 00:52:50 -0700 Subject: web: reimplement menus as a stack with more shared code --- web/web_prompt.c | 358 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 214 insertions(+), 144 deletions(-) (limited to 'web/web_prompt.c') diff --git a/web/web_prompt.c b/web/web_prompt.c index f65259e..1e2e1a1 100644 --- a/web/web_prompt.c +++ b/web/web_prompt.c @@ -18,15 +18,21 @@ enum prompt_mode { PM_VISIT, PM_NUM_DARTS, PM_END_MATCH, - PM_SELECT_MODE, - PM_MATCH_OPTS, - PM_MATCH_OPT_START_PTS, - PM_MATCH_OPT_THROWS_FIRST + PM_MENU }; enum prompt_mode pm; +enum menu { + MENU_MAIN, + MENU_MATCH_OPTS, + MENU_START_PTS, + MENU_THROWS_FIRST +}; +static enum menu menu_stack[10]; +static int menu_depth = 0; static char *list_optv[10]; static int list_optc; +static bool list_back; static struct { bufstr msgl, msgr, input; @@ -74,11 +80,28 @@ void prompt_flush() dom_set_content("#prompt-input", bufstr_flush(&bufs.input)); } +void set_prompt_mode(enum prompt_mode mode) +{ + pm = mode; + + if (pm != PM_DARTBOARD) + dom_add_class("#prompt", "active"); + else + dom_remove_class("#prompt", "active"); + + dom_set_uniq_class( + pm == PM_DARTBOARD ? "#keypad-dartboard" : + pm == PM_MENU ? "#keypad-list" : + "#keypad-default", + "visible", ".keypad"); +} + static void clear_list_opts() { for (int i = 0; i < list_optc; ++i) free(list_optv[i]); list_optc = 0; + list_back = false; } static void add_list_opt(char *str) @@ -119,97 +142,23 @@ static void flush_list_opts() free(elemv); } -void set_prompt_mode(enum prompt_mode mode) -{ - pm = mode; - - if (pm != PM_DARTBOARD) - dom_add_class("#prompt", "active"); - else - dom_remove_class("#prompt", "active"); - - dom_set_uniq_class( - pm == PM_DARTBOARD ? "#keypad-dartboard" : - pm == PM_SELECT_MODE ? "#keypad-list" : - pm == PM_MATCH_OPTS ? "#keypad-list" : - pm == PM_MATCH_OPT_START_PTS ? "#keypad-list" : - pm == PM_MATCH_OPT_THROWS_FIRST ? "#keypad-list" : - "#keypad-default", - "visible", ".keypad"); -} - -void prompt_user_visit() +void menu_display_main() { - set_prompt_mode(PM_VISIT); - prompt_set_msgl("Enter points:"); - prompt_set_msgr(NULL); - - dom_set_content("#key-submit", "OK"); - dom_set_content("#key-rem", "REMAINING"); -} - -void prompt_comp_visit() -{ - set_prompt_mode(PM_DARTBOARD); - - char buf[64]; - sprintf(buf, "%s is throwing…", state_active_leg()->name); - prompt_set_msgl(buf); - prompt_set_msgr(NULL); -} - -void prompt_num_darts() -{ - set_prompt_mode(PM_NUM_DARTS); - prompt_set_msgl("Darts needed?"); - prompt_set_msgr(NULL); - dom_set_content("#key-submit", "OK"); - dom_set_content("#key-rem", "REMAINING"); -} - -void prompt_end_match() -{ - scoreboard_set_player_active(0); - - char buf[64]; - sprintf(buf, "%s wins.", - state->legs[match_winning_player() - 1]->name); - - set_prompt_mode(PM_END_MATCH); - prompt_set_msgl(buf); - prompt_set_msgr(NULL); - - dom_set_content("#key-submit", "END MATCH"); - dom_set_content("#key-rem", "REMATCH"); -} - -void prompt_select_mode() -{ - scoreboard_hide_info(); - dom_set_content("#visits", NULL); - - set_prompt_mode(PM_SELECT_MODE); prompt_set_msgl("Select match mode:"); - prompt_set_msgr(NULL); - clear_list_opts(); add_list_opt("Play against computer"); add_list_opt("One-player scoreboard"); add_list_opt("Two-player scoreboard"); add_list_opt("Computer vs computer"); add_list_opt("Three-player scoreboard"); add_list_opt("Play against three computers"); - flush_list_opts(); } -void prompt_match_opts() +void menu_display_match_opts() { - set_prompt_mode(PM_MATCH_OPTS); prompt_set_msgl("Match options:"); - prompt_set_msgr(NULL); char buf[64]; - clear_list_opts(); add_list_opt("Start match"); sprintf(buf, "Starting points: %d", match_opts->start_pts); add_list_opt(buf); @@ -218,35 +167,128 @@ void prompt_match_opts() match_opts->p1_name : match_opts->p2_name); add_list_opt(buf); - add_list_opt("Back"); - flush_list_opts(); + + list_back = true; } -void prompt_match_opt_start_pts() +void menu_display_start_pts() { - set_prompt_mode(PM_MATCH_OPT_START_PTS); prompt_set_msgl("Starting points:"); - prompt_set_msgr(NULL); - prompt_flush(); - clear_list_opts(); add_list_opt("301"); add_list_opt("501"); add_list_opt("701"); add_list_opt("901"); - add_list_opt("Back"); + + list_back = true; +} + +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); + + list_back = true; +} + +void menu_display() +{ + if (pm != PM_MENU) + set_prompt_mode(PM_MENU); + + prompt_set_msgl(NULL); + prompt_set_msgr(NULL); + clear_list_opts(); + + switch (menu_stack[menu_depth]) { + case MENU_MAIN: + menu_display_main(); + break; + case MENU_MATCH_OPTS: + menu_display_match_opts(); + break; + case MENU_START_PTS: + menu_display_start_pts(); + break; + case MENU_THROWS_FIRST: + menu_display_throws_first(); + break; + } + + if (list_back) + add_list_opt("Back"); + flush_list_opts(); + prompt_flush(); } -static void match_opt_start_pts_submit(int val) +static void menu_pop() { - if (val < 1 || val > list_optc) { + --menu_depth; + menu_display(); +} + +static void menu_push(enum menu m) +{ + menu_stack[++menu_depth] = m; + menu_display(); +} + +void menu_submit_main(int mode) +{ + if (mode < M_FIRST || mode > M_LAST) { oi(); return; - } else if (val == list_optc) { - goto prompt_next; } + if (match_opts) match_opts_free(); + match_opts = calloc(1, sizeof(*match_opts)); + match_opts->mode = mode; + 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"; + } + + menu_push(MENU_MATCH_OPTS); +} + +static void menu_submit_match_opts(int val) +{ + if (val == 1) + start_match(); + else if (val == 2) + menu_push(MENU_START_PTS); + else if (val == 3) + menu_push(MENU_THROWS_FIRST); +} + +static void menu_submit_start_pts(int val) +{ if (val == 1) match_opts->start_pts = 301; else if (val == 2) @@ -256,58 +298,98 @@ static void match_opt_start_pts_submit(int val) else if (val == 4) match_opts->start_pts = 901; -prompt_next: - prompt_match_opts(); - prompt_flush(); + menu_pop(); } -void prompt_match_opt_throws_first() +static void menu_submit_throws_first(int val) { - set_prompt_mode(PM_MATCH_OPT_THROWS_FIRST); - prompt_set_msgl("Throws first:"); - prompt_set_msgr(NULL); - prompt_flush(); + match_opts->throws_first = val; - clear_list_opts(); - add_list_opt(match_opts->p1_name); - if (match_opts->p2_name) - add_list_opt(match_opts->p2_name); - add_list_opt("Back"); - flush_list_opts(); + menu_pop(); } -static void match_opt_throws_first_submit(int val) +static void menu_submit(int opt) { - if (val < 1 || val > list_optc) { + if (opt < 1 || opt > list_optc) { oi(); return; - } else if (val == list_optc) { - goto prompt_next; } - match_opts->throws_first = val; + if (list_back && opt == list_optc) { + menu_pop(); + return; + } -prompt_next: - prompt_match_opts(); - prompt_flush(); + switch (menu_stack[menu_depth]) { + case MENU_MAIN: + menu_submit_main(opt); + break; + case MENU_MATCH_OPTS: + menu_submit_match_opts(opt); + break; + case MENU_START_PTS: + menu_submit_start_pts(opt); + break; + case MENU_THROWS_FIRST: + menu_submit_throws_first(opt); + break; + } } -static void match_opts_submit(int val) +void prompt_main_menu() { - if (val < 1 || val > list_optc) { - oi(); - return; - } else if (val == list_optc) { - prompt_select_mode(); - prompt_flush(); - } + scoreboard_hide_info(); + dom_set_content("#visits", NULL); - if (val == 1) - start_match(); - else if (val == 2) - prompt_match_opt_start_pts(); - else if (val == 3) - prompt_match_opt_throws_first(); + menu_depth = 0; + menu_stack[0] = MENU_MAIN; + + menu_display(); +} + +void prompt_user_visit() +{ + set_prompt_mode(PM_VISIT); + prompt_set_msgl("Enter points:"); + prompt_set_msgr(NULL); + + dom_set_content("#key-submit", "OK"); + dom_set_content("#key-rem", "REMAINING"); +} + +void prompt_comp_visit() +{ + set_prompt_mode(PM_DARTBOARD); + + char buf[64]; + sprintf(buf, "%s is throwing…", state_active_leg()->name); + prompt_set_msgl(buf); + prompt_set_msgr(NULL); +} + +void prompt_num_darts() +{ + set_prompt_mode(PM_NUM_DARTS); + prompt_set_msgl("Darts needed?"); + prompt_set_msgr(NULL); + dom_set_content("#key-submit", "OK"); + dom_set_content("#key-rem", "REMAINING"); +} + +void prompt_end_match() +{ + scoreboard_set_player_active(0); + + char buf[64]; + sprintf(buf, "%s wins.", + state->legs[match_winning_player() - 1]->name); + + set_prompt_mode(PM_END_MATCH); + prompt_set_msgl(buf); + prompt_set_msgr(NULL); + + dom_set_content("#key-submit", "END MATCH"); + dom_set_content("#key-rem", "REMATCH"); } bool key_is_active(char *k) @@ -371,10 +453,7 @@ void prompt_handle_on_change() void prompt_handle_append(char *data) { - if (pm != PM_MATCH_OPTS && - pm != PM_MATCH_OPT_START_PTS && - pm != PM_MATCH_OPT_THROWS_FIRST && - pm != PM_SELECT_MODE && + if (pm != PM_MENU && pm != PM_VISIT && pm != PM_NUM_DARTS) return; @@ -426,10 +505,7 @@ void prompt_handle_submit() if (pm != PM_VISIT && pm != PM_NUM_DARTS && - pm != PM_SELECT_MODE && - pm != PM_MATCH_OPTS && - pm != PM_MATCH_OPT_START_PTS && - pm != PM_MATCH_OPT_THROWS_FIRST) + pm != PM_MENU) return; char *str = prompt_get(); @@ -439,14 +515,8 @@ void prompt_handle_submit() user_visit(atoi(str)); else if (pm == PM_NUM_DARTS) user_num_darts(atoi(str)); - else if (pm == PM_SELECT_MODE) - match_mode_selected(atoi(str)); - else if (pm == PM_MATCH_OPTS) - match_opts_submit(atoi(str)); - else if (pm == PM_MATCH_OPT_START_PTS) - match_opt_start_pts_submit(atoi(str)); - else if (pm == PM_MATCH_OPT_THROWS_FIRST) - match_opt_throws_first_submit(atoi(str)); + else if (pm == PM_MENU) + menu_submit(atoi(str)); } free(str); -- cgit v1.2.3-70-g09d2