Skip to content

Commit

Permalink
fix private/public data events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Dec 11, 2024
1 parent 9e40eb4 commit b3f55ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
80 changes: 37 additions & 43 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,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) {
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));
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,68 +540,58 @@ 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()) {
// 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);
peer->set_ready(false);
sort_peers_by_id(peers);
emit_signal("lobby_created", lobby, peers);
} else if (command == "joined_lobby") {
lobby->set_dict(data_dict.get("lobby", Dictionary()));
peer->set_ready(false);
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 @@ -851,7 +845,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
2 changes: 2 additions & 0 deletions modules/blazium_sdk/lobby/lobby_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<LobbyPeer> &peers);
String _increment_counter();

enum CommandType {
Expand Down

0 comments on commit b3f55ce

Please sign in to comment.