M1: Match state machine

Match (src/sim/match.gd) runs a server's matches back to back: lobby,
countdown, drop, play and results, then a fresh island and the lobby again.
It is stepped once per server tick and has no scene-tree or clock dependency.

- MatchRules holds max and min players, team size, the lobby, countdown and
  results timers (defaults in Tuning.MATCH_*) and whether bots fill empty
  places.
- Participants are server-chosen handles. Joining during a match waits for
  the next; leaving during the drop or play places the leaver as eliminated.
- Placements, winners (last team standing) and events for phase changes,
  joins, leaves, places, match start and end.
- Match 0 uses the server's seed; later matches derive their seed from it and
  their index. The next match's Simulation is built when the lobby opens.
- ServerLoop now drives a Match (immediate rules by default, so the smoke
  match is unchanged: same state hash), rebuilds bots each match, and counts
  server ticks across matches. The headless server logs match events.

Tests: test_match (13) covers every transition, leaving in each phase, an
empty lobby, an empty match, teams, late joiners, and ten consecutive matches
that each start identical to a freshly built simulation. A ServerLoop test
checks the server goes back to the lobby and starts the next match.

Closes #24

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 20:24:00 -05:00
co-authored by Claude Opus 5.5
parent 0013a81658
commit c6f3c19004
11 changed files with 649 additions and 23 deletions
+17
View File
@@ -40,6 +40,23 @@ name, so it can't drift from the game. To change a number, edit that file.
| Solo | 24 players | `SOLO_PLAYERS` | | Solo | 24 players | `SOLO_PLAYERS` |
| Duos | 12 teams of 2 | `DUO_TEAMS`, `DUO_TEAM_SIZE` | | Duos | 12 teams of 2 | `DUO_TEAMS`, `DUO_TEAM_SIZE` |
### Match flow
The server runs matches back to back, in [`src/sim/match.gd`](../src/sim/match.gd):
1. **Lobby.** Players join. Once `MATCH_MIN_PLAYERS` are in, it waits
`MATCH_LOBBY_SECONDS` for more, or counts down at once when full.
2. **Countdown.** `MATCH_COUNTDOWN_SECONDS`. If too many leave, it goes back to the lobby.
3. **Drop.** Bots fill the empty places, everyone boards the barge and loot is placed.
4. **Play.** Ends when at most one team has anyone standing.
5. **Results.** Finishing places and the winners stay up for `MATCH_RESULTS_SECONDS`,
then bots leave and the server goes back to the lobby with a new island.
Leaving during the drop or play counts as being eliminated where you stood. Joining
during a match waits in the lobby for the next one. Every timer can be changed per
server through `MatchRules`. Match 0 uses the server's seed; later matches derive theirs
from it, so a whole session can be replayed from one seed.
Bots fill empty slots. Offline play is a local server filled with bots, the same game Bots fill empty slots. Offline play is a local server filled with bots, the same game
code as online play. A downed duo partner can be revived, within `REVIVE_SECONDS`. code as online play. A downed duo partner can be revived, within `REVIVE_SECONDS`.
+10 -6
View File
@@ -44,6 +44,7 @@ func _ready() -> void:
started["engine"] = Engine.get_version_info()["string"] started["engine"] = Engine.get_version_info()["string"]
started["tick_rate_hz"] = Tuning.TICK_RATE_HZ started["tick_rate_hz"] = Tuning.TICK_RATE_HZ
started["world_hash"] = loop.sim.island.heightmap_hash() started["world_hash"] = loop.sim.island.heightmap_hash()
started["match_phase"] = loop.match_flow.phase_name()
started["listening"] = false started["listening"] = false
started["note"] = "networking arrives in v0.5; players join as idle slots until then" started["note"] = "networking arrives in v0.5; players join as idle slots until then"
_log("start", started) _log("start", started)
@@ -61,10 +62,10 @@ func _process(delta: float) -> void:
# Too far behind to catch up: drop the backlog rather than spiral. # Too far behind to catch up: drop the backlog rather than spiral.
_accumulator = minf(_accumulator, 1.0 / Tuning.TICK_RATE_HZ) _accumulator = minf(_accumulator, 1.0 / Tuning.TICK_RATE_HZ)
for _index: int in due: for _index: int in due:
if config.ticks > 0 and loop.sim.tick >= config.ticks: if config.ticks > 0 and loop.ticks >= config.ticks:
break break
_tick() _tick()
if config.ticks > 0 and loop.sim.tick >= config.ticks: if config.ticks > 0 and loop.ticks >= config.ticks:
_finish(0) _finish(0)
@@ -75,12 +76,15 @@ func _exit_tree() -> void:
func _tick() -> void: func _tick() -> void:
if config.fault == "script-error" and loop.sim.tick == FAULT_TICK: if config.fault == "script-error" and loop.ticks == FAULT_TICK:
_deliberate_script_error() _deliberate_script_error()
loop.run_tick() loop.run_tick()
if config.log_every > 0 and loop.sim.tick % config.log_every == 0: for event: Dictionary in loop.match_flow.events:
if event["type"] in ["phase", "match_started", "match_ended"]:
_log("match", event)
if config.log_every > 0 and loop.ticks % config.log_every == 0:
var stats: Dictionary = loop.timing_stats() var stats: Dictionary = loop.timing_stats()
_log("progress", {"tick": loop.sim.tick, "p95_us": stats["p95_us"], "max_us": stats["max_us"]}) _log("progress", {"tick": loop.ticks, "p95_us": stats["p95_us"], "max_us": stats["max_us"]})
func _finish(requested_code: int) -> void: func _finish(requested_code: int) -> void:
@@ -113,7 +117,7 @@ func _log(event: String, fields: Dictionary) -> void:
"seed": config.match_seed, "seed": config.match_seed,
} }
if loop != null: if loop != null:
record["tick"] = loop.sim.tick record["tick"] = loop.ticks
record.merge(fields, false) record.merge(fields, false)
print(JSON.stringify(record)) print(JSON.stringify(record))
+51 -17
View File
@@ -1,5 +1,5 @@
## Drives one match's simulation for a server: adds players and bots, gathers their ## Drives a server's matches: joins players, fills each match with bots, gathers their
## commands each tick, steps the simulation and times every tick. ## commands each tick, steps the match flow and times every tick.
## It has no scene-tree dependency, so tests can run it directly. ## It has no scene-tree dependency, so tests can run it directly.
class_name ServerLoop class_name ServerLoop
extends RefCounted extends RefCounted
@@ -7,35 +7,69 @@ extends RefCounted
## The server-tick budget from docs/ARCHITECTURE.md, in microseconds. ## The server-tick budget from docs/ARCHITECTURE.md, in microseconds.
const TICK_BUDGET_USEC: int = 12_000 const TICK_BUDGET_USEC: int = 12_000
var sim: Simulation ## The match flow. Its `sim` is the current match's simulation.
var match_flow: Match
var sim: Simulation:
get:
return match_flow.sim
## Entity IDs of everyone in the current match, humans first.
var player_ids: Array[int] = [] var player_ids: Array[int] = []
var bots: Array[WanderBot] = [] var bots: Array[WanderBot] = []
## Wall-clock duration of every tick run, in microseconds. ## Wall-clock duration of every tick run, in microseconds.
var tick_usec: PackedInt64Array = PackedInt64Array() var tick_usec: PackedInt64Array = PackedInt64Array()
## Server ticks run, across every match.
var ticks: int = 0
func _init(match_seed: int, human_players: int, bot_count: int) -> void: ## `human_players` join as idle participants with handles 1 and up; `bot_count` bots
sim = Simulation.new(match_seed) ## fill the rest of every match. With `rules` null the match starts at once, and the
for _index: int in human_players: ## server goes back to the lobby after each match's results and starts another.
player_ids.append(sim.add_player()) func _init(match_seed: int, human_players: int, bot_count: int, rules: MatchRules = null) -> void:
for _index: int in bot_count: if rules == null:
var id: int = sim.add_player() rules = MatchRules.immediate()
player_ids.append(id) rules.max_players = maxi(human_players + bot_count, 1)
bots.append(WanderBot.new(id)) rules.fill_with_bots = bot_count > 0
# Every match starts on the sky barge. # With only bots there's nobody to wait for, so the lobby starts with no one in it.
sim.start_drop() rules.min_players = 0 if human_players == 0 and bot_count > 0 else 1
sim.place_loot() match_flow = Match.new(match_seed, rules)
for handle: int in range(1, human_players + 1):
match_flow.join(handle)
if rules.lobby_ticks == 0 and rules.countdown_ticks == 0:
# Every match starts on the sky barge, on the first tick.
_advance()
## Runs one tick and records how long it took. ## Runs one tick and records how long it took.
func run_tick() -> void: func run_tick() -> void:
var started: int = Time.get_ticks_usec() var started: int = Time.get_ticks_usec()
for bot: WanderBot in bots: if match_flow.in_match():
sim.submit_command(bot.player_id, bot.think(sim)) for bot: WanderBot in bots:
sim.step() sim.submit_command(bot.player_id, bot.think(sim))
_advance()
ticks += 1
tick_usec.append(Time.get_ticks_usec() - started) tick_usec.append(Time.get_ticks_usec() - started)
func _advance() -> void:
match_flow.step()
for event: Dictionary in match_flow.events:
if event["type"] == "match_started":
_track_players()
elif event["type"] == "phase" and event["to"] == "lobby":
player_ids.clear()
bots.clear()
func _track_players() -> void:
player_ids.clear()
bots.clear()
for handle: int in match_flow.participants:
var entity_id: int = match_flow.entity_of[handle]
player_ids.append(entity_id)
if match_flow.bot_handles.has(handle):
bots.append(WanderBot.new(entity_id))
## Timing statistics over every tick run so far, in microseconds. ## Timing statistics over every tick run so far, in microseconds.
func timing_stats() -> Dictionary: func timing_stats() -> Dictionary:
var count: int = tick_usec.size() var count: int = tick_usec.size()
+246
View File
@@ -0,0 +1,246 @@
## A server's match flow: lobby, countdown, drop, play and results, then back to the
## lobby for a new match on a new island. Everything is driven by step(), one call per
## server tick, so it is deterministic and has no scene-tree or clock dependency.
##
## Participants are identified by handles the server chooses (a connection, a local
## player or a bot). Joining and leaving can happen in any phase: a join during a match
## waits in the lobby for the next one, and leaving during a match counts as being
## eliminated where they stood. Each match gets a fresh Simulation, built while the
## lobby waits, so nothing carries over from the last one.
class_name Match
extends RefCounted
const LOBBY: int = 0
const COUNTDOWN: int = 1
const DROP: int = 2
const PLAY: int = 3
const RESULTS: int = 4
const PHASE_NAMES: Array[String] = ["lobby", "countdown", "drop", "play", "results"]
## Bots added to fill a match get handles counting down from here, so they never clash
## with a server's own handles, which are positive.
const FIRST_BOT_HANDLE: int = -1
var rules: MatchRules
## The seed the server was started with. Match 0 uses it as is; later matches derive
## their own from it and their index.
var session_seed: int = 0
## How many matches have started. The current or next match's index is this, or this
## minus one once it has started.
var matches_started: int = 0
var phase: int = LOBBY
## Ticks spent in the current phase.
var phase_ticks: int = 0
## The current match's simulation during the drop, play and results. In the lobby and
## countdown, the next match's simulation, generated and waiting for players.
var sim: Simulation
## Participant handles in join order, and which of them are bots.
var participants: Array[int] = []
var bot_handles: Dictionary = {}
## Handles playing in the current match, and their entity IDs both ways.
var entity_of: Dictionary = {}
var handle_of: Dictionary = {}
## Handle -> team index, for the current match.
var team_of: Dictionary = {}
## Finishing order of the current match, last place first: {"handle", "place", "by",
## "left"}. "by" is the eliminating handle, or 0.
var placements: Array[Dictionary] = []
## The winning handles, empty for no winner (everyone left, or no one played).
var winners: Array[int] = []
## What happened during the last step(): phase changes, joins, leaves, placements.
var events: Array[Dictionary] = []
var _next_bot_handle: int = FIRST_BOT_HANDLE
## How many started the current match.
var _match_size: int = 0
func _init(seed_value: int, match_rules: MatchRules = null) -> void:
session_seed = seed_value
rules = match_rules if match_rules != null else MatchRules.new()
sim = Simulation.new(match_seed(0))
## The seed of match `index`: the session seed for the first, then a hash of it and the
## index, kept non-negative.
func match_seed(index: int) -> int:
if index == 0:
return session_seed
return (session_seed ^ SimRng._hash_name("match:%d" % index)) & 0x7FFFFFFFFFFFFFFF
## The current or next match's seed.
func current_seed() -> int:
return sim.rng.match_seed
func phase_name() -> String:
return PHASE_NAMES[phase]
func in_match() -> bool:
return phase == DROP or phase == PLAY
## Adds a participant. Returns false if the handle is already here or the lobby is
## full. During a match they wait for the next one.
func join(handle: int, bot: bool = false) -> bool:
if participants.has(handle) or participants.size() >= rules.max_players:
return false
participants.append(handle)
if bot:
bot_handles[handle] = true
events.append({"type": "joined", "handle": handle, "bot": bot})
return true
## Removes a participant. In a match, they're placed as if eliminated and their player
## leaves the simulation. Returns false if the handle isn't here.
func leave(handle: int) -> bool:
if not participants.has(handle):
return false
participants.erase(handle)
bot_handles.erase(handle)
events.append({"type": "left", "handle": handle})
if entity_of.has(handle):
var entity_id: int = entity_of[handle]
var player: SimEntity = sim.entities.find(entity_id)
if in_match() and player != null and not player.eliminated:
_place(handle, 0, true)
sim.remove_player(entity_id)
handle_of.erase(entity_id)
entity_of.erase(handle)
return true
## Passes a participant's command to the simulation. Returns false outside a match, for
## a handle that isn't playing, or if the simulation rejected it.
func submit_command(handle: int, command: PlayerCommand) -> bool:
if not in_match() or not entity_of.has(handle):
return false
return sim.submit_command(entity_of[handle], command)
## Handles still in the match and not eliminated, in join order.
func alive() -> Array[int]:
var living: Array[int] = []
if not in_match() and phase != RESULTS:
return living
for handle: int in entity_of:
var player: SimEntity = sim.entities.find(entity_of[handle])
if player != null and not player.eliminated:
living.append(handle)
return living
## Advances one server tick.
func step() -> void:
events.clear()
phase_ticks += 1
match phase:
LOBBY:
if participants.size() < rules.min_players:
phase_ticks = 0
elif participants.size() >= rules.max_players or phase_ticks >= rules.lobby_ticks:
_enter(COUNTDOWN)
if rules.countdown_ticks <= 0:
_start()
COUNTDOWN:
if participants.size() < rules.min_players:
_enter(LOBBY)
elif phase_ticks >= rules.countdown_ticks:
_start()
DROP, PLAY:
sim.step()
_collect_eliminations()
if phase == DROP and sim.drop.finished:
_enter(PLAY)
_check_finished()
RESULTS:
if phase_ticks >= rules.results_ticks:
_reset()
func _enter(next: int) -> void:
events.append({"type": "phase", "from": PHASE_NAMES[phase], "to": PHASE_NAMES[next], "match": current_seed()})
phase = next
phase_ticks = 0
## Fills empty places with bots, puts every participant on the barge and places loot.
func _start() -> void:
if rules.fill_with_bots:
while participants.size() < rules.max_players:
var handle: int = _next_bot_handle
_next_bot_handle -= 1
join(handle, true)
var size: int = maxi(rules.team_size, 1)
for index: int in participants.size():
var handle: int = participants[index]
var entity_id: int = sim.add_player()
entity_of[handle] = entity_id
handle_of[entity_id] = handle
team_of[handle] = index / size
_match_size = participants.size()
sim.start_drop()
sim.place_loot()
matches_started += 1
_enter(DROP)
events.append({"type": "match_started", "match": current_seed(), "index": matches_started - 1, "players": participants.size()})
_check_finished()
func _collect_eliminations() -> void:
for event: Dictionary in sim.events:
if event["type"] != "eliminated" or not handle_of.has(event["player"]):
continue
var by: int = int(handle_of.get(event["by"], 0))
_place(handle_of[event["player"]], by, false)
## Records a participant's finishing place: one more than the players left after them.
func _place(handle: int, by: int, left: bool) -> void:
for placed: Dictionary in placements:
if placed["handle"] == handle:
return
var place: int = _match_size - placements.size()
var record: Dictionary = {"handle": handle, "place": place, "by": by, "left": left}
placements.append(record)
var event: Dictionary = record.duplicate()
event["type"] = "placed"
events.append(event)
## Ends the match when at most one team has anyone standing.
func _check_finished() -> void:
if not in_match():
return
var teams: Dictionary = {}
for handle: int in alive():
teams[team_of[handle]] = true
if teams.size() > 1:
return
winners.clear()
for handle: int in alive():
winners.append(handle)
# Winners take first place, after everyone eliminated.
for handle: int in winners:
placements.append({"handle": handle, "place": 1, "by": 0, "left": false})
_enter(RESULTS)
events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate()})
## Back to the lobby: bots leave, the match's bookkeeping is cleared, and the next
## match's island is generated.
func _reset() -> void:
for handle: int in bot_handles.keys():
participants.erase(handle)
bot_handles.clear()
entity_of.clear()
handle_of.clear()
team_of.clear()
placements.clear()
winners.clear()
_next_bot_handle = FIRST_BOT_HANDLE
_match_size = 0
sim = Simulation.new(match_seed(matches_started))
_enter(LOBBY)
+1
View File
@@ -0,0 +1 @@
uid://dgeu4ua1a0qgm
+26
View File
@@ -0,0 +1,26 @@
## How a server runs its matches: lobby size, timers and team size. Defaults come from
## Tuning; a server or test changes them per instance.
class_name MatchRules
extends RefCounted
## The most participants in one match, bots included.
var max_players: int = Tuning.SOLO_PLAYERS
## The lobby counts down once this many participants have joined.
var min_players: int = Tuning.MATCH_MIN_PLAYERS
## Players on a team. 1 is solo.
var team_size: int = 1
## With enough players, the lobby waits this long for more before counting down. A
## full lobby counts down at once.
var lobby_ticks: int = roundi(Tuning.MATCH_LOBBY_SECONDS * Tuning.TICK_RATE_HZ)
var countdown_ticks: int = roundi(Tuning.MATCH_COUNTDOWN_SECONDS * Tuning.TICK_RATE_HZ)
var results_ticks: int = roundi(Tuning.MATCH_RESULTS_SECONDS * Tuning.TICK_RATE_HZ)
## Fill empty places with bots when the match starts.
var fill_with_bots: bool = true
## Rules for a server that skips the lobby: the match starts on the first tick.
static func immediate() -> MatchRules:
var rules: MatchRules = MatchRules.new()
rules.lobby_ticks = 0
rules.countdown_ticks = 0
return rules
+1
View File
@@ -0,0 +1 @@
uid://c8liu74vanogc
+9
View File
@@ -16,6 +16,15 @@ const DUO_TEAMS: int = 12
const DUO_TEAM_SIZE: int = 2 const DUO_TEAM_SIZE: int = 2
const REVIVE_SECONDS: float = 6.0 const REVIVE_SECONDS: float = 6.0
# Match flow, in seconds. See Match and MatchRules, which can override each one.
## The lobby counts down once this many participants have joined.
const MATCH_MIN_PLAYERS: int = 1
## With enough players, the lobby waits this long for more before counting down.
const MATCH_LOBBY_SECONDS: float = 20.0
const MATCH_COUNTDOWN_SECONDS: float = 10.0
## Results stay up this long before the server returns to the lobby.
const MATCH_RESULTS_SECONDS: float = 15.0
# Player # Player
const PLAYER_MAX_HEALTH: int = 100 const PLAYER_MAX_HEALTH: int = 100
const PLAYER_MAX_ARMOR: int = 100 const PLAYER_MAX_ARMOR: int = 100
+23
View File
@@ -66,3 +66,26 @@ func test_same_seed_gives_the_same_match() -> void:
var second: String = _run(2026, 2, 22, 300).sim.state_hash() var second: String = _run(2026, 2, 22, 300).sim.state_hash()
assert_eq(first, second) assert_eq(first, second)
assert_ne(first, _run(2027, 2, 22, 300).sim.state_hash()) assert_ne(first, _run(2027, 2, 22, 300).sim.state_hash())
func test_server_returns_to_the_lobby_and_starts_the_next_match() -> void:
var rules: MatchRules = MatchRules.immediate()
rules.max_players = 3
rules.min_players = 0
rules.results_ticks = 3
var loop: ServerLoop = ServerLoop.new(5, 0, 3, rules)
assert_eq(loop.match_flow.phase, Match.DROP)
assert_eq(loop.bots.size(), 3)
var first_seed: int = loop.match_flow.current_seed()
for bot: WanderBot in loop.bots.slice(1):
loop.sim.entities.find(bot.player_id).health = 0
loop.run_tick()
assert_eq(loop.match_flow.phase, Match.RESULTS)
for _index: int in 4:
loop.run_tick()
assert_eq(loop.match_flow.phase, Match.DROP, "lobby and countdown pass at once")
assert_eq(loop.match_flow.matches_started, 2)
assert_ne(loop.match_flow.current_seed(), first_seed)
assert_eq(loop.bots.size(), 3, "bots are rebuilt for the new match")
assert_eq(loop.player_ids, [1, 2, 3] as Array[int])
assert_eq(loop.ticks, 5)
+264
View File
@@ -0,0 +1,264 @@
extends TestCase
## The match state machine from issue M1: lobby, countdown, drop, play and results,
## players leaving at every stage, empty matches, and ten matches in a row with no state
## carried between them.
const SEED: int = 4242
## Short timers so tests run quickly: lobby 3 ticks, countdown 2, results 2.
func _rules(max_players: int = 4, bots: bool = true) -> MatchRules:
var rules: MatchRules = MatchRules.new()
rules.max_players = max_players
rules.min_players = 1
rules.lobby_ticks = 3
rules.countdown_ticks = 2
rules.results_ticks = 2
rules.fill_with_bots = bots
return rules
func _steps(m: Match, count: int) -> void:
for _index: int in count:
m.step()
## Steps until the phase changes to `wanted`, or fails after `limit` ticks.
func _until(m: Match, wanted: int, limit: int = 5000) -> void:
for _index: int in limit:
if m.phase == wanted:
return
m.step()
fail("never reached %s; stuck in %s" % [Match.PHASE_NAMES[wanted], m.phase_name()])
## Knocks a participant out as a weapon would: health to 0, noticed on the next step.
func _eliminate(m: Match, handle: int) -> void:
m.sim.entities.find(m.entity_of[handle]).health = 0
func _saw(m: Match, type: String) -> bool:
for event: Dictionary in m.events:
if event["type"] == type:
return true
return false
## The state a fresh simulation for match `index` should have just after it starts.
func _reference_hash(m: Match, index: int, players: int) -> String:
var sim: Simulation = Simulation.new(m.match_seed(index))
for _index: int in players:
sim.add_player()
sim.start_drop()
sim.place_loot()
return sim.state_hash()
func test_an_empty_lobby_waits_forever() -> void:
var m: Match = Match.new(SEED, _rules())
_steps(m, 50)
assert_eq(m.phase, Match.LOBBY)
assert_eq(m.matches_started, 0)
assert_eq(m.sim.entities.count(), 0, "nothing is spawned in the lobby")
func test_lobby_counts_down_after_waiting_then_starts_a_full_match() -> void:
var m: Match = Match.new(SEED, _rules())
assert_true(m.join(10))
_steps(m, 2)
assert_eq(m.phase, Match.LOBBY, "waiting for more players")
m.step()
assert_eq(m.phase, Match.COUNTDOWN)
assert_true(_saw(m, "phase"))
m.step()
assert_eq(m.phase, Match.COUNTDOWN)
m.step()
assert_eq(m.phase, Match.DROP)
assert_true(_saw(m, "match_started"))
assert_eq(m.participants.size(), 4, "bots filled the empty places")
assert_eq(m.bot_handles.size(), 3)
assert_eq(m.participants[0], 10, "the human keeps the first place in the roster")
for handle: int in m.participants:
var player: SimEntity = m.sim.entities.find(m.entity_of[handle])
assert_eq(player.drop_state, Drop.ABOARD, "everyone starts on the barge")
assert_true(m.sim.pickups.size() > 0, "loot was placed")
assert_eq(m.sim.tick, 0)
func test_a_full_lobby_counts_down_at_once() -> void:
var m: Match = Match.new(SEED, _rules(2))
m.join(1)
m.join(2)
assert_false(m.join(3), "the lobby is full")
assert_false(m.join(1), "already joined")
m.step()
assert_eq(m.phase, Match.COUNTDOWN)
func test_leaving_in_the_lobby_and_countdown_returns_to_waiting() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
_steps(m, 2)
m.leave(1)
m.step()
assert_eq(m.phase_ticks, 0, "the lobby timer resets with nobody waiting")
m.join(1)
_until(m, Match.COUNTDOWN)
assert_true(m.leave(1))
assert_false(m.leave(1), "already gone")
m.step()
assert_eq(m.phase, Match.LOBBY, "the countdown stops when too few remain")
assert_eq(m.matches_started, 0)
func test_the_drop_becomes_play_when_the_barge_finishes() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
_until(m, Match.DROP)
_until(m, Match.PLAY)
assert_true(m.sim.drop.finished)
for handle: int in m.participants:
assert_ne(m.sim.entities.find(m.entity_of[handle]).drop_state, Drop.ABOARD)
assert_eq(m.alive().size(), 4)
func test_last_one_standing_wins_and_places_are_recorded() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
_until(m, Match.DROP)
var bots: Array[int] = []
for handle: int in m.participants:
if handle != 1:
bots.append(handle)
_eliminate(m, bots[0])
m.step()
assert_true(_saw(m, "placed"))
_eliminate(m, bots[1])
_eliminate(m, bots[2])
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_true(_saw(m, "match_ended"))
assert_eq(m.winners, [1] as Array[int])
var places: Dictionary = {}
for record: Dictionary in m.placements:
places[record["handle"]] = record["place"]
assert_eq(places[bots[0]], 4)
assert_eq(places[1], 1)
assert_eq(places.size(), 4, "everyone has a place")
assert_true(places[bots[1]] in [2, 3] and places[bots[2]] in [2, 3])
func test_leaving_during_the_drop_and_play_counts_as_elimination() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
m.join(2)
_until(m, Match.DROP)
var entity: int = m.entity_of[2]
assert_true(m.leave(2))
assert_null(m.sim.entities.find(entity), "the leaver's player is gone")
assert_eq(m.placements[0]["handle"], 2)
assert_eq(m.placements[0]["place"], 4)
assert_true(m.placements[0]["left"])
_until(m, Match.PLAY)
m.leave(1)
assert_eq(m.placements[1]["place"], 3)
m.step()
assert_eq(m.phase, Match.PLAY, "two bots still fight")
assert_false(m.submit_command(1, PlayerCommand.new()), "a leaver can't send commands")
func test_everyone_leaving_ends_the_match_with_no_winner() -> void:
var m: Match = Match.new(SEED, _rules(2, false))
m.join(1)
m.join(2)
_until(m, Match.DROP)
m.leave(1)
m.leave(2)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners.size(), 0)
_until(m, Match.LOBBY)
assert_eq(m.participants.size(), 0)
func test_an_empty_match_ends_at_once_and_returns_to_the_lobby() -> void:
var rules: MatchRules = _rules(4, false)
rules.min_players = 0
var m: Match = Match.new(SEED, rules)
_until(m, Match.RESULTS, 20)
assert_eq(m.matches_started, 1)
assert_eq(m.winners.size(), 0)
assert_eq(m.placements.size(), 0)
_until(m, Match.LOBBY, 10)
func test_leaving_during_results_and_joining_during_a_match() -> void:
var m: Match = Match.new(SEED, _rules(3, false))
m.join(1)
m.join(2)
_until(m, Match.DROP)
assert_true(m.join(3), "a late joiner waits for the next match")
assert_false(m.entity_of.has(3))
assert_false(m.submit_command(3, PlayerCommand.new()))
_eliminate(m, 2)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [1] as Array[int], "the late joiner isn't in this match")
m.leave(1)
assert_eq(m.winners, [1] as Array[int], "leaving during results keeps the result")
_until(m, Match.LOBBY)
assert_eq(m.participants, [2, 3] as Array[int])
_until(m, Match.DROP)
assert_true(m.entity_of.has(3), "the late joiner plays the next match")
func test_teams_win_together() -> void:
var rules: MatchRules = _rules(4, false)
rules.team_size = 2
var m: Match = Match.new(SEED, rules)
for handle: int in [1, 2, 3, 4]:
m.join(handle)
_until(m, Match.DROP)
assert_eq(m.team_of[1], m.team_of[2])
assert_ne(m.team_of[2], m.team_of[3])
_eliminate(m, 3)
m.step()
assert_eq(m.phase, Match.DROP, "team 2 still has a player")
_eliminate(m, 1)
_eliminate(m, 4)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [2] as Array[int])
func test_ten_matches_in_a_row_start_clean() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(7)
var seeds: Dictionary = {}
for index: int in 10:
_until(m, Match.DROP)
assert_eq(m.matches_started, index + 1)
assert_eq(m.sim.tick, 0, "match %d starts at tick 0" % index)
assert_eq(m.participants.size(), 4)
assert_eq(m.placements.size(), 0, "match %d starts with no placements" % index)
assert_eq(m.winners.size(), 0)
assert_eq(m.sim.entities.count(), 4 + m.sim.pickups.size(), "only this match's players and loot")
assert_eq(m.sim.state_hash(), _reference_hash(m, index, 4), "match %d is identical to a fresh one" % index)
seeds[m.current_seed()] = true
var player_ids: Array = m.handle_of.keys()
player_ids.sort()
assert_eq(player_ids, [1, 2, 3, 4], "entity IDs start again from 1")
# Play a little, then end it.
_steps(m, 5)
for handle: int in m.participants:
if handle != 7:
_eliminate(m, handle)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [7] as Array[int])
_until(m, Match.LOBBY)
assert_eq(m.participants, [7] as Array[int], "bots leave after each match")
assert_eq(m.entity_of.size(), 0)
assert_eq(seeds.size(), 10, "every match gets its own island")
assert_eq(m.match_seed(0), SEED, "the first match uses the server's seed")
+1
View File
@@ -0,0 +1 @@
uid://djnmjalwl32eb