neon-sprawl/client/scripts/skill_progression_client.gd

123 lines
3.4 KiB
GDScript

extends Node
## NEO-73: prototype HTTP client for [code]GET /game/players/{id}/skill-progression[/code] (NEO-37).
signal progression_received(snapshot: Dictionary)
signal progression_sync_failed(reason: String)
const SCHEMA_VERSION := 1
@export var base_url: String = "http://127.0.0.1:5253"
@export var dev_player_id: String = "dev-local-1"
@export var injected_http: Node = null
var _http: Node
var _busy: bool = false
var _sync_pending: bool = false
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:
_sync_pending = true
return
_start_sync_request()
func _start_sync_request() -> void:
_busy = true
var url := "%s/game/players/%s/skill-progression" % [_base_root(), _player_path_segment()]
var err: Error = _http.request(url)
if err != OK:
var reason := "GET failed to start (%s)" % err
push_warning("SkillProgressionClient: %s" % reason)
_busy = false
progression_sync_failed.emit(reason)
_try_flush_pending_sync()
func _try_flush_pending_sync() -> void:
if not _sync_pending or _busy:
return
_sync_pending = false
_start_sync_request()
func skill_row(skill_id: String, snapshot: Dictionary = {}) -> Dictionary:
var skills: Variant = snapshot.get("skills", null)
if skills == null or not skills is Array:
return {}
for row_variant in skills as Array:
if not row_variant is Dictionary:
continue
var row: Dictionary = row_variant
if str(row.get("id", "")) == skill_id:
return row
return {}
func _base_root() -> String:
return base_url.strip_edges().rstrip("/")
func _player_path_segment() -> String:
return dev_player_id.strip_edges()
static func parse_progression_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 skills: Variant = root.get("skills", null)
if skills == null or not skills is Array:
return null
return root
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("SkillProgressionClient: %s" % reason)
progression_sync_failed.emit(reason)
_try_flush_pending_sync()
return
if response_code == 404:
var reason404 := "HTTP 404 (player unknown)"
push_warning("SkillProgressionClient: %s" % reason404)
progression_sync_failed.emit(reason404)
_try_flush_pending_sync()
return
if response_code < 200 or response_code >= 300:
var reason_code := "HTTP %s" % response_code
push_warning("SkillProgressionClient: %s" % reason_code)
progression_sync_failed.emit(reason_code)
_try_flush_pending_sync()
return
var text := body.get_string_from_utf8()
var snapshot: Variant = parse_progression_json(text)
if snapshot == null:
var reason_json := "non-JSON body or schemaVersion mismatch"
push_warning("SkillProgressionClient: %s" % reason_json)
progression_sync_failed.emit(reason_json)
_try_flush_pending_sync()
return
progression_received.emit(snapshot as Dictionary)
_try_flush_pending_sync()