diff options
Diffstat (limited to 'scripts/ServerBrowser.gd')
| -rw-r--r-- | scripts/ServerBrowser.gd | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/scripts/ServerBrowser.gd b/scripts/ServerBrowser.gd new file mode 100644 index 0000000..2a1492e --- /dev/null +++ b/scripts/ServerBrowser.gd @@ -0,0 +1,71 @@ +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://127.0.0.1:8181" +var awaiting_connection = false +var expecting = [] +var queued_messages = [] + +func _ready(): + refresh_game_list() + $RefreshButton.connect("pressed", self, "refresh_game_list") + +func join_game(): + $HostPopup.visible = false + $HostButton.disabled = true + $RefreshButton.disabled = true + var message = { "type" : "join_game" } + 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_games"} + 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 add_games_to_list(games): + for game in games: + var game_str = game["game_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 handle_gc_message(msg): + if msg == null or msg.error: return + msg = msg.result + if msg["type"] == "game_list": + add_games_to_list(msg["games"]) + ws_client.send_json({"type" : "ack"}) + if msg["type"] == "error": + print(msg["message"]) + +func _process(_delta): + $GameCoordinatorStatus.text = "Game Coordinator Connection: " + str(ws_client.state) + if ws_client.state == 2: + if awaiting_connection: + awaiting_connection = false + for queued_message in queued_messages: + ws_client.send_json( queued_message ) + + handle_gc_message(ws_client.receive(true)) |
