Skip to content

Commit

Permalink
Lobby client add has password and update lobby_data events (#167)
Browse files Browse the repository at this point in the history
* Add has password. Update lobby data a bit

* Update LobbyInfo.xml
  • Loading branch information
Ughuuu authored Dec 1, 2024
1 parent e6f0333 commit f7b8d6f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@
</signal>
<signal name="received_data">
<param index="0" name="data" type="Object" />
<param index="1" name="from_peer" type="String" />
<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="String" />
<param index="1" name="from_peer" type="LobbyPeer" />
<description>
Signal generated after a data_to call.
</description>
Expand Down
2 changes: 2 additions & 0 deletions modules/blazium_sdk/doc_classes/LobbyInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<member name="max_players" type="int" setter="" getter="get_max_players" default="0">
The maximum number of players allowed in the lobby.
</member>
<member name="password_protected" type="bool" setter="" getter="is_password_protected" default="true">
</member>
<member name="players" type="int" setter="" getter="get_players" default="0">
The number of players currently in the lobby.
</member>
Expand Down
30 changes: 24 additions & 6 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ LobbyClient::LobbyClient() {
}

LobbyClient::~LobbyClient() {
_socket->close(1000, "Disconnected");
_socket->close();
set_process_internal(false);
}

Expand Down Expand Up @@ -76,8 +76,8 @@ void LobbyClient::_bind_methods() {
// Register signals
ADD_SIGNAL(MethodInfo("disconnected_from_lobby"));
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::STRING, "from_peer")));
ADD_SIGNAL(MethodInfo("received_data_to", PropertyInfo(Variant::OBJECT, "data"), PropertyInfo(Variant::STRING, "from_peer")));
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("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"));
Expand Down Expand Up @@ -356,7 +356,11 @@ void LobbyClient::_send_data(const Dictionary &p_data_dict) {
emit_signal("append_log", "error", "Socket is not ready.");
return;
}
_socket->send_text(JSON::stringify(p_data_dict));
Error err = _socket->send_text(JSON::stringify(p_data_dict));
if (err != OK) {
emit_signal("append_log", "error", "No longer connected.");
_socket->close(1000, "Disconnected");
}
}

void update_peers(Dictionary p_data_dict, TypedArray<LobbyPeer> &peers) {
Expand Down Expand Up @@ -542,9 +546,23 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
}
sort_peers_by_id(peers);
} else if (command == "lobby_data") {
emit_signal("received_data", data_dict.get("peer_data", Variant()), data_dict.get("from_peer", ""));
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);
break;
}
}
} else if (command == "data_to") {
emit_signal("received_data_to", data_dict.get("peer_data", Variant()), data_dict.get("from_peer", ""));
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);
break;
}
}
} else if (command == "lobby_data_sent") {
// nothing for now
} else if (command == "data_to_sent") {
Expand Down
7 changes: 7 additions & 0 deletions modules/blazium_sdk/lobby/lobby_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ class LobbyInfo : public Resource {
int max_players = 0;
int players = 0;
bool sealed = false;
bool password_protected;

protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("get_host"), &LobbyInfo::get_host);
ClassDB::bind_method(D_METHOD("get_max_players"), &LobbyInfo::get_max_players);
ClassDB::bind_method(D_METHOD("is_sealed"), &LobbyInfo::is_sealed);
ClassDB::bind_method(D_METHOD("is_password_protected"), &LobbyInfo::is_password_protected);
ClassDB::bind_method(D_METHOD("get_id"), &LobbyInfo::get_id);
ClassDB::bind_method(D_METHOD("get_lobby_name"), &LobbyInfo::get_lobby_name);
ClassDB::bind_method(D_METHOD("get_host_name"), &LobbyInfo::get_host_name);
Expand All @@ -62,6 +64,7 @@ class LobbyInfo : public Resource {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "host"), "", "get_host");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_players"), "", "get_max_players");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sealed"), "", "is_sealed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "password_protected"), "", "is_password_protected");
}

public:
Expand All @@ -72,6 +75,7 @@ class LobbyInfo : public Resource {
void set_max_players(int p_max_players) { this->max_players = p_max_players; }
void set_players(int p_players) { this->players = p_players; }
void set_sealed(bool p_sealed) { this->sealed = p_sealed; }
void set_password_protected(bool p_password_protected) { this->password_protected = p_password_protected; }

void set_dict(const Dictionary &p_dict) {
this->set_host(p_dict.get("host", ""));
Expand All @@ -81,6 +85,7 @@ class LobbyInfo : public Resource {
this->set_id(p_dict.get("id", ""));
this->set_lobby_name(p_dict.get("name", ""));
this->set_host_name(p_dict.get("host_name", ""));
this->set_password_protected(p_dict.get("has_password", false));
}
Dictionary get_dict() const {
Dictionary dict;
Expand All @@ -91,6 +96,7 @@ class LobbyInfo : public Resource {
dict["id"] = this->get_id();
dict["name"] = this->get_lobby_name();
dict["host_name"] = this->get_host_name();
dict["has_password"] = this->is_password_protected();
return dict;
}

Expand All @@ -101,6 +107,7 @@ class LobbyInfo : public Resource {
int get_max_players() const { return max_players; }
int get_players() const { return players; }
bool is_sealed() const { return sealed; }
bool is_password_protected() const { return password_protected; }
LobbyInfo() {}
};

Expand Down

0 comments on commit f7b8d6f

Please sign in to comment.