Skip to content

Commit

Permalink
add peer user data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Dec 20, 2024
1 parent be6dfc6 commit a29a013
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 118 deletions.
40 changes: 25 additions & 15 deletions modules/blazium_sdk/doc_classes/AuthoritativeLobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
Generates [signal lobby_tagged].
</description>
</method>
<method name="add_peer_user_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Dictionary" />
<description>
Add user data to your own peer.
Returns a [LobbyResponse] object that has a [signal LobbyResponse.finished] signal that is emitted when finished.
Generates [signal received_peer_user_data].
</description>
</method>
<method name="connect_to_lobby">
<return type="bool" />
<description>
Expand Down Expand Up @@ -63,6 +72,15 @@
Generates [signal lobby_tagged].
</description>
</method>
<method name="del_peer_user_data">
<return type="LobbyResponse" />
<param index="0" name="keys" type="String[]" />
<description>
Delete one or more keys from the peers user data.
Returns a [LobbyResponse] object that has a [signal LobbyResponse.finished] signal that is emitted when finished.
Generates [signal received_peer_user_data].
</description>
</method>
<method name="disconnect_from_lobby">
<return type="void" />
<description>
Expand Down Expand Up @@ -148,15 +166,6 @@
Generates [signal lobby_sealed].
</description>
</method>
<method name="set_peer_name">
<return type="LobbyResponse" />
<param index="0" name="peer_name" type="String" />
<description>
Set your peer name.
Returns a [LobbyResponse] object that has a [signal LobbyResponse.finished] signal that is emitted when finished.
Generates [signal peer_named] signal if you are in lobby.
</description>
</method>
</methods>
<members>
<member name="connected" type="bool" setter="" getter="get_connected" default="false">
Expand Down Expand Up @@ -269,12 +278,6 @@
Signal generated after a peer sends a chat message.
</description>
</signal>
<signal name="peer_named">
<param index="0" name="peer" type="LobbyPeer" />
<description>
Signal generated after a peer names himself.
</description>
</signal>
<signal name="peer_ready">
<param index="0" name="peer" type="LobbyPeer" />
<param index="1" name="is_ready" type="bool" />
Expand Down Expand Up @@ -302,5 +305,12 @@
Signal generated after data is sent to peer.
</description>
</signal>
<signal name="received_peer_user_data">
<param index="0" name="peer" type="LobbyPeer" />
<param index="1" name="data" type="Object" />
<description>
Signal generated after user data is sent to peer.
</description>
</signal>
</signals>
</class>
40 changes: 25 additions & 15 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
Generates [signal received_peer_data].
</description>
</method>
<method name="add_peer_user_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Dictionary" />
<description>
Add user data to your own peer.
Returns a [LobbyResponse] object that has a [signal LobbyResponse.finished] signal that is emitted when finished.
Generates [signal received_peer_user_data].
</description>
</method>
<method name="add_peers_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Dictionary" />
Expand Down Expand Up @@ -112,6 +121,15 @@
Generates [signal received_peer_data].
</description>
</method>
<method name="del_peer_user_data">
<return type="LobbyResponse" />
<param index="0" name="keys" type="String[]" />
<description>
Delete one or more keys from the peers user data.
Returns a [LobbyResponse] object that has a [signal LobbyResponse.finished] signal that is emitted when finished.
Generates [signal received_peer_user_data].
</description>
</method>
<method name="del_peers_data">
<return type="LobbyResponse" />
<param index="0" name="keys" type="String[]" />
Expand Down Expand Up @@ -218,15 +236,6 @@
Generates [signal lobby_sealed].
</description>
</method>
<method name="set_peer_name">
<return type="LobbyResponse" />
<param index="0" name="peer_name" type="String" />
<description>
Set your peer name.
Returns a [LobbyResponse] object that has a [signal LobbyResponse.finished] signal that is emitted when finished.
Generates [signal peer_named] signal if you are in lobby.
</description>
</method>
</methods>
<members>
<member name="connected" type="bool" setter="" getter="get_connected" default="false">
Expand Down Expand Up @@ -343,12 +352,6 @@
Signal generated after a peer sends a chat message.
</description>
</signal>
<signal name="peer_named">
<param index="0" name="peer" type="LobbyPeer" />
<description>
Signal generated after a peer names himself.
</description>
</signal>
<signal name="peer_ready">
<param index="0" name="peer" type="LobbyPeer" />
<param index="1" name="is_ready" type="bool" />
Expand Down Expand Up @@ -377,5 +380,12 @@
Signal generated after data is sent to peer.
</description>
</signal>
<signal name="received_peer_user_data">
<param index="0" name="peer" type="LobbyPeer" />
<param index="1" name="data" type="Object" />
<description>
Signal generated after user data is sent to peer.
</description>
</signal>
</signals>
</class>
3 changes: 0 additions & 3 deletions modules/blazium_sdk/doc_classes/LobbyInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
<member name="host" type="String" setter="" getter="get_host" default="&quot;&quot;">
The host id of the lobby.
</member>
<member name="host_name" type="String" setter="" getter="get_host_name" default="&quot;&quot;">
The name of the host of the lobby.
</member>
<member name="id" type="String" setter="" getter="get_id" default="&quot;&quot;">
The id of the lobby.
</member>
Expand Down
6 changes: 3 additions & 3 deletions modules/blazium_sdk/doc_classes/LobbyPeer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<member name="id" type="String" setter="" getter="get_id" default="&quot;&quot;">
Identifier of the peer.
</member>
<member name="peer_name" type="String" setter="" getter="get_peer_name" default="&quot;&quot;">
Name of the peer.
</member>
<member name="ready" type="bool" setter="" getter="is_ready" default="false">
Whether the peer is ready.
</member>
<member name="user_data" type="Dictionary" setter="" getter="get_user_data" default="{}">
Public user data of the peer.
</member>
</members>
</class>
82 changes: 51 additions & 31 deletions modules/blazium_sdk/lobby/authoritative_lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void AuthoritativeLobbyClient::_bind_methods() {
// Register methods
ClassDB::bind_method(D_METHOD("connect_to_lobby"), &AuthoritativeLobbyClient::connect_to_lobby);
ClassDB::bind_method(D_METHOD("disconnect_from_lobby"), &AuthoritativeLobbyClient::disconnect_from_lobby);
ClassDB::bind_method(D_METHOD("set_peer_name", "peer_name"), &AuthoritativeLobbyClient::set_peer_name);
ClassDB::bind_method(D_METHOD("create_lobby", "title", "tags", "max_players", "password"), &AuthoritativeLobbyClient::create_lobby, DEFVAL(Dictionary()), DEFVAL(4), DEFVAL(""));
ClassDB::bind_method(D_METHOD("join_lobby", "lobby_id", "password"), &AuthoritativeLobbyClient::join_lobby, DEFVAL(""));
ClassDB::bind_method(D_METHOD("leave_lobby"), &AuthoritativeLobbyClient::leave_lobby);
Expand All @@ -85,11 +84,13 @@ void AuthoritativeLobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_lobby_tags", "tags"), &AuthoritativeLobbyClient::set_lobby_tags);
ClassDB::bind_method(D_METHOD("del_lobby_tags", "keys"), &AuthoritativeLobbyClient::del_lobby_tags);
ClassDB::bind_method(D_METHOD("set_lobby_sealed", "seal"), &AuthoritativeLobbyClient::seal_lobby);
ClassDB::bind_method(D_METHOD("add_peer_user_data", "data"), &AuthoritativeLobbyClient::add_user_data);
ClassDB::bind_method(D_METHOD("del_peer_user_data", "keys"), &AuthoritativeLobbyClient::del_user_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_peer_user_data", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::OBJECT, "data")));
ADD_SIGNAL(MethodInfo("lobby_notified", PropertyInfo(Variant::OBJECT, "data")));
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")));
Expand Down Expand Up @@ -283,6 +284,47 @@ Ref<LobbyResponse> AuthoritativeLobbyClient::kick_peer(const String &p_peer_id)
return response;
}

Ref<LobbyResponse> AuthoritativeLobbyClient::add_user_data(const Dictionary &p_user_data) {
String id = _increment_counter();
Dictionary command;
command["command"] = "user_data";
Dictionary data_dict;
data_dict["user_data"] = p_user_data;
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<LobbyResponse> AuthoritativeLobbyClient::del_user_data(const TypedArray<String> &p_keys) {
String id = _increment_counter();
Dictionary command;
command["command"] = "user_data";
Dictionary data_dict;
Dictionary data_object_dict;
data_dict["user_data"] = data_object_dict;
// set null value
for (int i = 0; i < p_keys.size(); i++) {
data_object_dict[p_keys[i]] = Variant();
}
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<LobbyResponse> AuthoritativeLobbyClient::set_lobby_tags(const Dictionary &p_tags) {
String id = _increment_counter();
Dictionary command;
Expand Down Expand Up @@ -363,24 +405,6 @@ Ref<LobbyResponse> AuthoritativeLobbyClient::lobby_ready(bool p_ready) {
return response;
}

Ref<LobbyResponse> AuthoritativeLobbyClient::set_peer_name(const String &p_peer_name) {
String id = _increment_counter();
Dictionary command;
command["command"] = "set_name";
Dictionary data_dict;
data_dict["name"] = p_peer_name;
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<LobbyResponse> AuthoritativeLobbyClient::seal_lobby(bool seal) {
String id = _increment_counter();
Dictionary command;
Expand Down Expand Up @@ -550,24 +574,20 @@ void AuthoritativeLobbyClient::_receive_data(const Dictionary &p_dict) {
break;
}
}
} else if (command == "peer_name") {
} else if (command == "peer_user_data") {
String peer_id = data_dict.get("peer_id", "");
String peer_name = data_dict.get("name", "");
Dictionary peer_user_data = data_dict.get("user_data", "");
if (peer->get_id() == peer_id) {
peer->set_peer_name(peer_name);
peer->set_user_data(peer_user_data);
// notify self
emit_signal("peer_named", peer);
emit_signal("received_peer_user_data", peer, peer_user_data);
} else {
// another peer got named
for (int i = 0; i < peers.size(); ++i) {
Ref<LobbyPeer> updated_peer = peers[i];
if (updated_peer->get_id() == peer_id) {
updated_peer->set_peer_name(peer_name);
// if the named peer is the host, update the host name
if (updated_peer->get_id() == lobby->get_host()) {
lobby->set_host_name(peer_name);
}
emit_signal("peer_named", updated_peer);
updated_peer->set_user_data(peer_user_data);
emit_signal("received_peer_user_data", updated_peer, peer_user_data);
break;
}
}
Expand Down Expand Up @@ -602,7 +622,7 @@ void AuthoritativeLobbyClient::_receive_data(const Dictionary &p_dict) {
Ref<LobbyPeer> joining_peer = Ref<LobbyPeer>(memnew(LobbyPeer));
Dictionary peer_dict = data_dict.get("peer", Dictionary());
joining_peer->set_id(peer_dict.get("id", ""));
joining_peer->set_peer_name(peer_dict.get("name", ""));
joining_peer->set_user_data(peer_dict.get("user_data", ""));
peers.append(joining_peer);
sort_peers_by_id(peers);
lobby->set_players(peers.size());
Expand Down
6 changes: 5 additions & 1 deletion modules/blazium_sdk/lobby/authoritative_lobby_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ class AuthoritativeLobbyClient : public BlaziumClient {
Ref<LobbyResponse> leave_lobby();
Ref<ListLobbyResponse> list_lobby(const Dictionary &p_tags, int p_start, int p_count);
Ref<LobbyResponse> kick_peer(const String &p_peer_id);

Ref<LobbyResponse> add_user_data(const Dictionary &p_user_data);
Ref<LobbyResponse> del_user_data(const TypedArray<String> &p_keys);

Ref<LobbyResponse> set_lobby_tags(const Dictionary &p_tags);
Ref<LobbyResponse> del_lobby_tags(const TypedArray<String> &p_keys);

Ref<LobbyResponse> lobby_chat(const String &chat_message);
Ref<LobbyResponse> lobby_ready(bool p_ready);
Ref<LobbyResponse> set_peer_name(const String &p_peer_name);
Ref<LobbyResponse> seal_lobby(bool seal);
Ref<AuthoritativeLobbyResponse> lobby_call(const String &p_method, const Array &p_args);

Expand Down
Loading

0 comments on commit a29a013

Please sign in to comment.