Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Dec 8, 2024
1 parent 5a169d6 commit 3c3644f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 48 deletions.
70 changes: 47 additions & 23 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,14 @@
<param index="3" name="password" type="String" default="&quot;&quot;" />
<description>
Create a lobby and become host. If you are already in a lobby, you cannot create one. You need to leave first.
The new lobby can have a title, tags, max players and password.
The new lobby can have a title, tags, max players and password. 0 max players means unlimited.
Generates [signal lobby_created] signal.
</description>
</method>
<method name="get_host_data">
<return type="Variant" />
<method name="disconnect_from_lobby">
<return type="void" />
<description>
Returns the lobby data. Only works if you are host.
</description>
</method>
<method name="get_peer_data">
<return type="Variant" />
<description>
Returns the self peer data. Only works if you are in a lobby.
Disconnect from the lobby server.
</description>
</method>
<method name="is_host">
Expand Down Expand Up @@ -104,19 +98,13 @@
Generates [signal peer_messaged].
</description>
</method>
<method name="send_peer_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Variant" />
<param index="1" name="target_peer" type="String" />
<description>
Send data to a peer.
</description>
</method>
<method name="set_lobby_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Variant" />
<param index="0" name="data" type="Dictionary" />
<param index="1" name="is_private" type="bool" default="false" />
<description>
Set data on the lobby. Only works if you are host.
Generates [signal received_lobby_data].
</description>
</method>
<method name="set_lobby_ready">
Expand All @@ -139,6 +127,18 @@
<return type="LobbyResponse" />
<param index="0" name="tags" type="Dictionary" />
<description>
Set tags on the lobby. Only works if you are host.
Generates [signal lobby_tagged].
</description>
</method>
<method name="set_peer_data">
<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" />
<description>
Set data on the peer. Only works if you are host.
Generates [signal received_peer_data] signal if you are in lobby.
</description>
</method>
<method name="set_peer_name">
Expand All @@ -149,6 +149,15 @@
Generates [signal peer_named] signal if you are in lobby.
</description>
</method>
<method name="set_peers_data">
<return type="LobbyResponse" />
<param index="0" name="data" type="Dictionary" />
<param index="1" name="is_private" type="bool" />
<description>
Set data on the peers. Only works if you are host.
Generates [signal received_peer_data] 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;" />
Expand All @@ -163,13 +172,20 @@
True if the client is connected, else false.
</member>
<member name="game_id" type="String" setter="set_game_id" getter="get_game_id" default="&quot;&quot;">
The game id.
</member>
<member name="host_data" type="Dictionary" setter="" getter="get_host_data" default="{}">
The current lobby private data. Only works if you are host.
</member>
<member name="lobby" type="LobbyInfo" setter="" getter="get_lobby">
The current lobby. Reflects changes to the lobby.
</member>
<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 @@ -274,7 +290,7 @@
</signal>
<signal name="peer_ready">
<param index="0" name="peer" type="LobbyPeer" />
<param index="1" name="ready" type="bool" />
<param index="1" name="is_ready" type="bool" />
<description>
Signal generated after a peer is ready.
</description>
Expand All @@ -285,11 +301,19 @@
Signal generated after a peer reconnects.
</description>
</signal>
<signal name="received_data">
<signal name="received_lobby_data">
<param index="0" name="data" type="Object" />
<param index="1" name="from_peer" type="LobbyPeer" />
<param index="1" name="is_private" type="bool" />
<description>
Signal generated after data is sent to the lobby.
</description>
</signal>
<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 received.
Signal generated after data is sent to peer.
</description>
</signal>
</signals>
Expand Down
5 changes: 4 additions & 1 deletion modules/blazium_sdk/doc_classes/LobbyInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<tutorials>
</tutorials>
<members>
<member name="data" type="Dictionary" setter="" getter="get_data" default="{}">
The lobby public data.
</member>
<member name="host" type="String" setter="" getter="get_host" default="&quot;&quot;">
The host id of the lobby.
</member>
Expand All @@ -22,7 +25,7 @@
The name of the lobby.
</member>
<member name="max_players" type="int" setter="" getter="get_max_players" default="0">
The maximum number of players allowed in the lobby.
The maximum number of players allowed in the lobby. 0 = unlimited
</member>
<member name="password_protected" type="bool" setter="" getter="is_password_protected" default="false">
</member>
Expand Down
2 changes: 2 additions & 0 deletions modules/blazium_sdk/doc_classes/LobbyPeer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<tutorials>
</tutorials>
<members>
<member name="data" type="Dictionary" setter="" getter="get_data" default="{}">
</member>
<member name="id" type="String" setter="" getter="get_id" default="&quot;&quot;">
Identifier of the peer.
</member>
Expand Down
85 changes: 68 additions & 17 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ void LobbyClient::_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::OBJECT, "host_data"), "", "get_host_data");
//ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "peer_data"), "", "get_peer_data");
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "host_data"), "", "get_host_data");
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>());
// Register methods
ClassDB::bind_method(D_METHOD("connect_to_lobby"), &LobbyClient::connect_to_lobby);
ClassDB::bind_method(D_METHOD("disconnect_from_lobby"), &LobbyClient::disconnect_from_lobby);
ClassDB::bind_method(D_METHOD("set_peer_name", "peer_name"), &LobbyClient::set_peer_name);
ClassDB::bind_method(D_METHOD("create_lobby", "title", "tags", "max_players", "password"), &LobbyClient::create_lobby, DEFVAL(Dictionary()), DEFVAL(4), DEFVAL(""));
ClassDB::bind_method(D_METHOD("join_lobby", "lobby_id", "password"), &LobbyClient::join_lobby, DEFVAL(""));
Expand All @@ -87,15 +88,17 @@ 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"), &LobbyClient::lobby_data);
ClassDB::bind_method(D_METHOD("send_peer_data", "data", "target_peer"), &LobbyClient::send_peer_data);
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);

