diff --git a/modules/blazium_sdk/doc_classes/LobbyClient.xml b/modules/blazium_sdk/doc_classes/LobbyClient.xml index 89fa73adfb7e..4c60fd8bc09c 100644 --- a/modules/blazium_sdk/doc_classes/LobbyClient.xml +++ b/modules/blazium_sdk/doc_classes/LobbyClient.xml @@ -9,6 +9,42 @@ + + + + + + Add data to the lobby. Only works if you are host. + Generates [signal received_lobby_data]. + + + + + + + Add tags to the lobby. Only works if you are host. + Generates [signal lobby_tagged]. + + + + + + + + + Add data to a peer. Only works if you are host. + Generates [signal received_peer_data]. + + + + + + + + Add data to all peers. Only works if you are host. + Generates [signal received_peer_data]. + + @@ -27,6 +63,42 @@ Generates [signal lobby_created] signal. + + + + + + Delete one or more keys from the lobby data. Only works if you are host. + Generates [signal received_lobby_data]. + + + + + + + Delete one or more keys from the lobby tags. Only works if you are host. + Generates [signal lobby_tagged]. + + + + + + + + + one or more keys from the peer data. Only works if you are host. + Generates [signal received_peer_data]. + + + + + + + + one or more keys from the peers data. Only works if you are host. + Generates [signal received_peer_data]. + + @@ -98,15 +170,6 @@ Generates [signal peer_messaged]. - - - - - - Set data on the lobby. Only works if you are host. - Generates [signal received_lobby_data]. - - @@ -123,24 +186,6 @@ Generates [signal lobby_sealed]. - - - - - Set tags on the lobby. Only works if you are host. - Generates [signal lobby_tagged]. - - - - - - - - - Set data on the peer. Only works if you are host. - Generates [signal received_peer_data] signal if you are in lobby. - - @@ -149,15 +194,6 @@ Generates [signal peer_named] signal if you are in lobby. - - - - - - Set data on the peers. Only works if you are host. - Generates [signal received_peer_data] signal if you are in lobby. - - diff --git a/modules/blazium_sdk/lobby/lobby_client.cpp b/modules/blazium_sdk/lobby/lobby_client.cpp index 02dce01302ad..85bccb789407 100644 --- a/modules/blazium_sdk/lobby/lobby_client.cpp +++ b/modules/blazium_sdk/lobby/lobby_client.cpp @@ -84,13 +84,20 @@ void LobbyClient::_bind_methods() { ClassDB::bind_method(D_METHOD("kick_peer", "peer_id"), &LobbyClient::kick_peer); ClassDB::bind_method(D_METHOD("send_chat_message", "chat_message"), &LobbyClient::lobby_chat); ClassDB::bind_method(D_METHOD("set_lobby_ready", "ready"), &LobbyClient::lobby_ready); - ClassDB::bind_method(D_METHOD("set_lobby_tags", "tags"), &LobbyClient::set_lobby_tags); + ClassDB::bind_method(D_METHOD("add_lobby_tags", "tags"), &LobbyClient::set_lobby_tags); + ClassDB::bind_method(D_METHOD("del_lobby_tags", "keys"), &LobbyClient::del_lobby_tags); ClassDB::bind_method(D_METHOD("set_lobby_sealed", "seal"), &LobbyClient::seal_lobby); ClassDB::bind_method(D_METHOD("notify_lobby", "data"), &LobbyClient::lobby_notify); ClassDB::bind_method(D_METHOD("notify_peer", "data", "target_peer"), &LobbyClient::peer_notify); - ClassDB::bind_method(D_METHOD("set_lobby_data", "data", "is_private"), &LobbyClient::lobby_data, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("set_peer_data", "data", "target_peer", "is_private"), &LobbyClient::set_peer_data); - ClassDB::bind_method(D_METHOD("set_peers_data", "data", "is_private"), &LobbyClient::set_peers_data); + + ClassDB::bind_method(D_METHOD("add_lobby_data", "data", "is_private"), &LobbyClient::lobby_data, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("del_lobby_data", "keys", "is_private"), &LobbyClient::del_lobby_data, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("add_peer_data", "data", "target_peer", "is_private"), &LobbyClient::set_peer_data, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("del_peer_data", "keys", "target_peer", "is_private"), &LobbyClient::del_peer_data, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("add_peers_data", "data", "is_private"), &LobbyClient::set_peers_data, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("del_peers_data", "keys", "is_private"), &LobbyClient::del_peers_data, DEFVAL(false)); // Register signals ADD_SIGNAL(MethodInfo("connected_to_lobby", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::STRING, "reconnection_token"))); @@ -298,6 +305,29 @@ Ref LobbyClient::set_lobby_tags(const Dictionary &p_ return response; } +Ref LobbyClient::del_lobby_tags(const TypedArray &p_keys) { + String id = _increment_counter(); + Dictionary command; + command["command"] = "lobby_tags"; + Dictionary data_dict; + command["data"] = data_dict; + Dictionary data_object_dict; + // set null value + for (int i = 0; i < p_keys.size(); i++) { + data_object_dict[p_keys[i]] = Variant(); + } + data_dict["tags"] = data_object_dict; + data_dict["id"] = id; + Array command_array; + Ref response; + response.instantiate(); + command_array.push_back(LOBBY_REQUEST); + command_array.push_back(response); + _commands[id] = command_array; + _send_data(command); + return response; +} + Ref LobbyClient::lobby_chat(const String &p_chat_message) { String id = _increment_counter(); Dictionary command; @@ -432,6 +462,30 @@ Ref LobbyClient::lobby_data(const Dictionary &p_lobb return response; } +Ref LobbyClient::del_lobby_data(const TypedArray &p_keys, bool p_is_private) { + String id = _increment_counter(); + Dictionary command; + command["command"] = "lobby_data"; + Dictionary data_dict; + Dictionary data_object_dict; + data_dict["lobby_data"] = data_object_dict; + // set null value + for (int i = 0; i < p_keys.size(); i++) { + data_object_dict[p_keys[i]] = Variant(); + } + data_dict["is_private"] = p_is_private; + data_dict["id"] = id; + command["data"] = data_dict; + Array command_array; + Ref response; + response.instantiate(); + command_array.push_back(LOBBY_REQUEST); + command_array.push_back(response); + _commands[id] = command_array; + _send_data(command); + return response; +} + Ref LobbyClient::set_peer_data(const Dictionary &p_peer_data, const String &p_target_peer, bool p_is_private) { String id = _increment_counter(); Dictionary command; @@ -452,6 +506,31 @@ Ref LobbyClient::set_peer_data(const Dictionary &p_p return response; } +Ref LobbyClient::del_peer_data(const TypedArray &p_keys, const String &p_target_peer, bool p_is_private) { + String id = _increment_counter(); + Dictionary command; + command["command"] = "data_to"; + Dictionary data_dict; + Dictionary data_object_dict; + data_dict["peer_data"] = data_object_dict; + // set null value + for (int i = 0; i < p_keys.size(); i++) { + data_object_dict[p_keys[i]] = Variant(); + } + data_dict["target_peer"] = p_target_peer; + data_dict["is_private"] = p_is_private; + data_dict["id"] = id; + command["data"] = data_dict; + Array command_array; + Ref response; + response.instantiate(); + command_array.push_back(LOBBY_REQUEST); + command_array.push_back(response); + _commands[id] = command_array; + _send_data(command); + return response; +} + Ref LobbyClient::set_peers_data(const Dictionary &p_peer_data, bool p_is_private) { String id = _increment_counter(); Dictionary command; @@ -471,6 +550,30 @@ Ref LobbyClient::set_peers_data(const Dictionary &p_ return response; } +Ref LobbyClient::del_peers_data(const TypedArray &p_keys, bool p_is_private) { + String id = _increment_counter(); + Dictionary command; + command["command"] = "data_to_all"; + Dictionary data_dict; + Dictionary data_object_dict; + data_dict["peer_data"] = data_object_dict; + // set null value + for (int i = 0; i < p_keys.size(); i++) { + data_object_dict[p_keys[i]] = Variant(); + } + data_dict["is_private"] = p_is_private; + data_dict["id"] = id; + command["data"] = data_dict; + Array command_array; + Ref response; + response.instantiate(); + command_array.push_back(LOBBY_REQUEST); + command_array.push_back(response); + _commands[id] = command_array; + _send_data(command); + return response; +} + void LobbyClient::_notification(int p_what) { switch (p_what) { case NOTIFICATION_INTERNAL_PROCESS: { @@ -510,14 +613,18 @@ void LobbyClient::_send_data(const Dictionary &p_data_dict) { } } -void update_peers(Dictionary p_data_dict, TypedArray &peers) { +void LobbyClient::_update_peers(Dictionary p_data_dict, TypedArray &p_peers) { Array peers_array = p_data_dict.get("peers", Array()); TypedArray peers_info; - peers.clear(); + p_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); + Ref peer_info = Ref(memnew(LobbyPeer)); + Dictionary peer_dict = peers_array[i]; + peer_info->set_dict(peer_dict); + if (peer_dict.has("private_data")) { + peer_data = peer_dict.get("private_data", Dictionary()); + } + p_peers.push_back(peer_info); } } @@ -536,6 +643,14 @@ void sort_peers_by_id(TypedArray &peers) { } } +void LobbyClient::_clear_lobby() { + lobby->set_dict(Dictionary()); + peers.clear(); + host_data = Dictionary(); + peer_data = Dictionary(); + peer->set_data(Dictionary()); +} + void LobbyClient::_receive_data(const Dictionary &p_dict) { String command = p_dict.get("command", "error"); String message = p_dict.get("message", ""); @@ -543,59 +658,42 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) { String message_id = data_dict.get("id", ""); Array command_array = _commands.get(message_id, Array()); _commands.erase(message_id); - if (command_array.size() == 2 && command != "error") { - int command_type = command_array[0]; - switch (command_type) { - case LOBBY_REQUEST: { - // nothing to update, will notify at the end - } 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()); - if (lobby_dict.has("private_data")) { - host_data = lobby_dict.get("private_data", Dictionary()); - } - peers = peers_info; + // update lobby and peers + { + TypedArray peers_info; + if (data_dict.has("peers")) { + // Iterate through peers and populate arrays + _update_peers(data_dict, peers_info); + sort_peers_by_id(peers_info); + } + if (data_dict.has("lobby")) { + Dictionary lobby_dict = data_dict.get("lobby", Dictionary()); + Ref lobby_info = Ref(memnew(LobbyInfo)); + lobby_info->set_dict(lobby_dict); + if (lobby_info->get_id() == lobby->get_id() || command == "lobby_created" || command == "joined_lobby") { + // Update lobby info because we viewed our own lobby + lobby->set_dict(lobby_info->get_dict()); + if (lobby_dict.has("private_data")) { + host_data = lobby_dict.get("private_data", Dictionary()); } - } break; + peers = peers_info; + } } } if (command == "peer_state") { Dictionary peer_dict = data_dict.get("peer", Dictionary()); peer->set_dict(peer_dict); reconnection_token = peer_dict.get("reconnection_token", ""); - peer_data = peer_dict.get("private_data", Dictionary()); emit_signal("connected_to_lobby", peer, reconnection_token); } 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") { - 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") { - lobby->set_dict(Dictionary()); - peers.clear(); - host_data = Dictionary(); - peer_data = Dictionary(); + _clear_lobby(); emit_signal("lobby_left", false); } else if (command == "lobby_kicked") { - lobby->set_dict(Dictionary()); - peers.clear(); - host_data = Dictionary(); - peer_data = Dictionary(); + _clear_lobby(); emit_signal("lobby_left", true); } else if (command == "lobby_sealed") { Dictionary lobby_dict = data_dict.get("lobby", Dictionary()); @@ -849,7 +947,7 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) { // Iterate through peers and populate arrays TypedArray peers_info; - update_peers(data_dict, 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); diff --git a/modules/blazium_sdk/lobby/lobby_client.h b/modules/blazium_sdk/lobby/lobby_client.h index c3eae420b0d5..28633537918b 100644 --- a/modules/blazium_sdk/lobby/lobby_client.h +++ b/modules/blazium_sdk/lobby/lobby_client.h @@ -261,7 +261,7 @@ class LobbyClient : public BlaziumClient { class ViewLobbyResult : public RefCounted { GDCLASS(ViewLobbyResult, RefCounted); String error; - TypedArray peers; + TypedArray peers_info; Ref lobby_info; protected: @@ -279,12 +279,12 @@ class LobbyClient : public BlaziumClient { public: void set_error(const String &p_error) { this->error = p_error; } - void set_peers(const TypedArray &p_peers) { this->peers = p_peers; } + void set_peers(const TypedArray &p_peers) { this->peers_info = 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; } + TypedArray get_peers() const { return peers_info; } Ref get_lobby() const { return lobby_info; } ViewLobbyResult() { lobby_info.instantiate(); @@ -300,8 +300,10 @@ class LobbyClient : public BlaziumClient { bool connected = false; Dictionary _commands; + void _clear_lobby(); void _receive_data(const Dictionary &p_data); void _send_data(const Dictionary &p_data); + void _update_peers(Dictionary p_data_dict, TypedArray &peers); String _increment_counter(); enum CommandType { @@ -340,6 +342,7 @@ class LobbyClient : public BlaziumClient { Ref view_lobby(const String &p_lobby_id, const String &p_password); Ref kick_peer(const String &p_peer_id); Ref set_lobby_tags(const Dictionary &p_tags); + Ref del_lobby_tags(const TypedArray &p_keys); Ref lobby_chat(const String &chat_message); Ref lobby_ready(bool p_ready); Ref set_peer_name(const String &p_peer_name); @@ -347,9 +350,15 @@ class LobbyClient : public BlaziumClient { Ref lobby_call(const String &p_method, const Array &p_args); Ref lobby_notify(const Variant &p_peer_data); Ref peer_notify(const Variant &p_peer_data, const String &p_target_peer); + Ref lobby_data(const Dictionary &p_lobby_data, bool p_is_private); + Ref del_lobby_data(const TypedArray &p_keys, bool p_is_private); + Ref set_peer_data(const Dictionary &p_peer_data, const String &p_target_peer, bool p_is_private); + Ref del_peer_data(const TypedArray &p_keys, const String &p_target_peer, bool p_is_private); + Ref set_peers_data(const Dictionary &p_peer_data, bool p_is_private); + Ref del_peers_data(const TypedArray &p_keys, bool p_is_private); LobbyClient(); ~LobbyClient();