118 lines
3.3 KiB
GDScript
118 lines
3.3 KiB
GDScript
extends Control
|
|
|
|
# TODO: rename to server browser
|
|
|
|
onready var is_loaded = false
|
|
|
|
onready var lobbies_grid = $v/body/p/lobbies
|
|
onready var zero_state = $v/body/p/lobbies/zero_state
|
|
onready var lobbies_label = $v/subhead/label
|
|
onready var display_name_edit = $v/subhead/display_name
|
|
|
|
onready var lobbies = {}
|
|
|
|
onready var lobby = preload("res://objects/lobby.tscn")
|
|
|
|
func _ready():
|
|
display_name_edit.text = Global.signaller_client.display_name
|
|
Global.signaller_client.connect("lobby_new", self, "_lobby_new")
|
|
Global.signaller_client.connect("lobby_delete", self, "_lobby_delete")
|
|
Global.signaller_client.connect("lobby_joined", self, "_lobby_joined")
|
|
Global.signaller_client.connect("lobby_left", self, "_lobby_left")
|
|
Global.signaller_client.connect("websocket_disconnected", self, "_signaller_disconnected")
|
|
Global.signaller_client.request_lobby_list()
|
|
|
|
if Global.check_onetime_flag("create-lobby"):
|
|
Global.signaller_client.create_lobby()
|
|
|
|
"""
|
|
var ls = []
|
|
for n in range(200):
|
|
ls.push_back({id = n, name = n, currentPlayers = n, maxPlayers = n, locked = false})
|
|
call_deferred("_lobby_new", ls)
|
|
"""
|
|
|
|
# TODO: add search
|
|
func _input(ev):
|
|
pass
|
|
# if ev is InputEventKey and ev.pressed:
|
|
# if ev.scancode == KEY_T:
|
|
# var ls = []
|
|
# for n in range(5):
|
|
# var j = n + lobbies.size()
|
|
# ls.push_back({id = j, name = j, currentPlayers = j, maxPlayers = j, locked = false})
|
|
# call_deferred("_lobby_new", ls)
|
|
|
|
func _signaller_disconnected():
|
|
Global.main_menu()
|
|
|
|
func _lobby_joined(_id, _peerId):
|
|
Global.lobby()
|
|
|
|
func _lobby_left(_id):
|
|
Global.lobby_browser()
|
|
|
|
func _on_back_pressed():
|
|
Global.main_menu()
|
|
|
|
func _on_create_lobby_pressed():
|
|
Global.signaller_client.create_lobby()
|
|
|
|
func _on_join_pressed():
|
|
var items = lobbies.get_selected_items()
|
|
if len(items) > 0:
|
|
Global.signaller_client.join_lobby(lobbies.get_item_metadata(items[0])["id"])
|
|
|
|
func _lobby_new(new_lobbies: Array):
|
|
# TODO: handle scrolling so that the user is never too jarred (like chat)
|
|
for lobby_index in range(len(new_lobbies)):
|
|
var lobby_data = new_lobbies[lobby_index]
|
|
var id = lobby_data["id"]
|
|
if lobbies.has(id):
|
|
_update_lobby(id, lobby_data)
|
|
else:
|
|
_add_lobby(id, lobby_data)
|
|
|
|
if Global.join_first_available_lobby:
|
|
Global.join_first_available_lobby = false
|
|
Global.signaller_client.join_lobby(id)
|
|
|
|
func _lobby_delete(id: String):
|
|
print("Lobby Deleted: %s" % id)
|
|
_delete_lobby(id)
|
|
|
|
func _add_lobby(id, lobby_data):
|
|
call_deferred("_update_lobbies_text")
|
|
print("New Lobby ", lobby_data)
|
|
# if lobby_data.currentPlayers > 0:
|
|
var new_lobby = lobby.instance()
|
|
new_lobby.set_with_dict(lobby_data)
|
|
lobbies_grid.add_child(new_lobby)
|
|
lobbies[id] = new_lobby
|
|
|
|
func _update_lobby(id, lobby_data):
|
|
call_deferred("_update_lobbies_text")
|
|
print("Updated Lobby ", lobby_data)
|
|
lobbies[id].set_with_dict(lobby_data)
|
|
|
|
func _delete_lobby(id):
|
|
call_deferred("_update_lobbies_text")
|
|
if lobbies.has(id):
|
|
print("Removing lobby...")
|
|
lobbies_grid.remove_child(lobbies[id])
|
|
lobbies[id].queue_free()
|
|
lobbies.erase(id)
|
|
|
|
func _update_lobbies_text():
|
|
var n = lobbies.size()
|
|
lobbies_label.text = "Active Lobbies: %d" % n
|
|
if n < 1:
|
|
if !lobbies_grid.is_a_parent_of(zero_state):
|
|
lobbies_grid.add_child(zero_state)
|
|
if n > 0:
|
|
if lobbies_grid.is_a_parent_of(zero_state):
|
|
lobbies_grid.remove_child(zero_state)
|
|
|
|
func _on_display_name_text_changed(new_text):
|
|
Global.signaller_client.display_name = new_text
|