From fcc02efbff337c05bf448231cd8bfbe016939f8a Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Thu, 28 Apr 2022 13:13:14 -0700 Subject: web: the bot should have his own dartboard --- web/dartboat_wasm.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'web/dartboat_wasm.c') diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index a4a075c..84cad62 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -12,7 +12,7 @@ int delay_ms = 500; enum prompt_mode { - PM_NONE, + PM_DARTBOARD, PM_VISIT, PM_NUM_DARTS, PM_END_MATCH, @@ -25,12 +25,13 @@ void set_prompt_mode(enum prompt_mode mode) { pm = mode; - if (pm != PM_NONE) + if (pm != PM_DARTBOARD) EM_ASM(setPromptActive()); else EM_ASM(setPromptInactive()); - EM_ASM({setKeypad($0)}, pm == PM_SELECT_MODE ? "select_mode" : "default"); + EM_ASM({setKeypad($0)}, pm == PM_DARTBOARD ? "dartboard" : + pm == PM_SELECT_MODE ? "select_mode" : "default"); } enum match_mode { @@ -60,9 +61,7 @@ void set_active_player(int pn) if (state->mode == M_PVC && pn == 2) { EM_ASM({promptMsgL($0)}, "Bot is throwing…"); - EM_ASM({setKeyLabel($0, $1)}, "submit", "OK"); - EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING"); - set_prompt_mode(PM_NONE); + set_prompt_mode(PM_DARTBOARD); } else { EM_ASM({promptMsgL($0)}, "Enter points:"); EM_ASM({setKeyLabel($0, $1)}, "submit", "OK"); @@ -158,7 +157,7 @@ EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) update_player_rem(state->active_player, state->active_leg->rem - pts); } -EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) +EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str, double x, double y) { char pts_str[10]; sprintf(pts_str, "%d", pts); @@ -167,6 +166,7 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) state->legs[1]->visits[state->legs[1]->n_visits-2].rem : state->legs[1]->start; + EM_ASM({draw_point($0, $1)}, x, y); update_player_rem(2, rem - pts); EM_ASM({setPromptInput($0)}, pts_str); EM_ASM({promptMsgR($0)}, str); @@ -176,6 +176,7 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) void handle_next(); EMSCRIPTEN_KEEPALIVE void end_boat_visit(int rem, double avg) { + EM_ASM(clear_points()); update_player_rem(2, rem); EM_ASM({updatePlayerAvg($0, $1)}, 2, avg); EM_ASM({setPromptInput($0)}, ""); @@ -215,10 +216,12 @@ EMSCRIPTEN_KEEPALIVE void boat_visit() len_str += sprintf(str + len_str, i ? "-%s" : "%s", seg_name); free(seg_name); + struct ccoords c = v->ccoords[i]; + char *tmp = malloc(len_str + 1); // free in draw_boat_throwing memcpy(tmp, str, len_str + 1); - EM_ASM({scheduleCCall($0, $1, $2, $3)}, "draw_boat_throwing", - delay_ms * (i+1), pts, tmp); + EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)}, "draw_boat_throwing", + delay_ms * (i+1), pts, tmp, c.x, c.y); } EM_ASM({scheduleCCall($0, $1, $2, $3)}, "end_boat_visit", @@ -487,7 +490,7 @@ char *prompt_get() void prompt_handle_pre(char *command) { - if (pm == PM_NONE) + if (pm == PM_DARTBOARD) return; EM_ASM(clearOi()); @@ -532,7 +535,7 @@ void prompt_handle_append(char *data) void prompt_handle_backspace() { - if (pm == PM_NONE) + if (pm == PM_DARTBOARD) return; char *str = prompt_get(); @@ -547,7 +550,7 @@ void prompt_handle_backspace() void prompt_handle_clear() { - if (pm == PM_NONE) + if (pm == PM_DARTBOARD) return; EM_ASM({setPromptInput($0)}, ""); -- cgit v1.2.3-70-g09d2