let match_state, user_rem, boat_rem; let oi_timeout; let prompt_handler; let delay_ms = 1000; const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0]; function stcall(f, ret_type, arg_types, args) { return Module.ccall(f, ret_type, arg_types ? ['number'].concat(arg_types) : ['number'], 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 = ''; } function initMatch() { if (match_state) stcall('free_match'); updateDelay(delay_ms); match_state = stcall('start_match', 'number'); setUserActive(); clearMatchLog(); promptSuggStr(''); stcall('draw_match'); } function oi() { document.getElementById('oi').style.visibility = 'visible'; oi_timeout = setTimeout(function() { oi_timeout = null; clearOi() }, 3000); } function clearOi() { document.getElementById('oi').style.visibility = 'hidden'; if (oi_timeout) { clearTimeout(oi_timeout); oi_timeout = null; } } function updateUserRem(rem) { user_rem = 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; Module.ccall('get_suggested', null, ['number', 'number'], [rem, 1]); } function updateBoatStateRem(rem) { let elem = document.getElementById('boat-rem'); elem.textContent = rem < 0 || rem == 1 ? 'BUST' : 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); } function updateBoatAvg(avg) { document.getElementById('boat-avg').textContent = avg.toFixed(2); } 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 promptUpdateRem() { let elem = document.getElementById('user-rem'); let pts = document.getElementById('prompt').textContent; 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 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(); } 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 = ''; } function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) { let elem = document.getElementById('match'); for (let [i, ptr] of [u_pts, u_rem, visit_no, b_rem, b_pts, b_darts].entries()) { let div = document.createElement('div'); let v = div.textContent = UTF8ToString(ptr); if (i == 0 || i == 4) { div.className = `p${POINT_CLASSES.find(x => x <= v)}`; } else if (i == 5) { if (v) div.textContent = `… ${v}`; } div.className += ` visit-col${i+1}`; elem.append(div); } elem.scrollTop = elem.scrollHeight; } function stdevChanged(val) { Module.ccall('change_stdev', null, ['number', 'number'], [val, val]); } function updateStdev(val) { document.getElementById('stdev').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; let key = data.key; if (isFinite(key)) promptAppend(key); else if (key == 'Enter') promptSubmit(); else if (key == 'Backspace') promptBackspace(); else if (key == 'r') promptSubmitRem(); } function modal(id) { document.getElementById(id).style.display = 'block'; } window.onclick = function(e) { if (e.target.classList.contains('modal')) e.target.style.display = 'none'; } document.addEventListener('keydown', processKey);