summaryrefslogtreecommitdiff
path: root/web/static
diff options
context:
space:
mode:
Diffstat (limited to 'web/static')
-rw-r--r--web/static/dartboat.js241
1 files changed, 140 insertions, 101 deletions
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index 7ffe78d..4bda599 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -1,8 +1,7 @@
-let prompt_num_darts = false;
let match_state, user_rem, boat_rem;
let oi_timeout;
+let prompt_handler;
let delay_ms = 1000;
-let prompt_mode;
const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0];
@@ -12,6 +11,114 @@ function stcall(f, ret_type, arg_types, args) {
args ? [match_state].concat(args) : [match_state]);
}
+let prompt_handler_visit = {
+ append(val) {
+ let elem = document.getElementById('prompt');
+ if (elem.textContent.length < 3) {
+ elem.textContent += val;
+ promptUpdateRem();
+ }
+ },
+
+ backspace() {
+ let elem = document.getElementById('prompt');
+ elem.textContent = elem.textContent.slice(0, -1);
+ promptUpdateRem();
+ },
+
+ clear() {
+ document.getElementById('prompt').textContent = '';
+ promptUpdateRem();
+ },
+
+ submit(pts_is_rem) {
+ let elem = document.getElementById('prompt');
+ let pts = elem.textContent;
+ if (!pts) return;
+ promptClear();
+
+ if (pts_is_rem)
+ pts = user_rem - pts;
+
+ if (!stcall('user_visit', 'number', ['number'], [pts])) {
+ oi();
+ return;
+ }
+
+ clearMatchLog();
+ stcall('draw_match');
+
+ if (!stcall('is_match_over', 'number')) {
+ setBoatActive();
+ stcall('boat_visit', 'number', ['number'], [delay_ms]);
+ }
+ },
+
+ submit_rem() {
+ this.submit(true);
+ }
+}
+
+let prompt_handler_num_darts = {
+ append(val) {
+ let elem = document.getElementById('prompt');
+ if (elem.textContent.length < 3)
+ elem.textContent += val;
+ },
+
+ backspace() {
+ let elem = document.getElementById('prompt');
+ elem.textContent = elem.textContent.slice(0, -1);
+ },
+
+ clear() {
+ document.getElementById('prompt').textContent = '';
+ },
+
+ submit() {
+ let elem = document.getElementById('prompt');
+ let val = elem.textContent;
+ if (!val) return;
+ promptClear();
+
+ if (stcall('resp_numdarts', null, ['number'], [val]))
+ prompt_handler = prompt_handler_init; //FIXME
+ else
+ oi();
+ }
+}
+
+let prompt_handler_init = {
+ submit() {
+ initMatch();
+ }
+}
+
+function promptAppend(...args) {
+ clearOi();
+ prompt_handler?.append?.(...args);
+}
+
+function promptClear(...args) {
+ clearOi();
+ prompt_handler?.clear?.(...args);
+}
+
+function promptBackspace(...args) {
+ clearOi();
+ prompt_handler?.backspace?.(...args);
+}
+
+function promptSubmit(...args) {
+ clearOi();
+ prompt_handler?.submit?.(...args);
+}
+
+function promptSubmitRem(...args) {
+ clearOi();
+ prompt_handler?.submit_rem?.(...args);
+}
+
function clearMatchLog() {
document.getElementById('match').textContent = '';
}
@@ -52,26 +159,6 @@ function updateBoatRem(rem) {
updateBoatStateRem(rem);
}
-function promptMsg(p) {
- document.getElementById('prompt-msg').textContent = UTF8ToString(p);
-}
-
-function promptMsgStr(str) {
- document.getElementById('prompt-msg').textContent = str;
-}
-
-function userSugg(p) {
- document.getElementById('user-sugg').textContent = UTF8ToString(p);
-}
-
-function boatSugg(p) {
- document.getElementById('boat-sugg').textContent = UTF8ToString(p);
-}
-
-function promptSuggStr(str) {
- document.getElementById('prompt-sugg').textContent = str;
-}
-
function updateUserStateRem(rem) {
let elem = document.getElementById('user-rem');
elem.textContent = rem < 0 || rem == 1 ? 'BUST' : rem;
@@ -84,6 +171,14 @@ function updateBoatStateRem(rem) {
Module.ccall('get_suggested', null, ['number', 'number'], [rem, 2]);
}
+function userSugg(p) {
+ document.getElementById('user-sugg').textContent = UTF8ToString(p);
+}
+
+function boatSugg(p) {
+ document.getElementById('boat-sugg').textContent = UTF8ToString(p);
+}
+
function updateUserAvg(avg) {
document.getElementById('user-avg').textContent = avg.toFixed(2);
}
@@ -92,96 +187,40 @@ function updateBoatAvg(avg) {
document.getElementById('boat-avg').textContent = avg.toFixed(2);
}
-function promptUpdateRem() {
- let elem = document.getElementById('user-rem');
- let pts = document.getElementById('prompt').textContent;
-
- updateUserStateRem(pts ? user_rem - pts : user_rem);
+function promptMsg(p) {
+ document.getElementById('prompt-msg').textContent = UTF8ToString(p);
}
-function promptAppend(val) {
- if (!prompt_mode || prompt_mode == 'init_match') return;
- clearOi();
-
- let elem = document.getElementById('prompt');
- if (elem.textContent.length < 3) {
- elem.textContent += val;
- if (!prompt_num_darts)
- promptUpdateRem();
- }
+function promptMsgStr(str) {
+ document.getElementById('prompt-msg').textContent = str;
}
-function promptClear() {
- if (!prompt_mode || prompt_mode == 'init_match') return;
- clearOi();
-
- document.getElementById('prompt').textContent = '';
- promptUpdateRem();
+function promptSuggStr(str) {
+ document.getElementById('prompt-sugg').textContent = str;
}
-function promptBackspace() {
- if (!prompt_mode || prompt_mode == 'init_match') return;
- clearOi();
+function promptUpdateRem() {
+ let elem = document.getElementById('user-rem');
+ let pts = document.getElementById('prompt').textContent;
- let elem = document.getElementById('prompt');
- elem.textContent = elem.textContent.slice(0, -1);
- promptUpdateRem();
+ updateUserStateRem(pts ? user_rem - pts : user_rem);
}
function promptNumDarts() {
promptMsgStr('Darts needed?');
- prompt_num_darts = true;
+ prompt_handler = prompt_handler_num_darts;
}
function setBoatActive() {
document.getElementById('user-state').classList.remove('active');
document.getElementById('boat-state').classList.add('active');
- prompt_mode = null;
+ prompt_handler = null;
}
function setUserActive() {
document.getElementById('boat-state').classList.remove('active');
document.getElementById('user-state').classList.add('active');
- prompt_mode = 'user_active';
-}
-
-function promptSubmit(remaining) {
- clearOi();
- if (!prompt_mode) return;
- if (prompt_mode == 'init_match') {
- initMatch();
- return;
- }
-
- let elem = document.getElementById('prompt');
- let p_user = elem.textContent;
- if (!p_user) return;
- promptClear();
-
- if (prompt_num_darts) {
- if (!stcall('resp_numdarts', null, ['number'], [p_user])) {
- oi();
- return;
- }
- prompt_num_darts = false;
- return;
- }
-
- if (remaining)
- p_user = user_rem - p_user;
-
- if (!stcall('user_visit', 'number', ['number'], [p_user])) {
- oi();
- return;
- }
-
- clearMatchLog();
- stcall('draw_match');
-
- if (!stcall('is_match_over', 'number')) {
- setBoatActive();
- stcall('boat_visit', 'number', ['number'], [delay_ms]);
- }
+ prompt_handler = prompt_handler_visit;
}
function drawBoatThrowing(pts, str) {
@@ -212,7 +251,7 @@ function scheduleEndBoatThrowing(rem, avg, delay_ms) {
}
function matchOver() {
- prompt_mode = 'init_match';
+ prompt_handler = prompt_handler_init;
promptSuggStr('Press OK to play again.');
document.getElementById('user-state').className = '';
}
@@ -220,13 +259,13 @@ function matchOver() {
function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) {
let elem = document.getElementById('match');
- for (let [i, v] of [u_pts, u_rem, visit_no, b_rem, b_pts, b_darts].entries()) {
+ for (let [i, ptr] of [u_pts, u_rem, visit_no, b_rem, b_pts, b_darts].entries()) {
let div = document.createElement('div');
- let vv = div.textContent = UTF8ToString(v);
+ let v = div.textContent = UTF8ToString(ptr);
if (i == 0 || i == 4) {
- div.className = `p${POINT_CLASSES.find(x => x <= vv)}`;
+ div.className = `p${POINT_CLASSES.find(x => x <= v)}`;
} else if (i == 5) {
- if (vv) div.textContent = `… ${vv}`;
+ if (v) div.textContent = `… ${v}`;
}
div.className += ` visit-col${i+1}`;
elem.append(div);
@@ -243,14 +282,14 @@ function updateStdev(val) {
document.getElementById('stdev').value = val;
}
-function updateDelay(val) {
- document.getElementById('delay').value = val;
-}
-
function delayChanged(val) {
delay_ms = val;
}
+function updateDelay(val) {
+ document.getElementById('delay').value = val;
+}
+
function processKey(data) {
if (data.altKey || data.ctrlKey || data.metaKey || data.target.type == 'text')
return;
@@ -263,7 +302,7 @@ function processKey(data) {
else if (key == 'Backspace')
promptBackspace();
else if (key == 'r')
- promptSubmit(true);
+ promptSubmitRem();
}
function modal(id) {