Player

The files that describe a Chess player: the data they supply when they join, the per-instance state the engine keeps for them, and the two scripts the server runs: one the moment they join, one when they leave.

gamehoster-player-join-schema.json

The shape of the data a client must send to join. Chess asks for one thing: a name, the label shown for you at the board.

[
  {
    "gamehoster-player-join-schema-name": "name",
    "gamehoster-player-join-schema-type": "string"
  }
]
chess/gamehoster-player/gamehoster-player-join-schema.json

gamehoster-player-schema.json

The per-instance state carried for each player, all sent to both clients: their name, a ready flag they toggle in the lobby, and the color (white or black) the instance assigns once the game starts.

[
  {
    "gamehoster-player-schema-name": "name",
    "gamehoster-player-schema-type": "string",
    "gamehoster-player-schema-sync": "sent",
    "gamehoster-player-schema-audience": "all"
  },
  {
    "gamehoster-player-schema-name": "ready",
    "gamehoster-player-schema-type": "bool",
    "gamehoster-player-schema-default": false,
    "gamehoster-player-schema-sync": "sent",
    "gamehoster-player-schema-audience": "all"
  },
  {
    "gamehoster-player-schema-name": "color",
    "gamehoster-player-schema-type": "string",
    "gamehoster-player-schema-sync": "sent",
    "gamehoster-player-schema-audience": "all"
  }
]
chess/gamehoster-player/gamehoster-player-schema.json

gamehoster-player-join.js

Runs on the server as a player connects: it routes them into a game still in its lobby with a free seat (or starts a fresh one), then records their name and leaves them not-ready. Colour is left unset here; the instance update hands out white and black once both players are ready.

// Runs on the server the moment a player connects. It first routes them into an
// instance (a game still in its lobby with a free seat, or a fresh one), then
// sets up their per-instance state. Colour is NOT assigned here; the instance
// update hands out white/black by join order once both players are ready.

// route: join a lobby-phase game that still has room, else start a new one
const open = Gamehoster.Instance.List().find(id =>
  Gamehoster.Instance.Info(id).players < Gamehoster.Instance.Info(id).capacity &&
  Gamehoster.Instance.State.Get(id, "phase") === "lobby")

Gamehoster.Instance.Join(open !== undefined ? open : Gamehoster.Instance.Create())

// set up: name from the join data, not yet ready
const me = Gamehoster.Context.playerId
Gamehoster.Player.State.Set(me, "name", Gamehoster.Join.Get("name"))
Gamehoster.Player.State.Set(me, "ready", false)
chess/gamehoster-player/gamehoster-player-join.js

gamehoster-player-leave.js

Runs when a player disconnects. If a match was underway, the leaver forfeits: the opponent is recorded as the winner and the game ends. If they leave during the lobby there is nothing to do, the seat simply reopens and the room keeps waiting.

// A player has left. If a match was in play, award it to the opponent and end
// the game; otherwise there is nothing to do (a lobby simply keeps waiting).

const here = Gamehoster.Context.instanceId
if (Gamehoster.Instance.State.Get(here, "phase") === "playing") {
  const me = Gamehoster.Context.playerId
  const myColor = Gamehoster.Player.State.Get(me, "color")
  Gamehoster.Instance.State.Set(here, "winner", myColor === "white" ? "black" : "white")
  Gamehoster.Instance.State.Set(here, "phase", "over")
}
chess/gamehoster-player/gamehoster-player-leave.js