Skip to content

Commit

Permalink
fix private data event and add del/clear data
Browse files Browse the repository at this point in the history
updat with clear/del functions

upd

fix private/public data events.
  • Loading branch information
Ughuuu committed Dec 12, 2024
1 parent 9096d05 commit 227fb06
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 45 deletions.
57 changes: 55 additions & 2 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
<tutorials>
</tutorials>
<methods>
<method name="clear_lobby_data">
<return type="LobbyResponse" />
<param index="0" name="is_private" type="bool" default="false" />
<description>
Clears the lobby data. Only works if you are host.
Generates [signal received_lobby_data].
</description>
</method>
<method name="clear_peer_data">
<return type="LobbyResponse" />
<param index="0" name="target_peer" type="String" />
<param index="1" name="is_private" type="bool" default="false" />
<description>
Clears the peer data. Only works if you are host.
Generates [signal received_peer_data].
</description>
</method>
<method name="clear_peers_data">
<return type="LobbyResponse" />
<param index="0" name="is_private" type="bool" default="false" />
<description>
Clears the peers data. Only works if you are host.
Generates [signal received_peer_data].
</description>
</method>
<method name="connect_to_lobby">
<return type="bool" />
<description>
Expand All @@ -27,6 +52,34 @@
Generates [signal lobby_created] signal.
</description>
</method>
<method name="del_lobby_data">
<return type="LobbyResponse" />
<param index="0" name="key" type="String" />
<param index="1" name="is_private" type="bool" default="false" />
<description>
Delete one key from the lobby data. Only works if you are host.
Generates [signal received_lobby_data].
</description>
</method>
<method name="del_peer_data">
<return type="LobbyResponse" />
<param index="0" name="key" type="String" />
<param index="1" name="target_peer" type="String" />
<param index="2" name="is_private" type="bool" default="false" />
<description>
Delete one key from the peer data. Only works if you are host.
Generates [signal received_peer_data].
</description>
</method>
<method name="del_peers_data">
<return type="LobbyResponse" />
<param index="0" name="key" type="String" />
<param index="1" name="is_private" type="bool" default="false" />
<description>
Delete one key from the peers data. Only works if you are host.
Generates [signal received_peer_data].
</description>
</method>
<method name="disconnect_from_lobby">
<return type="void" />
<description>
Expand Down Expand Up @@ -135,7 +188,7 @@
<return type="LobbyResponse" />
<param index="0" name="data" type="Dictionary" />
<param index="1" name="target_peer" type="String" />
<param index="2" name="is_private" type="bool" />
<param index="2" name="is_private" type="bool" default="false" />
<description>
Set data on the peer. Only works if you are host.
Generates [signal received_peer_data] signal if you are in lobby.
Expand All @@ -152,7 +205,7 @@
<method name="set_peers_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Dictionary" />
<param index="1" name="is_private" type="bool" />
<param index="1" name="is_private" type="bool" default="false" />
<description>
Set data on the peers. Only works if you are host.
Generates [signal received_peer_data] signal if you are in lobby.
Expand Down
233 changes: 190 additions & 43 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ void LobbyClient::_bind_methods() {
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("del_lobby_data", "key", "is_private"), &LobbyClient::del_lobby_data, DEFVAL(false));
ClassDB::bind_method(D_METHOD("clear_lobby_data", "is_private"), &LobbyClient::clear_lobby_data, DEFVAL(false));

ClassDB::bind_method(D_METHOD("set_peer_data", "data", "target_peer", "is_private"), &LobbyClient::set_peer_data, DEFVAL(false));
ClassDB::bind_method(D_METHOD("del_peer_data", "key", "target_peer", "is_private"), &LobbyClient::del_peer_data, DEFVAL(false));
ClassDB::bind_method(D_METHOD("clear_peer_data", "target_peer", "is_private"), &LobbyClient::clear_peer_data, DEFVAL(false));

