4765bbce28
ci / verify (push) Successful in 47s
Identity is shaped like Steamworks so swapping to it is one subclass and no schema change: the client presents an opaque ticket, the server validates it into a stable 64-bit account id, and nothing downstream sees anything else. LocalAuthProvider takes any ticket at face value -- insecure on purpose, and labelled as such everywhere, because the point is the shape rather than the security. Do not ship it. Characters persist as JSON keyed by account. Account ids are written as decimal strings because they are 64-bit and JSON numbers are doubles, which would silently round them. A corrupt store aborts the server rather than starting empty: starting empty looks like it worked and then saves over every character on the first level-up. Levels 1-15, +10 max health each, level DERIVED from lifetime experience rather than stored beside it, so a hand-edited save cannot produce a level 12 character with a level 3's experience. Experience is shared undivided across everyone alive in the instance -- splitting it would make bringing a friend cost you progress. A level-up heals by what it added, so gaining one mid-fight is relief rather than a bar that moved further from full. Death is permanent and unbinds the character entirely: no "return to the hub as the character who just died", because the run is over. The record is retired, never deleted. The five-character cap counts LIVING characters only -- counting the dead would lock a player out of their own account after five deaths. Verified by tools/diag_progression.tscn, which drives the real server through kill -> xp -> level -> health and death -> retire -> roster. The bot smoke test cannot cover that: bots are poor shots and rarely kill anything. Writing it caught two real ordering bugs -- the death event was dispatched before the payload that tells the player they died, and the dead character stayed bound to the peer. Also added --account and --store so several clients and test runs can coexist on one machine. The smoke test now uses a scratch store; without it a rerun resumed the previous run's characters and "a character was created" quietly stopped being true. 193 tests. check.sh, test.sh, smoke.sh, diag_progression and diag_prediction all pass.
221 lines
8.2 KiB
GDScript
221 lines
8.2 KiB
GDScript
extends GutTest
|
|
## Codec round-trips. A field that encodes but decodes wrong shows up as a
|
|
## mysterious gameplay bug three layers away, so it is pinned down here.
|
|
|
|
var world: SimWorld
|
|
|
|
|
|
func before_each() -> void:
|
|
world = SimWorld.new(7)
|
|
var p := world.add_player(42, "ada")
|
|
p.pos = Vector2(120.5, -64.25)
|
|
p.aim = 1.25
|
|
p.hp = 73
|
|
p.escape_ticks = 30
|
|
p.last_input_tick = 555
|
|
world.spawn_enemy(Content.turret(), Vector2(-200.0, 100.0))
|
|
world.spawn_boss(Content.warden())
|
|
world.tick = 9001
|
|
|
|
|
|
func test_snapshot_round_trips_player_state() -> void:
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
assert_eq(int(snap["tick"]), 9001)
|
|
var rec: Dictionary = snap["players"][0]
|
|
assert_eq(int(rec["peer"]), 42)
|
|
assert_almost_eq((rec["pos"] as Vector2).x, 120.5, 0.01)
|
|
assert_almost_eq((rec["pos"] as Vector2).y, -64.25, 0.01)
|
|
assert_almost_eq(float(rec["aim"]), 1.25, 0.001)
|
|
assert_eq(int(rec["hp"]), 73)
|
|
assert_eq(int(rec["last_input_tick"]), 555)
|
|
assert_true((int(rec["flags"]) & Protocol.F_ALIVE) != 0)
|
|
assert_true((int(rec["flags"]) & Protocol.F_ESCAPING) != 0)
|
|
assert_almost_eq(float(rec["escape"]), 30.0 / float(SimConfig.ESCAPE_CHANNEL_TICKS), 0.01)
|
|
|
|
|
|
func test_snapshot_carries_enemy_radius_for_late_joiners() -> void:
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
var e: Dictionary = snap["enemies"][0]
|
|
assert_almost_eq(float(e["radius"]), Content.turret().radius, 0.5)
|
|
assert_eq(int(e["visual"]), Content.turret().visual)
|
|
|
|
|
|
func test_snapshot_carries_the_boss() -> void:
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
assert_not_null(snap["boss"])
|
|
assert_eq(int(snap["boss"]["hp"]), Content.warden().max_hp)
|
|
|
|
|
|
func test_dead_enemies_are_not_sent() -> void:
|
|
for e in world.enemies.values():
|
|
e.alive = false
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
assert_eq((snap["enemies"] as Array).size(), 0)
|
|
|
|
|
|
func test_event_round_trip_preserves_bullet_spawn() -> void:
|
|
var ev := {
|
|
"t": SimEvent.Type.BULLET_SPAWN, "uid": 8123,
|
|
"pos": Vector2(10.0, -20.0), "vel": Vector2(0.0, 150.0),
|
|
"r": 7.5, "life": 300, "kind": SimConfig.KIND_NEEDLE,
|
|
"team": SimConfig.TEAM_ENEMY, "accel": 12.0, "turn": 0.02,
|
|
}
|
|
var packet := NetCodec.decode_events(NetCodec.encode_events(77, [ev]))
|
|
assert_eq(int(packet["tick"]), 77)
|
|
var out: Dictionary = packet["events"][0]
|
|
assert_eq(int(out["uid"]), 8123)
|
|
assert_eq(out["pos"], Vector2(10.0, -20.0))
|
|
assert_eq(out["vel"], Vector2(0.0, 150.0))
|
|
assert_almost_eq(float(out["r"]), 7.5, 0.001)
|
|
assert_eq(int(out["life"]), 300)
|
|
assert_almost_eq(float(out["accel"]), 12.0, 0.001)
|
|
assert_almost_eq(float(out["turn"]), 0.02, 0.0001)
|
|
|
|
|
|
func test_server_only_events_never_reach_the_wire() -> void:
|
|
var events: Array[Dictionary] = [
|
|
{"t": SimEvent.Type.PORTAL_USED, "peer": 3},
|
|
{"t": SimEvent.Type.ESCAPE_COMPLETED, "peer": 3},
|
|
{"t": SimEvent.Type.PLAYER_DIED, "peer": 3},
|
|
]
|
|
var packet := NetCodec.decode_events(NetCodec.encode_events(1, events))
|
|
assert_eq((packet["events"] as Array).size(), 1,
|
|
"instance transfers are the server's business, not the client's")
|
|
assert_eq(int(packet["events"][0]["t"]), SimEvent.Type.PLAYER_DIED)
|
|
|
|
|
|
func test_input_round_trip() -> void:
|
|
var frames: Array[InputFrame] = [
|
|
InputFrame.make(10, Vector2(1, 0), 0.0, InputFrame.BTN_FIRE),
|
|
InputFrame.make(11, Vector2(0, 1), 1.0, 0),
|
|
]
|
|
var out := NetCodec.decode_inputs(NetCodec.encode_inputs(frames))
|
|
assert_eq(out.size(), 2)
|
|
assert_eq(out[0].tick, 10)
|
|
assert_eq(out[1].tick, 11)
|
|
|
|
|
|
func test_truncated_input_packet_is_rejected_not_read_past() -> void:
|
|
var frames: Array[InputFrame] = [InputFrame.make(10, Vector2.ONE, 0.0, 1)]
|
|
var data := NetCodec.encode_inputs(frames)
|
|
# Claim three frames but send one. A hostile client will try exactly this.
|
|
data[0] = 3
|
|
assert_eq(NetCodec.decode_inputs(data).size(), 0)
|
|
|
|
|
|
func test_empty_input_packet_is_safe() -> void:
|
|
assert_eq(NetCodec.decode_inputs(PackedByteArray()).size(), 0)
|
|
|
|
|
|
# --- Roster -----------------------------------------------------------------
|
|
|
|
func test_roster_round_trips() -> void:
|
|
var entries: Array[Dictionary] = [
|
|
{"peer": 7, "name": "ada", "kind": Protocol.InstanceKind.LOBBY,
|
|
"instance": 1, "alive": true},
|
|
{"peer": 9, "name": "grace", "kind": Protocol.InstanceKind.DUNGEON,
|
|
"instance": 4, "alive": false},
|
|
]
|
|
var out := NetCodec.decode_roster(NetCodec.encode_roster(entries))
|
|
assert_eq(out.size(), 2)
|
|
assert_eq(int(out[0]["peer"]), 7)
|
|
assert_eq(String(out[0]["name"]), "ada")
|
|
assert_eq(int(out[0]["kind"]), Protocol.InstanceKind.LOBBY)
|
|
assert_true(out[0]["alive"])
|
|
assert_eq(String(out[1]["name"]), "grace")
|
|
assert_eq(int(out[1]["kind"]), Protocol.InstanceKind.DUNGEON,
|
|
"the hub needs to know someone is already inside")
|
|
assert_eq(int(out[1]["instance"]), 4)
|
|
assert_false(out[1]["alive"])
|
|
|
|
|
|
func test_roster_survives_unicode_names() -> void:
|
|
var entries: Array[Dictionary] = [
|
|
{"peer": 1, "name": "ゆき", "kind": 0, "instance": 1, "alive": true}]
|
|
var out := NetCodec.decode_roster(NetCodec.encode_roster(entries))
|
|
assert_eq(String(out[0]["name"]), "ゆき")
|
|
|
|
|
|
func test_empty_roster_is_safe() -> void:
|
|
assert_eq(NetCodec.decode_roster(PackedByteArray()).size(), 0)
|
|
assert_eq(NetCodec.decode_roster(NetCodec.encode_roster([] as Array[Dictionary])).size(), 0)
|
|
|
|
|
|
# --- Snapshot extras --------------------------------------------------------
|
|
|
|
func test_snapshot_carries_spawn_grace_and_linkdead_flags() -> void:
|
|
var p: SimPlayer = world.players[42]
|
|
p.spawn_grace = 30
|
|
p.linkdead = true
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
var flags := int(snap["players"][0]["flags"])
|
|
assert_true((flags & Protocol.F_SPAWN_GRACE) != 0)
|
|
assert_true((flags & Protocol.F_LINKDEAD) != 0)
|
|
|
|
|
|
func test_cleared_countdown_round_trips() -> void:
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world, 17))
|
|
assert_eq(int(snap["cleared_countdown"]), 17)
|
|
|
|
|
|
func test_countdown_defaults_to_not_applicable() -> void:
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
assert_eq(int(snap["cleared_countdown"]), Protocol.COUNTDOWN_NONE)
|
|
|
|
|
|
# --- Characters -------------------------------------------------------------
|
|
|
|
func test_character_roster_round_trips() -> void:
|
|
var rng := RandomNumberGenerator.new()
|
|
rng.seed = 5
|
|
var a := Character.create("Ada", rng)
|
|
a.grant_xp(Progression.total_xp_for_level(4))
|
|
var b := Character.create("Departed", rng)
|
|
b.retire()
|
|
var out := NetCodec.decode_characters(
|
|
NetCodec.encode_characters([a, b] as Array[Character], a.id))
|
|
assert_eq(String(out["selected"]), a.id)
|
|
var chars: Array = out["characters"]
|
|
assert_eq(chars.size(), 2)
|
|
assert_eq(String(chars[0]["name"]), "Ada")
|
|
assert_eq(int(chars[0]["level"]), a.level)
|
|
assert_eq(int(chars[0]["max_hp"]), a.max_hp(),
|
|
"the roster shows each character's own health ceiling")
|
|
assert_true(chars[0]["active"])
|
|
assert_false(chars[1]["active"], "a dead character is listed, not hidden")
|
|
|
|
|
|
## Colour is a character's only identity until cosmetics exist, so it has to
|
|
## survive the wire exactly rather than approximately.
|
|
func test_character_colour_survives_the_wire() -> void:
|
|
var rng := RandomNumberGenerator.new()
|
|
rng.seed = 11
|
|
var c := Character.create("Hue", rng)
|
|
var out := NetCodec.decode_characters(
|
|
NetCodec.encode_characters([c] as Array[Character], ""))
|
|
var got: Color = out["characters"][0]["colour"]
|
|
assert_eq(got.to_rgba32(), c.colour.to_rgba32())
|
|
|
|
|
|
func test_an_empty_character_list_is_safe() -> void:
|
|
var out := NetCodec.decode_characters(
|
|
NetCodec.encode_characters([] as Array[Character], ""))
|
|
assert_eq((out["characters"] as Array).size(), 0)
|
|
assert_eq(NetCodec.decode_characters(PackedByteArray())["characters"].size(), 0)
|
|
|
|
|
|
## Max health is per-character now, so a snapshot that assumed a constant would
|
|
## draw every levelled player's bar wrong.
|
|
func test_the_snapshot_carries_each_players_own_max_health() -> void:
|
|
var p: SimPlayer = world.players[42]
|
|
p.level = 5
|
|
p.max_hp = Progression.max_hp_for_level(5)
|
|
p.hp = 42
|
|
p.colour = Color(0.2, 0.6, 0.9)
|
|
var snap := NetCodec.decode_snapshot(NetCodec.encode_snapshot(world))
|
|
var rec: Dictionary = snap["players"][0]
|
|
assert_eq(int(rec["hp"]), 42)
|
|
assert_eq(int(rec["max_hp"]), Progression.max_hp_for_level(5))
|
|
assert_almost_eq((rec["colour"] as Color).b, 0.9, 0.02,
|
|
"party members are told apart by colour, so it rides the snapshot")
|