Skip to content

Commit

Permalink
update lobby call
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Dec 7, 2024
1 parent b42a5f2 commit 0d32e22
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 33 deletions.
59 changes: 40 additions & 19 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
Generates [signal lobby_created] signal.
</description>
</method>
<method name="get_host_data">
<return type="Variant" />
<description>
Returns the lobby data. Only works if you are host.
</description>
</method>
<method name="is_host">
<return type="bool" />
<description>
Expand Down Expand Up @@ -67,6 +73,23 @@
Lists all lobbies. Lobbies that are sealed won't show in the list, except if you disconnected and trying to reconnect to a lobby.
</description>
</method>
<method name="notify_lobby">
<return type="LobbyResponse" />
<param index="0" name="data" type="Variant" />
<description>
Send a notification either to the host, or if you are host send data to all peers.
Generates [signal notification_received] signal.
</description>
</method>
<method name="notify_peer">
<return type="LobbyResponse" />
<param index="0" name="data" type="Variant" />
<param index="1" name="target_peer" type="String" />
<description>
Send a notification to a peer, works only if you are host.
Generates [signal notification_received] signal.
</description>
</method>
<method name="send_chat_message">
<return type="LobbyResponse" />
<param index="0" name="chat_message" type="String" />
Expand All @@ -79,17 +102,15 @@
<return type="LobbyResponse" />
<param index="0" name="data" type="Variant" />
<description>
Send data either to the host, or if you are host send data to all peers.
Generates [signal received_data] signal.
Send data to all peers. If you are not host, sends to host instead.
</description>
</method>
<method name="send_lobby_data_to">
<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 either to a peer, works only if you are host.
Generates [signal received_data_to] signal.
Send data to a peer.
</description>
</method>
<method name="set_lobby_ready">
Expand Down Expand Up @@ -161,6 +182,13 @@
Signal generated after you connect to the lobby.
</description>
</signal>
<signal name="data_received">
<param index="0" name="data" type="Object" />
<param index="1" name="from_peer" type="LobbyPeer" />
<description>
Signal generated after data is received.
</description>
</signal>
<signal name="disconnected_from_lobby">
<param index="0" name="reason" type="String" />
<description>
Expand Down Expand Up @@ -206,6 +234,13 @@
Signals a log from a command.
</description>
</signal>
<signal name="notification_received">
<param index="0" name="data" type="Object" />
<param index="1" name="from_peer" type="LobbyPeer" />
<description>
Signal generated after a notification is received.
</description>
</signal>
<signal name="peer_disconnected">
<param index="0" name="peer" type="LobbyPeer" />
<description>
Expand Down Expand Up @@ -251,19 +286,5 @@
Signal generated after a peer reconnects.
</description>
</signal>
<signal name="received_data">
<param index="0" name="data" type="Object" />
<param index="1" name="from_peer" type="LobbyPeer" />
<description>
Signal generated after a lobby_data call.
</description>
</signal>
<signal name="received_data_to">
<param index="0" name="data" type="Object" />
<param index="1" name="from_peer" type="LobbyPeer" />
<description>
Signal generated after a data_to call.
</description>
</signal>
</signals>
</class>
3 changes: 3 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,9 @@
<tutorials>
</tutorials>
<members>
<member name="data" type="Variant" setter="" getter="get_data" default="null">
Data of the peer.
</member>
<member name="id" type="String" setter="" getter="get_id" default="&quot;&quot;">
Identifier of the peer.
</member>
Expand Down
78 changes: 66 additions & 12 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void LobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_lobby"), &LobbyClient::get_lobby);
ClassDB::bind_method(D_METHOD("get_peer"), &LobbyClient::get_peer);
ClassDB::bind_method(D_METHOD("get_peers"), &LobbyClient::get_peers);
ClassDB::bind_method(D_METHOD("get_host_data"), &LobbyClient::get_host_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 @@ -81,15 +82,17 @@ void LobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_lobby_ready", "ready"), &LobbyClient::lobby_ready);
ClassDB::bind_method(D_METHOD("set_lobby_tags", "tags"), &LobbyClient::set_lobby_tags);
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("send_lobby_data", "data"), &LobbyClient::lobby_data);
ClassDB::bind_method(D_METHOD("send_lobby_data_to", "data", "target_peer"), &LobbyClient::lobby_data_to);
ClassDB::bind_method(D_METHOD("send_peer_data", "data", "target_peer"), &LobbyClient::peer_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_data", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("received_data_to", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("notification_received", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("data_received", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::OBJECT, "from_peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
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 Down Expand Up @@ -359,10 +362,10 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::seal_lobby(bool seal) {
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data(const Variant &p_peer_data) {
Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_notify(const Variant &p_peer_data) {
String id = _increment_counter();
Dictionary command;
command["command"] = "lobby_data";
command["command"] = "lobby_notify";
Dictionary data_dict;
data_dict["peer_data"] = p_peer_data;
data_dict["id"] = id;
Expand All @@ -377,7 +380,44 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data(const Variant &p_peer_da
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_data_to(const Variant &p_peer_data, const String &p_target_peer) {
Ref<LobbyClient::LobbyResponse> LobbyClient::peer_notify(const Variant &p_peer_data, const String &p_target_peer) {
String id = _increment_counter();
Dictionary command;
command["command"] = "notify_to";
Dictionary data_dict;
data_dict["peer_data"] = p_peer_data;
data_dict["target_peer"] = p_target_peer;
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::lobby_data(const Variant &p_lobby_data) {
String id = _increment_counter();
Dictionary command;
command["command"] = "lobby_data";
Dictionary data_dict;
data_dict["lobby_data"] = p_lobby_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<LobbyClient::LobbyResponse> LobbyClient::peer_data(const Variant &p_peer_data, const String &p_target_peer) {
String id = _increment_counter();
Dictionary command;
command["command"] = "data_to";
Expand Down Expand Up @@ -655,35 +695,49 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
}
}
sort_peers_by_id(peers);
} else if (command == "lobby_data") {
} else if (command == "lobby_notify") {
String from_peer_id = data_dict.get("from_peer", "");
for (int i = 0; i < peers.size(); ++i) {
Ref<LobbyPeer> from_peer = peers[i];
if (from_peer->get_id() == from_peer_id) {
emit_signal("notification_received", data_dict.get("peer_data", Variant()), from_peer);
break;
}
}
} else if (command == "lobby_data_sent") {
// nothing for now
} else if (command == "data_to") {
String from_peer_id = data_dict.get("from_peer", "");
for (int i = 0; i < peers.size(); ++i) {
Ref<LobbyPeer> from_peer = peers[i];
if (from_peer->get_id() == from_peer_id) {
emit_signal("received_data", data_dict.get("peer_data", Variant()), from_peer);
emit_signal("data_received", data_dict.get("peer_data", Variant()), from_peer);
break;
}
}
} else if (command == "lobby_call") {
// authoritative call
Ref<AuthoritativeClient::LobbyCallResponse> response = command_array[1];
if (response.is_valid()) {
Ref<AuthoritativeClient::LobbyCallResponse::LobbyCallResult> result = Ref<AuthoritativeClient::LobbyCallResponse::LobbyCallResult>(memnew(AuthoritativeClient::LobbyCallResponse::LobbyCallResult));
result->set_result(data_dict.get("result", ""));
response->emit_signal("finished", result);
}
} else if (command == "data_to") {
} else if (command == "notified_to") {
String from_peer_id = data_dict.get("from_peer", "");
for (int i = 0; i < peers.size(); ++i) {
Ref<LobbyPeer> from_peer = peers[i];
if (from_peer->get_id() == from_peer_id) {
emit_signal("received_data_to", data_dict.get("peer_data", Variant()), from_peer);
emit_signal("notification_received", data_dict.get("peer_data", Variant()), from_peer);
break;
}
}
} else if (command == "lobby_data_sent") {
// nothing for now
} else if (command == "data_to_sent") {
// nothing for now
} else if (command == "lobby_notify_sent") {
// nothing for now
} else if (command == "notify_to_sent") {
// nothing for now
} else if (command == "error") {
if (command_array.size() == 2) {
int command_type = command_array[0];
Expand Down
16 changes: 14 additions & 2 deletions modules/blazium_sdk/lobby/lobby_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "../blazium_client.h"
#include "core/io/json.h"
#include "core/variant/variant.h"
#include "modules/websocket/websocket_peer.h"

class LobbyInfo : public Resource {
Expand Down Expand Up @@ -122,38 +123,45 @@ class LobbyPeer : public Resource {
GDCLASS(LobbyPeer, Resource);
String id;
String peer_name;
Variant data;
bool ready = false;

protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("get_id"), &LobbyPeer::get_id);
ClassDB::bind_method(D_METHOD("get_peer_name"), &LobbyPeer::get_peer_name);
ClassDB::bind_method(D_METHOD("is_ready"), &LobbyPeer::is_ready);
ClassDB::bind_method(D_METHOD("get_data"), &LobbyPeer::get_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "", "get_id");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "peer_name"), "", "get_peer_name");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ready"), "", "is_ready");
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data"), "", "get_data");
}

public:
void set_id(const String &p_id) { this->id = p_id; }
void set_peer_name(const String &p_peer_name) { this->peer_name = p_peer_name; }
void set_ready(bool p_ready) { this->ready = p_ready; }
void set_data(const Dictionary &p_data) { this->data = p_data; }
void set_dict(const Dictionary &p_dict) {
this->set_id(p_dict.get("id", ""));
this->set_peer_name(p_dict.get("name", ""));
this->set_ready(p_dict.get("ready", ""));
this->set_data(p_dict.get("data", Variant()));
}
Dictionary get_dict() const {
Dictionary dict;
dict["id"] = this->get_id();
dict["name"] = this->get_peer_name();
dict["ready"] = this->is_ready();
dict["data"] = this->get_data();
return dict;
}

String get_id() const { return id; }
String get_peer_name() const { return peer_name; }
bool is_ready() const { return ready; }
Variant get_data() const { return data; }
LobbyPeer() {}
};

Expand All @@ -164,6 +172,7 @@ class LobbyClient : public BlaziumClient {
String server_url;
String reconnection_token;
String game_id = "";
Variant host_data;
Ref<LobbyInfo> lobby;
Ref<LobbyPeer> peer;
TypedArray<LobbyPeer> peers;
Expand Down Expand Up @@ -307,6 +316,7 @@ class LobbyClient : public BlaziumClient {
String get_game_id() { return game_id; }
bool is_host() { return lobby->get_host() == peer->get_id(); }
bool get_connected() { return connected; }
Variant get_host_data() { return host_data; }
void set_lobby(const Ref<LobbyInfo> &p_lobby) { this->lobby = p_lobby; }
Ref<LobbyInfo> get_lobby() { return lobby; }
void set_peer(const Ref<LobbyPeer> &p_peer) { this->peer = p_peer; }
Expand All @@ -327,8 +337,10 @@ class LobbyClient : public BlaziumClient {
Ref<LobbyResponse> set_peer_name(const String &p_peer_name);
Ref<LobbyResponse> seal_lobby(bool seal);
Ref<LobbyResponse> lobby_call(const String &p_method, const Array &p_args);
Ref<LobbyResponse> lobby_data(const Variant &p_peer_data);
Ref<LobbyResponse> lobby_data_to(const Variant &p_peer_data, const String &p_target_peer);
Ref<LobbyResponse> lobby_notify(const Variant &p_peer_data);
Ref<LobbyResponse> peer_notify(const Variant &p_peer_data, const String &p_target_peer);
Ref<LobbyResponse> lobby_data(const Variant &p_lobby_data);
Ref<LobbyResponse> peer_data(const Variant &p_peer_data, const String &p_target_peer);

LobbyClient();
~LobbyClient();
Expand Down

0 comments on commit 0d32e22

Please sign in to comment.