diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-22 15:17:01 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-22 15:17:01 -0700 |
commit | e8592a80976919681d92a0d6ff89bae6d5fb992f (patch) | |
tree | cdd8390891637b5802f6c75dfaef85205555a298 /web/dartboat_wasm.c | |
parent | bd54e83d6e21f9eaab830095663a7ddad4c70c5c (diff) | |
download | dartboat-e8592a80976919681d92a0d6ff89bae6d5fb992f.tar.gz dartboat-e8592a80976919681d92a0d6ff89bae6d5fb992f.tar.xz |
handle web remaining state from c code
Diffstat (limited to 'web/dartboat_wasm.c')
-rw-r--r-- | web/dartboat_wasm.c | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index e00c330..e8cde21 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -125,6 +125,38 @@ EMSCRIPTEN_KEEPALIVE void draw_match() { } } +char *str_for_rem_state(int rem) { + char *buf = malloc(10); + + if (rem < 0 || rem == 1) + strcpy(buf, "BUST"); + else + sprintf(buf, "%d", rem); + + return buf; +} + +EMSCRIPTEN_KEEPALIVE void update_user_rem(int rem) { + char *str = str_for_rem_state(rem); + EM_ASM({updateUserStateRem($0)}, str); + free(str); + + get_suggested(rem, 1); +} + +EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) { + update_user_rem(state->l1->rem - pts); +} + +EMSCRIPTEN_KEEPALIVE void update_boat_rem(int rem) { + char *str = str_for_rem_state(rem); + EM_ASM({updateBoatStateRem($0)}, str); + free(str); + + get_suggested(rem, 2); +} + + EMSCRIPTEN_KEEPALIVE void boat_visit() { if (state->l1->rem <= 0 || state->l2->rem <= 0) return; @@ -143,7 +175,7 @@ EMSCRIPTEN_KEEPALIVE void boat_visit() { avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3; if (!delay_ms) { - EM_ASM({updateBoatStateRem($0)}, l->rem); + update_boat_rem(l->rem); EM_ASM({updateBoatAvg($0)}, avg); EM_ASM({clearMatchLog()}); draw_match(); @@ -196,7 +228,7 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(int points) { v->points = points; l->rem -= points; v->rem = l->rem; - EM_ASM({updateUserRem($0)}, l->rem); + update_user_rem(l->rem); if (v->rem > 0) EM_ASM({updateUserAvg($0)}, (double)(l->start-l->rem)/l->n_visits); @@ -212,19 +244,24 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(int points) { return true; } +// FIXME neg +EMSCRIPTEN_KEEPALIVE bool user_visit_to_rem(int rem) { + user_visit(state->l1->rem - rem); +} + EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) { char buf[10]; sprintf(buf, "%d", pts); int rem = state->l2->n_visits > 1 ? state->l2->visits[state->l2->n_visits-2].rem : state->l2->start; - EM_ASM({updateBoatStateRem($0)}, rem - pts); + update_boat_rem(rem - pts); EM_ASM({setPromptText($0)}, buf); EM_ASM({promptSugg($0)}, str); } EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, int avg) { - EM_ASM({updateBoatStateRem($0)}, rem); + update_boat_rem(rem); EM_ASM({updateBoatAvg($0)}, avg); EM_ASM({setPromptText($0)}, ""); EM_ASM(clearMatchLog()); @@ -280,8 +317,8 @@ EMSCRIPTEN_KEEPALIVE void start_match() { EM_ASM({updateUserName($0)}, state->l1->name); EM_ASM({updateBoatName($0)}, state->l2->name); - EM_ASM({updateUserRem($0)}, state->l1->rem); - EM_ASM({updateBoatStateRem($0)}, state->l2->rem); + update_user_rem(state->l1->rem); + update_boat_rem(state->l2->rem); EM_ASM(clearMatchLog()); set_user_active(); |