neon-sprawl/client/scripts/interaction_request_client.gd

76 lines
2.5 KiB
GDScript

extends Node
## NS-18 / NEO-25: POST InteractionRequest; prints server allow/deny to Output (prototype).
## [kbd]E[/kbd] → [code]prototype_terminal[/code]; [kbd]R[/kbd] → [code]prototype_resource_node_alpha[/code]
## (stable contract ids — must match server registry). Key handling is delegated from
## [code]main.gd[/code] [method Node._unhandled_key_input] (see [method post_interact_terminal] /
## [method post_interact_resource]) so the embedded Game dock still receives E/R reliably.
const ID_TERMINAL := "prototype_terminal"
const ID_RESOURCE_NODE := "prototype_resource_node_alpha"
@export var base_url: String = "http://127.0.0.1:5253"
@export var dev_player_id: String = "dev-local-1"
## GdUnit: inject a mock transport (same pattern as [code]TargetSelectionClient[/code] test double).
@export var injected_http: Node = null
var _http: Node
var _busy: 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 post_interact_terminal() -> void:
_post_interact(ID_TERMINAL)
func post_interact_resource() -> void:
_post_interact(ID_RESOURCE_NODE)
func _base_root() -> String:
return base_url.strip_edges().rstrip("/")
func _post_interact(interactable_id: String) -> void:
if _busy:
return
_busy = true
var url := "%s/game/players/%s/interact" % [_base_root(), dev_player_id.strip_edges()]
var payload := {"schemaVersion": 1, "interactableId": interactable_id}
var body := JSON.stringify(payload)
var headers := PackedStringArray(["Content-Type: application/json"])
var err: Error = _http.request(url, headers, HTTPClient.METHOD_POST, body)
if err != OK:
push_warning("InteractionRequestClient: POST failed to start (%s)" % err)
_busy = false
func _on_request_completed(
_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray
) -> void:
_busy = false
var text := body.get_string_from_utf8()
if response_code == 200:
var parsed: Variant = JSON.parse_string(text)
if parsed is Dictionary:
var d: Dictionary = parsed
var allowed: bool = bool(d.get("allowed", false))
var reason: Variant = d.get("reasonCode", null)
if allowed:
print("Interaction: allowed=true (reasonCode omitted on success)")
else:
print("Interaction: allowed=false reasonCode=%s" % str(reason))
return
print("Interaction: HTTP %s body=%s" % [response_code, text])