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 | |
parent | bd54e83d6e21f9eaab830095663a7ddad4c70c5c (diff) | |
download | dartboat-e8592a80976919681d92a0d6ff89bae6d5fb992f.tar.gz dartboat-e8592a80976919681d92a0d6ff89bae6d5fb992f.tar.xz |
handle web remaining state from c code
-rw-r--r-- | web/dartboat_wasm.c | 49 | ||||
-rw-r--r-- | web/static/dartboat.js | 29 |
2 files changed, 51 insertions, 27 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(); diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 292c50d..2a26a0c 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -45,9 +45,9 @@ let prompt_handlers = { }, submit_rem() { - let pts = _promptGetAndClear(); - if (pts) - Module.ccall('user_visit', 'number', ['number'], [user_rem - pts]); + let rem = _promptGetAndClear(); + if (rem) + Module.ccall('user_visit_to_rem', 'number', ['number'], [rem]); } }, num_darts: { @@ -127,22 +127,12 @@ function clearOi() { } } -let user_rem; -function updateUserRem(rem) { - user_rem = rem; - updateUserStateRem(rem); +function updateUserStateRem(ptr) { + document.getElementById('p1-rem').textContent = UTF8ToString(ptr); } -function updateUserStateRem(rem) { - let elem = document.getElementById('p1-rem'); - elem.textContent = rem < 0 || rem == 1 ? 'BUST' : rem; - Module.ccall('get_suggested', null, ['number', 'number'], [rem, 1]); -} - -function updateBoatStateRem(rem) { - let elem = document.getElementById('p2-rem'); - elem.textContent = rem < 0 || rem == 1 ? 'BUST' : rem; - Module.ccall('get_suggested', null, ['number', 'number'], [rem, 2]); +function updateBoatStateRem(ptr) { + document.getElementById('p2-rem').textContent = UTF8ToString(ptr); } function updateUserName(p) { @@ -182,10 +172,7 @@ function promptSugg(p) { } function promptUpdateRem() { - let elem = document.getElementById('p1-rem'); - let pts = _promptGet(); - - updateUserStateRem(pts ? user_rem - pts : user_rem); + Module.ccall('update_user_rem_from_pts', null, ['number'], [_promptGet()]); } function setUserActive() { |