66 lines
2.1 KiB
GDScript
66 lines
2.1 KiB
GDScript
extends Node
|
|
|
|
## NS-18: POST InteractionRequest; prints server allow/deny to Output (prototype).
|
|
|
|
const ProtoIx = preload("res://scripts/prototype_interaction_constants.gd")
|
|
|
|
@export var base_url: String = "http://127.0.0.1:5253"
|
|
@export var dev_player_id: String = "dev-local-1"
|
|
|
|
var _http: HTTPRequest
|
|
var _busy: bool = false
|
|
|
|
|
|
func _ready() -> void:
|
|
_http = HTTPRequest.new()
|
|
add_child(_http)
|
|
_http.request_completed.connect(_on_request_completed)
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
# Use _input: _unhandled_input often misses keys in the embedded Game dock / focus quirks.
|
|
# `interact` is in project.godot; KEY_E fallback for odd layouts.
|
|
if event.is_action_pressed("interact"):
|
|
_post_interact()
|
|
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()
|
|
|
|
|
|
func _base_root() -> String:
|
|
return base_url.strip_edges().rstrip("/")
|
|
|
|
|
|
func _post_interact() -> 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": ProtoIx.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])
|