Files
MusicPlayer/http_transport.gd
T
Sansan 56e9fe66d3 Move server calls to MusicServerClient and give players stable ids
Server requests move out of Settings into a new MusicServerClient
autoload with one function per endpoint. It sends requests through a
swappable HttpTransport so tests can fake the server, and it frees its
HTTPRequest nodes after use.

Players now have a UUID that never changes. Their position in
Settings.player_array is only used for the Alt+<number> keybinds.
Previously the position doubled as the id, so removing a player made
points and wins land on the wrong player, and could delete another
player's point from a song.

Also fixes:
- format_time showed 59.6 seconds as "00:60"
- searches containing regex characters such as "Q*bert" broke search
- SongObject and PlayerObject were Nodes that were never freed; they
  are now RefCounted
- search matching is split out of the UI code into find_matches()
2026-10-06 08:32:55 +02:00

38 lines
1.3 KiB
GDScript

class_name HttpTransport
extends RefCounted
## Sends HTTP requests for [code]MusicServerClient[/code] using [HTTPRequest] nodes.
##
## Tests subclass this to answer requests without a network connection.
var _owner: Node
func _init(owner: Node) -> void:
_owner = owner
## Requests [param url] and calls [param on_complete] with
## [code](response_code: int, body: PackedByteArray)[/code]. Connection errors are
## reported with response code 0.
func request(url: String, method: int, on_complete: Callable) -> void:
var http_request: HTTPRequest = HTTPRequest.new()
_owner.add_child(http_request)
http_request.request_completed.connect(
func(
result: int,
response_code: int,
_headers: PackedStringArray,
body: PackedByteArray,
) -> void:
http_request.queue_free()
on_complete.call(response_code if result == HTTPRequest.RESULT_SUCCESS else 0, body)
)
var headers: PackedStringArray = []
if method == HTTPClient.METHOD_PUT:
headers.append("Content-Type: application/json")
var error: int = http_request.request(url, headers, method)
if error != OK:
http_request.queue_free()
push_error("Could not send request to %s (error %d)" % [url, error])
on_complete.call(0, PackedByteArray())