Skip to content

Commit

Permalink
add lobby chat and peer chat signals (#172)
Browse files Browse the repository at this point in the history
update all

update some methods

remove info

Update lobby_client.cpp

Update lobby_client.cpp

fix lint

fix lint

Update LobbyInfo.xml

Update LobbyClient.xml
  • Loading branch information
Ughuuu authored Dec 3, 2024
1 parent 00526ee commit 783fb66
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
40 changes: 27 additions & 13 deletions modules/blazium_sdk/doc_classes/LobbyClient.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,31 @@
Generates [signal lobby_left].
</description>
</method>
<method name="list_lobby">
<method name="list_lobbies">
<return type="ListLobbyResponse" />
<param index="0" name="start" type="int" default="0" />
<param index="1" name="count" type="int" default="10" />
<description>
Lists all lobbies.
</description>
</method>
<method name="lobby_data">
<method name="send_chat_message">
<return type="LobbyResponse" />
<param index="0" name="chat_message" type="String" />
<description>
Send a chat message.
Generates [signal peer_messaged].
</description>
</method>
<method name="send_lobby_data">
<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.
</description>
</method>
<method name="lobby_data_to">
<method name="send_lobby_data_to">
<return type="LobbyResponse" />
<param index="0" name="data" type="Variant" />
<param index="1" name="target_peer" type="String" />
Expand All @@ -81,15 +89,15 @@
Generates [signal received_data_to] signal.
</description>
</method>
<method name="lobby_ready">
<method name="set_lobby_ready">
<return type="LobbyResponse" />
<param index="0" name="ready" type="bool" />
<description>
Ready up in the lobby. You need to be in a lobby and unready to run this.
Generates [signal peer_ready].
</description>
</method>
<method name="seal_lobby">
<method name="set_lobby_sealed">
<return type="LobbyResponse" />
<param index="0" name="seal" type="bool" />
<description>
Expand Down Expand Up @@ -132,14 +140,6 @@
</member>
</members>
<signals>
<signal name="append_log">
<param index="0" name="command" type="String" />
<param index="1" name="info" type="String" />
<param index="2" name="logs" type="String" />
<description>
Signals a log from a command.
</description>
</signal>
<signal name="disconnected_from_lobby">
<description>
Disconnects from the lobby.
Expand Down Expand Up @@ -170,6 +170,13 @@
Signal generated after the host seals the lobby.
</description>
</signal>
<signal name="log_updated">
<param index="0" name="command" type="String" />
<param index="1" name="logs" type="String" />
<description>
Signals a log from a command.
</description>
</signal>
<signal name="peer_joined">
<param index="0" name="peer" type="LobbyPeer" />
<description>
Expand All @@ -183,6 +190,13 @@
Signal generated after a peer leaves the lobby.
</description>
</signal>
<signal name="peer_messaged">
<param index="0" name="peer" type="LobbyPeer" />
<param index="1" name="chat_message" type="String" />
<description>
Signal generated after a peer sends a chat message.
</description>
</signal>
<signal name="peer_named">
<param index="0" name="peer" type="LobbyPeer" />
<description>
Expand Down
60 changes: 45 additions & 15 deletions modules/blazium_sdk/lobby/lobby_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ void LobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_lobby", "title", "max_players", "password"), &LobbyClient::create_lobby, DEFVAL(4), DEFVAL(""));
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_lobby", "start", "count"), &LobbyClient::list_lobby, DEFVAL(0), DEFVAL(10));
ClassDB::bind_method(D_METHOD("list_lobbies", "start", "count"), &LobbyClient::list_lobby, 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("lobby_ready", "ready"), &LobbyClient::lobby_ready);
ClassDB::bind_method(D_METHOD("seal_lobby", "seal"), &LobbyClient::seal_lobby);
ClassDB::bind_method(D_METHOD("lobby_data", "data"), &LobbyClient::lobby_data);
ClassDB::bind_method(D_METHOD("lobby_data_to", "data", "target_peer"), &LobbyClient::lobby_data_to);
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);
ClassDB::bind_method(D_METHOD("set_lobby_sealed", "seal"), &LobbyClient::seal_lobby);
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);

