diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-22 00:48:52 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-22 00:48:52 -0700 |
commit | 95a3eded5c9d3863363e2af9d1966e1892ead129 (patch) | |
tree | b38caba5ea3b4f6809707d88a80f659bccfc979c | |
parent | 4d94b22fa44367e682159ad922c53c3bb7f60930 (diff) | |
download | dartboat-95a3eded5c9d3863363e2af9d1966e1892ead129.tar.gz dartboat-95a3eded5c9d3863363e2af9d1966e1892ead129.tar.xz |
transfer more web ui logic to c code
-rw-r--r-- | web/dartboat_wasm.c | 86 | ||||
-rw-r--r-- | web/static/dartboat.js | 92 |
2 files changed, 102 insertions, 76 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index 65888b2..c648a29 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -15,6 +15,16 @@ struct match_state { int complete; }; +void set_user_active() { + EM_ASM(setUserActive()); + EM_ASM(setPromptHandlerVisit()); +} + +void set_boat_active() { + EM_ASM(setBoatActive()); + EM_ASM(setPromptHandlerNull()); +} + void suggested(int rem, char *buf) { buf[0] = 0; @@ -47,15 +57,22 @@ EMSCRIPTEN_KEEPALIVE void get_suggested(int rem, int player) { EM_ASM({boatSugg($0)}, buf); } -EMSCRIPTEN_KEEPALIVE bool user_visit(struct match_state *state, int points) { +EMSCRIPTEN_KEEPALIVE int is_match_over(struct match_state *state) { if (state->l1->rem <= 0 || state->l2->rem <= 0) - return false; + return 1; + return 0; +} - if (points > state->l1->rem || state->l1->rem - points == 1 || +EMSCRIPTEN_KEEPALIVE bool user_visit(struct match_state *state, int points) { + if (state->l1->rem <= 0 || state->l2->rem <= 0 || + points > state->l1->rem || state->l1->rem - points == 1 || points > 180 || points == 179 || points == 178 || points == 176 || points == 175 || points == 173 || points == 172 || points == 169 || - points == 166 || points == 163) + points == 166 || points == 163) { + + EM_ASM(oi()); return false; + } struct leg *l = state->l1; struct visit *v = l->visits + l->n_visits++; @@ -67,13 +84,16 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(struct match_state *state, int points) { if (v->rem > 0) EM_ASM({updateUserAvg($0)}, (double)(l->start-l->rem)/l->n_visits); + if (!is_match_over(state)) + set_boat_active(); + return true; } -EMSCRIPTEN_KEEPALIVE int is_match_over(struct match_state *state) { - if (state->l1->rem <= 0 || state->l2->rem <= 0) - return 1; - return 0; +void match_over() { + EM_ASM(setPromptHandlerInit()); + EM_ASM({promptSugg($0)}, "Press OK to play again."); + EM_ASM(setNoOneActive()); } EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) { @@ -118,10 +138,11 @@ EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) { } if (l1->rem <= 0) { - EM_ASM(promptNumDarts()); + EM_ASM({promptMsg($0)}, "Darts needed?"); + EM_ASM(setPromptHandlerNumDarts()); } else if (l2->rem <= 0) { EM_ASM({promptMsg($0)}, "Bot wins. :("); - EM_ASM(matchOver()); + match_over(); } else { EM_ASM({promptMsg($0)}, "Enter points:"); } @@ -145,12 +166,12 @@ EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) { avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3; if (!delay_ms) { - EM_ASM({updateBoatRem($0)}, l->rem); + EM_ASM({updateBoatStateRem($0)}, l->rem); EM_ASM({updateBoatAvg($0)}, avg); EM_ASM({clearMatchLog()}); draw_match(state); if (!is_match_over(state)) - EM_ASM(setUserActive()); + set_user_active(); return; } @@ -182,18 +203,40 @@ EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) { l->rem, avg, delay_ms * (v->n_darts + 1)); } -EMSCRIPTEN_KEEPALIVE int resp_numdarts(struct match_state *state, int n_darts) { - if (n_darts < 1 || n_darts > 4) - return 0; +EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(struct match_state *state, int pts, char *str) { + char buf[10]; + sprintf(buf, "%d", pts); + + EM_ASM({updateBoatStateRem($0)}, state->l2->rem - pts); + EM_ASM({setPromptText($0)}, buf); + EM_ASM({promptSugg($0)}, str); +} + +EMSCRIPTEN_KEEPALIVE void end_boat_throwing(struct match_state *state, int rem, int avg) { + EM_ASM({updateBoatStateRem($0)}, rem); + EM_ASM({updateBoatAvg($0)}, avg); + EM_ASM({setPromptText($0)}, ""); + EM_ASM(clearMatchLog()); + EM_ASM({promptSugg($0)}, ""); + draw_match(state); + + if (!is_match_over(state)) + set_user_active(); +} + +EMSCRIPTEN_KEEPALIVE void resp_numdarts(struct match_state *state, int n_darts) { + if (n_darts < 1 || n_darts > 3) { + EM_ASM(oi()); + return; + } struct leg *l = state->l1; EM_ASM({updateUserAvg($0)}, ((double)l->start / (((l->n_visits - 1) * 3) + n_darts)) * 3); EM_ASM({promptMsg($0)}, "You win! :)"); - EM_ASM(matchOver()); - - return 1; + match_over(); + EM_ASM(setPromptHandlerInit()); } void init_boat() { @@ -215,7 +258,12 @@ EMSCRIPTEN_KEEPALIVE struct match_state *start_match() { state->l2 = leg_init(501, "Bot"); EM_ASM({updateUserRem($0)}, state->l1->rem); - EM_ASM({updateBoatRem($0)}, state->l2->rem); + EM_ASM({updateBoatStateRem($0)}, state->l2->rem); + + EM_ASM(clearMatchLog()); + set_user_active(); + EM_ASM({promptSugg($0)}, ""); + draw_match(state); return state; } diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 4bda599..2e9429c 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -1,4 +1,4 @@ -let match_state, user_rem, boat_rem; +let match_state, user_rem; let oi_timeout; let prompt_handler; let delay_ms = 1000; @@ -40,18 +40,14 @@ let prompt_handler_visit = { if (pts_is_rem) pts = user_rem - pts; - if (!stcall('user_visit', 'number', ['number'], [pts])) { - oi(); + if (!stcall('user_visit', 'number', ['number'], [pts])) return; - } clearMatchLog(); stcall('draw_match'); - if (!stcall('is_match_over', 'number')) { - setBoatActive(); + if (!stcall('is_match_over', 'number')) stcall('boat_visit', 'number', ['number'], [delay_ms]); - } }, submit_rem() { @@ -81,10 +77,7 @@ let prompt_handler_num_darts = { if (!val) return; promptClear(); - if (stcall('resp_numdarts', null, ['number'], [val])) - prompt_handler = prompt_handler_init; //FIXME - else - oi(); + stcall('resp_numdarts', null, ['number'], [val]); } } @@ -94,6 +87,22 @@ let prompt_handler_init = { } } +function setPromptHandlerVisit() { + prompt_handler = prompt_handler_visit; +} + +function setPromptHandlerInit() { + prompt_handler = prompt_handler_init; +} + +function setPromptHandlerNumDarts() { + prompt_handler = prompt_handler_num_darts; +} + +function setPromptHandlerNull() { + prompt_handler = null; +} + function promptAppend(...args) { clearOi(); prompt_handler?.append?.(...args); @@ -126,12 +135,8 @@ function clearMatchLog() { function initMatch() { if (match_state) stcall('free_match'); - updateDelay(delay_ms); + //updateDelay(delay_ms); match_state = stcall('start_match', 'number'); - setUserActive(); - clearMatchLog(); - promptSuggStr(''); - stcall('draw_match'); } function oi() { @@ -154,11 +159,6 @@ function updateUserRem(rem) { updateUserStateRem(rem); } -function updateBoatRem(rem) { - boat_rem = rem; - updateBoatStateRem(rem); -} - function updateUserStateRem(rem) { let elem = document.getElementById('user-rem'); elem.textContent = rem < 0 || rem == 1 ? 'BUST' : rem; @@ -191,12 +191,8 @@ function promptMsg(p) { document.getElementById('prompt-msg').textContent = UTF8ToString(p); } -function promptMsgStr(str) { - document.getElementById('prompt-msg').textContent = str; -} - -function promptSuggStr(str) { - document.getElementById('prompt-sugg').textContent = str; +function promptSugg(p) { + document.getElementById('prompt-sugg').textContent = UTF8ToString(p); } function promptUpdateRem() { @@ -206,54 +202,36 @@ function promptUpdateRem() { updateUserStateRem(pts ? user_rem - pts : user_rem); } -function promptNumDarts() { - promptMsgStr('Darts needed?'); - prompt_handler = prompt_handler_num_darts; -} - function setBoatActive() { document.getElementById('user-state').classList.remove('active'); document.getElementById('boat-state').classList.add('active'); - prompt_handler = null; } function setUserActive() { document.getElementById('boat-state').classList.remove('active'); document.getElementById('user-state').classList.add('active'); - prompt_handler = prompt_handler_visit; } -function drawBoatThrowing(pts, str) { - updateBoatStateRem(boat_rem - pts); - document.getElementById('prompt').textContent = pts; - promptSuggStr(str); +function setNoOneActive() { + document.getElementById('user-state').classList.remove('active'); + document.getElementById('boat-state').classList.remove('active'); +} + +function setPromptText(ptr) { + document.getElementById('prompt').textContent = UTF8ToString(ptr); } function scheduleBoatThrowing(pts, ptr, delay_ms) { // ptr to c string must be copied before returning let str = UTF8ToString(ptr); - setTimeout(function() { drawBoatThrowing(pts, str); }, delay_ms); -} - -function endBoatThrowing(rem, avg) { - updateBoatRem(rem); - updateBoatAvg(avg); - document.getElementById('prompt').textContent = ''; - clearMatchLog(); - promptSuggStr(''); - stcall('draw_match'); - if (!stcall('is_match_over', 'number')) - setUserActive(); + setTimeout(function() { + stcall('draw_boat_throwing', null, ['number', 'string'], [pts, str]) }, + delay_ms); } function scheduleEndBoatThrowing(rem, avg, delay_ms) { - setTimeout(function() { endBoatThrowing(rem, avg); }, delay_ms); -} - -function matchOver() { - prompt_handler = prompt_handler_init; - promptSuggStr('Press OK to play again.'); - document.getElementById('user-state').className = ''; + setTimeout(function() { + stcall('end_boat_throwing', null, ['number', 'number'], [rem, avg]) }, delay_ms); } function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) { |