const $ = document.querySelector.bind(document); const $$ = document.querySelectorAll.bind(document); let oi_timeout; function oi() { $('#oi').style.visibility = 'visible'; oi_timeout = setTimeout(() => { oi_timeout = null; clearOi(); }, 3000); } function clearOi() { $('#oi').style.visibility = 'hidden'; if (oi_timeout) { clearTimeout(oi_timeout); oi_timeout = null; } } function scheduleCCall(f, ms, ...args) { const func = UTF8ToString(f); setTimeout(() => Module[`_${func}`](...args), ms); } function toCString(str) { // caller must free const len = lengthBytesUTF8(str) + 1; const cstr = _malloc(len); stringToUTF8(str, cstr, len); return cstr; } function promptGet() { // caller must free return toCString($('#prompt-input').textContent); } function promptHandle(command, data) { const str_c = toCString(command); const str_d = data && toCString(data); _prompt_handle(str_c, str_d); _free(str_c); if (str_d) _free(str_d); } function setPromptActive() { $('#prompt').classList.add('active') } function setPromptInactive() { $('#prompt').classList.remove('active') } function setPromptInput(ptr) { $('#prompt-input').textContent = UTF8ToString(ptr); } function promptMsgL(ptr) { $('#prompt-msg-l').textContent = UTF8ToString(ptr); } function promptMsgR(ptr) { $('#prompt-msg-r').textContent = UTF8ToString(ptr); } function setKeypad(keypad) { const keypad_id = `keypad-${UTF8ToString(keypad)}`; $$('.keypad').forEach(e => { e.style.display = e.id === keypad_id ? '' : 'none'; }); } function isKeyActive(k) { return $(`#key-${UTF8ToString(k)}`).classList.contains('active'); } function toggleKey(k) { $(`#key-${UTF8ToString(k)}`).classList.toggle('active'); } function deactivateKey(k) { $(`#key-${UTF8ToString(k)}`).classList.remove('active'); } function setKeyLabel(k, ptr) { $(`#key-${UTF8ToString(k)}`).textContent = UTF8ToString(ptr); } function setPlayerActive(n) { $$('[id$=-info]').forEach(e => e.classList[n && e.id == `p${n}-info` ? 'add' : 'remove']('active')); } function showPlayerInfo(n) { $(`#p${n}-info-inner`).style.visibility = 'visible'; } function hidePlayerInfo(n) { $(`#p${n}-info-inner`).style.visibility = 'hidden'; } function updatePlayerName(n, ptr) { $(`#p${n}-name`).textContent = UTF8ToString(ptr); } function updatePlayerRem(n, ptr) { $(`#p${n}-rem`).textContent = UTF8ToString(ptr); } function updatePlayerSugg(n, ptr) { $(`#p${n}-sugg`).textContent = UTF8ToString(ptr); } function updatePlayerAvg(n, avg) { $(`#p${n}-avg`).textContent = avg.toFixed(2); } function clearVisits() { $('#visits').textContent = ''; } const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 0]; function drawVisit(visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts) { let e = $('#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 updateDelay(val) { $('#delay').value = val; } function updateStdev(val) { $('#stdev').value = parseFloat(val.toFixed(2).slice(0, 4)); } function setOpt(opt, val, update_opts = true) { if (update_opts) localStorage.setItem(`dartboat_${opt}`, val); if (opt === 'delay') _set_delay(Number(val)); else if (opt === 'stdev') _set_stdev(Number(val), Number(val)); } function readOpts() { let val; if ((val = localStorage.getItem('dartboat_delay'))) setOpt('delay', val, false); if ((val = localStorage.getItem('dartboat_stdev'))) setOpt('stdev', val, false); } document.addEventListener('DOMContentLoaded', () => { let f; f = e => e.target.dataset.command.split(';').forEach(x => promptHandle(...x.split(':', 2))); $$('[data-command]').forEach(x => x.addEventListener('click', f)); f = e => setOpt(e.target.dataset.setting, e.target.value); $$('[data-setting]').forEach(x => x.addEventListener('change', f)); f = e => $(`#${e.target.dataset.modal}`).style.display = 'block'; $$('[data-modal]').forEach(x => x.addEventListener('click', f)); f = e => e.target.style.display = 'none'; $$('.modal').forEach(x => x.addEventListener('click', f)); }); 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('rem'); else if (e.key == 'u') promptHandle('undo'); }); function boatReady() { if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => _init()); else _init(); }