Skip to content

Commit

Permalink
add peer private data to auth client
Browse files Browse the repository at this point in the history
update private peer data
  • Loading branch information
Ughuuu committed Dec 16, 2024
1 parent f42b69e commit cb58d45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 9 additions & 0 deletions modules/blazium_sdk/doc_classes/AuthoritativeClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
- [member peer]: The current peer. Reflects changes to the self peer.
- [member peers]: The lobby peers. Reflects changes to all peers.
- [member lobby]: The lobby. Reflects changes to the lobby.
- [member peer_data]: The current peer private data.

[b]Note:[/b] The main difference between this service and the non authoritative [LobbyClient] are:
- The host cannot set any data or notify peer_disconnected.
- The [signal received_lobby_data] signal doesn't have the [code]is_private[/code] parameter.
</description>
<tutorials>
</tutorials>
Expand Down Expand Up @@ -174,6 +179,9 @@
<member name="peer" type="LobbyPeer" setter="" getter="get_peer">
The current peer. Reflects changes to the self peer.
</member>
<member name="peer_data" type="Dictionary" setter="" getter="get_peer_data" default="{}">
The current peer private data.
</member>
<member name="peers" type="LobbyPeer[]" setter="" getter="get_peers" default="[]">
The lobby peers. Reflects changes to all peers.
</member>
Expand Down Expand Up @@ -298,6 +306,7 @@
<signal name="received_peer_data">
<param index="0" name="data" type="Object" />
<param index="1" name="to_peer" type="LobbyPeer" />
<param index="2" name="is_private" type="bool" />
<description>
Signal generated after data is sent to peer.
</description>
Expand Down
16 changes: 13 additions & 3 deletions modules/blazium_sdk/lobby/authoritative_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void AuthoritativeClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_lobby"), &AuthoritativeClient::get_lobby);
ClassDB::bind_method(D_METHOD("get_peer"), &AuthoritativeClient::get_peer);
ClassDB::bind_method(D_METHOD("get_peers"), &AuthoritativeClient::get_peers);
ClassDB::bind_method(D_METHOD("get_peer_data"), &AuthoritativeClient::get_peer_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");
Expand All @@ -65,6 +66,7 @@ void AuthoritativeClient::_bind_methods() {
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(PropertyInfo(Variant::DICTIONARY, "peer_data"), "", "get_peer_data");
ADD_PROPERTY_DEFAULT("peers", TypedArray<LobbyPeer>());
ADD_PROPERTY_DEFAULT("peer", Ref<LobbyPeer>());
ADD_PROPERTY_DEFAULT("lobby", Ref<LobbyInfo>());
Expand All @@ -90,7 +92,7 @@ void AuthoritativeClient::_bind_methods() {
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("lobby_notified", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("received_peer_data", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "to_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("received_peer_data", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "to_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::BOOL, "is_private")));
ADD_SIGNAL(MethodInfo("received_lobby_data", PropertyInfo(Variant::OBJECT, "data")));
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")));
Expand Down Expand Up @@ -119,6 +121,7 @@ Ref<LobbyInfo> AuthoritativeClient::get_lobby() { return lobby; }
void AuthoritativeClient::set_peer(const Ref<LobbyPeer> &p_peer) { this->peer = p_peer; }
Ref<LobbyPeer> AuthoritativeClient::get_peer() { return peer; }
TypedArray<LobbyPeer> AuthoritativeClient::get_peers() { return peers; }
Dictionary AuthoritativeClient::get_peer_data() { return peer_data; }

bool AuthoritativeClient::connect_to_lobby() {
if (connected) {
Expand Down Expand Up @@ -666,13 +669,20 @@ void AuthoritativeClient::_receive_data(const Dictionary &p_dict) {
// nothing for now
} else if (command == "data_to") {
String target_peer_id = data_dict.get("target_peer", "");
bool is_private = data_dict.get("is_private", false);
Dictionary peer_data_variant = data_dict.get("peer_data", Dictionary());
for (int i = 0; i < peers.size(); ++i) {
Ref<LobbyPeer> updated_peer = peers[i];
if (updated_peer->get_id() == target_peer_id) {
// got peer data, update it
updated_peer->set_data(peer_data_variant);
emit_signal("received_peer_data", peer_data_variant, updated_peer);
if (is_private && target_peer_id == peer->get_id()) {
// private data, update self
peer_data = peer_data_variant;
} else {
// public peer data
updated_peer->set_data(peer_data_variant);
}
emit_signal("received_peer_data", peer_data_variant, updated_peer, is_private);
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions modules/blazium_sdk/lobby/authoritative_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AuthoritativeClient : public BlaziumClient {
String server_url = "wss://authlobby.blazium.app/connect";
String reconnection_token = "";
String game_id = "";
Dictionary peer_data = Dictionary();
Ref<LobbyInfo> lobby;
Ref<LobbyPeer> peer;
TypedArray<LobbyPeer> peers = TypedArray<LobbyPeer>();
Expand Down Expand Up @@ -75,6 +76,7 @@ class AuthoritativeClient : public BlaziumClient {
String get_game_id();
bool is_host();
bool get_connected();
Dictionary get_peer_data();
void set_lobby(const Ref<LobbyInfo> &p_lobby);
Ref<LobbyInfo> get_lobby();
void set_peer(const Ref<LobbyPeer> &p_peer);
Expand Down

0 comments on commit cb58d45

Please sign in to comment.