Files
tidewall/test/sim/test_match.gd
T
claudeandClaude Opus 5.5 f795ee3fa5 N6: Joining games: direct connect, LAN and server list
- LAN discovery: a listening server answers QUERY packets on its game port
  plus one (ServerBeacon, off with --no-lan). ServerBrowser scans eight game
  ports from 24650 by broadcast.
- A self-hostable list server: --list-server [--port=N] (ListServer,
  default UDP 24660). Game servers register with --list=HOST[:PORT] and
  --name=NAME; they are listed under their source address only after
  echoing a CHALLENGE token. Entries expire after 45 s, 8 per address,
  256 in all; every source is rate-limited (SourceLimiter).
- NetDiscovery messages: every QUERY is padded to 512 bytes and no answer
  is bigger, so neither service amplifies spoofed requests.
- tidewall --browse [--list=...] [--lan-target=IP] [--lan-port=N] prints
  the servers found as JSON until menus (A10) show them.
- People who connect mid-match or during results watch a living player
  (Match.watch) and take places ahead of bots in the next lobby, instead
  of being told the server is full. Bots fill only the places left when
  the match starts.
- Tests: codec round trips, size limits and a 40,000-message fuzz; list
  server with fake servers (challenge, stolen and wrong tokens, renewal,
  expiry, caps, flood); LAN scan over loopback; watchers over UDP; lobby
  backfill. make discovery-check runs it all as real processes.

Closes #37

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 17:33:32 -05:00

333 lines
10 KiB
GDScript

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.start_tide()
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_false(m.join(3), "nobody joins a match under way")
assert_true(m.watch(3), "a late arrival watches and waits for the next match")
assert_false(m.watch(3), "once")
assert_false(m.watch(1), "a participant isn't a watcher")
assert_false(m.entity_of.has(3))
assert_true(m.spectate_target(3) in [1, 2], "they watch someone still standing")
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")
assert_eq(m.spectate_target(3), 1, "during results they watch the winner")
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])
assert_eq(m.watchers.size(), 0)
_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")
func test_watch_is_only_for_a_match_under_way() -> void:
var m: Match = Match.new(SEED, _rules())
assert_false(m.watch(5), "in the lobby, people join")
m.join(1)
_until(m, Match.COUNTDOWN)
assert_false(m.watch(5), "the countdown still takes people")
assert_true(m.join(5))
func test_bots_backfill_around_people_joining_and_leaving_in_the_lobby() -> void:
var rules: MatchRules = _rules(6)
rules.min_players = 2
rules.lobby_ticks = 50
var m: Match = Match.new(SEED, rules)
m.join(1)
m.join(2)
m.join(3)
_steps(m, 5)
m.leave(2)
m.join(4)
m.leave(1)
m.join(5)
m.join(6)
assert_eq(m.phase, Match.LOBBY)
assert_eq(m.bot_handles.size(), 0, "no bots are added while people come and go")
_until(m, Match.DROP)
var people: Array[int] = []
for handle: int in m.participants:
if not m.bot_handles.has(handle):
people.append(handle)
people.sort()
assert_eq(people, [3, 4, 5, 6] as Array[int], "everyone still here has a place")
assert_eq(m.bot_handles.size(), 2, "bots fill exactly the places left")
assert_eq(m.participants.size(), 6)
for handle: int in people:
assert_true(m.entity_of.has(handle))
func test_watchers_take_places_ahead_of_bots_in_the_next_match() -> void:
var m: Match = Match.new(SEED, _rules(4))
m.join(1)
_until(m, Match.DROP)
assert_eq(m.bot_handles.size(), 3)
for handle: int in [7, 8, 9]:
assert_true(m.watch(handle))
m.leave(8)
assert_false(m.watchers.has(8))
for handle: int in m.participants.duplicate():
if handle != 1:
_eliminate(m, handle)
m.step()
assert_eq(m.phase, Match.RESULTS)
_until(m, Match.LOBBY)
assert_true(_saw(m, "joined"))
assert_eq(m.participants, [1, 7, 9] as Array[int], "watchers take places, bots are gone")
_until(m, Match.DROP)
assert_eq(m.bot_handles.size(), 1, "one bot fills the one empty place")
for handle: int in [1, 7, 9]:
assert_true(m.entity_of.has(handle))