summaryrefslogtreecommitdiff
path: root/scripts/ServerBrowser.gd
blob: 41f478a738cfb0666f1dd79e5144abfaad9d3fa4 (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
extends Control

onready var ws_client = $GameCoordinator
onready var game_list = $GameList

enum { ANY, GAME_LIST, HOST_RESPONSE, JOIN_RESPONSE, PASSWORD_RESPONSE }

var game_ids = []

# change this!!!
var game_coordinator_url = "ws://192.168.7.112:8181"
var awaiting_connection = false
var expecting = []
var queued_messages = []

func _ready():
	refresh_game_list()
	$RefreshButton.connect("pressed", self, "refresh_game_list")
	$HostPopup/Control/PrivateToggle.connect("toggled", self, "toggle_password_vis")
	$Username.connect("text_changed", $HostPopup/Control/GameName, "set_text")
	$HostPopup/Control/PlayerCount.connect("item_selected", self, "test_select_signal")
	

func test_select_signal(index):
	print("selected %d" % index)

func join_game():
	$HostPopup.visible = false
	$HostButton.disabled = true
	$RefreshButton.disabled = true
	var message = { "type" : "join_lobby" }
	if ws_client.state != 2:
		ws_client.sock_connect_to_url(game_coordinator_url)
		queued_messages.push_back( message )
		awaiting_connection = true
	else:
		ws_client.send_json( message )

func host_game():
	pass

func refresh_game_list():
	$JoinButton.disabled = true
	game_ids.clear()
	game_list.clear()

	var message = {"type" : "list_open_lobbies"}
	if ws_client.state != 2:
		ws_client.sock_connect_to_url(game_coordinator_url)
		ws_client.send_json( message )
	else:
		ws_client.send_json( message )

func add_games_to_list(games):
	for game in games:
		var game_str = game["lobby_name"] + " (" + str(int(game["current_players"])) + "/" + str(int(game["max_players"])) + ") (" +game["state"]+ ")" + (" (PRIVATE)" if game["private"] else "") 
		game_list.add_item( game_str, null, true if game["state"] == "LOBBY" else false )
		game_ids.append( game["id"] )

func toggle_password_vis(pressed):
	$HostPopup/Control/PlayerCount.select(3)
	$HostPopup/Control/Password.visible = pressed
	

func _process(_delta):
	$GameCoordinatorStatus.text = "Game Coordinator Connection: " + str(ws_client.state)
	if ws_client.state == 2:
		var recv_message = ws_client.receive(true) # true argument means expect + convert to JSON
		if recv_message:
			if "type" in recv_message and recv_message["type"] == "lobby_list":
				add_games_to_list(recv_message["lobbies"])