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
+146
View File
@@ -0,0 +1,146 @@
extends CanvasLayer
## Character roster: pick one, or make one.
##
## Shown when the player has no character in the world -- at first login, and
## again when their last one dies. Permadeath makes this a screen players will
## see repeatedly, so dead characters stay listed rather than vanishing: seeing
## the run you lost is the point of keeping the record.
signal select_requested(character_id: String)
signal create_requested(character_name: String)
var _list: VBoxContainer
var _name_field: LineEdit
var _create_button: Button
var _status: Label
var _title: Label
var _known: Array[Dictionary] = []
var _selected: String = ""
func _ready() -> void:
layer = 30
var root := Control.new()
root.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(root)
var scrim := ColorRect.new()
scrim.color = Color(0.03, 0.03, 0.06, 0.92)
scrim.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
root.add_child(scrim)
var centre := CenterContainer.new()
centre.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
root.add_child(centre)
var panel := VBoxContainer.new()
panel.custom_minimum_size = Vector2(460.0, 0.0)
panel.add_theme_constant_override("separation", 8)
centre.add_child(panel)
_title = Label.new()
_title.text = "CHARACTERS"
_title.add_theme_font_size_override("font_size", 26)
panel.add_child(_title)
_list = VBoxContainer.new()
_list.add_theme_constant_override("separation", 4)
panel.add_child(_list)
var spacer := Control.new()
spacer.custom_minimum_size = Vector2(0.0, 10.0)
panel.add_child(spacer)
var row := HBoxContainer.new()
panel.add_child(row)
_name_field = LineEdit.new()
_name_field.placeholder_text = "new character name"
_name_field.max_length = Character.MAX_NAME
_name_field.size_flags_horizontal = Control.SIZE_EXPAND_FILL
row.add_child(_name_field)
_create_button = Button.new()
_create_button.text = "Create"
_create_button.pressed.connect(_on_create)
row.add_child(_create_button)
_status = Label.new()
_status.add_theme_font_size_override("font_size", 12)
_status.add_theme_color_override("font_color", Color(1.0, 0.6, 0.5))
panel.add_child(_status)
visible = false
func _on_create() -> void:
var wanted := _name_field.text.strip_edges()
if wanted.is_empty():
set_status("give the character a name")
return
create_requested.emit(wanted)
_name_field.text = ""
func set_status(text: String) -> void:
_status.text = text
## Rebuild from the server's roster. Called on every change rather than diffed:
## the list is at most a handful of rows and correctness matters more than
## avoiding a few Control allocations.
func refresh(characters: Array[Dictionary], selected: String) -> void:
_known = characters
_selected = selected
for child in _list.get_children():
child.queue_free()
var living := 0
for c in characters:
if c["active"]:
living += 1
_list.add_child(_row_for(c))
if characters.is_empty():
var empty := Label.new()
empty.text = "No characters yet. Name one below to begin."
empty.add_theme_color_override("font_color", Color(0.7, 0.75, 0.85))
_list.add_child(empty)
var room := living < CharacterStore.MAX_ACTIVE
_create_button.disabled = not room
_name_field.editable = room
_title.text = "CHARACTERS (%d / %d living)" % [living, CharacterStore.MAX_ACTIVE]
if not room:
set_status("%d living characters is the limit -- one must fall first"
% CharacterStore.MAX_ACTIVE)
func _row_for(c: Dictionary) -> Control:
var row := HBoxContainer.new()
# The character's colour, which is its only identity until cosmetics exist.
var swatch := ColorRect.new()
swatch.color = c["colour"]
swatch.custom_minimum_size = Vector2(14.0, 14.0)
row.add_child(swatch)
var alive: bool = c["active"]
var label := Label.new()
var suffix := ""
if not alive:
suffix = " (dead)"
elif String(c["id"]) == _selected:
suffix = " <- playing"
label.text = " %s level %d %d hp%s" % [
c["name"], int(c["level"]), int(c["max_hp"]), suffix]
label.add_theme_color_override("font_color",
Color(0.85, 0.9, 1.0) if alive else Color(0.55, 0.4, 0.42))
label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
row.add_child(label)
var play := Button.new()
play.text = "Play"
# A dead character is listed as a record, never as an option.
play.disabled = not alive or String(c["id"]) == _selected
play.pressed.connect(func() -> void: select_requested.emit(String(c["id"])))
row.add_child(play)
return row
+1
View File
@@ -0,0 +1 @@
uid://w2dehyfmfm0i
+21 -3
View File
@@ -91,8 +91,12 @@ func _status_text() -> String:
if client == null:
return "connecting..."
var where := "LOBBY" if client.instance_kind == Protocol.InstanceKind.LOBBY else "DUNGEON"
return "%s instance %d hp %d/%d %d fps" % [
where, client.instance_id, client.my_hp, SimConfig.PLAYER_MAX_HP,
var who := client.current_character()
var name_part := ""
if not who.is_empty():
name_part = "%s lv %d " % [who["name"], int(who["level"])]
return "%s%s instance %d hp %d/%d %d fps" % [
name_part, where, client.instance_id, client.my_hp, client.my_max_hp,
Engine.get_frames_per_second()]
@@ -110,8 +114,9 @@ func _draw_hud() -> void:
if client == null:
return
var origin := Vector2(MARGIN, MARGIN + 28.0)
_bar(origin, float(client.my_hp) / float(SimConfig.PLAYER_MAX_HP),
_bar(origin, float(client.my_hp) / float(maxi(client.my_max_hp, 1)),
Color(0.35, 0.9, 0.6), Color(0.1, 0.15, 0.18))
_draw_xp_bar(origin + Vector2(0.0, BAR_H + 3.0))
if client.my_escaping:
_bar(origin + Vector2(0.0, BAR_H + 8.0), client.my_escape,
@@ -144,6 +149,19 @@ func _draw_hud() -> void:
Color(1.0, 0.2, 0.25, 0.18 * _hit_flash))
## A thin bar under health: progress toward the next level, and the level
## itself. Drawn from the server's numbers, never recomputed locally.
func _draw_xp_bar(at: Vector2) -> void:
var who := client.current_character()
if who.is_empty():
return
var level := int(who["level"])
var progress: float = who["progress"] if level < Progression.MAX_LEVEL else 1.0
_canvas.draw_rect(Rect2(at, Vector2(BAR_W, 4.0)), Color(0.1, 0.12, 0.18))
_canvas.draw_rect(Rect2(at, Vector2(BAR_W * progress, 4.0)),
Color(0.6, 0.55, 1.0) if level < Progression.MAX_LEVEL else Color(1.0, 0.85, 0.4))
## Shown after the boss dies, so the victory lap has a visible clock on it.
func _draw_cleared_countdown() -> void:
if client.cleared_countdown >= Protocol.COUNTDOWN_NONE: