Files
tiny-worlds/web/view-story.mjs
T

57 lines
2.0 KiB
JavaScript

// web/view-story.mjs -- the story: the whole deliverable, read-only.
//
// Every non-dropped literature turn from turn-0 to the last
// recorded, across all sessions in order -- the reading surface the
// adjudication review reads ("machinery seams show as prose seams").
// OOC, tools, verbs, thinking, rings: all out, by ruling; carried
// copies never re-render, each turn appears once from its home
// session. Readable by anyone signed in -- spectators read
// everything.
import { $, el } from './dom.mjs';
import { api } from './client.mjs';
import { state } from './state.mjs';
import { banner } from './chrome.mjs';
import { navigate, runGone } from './router.mjs';
import { renderContent } from './transcript.mjs';
let storyRun = null;
export async function enter(id, seq) {
storyRun = id;
let detail;
try {
detail = await api(`/runs/${id}`);
} catch (e) {
if (e.status === 404) return runGone(id, 'the story view');
return banner(`could not reach the server: ${e.message}`);
}
if (seq !== state.seq) return;
$('story-title').textContent = `${detail.display} -- the story`;
const story = await api(`/runs/${id}/story`).catch(() => null);
if (seq !== state.seq) return;
if (!story) return banner('the story could not be read');
const box = $('story');
box.textContent = '';
if (!story.sessions.length) {
box.appendChild(el('div', 'hint', 'nothing has happened yet -- the story begins at the table'));
return;
}
for (const [si, s] of story.sessions.entries()) {
if (si > 0) box.appendChild(el('div', 'session-break'));
for (const turn of s.turns) {
const d = el('div', `turn ${turn.who}`);
d.appendChild(el('div', 'who', turn.who === 'gm' ? 'GM' : 'player'));
const body = el('div');
d.appendChild(body);
renderContent(body, turn.text, { extract: turn.who === 'gm' });
box.appendChild(d);
}
}
}
// wired once at module load: the back button returns to the table
$('story-back').addEventListener('click', () => {
if (storyRun) navigate(`/runs/${storyRun}`);
});