extends TestCase ## Issue M8: a seed and a command log reproduce a match. ## Plays a small match to its end with one scripted player, recording it. func _recorded(seed_value: int, bots: int, rules: MatchRules = null) -> ServerLoop: var loop: ServerLoop = ServerLoop.new(seed_value, 1, bots, rules) loop.play_humans_by_script() loop.record_commands() var limit: int = BotBatch.tick_limit() + 2000 while loop.ticks < limit and loop.recorder.final_hash.is_empty(): loop.run_tick() return loop func test_a_replay_ends_in_the_same_state() -> void: var loop: ServerLoop = _recorded(41, 5) var commands: CommandLog = loop.recorder assert_false(commands.final_hash.is_empty(), "the match ended") assert_eq(commands.winners.size(), 1) assert_true(commands.count() >= commands.final_tick * 6, "every seat's command is logged every tick") # Through bytes, as a saved file would be. var replay: MatchReplay = MatchReplay.new(CommandLog.from_bytes(commands.to_bytes())) assert_true(replay.run_to_end()) var verdict: Dictionary = replay.verify() assert_true(verdict["ok"], "replay %s" % verdict) assert_eq(replay.sim.tick, commands.final_tick) assert_eq(replay.flow.winners, commands.winners) assert_false(replay.step(), "a finished replay doesn't step") func test_a_replay_runs_the_lobby_and_countdown_too() -> void: var rules: MatchRules = MatchRules.new() rules.max_players = 4 rules.lobby_ticks = 20 rules.countdown_ticks = 10 var loop: ServerLoop = _recorded(42, 3, rules) var replay: MatchReplay = MatchReplay.new(loop.recorder) assert_eq(replay.flow.phase, Match.LOBBY) assert_true(replay.run_until(func() -> bool: return replay.flow.in_match())) assert_eq(replay.ticks, 30) assert_eq(replay.sim.tick, 0) assert_true(replay.run_to_end()) assert_true(replay.verify()["ok"]) func test_a_replay_can_stop_partway() -> void: var loop: ServerLoop = _recorded(41, 5) var again: ServerLoop = ServerLoop.new(41, 1, 5) again.play_humans_by_script() for _index: int in 400: again.run_tick() var replay: MatchReplay = MatchReplay.new(loop.recorder) assert_true(replay.run_until(func() -> bool: return replay.sim.tick >= 400)) assert_eq(replay.sim.tick, 400) assert_eq(replay.sim.state_hash(), again.sim.state_hash(), "the replay passes through the same states") func test_different_commands_give_a_different_match() -> void: var loop: ServerLoop = _recorded(41, 5) var silent: CommandLog = CommandLog.new(41, 1, loop.recorder.rules) silent.final_tick = loop.recorder.final_tick silent.final_hash = loop.recorder.final_hash silent.winners = loop.recorder.winners var replay: MatchReplay = MatchReplay.new(silent) replay.run_to_end() assert_false(replay.verify()["ok"], "nobody moving isn't the same match") func test_bots_leave_the_simulations_random_streams_alone() -> void: # A bot is a player: thinking must not change the simulation, or a replay with no # bots thinking could never reach the same state. var loop: ServerLoop = ServerLoop.new(9, 0, 8) for _index: int in 900: loop.run_tick() assert_false(loop.sim.rng.stream_names().has("bots")) func test_only_the_first_match_is_recorded() -> void: var rules: MatchRules = ServerLoop.immediate_rules(0, 3) rules.results_ticks = 2 var loop: ServerLoop = ServerLoop.new(5, 0, 3, rules) var commands: CommandLog = loop.record_commands() for bot: BotBrain in loop.bots.slice(1): loop.sim.entities.find(bot.player_id).health = 0 loop.run_tick() assert_false(commands.final_hash.is_empty()) var count: int = commands.count() for _index: int in 30: loop.run_tick() assert_eq(loop.match_flow.matches_started, 2) assert_eq(commands.count(), count, "the second match isn't added to the log")