summaryrefslogtreecommitdiff
path: root/web/static/dartboat.js
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-22 00:48:52 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-22 00:48:52 -0700
commit95a3eded5c9d3863363e2af9d1966e1892ead129 (patch)
treeb38caba5ea3b4f6809707d88a80f659bccfc979c /web/static/dartboat.js
parent4d94b22fa44367e682159ad922c53c3bb7f60930 (diff)
downloaddartboat-95a3eded5c9d3863363e2af9d1966e1892ead129.tar.gz
dartboat-95a3eded5c9d3863363e2af9d1966e1892ead129.tar.xz
transfer more web ui logic to c code
Diffstat (limited to 'web/static/dartboat.js')
-rw-r--r--web/static/dartboat.js92
1 files changed, 35 insertions, 57 deletions
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) {