// 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("lobby_notified", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("received_data", 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"), PropertyInfo(Variant::BOOL, "is_private")));
ADD_SIGNAL(MethodInfo("received_lobby_data", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::BOOL, "is_private")));
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")));
ADD_SIGNAL(MethodInfo("lobby_left", PropertyInfo(Variant::BOOL, "kicked")));
Expand All @@ -106,7 +109,7 @@ void LobbyClient::_bind_methods() {
ADD_SIGNAL(MethodInfo("peer_left", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::BOOL, "kicked")));
ADD_SIGNAL(MethodInfo("peer_disconnected", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("peer_messaged", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::STRING, "chat_message")));
ADD_SIGNAL(MethodInfo("peer_ready", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::BOOL, "ready")));
ADD_SIGNAL(MethodInfo("peer_ready", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::BOOL, "is_ready")));
ADD_SIGNAL(MethodInfo("log_updated", PropertyInfo(Variant::STRING, "command"), PropertyInfo(Variant::STRING, "logs")));
}

Expand Down Expand Up @@ -135,8 +138,13 @@ bool LobbyClient::connect_to_lobby() {

void LobbyClient::disconnect_from_lobby() {
if (connected) {
_socket->close();
_socket->close(1000, "Normal Closure");
connected = false;
host_data = Dictionary();
peer_data = Dictionary();
peer->set_data(Dictionary());
peers.clear();
lobby->set_data(Dictionary());
set_process_internal(false);
emit_signal("disconnected_from_lobby", _socket->get_close_reason());
emit_signal("log_updated", "disconnect_from_lobby", "Disconnected from: " + get_server_url());
Expand Down Expand Up @@ -402,12 +410,13 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::peer_notify(const Variant &p_peer_d
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data(const Variant &p_lobby_data) {
Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data(const Dictionary &p_lobby_data, bool p_is_private) {
String id = _increment_counter();
Dictionary command;
command["command"] = "lobby_data";
Dictionary data_dict;
data_dict["lobby_data"] = p_lobby_data;
data_dict["is_private"] = p_is_private;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Expand All @@ -420,13 +429,33 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data(const Variant &p_lobby_d
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::send_peer_data(const Variant &p_peer_data, const String &p_target_peer) {
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;
command["command"] = "data_to";
Dictionary data_dict;
data_dict["peer_data"] = p_peer_data;
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;
command["command"] = "data_to_all";
Dictionary data_dict;
data_dict["peer_data"] = p_peer_data;
data_dict["is_private"] = p_is_private;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
Expand Down Expand Up @@ -529,6 +558,9 @@ void LobbyClient::_receive_data(const Dictionary &p_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;
}
} break;
Expand All @@ -538,6 +570,7 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
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()));
Expand All @@ -552,10 +585,14 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
} else if (command == "lobby_left") {
lobby->set_dict(Dictionary());
peers.clear();
host_data = Dictionary();
peer_data = Dictionary();
emit_signal("lobby_left", false);
} else if (command == "lobby_kicked") {
lobby->set_dict(Dictionary());
peers.clear();
host_data = Dictionary();
peer_data = Dictionary();
emit_signal("lobby_left", true);
} else if (command == "lobby_sealed") {
Dictionary lobby_dict = data_dict.get("lobby", Dictionary());
Expand Down Expand Up @@ -692,18 +729,32 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
break;
}
}
} else if (command == "lobby_data_sent") {
host_data = data_dict.get("lobby_data", Variant());
} else if (command == "lobby_data") {
Dictionary lobby_data = data_dict.get("lobby_data", Dictionary());
bool is_private = data_dict.get("is_private", false);
if (is_private) {
host_data = lobby_data;
} else {
lobby->set_data(lobby_data);
}
emit_signal("received_lobby_data", lobby_data, is_private);
// nothing for now
} else if (command == "data_to") {
String from_peer_id = data_dict.get("from_peer", "");
String peer_data_variant = data_dict.get("peer_data", Variant());
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> from_peer = peers[i];
if (from_peer->get_id() == from_peer_id) {
Ref<LobbyPeer> updated_peer = peers[i];
if (updated_peer->get_id() == target_peer_id) {
// got peer data, update it
peer_data = peer_data_variant;
emit_signal("received_data", peer_data_variant, from_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
Loading

0 comments on commit 3c3644f

Please sign in to comment.