/* ((Triforce, 11-25) The Randomizer page: OOTRR's seeds (launcher/App/Backend.Randomizer.cs). The launcher sends state.rando: tool {have, version, size, busy, frac, line}, game, busy, line, seed, cos {music, sfx, colors, title}, preset, changed, rows {: {st: open|held|hidden, v, show, max?, why?}}, seeds [{id, seed, on, sub, settings, spoiler, cos}], note, schema {prefix, groups [{id,label,tip}], presets [{id,name,tip}], settings [{id,label,group,kind,tip,tag,unsupported,choices?,min?,max?,step?}]} (the schema once per OOTRR release; the rows each time). Tabs: New seed, OOTRR's own groups, Your seeds. Everything this page changes goes back as rando.* commands; the launcher answers with the state again. Uses app.js's $, S, ACTIONS, cmd, sheets. */ 'use strict'; S.rtab = 'new'; const RCOS = { music: { label: 'Music', opts: [['off', 'Off', 'The game\'s own music where it always plays'], ['grouped', 'Shuffled within its kind', 'Area themes trade with area themes, fanfares with fanfares'], ['anywhere', 'Shuffled anywhere', 'Any track can play anywhere']] }, sfx: { label: 'Sound effects', opts: [['off', 'Off', 'Every sound as it comes'], ['grouped', 'Shuffled within its kind', 'Sounds trade with sounds of the same kind'], ['anywhere', 'Shuffled anywhere', 'Any sound can stand for any other']] }, colors: { label: 'Colors', opts: [['off', 'Off', 'Tunics and gear in their own colors'], ['random', 'Random', 'Tunics, gauntlets and trails in colors drawn from the seed']] }, }; function rSchema() { return S.randoSchema || null; } /* a number as the row shows it: a multiple (OOTRR's "times") as 1.5x, else the number */ function rNum(x, v) { return x && x.times ? (v / x.times).toFixed(2).replace(/\.?0+$/, '') + '\u00d7' : String(v); } function rSetting(id) { const s = rSchema(); return s ? s.settings.find(x => x.id === id) : null; } /* a description as OOTRR wrote it, its paragraphs kept (shown with white-space: pre-line) */ function rTip(s) { return esc(String(s || '').replace(/\r/g, '').replace(/[ \t]+/g, ' ').replace(/ *\n */g, '\n').replace(/\n{3,}/g, '\n\n').trim()); } function drawRandoTabs() { const s = rSchema(), r = S.rando || {}; const tabs = [{ id: 'new', name: 'New seed' }].concat(r.tool && r.tool.have && s ? s.groups.map(g => ({ id: 'g:' + g.id, name: g.label })) : []) .concat({ id: 'seeds', name: 'Your seeds' + (r.seeds && r.seeds.length ? ' · ' + r.seeds.length : '') }); if (!tabs.some(t => t.id === S.rtab)) S.rtab = 'new'; $('#randoTabs').innerHTML = tabs.map((t, i) => `${esc(t.name)}`).join(''); const on = tabs.findIndex(t => t.id === S.rtab); $('#s-rando').dataset.first = 'rt-' + Math.max(0, on); } function rRow(id, label, value, act, em, tip, cls) { return `
${esc(label)}${esc(value)}${esc(em)}
`; } function drawRandoNew(r) { const s = rSchema(); // (OOTRR is there but its settings could not be read: said, with a way to try again) if (r.tool && r.tool.have && !s && r.schemaError) return `

OOTRR's settings could not be read: ${esc(r.schemaError)}

` + rRow('retry', 'Read them again', 'Ask OOTRR for its settings once more', 'rando.open', 'Try again', rTip('If it keeps failing, the launcher\'s log says more (Settings, Log folder).')); if (r.tool && r.tool.have && !s) return '

Reading OOTRR\'s settings…

'; if (!r.tool || !r.tool.have || !s) return `

${r.tool && r.tool.busy ? 'Getting the randomizer…' : 'Get the randomizer to choose its settings. It is a download of ' + esc(r.tool ? r.tool.size : '5 MB') + ', once.'}

`; const preset = s.presets.find(p => p.id === r.preset); const cos = r.cos || {}; const cosShow = k => (RCOS[k].opts.find(o => o[0] === cos[k]) || RCOS[k].opts[0])[1]; return '

Settings

' + rRow('preset', 'Preset', preset ? preset.name : 'Custom · ' + (r.changed || 0) + ' changed from OOTRR\'s defaults', 'rpreset', 'Change', rTip(preset ? preset.tip : 'Your own choice of settings: every tab above shows them.')) + rRow('string', 'Settings string', 'Paste one to play the same settings as someone else', 'rpaste', 'Paste', rTip('A settings string (it starts with ' + (s.prefix || 'OOTRR1-') + ') sets every setting at once. Your seeds give theirs: Your seeds, then a seed, then Copy settings string.')) + '

Seed

' + rRow('seed', 'Seed', r.seed || '(a random one)', 'rseedtext', 'Change', rTip('The same seed with the same settings and OOTRR version makes the same game on every PC: share both for a race.')) + rRow('reseed', 'New random seed', 'Draw another seed text', 'rando.reseed', 'Draw', rTip('Eight letters and digits, drawn at random.')) + '

Cosmetics

' + ['music', 'sfx', 'colors'].map(k => rRow('cos-' + k, RCOS[k].label, cosShow(k), 'rcos:' + k, 'Change', rTip(RCOS[k].opts.map(o => o[1] + ': ' + o[2]).join('. ')))).join('') + rRow('cos-title', 'Title screen', cos.title === false ? 'As it comes' : 'Says Randomizer under the logo', 'rcostitle', cos.title === false ? 'Turn on' : 'Turn off', rTip('OOTRR writes a Randomizer line under the game\'s logo, so a seed is easy to tell from the plain game.')) + '

About

' + rRow('about', 'OOTRR ' + r.tool.version, 'The Ocarina of Time Remake Randomizer, by its own team · free software (GPL-3.0)', 'rando.page', 'Its page', rTip('Triforce runs OOTRR\'s own program. Its logic comes from the OoT Randomizer for the N64 game. Report a seed that cannot be finished on its page.')); } function drawRandoGroup(r, group) { const s = rSchema(); if (!s || !r.rows) return ''; const list = s.settings.filter(x => x.group === group && r.rows[x.id] && r.rows[x.id].st !== 'hidden'); if (!list.length) return '

Nothing to set here with the settings as they are.

'; const g = s.groups.find(x => x.id === group); return (g && g.tip ? `

${esc(g.tip)}

` : '') + list.map(x => { const row = r.rows[x.id], held = row.st === 'held', soon = !!x.unsupported; const tip = rTip(x.tip + (held ? ' (' + row.why + ')' : '') + (soon ? ' (OOTRR\'s build does not apply this one yet.)' : '')); const cls = held || soon || (x.noOn && !row.v) ? 'off' : ''; if (x.kind === 'number' && !held && !soon) { const hi = row.max != null ? row.max : x.max, v = Math.min(+row.v, hi), p = hi > x.min ? (v - x.min) / (hi - x.min) * 100 : 0; return `
${esc(x.label)}
${esc(rNum(x, v))}
`; } const em = held ? 'Held' : soon ? 'Not yet' : x.kind === 'toggle' ? (row.v ? 'Turn off' : 'Turn on') : 'Change'; const act = held || soon || (x.noOn && !row.v) ? 'rnone' : x.kind === 'toggle' ? 'rtog:' + x.id : x.kind === 'multi' ? 'rmulti:' + x.id : 'rchoice:' + x.id; return rRow(x.id, x.label, row.show, act, em, tip, cls); }).join(''); } function drawRandoSeeds(r) { const seeds = r.seeds || []; if (!seeds.length) return `

${r.game ? 'No seeds yet. Make one on New seed: it goes into your mods folder, on.' : esc(r.note || '')}

`; return '
' + seeds.map((x, i) => `
` + `
Seed ${esc(x.seed)}${esc(x.sub)}
${x.on ? '◆ On' : 'Off'}
`).join('') + '
'; } function drawRandoCard(r) { const t = r.tool || {}, card = $('#randoCard'); if (!t.have) { const pct = Math.round((t.frac || 0) * 100); card.innerHTML = '

Get the randomizer

OOTRR shuffles where the items are, and makes sure every seed can be finished. Triforce downloads its program once (' + esc(t.size || '') + ') and checks it before it runs.

' + (t.busy ? `

${esc(t.line || 'Downloading')}

` : `
Download
`); return; } const s = rSchema(), preset = s && s.presets.find(p => p.id === r.preset); card.innerHTML = `

Seed ${esc(r.seed || '')}

` + `

${esc(preset ? preset.name : 'Custom settings')} · ${r.changed || 0} changed from OOTRR's defaults

` + (r.busy ? `

${esc(r.line || 'Working')}

Stop
` : (r.problem ? `

OOTRR cannot make these settings yet: ${esc(r.problem)}

` : '') + `
Make the seed
`) + `

It goes into your mods folder, on, with your other seeds off. Start a new game in an empty file to play it.

` + ((r.seeds || []).some(x => x.on) ? `

Turn every seed off to play the game as it comes.

` : ''); } function drawRando() { const r = S.rando; if (!r) return; // (a slider being dragged, or one whose value is on its way, is not drawn over: the launcher's answer draws the page then) if (S.rDrag || S.rSend) { S.rPending = true; return; } S.rPending = false; drawRandoTabs(); const keep = focused && focused.id; const body = S.rtab === 'new' ? drawRandoNew(r) : S.rtab === 'seeds' ? drawRandoSeeds(r) : drawRandoGroup(r, S.rtab.slice(2)); $('#rando').innerHTML = body + (r.note && S.rtab !== 'seeds' ? `

${esc(r.note)}

` : ''); drawRandoCard(r); $('#randoEb').textContent = r.tool && r.tool.have ? 'OOTRR ' + r.tool.version + ' · every seed can be finished' : 'Shuffle the items · every seed can be finished'; if (keep && $('#' + keep) && screen === 'rando') { focused = null; setFocus($('#' + keep)); } else if (screen === 'rando' && (!focused || !document.contains(focused))) { focused = null; setFocus($('#' + $('#s-rando').dataset.first)); } } /* the focused row's tip, under the list (mouse, keyboard and controller alike) */ function randoFocus(el) { const box = $('#randoTip'); if (!box) return; const tip = el && el.closest && el.closest('#s-rando') ? (el.dataset.tip || (el.closest('[data-tip]') || {}).dataset?.tip || '') : null; if (tip == null) return; // (data-tip comes back as plain text, the attribute's escaping undone: it is escaped again here, as OOTRR's words and a seed's // file may hold anything) const row = el.closest('.row-set, .mod'), name = row && row.querySelector('b') ? row.querySelector('b').textContent : ''; box.innerHTML = tip ? `
${name ? `${esc(name)}` : ''}

${esc(tip)}

` : ''; box.classList.toggle('on', !!tip); } /* ---------------- sheets ---------------- */ function openInput(title, text, value, reply, placeholder, max) { openSheet(`

${esc(title)}

${text}

` + `
Cancel
OK
`, 'rIn'); S.rInReply = reply; const inp = $('#rIn'); inp.addEventListener('keydown', e => { if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); ACTIONS.rinok(); } else if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); closeSheet(); } else if (e.key === 'ArrowDown') { e.preventDefault(); e.stopPropagation(); inp.blur(); setFocus($('#rInOk'), true); } else e.stopPropagation(); }); setTimeout(() => { inp.focus(); inp.select(); }, 30); } ACTIONS.rinok = () => { const v = ($('#rIn') || {}).value || ''; const reply = S.rInReply; closeSheet(true); if (reply) cmd(reply, { text: v }); }; function rChoiceSheet(title, text, options, reply, ctx, ok) { const at = Math.max(0, options.findIndex(o => o.sel)); const opts = options.map((o, i) => `
` + `
${esc(o.name)}${o.note ? `${esc(o.note)}` : ''}${o.sub ? `${esc(o.sub)}` : ''}
`).join(''); openSheet(`

${esc(title)}

${text ? `
${text}
` : ''}
${opts}
` + `
Cancel
${esc(ok || 'Choose')}
`, 'op' + at); S.rChoice = { reply, ctx }; const sel = $('#op' + at); if (sel) sel.scrollIntoView({ block: 'nearest' }); } ACTIONS.rchoiceok = () => { const o = $('.opt.sel', $('#sheet')), c = S.rChoice; closeSheet(true); if (o && c) cmd(c.reply, { id: o.dataset.id, ctx: c.ctx }); }; function openMulti(x, row) { const picked = new Set(Array.isArray(row.v) ? row.v : []); const described = x.choices.some(c => c.tip); const opts = x.choices.map((c, i) => `
` + `
${esc(c.label)}${described && c.tip ? `${esc(c.tip)}` : ''}
`).join(''); openSheet(`

${esc(x.label)}

${rTip(x.tip)}
${opts}
` + `
NoneCancel
Done
`, 'mo0'); S.rMulti = x.id; mCount(); } /* how many are ticked, beside the sheet's title */ function mCount() { const n = $$('#sheet .opt.sel').length, c = $('#mCount'); if (c) c.textContent = n ? '\u00b7 ' + n + ' chosen' : ''; } ACTIONS.mopt = (a, el) => { el.classList.toggle('sel'); mCount(); }; ACTIONS.mclear = () => { $$('#sheet .opt').forEach(o => o.classList.remove('sel')); mCount(); }; ACTIONS.mdone = () => { const v = $$('#sheet .opt.sel').map(o => o.dataset.id); const id = S.rMulti; closeSheet(true); cmd('rando.set', { id, value: v }); }; function openSeedSheet(i) { const x = ((S.rando || {}).seeds || [])[i]; if (!x) return; openSheet(`

Seed ${esc(x.seed)}

${esc(x.sub)}${x.cos ? ' · ' + esc(x.cos) : ''}

` + `

${x.on ? 'This seed is on: the game plays it from its next start. Saves you make now belong to it: play them only with it on.' : 'Turning it on turns your other seeds off. Start a new game in an empty file, or load a save you made with this seed.'}

` + `
Close` + (x.spoiler ? `Spoiler log` : '') + (x.settings ? `Copy settings string` : '') + `Its folderRemove` + (x.on ? `
Turn off
` : `
Turn on
`) + '
', 'sh1'); // (the seed by its folder, not its place in the list: a seed made or removed while the sheet is open moves the others) S.rSheetSeed = x.id; } /* ---------------- actions ---------------- */ ACTIONS.rtab = (t) => { S.rtab = t; drawRando(); $('#rando').scrollTop = 0; const i = $$('#randoTabs span').findIndex(s => s.dataset.act === 'rtab:' + t); setFocus($('#rt-' + Math.max(0, i))); }; ACTIONS.rnone = () => sfx('error'); ACTIONS.rtog = (id) => { const row = S.rando.rows[id]; if (row) cmd('rando.set', { id, value: !row.v }); }; ACTIONS.rchoice = (id) => { const x = rSetting(id), row = S.rando.rows[id]; if (!x || !row) return; // (an option OOTRR's build cannot apply yet is listed, marked, and refused by the launcher if chosen) rChoiceSheet(x.label, rTip(x.tip), x.choices.map(c => ({ id: c.id, name: c.label, note: c.live === false ? 'Not in this OOTRR yet' : '', dim: c.live === false, sub: c.tip, sel: c.id === row.v })), 'rando.pick', id); }; ACTIONS.rmulti = (id) => { const x = rSetting(id), row = S.rando.rows[id]; if (x && row) openMulti(x, row); }; ACTIONS.rpreset = () => { const s = rSchema(); if (!s) return; rChoiceSheet('Preset', 'OOTRR\'s own starting points. Every setting can still be changed after.', s.presets.map(p => ({ id: p.id, name: p.name, sub: p.tip, sel: p.id === S.rando.preset })), 'rando.preset.pick', '', 'Use it'); }; ACTIONS.rpaste = () => openInput('Paste a settings string', 'It sets every setting at once, as the seed it came from had them.', '', 'rando.decode', (rSchema() || {}).prefix ? rSchema().prefix + '…' : 'OOTRR1-…'); ACTIONS.rseedtext = () => openInput('Seed', 'Any text, up to ' + (S.rando.maxSeed || 200) + ' letters: the same text with the same settings and OOTRR makes the same seed. Leave it empty for a random one.', S.rando.seed || '', 'rando.seed', 'A word, a date, anything', S.rando.maxSeed || 200); ACTIONS.rcos = (k) => { const c = RCOS[k]; if (!c) return; rChoiceSheet(c.label, 'Applied when the seed\'s files are written.', c.opts.map(o => ({ id: o[0], name: o[1], sub: o[2], sel: o[0] === (S.rando.cos || {})[k] })), 'rando.cos.pick', k); }; ACTIONS.rcostitle = () => cmd('rando.cos', { key: 'title', value: (S.rando.cos || {}).title === false ? 'on' : 'off' }); ACTIONS.rseed = (i) => openSeedSheet(+i); ACTIONS.rsgo = (what) => { const id = S.rSheetSeed; closeSheet(true); if (id) cmd('rando.' + what, { id }); }; ACTIONS.rsoff = () => { closeSheet(true); cmd('rando.off'); };