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:
@@ -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")
|
||||
Reference in New Issue
Block a user