diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-23 00:28:21 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-23 00:28:21 -0700 |
commit | 39b76b0e432077f9777e66bb1e4d3486db5edbf0 (patch) | |
tree | cdd018df9f67924bb7b2fe6f31589f3d953936cc /web/dartboat_wasm.c | |
parent | 07f0616f88553e0a4b3a5c23b0024e78d94d9d7e (diff) | |
download | dartboat-39b76b0e432077f9777e66bb1e4d3486db5edbf0.tar.gz dartboat-39b76b0e432077f9777e66bb1e4d3486db5edbf0.tar.xz |
use common functions for updating player states
Diffstat (limited to 'web/dartboat_wasm.c')
-rw-r--r-- | web/dartboat_wasm.c | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index 19d37db..5a043d1 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -8,6 +8,9 @@ #include <emscripten/emscripten.h> +#define P_USER 1 +#define P_BOAT 2 + int delay_ms = 1000; // TODO refactor *everything* @@ -20,12 +23,12 @@ struct match_state { struct match_state *state; void set_user_active() { - EM_ASM(setUserActive()); + EM_ASM({setPlayerActive($0)}, P_USER); EM_ASM({setPromptHandler($0)}, "visit"); } void set_boat_active() { - EM_ASM(setBoatActive()); + EM_ASM({setPlayerActive($0)}, P_BOAT); EM_ASM({setPromptHandler()}, ""); } @@ -54,11 +57,7 @@ void suggested(int rem, char *buf) { EMSCRIPTEN_KEEPALIVE void get_suggested(int rem, int player) { static char buf[20]; suggested(rem, buf); - - if (player == 1) - EM_ASM({userSugg($0)}, buf); - else - EM_ASM({boatSugg($0)}, buf); + EM_ASM({updatePlayerSugg($0, $1)}, player, buf); } EMSCRIPTEN_KEEPALIVE int is_match_over() { @@ -70,7 +69,7 @@ EMSCRIPTEN_KEEPALIVE int is_match_over() { void match_over() { EM_ASM({setPromptHandler($0)}, "init"); EM_ASM({promptSugg($0)}, "Press OK to play again."); - EM_ASM(setNoneActive()); + EM_ASM(setPlayerActive()); } EMSCRIPTEN_KEEPALIVE void draw_match() { @@ -138,10 +137,10 @@ char *str_for_rem_state(int rem) { EMSCRIPTEN_KEEPALIVE void update_user_rem(int rem) { char *str = str_for_rem_state(rem); - EM_ASM({updateUserStateRem($0)}, str); + EM_ASM({updatePlayerRem($0, $1)}, P_USER, str); free(str); - get_suggested(rem, 1); + get_suggested(rem, P_USER); } EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) { @@ -150,10 +149,10 @@ EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) { EMSCRIPTEN_KEEPALIVE void update_boat_rem(int rem) { char *str = str_for_rem_state(rem); - EM_ASM({updateBoatStateRem($0)}, str); + EM_ASM({updatePlayerRem($0, $1)}, P_BOAT, str); free(str); - get_suggested(rem, 2); + get_suggested(rem, P_BOAT); } void end_boat_throwing(int, int); // FIXME @@ -218,7 +217,8 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(int points) { update_user_rem(l->rem); if (v->rem > 0) - EM_ASM({updateUserAvg($0)}, (double)(l->start-l->rem)/l->n_visits); + EM_ASM({updatePlayerAvg($0, $1)}, P_USER, + (double)(l->start-l->rem)/l->n_visits); EM_ASM({clearMatchLog()}); draw_match(); @@ -249,7 +249,7 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) { EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, int avg) { update_boat_rem(rem); - EM_ASM({updateBoatAvg($0)}, avg); + EM_ASM({updatePlayerAvg($0, $1)}, P_BOAT, avg); EM_ASM({setPromptText($0)}, ""); EM_ASM(clearMatchLog()); EM_ASM({promptSugg($0)}, ""); @@ -267,7 +267,7 @@ EMSCRIPTEN_KEEPALIVE void resp_numdarts(int n_darts) { struct leg *l = state->l1; - EM_ASM({updateUserAvg($0)}, + EM_ASM({updatePlayerAvg($0, $1)}, P_USER, ((double)l->start / (((l->n_visits - 1) * 3) + n_darts)) * 3); EM_ASM({promptMsg($0)}, "You win! :)"); match_over(); @@ -302,8 +302,8 @@ EMSCRIPTEN_KEEPALIVE void start_match() { state->l1 = leg_init(501, "User"); state->l2 = leg_init(501, "Bot"); - EM_ASM({updateUserName($0)}, state->l1->name); - EM_ASM({updateBoatName($0)}, state->l2->name); + EM_ASM({updatePlayerName($0, $1)}, P_USER, state->l1->name); + EM_ASM({updatePlayerName($0, $1)}, P_BOAT, state->l2->name); update_user_rem(state->l1->rem); update_boat_rem(state->l2->rem); |