neon-sprawl/client/scripts/npc_runtime_client.gd

126 lines
3.7 KiB
GDScript

extends Node
## NEO-97: prototype HTTP client for [code]GET /game/world/npc-runtime-snapshot[/code] (NEO-94).
signal runtime_received(snapshot: Dictionary)
signal runtime_sync_failed(reason: String)
const SCHEMA_VERSION := 1
@export var base_url: String = "http://127.0.0.1:5253"
@export var injected_http: Node = null
var _http: Node
var _busy: bool = false
var _last_snapshot: Dictionary = {}
func _ready() -> void:
if injected_http != null:
_http = injected_http
else:
_http = HTTPRequest.new()
add_child(_http)
if _http is HTTPRequest:
(_http as HTTPRequest).timeout = 30.0
@warning_ignore("unsafe_method_access")
_http.request_completed.connect(_on_request_completed)
func request_sync_from_server() -> void:
if _busy:
return
_busy = true
var url := "%s/game/world/npc-runtime-snapshot" % _base_root()
var err: Error = _http.request(url)
if err != OK:
var reason := "GET failed to start (%s)" % err
push_warning("NpcRuntimeClient: %s" % reason)
_busy = false
runtime_sync_failed.emit(reason)
static func parse_runtime_json(text: String) -> Variant:
var parsed: Variant = JSON.parse_string(text)
if not parsed is Dictionary:
return null
var root: Dictionary = parsed
if int(root.get("schemaVersion", -1)) != SCHEMA_VERSION:
return null
var instances: Variant = root.get("npcInstances", null)
if instances == null or not instances is Array:
return null
return root
func npc_row(instance_id: String, snapshot: Dictionary = {}) -> Dictionary:
var source: Dictionary = snapshot
if source.is_empty():
source = _last_snapshot
return npc_row_in_snapshot(instance_id, source)
static func npc_row(instance_id: String, snapshot: Dictionary = {}) -> Dictionary:
return npc_row_in_snapshot(instance_id, snapshot)
static func npc_row_in_snapshot(instance_id: String, snapshot: Dictionary) -> Dictionary:
var source: Dictionary = snapshot
if source.is_empty():
return {}
var instances: Variant = source.get("npcInstances", null)
if instances == null or not instances is Array:
return {}
for row_variant in instances as Array:
if not row_variant is Dictionary:
continue
var row: Dictionary = row_variant
if str(row.get("npcInstanceId", "")) == instance_id:
return row
return {}
static func row_for_aggro_holder(player_id: String, snapshot: Dictionary = {}) -> Dictionary:
var holder: String = player_id.strip_edges()
if holder.is_empty() or snapshot.is_empty():
return {}
var instances: Variant = snapshot.get("npcInstances", null)
if instances == null or not instances is Array:
return {}
for row_variant in instances as Array:
if not row_variant is Dictionary:
continue
var row: Dictionary = row_variant
if str(row.get("aggroHolderPlayerId", "")) == holder:
return row
return {}
func _base_root() -> String:
return base_url.strip_edges().rstrip("/")
func _on_request_completed(
result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray
) -> void:
_busy = false
if result != HTTPRequest.RESULT_SUCCESS:
var reason := "HTTP failed (result=%s)" % result
push_warning("NpcRuntimeClient: %s" % reason)
runtime_sync_failed.emit(reason)
return
if response_code < 200 or response_code >= 300:
var reason_code := "HTTP %s" % response_code
push_warning("NpcRuntimeClient: %s" % reason_code)
runtime_sync_failed.emit(reason_code)
return
var text := body.get_string_from_utf8()
var snapshot: Variant = parse_runtime_json(text)
if snapshot == null:
var reason_json := "non-JSON body or schemaVersion mismatch"
push_warning("NpcRuntimeClient: %s" % reason_json)
runtime_sync_failed.emit(reason_json)
return
_last_snapshot = snapshot as Dictionary
runtime_received.emit(_last_snapshot)