// Register signals
ADD_SIGNAL(MethodInfo("disconnected_from_lobby"));
Expand All @@ -84,8 +85,9 @@ void LobbyClient::_bind_methods() {
ADD_SIGNAL(MethodInfo("lobby_sealed", PropertyInfo(Variant::BOOL, "sealed")));
ADD_SIGNAL(MethodInfo("peer_joined", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer")));
ADD_SIGNAL(MethodInfo("peer_left", PropertyInfo(Variant::OBJECT, "peer", PROPERTY_HINT_RESOURCE_TYPE, "LobbyPeer"), PropertyInfo(Variant::BOOL, "kicked")));
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("append_log", PropertyInfo(Variant::STRING, "command"), PropertyInfo(Variant::STRING, "info"), PropertyInfo(Variant::STRING, "logs")));
ADD_SIGNAL(MethodInfo("log_updated", PropertyInfo(Variant::STRING, "command"), PropertyInfo(Variant::STRING, "logs")));
}

bool LobbyClient::connect_to_lobby(const String &p_game_id) {
Expand All @@ -97,13 +99,13 @@ bool LobbyClient::connect_to_lobby(const String &p_game_id) {
Error err = _socket->connect_to_url(url);
if (err != OK) {
set_process_internal(false);
emit_signal("append_log", "error", "Unable to connect to lobby server at: " + url);
emit_signal("log_updated", "error", "Unable to connect to lobby server at: " + url);
connected = false;
return false;
}
connected = true;
set_process_internal(true);
emit_signal("append_log", "connect_to_lobby", "Connected to: " + url);
emit_signal("log_updated", "connect_to_lobby", "Connected to: " + url);
return true;
}

Expand Down Expand Up @@ -227,6 +229,24 @@ Ref<LobbyClient::LobbyResponse> LobbyClient::kick_peer(const String &p_peer_id)
return response;
}

Ref<LobbyClient::LobbyResponse> LobbyClient::lobby_chat(const String &p_chat_message) {
String id = _increment_counter();
Dictionary command;
command["command"] = "chat_lobby";
Dictionary data_dict;
command["data"] = data_dict;
data_dict["chat"] = p_chat_message;
data_dict["id"] = id;
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_ready(bool p_ready) {
String id = _increment_counter();
Dictionary command;
Expand Down Expand Up @@ -335,14 +355,14 @@ void LobbyClient::_notification(int p_what) {
Vector<uint8_t> packet_buffer;
Error err = _socket->get_packet_buffer(packet_buffer);
if (err != OK) {
emit_signal("append_log", "error", "Unable to get packet.");
emit_signal("log_updated", "error", "Unable to get packet.");
return;
}
String packet_string = String::utf8((const char *)packet_buffer.ptr(), packet_buffer.size());
_receive_data(JSON::parse_string(packet_string));
}
} else if (state == WebSocketPeer::STATE_CLOSED) {
emit_signal("append_log", "error", "WebSocket closed unexpectedly.");
emit_signal("log_updated", "error", "WebSocket closed unexpectedly.");
emit_signal("disconnected_from_lobby");
set_process_internal(false);
connected = false;
Expand All @@ -353,12 +373,12 @@ void LobbyClient::_notification(int p_what) {

void LobbyClient::_send_data(const Dictionary &p_data_dict) {
if (_socket->get_ready_state() != WebSocketPeer::STATE_OPEN) {
emit_signal("append_log", "error", "Socket is not ready.");
emit_signal("log_updated", "error", "Socket is not ready.");
return;
}
Error err = _socket->send_text(JSON::stringify(p_data_dict));
if (err != OK) {
emit_signal("append_log", "error", "No longer connected.");
emit_signal("log_updated", "error", "No longer connected.");
_socket->close(1000, "Disconnected");
}
}
Expand Down Expand Up @@ -396,7 +416,7 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
String message_id = data_dict.get("id", "");
Array command_array = _commands.get(message_id, Array());
_commands.erase(message_id);
emit_signal("append_log", command, message);
emit_signal("log_updated", command, message);
if (command_array.size() == 2 && command != "error") {
int command_type = command_array[0];
switch (command_type) {
Expand Down Expand Up @@ -481,6 +501,16 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
}
} 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", "");
for (int i = 0; i < peers.size(); ++i) {
Ref<LobbyPeer> found_peer = peers[i];
if (found_peer->get_id() == peer_id) {
emit_signal("peer_messaged", found_peer, chat_data);
break;
}
}
} else if (command == "peer_name") {
String peer_id = data_dict.get("peer_id", "");
String peer_name = data_dict.get("name", "");
Expand Down Expand Up @@ -599,11 +629,11 @@ void LobbyClient::_receive_data(const Dictionary &p_dict) {
}
} break;
default: {
emit_signal("append_log", "error", p_dict["message"]);
emit_signal("log_updated", "error", p_dict["message"]);
} break;
}
}
} else {
emit_signal("append_log", "error", "Unknown command received.");
emit_signal("log_updated", "error", "Unknown command received.");
}
}
1 change: 1 addition & 0 deletions modules/blazium_sdk/lobby/lobby_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ class LobbyClient : public BlaziumClient {
Ref<ListLobbyResponse> list_lobby(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> 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);
Expand Down

0 comments on commit 783fb66

Please sign in to comment.