aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authortpart <tpart120@proton.me>2024-09-27 21:10:52 +0200
committertpart <tpart120@proton.me>2024-09-27 21:10:58 +0200
commitb68dc5d1d229e8bcdf655c99daf3a9e6ecac8aa7 (patch)
tree5c92e2c82ae07a6489564fd3bb8b7f36e2231642 /client
parent6fddd0ebf9346214798fee5f1c26733e55e8056d (diff)
downloadhurrycurry-b68dc5d1d229e8bcdf655c99daf3a9e6ecac8aa7.tar
hurrycurry-b68dc5d1d229e8bcdf655c99daf3a9e6ecac8aa7.tar.bz2
hurrycurry-b68dc5d1d229e8bcdf655c99daf3a9e6ecac8aa7.tar.zst
Load server list with singleton (Fixes #181)
Diffstat (limited to 'client')
-rw-r--r--client/menu/main.gd1
-rw-r--r--client/menu/play.gd30
-rw-r--r--client/menu/play.tscn2
-rw-r--r--client/project.godot1
-rw-r--r--client/server_list.gd49
-rw-r--r--client/server_list.tscn8
6 files changed, 70 insertions, 21 deletions
diff --git a/client/menu/main.gd b/client/menu/main.gd
index 9b809776..997653c9 100644
--- a/client/menu/main.gd
+++ b/client/menu/main.gd
@@ -24,6 +24,7 @@ func _ready():
if OS.has_feature("web"):
quit_button.hide()
Sound.play_music("MainMenu")
+ ServerList.fetch_server_list()
func _menu_cover(state):
$side.visible = not state
diff --git a/client/menu/play.gd b/client/menu/play.gd
index 65da0372..1fe19224 100644
--- a/client/menu/play.gd
+++ b/client/menu/play.gd
@@ -28,31 +28,20 @@ var url_regex: RegEx = RegEx.new()
@onready var server_connect = $side/margin/options/second/server/connect
func _ready():
- super()
url_regex.compile("^(?:(ws|wss)://)?([^:]+)(?::([0-9]+))?$")
if OS.has_feature("web"):
server.hide()
connect_uri.text = Global.get_profile("last_server_url")
Sound.play_music("MainMenu")
- update_server_list()
-
-func update_server_list():
- server_list_loading.visible = true
- req.request_completed.connect(_on_request_completed)
- req.request("https://hurrycurry-registry.metamuffin.org/v1/list", [
- "Accept: application/json",
- "User-Agent: Hurry Curry! %s" % Global.VERSION
- ])
+
+ ServerList.update_server_list.connect(update_server_list)
+ ServerList.update_loading.connect(update_server_list_loading)
+ update_server_list(ServerList.current_list)
+ update_server_list_loading(ServerList.loading)
+
+ super()
-func _on_request_completed(result: int, _response_code: int, _headers: PackedStringArray, body: PackedByteArray):
- server_list_loading.visible = false
- if result != 0:
- push_warning("Fetching server list failed with code %d" % result)
- return
- var json = JSON.parse_string(body.get_string_from_utf8())
- if json == null:
- push_error("Server list response invalid")
- return
+func update_server_list(json: Array):
for c in server_list.get_children():
c.queue_free()
for i in json:
@@ -65,6 +54,9 @@ func _on_request_completed(result: int, _response_code: int, _headers: PackedStr
b.pressed.connect(connect_to.bind(i.address[0]))
server_list.add_child(b)
+func update_server_list_loading(status: bool):
+ server_list_loading.visible = status
+
func _menu_cover(state):
$side.visible = not state
diff --git a/client/menu/play.tscn b/client/menu/play.tscn
index 8a705629..0b55f78e 100644
--- a/client/menu/play.tscn
+++ b/client/menu/play.tscn
@@ -118,8 +118,6 @@ alignment = 0
[node name="VBoxContainer" type="VBoxContainer" parent="side/margin/options/second"]
layout_mode = 2
-[node name="HTTPRequest" type="HTTPRequest" parent="."]
-
[connection signal="text_changed" from="side/margin/options/second/connect/uri" to="." method="_on_uri_text_changed"]
[connection signal="pressed" from="side/margin/options/second/connect/connect" to="." method="_on_connect_pressed"]
[connection signal="pressed" from="side/margin/options/second/server/control" to="." method="_on_server_pressed"]
diff --git a/client/project.godot b/client/project.godot
index ad3b9f5c..d1e85f05 100644
--- a/client/project.godot
+++ b/client/project.godot
@@ -24,6 +24,7 @@ Server="*res://server.gd"
Sound="*res://audio/sound.tscn"
DisableWrongJoypads="*res://disable_wrong_joypads.gd"
InputManager="*res://menu/settings/input/input_manager.gd"
+ServerList="*res://server_list.tscn"
[display]
diff --git a/client/server_list.gd b/client/server_list.gd
new file mode 100644
index 00000000..f509a34e
--- /dev/null
+++ b/client/server_list.gd
@@ -0,0 +1,49 @@
+# Hurry Curry! - a game about cooking
+# Copyright 2024 tpart
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, version 3 of the License only.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+extends Node
+
+signal update_loading(status: bool)
+signal update_server_list(list: Array)
+
+var current_list := []
+var loading := false
+
+@onready var req: HTTPRequest = $HTTPRequest
+
+func fetch_server_list():
+ if loading:
+ push_warning("Server list is already loading")
+ return
+ loading = true
+ update_loading.emit(true)
+ req.request_completed.connect(_on_request_completed)
+ req.request("https://hurrycurry-registry.metamuffin.org/v1/list", [
+ "Accept: application/json",
+ "User-Agent: Hurry Curry! %s" % Global.VERSION
+ ])
+
+func _on_request_completed(result: int, _response_code: int, _headers: PackedStringArray, body: PackedByteArray):
+ loading = false
+ update_loading.emit(false)
+ if result != 0:
+ push_warning("Fetching server list failed with code %d" % result)
+ return
+ var json = JSON.parse_string(body.get_string_from_utf8())
+ if json == null:
+ push_error("Server list response invalid")
+ return
+ current_list = json
+ update_server_list.emit(json)
diff --git a/client/server_list.tscn b/client/server_list.tscn
new file mode 100644
index 00000000..f43da77f
--- /dev/null
+++ b/client/server_list.tscn
@@ -0,0 +1,8 @@
+[gd_scene load_steps=2 format=3 uid="uid://3p61jpqt7acp"]
+
+[ext_resource type="Script" path="res://server_list.gd" id="1_a8xqo"]
+
+[node name="ServerList" type="Node"]
+script = ExtResource("1_a8xqo")
+
+[node name="HTTPRequest" type="HTTPRequest" parent="."]