Make an online game
Players on different machines in one game: a Node process runs the world, each page holds a seat and gets only what that seat may know. Your rules must import the engine only, so the page, the server and the tests play the same game.
Quick start
// server.ts: run with `npm run server` (Node 22)
import { startServer } from '@v12-engine/engine/server'
import { mapNamed } from './src/maps'
import { rules, onStart } from './src/rules'
const map = mapNamed()
const server = await startServer({
rules,
map,
onStart,
ai: map.players.slice(1).map((player) => player.id), // seats the computer plays; leave out for people only
port: Number(process.env.PORT ?? 8787)
})
process.once('SIGINT', () => void server.close().then(() => process.exit(0)))// src/main.ts
import { launch } from '@v12-engine/client'
import { connect, openSocket } from '@v12-engine/engine'
import { game } from './game'
const url = new URLSearchParams(location.search).get('server') || `ws://${location.hostname}:8787`
launch({
target: '#game',
game,
session: async () => {
const transport = await openSocket(url)
const joining = connect(transport, { rules: game.rules })
transport.send(JSON.stringify({ type: 'join', name: 'Player' }))
return joining
}
})Run npm run server and npm run dev side by side, then open /?server. The first player takes seat 1, the computer plays the rest, and the game starts.
Build it yourself
1. A lobby and reconnects
import { createScenes, onlineScene } from '@v12-engine/client/app'
const online = onlineScene({ rules: game.rules, url: 'wss://example.com/game' })
const scenes = createScenes([online])Players take seats, rename, change settings (ServerHooks.settings) and say ready; a dropped connection gets its seat back. The scene finds the server in url (OnlineArgs), then ?server=, then port 8787 of the page's host. Draw your own lobby with createLobby() (LobbyController).
2. Logins
startServer({
rules,
map,
seat: async ({ ticket }) => {
const user = await logins.check(ticket)
return user ? { player: user.seat, name: user.name, id: user.id } : { refuse: 'log in first' }
}
})The online scene sends ?ticket= (or its ticket) with its join.
3. Many games, a game list, matchmaking
import { GameRooms, mapMode } from '@v12-engine/engine/server'
const rooms = await GameRooms.start({
port: 8787,
rooms: { arena: { mode: mapMode(rules, arenaMap, { onStart }) }, duel: { mode: mapMode(rules, duelMap, { onStart }) } }
})Each room is at ws://host:8787/<room>. Add kinds and players can create rooms, browse GET /games and queue for a match; the client's Find a game scene (findGameScene) shows all of it. Ratings: createRatings({ file }).
4. Production
Call server.close({ drain: 30 }) on SIGTERM, put TLS on a proxy with an idle timeout over 60 s, and check /health and /metrics. The nginx block and the Docker image are in Multiplayer: run it in production.
Reference
startServer option | What it does |
|---|---|
map, seats | A seat per map player with a start, or { player, name, team? } |
onStart(world, { map, settings }) | Runs on each new world |
makeWorld(settings) | Each game's world in code, instead of map |
ai, computer | Seats the computer plays and how (a brain, options, or null) |
watch, adminKey, abandonAfter, log | Server options (GameServerOptions) |
Everything else (what a player gets to know, cheating, limits, watching, ranks): Multiplayer and servers.
In the example
apps/example/src/examples/server/main.ts and modes.ts: a melee mode, rooms, a relay for watching, draining on SIGTERM. npm run server -- --mode arena --ai 2 plays the computer; --watch lets others watch.