ClassDB::bind_method(D_METHOD("set_peers_data", "data", "is_private"), &LobbyClient::set_peers_data, DEFVAL(false));
ClassDB::bind_method(D_METHOD("del_peers_data", "key", "is_private"), &LobbyClient::del_peers_data, DEFVAL(false));
ClassDB::bind_method(D_METHOD("clear_peers_data", "is_private"), &LobbyClient::clear_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")));
Expand Down Expand Up @@ -432,6 +441,53 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data(const Dictionary &p_lobb
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::del_lobby_data(const String &p_key, 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
data_object_dict[p_key] = Variant();
data_dict["is_private"] = p_is_private;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Ref<LobbyResponse> 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::LobbyResponse> LobbyClient::clear_lobby_data(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;
Array keys = data_object_dict.keys();
// set null values
for (int i = 0; i < keys.size(); i++) {
data_object_dict[keys[i]] = Variant();
}
data_dict["is_private"] = p_is_private;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Ref<LobbyResponse> 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::LobbyResponse> LobbyClient::set_peer_data(const Dictionary &p_peer_data, const String &p_target_peer, bool p_is_private) {
String id = _increment_counter();
Dictionary command;
Expand All @@ -452,6 +508,55 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::set_peer_data(const Dictionary &p_p
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::del_peer_data(const String &p_key, 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
data_object_dict[p_key] = 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<LobbyResponse> 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::LobbyResponse> LobbyClient::clear_peer_data(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 values
Array keys = data_object_dict.keys();
for (int i = 0; i < keys.size(); i++) {
data_object_dict[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<LobbyResponse> 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::LobbyResponse> LobbyClient::set_peers_data(const Dictionary &p_peer_data, bool p_is_private) {
String id = _increment_counter();
Dictionary command;
Expand All @@ -471,6 +576,53 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::set_peers_data(const Dictionary &p_
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::del_peers_data(const String &p_key, 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
data_object_dict[p_key] = Variant();
data_dict["is_private"] = p_is_private;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Ref<LobbyResponse> 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::LobbyResponse> LobbyClient::clear_peers_data(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 values
Array keys = data_object_dict.keys();
for (int i = 0; i < keys.size(); i++) {
data_object_dict[keys[i]] = Variant();
}
data_dict["is_private"] = p_is_private;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Ref<LobbyResponse> 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: {
Expand Down Expand Up @@ -510,13 +662,17 @@ void LobbyClient::_send_data(const Dictionary &p_data_dict) {
}
}

void update_peers(Dictionary p_data_dict, TypedArray<LobbyPeer> &peers) {
void LobbyClient::_update_peers(Dictionary p_data_dict, TypedArray<LobbyPeer> &peers) {

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

the following warning is treated as an error

Check warning on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

declaration of 'peers' hides class member

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor (target=editor tests=yes)

declaration of 'peers' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release)

the following warning is treated as an error

Check warning on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release)

declaration of 'peers' hides class member

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

declaration of 'peers' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template w/ GCC (target=template_release, tests=yes, use_mingw=yes)

declaration of 'peers' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

declaration of 'peers' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 665 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

declaration of 'peers' shadows a member of 'LobbyClient' [-Werror=shadow]
Array peers_array = p_data_dict.get("peers", Array());
TypedArray<LobbyPeer> peers_info;
peers.clear();
for (int i = 0; i < peers_array.size(); ++i) {
Ref<LobbyPeer> peer = Ref<LobbyPeer>(memnew(LobbyPeer));

Check warning on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

declaration of 'peer' hides class member

Check failure on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor (target=editor tests=yes)

declaration of 'peer' shadows a member of 'LobbyClient' [-Werror=shadow]

Check warning on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release)

declaration of 'peer' hides class member

Check failure on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

declaration of 'peer' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template w/ GCC (target=template_release, tests=yes, use_mingw=yes)

declaration of 'peer' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

declaration of 'peer' shadows a member of 'LobbyClient' [-Werror=shadow]

Check failure on line 670 in modules/blazium_sdk/lobby/lobby_client.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

declaration of 'peer' shadows a member of 'LobbyClient' [-Werror=shadow]
peer->set_dict(peers_array[i]);
Dictionary peer_dict = peers_array[i];
peer->set_dict(peer_dict);
if (peer_dict.has("private_data")) {
peer_data = peer_dict.get("private_data", Dictionary());
}
peers.push_back(peer);
}
}
Expand All @@ -536,66 +692,57 @@ void sort_peers_by_id(TypedArray<LobbyPeer> &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", "");
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);
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<LobbyPeer> peers_info;
update_peers(data_dict, peers_info);
sort_peers_by_id(peers_info);
Ref<LobbyInfo> lobby_info = Ref<LobbyInfo>(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<LobbyPeer> 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<LobbyInfo> lobby_info = Ref<LobbyInfo>(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());
Expand Down Expand Up @@ -849,7 +996,7 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {

// Iterate through peers and populate arrays
TypedArray<LobbyPeer> peers_info;
update_peers(data_dict, peers_info);
_update_peers(data_dict, peers_info);
sort_peers_by_id(peers_info);
Ref<LobbyInfo> lobby_info = Ref<LobbyInfo>(memnew(LobbyInfo));
lobby_info->set_dict(lobby_dict);
Expand Down
Loading

0 comments on commit 227fb06

Please sign in to comment.