Run a server and join it
startServer runs one game in Node (npm run server in a starter). A page joins it with the app's server option.
ts
// in server.ts
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,
// the seats the computer plays (`computer` says which brain: the melee brain unless told); left out, the game starts
// once every seat is taken by a person
ai: map.players.slice(1).map((player: { id: number }) => player.id),
port: Number(process.env.PORT ?? 8787)
})
process.once('SIGINT', () => void server.close().then(() => process.exit(0)))ts
// in src/main.ts: the seat at it, among the app's scenes
import { appScenes, sceneAsked, startApp } from '@v12-engine/client/app'
import { game } from './game'
import { maps } from './maps'
import { onStart } from './rules'
const scenes = appScenes({ game, maps, onStart, server: { url: 'ws://localhost:8787' } })
startApp({ target: '#game', title: 'My game', scenes, start: sceneAsked(scenes, 'home') })Done: run npm run server beside npm run dev and open /?server. You take the first seat; the computer plays the rest.
The server runs the world. Each page holds a seat, sends commands, and is told only what its seat may see.
Host many games on one port
--server rooms in npm create, or GameRooms in code. In the page, server: { rooms: true } opens a lobby that finds and makes games.
Reference
| Option | What it does |
|---|---|
ai | Seats the computer plays. Left out, the game waits for every seat. |
computer | Which brain (the melee brain by default). |
mode, seats | The game mode and who sits where (ModeSeat: player, name, team, base). |
server: true (page) | Takes the address from the page: /?server=ws://host:8787. |
joinServer(url, game, name) | A session of your own, for launch or a game scene's session. |
See Online, on a server and Multiplayer.