diff --git a/site/atlas/app.js b/site/atlas/app.js index 61ccbee..90ae554 100644 --- a/site/atlas/app.js +++ b/site/atlas/app.js @@ -272,7 +272,15 @@ const chance = s.chance; const t = svgEl("text", { x: colX(i), y: 18, class: "stage-label" + (chance ? " stage-chance" : "") }, svg); t.textContent = s.label; - if (chance) { const r = svgEl("text", { x: colX(i), y: 31, class: "stage-label stage-chance" }, svg); r.textContent = "game roll"; } + if (chance) { + const r = svgEl("text", { x: colX(i), y: 31, class: "stage-label stage-chance" }, svg); + r.textContent = "game roll"; + const here = G.nodes.filter((n) => n.stage === i), tot = here.reduce((a, n) => a + n.n, 0) || 1; + const w = r.getComputedTextLength ? r.getComputedTextLength() : 70; // the headings are left-aligned + Dice.svg(svg, colX(i) + w + 15, 27, { title: `${s.label}: the game's roll`, n: tot, + options: here.map((n) => [n.value, n.n / tot]), + note: "Share of riders who reached this step, in the runs shown. The game decides it; earlier choices can shift the odds." }); + } }); // links @@ -342,7 +350,7 @@ ${state.widthMode === "traffic" ? "Band width = number of runs" : "Line width = chance of the outcome"} 0% ${pct(pmax, 0)} Grey = fewer than ${state.minN} runs - Gold headings = steps the game rolls, not you`; + ${Dice.legend('and gold headings = steps the game rolls at random, not your choice. Click the dice for the odds of each outcome.')}`; } function renderBestRoutes() { diff --git a/site/atlas/dice.js b/site/atlas/dice.js new file mode 100644 index 0000000..75b61d7 --- /dev/null +++ b/site/atlas/dice.js @@ -0,0 +1,97 @@ +/* + * Rolls of the dice. Wherever the page shows a step the game decides at random (not the rider's choice), a pair of + * dice sits next to it. Clicking the dice shows the odds of every outcome of that roll. + * + * Dice.html(roll) an inline button for HTML text + * Dice.svg(parent, x, y, roll) the same glyph inside an SVG map + * Dice.legend(text?) the legend line ("a roll: the game decides ...") + * + * roll = { title, options: [[label, p], ...], note?, n? } (p between 0 and 1; options may be empty with a note) + */ +(function () { + "use strict"; + const NS = "http://www.w3.org/2000/svg"; + // two dice, the back one showing two, the front one five + const GLYPH = ``; + const TIP = "A roll: the game decides, not you. Click for the odds of each outcome."; + + const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c])); + const pack = (roll) => encodeURIComponent(JSON.stringify(roll || {})); + const pct = (p) => (p == null ? "–" : p < 0.001 && p > 0 ? "<0.1%" : `${(100 * p).toFixed(p < 0.1 ? 1 : 0)}%`); + + function html(roll) { + return ``; + } + + function svg(parent, x, y, roll) { + const g = document.createElementNS(NS, "g"); + g.setAttribute("class", "dice dice-svg"); + g.setAttribute("data-roll", pack(roll)); + g.setAttribute("transform", `translate(${x - 11},${y - 8})`); + g.setAttribute("tabindex", "0"); + g.setAttribute("role", "button"); + g.setAttribute("aria-label", `${(roll && roll.title) || "A roll"}: show the odds`); + const t = document.createElementNS(NS, "title"); + t.textContent = TIP; + g.appendChild(t); + const halo = document.createElementNS(NS, "rect"); + for (const [k, v] of Object.entries({ x: -3, y: -3, width: 28, height: 22, rx: 6, fill: "var(--map-halo, var(--bg))", "fill-opacity": 0.9 })) halo.setAttribute(k, v); + g.appendChild(halo); + g.insertAdjacentHTML("beforeend", GLYPH.replace(/^]*>/, "").replace(/<\/svg>$/, "")); + parent.appendChild(g); + return g; + } + + function legend(text) { + return `${GLYPH.replace('class="dice-glyph"', 'class="dice-glyph legend" style="display:inline-block;vertical-align:-3px"')} ${text || "= a roll: the game decides at random, not your choice. Click any dice for the odds of each outcome."}`; + } + + // ---- the popover + let pop = null; + function hide() { if (pop) pop.hidden = true; } + function show(roll, x, y) { + if (!pop) { + pop = document.createElement("div"); + pop.id = "dice-pop"; + pop.setAttribute("role", "dialog"); + document.body.appendChild(pop); + } + const opts = (roll.options || []).filter(([, p]) => p != null).sort((a, b) => b[1] - a[1]); + pop.innerHTML = `${esc(roll.title || "A roll")} + ${opts.length ? `` : ""} +

${esc(roll.note || "The game decides this at random. Your earlier choices can change the odds, but not the outcome.")}${roll.n ? ` From ${Number(roll.n).toLocaleString()} runs.` : ""}

`; + pop.hidden = false; + const r = pop.getBoundingClientRect(); + pop.style.left = `${Math.max(8, Math.min(window.innerWidth - r.width - 8, x - r.width / 2))}px`; + pop.style.top = `${y + 14 + r.height > window.innerHeight ? Math.max(8, y - r.height - 14) : y + 14}px`; + } + + function open(e, t) { + e.preventDefault(); + e.stopPropagation(); + let roll = {}; + try { roll = JSON.parse(decodeURIComponent(t.getAttribute("data-roll"))); } catch { /* an empty roll */ } + const b = t.getBoundingClientRect(); + show(roll, b.left + b.width / 2, b.bottom - 6); + } + // capture phase: a dice inside a map or a card must not also select what is under it + document.addEventListener("click", (e) => { + const t = e.target.closest && e.target.closest("[data-roll]"); + if (t) return open(e, t); + if (pop && !pop.hidden && !pop.contains(e.target)) hide(); + }, true); + document.addEventListener("keydown", (e) => { + if (e.key === "Escape") return hide(); + const t = (e.key === "Enter" || e.key === " ") && e.target.closest && e.target.closest(".dice-svg[data-roll]"); + if (t) open(e, t); + }, true); + window.addEventListener("scroll", hide, { passive: true }); + + window.Dice = { html, svg, legend }; +})(); diff --git a/site/atlas/explorer.js b/site/atlas/explorer.js index 07d58ce..38d467f 100644 --- a/site/atlas/explorer.js +++ b/site/atlas/explorer.js @@ -325,6 +325,13 @@ pills.push({ g, x, y, w, h, dy: 0 }); el("rect", { x: x - w / 2, y: y - h / 2, width: w, height: h, rx: h / 2, fill: bg, "fill-opacity": 0.95, stroke: edge || ink, "stroke-opacity": edge ? 0.95 : 0.4, "stroke-width": edge ? 2 : 1 }, g); el("text", { x, y: y + (strong ? 4.5 : 4), "text-anchor": "middle", "font-size": strong ? 12.5 : 11, "font-weight": strong ? 700 : 500, fill: ink, "font-family": "var(--font-data)" }, g).textContent = text; + return { g, w }; + } + + /** What a roll on the route can come out as: every outcome with its odds, for the dice. */ + function rollOf(s, title) { + return { title: title || s.label || "A roll", options: Object.entries(s.probs || {}).filter(([, p]) => p > 0.0005), + note: "The game decides this at random. The odds are the model's, given the choices on this route so far." }; } function drawDragons(g, routesOn) { @@ -527,7 +534,8 @@ if (p > 0.995) return; // the last roll leads straight into the dragon, whose own label sits there: put the roll under the fork point const at = !ro.at ? { x: ro.P.x + 34, y: ro.P.y - 22 } : ro.at === pts.length - 1 ? { x: pts[ro.at - 1].P.x, y: pts[ro.at - 1].P.y + 24 } : mid(pts[ro.at - 1].P, pts[ro.at].P, 0.38); - pill(gLab, at.x, at.y, ro.s.var === "shadow" ? (ro.s.taken === "yes" ? `shadow ${pct(p)}` : `no shadow ${pct(p)}`) : `roll ${pct(p)}`); + const lab = pill(gLab, at.x, at.y, ro.s.var === "shadow" ? (ro.s.taken === "yes" ? `shadow ${pct(p)}` : `no shadow ${pct(p)}`) : `roll ${pct(p)}`); + Dice.svg(lab.g, at.x - lab.w / 2 - 14, at.y, rollOf(ro.s, ro.s.var === "shadow" ? "Does the shadow show at the tree?" : null)); }); // destination label const end = pts[pts.length - 1]; @@ -715,7 +723,7 @@ const sp = st.info, dir = sp.place.split(":")[1]; const fs = st.policy ? S.makeParams(model().model.tables, { alphas: model().model.alphas }).chance("fs", { direction: dir }) : null; info = `
First sighting · ${dir === "Center" ? "forest centre" : dir.toLowerCase()} -

The first dragon is a roll of the dice: nobody bonds here, it decides what you face next.${fs ? ` Going ${dir === "Center" ? "to the forest centre" : dir.toLowerCase()}, riders met: ${Object.entries(fs).sort((a, b) => b[1] - a[1]).filter(([, p]) => p > 0.005).map(([k, p]) => `${k} ${pct(p)}`).join(", ")}.` : ""}

`; +

The first dragon is a roll of the dice${fs ? ` ${Dice.html({ title: `First dragon, going ${dir === "Center" ? "to the forest centre" : dir.toLowerCase()}`, options: Object.entries(fs).filter(([, p]) => p > 0.0005), note: "The game decides which dragon you see first. Which way you went changes the odds." })}` : ""}: nobody bonds here, it decides what you face next.${fs ? ` Going ${dir === "Center" ? "to the forest centre" : dir.toLowerCase()}, riders met: ${Object.entries(fs).sort((a, b) => b[1] - a[1]).filter(([, p]) => p > 0.005).map(([k, p]) => `${k} ${pct(p)}`).join(", ")}.` : ""}

`; } return `Unofficial atlas · built from ${st.nRuns ? st.nRuns.toLocaleString() : "thousands of"} logged runs

The valley

@@ -784,7 +792,7 @@ } const endOf = (o) => (o.end ? o.end : "continues"); function treeList(options) { - return `