summaryrefslogtreecommitdiff
path: root/UI/GameLobby/game_lobby.gd
blob: f80015bd263d1fff920321ad8db2f31150f25821 (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
extends Control


var games := []

var refresh_interval := 15.0

@onready var lobby_game_scene := preload("res://UI/GameLobby/lobby_game.tscn")


func _ready() -> void:
	%GamesList.get_child(0).queue_free()
	
	%ServerUrl.text = GameLobby.server_base_url
	
	if await check_game_lobby_available():
		get_server_list()
		$RefreshTimer.wait_time = refresh_interval
		$RefreshTimer.start()
	else:
		%ServerNotReachable.text = %ServerNotReachable.text.replace("%SERVER%", GameLobby.server_base_url)


func check_game_lobby_available():
	var is_available = await GameLobby.check_available()
	
	%ServerNotReachable.visible = !is_available
	
	return is_available


func get_server_list():
	var response = await GameLobby.request_get("/get-list")
	var result = response.parse_json()
	print(result)
	
	if result["success"]:
		for node in %GamesList.get_children():
			node.queue_free()
		
		if result["data"].size() > 0:
			%NoGamesFound.visible = false
			
			for game in result["data"]:
				var lobby_game = lobby_game_scene.instantiate()
				lobby_game.game_id = game["id"]
				lobby_game.game_name = game["name"]
				%GamesList.add_child(lobby_game)
		else:
			%NoGamesFound.visible = true


func _on_back_button_pressed() -> void:
	get_tree().change_scene_to_file("res://UI/Start.tscn")


func _on_refresh_pressed() -> void:
	if await check_game_lobby_available():
		get_server_list()
		$RefreshTimer.start(refresh_interval)


func _on_refresh_timer_timeout() -> void:
	get_server_list()


func _on_set_server_pressed() -> void:
	var config = Client.get_config()
	config.set_value(
		"general",
		"game_lobby_server_base_url",
		%ServerUrl.text
	)
	config.save("user://config")
	
	GameLobby.server_base_url = %ServerUrl.text