#include "web_control.h" #include "web_dom.h" #include "web_prompt.h" #include "web_match.h" #include "web_misc.h" #include "web_scoreboard.h" #include #include #include #include int oi_timeout; enum prompt_mode { PM_DARTBOARD, PM_VISIT, PM_NUM_DARTS, PM_END_MATCH, 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; } bufs; void oi() { dom_add_class("#oi", "visible"); oi_timeout = EM_ASM_INT({return scheduleCCall($0, $1)}, "clear_oi", 3000); } EMSCRIPTEN_KEEPALIVE void clear_oi() { if (!oi_timeout) return; dom_remove_class("#oi", "visible"); EM_ASM({clearTimeout($0)}, oi_timeout); oi_timeout = 0; } void prompt_set_msgl(char *str) { bufstr_buf(&bufs.msgl, str); } void prompt_set_msgr(char *str) { bufstr_buf(&bufs.msgr, str); } void prompt_set_input(char *str) { bufstr_buf(&bufs.input, str); } void prompt_flush() { if (bufstr_changed(&bufs.msgl)) dom_set_content("#prompt-msg-l", bufstr_flush(&bufs.msgl)); if (bufstr_changed(&bufs.msgr)) dom_set_content("#prompt-msg-r", bufstr_flush(&bufs.msgr)); if (bufstr_changed(&bufs.input)) 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) { list_optv[list_optc++] = strdup(str); } static void flush_list_opts() { struct dom_elem **elemv = malloc(list_optc * sizeof(*elemv)); for (int i = 0; i < list_optc; ++i) { struct dom_elem *e = dom_elem_init(NULL, "div", 2); dom_elem_add_attr(e, "class", "key"); char buf[64]; sprintf(buf, "append:%d;submit", i + 1); dom_elem_add_attr(e, "data-command", buf); struct dom_elem *child; child = dom_elem_init(NULL, "span", 1); asprintf(&child->content, "[%d]", i + 1); dom_elem_add_attr(child, "class", "keyboard-val"); dom_elem_add_child(e, child); sprintf(buf, "\u2002%s", list_optv[i]); child = dom_text_init(buf); dom_elem_add_child(e, child); elemv[i] = e; } dom_set_content("#keypad-list", NULL); dom_append_elemv("#keypad-list", list_optc, elemv); for (int i = 0; i < list_optc; ++i) dom_elem_free(elemv[i]); free(elemv); } void menu_display_main() { prompt_set_msgl("Select match mode:"); 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"); } void menu_display_match_opts() { prompt_set_msgl("Match options:"); char buf[64]; add_list_opt("Start match"); sprintf(buf, "Starting points: %d", match_opts->start_pts); add_list_opt(buf); sprintf(buf, "Throws first: %s", match_opts->players[match_opts->throws_first - 1].name); add_list_opt(buf); list_back = true; } void menu_display_start_pts() { prompt_set_msgl("Starting points:"); add_list_opt("301"); add_list_opt("501"); add_list_opt("701"); add_list_opt("901"); list_back = true; } void menu_display_throws_first() { prompt_set_msgl("Throws first:"); for (int i = 0; i < match_opts->num_players; ++i) add_list_opt(match_opts->players[i].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 menu_pop() { --menu_depth; menu_display(); } static void menu_push(enum menu m) { menu_stack[++menu_depth] = m; menu_display(); } void menu_submit_main(int val) { match_opts_new(); match_opts->start_pts = 501; match_opts->throws_first = 1; // names need to be freed if we stop using string literals 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); } 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) match_opts->start_pts = 501; else if (val == 3) match_opts->start_pts = 701; else if (val == 4) match_opts->start_pts = 901; menu_pop(); } static void menu_submit_throws_first(int val) { match_opts->throws_first = val; menu_pop(); } static void menu_submit(int opt) { if (opt < 1 || opt > list_optc) { oi(); return; } if (list_back && opt == list_optc) { menu_pop(); return; } 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; } } void prompt_main_menu() { scoreboard_hide_info(); dom_set_content("#visits", NULL); 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) { char sel[64]; sprintf(sel, "#key-%s", k); return dom_has_class(sel, "active"); } void toggle_key(char *k) { char sel[64]; sprintf(sel, "#key-%s", k); dom_toggle_class(sel, "active"); } void deactivate_key(char *k) { char sel[64]; sprintf(sel, "#key-%s", k); dom_remove_class(sel, "active"); } char *prompt_get() { return dom_get_content("#prompt-input"); } void prompt_handle_pre(char *command) { if (pm == PM_DARTBOARD) return; clear_oi(); if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) && strcmp(command, "undo")) deactivate_key("undo"); if (pm == PM_END_MATCH && strcmp(command, "rem")) deactivate_key("rem"); if (pm == PM_END_MATCH && strcmp(command, "submit")) deactivate_key("submit"); if (strcmp(command, "exit")) deactivate_key("exit"); } void prompt_handle_on_change() { prompt_flush(); if (pm != PM_VISIT) return; char *str = prompt_get(); update_user_rem_from_pts(atoi(str)); free(str); } void prompt_handle_append(char *data) { if (pm != PM_MENU && pm != PM_VISIT && pm != PM_NUM_DARTS) return; char *str = prompt_get(); size_t len_str = strlen(str); size_t len_data = strlen(data); if (len_str < 3) { str = realloc(str, len_str + len_data + 1); memcpy(str + len_str, data, len_data + 1); prompt_set_input(str); prompt_handle_on_change(); } free(str); } void prompt_handle_backspace() { if (pm == PM_DARTBOARD) return; char *str = prompt_get(); size_t len_str = strlen(str); if (len_str > 0) { str[len_str-1] = 0; prompt_set_input(str); prompt_handle_on_change(); } free(str); } void prompt_handle_clear() { if (pm == PM_DARTBOARD) return; prompt_set_input(NULL); prompt_handle_on_change(); } void prompt_handle_submit() { if (pm == PM_END_MATCH) { if (key_is_active("submit")) end_match(); toggle_key("submit"); return; } if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_MENU) return; char *str = prompt_get(); prompt_handle_clear(); if (*str) { if (pm == PM_VISIT) user_visit(atoi(str)); else if (pm == PM_NUM_DARTS) user_num_darts(atoi(str)); else if (pm == PM_MENU) menu_submit(atoi(str)); } free(str); } void prompt_handle_rem() { if (pm == PM_END_MATCH) { if (key_is_active("rem")) start_match(); toggle_key("rem"); return; } if (pm != PM_VISIT) return; char *str = prompt_get(); prompt_handle_clear(); if (*str) user_visit_to_rem(atoi(str)); free(str); } void prompt_handle_undo() { if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH) return; prompt_handle_clear(); if (key_is_active("undo")) user_undo(); toggle_key("undo"); } void prompt_handle_exit() { if (key_is_active("exit")) end_match(); toggle_key("exit"); } EMSCRIPTEN_KEEPALIVE void prompt_handle(char *command, char *data) { prompt_handle_pre(command); if (!strcmp(command, "append")) prompt_handle_append(data); else if (!strcmp(command, "backspace")) prompt_handle_backspace(); else if (!strcmp(command, "clear")) prompt_handle_clear(); else if (!strcmp(command, "submit")) prompt_handle_submit(); else if (!strcmp(command, "rem")) prompt_handle_rem(); else if (!strcmp(command, "undo")) prompt_handle_undo(); else if (!strcmp(command, "exit")) prompt_handle_exit(); }