diff --git a/modules/blazium_sdk/config.py b/modules/blazium_sdk/config.py index 58a59596d491..a973e6216753 100644 --- a/modules/blazium_sdk/config.py +++ b/modules/blazium_sdk/config.py @@ -12,8 +12,6 @@ def get_doc_classes(): "BlaziumClient", "LobbyInfo", "LobbyPeer", - "CreateLobbyResponse", - "CreateLobbyResult", "LobbyResponse", "LobbyResult", "ListLobbyResponse", diff --git a/modules/blazium_sdk/doc_classes/BlaziumClient.xml b/modules/blazium_sdk/doc_classes/BlaziumClient.xml index e10c4c97719e..7e57c0939945 100644 --- a/modules/blazium_sdk/doc_classes/BlaziumClient.xml +++ b/modules/blazium_sdk/doc_classes/BlaziumClient.xml @@ -4,8 +4,7 @@ An abstract base node used to connect to a blazium services. - An abstract base node used to connect to a blazium services. See implementations of it: - - [LobbyClient] + An abstract base node used to connect to a blazium services. diff --git a/modules/blazium_sdk/doc_classes/CreateLobbyResponse.xml b/modules/blazium_sdk/doc_classes/CreateLobbyResponse.xml deleted file mode 100644 index bdd6d9e125b2..000000000000 --- a/modules/blazium_sdk/doc_classes/CreateLobbyResponse.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - Response from a create lobby request. - - - Response from a create lobby request. Await on finished to get the [CreateLobbyResult]. - - - - - - - - Signal emitted when the request is finished. - - - - diff --git a/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml b/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml deleted file mode 100644 index aa8bc438a3ac..000000000000 --- a/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - A result from a [CreateLobbyResponse]. - - - A result from a [CreateLobbyResponse]. Contains either result or error. - - - - - - - - Returns true if there is an error. - - - - - - Gets the error message. - - - Gets the lobby name. - - - diff --git a/modules/blazium_sdk/doc_classes/ListLobbyResult.xml b/modules/blazium_sdk/doc_classes/ListLobbyResult.xml index 809e0a544f57..e87b0b173ee4 100644 --- a/modules/blazium_sdk/doc_classes/ListLobbyResult.xml +++ b/modules/blazium_sdk/doc_classes/ListLobbyResult.xml @@ -20,7 +20,7 @@ Gets the error message. - + Gets the lobbies. diff --git a/modules/blazium_sdk/doc_classes/LobbyClient.xml b/modules/blazium_sdk/doc_classes/LobbyClient.xml index 0beac4b9c347..8da1ef985166 100644 --- a/modules/blazium_sdk/doc_classes/LobbyClient.xml +++ b/modules/blazium_sdk/doc_classes/LobbyClient.xml @@ -13,25 +13,26 @@ - Connect to a Blazium Lobby Server using a game id. + Connect to a Blazium Lobby Server using the game_id and [member server_url]. - - - + + + + Create a lobby and become host. If you are already in a lobby, you cannot create one. You need to leave first. - Will generate either error signal or lobby_created. + Generates [signal lobby_created] signal. - - + + Joint a lobby. If you are already in a lobby, you cannot join another one. You need to leave first. - Will generate either error signal or lobby_joined. + Generates [signal lobby_joined]. @@ -39,13 +40,14 @@ Kick a peer. You need to be host to do so. + Generates [signal peer_left] signal with kicked set to true. Leave a lobby. You need to be in a lobby to leave one. - Will generate either error signal or lobby_left. + Generates [signal lobby_left]. @@ -54,7 +56,6 @@ Lists all lobbies. - Will generate either error signal or lobby_list. @@ -62,7 +63,7 @@ Send data either to the host, or if you are host send data to all peers. - Generates received_data signal. + Generates [signal received_data] signal. @@ -71,48 +72,67 @@ Send data either to a peer, works only if you are host. - Generates received_data_to signal. + Generates [signal received_data_to] signal. Ready up in the lobby. You need to be in a lobby and unready to run this. - Will generate either error signal or peer_ready. + Generates [signal peer_ready]. Ready up in the lobby. You need to be in a lobby and ready to run this. - Will generate either error signal or peer_unready. + Generates [signal peer_unready]. Seals the lobby. You need to be the host to do this and the lobby needs to be unsealed. - Will generate either error signal or lobby_sealed. + Generates [signal lobby_sealed]. + + + + + + + Set your peer name. + Generates [signal peer_named] signal if you are in lobby. Unseals the lobby. You need to be the host to do this and the lobby needs to be sealed. - Will generate either error signal or lobby_unsealed. + Generates [signal lobby_unsealed]. - + View data from a lobby. Returns lobby settings and peers. - Will generate either error signal or lobby_view. + + True if the client is connected, else false. + + + The current lobby. + + + The current peer. + + + The lobby peers. + Set to what url this lobby should connect to. @@ -126,14 +146,21 @@ Signals a log from a command. + + + Disconnects from the lobby. + + - + + Signal generated after a lobby is created. - + + Signal generated after you joint a lobby. @@ -154,33 +181,32 @@ - + Signal generated after a peer joins the lobby. - + Signal generated after a peer leaves the lobby. - - + Signal generated after a peer names himself. - + Signal generated after a peer is ready. - + Signal generated after a peer is unready. diff --git a/modules/blazium_sdk/doc_classes/LobbyInfo.xml b/modules/blazium_sdk/doc_classes/LobbyInfo.xml index f78004bfc3a7..72fb1cb0344d 100644 --- a/modules/blazium_sdk/doc_classes/LobbyInfo.xml +++ b/modules/blazium_sdk/doc_classes/LobbyInfo.xml @@ -1,5 +1,5 @@ - + Lobby information returned after a view lobby request. @@ -12,9 +12,21 @@ The host id of the lobby. + + The name of the host of the lobby. + + + The id of the lobby. + + + The name of the lobby. + The maximum number of players allowed in the lobby. + + The number of players currently in the lobby. + Whether the lobby is sealed. diff --git a/modules/blazium_sdk/doc_classes/LobbyPeer.xml b/modules/blazium_sdk/doc_classes/LobbyPeer.xml index 9c0ba6ad049c..858ecccc7dee 100644 --- a/modules/blazium_sdk/doc_classes/LobbyPeer.xml +++ b/modules/blazium_sdk/doc_classes/LobbyPeer.xml @@ -1,5 +1,5 @@ - + Lobby peers information returned after a view lobby request. @@ -12,7 +12,7 @@ Identifier of the peer. - + Name of the peer. diff --git a/modules/blazium_sdk/doc_classes/ViewLobbyResult.xml b/modules/blazium_sdk/doc_classes/ViewLobbyResult.xml index 34cf7afb8069..a5934ebff323 100644 --- a/modules/blazium_sdk/doc_classes/ViewLobbyResult.xml +++ b/modules/blazium_sdk/doc_classes/ViewLobbyResult.xml @@ -9,12 +9,6 @@ - - - - Gets the lobby info. - - @@ -26,6 +20,9 @@ Gets the error message. + + Gets the lobby. + Gets the peers. diff --git a/modules/blazium_sdk/lobby/lobby_client.cpp b/modules/blazium_sdk/lobby/lobby_client.cpp index 920583fab9d1..50b927915adc 100644 --- a/modules/blazium_sdk/lobby/lobby_client.cpp +++ b/modules/blazium_sdk/lobby/lobby_client.cpp @@ -31,7 +31,10 @@ #include "./lobby_client.h" #include "scene/main/node.h" LobbyClient::LobbyClient() { + lobby.instantiate(); + peer.instantiate(); _socket = Ref(WebSocketPeer::create()); + set_process_internal(false); } LobbyClient::~LobbyClient() { @@ -42,14 +45,27 @@ LobbyClient::~LobbyClient() { void LobbyClient::_bind_methods() { ClassDB::bind_method(D_METHOD("set_server_url", "server_url"), &LobbyClient::set_server_url); ClassDB::bind_method(D_METHOD("get_server_url"), &LobbyClient::get_server_url); + ClassDB::bind_method(D_METHOD("get_connected"), &LobbyClient::get_connected); + ClassDB::bind_method(D_METHOD("get_lobby"), &LobbyClient::get_lobby); + ClassDB::bind_method(D_METHOD("get_peer"), &LobbyClient::get_peer); + ClassDB::bind_method(D_METHOD("get_peers"), &LobbyClient::get_peers); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "server_url", PROPERTY_HINT_NONE, ""), "set_server_url", "get_server_url"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connected"), "", "get_connected"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "lobby", PROPERTY_HINT_RESOURCE_TYPE, "LobbyInfo"), "", "get_lobby"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), "", "get_peer"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "peers", PROPERTY_HINT_ARRAY_TYPE, "LobbyPeer"), "", "get_peers"); + ADD_PROPERTY_DEFAULT("peers", TypedArray()); + ADD_PROPERTY_DEFAULT("peer", Ref()); + ADD_PROPERTY_DEFAULT("lobby", Ref()); // Register methods ClassDB::bind_method(D_METHOD("connect_to_lobby", "game_id"), &LobbyClient::connect_to_lobby); - ClassDB::bind_method(D_METHOD("create_lobby", "max_players", "password"), &LobbyClient::create_lobby, DEFVAL(4), DEFVAL("")); - ClassDB::bind_method(D_METHOD("join_lobby", "lobby_name", "password"), &LobbyClient::join_lobby, DEFVAL("")); + ClassDB::bind_method(D_METHOD("set_peer_name", "peer_name"), &LobbyClient::set_peer_name); + ClassDB::bind_method(D_METHOD("create_lobby", "title", "max_players", "password"), &LobbyClient::create_lobby, DEFVAL(4), DEFVAL("")); + ClassDB::bind_method(D_METHOD("join_lobby", "lobby_id", "password"), &LobbyClient::join_lobby, DEFVAL("")); ClassDB::bind_method(D_METHOD("leave_lobby"), &LobbyClient::leave_lobby); ClassDB::bind_method(D_METHOD("list_lobby", "start", "count"), &LobbyClient::list_lobby, DEFVAL(0), DEFVAL(10)); - ClassDB::bind_method(D_METHOD("view_lobby", "lobby_name", "password"), &LobbyClient::view_lobby, DEFVAL("")); + ClassDB::bind_method(D_METHOD("view_lobby", "lobby_id", "password"), &LobbyClient::view_lobby, DEFVAL(""), DEFVAL("")); ClassDB::bind_method(D_METHOD("kick_peer", "peer_id"), &LobbyClient::kick_peer); ClassDB::bind_method(D_METHOD("lobby_ready"), &LobbyClient::lobby_ready); ClassDB::bind_method(D_METHOD("lobby_unready"), &LobbyClient::lobby_unready); @@ -59,29 +75,36 @@ void LobbyClient::_bind_methods() { ClassDB::bind_method(D_METHOD("lobby_data_to", "data", "target_peer"), &LobbyClient::lobby_data_to); // Register signals - ADD_SIGNAL(MethodInfo("peer_named", PropertyInfo(Variant::STRING, "peer"), PropertyInfo(Variant::STRING, "name"))); + ADD_SIGNAL(MethodInfo("disconnected_from_lobby")); + ADD_SIGNAL(MethodInfo("peer_named", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); ADD_SIGNAL(MethodInfo("received_data", PropertyInfo(Variant::STRING, "data"))); ADD_SIGNAL(MethodInfo("received_data_to", PropertyInfo(Variant::STRING, "data"))); - ADD_SIGNAL(MethodInfo("lobby_created", PropertyInfo(Variant::STRING, "lobby"))); - ADD_SIGNAL(MethodInfo("lobby_joined", PropertyInfo(Variant::STRING, "lobby"))); + ADD_SIGNAL(MethodInfo("lobby_created", PropertyInfo(Variant::OBJECT, "lobby", PROPERTY_HINT_RESOURCE_TYPE, "LobbyInfo"), PropertyInfo(Variant::ARRAY, "peers", PROPERTY_HINT_ARRAY_TYPE, "LobbyPeer"))); + ADD_SIGNAL(MethodInfo("lobby_joined", PropertyInfo(Variant::OBJECT, "lobby", PROPERTY_HINT_RESOURCE_TYPE, "LobbyInfo"), PropertyInfo(Variant::ARRAY, "peers", PROPERTY_HINT_ARRAY_TYPE, "LobbyPeer"))); ADD_SIGNAL(MethodInfo("lobby_left")); ADD_SIGNAL(MethodInfo("lobby_sealed")); ADD_SIGNAL(MethodInfo("lobby_unsealed")); - ADD_SIGNAL(MethodInfo("peer_joined", PropertyInfo(Variant::STRING, "peer"))); - ADD_SIGNAL(MethodInfo("peer_left", PropertyInfo(Variant::STRING, "peer"), PropertyInfo(Variant::BOOL, "kicked"))); - ADD_SIGNAL(MethodInfo("peer_ready", PropertyInfo(Variant::STRING, "peer"))); - ADD_SIGNAL(MethodInfo("peer_unready", PropertyInfo(Variant::STRING, "peer"))); + ADD_SIGNAL(MethodInfo("peer_joined", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); + ADD_SIGNAL(MethodInfo("peer_left", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::BOOL, "kicked"))); + ADD_SIGNAL(MethodInfo("peer_ready", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); + ADD_SIGNAL(MethodInfo("peer_unready", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); ADD_SIGNAL(MethodInfo("append_log", PropertyInfo(Variant::STRING, "command"), PropertyInfo(Variant::STRING, "info"), PropertyInfo(Variant::STRING, "logs"))); } -bool LobbyClient::connect_to_lobby(const String &game_id) { +bool LobbyClient::connect_to_lobby(const String &p_game_id) { + if (connected) { + return true; + } String lobby_url = get_server_url(); - String url = lobby_url + "?gameID=" + game_id; + String url = lobby_url + "?gameID=" + p_game_id; Error err = _socket->connect_to_url(url); if (err != OK) { + set_process_internal(false); emit_signal("append_log", "error", "Unable to connect to lobby server at: " + url); + connected = false; return false; } + connected = true; set_process_internal(true); emit_signal("append_log", "connect_to_lobby", "Connected to: " + url); return true; @@ -91,38 +114,39 @@ String LobbyClient::_increment_counter() { return String::num(_counter++); } -Ref LobbyClient::create_lobby(int max_players, const String &password) { +Ref LobbyClient::create_lobby(const String &p_lobby_name, int p_max_players, const String &p_password) { String id = _increment_counter(); Dictionary command; command["command"] = "create_lobby"; Dictionary data_dict; command["data"] = data_dict; - data_dict["max_players"] = max_players; - data_dict["password"] = password; + data_dict["name"] = p_lobby_name; + data_dict["max_players"] = p_max_players; + data_dict["password"] = p_password; data_dict["id"] = id; Array command_array; - Ref response; + Ref response; response.instantiate(); - command_array.push_back(CREATE_LOBBY); + command_array.push_back(LOBBY_VIEW); command_array.push_back(response); _commands[id] = command_array; _send_data(command); return response; } -Ref LobbyClient::join_lobby(const String &lobby_name, const String &password) { +Ref LobbyClient::join_lobby(const String &p_lobby_id, const String &p_password) { String id = _increment_counter(); Dictionary command; command["command"] = "join_lobby"; Dictionary data_dict; command["data"] = data_dict; - data_dict["lobby_name"] = lobby_name; - data_dict["password"] = password; + data_dict["lobby_id"] = p_lobby_id; + data_dict["password"] = p_password; data_dict["id"] = id; Array command_array; - Ref response; + Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_VIEW); command_array.push_back(response); _commands[id] = command_array; _send_data(command); @@ -139,21 +163,21 @@ Ref LobbyClient::leave_lobby() { Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); return response; } -Ref LobbyClient::list_lobby(int start, int count) { +Ref LobbyClient::list_lobby(int p_start, int p_count) { String id = _increment_counter(); Dictionary command; command["command"] = "list_lobby"; Dictionary data_dict; data_dict["id"] = id; - data_dict["start"] = start; - data_dict["count"] = count; + data_dict["start"] = p_start; + data_dict["count"] = p_count; command["data"] = data_dict; Array command_array; Ref response; @@ -165,14 +189,18 @@ Ref LobbyClient::list_lobby(int start, int count return response; } -Ref LobbyClient::view_lobby(const String &lobby_name, const String &password) { +Ref LobbyClient::view_lobby(const String &p_lobby_id, const String &p_password) { String id = _increment_counter(); Dictionary command; command["command"] = "view_lobby"; Dictionary data_dict; command["data"] = data_dict; - data_dict["lobby_name"] = lobby_name; - data_dict["password"] = password; + if (p_lobby_id.is_empty()) { + data_dict["lobby_id"] = lobby->get_id(); + } else { + data_dict["lobby_id"] = p_lobby_id; + } + data_dict["password"] = p_password; data_dict["id"] = id; Array command_array; Ref response; @@ -184,18 +212,18 @@ Ref LobbyClient::view_lobby(const String &lobby_ return response; } -Ref LobbyClient::kick_peer(const String &peer_id) { +Ref LobbyClient::kick_peer(const String &p_peer_id) { String id = _increment_counter(); Dictionary command; command["command"] = "kick_peer"; Dictionary data_dict; command["data"] = data_dict; - data_dict["peer_id"] = peer_id; + data_dict["peer_id"] = p_peer_id; data_dict["id"] = id; Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); @@ -212,7 +240,7 @@ Ref LobbyClient::lobby_ready() { Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); @@ -229,25 +257,25 @@ Ref LobbyClient::lobby_unready() { Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); return response; } -Ref LobbyClient::set_peer_name(const String &peer_name) { +Ref LobbyClient::set_peer_name(const String &p_peer_name) { String id = _increment_counter(); Dictionary command; command["command"] = "set_name"; Dictionary data_dict; - data_dict["peer_name"] = peer_name; + data_dict["name"] = p_peer_name; data_dict["id"] = id; command["data"] = data_dict; Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); @@ -264,7 +292,7 @@ Ref LobbyClient::seal_lobby() { Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); @@ -281,44 +309,44 @@ Ref LobbyClient::unseal_lobby() { Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); return response; } -Ref LobbyClient::lobby_data(const String &peer_data) { +Ref LobbyClient::lobby_data(const String &p_peer_data) { String id = _increment_counter(); Dictionary command; command["command"] = "lobby_data"; Dictionary data_dict; - data_dict["peer_data"] = peer_data; + data_dict["peer_data"] = p_peer_data; data_dict["id"] = id; command["data"] = data_dict; Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); return response; } -Ref LobbyClient::lobby_data_to(const String &peer_data, const String &target_peer) { +Ref LobbyClient::lobby_data_to(const String &p_peer_data, const String &p_target_peer) { String id = _increment_counter(); Dictionary command; command["command"] = "data_to"; Dictionary data_dict; - data_dict["peer_data"] = peer_data; - data_dict["target_peer"] = target_peer; + data_dict["peer_data"] = p_peer_data; + data_dict["target_peer"] = p_target_peer; data_dict["id"] = id; command["data"] = data_dict; Array command_array; Ref response; response.instantiate(); - command_array.push_back(SIMPLE_REQUEST); + command_array.push_back(LOBBY_REQUEST); command_array.push_back(response); _commands[id] = command_array; _send_data(command); @@ -344,229 +372,216 @@ void LobbyClient::_notification(int p_what) { } } else if (state == WebSocketPeer::STATE_CLOSED) { emit_signal("append_log", "error", "WebSocket closed unexpectedly."); + emit_signal("disconnected_from_lobby"); + set_process_internal(false); + connected = false; } } break; } } -void LobbyClient::_send_data(const Dictionary &data_dict) { +void LobbyClient::_send_data(const Dictionary &p_data_dict) { if (_socket->get_ready_state() != WebSocketPeer::STATE_OPEN) { emit_signal("append_log", "error", "Socket is not ready."); return; } - _socket->send_text(JSON::stringify(data_dict)); + _socket->send_text(JSON::stringify(p_data_dict)); +} + +void update_peers(Dictionary p_data_dict, TypedArray &peers) { + Array peers_array = p_data_dict.get("peers", Array()); + TypedArray peers_info; + peers.clear(); + for (int i = 0; i < peers_array.size(); ++i) { + Ref peer = Ref(memnew(LobbyPeer)); + peer->set_dict(peers_array[i]); + peers.push_back(peer); + } +} + +// TODO optimize this +void sort_peers_by_id(TypedArray &peers) { + for (int i = 0; i < peers.size(); ++i) { + for (int j = i + 1; j < peers.size(); ++j) { + Ref peer_i = peers[i]; + Ref peer_j = peers[j]; + if (peer_i->get_id().casecmp_to(peer_j->get_id()) > 0) { + Ref temp = peers[i]; + peers[i] = peers[j]; + peers[j] = temp; + } + } + } } -void LobbyClient::_receive_data(const Dictionary &dict) { - String command = dict.get("command", "error"); - String message = dict.get("message", command); - Dictionary data_dict = dict.get("data", Dictionary()); +void LobbyClient::_receive_data(const Dictionary &p_dict) { + String command = p_dict.get("command", "error"); + String message = p_dict.get("message", command); + Dictionary data_dict = p_dict.get("data", Dictionary()); String message_id = data_dict.get("id", ""); Array command_array = _commands.get(message_id, Array()); _commands.erase(message_id); emit_signal("append_log", command, message); - if (command == "lobby_created") { - String lobby_name = data_dict.get("lobby_name", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - result->set_lobby_name(lobby_name); - response->emit_signal("finished", result); - } + if (command_array.size() == 2 && command != "error") { + int command_type = command_array[0]; + switch (command_type) { + case LOBBY_REQUEST: { + Ref response = command_array[1]; + if (response.is_valid()) { + Ref result = Ref(memnew(LobbyResponse::LobbyResult)); + response->emit_signal("finished", result); + } + } break; + case LOBBY_VIEW: { + Dictionary lobby_dict = data_dict.get("lobby", Dictionary()); + + // Iterate through peers and populate arrays + TypedArray peers_info; + update_peers(data_dict, peers_info); + sort_peers_by_id(peers_info); + Ref lobby_info = Ref(memnew(LobbyInfo)); + lobby_info->set_dict(lobby_dict); + if (lobby_info->get_id() == lobby->get_id()) { + // Update lobby info because we viewed our own lobby + lobby->set_dict(lobby_info->get_dict()); + peers = peers_info; + } + if (command_array.size() == 2) { + Ref response = command_array[1]; + if (response.is_valid()) { + Ref result; + result.instantiate(); + result->set_peers(peers_info); + result->set_lobby(lobby_info); + response->emit_signal("finished", result); + } + } + } break; } - emit_signal("lobby_created", lobby_name); + } + if (command == "peer_state") { + peer->set_id(data_dict.get("peer_id", "")); + } else if (command == "lobby_created") { + lobby->set_dict(data_dict.get("lobby", Dictionary())); + update_peers(data_dict, peers); + sort_peers_by_id(peers); + emit_signal("lobby_created", lobby, peers); } else if (command == "joined_lobby") { - String lobby_name = data_dict.get("lobby_name", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } - emit_signal("lobby_joined", lobby_name); + lobby->set_dict(data_dict.get("lobby", Dictionary())); + update_peers(data_dict, peers); + sort_peers_by_id(peers); + emit_signal("lobby_joined", lobby, peers); } else if (command == "lobby_left") { - // Either if you leave a lobby, or if you get kicked - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } + lobby->set_dict(Dictionary()); + peers.clear(); emit_signal("lobby_left"); } else if (command == "lobby_sealed") { - // Either if you seal a lobby, or if host seals - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } + Dictionary lobby_dict = data_dict.get("lobby", Dictionary()); + lobby->set_sealed(true); emit_signal("lobby_sealed"); } else if (command == "lobby_unsealed") { - // Either if you unseal a lobby, or if host unseals - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } + lobby->set_sealed(false); emit_signal("lobby_unsealed"); } else if (command == "lobby_list") { Array arr = data_dict.get("lobbies", Array()); - TypedArray lobbies = arr; + TypedArray lobbies_input = arr; + TypedArray lobbies_output; + for (int i = 0; i < lobbies_input.size(); ++i) { + Dictionary lobby_dict = lobbies_input[i]; + Ref lobby_info; + lobby_info.instantiate(); + lobby_info->set_dict(lobby_dict); + + lobbies_output.push_back(lobby_info); + } if (command_array.size() == 2) { Ref response = command_array[1]; if (response.is_valid()) { Ref result; result.instantiate(); - result->set_lobbies(lobbies); + result->set_lobbies(lobbies_output); response->emit_signal("finished", result); } } } else if (command == "lobby_view") { - Dictionary lobby_dict = data_dict.get("lobby", Dictionary()); - String host = lobby_dict.get("host", ""); - bool sealed = lobby_dict.get("sealed", false); - int max_players = lobby_dict.get("max_players", 0); - - // Iterate through peers and populate arrays - Array arr = data_dict["peers"]; - TypedArray peers; - for (int i = 0; i < arr.size(); ++i) { - Ref peer; - peer.instantiate(); - String peer_id = arr[i].get("id"); - String peer_name = arr[i].get("name"); - bool peer_ready = arr[i].get("ready"); - peer->set_id(peer_id); - peer->set_name(peer_name); - peer->set_ready(peer_ready); - peers.push_back(peer); - } - Ref lobby_info; - lobby_info.instantiate(); - lobby_info->set_host(host); - lobby_info->set_sealed(sealed); - lobby_info->set_max_players(max_players); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - result->set_peers(peers); - result->set_lobby_info(lobby_info); - response->emit_signal("finished", result); - } - } + // nothing for now } else if (command == "peer_name") { - // Either if you ready a lobby, or if someone else readies String peer_id = data_dict.get("peer_id", ""); String peer_name = data_dict.get("name", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); + if (peer->get_id() == peer_id) { + peer->set_peer_name(peer_name); + } + for (int i = 0; i < peers.size(); ++i) { + Ref updated_peer = peers[i]; + if (updated_peer->get_id() == peer_id) { + updated_peer->set_peer_name(peer_name); + // if the named peer is the host, update the host name + if (updated_peer->get_id() == lobby->get_host()) { + lobby->set_host_name(peer_name); + } + emit_signal("peer_named", updated_peer); + break; } } - emit_signal("peer_named", peer_id, peer_name); } else if (command == "peer_ready") { - // Either if you ready a lobby, or if someone else readies String peer_id = data_dict.get("peer_id", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); + if (peer->get_id() == peer_id) { + peer->set_ready(true); + } + for (int i = 0; i < peers.size(); ++i) { + Ref updated_peer = peers[i]; + if (updated_peer->get_id() == String(peer_id)) { + updated_peer->set_ready(true); + emit_signal("peer_ready", updated_peer); + break; } } - emit_signal("peer_ready", peer_id); } else if (command == "peer_unready") { - // Either if you unready a lobby, or if someone else unreadies String peer_id = data_dict.get("peer_id", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); + if (peer->get_id() == peer_id) { + peer->set_ready(false); + } + for (int i = 0; i < peers.size(); ++i) { + Ref updated_peer = peers[i]; + if (updated_peer->get_id() == String(data_dict.get("peer_id", ""))) { + updated_peer->set_ready(false); + emit_signal("peer_unready", updated_peer); + break; } } - emit_signal("peer_unready", peer_id); } else if (command == "peer_joined") { - String peer_id = data_dict.get("peer_id", ""); - String peer_name = data_dict.get("peer_name", ""); - emit_signal("peer_joined", peer_id, peer_name); + Ref joining_peer = Ref(memnew(LobbyPeer)); + Dictionary peer_dict = data_dict.get("peer", Dictionary()); + joining_peer->set_id(peer_dict.get("id", "")); + joining_peer->set_peer_name(peer_dict.get("name", "")); + peers.append(joining_peer); + sort_peers_by_id(peers); + lobby->set_players(peers.size()); + emit_signal("peer_joined", joining_peer); } else if (command == "peer_left") { - // Either if you kick a peer, or a peer leaves - String peer_id = data_dict.get("peer_id", ""); - bool kicked = data_dict.get("kicked", false); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); + for (int i = 0; i < peers.size(); ++i) { + Ref leaving_peer = peers[i]; + if (leaving_peer->get_id() == String(data_dict.get("peer_id", ""))) { + peers.remove_at(i); + lobby->set_players(peers.size()); + emit_signal("peer_left", leaving_peer, data_dict.get("kicked", false)); + break; } } - emit_signal("peer_left", peer_id, kicked); + sort_peers_by_id(peers); } else if (command == "lobby_data") { - String peer_data = data_dict.get("peer_data", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } - emit_signal("received_data", peer_data); + emit_signal("received_data", data_dict.get("peer_data", "")); } else if (command == "data_to") { - String peer_data = data_dict.get("peer_data", ""); - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } - emit_signal("received_data_to", peer_data); + emit_signal("received_data_to", data_dict.get("peer_data", "")); } else if (command == "lobby_data_sent") { - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } + // nothing for now } else if (command == "data_to_sent") { - if (command_array.size() == 2) { - Ref response = command_array[1]; - if (response.is_valid()) { - Ref result; - result.instantiate(); - response->emit_signal("finished", result); - } - } + // nothing for now } else if (command == "error") { if (command_array.size() == 2) { int command_type = command_array[0]; switch (command_type) { - case SIMPLE_REQUEST: { + case LOBBY_REQUEST: { Ref lobby_response = command_array[1]; if (lobby_response.is_valid()) { Ref result; @@ -584,15 +599,6 @@ void LobbyClient::_receive_data(const Dictionary &dict) { list_response->emit_signal("finished", result); } } break; - case CREATE_LOBBY: { - Ref create_response = command_array[1]; - if (create_response.is_valid()) { - Ref result; - result.instantiate(); - result->set_error(message); - create_response->emit_signal("finished", result); - } - } break; case LOBBY_VIEW: { Ref view_response = command_array[1]; if (view_response.is_valid()) { @@ -603,7 +609,7 @@ void LobbyClient::_receive_data(const Dictionary &dict) { } } break; default: { - emit_signal("append_log", "error", dict["message"]); + emit_signal("append_log", "error", p_dict["message"]); } break; } } diff --git a/modules/blazium_sdk/lobby/lobby_client.h b/modules/blazium_sdk/lobby/lobby_client.h index ac3b2076cdba..575d4f49d98f 100644 --- a/modules/blazium_sdk/lobby/lobby_client.h +++ b/modules/blazium_sdk/lobby/lobby_client.h @@ -35,54 +35,127 @@ #include "core/io/json.h" #include "modules/websocket/websocket_peer.h" +class LobbyInfo : public Resource { + GDCLASS(LobbyInfo, Resource); + String id; + String lobby_name; + String host; + String host_name; + int max_players = 0; + int players = 0; + bool sealed = false; + +protected: + static void _bind_methods() { + ClassDB::bind_method(D_METHOD("get_host"), &LobbyInfo::get_host); + ClassDB::bind_method(D_METHOD("get_max_players"), &LobbyInfo::get_max_players); + ClassDB::bind_method(D_METHOD("is_sealed"), &LobbyInfo::is_sealed); + ClassDB::bind_method(D_METHOD("get_id"), &LobbyInfo::get_id); + ClassDB::bind_method(D_METHOD("get_lobby_name"), &LobbyInfo::get_lobby_name); + ClassDB::bind_method(D_METHOD("get_host_name"), &LobbyInfo::get_host_name); + ClassDB::bind_method(D_METHOD("get_players"), &LobbyInfo::get_players); + + ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "", "get_id"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "lobby_name"), "", "get_lobby_name"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "host_name"), "", "get_host_name"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "players"), "", "get_players"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "host"), "", "get_host"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_players"), "", "get_max_players"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sealed"), "", "is_sealed"); + } + +public: + void set_id(const String &p_id) { this->id = p_id; } + void set_lobby_name(const String &p_lobby_name) { this->lobby_name = p_lobby_name; } + void set_host(const String &p_host) { this->host = p_host; } + void set_host_name(const String &p_host_name) { this->host_name = p_host_name; } + void set_max_players(int p_max_players) { this->max_players = p_max_players; } + void set_players(int p_players) { this->players = p_players; } + void set_sealed(bool p_sealed) { this->sealed = p_sealed; } + + void set_dict(const Dictionary &p_dict) { + this->set_host(p_dict.get("host", "")); + this->set_max_players(p_dict.get("max_players", 0)); + this->set_sealed(p_dict.get("sealed", false)); + this->set_players(p_dict.get("players", 0)); + this->set_id(p_dict.get("id", "")); + this->set_lobby_name(p_dict.get("name", "")); + this->set_host_name(p_dict.get("host_name", "")); + } + Dictionary get_dict() const { + Dictionary dict; + dict["host"] = this->get_host(); + dict["max_players"] = this->get_max_players(); + dict["sealed"] = this->is_sealed(); + dict["players"] = this->get_players(); + dict["id"] = this->get_id(); + dict["name"] = this->get_lobby_name(); + dict["host_name"] = this->get_host_name(); + return dict; + } + + String get_id() const { return id; } + String get_lobby_name() const { return lobby_name; } + String get_host() const { return host; } + String get_host_name() const { return host_name; } + int get_max_players() const { return max_players; } + int get_players() const { return players; } + bool is_sealed() const { return sealed; } + LobbyInfo() {} +}; + +class LobbyPeer : public Resource { + GDCLASS(LobbyPeer, Resource); + String id; + String peer_name; + bool ready = false; + +protected: + static void _bind_methods() { + ClassDB::bind_method(D_METHOD("get_id"), &LobbyPeer::get_id); + ClassDB::bind_method(D_METHOD("get_peer_name"), &LobbyPeer::get_peer_name); + ClassDB::bind_method(D_METHOD("is_ready"), &LobbyPeer::is_ready); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "", "get_id"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "peer_name"), "", "get_peer_name"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ready"), "", "is_ready"); + } + +public: + void set_id(const String &p_id) { this->id = p_id; } + void set_peer_name(const String &p_peer_name) { this->peer_name = p_peer_name; } + void set_ready(bool p_ready) { this->ready = p_ready; } + void set_dict(const Dictionary &p_dict) { + this->set_id(p_dict.get("id", "")); + this->set_peer_name(p_dict.get("name", "")); + this->set_ready(p_dict.get("ready", "")); + } + Dictionary get_dict() const { + Dictionary dict; + dict["id"] = this->get_id(); + dict["name"] = this->get_peer_name(); + dict["ready"] = this->is_ready(); + return dict; + } + + String get_id() const { return id; } + String get_peer_name() const { return peer_name; } + bool is_ready() const { return ready; } + LobbyPeer() {} +}; + class LobbyClient : public BlaziumClient { GDCLASS(LobbyClient, BlaziumClient); enum CommandType { - CREATE_LOBBY = 0, - SIMPLE_REQUEST, + LOBBY_REQUEST = 0, LOBBY_VIEW, LOBBY_LIST }; String server_url = "wss://lobby.blazium.app/connect"; + Ref lobby; + Ref peer; + TypedArray peers; public: - class CreateLobbyResponse : public RefCounted { - GDCLASS(CreateLobbyResponse, RefCounted); - - protected: - static void _bind_methods() { - ADD_SIGNAL(MethodInfo("finished", PropertyInfo(Variant::OBJECT, "result", PROPERTY_HINT_RESOURCE_TYPE, "CreateLobbyResult"))); - } - - public: - class CreateLobbyResult : public RefCounted { - GDCLASS(CreateLobbyResult, RefCounted); - String error; - String lobby_name; - - protected: - static void _bind_methods() { - ClassDB::bind_method(D_METHOD("has_error"), &CreateLobbyResult::has_error); - ClassDB::bind_method(D_METHOD("get_error"), &CreateLobbyResult::get_error); - ClassDB::bind_method(D_METHOD("get_lobby_name"), &CreateLobbyResult::get_lobby_name); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "error"), "", "get_error"); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "lobby_name"), "", "get_lobby_name"); - } - - public: - void set_error(String p_error) { this->error = p_error; } - void set_lobby_name(String p_lobby_name) { this->lobby_name = p_lobby_name; } - - bool has_error() const { return !error.is_empty(); } - String get_error() const { return error; } - String get_lobby_name() const { return lobby_name; } - CreateLobbyResult(const CreateLobbyResult &other) : - error(other.error), lobby_name(other.lobby_name) {} - CreateLobbyResult() {} - }; - CreateLobbyResponse(const CreateLobbyResponse &other) {} - CreateLobbyResponse() {} - }; class LobbyResponse : public RefCounted { GDCLASS(LobbyResponse, RefCounted); @@ -109,13 +182,14 @@ class LobbyClient : public BlaziumClient { bool has_error() const { return !error.is_empty(); } String get_error() const { return error; } - LobbyResult(const LobbyResult &other) : - error(other.error) {} + LobbyResult(const LobbyResult &p_other) : + error(p_other.error) {} LobbyResult() {} }; - LobbyResponse(const LobbyResponse &other) {} + LobbyResponse(const LobbyResponse &p_other) {} LobbyResponse() {} }; + class ListLobbyResponse : public RefCounted { GDCLASS(ListLobbyResponse, RefCounted); @@ -129,7 +203,7 @@ class LobbyClient : public BlaziumClient { GDCLASS(ListLobbyResult, RefCounted); String error; - TypedArray lobbies; + TypedArray lobbies; protected: static void _bind_methods() { @@ -137,79 +211,25 @@ class LobbyClient : public BlaziumClient { ClassDB::bind_method(D_METHOD("get_error"), &ListLobbyResult::get_error); ClassDB::bind_method(D_METHOD("get_lobbies"), &ListLobbyResult::get_lobbies); ADD_PROPERTY(PropertyInfo(Variant::STRING, "error"), "", "get_error"); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lobbies"), "", "get_lobbies"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lobbies", PROPERTY_HINT_ARRAY_TYPE, "LobbyInfo"), "", "get_lobbies"); + ADD_PROPERTY_DEFAULT("lobbies", TypedArray()); } public: - void set_error(String p_error) { this->error = p_error; } - void set_lobbies(TypedArray p_lobbies) { this->lobbies = p_lobbies; } + void set_error(const String &p_error) { this->error = p_error; } + void set_lobbies(const TypedArray &p_lobbies) { this->lobbies = p_lobbies; } bool has_error() const { return !error.is_empty(); } String get_error() const { return error; } - TypedArray get_lobbies() const { return lobbies; } - ListLobbyResult(const ListLobbyResult &other) : - error(other.error), lobbies(other.lobbies) {} + TypedArray get_lobbies() const { return lobbies; } + ListLobbyResult(const ListLobbyResult &p_other) : + error(p_other.error), lobbies(p_other.lobbies) {} ListLobbyResult() {} }; - ListLobbyResponse(const ListLobbyResponse &other) {} + ListLobbyResponse(const ListLobbyResponse &p_other) {} ListLobbyResponse() {} }; - class LobbyInfo : public RefCounted { - GDCLASS(LobbyInfo, RefCounted); - String host; - int max_players = 0; - bool sealed = false; - - protected: - static void _bind_methods() { - ClassDB::bind_method(D_METHOD("get_host"), &LobbyInfo::get_host); - ClassDB::bind_method(D_METHOD("get_max_players"), &LobbyInfo::get_max_players); - ClassDB::bind_method(D_METHOD("is_sealed"), &LobbyInfo::is_sealed); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "host"), "", "get_host"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "max_players"), "", "get_max_players"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sealed"), "", "is_sealed"); - } - - public: - void set_host(String p_host) { this->host = p_host; } - void set_max_players(int p_max_players) { this->max_players = p_max_players; } - void set_sealed(bool p_sealed) { this->sealed = p_sealed; } - - String get_host() const { return host; } - int get_max_players() const { return max_players; } - bool is_sealed() const { return sealed; } - LobbyInfo(const LobbyInfo &other) : - host(other.host), max_players(other.max_players), sealed(other.sealed) {} - LobbyInfo() {} - }; - class LobbyPeer : public RefCounted { - GDCLASS(LobbyPeer, RefCounted); - String id; - String name; - bool ready = false; - - protected: - static void _bind_methods() { - ClassDB::bind_method(D_METHOD("get_id"), &LobbyPeer::get_id); - ClassDB::bind_method(D_METHOD("get_name"), &LobbyPeer::get_name); - ClassDB::bind_method(D_METHOD("is_ready"), &LobbyPeer::is_ready); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "", "get_id"); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "name"), "", "get_name"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ready"), "", "is_ready"); - } - public: - void set_id(String p_id) { this->id = p_id; } - void set_name(String p_name) { this->name = p_name; } - void set_ready(bool p_ready) { this->ready = p_ready; } - - String get_id() const { return id; } - String get_name() const { return name; } - bool is_ready() const { return ready; } - LobbyPeer(const LobbyPeer &other) : - id(other.id), name(other.name), ready(other.ready) {} - LobbyPeer() {} - }; class ViewLobbyResponse : public RefCounted { GDCLASS(ViewLobbyResponse, RefCounted); @@ -230,39 +250,41 @@ class LobbyClient : public BlaziumClient { ClassDB::bind_method(D_METHOD("has_error"), &ViewLobbyResult::has_error); ClassDB::bind_method(D_METHOD("get_error"), &ViewLobbyResult::get_error); ClassDB::bind_method(D_METHOD("get_peers"), &ViewLobbyResult::get_peers); - ClassDB::bind_method(D_METHOD("get_lobby_info"), &ViewLobbyResult::get_lobby_info); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "peers"), "", "get_peers"); + ClassDB::bind_method(D_METHOD("get_lobby"), &ViewLobbyResult::get_lobby); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "peers", PROPERTY_HINT_ARRAY_TYPE, "LobbyPeer"), "", "get_peers"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "lobby", PROPERTY_HINT_RESOURCE_TYPE, "LobbyInfo"), "", "get_lobby"); + ADD_PROPERTY_DEFAULT("lobby", Ref()); + ADD_PROPERTY_DEFAULT("peers", TypedArray()); ADD_PROPERTY(PropertyInfo(Variant::STRING, "error"), "", "get_error"); } public: - void set_error(String p_error) { this->error = p_error; } - void set_peers(TypedArray p_peers) { this->peers = p_peers; } - void set_lobby_info(Ref p_lobby_info) { this->lobby_info = p_lobby_info; } + void set_error(const String &p_error) { this->error = p_error; } + void set_peers(const TypedArray &p_peers) { this->peers = p_peers; } + void set_lobby(const Ref &p_lobby_info) { this->lobby_info = p_lobby_info; } bool has_error() const { return !error.is_empty(); } String get_error() const { return error; } TypedArray get_peers() const { return peers; } - Ref get_lobby_info() const { return lobby_info; } + Ref get_lobby() const { return lobby_info; } ViewLobbyResult() { lobby_info.instantiate(); } ~ViewLobbyResult() { } }; - ViewLobbyResponse(const ViewLobbyResponse &other) {} + ViewLobbyResponse(const ViewLobbyResponse &p_other) {} ViewLobbyResponse() {} }; private: Ref _socket; int _counter = 0; + bool connected = false; Dictionary _commands; - String _get_data_from_dict(const Dictionary &dict, const String &key); - void _receive_data(const Dictionary &data); - void _send_data(const Dictionary &data); - void _wait_ready(); + void _receive_data(const Dictionary &p_data); + void _send_data(const Dictionary &p_data); String _increment_counter(); protected: @@ -270,22 +292,29 @@ class LobbyClient : public BlaziumClient { static void _bind_methods(); public: - bool connect_to_lobby(const String &game_id); - void set_server_url(String p_server_url) { this->server_url = p_server_url; } + void set_server_url(const String &p_server_url) { this->server_url = p_server_url; } String get_server_url() { return server_url; } - Ref create_lobby(int max_players, const String &password); - Ref join_lobby(const String &lobby_name, const String &password); + bool get_connected() { return connected; } + void set_lobby(const Ref &p_lobby) { this->lobby = p_lobby; } + Ref get_lobby() { return lobby; } + void set_peer(const Ref &p_peer) { this->peer = p_peer; } + Ref get_peer() { return peer; } + TypedArray get_peers() { return peers; } + + bool connect_to_lobby(const String &p_game_id); + Ref create_lobby(const String &p_lobby_name, int p_max_players, const String &p_password); + Ref join_lobby(const String &p_lobby_id, const String &p_password); Ref leave_lobby(); - Ref list_lobby(int start, int count); - Ref view_lobby(const String &lobby_name, const String &password); - Ref kick_peer(const String &peer_id); + Ref list_lobby(int p_start, int p_count); + Ref view_lobby(const String &p_lobby_id, const String &p_password); + Ref kick_peer(const String &p_peer_id); Ref lobby_ready(); Ref lobby_unready(); - Ref set_peer_name(const String &peer_name); + Ref set_peer_name(const String &p_peer_name); Ref seal_lobby(); Ref unseal_lobby(); - Ref lobby_data(const String &peer_data); - Ref lobby_data_to(const String &peer_data, const String &target_peer); + Ref lobby_data(const String &p_peer_data); + Ref lobby_data_to(const String &p_peer_data, const String &p_target_peer); LobbyClient(); ~LobbyClient(); diff --git a/modules/blazium_sdk/register_types.cpp b/modules/blazium_sdk/register_types.cpp index b70b26c3e8f5..5a825fc946d3 100644 --- a/modules/blazium_sdk/register_types.cpp +++ b/modules/blazium_sdk/register_types.cpp @@ -35,11 +35,9 @@ void initialize_blazium_sdk_module(ModuleInitializationLevel p_level) { if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { GDREGISTER_ABSTRACT_CLASS(BlaziumClient); + GDREGISTER_CLASS(LobbyInfo); + GDREGISTER_CLASS(LobbyPeer); GDREGISTER_CLASS(LobbyClient); - GDREGISTER_CLASS(LobbyClient::LobbyInfo); - GDREGISTER_CLASS(LobbyClient::LobbyPeer); - GDREGISTER_CLASS(LobbyClient::CreateLobbyResponse::CreateLobbyResult); - GDREGISTER_CLASS(LobbyClient::CreateLobbyResponse); GDREGISTER_CLASS(LobbyClient::LobbyResponse::LobbyResult); GDREGISTER_CLASS(LobbyClient::LobbyResponse); GDREGISTER_CLASS(LobbyClient::ListLobbyResponse::ListLobbyResult);