neon-sprawl/client/scripts/faction_standing_client.gd

158 lines
4.5 KiB
GDScript

extends Node
## NEO-142: prototype HTTP client for [code]GET /game/players/{id}/faction-standing[/code] (NEO-139).
signal faction_standing_received(snapshot: Dictionary)
signal faction_standing_sync_failed(reason: String)
const SCHEMA_VERSION := 1
const GRID_OPERATORS_ID := "prototype_faction_grid_operators"
const RUST_COLLECTIVE_ID := "prototype_faction_rust_collective"
const _DISPLAY_NAMES: Dictionary = {
GRID_OPERATORS_ID: "Grid Operators",
RUST_COLLECTIVE_ID: "Rust Collective",
}
@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/faction-standing" % [_base_root(), _player_path_segment()]
var err: Error = _http.request(url)
if err != OK:
var reason := "GET failed to start (%s)" % err
push_warning("FactionStandingClient: %s" % reason)
_busy = false
faction_standing_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()
static func prototype_faction_ids_in_order() -> Array:
return [GRID_OPERATORS_ID, RUST_COLLECTIVE_ID]
static func display_name_for(faction_id: String) -> String:
var fid := faction_id.strip_edges()
if _DISPLAY_NAMES.has(fid):
return str(_DISPLAY_NAMES[fid])
return _title_case_token(fid)
static func standing_for(faction_id: String, snapshot: Dictionary = {}) -> int:
var row: Dictionary = faction_row(faction_id, snapshot)
if row.is_empty():
return 0
return int(row.get("standing", 0))
static func faction_row(faction_id: String, snapshot: Dictionary = {}) -> Dictionary:
var factions: Variant = snapshot.get("factions", null)
if factions == null or not factions is Array:
return {}
var fid := faction_id.strip_edges()
for row_variant in factions as Array:
if not row_variant is Dictionary:
continue
var row: Dictionary = row_variant
if str(row.get("id", "")) == fid:
return row
return {}
static func parse_faction_standing_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
if str(root.get("playerId", "")).strip_edges().is_empty():
return null
var factions: Variant = root.get("factions", null)
if factions == null or not factions is Array:
return null
return root
static func _title_case_token(token: String) -> String:
var trimmed := token.strip_edges()
if trimmed.is_empty():
return trimmed
return trimmed.substr(0, 1).to_upper() + trimmed.substr(1)
func _base_root() -> String:
return base_url.strip_edges().rstrip("/")
func _player_path_segment() -> String:
return dev_player_id.strip_edges()
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("FactionStandingClient: %s" % reason)
faction_standing_sync_failed.emit(reason)
_try_flush_pending_sync()
return
if response_code == 404:
var reason404 := "HTTP 404 (player unknown)"
push_warning("FactionStandingClient: %s" % reason404)
faction_standing_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("FactionStandingClient: %s" % reason_code)
faction_standing_sync_failed.emit(reason_code)
_try_flush_pending_sync()
return
var text := body.get_string_from_utf8()
var snapshot: Variant = parse_faction_standing_json(text)
if snapshot == null:
var reason_json := "non-JSON body or schemaVersion mismatch"
push_warning("FactionStandingClient: %s" % reason_json)
faction_standing_sync_failed.emit(reason_json)
_try_flush_pending_sync()
return
faction_standing_received.emit(snapshot as Dictionary)
_try_flush_pending_sync()