From 24b71a541f73bffc4ba65ad479b441df2913ef0a Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Thu, 21 Apr 2022 19:54:05 -0700 Subject: rewrite some JS web logic in C --- web/static/dartboat.js | 113 ++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 68 deletions(-) (limited to 'web/static') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index c1a197a..7ffe78d 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -1,9 +1,8 @@ -let match_active = false; -let prompt_disable = false; let prompt_num_darts = false; let match_state, user_rem, boat_rem; let oi_timeout; let delay_ms = 1000; +let prompt_mode; const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0]; @@ -13,13 +12,17 @@ function stcall(f, ret_type, arg_types, args) { args ? [match_state].concat(args) : [match_state]); } +function clearMatchLog() { + document.getElementById('match').textContent = ''; +} + function initMatch() { - stcall('free_match'); + if (match_state) + stcall('free_match'); updateDelay(delay_ms); match_state = stcall('start_match', 'number'); - match_active = true; - document.getElementById('user-state').className = 'active'; - document.getElementById('match').textContent = ''; + setUserActive(); + clearMatchLog(); promptSuggStr(''); stcall('draw_match'); } @@ -96,14 +99,8 @@ function promptUpdateRem() { updateUserStateRem(pts ? user_rem - pts : user_rem); } -function boatTempRem(pts, str) { - updateBoatStateRem(boat_rem - pts); - document.getElementById('prompt').textContent = pts; - promptSuggStr(str); -} - function promptAppend(val) { - if (!match_active || prompt_disable) return; + if (!prompt_mode || prompt_mode == 'init_match') return; clearOi(); let elem = document.getElementById('prompt'); @@ -115,7 +112,7 @@ function promptAppend(val) { } function promptClear() { - if (!match_active || prompt_disable) return; + if (!prompt_mode || prompt_mode == 'init_match') return; clearOi(); document.getElementById('prompt').textContent = ''; @@ -123,7 +120,7 @@ function promptClear() { } function promptBackspace() { - if (!match_active || prompt_disable) return; + if (!prompt_mode || prompt_mode == 'init_match') return; clearOi(); let elem = document.getElementById('prompt'); @@ -137,22 +134,21 @@ function promptNumDarts() { } function setBoatActive() { - document.getElementById('user-state').className = ''; - document.getElementById('boat-state').className = 'active'; - promptMsgStr('Bot is throwing…'); - prompt_disable = true; + document.getElementById('user-state').classList.remove('active'); + document.getElementById('boat-state').classList.add('active'); + prompt_mode = null; } function setUserActive() { - document.getElementById('user-state').className = 'active'; - document.getElementById('boat-state').className = ''; - prompt_disable = false; + document.getElementById('boat-state').classList.remove('active'); + document.getElementById('user-state').classList.add('active'); + prompt_mode = 'user_active'; } function promptSubmit(remaining) { clearOi(); - if (prompt_disable) return; - if (!match_active) { + if (!prompt_mode) return; + if (prompt_mode == 'init_match') { initMatch(); return; } @@ -179,67 +175,48 @@ function promptSubmit(remaining) { return; } - elem = document.getElementById('match'); - elem.textContent = ''; + clearMatchLog(); stcall('draw_match'); if (!stcall('is_match_over', 'number')) { setBoatActive(); - stcall('boat_visit', 'number'); + stcall('boat_visit', 'number', ['number'], [delay_ms]); } } -function boatVisitRes(rem, n, p1, p2, p3, ptr1, ptr2, ptr3, avg) { - // cannot convert in timeout func because strings are freed in c func - let s1 = UTF8ToString(ptr1); - let s2 = UTF8ToString(ptr2); - let s3 = UTF8ToString(ptr3); - - if (delay_ms == 0) { - updateBoatRem(rem); - updateBoatAvg(avg); - document.getElementById('match').textContent = ''; - stcall('draw_match'); - setUserActive(); - } else { - setTimeout(function() { boatTempRem(p1, s1); }, delay_ms); +function drawBoatThrowing(pts, str) { + updateBoatStateRem(boat_rem - pts); + document.getElementById('prompt').textContent = pts; + promptSuggStr(str); +} - if (n > 1) { - setTimeout(function() { boatTempRem(p1 + p2, `${s1}-${s2}`); }, delay_ms * 2); - } +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); +} - if (n > 2) { - setTimeout(function() { boatTempRem(p1 + p2 + p3, `${s1}-${s2}-${s3}`); }, delay_ms * 3); - } +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() { - updateBoatRem(rem); - updateBoatAvg(avg); - document.getElementById('prompt').textContent = ''; - document.getElementById('match').textContent = ''; - stcall('draw_match'); - promptSuggStr(''); - setUserActive(); - }, delay_ms * (n + 1)); - } +function scheduleEndBoatThrowing(rem, avg, delay_ms) { + setTimeout(function() { endBoatThrowing(rem, avg); }, delay_ms); } function matchOver() { - match_active = false; + prompt_mode = 'init_match'; promptSuggStr('Press OK to play again.'); document.getElementById('user-state').className = ''; } -function drawVisitNames(n1, n2) { - let elem = document.getElementById('match'); - for (let [k, v] of Object.entries({ 'user-name': n1, 'boat-name': n2 })) { - let div = document.createElement('div'); - div.className = k; - div.textContent = UTF8ToString(v); - elem.append(div); - } -} - function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) { let elem = document.getElementById('match'); -- cgit v1.2.3-70-g09d2