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.
The pattern
Section titled “The pattern”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.
Why it’s needed
Section titled “Why it’s needed”It covers two cases:
- The role-holder drops out mid-game and the role would otherwise vanish until someone re-joined.
- 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.
Variations
Section titled “Variations”- Filtered election. Narrow who can be chosen with
filters(e.g. onlyalive: trueplayers):- kind: assign_state_randomassigns: [{ key: seeker, value: true }]filters: [{ key: state.alive, value: true }] - Notify the chosen player. Add
notifyso they find out:- kind: assign_state_randomassigns: [{ 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.