Skip to content

Watch a replay, and save one ​

A game scene with replays: true records every game (the seed and the commands) in the browser. replaysScene lists them and plays one with a bar to pause, speed up and seek.

ts
// in src/main.ts
import { appScenes, gameScene, replaysScene, sceneAsked, sceneRef, startApp } from '@v12-engine/client/app'
import { listReplays } from '@v12-engine/client'
import { game } from './game'
import { mapNamed, maps } from './maps'
import { onStart } from './rules'

const scenes = appScenes({ game, maps, onStart })
// the play scene in its place, recording every game it plays; the list of them a scene
const play = gameScene({
	id: 'play',
	name: 'Play',
	game,
	map: () => mapNamed(),
	onStart,
	replays: true,
	exit: { label: 'Home', scene: sceneRef('home') }
})
scenes[scenes.findIndex((scene) => scene.id === play.id)] = play
scenes.push(replaysScene())
startApp({ target: '#game', title: 'My game', scenes, start: sceneAsked(scenes, 'home') })

// what is kept, from anywhere on the page
const replays = await listReplays()
console.log(replays.map((replay) => `${replay.name}: ${replay.seconds}s`))

Done: play a game, go home, open Replays, and watch it again.

Send replays somewhere else ​

ts
replays: { keep: (replay) => upload(replay) }

Play one in a place of your own ​

watchReplay(replay, { game, canvas, overlay }) makes a client with the replay bar over it, as the replays scene does.

Reference ​

NameWhat it is
ReplayPlain data: game id and version, map, seed, commands by tick. Plays anywhere: server, page, test.
ReplayPlaybackThe bar's controller: playing, speed, seconds, seekTo.
ReplaysControllerThe list's: list, remove.
listReplays()What's kept in this browser.
CommandReplay.debugWhether debug tools were used, so cheats play back the same.

A replay records its rules' version: after you change the rules, an old replay can't play back exactly. See Sessions and Testing and balancing rules.