79 lines
2.7 KiB
GDScript
79 lines
2.7 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).
|
|
|
|
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 _input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("interact"):
|
|
_post_interact(ID_TERMINAL)
|
|
elif event.is_action_pressed("interact_secondary"):
|
|
_post_interact(ID_RESOURCE_NODE)
|
|
elif event is InputEventKey:
|
|
var k := event as InputEventKey
|
|
if k.pressed and not k.echo and (k.physical_keycode == KEY_E or k.keycode == KEY_E):
|
|
_post_interact(ID_TERMINAL)
|
|
elif k.pressed and not k.echo and (k.physical_keycode == KEY_R or k.keycode == KEY_R):
|
|
_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])
|