aboutsummaryrefslogtreecommitdiff
path: root/client/system/server_list.gd
blob: 77b5ee0c0c22daa839b3b52d3a0f69d92cce214d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# 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)

enum Registry {
	MDNS = 0,
	GLOBAL = 1,
}

const MDNS_URL: String = "http://127.0.0.1:27033/v1/list"
const HEADERS: Array[String] = [
		"Accept: application/json",
		"User-Agent: Hurry Curry! %s" % Global.VERSION
	]

var current_list: Array[Array] = [[], []]
var loading := false
var mdns := HTTPRequest.new()
var reg := HTTPRequest.new()

# Fallback to http, since there seems to be a problem related to mbed tls in Godot.
# See: https://github.com/godotengine/godot/issues/96103
var using_http_fallback := false

var mdns_timer := Timer.new()
var reg_timer := Timer.new()
# after 30 minutes we stop fetching results to reduce server load
var timeout := Timer.new()

func _ready() -> void:
	add_child(mdns)
	add_child(reg)
	mdns_timer.wait_time = 5.
	mdns_timer.one_shot = false
	mdns_timer.timeout.connect(fetch_server_list.bind(Registry.MDNS))
	add_child(mdns_timer)
	reg_timer.wait_time = 60.
	reg_timer.one_shot = false
	reg_timer.timeout.connect(fetch_server_list.bind(Registry.GLOBAL))
	add_child(reg_timer)
	timeout.wait_time = 60. * 30.
	timeout.timeout.connect(func():
		stop()
	)
	add_child(timeout)
	mdns.request_completed.connect(_on_request_completed.bind(Registry.MDNS))
	reg.request_completed.connect(_on_request_completed.bind(Registry.GLOBAL))

func fetch_server_list(registry: Registry) -> void:
	match registry:
		Registry.MDNS:
			if Settings.read("online.use_discover"):
				match Discover.state:
					Service.State.STOPPED: Discover.start()
					Service.State.RUNNING: mdns.request(MDNS_URL, HEADERS)
		Registry.GLOBAL:
			if Settings.read("online.use_registry"):
				var url: String = Settings.read("online.registry_url")
				url = url.replace("https:", "http:") if using_http_fallback else url
				reg.request(url + "/v1/list", HEADERS)

	loading = true
	update_loading.emit(true)

func _on_request_completed(result: int, _response_code: int, _headers: PackedStringArray, body: PackedByteArray, registry: Registry):
	loading = false
	update_loading.emit(false)
	if result != 0:
		push_warning("Fetching server list failed with code %d." % result)
		if !using_http_fallback:
			print("Retrying with http...")
			using_http_fallback = true
			fetch_server_list(Registry.GLOBAL)
		return
	var json = JSON.parse_string(body.get_string_from_utf8())
	if json == null:
		push_error("Server list response invalid")
		return
	current_list[registry] = json
	update_server_list.emit(current_list)

func start() -> void:
	timeout.stop()
	timeout.start()
	mdns_timer.start()
	fetch_server_list(Registry.MDNS)
	reg_timer.start()
	fetch_server_list(Registry.GLOBAL)

func stop() -> void:
	timeout.stop()
	mdns_timer.stop()
	reg_timer.stop()

func one_shot() -> void:
	start()
	stop()