> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rulesets

> Match rules that run on the server: the interface, determinism requirements, and third-party WASM rulesets

A ruleset defines the match rules for one game and runs in the game service. The platform handles rooms, matchmaking, autoplay, spectating, replays, and stake settlement. The ruleset is responsible only for:

| Responsibility | Description |
| - | - |
| Setup | Creates the initial state from the seats and configuration |
| Legal actions | Lists the actions a seat can take now. Autoplay and timeouts perform the first one |
| Apply action | Returns the new state and events, or rejects an illegal action |
| View | What a seat can see. Hidden information (such as other players' hands) goes only to its owner |
| Result | At the end, gives each seat's placement and score. Stake tables settle zero-sum by score |
| Time limit, turn | The time limit for the current move and whose turn it is |
| Bot (optional) | How a bot makes its move |

A ruleset is identified as "name\@major version", for example `doudizhu@1`. A game declares its ruleset in `realtime.ruleset` in `manifest.json`.

## Determinism

A ruleset must be a pure function: the same state, action, and random numbers produce the same result.

* Use only the random numbers the room provides. The seed is committed publicly before the match and revealed afterward, so the match can be replayed in full.
* Do not read the clock or access the network or files.
* The state must serialize to JSON and read back. The platform uses it for snapshots and cross-instance takeover.

## First-party rulesets

The platform's own games are written in Go and compiled into the game service, for example Dou Dizhu `doudizhu@1` and Gomoku `gomoku@1`.

## Third-party rulesets (WASM)

> [!WARNING]
>
> The third-party ruleset interface is final, and you can develop and test against it. The engine that runs WASM and upload review are not open yet; the upload endpoint currently returns `501 ruleset_upload_reserved`.

A third-party ruleset is a WASM module that exports the following functions, all with JSON input and output:

| Function | Input | Output |
| - | - | - |
| `nl_setup` | `{seats: [{id, name, seat}], config}` | `{state}`, or `{error}` for an invalid configuration |
| `nl_legal` | `{state, seat}` | `{actions: [...]}` |
| `nl_apply` | `{state, seat, action}` | `{state, events: [{type, data?, to?}]}`, or `{error: "reason"}` for an illegal action |
| `nl_view` | `{state, seat}` | `{view}`. A `seat` of `-1` is the spectator view |
| `nl_result` | `{state}` | `{result: null}` or `{result: {ranks, scores, reason?}}` |
| `nl_timeout` | `{state}` | `{ms}`; `0` means no time limit |
| `nl_to_move` | `{state}` | `{seat}`; `-1` means no one is to move |
| `nl_bot` | `{state, seat}` | `{action}` or `{action: null}`. Optional; if not exported, bots perform the first legal action |

`state` is JSON defined by the ruleset, and the platform stores it as is. An event with an empty `to` is visible to everyone (including spectators); otherwise it is sent only to the listed seats. The `error` returned by `nl_apply` is sent back to the client as the failure reason in `action.ack`.

### ABI (version 1)

* Export `memory`, `nl_abi_version() -> i32` (returns `1`), `nl_alloc(size i32) -> i32`, and `nl_free(ptr i32, size i32)`.
* Every rule function has the signature `(ptr i32, len i32) -> i64`. The input JSON is at `[ptr, ptr+len)`; the high 32 bits of the return value are the output pointer and the low 32 bits are the output length. The platform calls `nl_free` after reading it.
* Imports from module `"nl"`:
  * `rng_u32() -> i32`: the room's deterministic random number. Call it only in `nl_setup` and `nl_apply`.
  * `log(ptr i32, len i32)`: debug logging, rate-limited in production.
* WASI is not imported: there is no clock, file system, network, or environment variables.
* Limits: up to 16 MiB of linear memory and up to 50 milliseconds per call. A call that exceeds them fails.

### ruleset.json

```json theme={null}
{ "id": "dev.reversi@1", "min_players": 2, "max_players": 2, "abi": 1 }
```

`id` consists of lowercase letters, digits, dots, and hyphens, and third-party rulesets carry a developer prefix (such as `dev.`). Bump the major version when you change the state format or the rules; rooms in progress keep using the old version. Player count is 1–16.

### Planned publishing flow

1. Upload `module.wasm` (up to 4 MiB) and `ruleset.json`. The response is `{id, sha256, status: "pending_review"}`.
2. Staff review it: they check the source or a reproducible build, verify that the same input produces the same output twice, and run a large number of random matches within the limits.
3. Once approved, the game service loads the module, and `realtime.ruleset` in `manifest.json` and table configurations can use the name.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.