let oi_timeout; function oi() { document.getElementById('oi').style.visibility = 'visible'; oi_timeout = setTimeout(() => { oi_timeout = null; clearOi(); }, 3000); } function clearOi() { document.getElementById('oi').style.visibility = 'hidden'; if (oi_timeout) { clearTimeout(oi_timeout); oi_timeout = null; } } function scheduleCCall(f, ms, ...args) { let types = []; let vals = []; for (let i = 0; i < args.length/2; i++) vals[i] = (types[i] = UTF8ToString(args[i*2])) == 'string' ? UTF8ToString(args[i*2+1]) : args[i*2+1]; let func = UTF8ToString(f); setTimeout(() => Module.ccall(func, null, types, vals), ms); } let prompt_handler; let prompt_handlers = { default: { append(val) { let e = document.getElementById('prompt-input'); if (e.textContent.length < 3) { e.textContent += val; this._on_change?.(); } }, backspace() { let e = document.getElementById('prompt-input'); e.textContent = e.textContent.slice(0, -1); this._on_change?.(); }, clear() { document.getElementById('prompt-input').textContent = ''; this._on_change?.(); }, pre(action) { clearOi(); }, _get() { return document.getElementById('prompt-input').textContent; }, _get_and_clear() { let val = this._get(); this.clear(); return val; } } } prompt_handlers.visit = { __proto__: prompt_handlers.default, submit() { let pts = this._get_and_clear(); if (pts) Module.ccall('user_visit', 'number', ['number'], [pts]); }, submit_rem() { let rem = this._get_and_clear(); if (rem) Module.ccall('user_visit_to_rem', 'number', ['number'], [rem]); }, undo() { this.clear(); let cl = document.getElementById('key_undo').classList; if (cl.contains('active')) Module.ccall('user_undo'); cl.toggle('active'); }, pre(action) { super.pre(action); if (action != 'undo') document.getElementById('key_undo').classList.remove('active'); }, _on_change(val) { Module.ccall('update_user_rem_from_pts', null, ['number'], [val || this._get()]); } }; prompt_handlers.num_darts = { __proto__: prompt_handlers.default, submit() { let n = this._get_and_clear(); if (n) Module.ccall('resp_numdarts', null, ['number'], [n]); } }; prompt_handlers.match_over = { __proto__: prompt_handlers.default, append() {}, undo() { prompt_handlers.visit.undo(); }, pre(action) { prompt_handlers.visit.pre(action); }, submit() { Module.ccall('match_init'); } }; prompt_handlers.init = { __proto__: prompt_handlers.default, submit() { let v = this._get_and_clear(); if (v) Module.ccall('start_match', null, ['number'], [v]); } }; function setPromptHandler(ptr) { let str = UTF8ToString(ptr); prompt_handler = prompt_handlers[str]; document.querySelectorAll('.keypad').forEach(e => e.style.display = 'none'); document.getElementById(`keypad-${str === 'init' ? 'init' : 'default'}`) .style.removeProperty('display'); } function promptHandle(action, ...args) { prompt_handler?.pre?.(action); prompt_handler?.[action]?.(...args); } function setPromptInput(ptr) { document.getElementById('prompt-input').textContent = UTF8ToString(ptr); } function promptMsgL(p) { document.getElementById('prompt-msg-l').textContent = UTF8ToString(p); } function promptMsgR(p) { document.getElementById('prompt-msg-r').textContent = UTF8ToString(p); } function setPlayerActive(n) { document.querySelectorAll('[id$=-info]').forEach(e => e.classList[n && e.id == `p${n}-info` ? 'add' : 'remove']('active')); } function hidePlayerInfo(n) { let elems = document.getElementById(`p${n}-info`).children; for (let i = 0; i < elems.length; ++i) elems[i].style.visibility = 'hidden'; } function showPlayerInfo(n) { let elems = document.getElementById(`p${n}-info`).children; for (let i = 0; i < elems.length; ++i) elems[i].style.removeProperty('visibility'); } function updatePlayerRem(n, ptr) { document.getElementById(`p${n}-rem`).textContent = UTF8ToString(ptr); } function updatePlayerName(n, ptr) { document.getElementById(`p${n}-name`).textContent = UTF8ToString(ptr); } function updatePlayerSugg(n, ptr) { document.getElementById(`p${n}-sugg`).textContent = UTF8ToString(ptr); } function updatePlayerAvg(n, avg) { document.getElementById(`p${n}-avg`).textContent = avg.toFixed(2); } function clearVisits() { document.getElementById('visits').textContent = ''; } const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0]; function drawVisit(visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts) { let e = document.getElementById('visits'); for (let [i, ptr] of [ p1_pts, p1_rem, visit_no, p2_rem, p2_pts, p2_darts].entries()) { let div = e.appendChild(document.createElement('div')); let v = div.textContent = UTF8ToString(ptr); div.classList.add(`visit-col${i+1}`); if (i == 0 || i == 4) div.classList.add(`p${POINT_CLASSES.find(x => x <= v)}`); else if (i == 5 && v) div.textContent = `… ${v}`; } e.scrollTop = e.scrollHeight; } function setDelay(val, update_opts = true) { if (update_opts) localStorage.setItem('dartboat_delay', val); Module.ccall('set_delay', null, ['number'], [val]); } function updateDelay(val) { document.getElementById('delay').value = val; } function setStdev(val, update_opts = true) { if (update_opts) localStorage.setItem('dartboat_stdev', val); Module.ccall('set_stdev', null, ['number', 'number'], [val, val]); } function updateStdev(val) { document.getElementById('stdev').value = val; } function read_opts() { let val; if ((val = localStorage.getItem('dartboat_delay'))) setDelay(val, false); if ((val = localStorage.getItem('dartboat_stdev'))) setStdev(val, false); } function modal(id) { document.getElementById(id).style.display = 'block'; } document.addEventListener('click', e => { if (e.target.classList.contains('modal')) e.target.style.display = 'none'; }); document.addEventListener('keydown', e => { if (e.altKey || e.ctrlKey || e.metaKey || e.target.type == 'text') return; if (isFinite(e.key)) promptHandle('append', e.key); else if (e.key == 'Enter') promptHandle('submit'); else if (e.key == 'Backspace') promptHandle('backspace'); else if (e.key == 'c') promptHandle('clear'); else if (e.key == 'r') promptHandle('submit_rem'); else if (e.key == 'u') promptHandle('undo'); });