summaryrefslogtreecommitdiff
path: root/UI/GameLobby/game_lobby.gd
blob: 9f13882b464d522854ee3cc04c0d620d4512acd8 (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
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()
	
	if await check_game_lobby_available():
		get_server_list()
		$RefreshTimer.wait_time = refresh_interval
		$RefreshTimer.start()
	else:
		%ServerNotReachable.text = %ServerNotReachable.text.replace("%SERVER%", Client.game_lobby_url)


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


func get_server_list():
	var response = await Client.request("%s/?get-list" % [Client.game_lobby_url], [], HTTPClient.METHOD_GET)
	var result = response.parse_json()
	print(result)
	
	if result["success"]:
		for node in %GamesList.get_children():
			node.queue_free()
		
		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)


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()