diff options
Diffstat (limited to 'web')
-rw-r--r-- | web/dartboat_wasm.c | 166 | ||||
-rw-r--r-- | web/static/dartboat.js | 135 |
2 files changed, 188 insertions, 113 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index d9c3540..322127a 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -10,6 +10,28 @@ int delay_ms = 500; +enum prompt_mode { + PM_NONE, + PM_VISIT, + PM_NUM_DARTS, + PM_END_MATCH, + PM_SELECT_MODE +}; + +enum prompt_mode pm; + +void set_prompt_mode(enum prompt_mode mode) +{ + pm = mode; + + if (pm != PM_NONE) + EM_ASM(setPromptActive()); + else + EM_ASM(setPromptInactive()); + + EM_ASM({setKeypad($0)}, pm == PM_SELECT_MODE ? "select_mode" : "default"); +} + enum match_mode { M_FIRST = 1, M_PVC = M_FIRST, @@ -37,10 +59,10 @@ void set_active_player(int pn) if (state->mode == M_PVC && pn == 2) { EM_ASM({promptMsgL($0)}, "Bot is throwing…"); - EM_ASM({setPromptHandler($0)}, ""); + set_prompt_mode(PM_NONE); } else { EM_ASM({promptMsgL($0)}, "Enter points:"); - EM_ASM({setPromptHandler($0)}, "visit"); + set_prompt_mode(PM_VISIT); } EM_ASM({promptMsgR($0)}, ""); } @@ -206,7 +228,7 @@ void clear_player_info(int pn) void prompt_num_darts() { - EM_ASM({setPromptHandler($0)}, "num_darts"); + set_prompt_mode(PM_NUM_DARTS); EM_ASM({promptMsgL($0)}, "Darts needed?"); EM_ASM({promptMsgR($0)}, ""); } @@ -214,7 +236,7 @@ void prompt_num_darts() void prompt_end_match() { EM_ASM(setPlayerActive()); // sets all inactive - EM_ASM({setPromptHandler($0)}, "end_match"); + set_prompt_mode(PM_END_MATCH); EM_ASM({promptMsgL($0)}, state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" : "You win! :)"); @@ -229,7 +251,7 @@ EMSCRIPTEN_KEEPALIVE void prompt_select_mode() } EM_ASM(clearVisits()); - EM_ASM({setPromptHandler($0)}, "select_mode"); + set_prompt_mode(PM_SELECT_MODE); EM_ASM({promptMsgL($0)}, "Select match mode:"); EM_ASM({promptMsgR($0)}, ""); } @@ -432,8 +454,142 @@ void init_boat() EM_ASM({updateStdev($0)}, horizontal_stdev); } +char *prompt_get() +{ + return (char *)EM_ASM_INT({return promptGet()}); +} + +void prompt_handle_pre(char *action) +{ + if (pm == PM_NONE) + return; + + EM_ASM(clearOi()); + + if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) && + strcmp(action, "undo")) + EM_ASM(deactivateUndo()); +} + +void prompt_handle_on_change() +{ + if (pm != PM_VISIT) + return; + + char *str = prompt_get(); + update_user_rem_from_pts(atoi(str)); + free(str); +} + +void prompt_handle_append(char *data) +{ + if (pm != PM_SELECT_MODE && pm != PM_VISIT && pm != PM_NUM_DARTS) + return; + + char *str = prompt_get(); + size_t len_str = strlen(str); + size_t len_data = strlen(data); + if (len_str < 3) { + str = realloc(str, len_str + len_data + 1); + memcpy(str + len_str, data, len_data + 1); + EM_ASM({setPromptInput($0)}, str); + prompt_handle_on_change(); + } + free(str); +} + +void prompt_handle_backspace() +{ + if (pm == PM_NONE) + return; + + char *str = prompt_get(); + size_t len_str = strlen(str); + if (len_str > 0) { + str[len_str-1] = 0; + EM_ASM({setPromptInput($0)}, str); + prompt_handle_on_change(); + } + free(str); +} + +void prompt_handle_clear() +{ + if (pm == PM_NONE) + return; + + EM_ASM(promptClear()); + prompt_handle_on_change(); +} + +void prompt_handle_submit() +{ + if (pm == PM_END_MATCH) { + end_match(); + return; + } + + if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_SELECT_MODE) + return; + + char *str = prompt_get(); + if (*str) { + if (pm == PM_VISIT) + user_visit(atoi(str)); + else if (pm == PM_NUM_DARTS) + user_num_darts(atoi(str)); + else if (pm == PM_SELECT_MODE) + start_match(atoi(str)); + } + + free(str); + prompt_handle_clear(); +} + +void prompt_handle_submit_rem() +{ + if (pm != PM_VISIT) + return; + + char *str = prompt_get(); + if (*str) + user_visit_to_rem(atoi(str)); + free(str); + prompt_handle_clear(); +} + +void prompt_handle_undo() +{ + if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH) + return; + + prompt_handle_clear(); + if (EM_ASM_INT({return isUndoActive()})) + user_undo(); + EM_ASM(toggleUndo()); +} + +EMSCRIPTEN_KEEPALIVE void prompt_handle(char *action, char *data) +{ + prompt_handle_pre(action); + + if (!strcmp(action, "append")) + prompt_handle_append(data); + else if (!strcmp(action, "backspace")) + prompt_handle_backspace(); + else if (!strcmp(action, "clear")) + prompt_handle_clear(); + else if (!strcmp(action, "submit")) + prompt_handle_submit(); + else if (!strcmp(action, "submit_rem")) + prompt_handle_submit_rem(); + else if (!strcmp(action, "undo")) + prompt_handle_undo(); +} + int main() { init_boat(); + set_prompt_mode(PM_VISIT); handle_next(); } diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 9cedc4a..f0855c2 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -26,128 +26,47 @@ function scheduleCCall(f, ms, ...args) { setTimeout(() => Module.ccall(func, null, types, vals), ms); } -let prompt_handler; -let prompt_handlers = { - default: { - _get() { - return document.getElementById('prompt-input').textContent; - }, - - _get_and_clear() { - let val = this._get(); - this.clear(); - return val; - }, - - _pre(action) { - clearOi(); - }, - - append(val) { - let e = document.getElementById('prompt-input'); - if (e.textContent.length < 3) { - let tc = e.textContent += val; - this._on_change?.(tc); - } - }, - - backspace() { - let e = document.getElementById('prompt-input'); - let tc = e.textContent = e.textContent.slice(0, -1); - this._on_change?.(tc); - }, - - clear() { - let tc = document.getElementById('prompt-input').textContent = ''; - this._on_change?.(tc); - } - } +function promptGet() { + let tc = document.getElementById('prompt-input').textContent; + let len = lengthBytesUTF8(tc) + 1; + let str = _malloc(len); + stringToUTF8(tc, str, len); + return str; } -prompt_handlers.default_with_undo = { - __proto__: prompt_handlers.default, - - _pre(action) { - super._pre(action); - if (action != 'undo') - document.getElementById('key_undo').classList.remove('active'); - }, - - undo() { - this.clear(); - let cl = document.getElementById('key_undo').classList; - if (cl.contains('active')) - Module.ccall('user_undo'); - cl.toggle('active'); - } +function promptClear() { + document.getElementById('prompt-input').textContent = ''; } -prompt_handlers.visit = { - __proto__: prompt_handlers.default_with_undo, - - _on_change(val) { - Module.ccall('update_user_rem_from_pts', null, - ['number'], [val || this._get()]); - }, - - submit() { - let pts = this._get_and_clear(); - if (pts) - Module.ccall('user_visit', null, ['number'], [pts]); - }, - - submit_rem() { - let rem = this._get_and_clear(); - if (rem) - Module.ccall('user_visit_to_rem', null, ['number'], [rem]); - } -}; - -prompt_handlers.num_darts = { - __proto__: prompt_handlers.default_with_undo, - - submit() { - let n = this._get_and_clear(); - if (n) - Module.ccall('user_num_darts', null, ['number'], [n]); - } -}; - -prompt_handlers.end_match = { - __proto__: prompt_handlers.default_with_undo, - - append() {}, +function isUndoActive() { + return document.getElementById('key_undo').classList.contains('active'); +} - submit() { - Module.ccall('end_match'); - } -}; +function toggleUndo() { + document.getElementById('key_undo').classList.toggle('active'); +} -prompt_handlers.select_mode = { - __proto__: prompt_handlers.default, - _keypad: 'select_mode', +function deactivateUndo() { + document.getElementById('key_undo').classList.remove('active'); +} - submit() { - let v = this._get_and_clear(); - if (v) - Module.ccall('start_match', null, ['number'], [v]); - } -}; +function setPromptActive() { + document.getElementById('prompt').classList.add('active') +} -function setPromptHandler(ptr) { - prompt_handler = prompt_handlers[UTF8ToString(ptr)]; - document.getElementById('prompt').classList[ - prompt_handler ? 'add' : 'remove']('active'); +function setPromptInactive() { + document.getElementById('prompt').classList.remove('active') +} - let keypad_id = `keypad-${prompt_handler?._keypad || 'default'}`; +function setKeypad(keypad) { + let keypad_id = `keypad-${UTF8ToString(keypad)}`; document.querySelectorAll('.keypad').forEach(e => { e.style.display = e.id === keypad_id ? '' : 'none'; }); } -function promptHandle(action, ...args) { - prompt_handler?._pre?.(action); - prompt_handler?.[action]?.(...args); +function promptHandle(action, data) { + Module.ccall('prompt_handle', null, ['string', 'string'], [action, data]); } function setPromptInput(ptr) { |