fix(client): address PR #4 review feedback
Hoshe critical fixes:
- _action_enum_to_wire uses InputMapper.Action constants instead of
fragile integer literals; OPEN_MENU explicitly handled as client-only
- Remove int() coercion on tick/entity_id — use direct assignment since
GDScript int is signed 64-bit (safe for realistic tick values)
- Check encode result before buffering in send_input() — reject empty
bytes instead of corrupting the outbound stream
- Test snapshot now uses Protocol format {tick, entities} instead of
legacy schema; GameState updated to derive player position from
entity data; main.gd and world_renderer.gd updated accordingly
Hoshe warnings:
- 5 negative tests added (truncated bytes, wrong type, missing fields,
empty bytes, encode validation) — 20/20 tests pass
- receive_bytes signal is emitted at consume time in poll_snapshot by
design (documented in code)
Tyre suggestions:
- Remove duplicated root-level fixtures — single source of truth in
client/tests/fixtures/msgpack/
- gen_fixtures.rs writes directly to client/ directory
- Add `make fixtures` target for regeneration
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,31 +1,26 @@
|
||||
extends Node
|
||||
|
||||
# Updated each frame from ObserverSnapshot data
|
||||
# Updated each frame from ObserverSnapshot data (Protocol format: {tick, entities}).
|
||||
# Entities use Protocol decoded format: {entity_id, x, y, z, kind: {variant, data}}.
|
||||
var current_snapshot: Dictionary = {}
|
||||
var current_tick: int = 0
|
||||
var player_position: Vector2 = Vector2.ZERO
|
||||
var visible_entities: Array = []
|
||||
var fog_state: Dictionary = {}
|
||||
var hud_data: Dictionary = {}
|
||||
|
||||
# Player entity ID — the first entity is assumed to be the player (will be
|
||||
# refined when the server assigns explicit player entity IDs).
|
||||
var player_entity_id: int = 1
|
||||
|
||||
func apply_snapshot(snapshot: Dictionary) -> void:
|
||||
current_snapshot = snapshot
|
||||
|
||||
# Parse player data
|
||||
if snapshot.has("player") and snapshot.player.has("position"):
|
||||
var pos = snapshot.player.position
|
||||
if pos is Array and pos.size() >= 2:
|
||||
player_position = Vector2(pos[0], pos[1])
|
||||
else:
|
||||
push_warning("GameState: malformed player position in snapshot")
|
||||
if snapshot.has("tick"):
|
||||
current_tick = snapshot.tick
|
||||
|
||||
# Parse entities
|
||||
if snapshot.has("entities"):
|
||||
visible_entities = snapshot.entities
|
||||
|
||||
# Parse fog state
|
||||
if snapshot.has("fog"):
|
||||
fog_state = snapshot.fog
|
||||
|
||||
# Parse HUD data
|
||||
if snapshot.has("hud"):
|
||||
hud_data = snapshot.hud
|
||||
# Derive player position from the player entity
|
||||
for entity in visible_entities:
|
||||
if entity.has("entity_id") and entity.entity_id == player_entity_id:
|
||||
player_position = Vector2(entity.x, entity.y)
|
||||
break
|
||||
|
||||
@@ -49,7 +49,11 @@ func send_input(player_input: Dictionary) -> void:
|
||||
if action_name.is_empty():
|
||||
return
|
||||
var tick: int = player_input.get("timestamp_msec", 0)
|
||||
_outbound_buffer.append(Protocol.encode_player_input(tick, action_name))
|
||||
var encoded := Protocol.encode_player_input(tick, action_name)
|
||||
if encoded.size() == 0:
|
||||
push_error("SimBridge: failed to encode player input (action=%s)" % action_name)
|
||||
return
|
||||
_outbound_buffer.append(encoded)
|
||||
|
||||
# Poll for snapshot from simulation.
|
||||
# In test mode returns hardcoded data. In live mode, returns the last decoded snapshot (if any).
|
||||
@@ -83,41 +87,37 @@ func drain_outbound() -> Array[PackedByteArray]:
|
||||
return messages
|
||||
|
||||
# Map InputMapper.Action enum values to wire-format action names (matching Rust PlayerAction).
|
||||
# OPEN_MENU is client-only — no Rust equivalent, not sent over the wire.
|
||||
static func _action_enum_to_wire(action: int) -> String:
|
||||
match action:
|
||||
0: return "MoveNorth" # InputMapper.Action.MOVE_NORTH
|
||||
1: return "MoveSouth" # InputMapper.Action.MOVE_SOUTH
|
||||
2: return "MoveEast" # InputMapper.Action.MOVE_EAST
|
||||
3: return "MoveWest" # InputMapper.Action.MOVE_WEST
|
||||
4: return "Interact" # InputMapper.Action.INTERACT
|
||||
5: return "UsePerceptionMode" # InputMapper.Action.USE_PERCEPTION_MODE
|
||||
7: return "Pause" # InputMapper.Action.PAUSE
|
||||
InputMapper.Action.MOVE_NORTH: return "MoveNorth"
|
||||
InputMapper.Action.MOVE_SOUTH: return "MoveSouth"
|
||||
InputMapper.Action.MOVE_EAST: return "MoveEast"
|
||||
InputMapper.Action.MOVE_WEST: return "MoveWest"
|
||||
InputMapper.Action.INTERACT: return "Interact"
|
||||
InputMapper.Action.USE_PERCEPTION_MODE: return "UsePerceptionMode"
|
||||
InputMapper.Action.PAUSE: return "Pause"
|
||||
InputMapper.Action.OPEN_MENU:
|
||||
# Client-only action, not part of wire protocol
|
||||
push_warning("SimBridge: OPEN_MENU is client-only, not sent to server")
|
||||
return ""
|
||||
_:
|
||||
push_warning("SimBridge: unknown action enum %s" % action)
|
||||
return ""
|
||||
|
||||
# Hardcoded test snapshot for development (deterministic per D-010 principle 4)
|
||||
# Hardcoded test snapshot matching Protocol format (deterministic per D-010 principle 4).
|
||||
# Uses the same {tick, entities} schema as Protocol.decode_snapshot() returns.
|
||||
func _test_snapshot() -> Dictionary:
|
||||
_test_tick += 1
|
||||
return {
|
||||
"tick": _test_tick,
|
||||
"player": {
|
||||
"position": [10, 10],
|
||||
"health": 100
|
||||
},
|
||||
"entities": [
|
||||
{
|
||||
"id": 1,
|
||||
"type": "npc",
|
||||
"position": [12, 8],
|
||||
"name": "Test NPC"
|
||||
"entity_id": 1,
|
||||
"x": 10.0,
|
||||
"y": 10.0,
|
||||
"z": 0,
|
||||
"kind": { "variant": "Npc", "data": null },
|
||||
},
|
||||
],
|
||||
"fog": {
|
||||
"radius": 8
|
||||
},
|
||||
"hud": {
|
||||
"perception_mode": "baseline",
|
||||
"time": "08:00"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,16 +24,6 @@ func _process(_delta: float) -> void:
|
||||
# Track camera to player position (D-015)
|
||||
camera.position = GameState.player_position * 32 # tile-space to pixel-space
|
||||
|
||||
# Update HUD
|
||||
if hud and hud.has_method("update_from_hud_data"):
|
||||
hud.update_from_hud_data(GameState.hud_data)
|
||||
if snapshot.has("player") and snapshot.player.has("health"):
|
||||
hud.update_health(snapshot.player.health)
|
||||
|
||||
# Wire monologue display (D-016 perception data path)
|
||||
if snapshot.has("monologue") and monologue_display:
|
||||
monologue_display.show_monologue(snapshot.monologue)
|
||||
|
||||
# Send queued input to simulation
|
||||
var inputs = InputMapper.flush_queue()
|
||||
for input in inputs:
|
||||
|
||||
@@ -31,8 +31,11 @@ static func decode_snapshot(bytes: PackedByteArray) -> Variant:
|
||||
if entity != null:
|
||||
entities.append(entity)
|
||||
|
||||
# GDScript int is signed 64-bit. Rust tick is u64 but will not exceed 2^63
|
||||
# in any realistic scenario (would require ~29 billion years at 10 ticks/min).
|
||||
var tick: int = raw["tick"]
|
||||
return {
|
||||
"tick": int(raw["tick"]),
|
||||
"tick": tick,
|
||||
"entities": entities,
|
||||
}
|
||||
|
||||
@@ -44,8 +47,9 @@ static func _decode_entity(raw: Dictionary) -> Variant:
|
||||
push_warning("Protocol: entity missing required fields: %s" % str(raw.keys()))
|
||||
return null
|
||||
|
||||
var entity_id: int = raw["entity_id"]
|
||||
return {
|
||||
"entity_id": int(raw["entity_id"]),
|
||||
"entity_id": entity_id,
|
||||
"x": float(raw["x"]),
|
||||
"y": float(raw["y"]),
|
||||
"z": int(raw["z"]),
|
||||
@@ -108,7 +112,8 @@ static func decode_player_input(bytes: PackedByteArray) -> Variant:
|
||||
push_error("Protocol: player_input missing required fields")
|
||||
return null
|
||||
|
||||
var tick: int = raw["tick"]
|
||||
return {
|
||||
"tick": int(raw["tick"]),
|
||||
"tick": tick,
|
||||
"action": _decode_enum_variant(raw["action"]),
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://fkmd537xvwxe
|
||||
@@ -15,6 +15,6 @@ func update_from_state() -> void:
|
||||
if entity_renderer and entity_renderer.has_method("update_entities"):
|
||||
entity_renderer.update_entities(GameState.visible_entities)
|
||||
|
||||
# Update fog overlay
|
||||
# Update fog overlay (fog data will come in D-020 expansion)
|
||||
if fog_renderer and fog_renderer.has_method("update_fog"):
|
||||
fog_renderer.update_fog(GameState.fog_state, GameState.player_position)
|
||||
fog_renderer.update_fog({}, GameState.player_position)
|
||||
|
||||
Reference in New Issue
Block a user