From 82c49c4fe62f6d5aba8949c8dae3163043a355c7 Mon Sep 17 00:00:00 2001
From: Mounir Tohami <53877170+WhalesState@users.noreply.github.com>
Date: Thu, 28 Nov 2024 11:52:01 +0200
Subject: [PATCH] add lobby_id and try to add lobby_name
---
.../doc_classes/CreateLobbyResult.xml | 3 +
.../blazium_sdk/doc_classes/LobbyClient.xml | 20 +++-
modules/blazium_sdk/lobby/lobby_client.cpp | 94 +++++++++++--------
modules/blazium_sdk/lobby/lobby_client.h | 81 +++++++++-------
4 files changed, 120 insertions(+), 78 deletions(-)
diff --git a/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml b/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml
index aa8bc438a3ac..6d4b6bb557cb 100644
--- a/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml
+++ b/modules/blazium_sdk/doc_classes/CreateLobbyResult.xml
@@ -20,6 +20,9 @@
Gets the error message.
+
+ Gets the lobby id.
+
Gets the lobby name.
diff --git a/modules/blazium_sdk/doc_classes/LobbyClient.xml b/modules/blazium_sdk/doc_classes/LobbyClient.xml
index 0beac4b9c347..3aa5112786f1 100644
--- a/modules/blazium_sdk/doc_classes/LobbyClient.xml
+++ b/modules/blazium_sdk/doc_classes/LobbyClient.xml
@@ -18,8 +18,9 @@
-
-
+
+
+
Create a lobby and become host. If you are already in a lobby, you cannot create one. You need to leave first.
Will generate either error signal or lobby_created.
@@ -27,7 +28,7 @@
-
+
Joint a lobby. If you are already in a lobby, you cannot join another one. You need to leave first.
@@ -104,7 +105,7 @@
-
+
View data from a lobby. Returns lobby settings and peers.
@@ -113,6 +114,12 @@
+
+ True if the client is connected, else false.
+
+
+ The current lobby id.
+
Set to what url this lobby should connect to.
@@ -126,6 +133,11 @@
Signals a log from a command.
+
+
+ Disconnects from the lobby.
+
+
diff --git a/modules/blazium_sdk/lobby/lobby_client.cpp b/modules/blazium_sdk/lobby/lobby_client.cpp
index 920583fab9d1..9e14bf3266d3 100644
--- a/modules/blazium_sdk/lobby/lobby_client.cpp
+++ b/modules/blazium_sdk/lobby/lobby_client.cpp
@@ -32,6 +32,7 @@
#include "scene/main/node.h"
LobbyClient::LobbyClient() {
_socket = Ref(WebSocketPeer::create());
+ set_process_internal(false);
}
LobbyClient::~LobbyClient() {
@@ -42,14 +43,18 @@ LobbyClient::~LobbyClient() {
void LobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_server_url", "server_url"), &LobbyClient::set_server_url);
ClassDB::bind_method(D_METHOD("get_server_url"), &LobbyClient::get_server_url);
+ ClassDB::bind_method(D_METHOD("get_connected"), &LobbyClient::get_connected);
+ ClassDB::bind_method(D_METHOD("get_lobby_id"), &LobbyClient::get_lobby_id);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "server_url", PROPERTY_HINT_NONE, ""), "set_server_url", "get_server_url");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connected"), "", "get_connected");
+ ADD_PROPERTY(PropertyInfo(Variant::STRING, "lobby_id"), "", "get_lobby_id");
// Register methods
ClassDB::bind_method(D_METHOD("connect_to_lobby", "game_id"), &LobbyClient::connect_to_lobby);
- ClassDB::bind_method(D_METHOD("create_lobby", "max_players", "password"), &LobbyClient::create_lobby, DEFVAL(4), DEFVAL(""));
- ClassDB::bind_method(D_METHOD("join_lobby", "lobby_name", "password"), &LobbyClient::join_lobby, DEFVAL(""));
+ 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("view_lobby", "lobby_name", "password"), &LobbyClient::view_lobby, DEFVAL(""));
+ ClassDB::bind_method(D_METHOD("view_lobby", "lobby_id", "password"), &LobbyClient::view_lobby, DEFVAL(""));
ClassDB::bind_method(D_METHOD("kick_peer", "peer_id"), &LobbyClient::kick_peer);
ClassDB::bind_method(D_METHOD("lobby_ready"), &LobbyClient::lobby_ready);
ClassDB::bind_method(D_METHOD("lobby_unready"), &LobbyClient::lobby_unready);
@@ -59,6 +64,7 @@ void LobbyClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("lobby_data_to", "data", "target_peer"), &LobbyClient::lobby_data_to);
// Register signals
+ ADD_SIGNAL(MethodInfo("disconnected_from_lobby"));
ADD_SIGNAL(MethodInfo("peer_named", PropertyInfo(Variant::STRING, "peer"), PropertyInfo(Variant::STRING, "name")));
ADD_SIGNAL(MethodInfo("received_data", PropertyInfo(Variant::STRING, "data")));
ADD_SIGNAL(MethodInfo("received_data_to", PropertyInfo(Variant::STRING, "data")));
@@ -74,14 +80,20 @@ void LobbyClient::_bind_methods() {
ADD_SIGNAL(MethodInfo("append_log", PropertyInfo(Variant::STRING, "command"), PropertyInfo(Variant::STRING, "info"), PropertyInfo(Variant::STRING, "logs")));
}
-bool LobbyClient::connect_to_lobby(const String &game_id) {
+bool LobbyClient::connect_to_lobby(const String &p_game_id) {
+ if (connected) {
+ return true;
+ }
String lobby_url = get_server_url();
- String url = lobby_url + "?gameID=" + game_id;
+ String url = lobby_url + "?gameID=" + 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);
+ connected = false;
return false;
}
+ connected = true;
set_process_internal(true);
emit_signal("append_log", "connect_to_lobby", "Connected to: " + url);
return true;
@@ -91,14 +103,15 @@ String LobbyClient::_increment_counter() {
return String::num(_counter++);
}
-Ref LobbyClient::create_lobby(int max_players, const String &password) {
+Ref LobbyClient::create_lobby(const String &p_lobby_name, int p_max_players, const String &p_password) {
String id = _increment_counter();
Dictionary command;
command["command"] = "create_lobby";
Dictionary data_dict;
command["data"] = data_dict;
- data_dict["max_players"] = max_players;
- data_dict["password"] = password;
+ data_dict["name"] = p_lobby_name;
+ data_dict["max_players"] = p_max_players;
+ data_dict["password"] = p_password;
data_dict["id"] = id;
Array command_array;
Ref response;
@@ -110,14 +123,14 @@ Ref LobbyClient::create_lobby(int max_players,
return response;
}
-Ref LobbyClient::join_lobby(const String &lobby_name, const String &password) {
+Ref LobbyClient::join_lobby(const String &p_lobby_id, const String &p_password) {
String id = _increment_counter();
Dictionary command;
command["command"] = "join_lobby";
Dictionary data_dict;
command["data"] = data_dict;
- data_dict["lobby_name"] = lobby_name;
- data_dict["password"] = password;
+ data_dict["lobby_id"] = p_lobby_id;
+ data_dict["password"] = p_password;
data_dict["id"] = id;
Array command_array;
Ref response;
@@ -146,14 +159,14 @@ Ref LobbyClient::leave_lobby() {
return response;
}
-Ref LobbyClient::list_lobby(int start, int count) {
+Ref LobbyClient::list_lobby(int p_start, int p_count) {
String id = _increment_counter();
Dictionary command;
command["command"] = "list_lobby";
Dictionary data_dict;
data_dict["id"] = id;
- data_dict["start"] = start;
- data_dict["count"] = count;
+ data_dict["start"] = p_start;
+ data_dict["count"] = p_count;
command["data"] = data_dict;
Array command_array;
Ref response;
@@ -165,14 +178,14 @@ Ref LobbyClient::list_lobby(int start, int count
return response;
}
-Ref LobbyClient::view_lobby(const String &lobby_name, const String &password) {
+Ref LobbyClient::view_lobby(const String &p_lobby_id, const String &p_password) {
String id = _increment_counter();
Dictionary command;
command["command"] = "view_lobby";
Dictionary data_dict;
command["data"] = data_dict;
- data_dict["lobby_name"] = lobby_name;
- data_dict["password"] = password;
+ data_dict["lobby_id"] = p_lobby_id;
+ data_dict["password"] = p_password;
data_dict["id"] = id;
Array command_array;
Ref response;
@@ -184,13 +197,13 @@ Ref LobbyClient::view_lobby(const String &lobby_
return response;
}
-Ref LobbyClient::kick_peer(const String &peer_id) {
+Ref LobbyClient::kick_peer(const String &p_peer_id) {
String id = _increment_counter();
Dictionary command;
command["command"] = "kick_peer";
Dictionary data_dict;
command["data"] = data_dict;
- data_dict["peer_id"] = peer_id;
+ data_dict["peer_id"] = p_peer_id;
data_dict["id"] = id;
Array command_array;
Ref response;
@@ -236,12 +249,12 @@ Ref LobbyClient::lobby_unready() {
return response;
}
-Ref LobbyClient::set_peer_name(const String &peer_name) {
+Ref LobbyClient::set_peer_name(const String &p_peer_name) {
String id = _increment_counter();
Dictionary command;
command["command"] = "set_name";
Dictionary data_dict;
- data_dict["peer_name"] = peer_name;
+ data_dict["peer_name"] = p_peer_name;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
@@ -288,12 +301,12 @@ Ref LobbyClient::unseal_lobby() {
return response;
}
-Ref LobbyClient::lobby_data(const String &peer_data) {
+Ref LobbyClient::lobby_data(const String &p_peer_data) {
String id = _increment_counter();
Dictionary command;
command["command"] = "lobby_data";
Dictionary data_dict;
- data_dict["peer_data"] = peer_data;
+ data_dict["peer_data"] = p_peer_data;
data_dict["id"] = id;
command["data"] = data_dict;
Array command_array;
@@ -306,13 +319,13 @@ Ref LobbyClient::lobby_data(const String &peer_data)
return response;
}
-Ref LobbyClient::lobby_data_to(const String &peer_data, const String &target_peer) {
+Ref LobbyClient::lobby_data_to(const String &p_peer_data, const String &p_target_peer) {
String id = _increment_counter();
Dictionary command;
command["command"] = "data_to";
Dictionary data_dict;
- data_dict["peer_data"] = peer_data;
- data_dict["target_peer"] = target_peer;
+ 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;
@@ -344,41 +357,45 @@ void LobbyClient::_notification(int p_what) {
}
} else if (state == WebSocketPeer::STATE_CLOSED) {
emit_signal("append_log", "error", "WebSocket closed unexpectedly.");
+ emit_signal("disconnected_from_lobby");
+ set_process_internal(false);
+ connected = false;
}
} break;
}
}
-void LobbyClient::_send_data(const Dictionary &data_dict) {
+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.");
return;
}
- _socket->send_text(JSON::stringify(data_dict));
+ _socket->send_text(JSON::stringify(p_data_dict));
}
-void LobbyClient::_receive_data(const Dictionary &dict) {
- String command = dict.get("command", "error");
- String message = dict.get("message", command);
- Dictionary data_dict = dict.get("data", Dictionary());
+void LobbyClient::_receive_data(const Dictionary &p_dict) {
+ String command = p_dict.get("command", "error");
+ String message = p_dict.get("message", command);
+ Dictionary data_dict = p_dict.get("data", Dictionary());
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);
if (command == "lobby_created") {
- String lobby_name = data_dict.get("lobby_name", "");
+ lobby_id = data_dict.get("lobby_id", "");
if (command_array.size() == 2) {
Ref response = command_array[1];
if (response.is_valid()) {
Ref result;
result.instantiate();
- result->set_lobby_name(lobby_name);
+ result->set_lobby_id(lobby_id);
+ result->set_lobby_name(data_dict.get("lobby_name", ""));
response->emit_signal("finished", result);
}
}
- emit_signal("lobby_created", lobby_name);
+ emit_signal("lobby_created", lobby_id);
} else if (command == "joined_lobby") {
- String lobby_name = data_dict.get("lobby_name", "");
+ lobby_id = data_dict.get("lobby_id", "");
if (command_array.size() == 2) {
Ref response = command_array[1];
if (response.is_valid()) {
@@ -387,7 +404,7 @@ void LobbyClient::_receive_data(const Dictionary &dict) {
response->emit_signal("finished", result);
}
}
- emit_signal("lobby_joined", lobby_name);
+ emit_signal("lobby_joined", lobby_id);
} else if (command == "lobby_left") {
// Either if you leave a lobby, or if you get kicked
if (command_array.size() == 2) {
@@ -398,6 +415,7 @@ void LobbyClient::_receive_data(const Dictionary &dict) {
response->emit_signal("finished", result);
}
}
+ lobby_id = "";
emit_signal("lobby_left");
} else if (command == "lobby_sealed") {
// Either if you seal a lobby, or if host seals
@@ -603,7 +621,7 @@ void LobbyClient::_receive_data(const Dictionary &dict) {
}
} break;
default: {
- emit_signal("append_log", "error", dict["message"]);
+ emit_signal("append_log", "error", p_dict["message"]);
} break;
}
}
diff --git a/modules/blazium_sdk/lobby/lobby_client.h b/modules/blazium_sdk/lobby/lobby_client.h
index ac3b2076cdba..c55d07a97425 100644
--- a/modules/blazium_sdk/lobby/lobby_client.h
+++ b/modules/blazium_sdk/lobby/lobby_client.h
@@ -44,6 +44,7 @@ class LobbyClient : public BlaziumClient {
LOBBY_LIST
};
String server_url = "wss://lobby.blazium.app/connect";
+ String lobby_id;
public:
class CreateLobbyResponse : public RefCounted {
@@ -58,26 +59,31 @@ class LobbyClient : public BlaziumClient {
class CreateLobbyResult : public RefCounted {
GDCLASS(CreateLobbyResult, RefCounted);
String error;
+ String lobby_id;
String lobby_name;
protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("has_error"), &CreateLobbyResult::has_error);
ClassDB::bind_method(D_METHOD("get_error"), &CreateLobbyResult::get_error);
+ ClassDB::bind_method(D_METHOD("get_lobby_id"), &CreateLobbyResult::get_lobby_id);
ClassDB::bind_method(D_METHOD("get_lobby_name"), &CreateLobbyResult::get_lobby_name);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "error"), "", "get_error");
+ ADD_PROPERTY(PropertyInfo(Variant::STRING, "lobby_id"), "", "get_lobby_id");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "lobby_name"), "", "get_lobby_name");
}
public:
- void set_error(String p_error) { this->error = p_error; }
- void set_lobby_name(String p_lobby_name) { this->lobby_name = p_lobby_name; }
+ void set_error(const String &p_error) { this->error = p_error; }
+ void set_lobby_id(const String &p_lobby_id) { this->lobby_id = p_lobby_id; }
+ void set_lobby_name(const String &p_lobby_name) { this->lobby_name = p_lobby_name; }
bool has_error() const { return !error.is_empty(); }
String get_error() const { return error; }
+ String get_lobby_id() const { return lobby_id; }
String get_lobby_name() const { return lobby_name; }
- CreateLobbyResult(const CreateLobbyResult &other) :
- error(other.error), lobby_name(other.lobby_name) {}
+ CreateLobbyResult(const CreateLobbyResult &p_other) :
+ error(p_other.error), lobby_id(p_other.lobby_id) {}
CreateLobbyResult() {}
};
CreateLobbyResponse(const CreateLobbyResponse &other) {}
@@ -109,11 +115,11 @@ class LobbyClient : public BlaziumClient {
bool has_error() const { return !error.is_empty(); }
String get_error() const { return error; }
- LobbyResult(const LobbyResult &other) :
- error(other.error) {}
+ LobbyResult(const LobbyResult &p_other) :
+ error(p_other.error) {}
LobbyResult() {}
};
- LobbyResponse(const LobbyResponse &other) {}
+ LobbyResponse(const LobbyResponse &p_other) {}
LobbyResponse() {}
};
class ListLobbyResponse : public RefCounted {
@@ -141,17 +147,17 @@ class LobbyClient : public BlaziumClient {
}
public:
- void set_error(String p_error) { this->error = p_error; }
- void set_lobbies(TypedArray p_lobbies) { this->lobbies = p_lobbies; }
+ void set_error(const String &p_error) { this->error = p_error; }
+ void set_lobbies(const TypedArray &p_lobbies) { this->lobbies = p_lobbies; }
bool has_error() const { return !error.is_empty(); }
String get_error() const { return error; }
TypedArray get_lobbies() const { return lobbies; }
- ListLobbyResult(const ListLobbyResult &other) :
- error(other.error), lobbies(other.lobbies) {}
+ ListLobbyResult(const ListLobbyResult &p_other) :
+ error(p_other.error), lobbies(p_other.lobbies) {}
ListLobbyResult() {}
};
- ListLobbyResponse(const ListLobbyResponse &other) {}
+ ListLobbyResponse(const ListLobbyResponse &p_other) {}
ListLobbyResponse() {}
};
class LobbyInfo : public RefCounted {
@@ -171,15 +177,15 @@ class LobbyClient : public BlaziumClient {
}
public:
- void set_host(String p_host) { this->host = p_host; }
+ void set_host(const String &p_host) { this->host = p_host; }
void set_max_players(int p_max_players) { this->max_players = p_max_players; }
void set_sealed(bool p_sealed) { this->sealed = p_sealed; }
String get_host() const { return host; }
int get_max_players() const { return max_players; }
bool is_sealed() const { return sealed; }
- LobbyInfo(const LobbyInfo &other) :
- host(other.host), max_players(other.max_players), sealed(other.sealed) {}
+ LobbyInfo(const LobbyInfo &p_other) :
+ host(p_other.host), max_players(p_other.max_players), sealed(p_other.sealed) {}
LobbyInfo() {}
};
class LobbyPeer : public RefCounted {
@@ -199,15 +205,15 @@ class LobbyClient : public BlaziumClient {
}
public:
- void set_id(String p_id) { this->id = p_id; }
- void set_name(String p_name) { this->name = p_name; }
+ void set_id(const String &p_id) { this->id = p_id; }
+ void set_name(const String &p_name) { this->name = p_name; }
void set_ready(bool p_ready) { this->ready = p_ready; }
String get_id() const { return id; }
String get_name() const { return name; }
bool is_ready() const { return ready; }
- LobbyPeer(const LobbyPeer &other) :
- id(other.id), name(other.name), ready(other.ready) {}
+ LobbyPeer(const LobbyPeer &p_other) :
+ id(p_other.id), name(p_other.name), ready(p_other.ready) {}
LobbyPeer() {}
};
class ViewLobbyResponse : public RefCounted {
@@ -236,9 +242,9 @@ class LobbyClient : public BlaziumClient {
}
public:
- void set_error(String p_error) { this->error = p_error; }
- void set_peers(TypedArray p_peers) { this->peers = p_peers; }
- void set_lobby_info(Ref p_lobby_info) { this->lobby_info = p_lobby_info; }
+ void set_error(const String &p_error) { this->error = p_error; }
+ void set_peers(const TypedArray &p_peers) { this->peers = p_peers; }
+ void set_lobby_info(const Ref &p_lobby_info) { this->lobby_info = p_lobby_info; }
bool has_error() const { return !error.is_empty(); }
String get_error() const { return error; }
@@ -250,18 +256,19 @@ class LobbyClient : public BlaziumClient {
~ViewLobbyResult() {
}
};
- ViewLobbyResponse(const ViewLobbyResponse &other) {}
+ ViewLobbyResponse(const ViewLobbyResponse &p_other) {}
ViewLobbyResponse() {}
};
private:
Ref _socket;
int _counter = 0;
+ bool connected = false;
Dictionary _commands;
- String _get_data_from_dict(const Dictionary &dict, const String &key);
- void _receive_data(const Dictionary &data);
- void _send_data(const Dictionary &data);
+ String _get_data_from_dict(const Dictionary &p_dict, const String &p_key);
+ void _receive_data(const Dictionary &p_data);
+ void _send_data(const Dictionary &p_data);
void _wait_ready();
String _increment_counter();
@@ -270,22 +277,24 @@ class LobbyClient : public BlaziumClient {
static void _bind_methods();
public:
- bool connect_to_lobby(const String &game_id);
- void set_server_url(String p_server_url) { this->server_url = p_server_url; }
+ bool connect_to_lobby(const String &p_game_id);
+ void set_server_url(const String &p_server_url) { this->server_url = p_server_url; }
String get_server_url() { return server_url; }
- Ref create_lobby(int max_players, const String &password);
- Ref join_lobby(const String &lobby_name, const String &password);
+ bool get_connected() { return connected; }
+ String get_lobby_id() { return lobby_id; }
+ Ref create_lobby(const String &p_lobby_name, int p_max_players, const String &p_password);
+ Ref join_lobby(const String &p_lobby_id, const String &p_password);
Ref leave_lobby();
- Ref list_lobby(int start, int count);
- Ref view_lobby(const String &lobby_name, const String &password);
- Ref kick_peer(const String &peer_id);
+ Ref list_lobby(int p_start, int p_count);
+ Ref view_lobby(const String &p_lobby_id, const String &p_password);
+ Ref kick_peer(const String &p_peer_id);
Ref lobby_ready();
Ref lobby_unready();
- Ref set_peer_name(const String &peer_name);
+ Ref set_peer_name(const String &p_peer_name);
Ref seal_lobby();
Ref unseal_lobby();
- Ref lobby_data(const String &peer_data);
- Ref lobby_data_to(const String &peer_data, const String &target_peer);
+ Ref lobby_data(const String &p_peer_data);
+ Ref lobby_data_to(const String &p_peer_data, const String &p_target_peer);
LobbyClient();
~LobbyClient();