Files
tidewall/test/server/test_command_log.gd
T
claudeandClaude Opus 5.5 1d6a24ab64 M8: Complete offline match
`make match` plays one whole match from the lobby to the results and back to the
lobby: one scripted player and 23 bots on a local server, with no script errors.

- CommandLog records the seed, the rules and every command the server is given.
  MatchReplay plays a log again with nobody thinking and ends on the same state
  hash; `make match` saves the log, reads it back and checks that.
- ServerLoop.submit() is now the one way commands reach the simulation, so a
  recording server sees them all.
- ScriptedPlayer plays a human seat: it drops, loots, fights and flees as a bot
  does, and also cuts driftwood and builds a breakwater and a wall.
- Bots draw randomness from their own per-bot generators, not the simulation's
  streams. A bot thinking no longer changes the simulation's state, which a
  replay without bots needs.
- MatchView and EntityView draw a match from one Simulation: terrain, sea,
  points of interest, props, pieces, players, the barge, items and chests, with
  stand-in shapes for the art pass to replace.
- `make match-captures` renders eight stages of the recorded match.

Closes #31

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

106 lines
4.2 KiB
GDScript

extends TestCase
## The command log from issue M8: what a match was given, kept so it can be replayed.
func _command(tick: int, buttons: int) -> PlayerCommand:
return PlayerCommand.from_input(tick, Vector2(0.5, -1.0), 1.25, -0.3, buttons, 2, 1)
func _log() -> CommandLog:
var rules: MatchRules = MatchRules.new()
rules.max_players = 6
rules.lobby_ticks = 12
rules.fill_with_bots = false
# A seed too large for a JSON number to hold exactly.
var commands: CommandLog = CommandLog.new(0x7FFFFFFFFFFFFFF1, 2, rules)
commands.record(0, 1, _command(0, PlayerCommand.BUTTON_JUMP))
commands.record(0, -3, _command(0, 0))
commands.record(7, 2, _command(9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD))
commands.final_tick = 8
commands.final_hash = "abc123"
commands.winners = [-3] as Array[int]
commands.notes = {"bots": 4}
return commands
func test_records_keep_their_tick_handle_and_command() -> void:
var commands: CommandLog = _log()
assert_eq(commands.count(), 3)
assert_eq(commands.at_tick_of(2), 7)
assert_eq(commands.handle_of(1), -3)
assert_eq(commands.handle_of(2), 2)
assert_eq(commands.command_of(2).encode(), _command(9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD).encode())
func test_round_trips_through_bytes() -> void:
var commands: CommandLog = _log()
var back: CommandLog = CommandLog.from_bytes(commands.to_bytes())
assert_not_null(back)
assert_eq(back.session_seed, 0x7FFFFFFFFFFFFFF1, "the whole 63-bit seed survives")
assert_eq(back.humans, 2)
assert_eq(back.rules.max_players, 6)
assert_eq(back.rules.lobby_ticks, 12)
assert_false(back.rules.fill_with_bots)
assert_eq(back.rules.countdown_ticks, commands.rules.countdown_ticks)
assert_eq(back.count(), 3)
for index: int in 3:
assert_eq(back.at_tick_of(index), commands.at_tick_of(index))
assert_eq(back.handle_of(index), commands.handle_of(index))
assert_eq(back.command_of(index).encode(), commands.command_of(index).encode())
assert_eq(back.final_tick, 8)
assert_eq(back.final_hash, "abc123")
assert_eq(back.winners, [-3] as Array[int])
assert_eq(int(back.notes["bots"]), 4)
func test_an_empty_log_round_trips() -> void:
var back: CommandLog = CommandLog.from_bytes(CommandLog.new(5, 0).to_bytes())
assert_not_null(back)
assert_eq(back.count(), 0)
assert_eq(back.session_seed, 5)
func test_saves_and_loads_a_file() -> void:
var path: String = "user://test_command_log/roundtrip.tclog"
assert_eq(_log().save(path), OK)
var back: CommandLog = CommandLog.load_from(path)
assert_not_null(back)
assert_eq(back.count(), 3)
DirAccess.remove_absolute(path)
assert_null(CommandLog.load_from(path), "a missing file is no log")
func test_bad_bytes_are_refused_without_errors() -> void:
var good: PackedByteArray = _log().to_bytes()
assert_null(CommandLog.from_bytes(PackedByteArray()))
assert_null(CommandLog.from_bytes("TWCL".to_ascii_buffer()))
var wrong_magic: PackedByteArray = good.duplicate()
wrong_magic[0] = 0x58
assert_null(CommandLog.from_bytes(wrong_magic))
# Cut short anywhere: never a log with records missing, never an engine error.
for size: int in range(0, good.size(), 7):
var cut: CommandLog = CommandLog.from_bytes(good.slice(0, size))
assert_true(cut == null or cut.count() == 3, "cut at %d bytes" % size)
# A header length that points past the end.
var long_header: PackedByteArray = good.duplicate()
long_header.encode_u32(4, 0x7FFFFFFF)
assert_null(CommandLog.from_bytes(long_header))
# A header that isn't JSON.
var head_size: int = good.decode_u32(4)
var not_json: PackedByteArray = good.duplicate()
for index: int in range(8, 8 + head_size):
not_json[index] = 0x7B
assert_null(CommandLog.from_bytes(not_json))
# A raw length that is huge, or not a whole number of records.
var huge: PackedByteArray = good.duplicate()
huge.encode_u32(8 + head_size, 0x7FFFFFF0)
assert_null(CommandLog.from_bytes(huge))
var ragged: PackedByteArray = good.duplicate()
ragged.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 3 + 1)
assert_null(CommandLog.from_bytes(ragged))
# Records that don't decompress to the promised length.
var short: PackedByteArray = good.duplicate()
short.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 40)
assert_null(CommandLog.from_bytes(short))