Sessions: who sees what
A session is all a player's screen holds of a game. It hands out snapshots of what that player may know, and takes the player's commands. The same client works whether the world runs in the page, a worker or on a server.
// a command from the screen: the session decides it is this player's
session.send({ type: 'order', entities: [12], order: { type: 'move', point: { x: 30, z: 10 } } })
session.send(nextWave.make({})) // a command of the game's own (`defineCommand`)The session stamps who sent a command; the command never says.
Run the world in a worker
A long tick on the page's thread is a late frame. Move the world to a worker:
// game.worker.ts
import { serveWorld } from '@v12-engine/engine'
import { rules, onStart } from './rules'
serveWorld(rules, { onStart })// main.ts: no `start` here, it is the worker's
launch({ target: '#game', game, map: mapNamed(), worker: new Worker(new URL('./game.worker.ts', import.meta.url), { type: 'module' }) })The same seed plays the same game either way. Replays aren't recorded in a worker game.
The worker's file can also run code before play on a new or a loaded game (beforePlay), and tell the page plain data while the game lasts (tell in the context of onStart and beforePlay; the page hears it with session.onTold). A campaign uses this for a mission won. For a campaign, use serveCampaign of @v12-engine/client/campaign-worker and give startApp the worker (worker). See Keep the page smooth with workers.
Join a server
import { startServer } from '@v12-engine/engine/server'
const server = await startServer({ rules: rules, map: mapNamed(), onStart, ai: [2], port: 8787 })import { connect, openSocket } from '@v12-engine/engine'
launch({
target: '#game',
game,
session: async () => {
const transport = await openSocket('ws://localhost:8787')
const joining = connect(transport, { rules: game.rules })
transport.send(JSON.stringify({ type: 'join', name: 'Player' }))
return joining
}
})Snapshots arrive as deltas, and the screen draws a little behind the server to keep motion smooth. See Run a server and join it.
Try a game as if over a network
loopback() makes both ends of a transport in one page, with latency, jitter and loss:
import { connect, loopback, SessionHost } from '@v12-engine/engine'
const host = new SessionHost(world, { ownsWorld: true })
const [client, server] = loopback({ latency: 80, jitter: 30 })
host.connect(1, server)
host.start()
const session = await connect(client, { rules: rules })What a player is told
Every snapshot is cut to the player's team, the same in every session, so a game tested in the page keeps its secrets online.
| Information | Own and allied | Everyone else |
|---|---|---|
| Position, type, health | Always | Only while in sight |
| Orders, queue, rally point, carried gold | Yes | Never |
| Gold, supply, research | Own player | Never |
| Buildings out of sight | As last seen | |
| Deaths, attacks, projectiles | Yes | Only in sight |
Your own data follows the same rule: a component or event says who gets it (replicate), and Rules.report(world, player) is asked per player. See Multiplayer.
Watch and replay
- Watchers on a server: off unless the server's
watchoption allows them. They see the whole map. - A game shared from a page:
shareGamelets others watch through a relay. - Replays: the start and every command at its tick, replayed by running the game again (
CommandReplay). Exact, a few kilobytes. A hash every ten seconds says where a replay with changed rules came apart (ReplayPlayer.apart).
| Call | What it does |
|---|---|
recordGame(world, { player }) → stop() | Records, and gives the Replay. |
playReplay(replay, { rules: game }) | Plays one back; its session is the seat to draw. |
runReplay(replay, { rules: game }) | Runs one with no screen, for a test or a server. |
kind: 'frames' | Records what an observer was sent, for rules that can't be rerun. |
Reference
| Session | The world is | Made with |
|---|---|---|
LocalSession | in the page | launch({ map }), or new LocalSession(world, player, { debug, ownsWorld, observer }) |
WorkerSession | in a worker | launch({ worker }), or workerSession(worker, { rules: game, map }) |
RemoteSession | on a server, over a wire | launch({ session }), with connect(transport, { rules: game }) |
SessionHost | hosts one world for many seats | new SessionHost(world), host.connect(player, transport) |
GameSession member | What it is |
|---|---|
snapshot() | What the player knows now: units in sight, resources, objectives, the report, this tick's events. |
send(command) | A command from this player. |
registry, terrain, tickRate, playerId, team, fog() | What doesn't change, and the fog. |
can | Extras some sessions have: pace (speed, advance), saves, inspect, timing, computer. A remote seat has none. |
ownsWorld frees the world when the session is disposed. debug lets the debug panel reach the world (in the page only). observer: true sees the whole map.