Keep displayed dice in sync with sequential event playback

This commit is contained in:
coyotte508 2026-09-08 21:51:34 +02:00
commit a2d9ea5262
3 changed files with 32 additions and 3 deletions

View file

@ -38,7 +38,7 @@
import TravelGuide from "./lib/TravelGuide.svelte";
import StaminaGuide from "./lib/StaminaGuide.svelte";
let { store }: { store: Store } = $props();
const s = $derived(store.state);
const s = $derived(store.scene ?? store.state);
const actingSeats = $derived(
s
? activePlayers(s).filter(

View file

@ -115,8 +115,19 @@ export class Store {
}
const scene = structuredClone(previous);
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
for (const event of s.log.slice(previous.log.length)) {
const events = s.log.slice(previous.log.length);
// A broadcast contains only the final dice snapshot. Apply it at the last
// roll cue, never to earlier movement/eruption frames or intermediate rolls.
const lastRoll = events.reduce((last, event, index) => (event.sound === "dice" ? index : last), -1);
for (const [index, event] of events.entries()) {
scene.log.push(event);
if (index === lastRoll) {
scene.round = s.round;
scene.phase = s.phase;
scene.players = structuredClone(s.players);
scene.ghost = structuredClone(s.ghost);
scene.ghostVisible = [...s.ghostVisible];
}
const action = event.animation;
const cue = event.sound ?? (event.type === "equipment" ? "gear" : undefined);
if (action?.kind === "move") {

View file

@ -35,7 +35,15 @@ try {
text: "Lava followed",
animation: { kind: "eruption", cells: [start] },
});
next.round = initial.round + 1;
next.phase = "planning";
next.players[0].dice[0].face = initial.players[0].dice[0].face === 6 ? 1 : 6;
next.log.push({ round: next.round, type: "phase", text: "New round dice", sound: "dice" });
store.receive(next);
assert(
store.scene.players[0].dice[0].face === initial.players[0].dice[0].face,
"Next roll appeared before movement"
);
assert(
store.state.revision === next.revision && store.state.board.find((c) => c.id === start).lava,
"Authoritative state must update immediately"
@ -53,11 +61,21 @@ try {
await new Promise((resolve) => setTimeout(resolve, 760));
assert(store.journal.at(-1).text === "Lava followed", "Eruption did not advance on its own");
assert(store.scene.board.find((c) => c.id === start).lava, "Lava scene did not advance");
assert(
store.scene.players[0].dice[0].face === initial.players[0].dice[0].face,
"Next roll appeared during eruption"
);
await new Promise((resolve) => setTimeout(resolve, 1150));
assert(store.journal.at(-1).text === "New round dice", "Playback did not reach the roll");
assert(
store.scene.players[0].dice[0].face === next.players[0].dice[0].face,
"Dice did not update at their roll cue"
);
const later = structuredClone(next);
later.revision++;
later.log.push({ round: 1, type: "equipment", text: "Later action" });
store.receive(later);
assert(store.journal.at(-1).text === "Lava followed", "New updates jumped the queue");
assert(store.journal.at(-1).text === "New round dice", "New updates jumped the queue");
store.skipPresentation();
assert(
!store.animating && !store.waiting && store.journal.at(-1).text === "Later action",