Skip to content

Hooks

The rules DSL is meant to express a whole game. For the rare rule it can’t, such as a combinatorial search a builder would rather write as code, a rules file can declare a hook: a named piece of C#, run in a sandbox, that the rules call like a function or an effect.

A hook is declared under hooks with a kind and a C# signature: its parameters, each with a DSL type, and its result. void means it returns nothing. Chess declares one, to write a finished game in the standard PGN notation:

hooks:
pgnExport:
kind: notation
signature: "(log: MatchLog) -> string"
Kind What it’s for
notation Not a rule: writing the game’s record out, such as chess’s PGN
effect Effects too intricate for the DSL
condition A test too intricate for an expression
scoring A score that needs a search, such as matching tasks to objective cards
choiceGenerator Generating the options for a choice, such as every way to split a purchase

The rules call an effect hook with call, giving its arguments by name, as Everdell’s proof file does:

- call: { hook: dealForestAndEvents, args: { players: players } }

Hooks are for what the language truly can’t say. Most rules that look like they need code don’t:

  • A long calculation is a function. Functions can call each other, and recursion is allowed, so a search over a few choices, such as which objective cards score best for a player, is a function.
  • A sequence of game steps is a procedure: a named list of effects with parameters.
  • A rule that’s on only sometimes is a variant.

The six games the language was frozen on (chess, 7 Wonders, Great Western Trail, Brass: Birmingham, Dominion and Terraforming Mars) are written without a single rule in a hook. Chess declares the only hook among them, and it’s the PGN export, which isn’t a rule.

When hooks arrive, each will implement a narrow C# interface and see the game only through a read-only view of the state, changing it only by returning effects. Its code may use only the engine’s API and a short list of .NET types: reflection, files, threads, the clock and unsafe code are compile errors. Games with hooks will run in their own isolated processes with CPU and memory limits, so one game’s hook can’t affect another’s matches.