From 0d32e220cfd1b2bda0ae1cd584bc638f725d84ab Mon Sep 17 00:00:00 2001 From: Dragos Daian Date: Sat, 7 Dec 2024 16:07:12 +0100 Subject: [PATCH] update lobby call --- .../blazium_sdk/doc_classes/LobbyClient.xml | 59 +++++++++----- modules/blazium_sdk/doc_classes/LobbyPeer.xml | 3 + modules/blazium_sdk/lobby/lobby_client.cpp | 78 ++++++++++++++++--- modules/blazium_sdk/lobby/lobby_client.h | 16 +++- 4 files changed, 123 insertions(+), 33 deletions(-) diff --git a/modules/blazium_sdk/doc_classes/LobbyClient.xml b/modules/blazium_sdk/doc_classes/LobbyClient.xml index 2f266e7fedb7..f09700514f2b 100644 --- a/modules/blazium_sdk/doc_classes/LobbyClient.xml +++ b/modules/blazium_sdk/doc_classes/LobbyClient.xml @@ -27,6 +27,12 @@ Generates [signal lobby_created] signal. + + + + Returns the lobby data. Only works if you are host. + + @@ -67,6 +73,23 @@ Lists all lobbies. Lobbies that are sealed won't show in the list, except if you disconnected and trying to reconnect to a lobby. + + + + + Send a notification either to the host, or if you are host send data to all peers. + Generates [signal notification_received] signal. + + + + + + + + Send a notification to a peer, works only if you are host. + Generates [signal notification_received] signal. + + @@ -79,17 +102,15 @@ - Send data either to the host, or if you are host send data to all peers. - Generates [signal received_data] signal. + Send data to all peers. If you are not host, sends to host instead. - + - Send data either to a peer, works only if you are host. - Generates [signal received_data_to] signal. + Send data to a peer. @@ -161,6 +182,13 @@ Signal generated after you connect to the lobby. + + + + + Signal generated after data is received. + + @@ -206,6 +234,13 @@ Signals a log from a command. + + + + + Signal generated after a notification is received. + + @@ -251,19 +286,5 @@ Signal generated after a peer reconnects. - - - - - Signal generated after a lobby_data call. - - - - - - - Signal generated after a data_to call. - - diff --git a/modules/blazium_sdk/doc_classes/LobbyPeer.xml b/modules/blazium_sdk/doc_classes/LobbyPeer.xml index 858ecccc7dee..80b6be5c8513 100644 --- a/modules/blazium_sdk/doc_classes/LobbyPeer.xml +++ b/modules/blazium_sdk/doc_classes/LobbyPeer.xml @@ -9,6 +9,9 @@ + + Data of the peer. + Identifier of the peer. diff --git a/modules/blazium_sdk/lobby/lobby_client.cpp b/modules/blazium_sdk/lobby/lobby_client.cpp index 68f67a4e412f..af5554aecedc 100644 --- a/modules/blazium_sdk/lobby/lobby_client.cpp +++ b/modules/blazium_sdk/lobby/lobby_client.cpp @@ -57,6 +57,7 @@ void LobbyClient::_bind_methods() { 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); + ClassDB::bind_method(D_METHOD("get_host_data"), &LobbyClient::get_host_data); ADD_PROPERTY(PropertyInfo(Variant::STRING, "server_url", PROPERTY_HINT_NONE, ""), "set_server_url", "get_server_url"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "reconnection_token", PROPERTY_HINT_NONE, ""), "set_reconnection_token", "get_reconnection_token"); @@ -81,15 +82,17 @@ void LobbyClient::_bind_methods() { 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("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("send_lobby_data", "data"), &LobbyClient::lobby_data); - ClassDB::bind_method(D_METHOD("send_lobby_data_to", "data", "target_peer"), &LobbyClient::lobby_data_to); + ClassDB::bind_method(D_METHOD("send_peer_data", "data", "target_peer"), &LobbyClient::peer_data); // Register signals ADD_SIGNAL(MethodInfo("connected_to_lobby", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::STRING, "reconnection_token"))); ADD_SIGNAL(MethodInfo("disconnected_from_lobby", PropertyInfo(Variant::STRING, "reason"))); ADD_SIGNAL(MethodInfo("peer_named", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); - ADD_SIGNAL(MethodInfo("received_data", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); - ADD_SIGNAL(MethodInfo("received_data_to", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); + ADD_SIGNAL(MethodInfo("notification_received", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); + ADD_SIGNAL(MethodInfo("data_received", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"))); 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", PropertyInfo(Variant::BOOL, "kicked"))); @@ -359,10 +362,10 @@ Ref LobbyClient::seal_lobby(bool seal) { return response; } -Ref LobbyClient::lobby_data(const Variant &p_peer_data) { +Ref LobbyClient::lobby_notify(const Variant &p_peer_data) { String id = _increment_counter(); Dictionary command; - command["command"] = "lobby_data"; + command["command"] = "lobby_notify"; Dictionary data_dict; data_dict["peer_data"] = p_peer_data; data_dict["id"] = id; @@ -377,7 +380,44 @@ Ref LobbyClient::lobby_data(const Variant &p_peer_da return response; } -Ref LobbyClient::lobby_data_to(const Variant &p_peer_data, const String &p_target_peer) { +Ref LobbyClient::peer_notify(const Variant &p_peer_data, const String &p_target_peer) { + String id = _increment_counter(); + Dictionary command; + command["command"] = "notify_to"; + Dictionary data_dict; + 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(LOBBY_REQUEST); + command_array.push_back(response); + _commands[id] = command_array; + _send_data(command); + return response; +} + +Ref LobbyClient::lobby_data(const Variant &p_lobby_data) { + String id = _increment_counter(); + Dictionary command; + command["command"] = "lobby_data"; + Dictionary data_dict; + data_dict["lobby_data"] = p_lobby_data; + 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::peer_data(const Variant &p_peer_data, const String &p_target_peer) { String id = _increment_counter(); Dictionary command; command["command"] = "data_to"; @@ -655,35 +695,49 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) { } } sort_peers_by_id(peers); - } else if (command == "lobby_data") { + } else if (command == "lobby_notify") { + String from_peer_id = data_dict.get("from_peer", ""); + for (int i = 0; i < peers.size(); ++i) { + Ref from_peer = peers[i]; + if (from_peer->get_id() == from_peer_id) { + emit_signal("notification_received", data_dict.get("peer_data", Variant()), from_peer); + break; + } + } + } else if (command == "lobby_data_sent") { + // nothing for now + } else if (command == "data_to") { String from_peer_id = data_dict.get("from_peer", ""); for (int i = 0; i < peers.size(); ++i) { Ref from_peer = peers[i]; if (from_peer->get_id() == from_peer_id) { - emit_signal("received_data", data_dict.get("peer_data", Variant()), from_peer); + emit_signal("data_received", data_dict.get("peer_data", Variant()), from_peer); break; } } } else if (command == "lobby_call") { + // authoritative call Ref response = command_array[1]; if (response.is_valid()) { Ref result = Ref(memnew(AuthoritativeClient::LobbyCallResponse::LobbyCallResult)); result->set_result(data_dict.get("result", "")); response->emit_signal("finished", result); } - } else if (command == "data_to") { + } else if (command == "notified_to") { String from_peer_id = data_dict.get("from_peer", ""); for (int i = 0; i < peers.size(); ++i) { Ref from_peer = peers[i]; if (from_peer->get_id() == from_peer_id) { - emit_signal("received_data_to", data_dict.get("peer_data", Variant()), from_peer); + emit_signal("notification_received", data_dict.get("peer_data", Variant()), from_peer); break; } } - } else if (command == "lobby_data_sent") { - // nothing for now } else if (command == "data_to_sent") { // nothing for now + } else if (command == "lobby_notify_sent") { + // nothing for now + } else if (command == "notify_to_sent") { + // nothing for now } else if (command == "error") { if (command_array.size() == 2) { int command_type = command_array[0]; diff --git a/modules/blazium_sdk/lobby/lobby_client.h b/modules/blazium_sdk/lobby/lobby_client.h index c736bb7dd030..ac8607c8ecae 100644 --- a/modules/blazium_sdk/lobby/lobby_client.h +++ b/modules/blazium_sdk/lobby/lobby_client.h @@ -33,6 +33,7 @@ #include "../blazium_client.h" #include "core/io/json.h" +#include "core/variant/variant.h" #include "modules/websocket/websocket_peer.h" class LobbyInfo : public Resource { @@ -122,6 +123,7 @@ class LobbyPeer : public Resource { GDCLASS(LobbyPeer, Resource); String id; String peer_name; + Variant data; bool ready = false; protected: @@ -129,31 +131,37 @@ class LobbyPeer : public Resource { 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); + ClassDB::bind_method(D_METHOD("get_data"), &LobbyPeer::get_data); 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"); + ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data"), "", "get_data"); } 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_data(const Dictionary &p_data) { this->data = p_data; } 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", "")); + this->set_data(p_dict.get("data", Variant())); } Dictionary get_dict() const { Dictionary dict; dict["id"] = this->get_id(); dict["name"] = this->get_peer_name(); dict["ready"] = this->is_ready(); + dict["data"] = this->get_data(); return dict; } String get_id() const { return id; } String get_peer_name() const { return peer_name; } bool is_ready() const { return ready; } + Variant get_data() const { return data; } LobbyPeer() {} }; @@ -164,6 +172,7 @@ class LobbyClient : public BlaziumClient { String server_url; String reconnection_token; String game_id = ""; + Variant host_data; Ref lobby; Ref peer; TypedArray peers; @@ -307,6 +316,7 @@ class LobbyClient : public BlaziumClient { String get_game_id() { return game_id; } bool is_host() { return lobby->get_host() == peer->get_id(); } bool get_connected() { return connected; } + Variant get_host_data() { return host_data; } 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; } @@ -327,8 +337,10 @@ class LobbyClient : public BlaziumClient { Ref set_peer_name(const String &p_peer_name); Ref seal_lobby(bool seal); Ref lobby_call(const String &p_method, const Array &p_args); - Ref lobby_data(const Variant &p_peer_data); - Ref lobby_data_to(const Variant &p_peer_data, const String &p_target_peer); + 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 Variant &p_lobby_data); + Ref peer_data(const Variant &p_peer_data, const String &p_target_peer); LobbyClient(); ~LobbyClient();