extends Node ## NEO-122: fetches [code]GET /game/world/quest-definitions[/code] (NEO-115). ## Caches ordered quest rows for HUD display names and eligible-quest iteration. signal definitions_ready(quests: Array) signal definitions_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 _quests: Array = [] 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/quest-definitions" % _base_root() var err: Error = _http.request(url) if err != OK: var reason := "GET failed to start (%s)" % err push_warning("QuestDefinitionsClient: %s" % reason) _busy = false definitions_sync_failed.emit(reason) func display_name_for(quest_id: String) -> String: for row_variant in _quests: if not row_variant is Dictionary: continue var row: Dictionary = row_variant if str(row.get("id", "")) == quest_id: var dn := str(row.get("displayName", "")).strip_edges() if not dn.is_empty(): return dn return quest_id func quests_snapshot() -> Array: return _quests.duplicate(true) func _base_root() -> String: return base_url.strip_edges().rstrip("/") static func parse_quests_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 quests: Variant = root.get("quests", null) if quests == null or not quests is Array: return null return quests as Array 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("QuestDefinitionsClient: %s" % reason) definitions_sync_failed.emit(reason) return if response_code < 200 or response_code >= 300: var reason_code := "HTTP %s" % response_code push_warning("QuestDefinitionsClient: %s" % reason_code) definitions_sync_failed.emit(reason_code) return var text := body.get_string_from_utf8() var quests: Variant = parse_quests_json(text) if quests == null: var reason_json := "non-JSON body or schemaVersion mismatch" push_warning("QuestDefinitionsClient: %s" % reason_json) definitions_sync_failed.emit(reason_json) return _quests = (quests as Array).duplicate(true) definitions_ready.emit(_quests.duplicate(true))