Library

Teams and lock

Team numbering, moving players and locking teams with lockTeams().

Team numbering

NumberTeamConstant
0SpectatorTEAM_SPEC
1FFA (free for all)TEAM_FFA
2RedTEAM_RED
3BlueTEAM_BLUE
4GreenTEAM_GREEN
5YellowTEAM_YELLOW
ts
import { TEAM_BLUE, TEAM_SPEC } from 'bonktools';

room.setTeam(playerId, TEAM_BLUE);
room.setTeam(playerId, TEAM_SPEC);

Moving players

The host moves any player with setTeam(id, team). The bot moves itself with joinTeam(team).

ts
room.on('player-join', (p) => {
  // sends every newcomer to spectators
  room.setTeam(p.id, TEAM_SPEC);
});

Locking the teams

ts
room.lockTeams();      // players can no longer switch teams or leave to spectate
room.unlockTeams();    // unlocks again
room.state.teamsLocked; // current state

With the teams locked, only the host moves players: room.setTeam(id, team) keeps working normally. This prevents someone from joining a team on their own and unbalancing the match.

How it works

  • Only the host can lock. Called by a client that isn't the host, lockTeams() is ignored and logs a warning.
  • Local state. room.state.teamsLocked is updated when locking and also when the server reports it (teamlock-toggle).
  • Late joiners already receive the room locked.
  • Room rebuild. If the room goes down and is recreated, the lock is reapplied automatically (unless you called unlockTeams() before).
  • No bursts. Calling lockTeams() several times sends the packet only once. If the server refuses because of too many requests (rate_limit_tl), the library undoes the local state and retries after 1.5 s.

setTeamLock(boolean) is still available; lockTeams() and unlockTeams() are shortcuts.

Team mode

room.setTeamsEnabled(true) turns team mode on. Without it, everyone plays FFA. In football, always use teams.