261 lines
9.1 KiB
JavaScript
261 lines
9.1 KiB
JavaScript
import { DIRECTIONS, PHASES, GENE } from "../engine/catalog.js";
|
|
import { amoeba, colors, icon, PHASE_ICONS } from "./art.js";
|
|
import { richText, renderRichText } from "./rich-text.js";
|
|
|
|
const escape = (text) =>
|
|
String(text).replace(
|
|
/[&<>"']/g,
|
|
(c) =>
|
|
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
|
c
|
|
],
|
|
);
|
|
const escapePattern = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
function coordinates(text) {
|
|
return text
|
|
.split(/(\b[A-E][1-5]\b)/)
|
|
.map((part) =>
|
|
/^[A-E][1-5]$/.test(part)
|
|
? `<span class="journal-cell" title="Space ${part}">${part}</span>`
|
|
: richText(part),
|
|
)
|
|
.join("");
|
|
}
|
|
|
|
export function playerReference(player, number, translate = (text) => text) {
|
|
const label = number
|
|
? translate(`${player.name}, amoeba ${number}`)
|
|
: player.name;
|
|
const portrait = `<span class="journal-portrait">${amoeba(player.color, Number(number) || 0)}${number ? `<b>${number}</b>` : ""}</span>`;
|
|
return `<span class="${number ? "journal-amoeba" : "journal-player"}" title="${escape(label)}" ${number ? `role="img" tabindex="0" aria-label="${escape(label)}"` : ""}>${portrait}${number ? "" : `<strong>${escape(player.name)}</strong>`}</span>`;
|
|
}
|
|
|
|
function textRenderer(players) {
|
|
const byName = new Map(
|
|
players.filter((p) => p.name && colors[p.color]).map((p) => [p.name, p]),
|
|
);
|
|
if (!byName.size) return coordinates;
|
|
const names = [...byName.keys()]
|
|
.sort((a, b) => b.length - a.length)
|
|
.map(escapePattern)
|
|
.join("|");
|
|
const references = new RegExp(
|
|
`(?<![\\p{L}\\p{N}_])(${names})(?: #([1-7])(?!\\d))?(?![\\p{L}\\p{N}_])`,
|
|
"gu",
|
|
);
|
|
const placement = new RegExp(
|
|
`^(${names}) places amoeba #([1-7]) at (.+)$`,
|
|
"u",
|
|
);
|
|
return (text) => {
|
|
const placed = text.match(placement);
|
|
if (placed)
|
|
return `${playerReference(byName.get(placed[1]), placed[2])} placed at ${coordinates(placed[3])}`;
|
|
let result = "",
|
|
offset = 0;
|
|
for (const match of text.matchAll(references)) {
|
|
result += coordinates(text.slice(offset, match.index));
|
|
result += playerReference(byName.get(match[1]), match[2]);
|
|
offset = match.index + match[0].length;
|
|
}
|
|
return result + coordinates(text.slice(offset));
|
|
};
|
|
}
|
|
|
|
function environmentEntry(line) {
|
|
const match =
|
|
line.phase === 1 &&
|
|
line.text.match(
|
|
/^The current is (west|north|east|south|still)\. Ozone layer: (\d+)\.$/,
|
|
);
|
|
if (!match) return;
|
|
const direction = DIRECTIONS.findIndex((d) => d.toLowerCase() === match[1]);
|
|
return `<div class="journal-environment" role="img" aria-label="Current ${match[1]}. Ozone layer ${match[2]}."><span class="journal-current"><span class="journal-current-arrow" aria-hidden="true" style="transform:rotate(${[180, 270, 0, 90, 0][direction]}deg)">${direction === 4 ? "·" : "➜"}</span><strong>${DIRECTIONS[direction]}</strong></span><span class="journal-ozone" title="Ozone layer ${match[2]} — protection from radiation">${icon("ozone")}<strong>${match[2]}</strong></span></div>`;
|
|
}
|
|
|
|
function swimmingRoll(line, renderText, players, omitSubject) {
|
|
const match = line.text.match(
|
|
/^(.+) #([1-7]) rolls ([1-6])(?: and ([1-6]))?\.$/,
|
|
);
|
|
if (!match || !players.some((player) => player.name === match[1])) return;
|
|
const pips = [
|
|
[],
|
|
[[12, 12]],
|
|
[
|
|
[6, 6],
|
|
[18, 18],
|
|
],
|
|
[
|
|
[6, 6],
|
|
[12, 12],
|
|
[18, 18],
|
|
],
|
|
[
|
|
[6, 6],
|
|
[18, 6],
|
|
[6, 18],
|
|
[18, 18],
|
|
],
|
|
[
|
|
[6, 6],
|
|
[18, 6],
|
|
[12, 12],
|
|
[6, 18],
|
|
[18, 18],
|
|
],
|
|
[
|
|
[6, 6],
|
|
[18, 6],
|
|
[6, 12],
|
|
[18, 12],
|
|
[6, 18],
|
|
[18, 18],
|
|
],
|
|
];
|
|
const dice = match
|
|
.slice(3)
|
|
.filter(Boolean)
|
|
.map((value) => {
|
|
const direction = ["West", "North", "East", "South", "Stay", "Choose"][
|
|
Number(value) - 1
|
|
];
|
|
return `<span class="journal-die" role="img" aria-label="Rolled ${value}: ${direction}" title="${value}: ${direction}"><svg viewBox="0 0 24 24" aria-hidden="true"><rect x="1" y="1" width="22" height="22" rx="4"/>${pips[Number(value)].map(([x, y]) => `<circle cx="${x}" cy="${y}" r="1.8"/>`).join("")}</svg><small>${direction}</small></span>`;
|
|
})
|
|
.join("");
|
|
return `<span class="journal-text">${omitSubject ? "" : `${renderText(`${match[1]} #${match[2]}`)} `}rolls <span class="journal-dice">${dice}</span>.</span>`;
|
|
}
|
|
|
|
export function renderJournalSentence(
|
|
source,
|
|
players,
|
|
translate = (text) => text,
|
|
omitSubject,
|
|
) {
|
|
let subjectOmitted = false;
|
|
const byName = new Map(
|
|
players
|
|
.filter((p) => p.name && colors[p.color])
|
|
.map((p) => [escape(p.name), p]),
|
|
);
|
|
const names = [...byName.keys()]
|
|
.sort((a, b) => b.length - a.length)
|
|
.map(escapePattern)
|
|
.join("|");
|
|
const references =
|
|
names &&
|
|
new RegExp(
|
|
`(?<![\\p{L}\\p{N}_])(${names})(?: #([1-7])(?!\\d))?(?![\\p{L}\\p{N}_])`,
|
|
"gu",
|
|
);
|
|
return renderRichText(source, translate)
|
|
.split(/(<[^>]+>)/g)
|
|
.map((part) => {
|
|
if (part.startsWith("<")) return part;
|
|
return part
|
|
.split(/(\b[A-E][1-5]\b)/)
|
|
.map((piece) =>
|
|
/^[A-E][1-5]$/.test(piece)
|
|
? `<span class="journal-cell" title="${escape(translate(`Space ${piece}`))}">${piece}</span>`
|
|
: references
|
|
? piece.replace(references, (_, name, number) => {
|
|
const player = byName.get(name);
|
|
if (
|
|
!subjectOmitted &&
|
|
omitSubject?.name === player.name &&
|
|
String(omitSubject.number) === number
|
|
) {
|
|
subjectOmitted = true;
|
|
return "";
|
|
}
|
|
return playerReference(player, number, translate);
|
|
})
|
|
: piece,
|
|
)
|
|
.join("");
|
|
})
|
|
.join("")
|
|
.trim();
|
|
}
|
|
|
|
function journalSentence(source, players, omitSubject) {
|
|
const data = players.map(({ name, color }) => ({ name, color }));
|
|
return `<span data-localized-rich-text="${escape(source)}" data-journal-players="${escape(JSON.stringify(data))}"${omitSubject ? ` data-journal-omit-subject="${escape(JSON.stringify(omitSubject))}"` : ""} translate="no">${renderJournalSentence(source, data, undefined, omitSubject)}</span>`;
|
|
}
|
|
|
|
function renderEntry(line, renderText, players, omitSubject) {
|
|
const entry =
|
|
environmentEntry(line) ??
|
|
swimmingRoll(line, renderText, players, omitSubject) ??
|
|
`<span class="journal-text">${journalSentence(line.text, players, omitSubject)}</span>`;
|
|
const causes = [...new Set(line.genes ?? [])]
|
|
.filter((id) => GENE[id])
|
|
.map((id) => richText(GENE[id].name));
|
|
return [entry, ...causes].join(" ");
|
|
}
|
|
|
|
export function journalEntryHtml(line, players) {
|
|
return line ? renderEntry(line, textRenderer(players), players) : "";
|
|
}
|
|
|
|
function actionGroups(lines, players, renderText) {
|
|
const groups = [];
|
|
const byLongestName = [...players].sort(
|
|
(a, b) => b.name.length - a.name.length,
|
|
);
|
|
for (const line of lines) {
|
|
const player = byLongestName.find((p) =>
|
|
line.text.startsWith(`${p.name} #`),
|
|
);
|
|
const number =
|
|
player && line.text.slice(player.name.length).match(/^ #([1-7]) /)?.[1];
|
|
const subject = number ? { name: player.name, number } : undefined;
|
|
const previous = groups.at(-1);
|
|
if (
|
|
subject &&
|
|
previous?.subject?.name === subject.name &&
|
|
previous.subject.number === subject.number
|
|
)
|
|
previous.lines.push(line);
|
|
else groups.push({ player, subject, lines: [line] });
|
|
}
|
|
return groups
|
|
.map(({ player, subject, lines }) =>
|
|
lines.length === 1
|
|
? `<li class="journal-event">${renderEntry(lines[0], renderText, players)}</li>`
|
|
: `<li class="journal-event journal-event--combined">${playerReference(player, subject.number)}<span class="journal-sequence">${lines.map((line) => `<span class="journal-action">${renderEntry(line, renderText, players, subject)}</span>`).join(" ")}</span></li>`,
|
|
)
|
|
.join("");
|
|
}
|
|
|
|
export function journalHtml(lines, players) {
|
|
if (!lines.length) return '<li class="journal-empty">No moves yet.</li>';
|
|
const renderText = textRenderer(players);
|
|
const groups = [];
|
|
for (const line of lines) {
|
|
let round = groups.at(-1);
|
|
if (!round || round.number !== line.round) {
|
|
round = { number: line.round, phases: [] };
|
|
groups.push(round);
|
|
}
|
|
let group = round.phases.at(-1);
|
|
if (!group || group.phase !== line.phase) {
|
|
group = { phase: line.phase, lines: [] };
|
|
round.phases.push(group);
|
|
}
|
|
group.lines.push(line);
|
|
}
|
|
return groups
|
|
.map(({ number, phases }) => {
|
|
return `<li class="journal-round-group"><h3 class="journal-round-divider">${number ? `Round ${Number(number)}` : "Setup"}</h3><ol>${phases
|
|
.map(({ phase, lines }) => {
|
|
const heading =
|
|
typeof phase === "number" && PHASES[phase]
|
|
? `<h4>${icon(PHASE_ICONS[phase])}${phase + 1} · ${escape(PHASES[phase])}</h4>`
|
|
: "";
|
|
return `<li class="journal-group">${heading}<ol>${actionGroups(lines, players, renderText)}</ol></li>`;
|
|
})
|
|
.join("")}</ol></li>`;
|
|
})
|
|
.join("");
|
|
}
|