Skip to content

Ensure a role exists

Games with a singleton role — the “it” player, the seeker, the bomb holder — have a failure mode: the player holding the role disconnects, and now nobody has it. This pattern detects the gap and re-elects.

count_state … n: 0 fires while zero players have the role; assign_state_random hands it to a random player.

- id: ensure_it_exists
when:
kind: count_state
who: any
key: it
value: true
n: 0
do:
- { kind: log, message: "no it player; electing one at random" }
- kind: assign_state_random
assigns:
- { key: it, value: true }

Source: apps/wage-engine/src/games/tag/game.yaml.

It covers two cases:

  1. The role-holder drops out mid-game and the role would otherwise vanish until someone re-joined.
  2. Bot-only or seedless sessions where no human ever joined to take the starting role.

In a normal session where the first joiner gets the role via playerJoinStates, this rule simply never fires — n is never 0.

  • Filtered election. Narrow who can be chosen with filters (e.g. only alive: true players):
    - kind: assign_state_random
    assigns: [{ key: seeker, value: true }]
    filters: [{ key: state.alive, value: true }]
  • Notify the chosen player. Add notify so they find out:
    - kind: assign_state_random
    assigns: [{ key: it, value: true }]
    notify: { type: toast, text: "You're it!" }
  • Keep exactly N. This guards the floor (≥ 1). To also cap a role, pair it with a rule that clears extras, or assign the role only through controlled handoffs.