Stage 2: accounts, characters, permadeath, levels and experience
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.
This commit is contained in:
2026-09-04 00:44:34 +02:00
parent ff5e527ad4
commit 4765bbce28
36 changed files with 1613 additions and 34 deletions
+57
View File
@@ -45,6 +45,10 @@ static func encode_snapshot(world: SimWorld,
b.put_float(p.pos.y)
b.put_u16(wrapi(roundi(p.aim / TAU * 65536.0), 0, 65536))
b.put_u16(clampi(p.hp, 0, 65535))
# Sent rather than assumed: max health follows the character's level, so
# a HUD bar computed from a constant would be wrong for anyone past
# level 1, and wrong for every other player in the party.
b.put_u16(clampi(p.max_hp, 1, 65535))
var flags := 0
if p.alive:
flags |= Protocol.F_ALIVE
@@ -61,6 +65,10 @@ static func encode_snapshot(world: SimWorld,
b.put_u8(clampi(roundi(float(p.respawn_lockout) / 6.0), 0, 255))
# Echoed so the owning client knows how far to rewind when reconciling.
b.put_u32(p.last_input_tick)
# Static per character, so sending it every snapshot is slightly
# wasteful -- but it is four bytes, and the alternative is a separate
# message plus the join-ordering bug where someone arrives before it.
b.put_u32(p.colour.to_rgba32())
var live_enemies: Array[SimEnemy] = []
for e in world.enemies.values():
@@ -111,10 +119,12 @@ static func decode_snapshot(data: PackedByteArray) -> Dictionary:
"pos": Vector2(b.get_float(), b.get_float()),
"aim": float(b.get_u16()) / 65536.0 * TAU,
"hp": b.get_u16(),
"max_hp": b.get_u16(),
"flags": b.get_u8(),
"escape": float(b.get_u8()) / 255.0,
"respawn_wait": float(b.get_u8()) / 10.0,
"last_input_tick": b.get_u32(),
"colour": Color.hex(b.get_u32()),
})
var ecount := b.get_u16()
@@ -353,3 +363,50 @@ static func decode_map_chunks_into(map: MapGrid, data: PackedByteArray) -> int:
map.apply_chunk(id, b.get_data(n)[1])
applied += 1
return applied
# --- Character roster -------------------------------------------------------
# Sent once at login and after any change. Low frequency and carries strings,
# like the online roster, so it is the same fixed-header-then-utf8 shape.
static func encode_characters(chars: Array[Character], selected: String) -> PackedByteArray:
var b := StreamPeerBuffer.new()
b.big_endian = false
b.put_utf8_string(selected)
b.put_u8(mini(chars.size(), 255))
for c in chars:
b.put_utf8_string(c.id)
b.put_utf8_string(c.display_name)
b.put_u8(clampi(c.level, 1, 255))
b.put_u32(maxi(c.total_xp, 0))
b.put_u8(clampi(roundi(c.xp_progress() * 255.0), 0, 255))
b.put_u16(clampi(c.max_hp(), 1, 65535))
b.put_u8(1 if c.active else 0)
b.put_u32(c.colour.to_rgba32())
return b.data_array
## Returns { "selected": String, "characters": Array[Dictionary] }.
static func decode_characters(data: PackedByteArray) -> Dictionary:
var out: Array[Dictionary] = []
if data.size() < 1:
return {"selected": "", "characters": out}
var b := StreamPeerBuffer.new()
b.big_endian = false
b.data_array = data
var selected := b.get_utf8_string()
var count := b.get_u8()
for _i in count:
if b.get_available_bytes() <= 0:
break
out.append({
"id": b.get_utf8_string(),
"name": b.get_utf8_string(),
"level": b.get_u8(),
"xp": b.get_u32(),
"progress": float(b.get_u8()) / 255.0,
"max_hp": b.get_u16(),
"active": b.get_u8() == 1,
"colour": Color.hex(b.get_u32()),
})
return {"selected": selected, "characters": out}