Commands

Snake has a single command, steer, which sets the direction the sending player is holding. It is a folder with its parameter schema, its writes manifest, and the server handler that applies it.

<gamehoster-config-contentRoot>/
  game.gamehoster.org/
    gamehoster-games/
      snake/
        gamehoster-commands/
          steer/
            gamehoster-command-schema.json
            gamehoster-command.json
            gamehoster-command-handler.js
The gamehoster-commands/ folder: one folder per command, each with its schema, manifest and handler.

Contents

One folder for the one command; it holds a parameter schema, a writes manifest and a handler.

NameTypeDescription
gamehoster-command-schema.jsonfileThe shape of a steer command: one field, dir, an int.
gamehoster-command.jsonfileThe writes manifest: steer may write player.held and player.pending.
gamehoster-command-handler.jsfileRuns on the server: set the held direction and queue a turn.

gamehoster-command-schema.json

The shape of a steer command. It has one field, dir, an int.

[
  {
    "gamehoster-command-schema-name": "dir",
    "gamehoster-command-schema-type": "int"
  }
]
snake/gamehoster-commands/steer/gamehoster-command-schema.json

gamehoster-command.json

The writes manifest. steer may write only player.held and player.pending, so the server firewall lets it touch nothing else.

{ "writes": ["player.held", "player.pending"] }
snake/gamehoster-commands/steer/gamehoster-command.json

gamehoster-command-handler.js

Runs on the server when a steer arrives. dir is 0 right, 1 down, 2 left, 3 up, or -1 when no key is held. It sets the player's held direction, and holding the current heading makes the snake move every tick instead of every other. If dir is perpendicular to the current heading it queues it as the next turn. A reversal is ignored, so a snake cannot turn straight back into its own neck. A dead player's steer does nothing.

// Set the sending player's held direction on the authoritative server. `dir` is 0=right, 1=down,
// 2=left, 3=up, or -1 when no direction key is held. Holding the current direction makes the snake
// move every tick instead of every other; a perpendicular direction is queued as the next turn (a
// reversal is ignored, so you cannot crash straight into your own neck).

const me = Gamehoster.Context.playerId
if (!Gamehoster.Player.State.Get(me, "alive")) return

const d = Gamehoster.Command.Get("dir")
Gamehoster.Player.State.Set(me, "held", d)
if (d !== -1) {
  const dir = Gamehoster.Player.State.Get(me, "dir")
  if (d !== dir && d !== (dir + 2) % 4) Gamehoster.Player.State.Set(me, "pending", d)   // valid turn: queue it
}
snake/gamehoster-commands/steer/gamehoster-command-handler.js