diff --git a/assets/Json/createServer.json b/assets/Json/createServer.json index 8fcac3ec..7f523dc8 100644 --- a/assets/Json/createServer.json +++ b/assets/Json/createServer.json @@ -297,6 +297,16 @@ "y": 500 } } - ] + ], + "errorMessage": { + "type": "text", + "color": "red", + "size": 2, + "text": "SERVER ALREADY CREATED", + "position" : { + "x": 3200, + "y": 8500 + } + } } \ No newline at end of file diff --git a/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp b/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp index aae16487..2f5448b2 100644 --- a/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp +++ b/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp @@ -15,6 +15,7 @@ #include "Parallax.hpp" #include "ResourcesManager.hpp" #include "SelectLobbySystems.hpp" +#include "Menu.hpp" #include "init.hpp" namespace Menu { @@ -187,7 +188,21 @@ namespace Menu { { auto arrInputBox = Registry::getInstance().getComponents(); auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Types::InputBox)}); - + static bool isExist = false; + + if (Nitwork::NitworkClient::getInstance().serverAlreadyCreated() && !isExist) { + try { + nlohmann::json jsonData = Json::getInstance().getDataByJsonType( + ResourcesManager::getPathByJsonType(JsonType::CREATE_SERVER), + "errorMessage"); + ::Menu::MenuBuilder::getInstance().initMenuEntity( + jsonData); + } catch (std::runtime_error &err) { + Logger::warn(err.what()); + } + isExist = true; + return; + } for (auto id : ids) { if (arrInputBox[id].name == "port") { Nitwork::NitworkClient::getInstance().createForkedServer(arrInputBox[id].text); diff --git a/src/Nitwork/NitworkClient.cpp b/src/Nitwork/NitworkClient.cpp index b75c3198..c76b7939 100644 --- a/src/Nitwork/NitworkClient.cpp +++ b/src/Nitwork/NitworkClient.cpp @@ -421,6 +421,14 @@ namespace Nitwork { addPacketToSend(packet); } + bool NitworkClient::serverAlreadyCreated() + { + if (_serverPids.size() > 0) { + return true; + } + return (false); + } + void NitworkClient::createForkedServer(const std::string &port) { if (_serverPids.size() > 0) { diff --git a/src/Nitwork/NitworkClient.hpp b/src/Nitwork/NitworkClient.hpp index dc0659d5..514d7a6e 100644 --- a/src/Nitwork/NitworkClient.hpp +++ b/src/Nitwork/NitworkClient.hpp @@ -117,6 +117,11 @@ namespace Nitwork { void addCreateLobbyMsg(const std::string &name, enum gameType_e gameType, unsigned int maxNbPlayer); + /** + * @brief Check if the server is created by the client + */ + bool serverAlreadyCreated(); + /** * @brief Create a server * @param port port of the server