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

remove view lobby as it's unused

fix id of lobby call

add demo link
  • Loading branch information
Ughuuu committed Dec 16, 2024
1 parent f42b69e commit f762a77
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 80 deletions.
19 changes: 10 additions & 9 deletions modules/blazium_sdk/doc_classes/AuthoritativeClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
- [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>
<link title="Lobby SDK Demo">https://github.com/blazium-engine/blazium-lobby-sdk</link>
</tutorials>
<methods>
<method name="add_lobby_tags">
Expand Down Expand Up @@ -151,15 +157,6 @@
Generates [signal peer_named] signal if you are in lobby.
</description>
</method>
<method name="view_lobby">
<return type="ViewLobbyResponse" />
<param index="0" name="lobby_id" type="String" default="&quot;&quot;" />
<param index="1" name="password" type="String" default="&quot;&quot;" />
<description>
View data from a lobby. Returns lobby settings and peers.
Returns a [ViewLobbyResponse] object that has a [signal ViewLobbyResponse.finished] signal that is emitted when finished.
</description>
</method>
</methods>
<members>
<member name="connected" type="bool" setter="" getter="get_connected" default="false">
Expand All @@ -174,6 +171,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 +298,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
10 changes: 1 addition & 9 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [member peer_data]: The current peer private data.
</description>
<tutorials>
<link title="Lobby SDK Demo">https://github.com/blazium-engine/blazium-lobby-sdk</link>
</tutorials>
<methods>
<method name="add_lobby_data">
Expand Down Expand Up @@ -226,15 +227,6 @@
Generates [signal peer_named] signal if you are in lobby.
</description>
</method>
<method name="view_lobby">
<return type="ViewLobbyResponse" />
<param index="0" name="lobby_id" type="String" default="&quot;&quot;" />
<param index="1" name="password" type="String" default="&quot;&quot;" />
<description>
View data from a lobby. Returns lobby settings and peers.
Returns a [ViewLobbyResponse] object that has a [signal ViewLobbyResponse.finished] signal that is emitted when finished.
</description>
</method>
</methods>
<members>
<member name="connected" type="bool" setter="" getter="get_connected" default="false">
Expand Down
55 changes: 21 additions & 34 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 @@ -77,7 +79,6 @@ void AuthoritativeClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("leave_lobby"), &AuthoritativeClient::leave_lobby);
ClassDB::bind_method(D_METHOD("lobby_call", "method", "args"), &AuthoritativeClient::lobby_call);
ClassDB::bind_method(D_METHOD("list_lobbies", "tags", "start", "count"), &AuthoritativeClient::list_lobby, DEFVAL(Dictionary()), DEFVAL(0), DEFVAL(10));
ClassDB::bind_method(D_METHOD("view_lobby", "lobby_id", "password"), &AuthoritativeClient::view_lobby, DEFVAL(""), DEFVAL(""));
ClassDB::bind_method(D_METHOD("kick_peer", "peer_id"), &AuthoritativeClient::kick_peer);
ClassDB::bind_method(D_METHOD("send_chat_message", "chat_message"), &AuthoritativeClient::lobby_chat);
ClassDB::bind_method(D_METHOD("set_lobby_ready", "ready"), &AuthoritativeClient::lobby_ready);
Expand All @@ -90,7 +91,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 +120,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 @@ -209,6 +211,7 @@ Ref<AuthoritativeResponse> AuthoritativeClient::lobby_call(const String &p_metho
Dictionary data_dict;
data_dict["function"] = p_method;
data_dict["inputs"] = p_args;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Ref<AuthoritativeResponse> response;
Expand Down Expand Up @@ -261,29 +264,6 @@ Ref<ListLobbyResponse> AuthoritativeClient::list_lobby(const Dictionary &p_tags,
return response;
}

Ref<ViewLobbyResponse> AuthoritativeClient::view_lobby(const String &p_lobby_id, const String &p_password) {
String id = _increment_counter();
Dictionary command;
command["command"] = "view_lobby";
Dictionary data_dict;
command["data"] = data_dict;
if (p_lobby_id.is_empty()) {
data_dict["lobby_id"] = lobby->get_id();
} else {
data_dict["lobby_id"] = p_lobby_id;
}
data_dict["password"] = p_password;
data_dict["id"] = id;
Array command_array;
Ref<ViewLobbyResponse> response;
response.instantiate();
command_array.push_back(LOBBY_VIEW);
command_array.push_back(response);
_commands[id] = command_array;
_send_data(command);
return response;
}

Ref<LobbyResponse> AuthoritativeClient::kick_peer(const String &p_peer_id) {
String id = _increment_counter();
Dictionary command;
Expand Down Expand Up @@ -555,8 +535,6 @@ void AuthoritativeClient::_receive_data(const Dictionary &p_dict) {
response->emit_signal("finished", result);
}
}
} else if (command == "lobby_view") {
// nothing for now
} else if (command == "peer_chat") {
String peer_id = data_dict.get("from_peer", "");
String chat_data = data_dict.get("chat_data", "");
Expand Down Expand Up @@ -666,22 +644,31 @@ 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;
}
}
} else if (command == "lobby_call") {
Ref<AuthoritativeResponse> response = command_array[1];
if (response.is_valid()) {
Ref<AuthoritativeResponse::AuthoritativeResult> result = Ref<AuthoritativeResponse::AuthoritativeResult>(memnew(AuthoritativeResponse::AuthoritativeResult));
result->set_result(data_dict.get("result", ""));
response->emit_signal("finished", result);
if (command_array.size() == 2) {
Ref<AuthoritativeResponse> response = command_array[1];
if (response.is_valid()) {
Ref<AuthoritativeResponse::AuthoritativeResult> result = Ref<AuthoritativeResponse::AuthoritativeResult>(memnew(AuthoritativeResponse::AuthoritativeResult));
result->set_result(data_dict.get("result", ""));
response->emit_signal("finished", result);
}
}
} else if (command == "notified_to") {
String from_peer_id = data_dict.get("from_peer", "");
Expand Down
3 changes: 2 additions & 1 deletion 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 All @@ -87,7 +89,6 @@ class AuthoritativeClient : public BlaziumClient {
Ref<ViewLobbyResponse> join_lobby(const String &p_lobby_id, const String &p_password);
Ref<LobbyResponse> leave_lobby();
Ref<ListLobbyResponse> list_lobby(const Dictionary &p_tags, int p_start, int p_count);
Ref<ViewLobbyResponse> view_lobby(const String &p_lobby_id, const String &p_password);
Ref<LobbyResponse> kick_peer(const String &p_peer_id);
Ref<LobbyResponse> set_lobby_tags(const Dictionary &p_tags);
Ref<LobbyResponse> del_lobby_tags(const TypedArray<String> &p_keys);
Expand Down
26 changes: 0 additions & 26 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ void LobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("join_lobby", "lobby_id", "password"), &LobbyClient::join_lobby, DEFVAL(""));
ClassDB::bind_method(D_METHOD("leave_lobby"), &LobbyClient::leave_lobby);
ClassDB::bind_method(D_METHOD("list_lobbies", "tags", "start", "count"), &LobbyClient::list_lobby, DEFVAL(Dictionary()), DEFVAL(0), DEFVAL(10));
ClassDB::bind_method(D_METHOD("view_lobby", "lobby_id", "password"), &LobbyClient::view_lobby, DEFVAL(""), DEFVAL(""));
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);
Expand Down Expand Up @@ -261,29 +260,6 @@ Ref<ListLobbyResponse> LobbyClient::list_lobby(const Dictionary &p_tags, int p_s
return response;
}

Ref<ViewLobbyResponse> LobbyClient::view_lobby(const String &p_lobby_id, const String &p_password) {
String id = _increment_counter();
Dictionary command;
command["command"] = "view_lobby";
Dictionary data_dict;
command["data"] = data_dict;
if (p_lobby_id.is_empty()) {
data_dict["lobby_id"] = lobby->get_id();
} else {
data_dict["lobby_id"] = p_lobby_id;
}
data_dict["password"] = p_password;
data_dict["id"] = id;
Array command_array;
Ref<ViewLobbyResponse> response;
response.instantiate();
command_array.push_back(LOBBY_VIEW);
command_array.push_back(response);
_commands[id] = command_array;
_send_data(command);
return response;
}

Ref<LobbyResponse> LobbyClient::kick_peer(const String &p_peer_id) {
String id = _increment_counter();
Dictionary command;
Expand Down Expand Up @@ -746,8 +722,6 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
response->emit_signal("finished", result);
}
}
} else if (command == "lobby_view") {
// nothing for now
} else if (command == "peer_chat") {
String peer_id = data_dict.get("from_peer", "");
String chat_data = data_dict.get("chat_data", "");
Expand Down
1 change: 0 additions & 1 deletion modules/blazium_sdk/lobby/lobby_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class LobbyClient : public BlaziumClient {
Ref<ViewLobbyResponse> join_lobby(const String &p_lobby_id, const String &p_password);
Ref<LobbyResponse> leave_lobby();
Ref<ListLobbyResponse> list_lobby(const Dictionary &p_tags, int p_start, int p_count);
Ref<ViewLobbyResponse> view_lobby(const String &p_lobby_id, const String &p_password);
Ref<LobbyResponse> kick_peer(const String &p_peer_id);
Ref<LobbyResponse> set_lobby_tags(const Dictionary &p_tags);
Ref<LobbyResponse> del_lobby_tags(const TypedArray<String> &p_keys);
Expand Down

0 comments on commit f762a77

Please sign in to comment.