diff --git a/Flakkari/Server/Game/GameManager.cpp b/Flakkari/Server/Game/GameManager.cpp index 9d95a7fa..ff92c7b5 100644 --- a/Flakkari/Server/Game/GameManager.cpp +++ b/Flakkari/Server/Game/GameManager.cpp @@ -14,9 +14,6 @@ namespace Flakkari { -std::shared_ptr GameManager::_instance = nullptr; -std::mutex GameManager::_mutex; - #define STR_ADDRESS std::string(*client->getAddress()) GameManager::GameManager() @@ -68,19 +65,8 @@ GameManager::GameManager() } } -std::shared_ptr GameManager::getInstance() -{ - std::lock_guard lock(_mutex); - if (!_instance) - _instance = std::make_shared(); - return _instance; -} - int GameManager::addGame(std::string gameName) { - auto &_gamesStore = getInstance()->_gamesStore; - auto _game_dir = getInstance()->_game_dir; - if (_gamesStore.find(gameName) != _gamesStore.end()) return FLAKKARI_LOG_ERROR("game already loaded"), 1; std::ifstream configFile(_game_dir + "/" + gameName + "/config.cfg"); @@ -100,7 +86,6 @@ int GameManager::addGame(std::string gameName) std::shared_ptr GameManager::getGame(std::string gameName) { - auto &_gamesStore = getInstance()->_gamesStore; if (_gamesStore.find(gameName) == _gamesStore.end()) return FLAKKARI_LOG_ERROR("game not found"), nullptr; return std::make_shared(gameName, _gamesStore[gameName]); @@ -108,7 +93,6 @@ std::shared_ptr GameManager::getGame(std::string gameName) std::vector> GameManager::getGamesInstances() { - auto &_gamesInstances = getInstance()->_gamesInstances; std::vector> gamesInstances; for (auto &game : _gamesInstances) @@ -118,9 +102,6 @@ std::vector> GameManager::getGamesInstances() int GameManager::updateGame(std::string gameName) { - auto &_gamesStore = getInstance()->_gamesStore; - auto _game_dir = getInstance()->_game_dir; - if (_gamesStore.find(gameName) == _gamesStore.end()) return FLAKKARI_LOG_ERROR("game not found"), 1; std::ifstream configFile(_game_dir + "/" + gameName + "/config.cfg"); @@ -137,7 +118,6 @@ int GameManager::updateGame(std::string gameName) int GameManager::removeGame(std::string gameName) { - auto &_gamesStore = getInstance()->_gamesStore; if (_gamesStore.find(gameName) == _gamesStore.end()) return FLAKKARI_LOG_ERROR("game not found"), 1; @@ -149,7 +129,6 @@ int GameManager::removeGame(std::string gameName) void GameManager::listGames() { - auto &_gamesStore = getInstance()->_gamesStore; std::string gamesList = "Games list:\n"; for (const auto &game : _gamesStore) @@ -159,20 +138,14 @@ void GameManager::listGames() void GameManager::addClientToGame(std::string gameName, std::shared_ptr client) { - auto current = getInstance(); - auto &gamesStore = current->_gamesStore; - auto &gamesInstances = current->_gamesInstances; - auto &waitingClients = current->_waitingClients; - - if (gamesStore.find(gameName) == gamesStore.end()) - { + if (_gamesStore.find(gameName) == _gamesStore.end()) { FLAKKARI_LOG_ERROR("game not found"); client.reset(); return; } - auto &gameStore = gamesStore[gameName]; - auto &gameInstance = gamesInstances[gameName]; + auto &gameStore = _gamesStore[gameName]; + auto &gameInstance = _gamesInstances[gameName]; auto minPlayers = gameStore->at("minPlayers").get(); auto maxPlayers = gameStore->at("maxPlayers").get(); @@ -188,7 +161,7 @@ void GameManager::addClientToGame(std::string gameName, std::shared_ptr if (gameInstance.size() >= maxInstances) { FLAKKARI_LOG_ERROR("game \"" + gameName + "\"is full"); - waitingClients[gameName].push(client); + _waitingClients[gameName].push(client); return; } gameInstance.push_back(std::make_shared(gameName, gameStore)); @@ -209,25 +182,23 @@ void GameManager::addClientToGame(std::string gameName, std::shared_ptr void GameManager::removeClientFromGame(std::string gameName, std::shared_ptr client) { - auto &gamesStore = getInstance()->_gamesStore; - auto &gamesInstances = getInstance()->_gamesInstances; - auto &waitingClients = getInstance()->_waitingClients; - if (gamesStore.find(gameName) == gamesStore.end()) + if (_gamesStore.find(gameName) == _gamesStore.end()) return FLAKKARI_LOG_ERROR("game not found"), void(); - auto &waitingQueue = waitingClients[gameName]; + auto &waitingQueue = _waitingClients[gameName]; - auto minPlayers = gamesStore[gameName]->at("minPlayers").get(); + auto minPlayers = _gamesStore[gameName]->at("minPlayers").get(); - for (auto &instance : gamesInstances[gameName]) - { + for (auto &instance : _gamesInstances[gameName]) { if (!instance->removePlayer(client)) continue; - if (instance->getPlayers().empty()) - { - gamesInstances[gameName].erase( - std::find(gamesInstances[gameName].begin(), gamesInstances[gameName].end(), instance)); + if (instance->getPlayers().empty()) { + _gamesInstances[gameName].erase( + std::find( + _gamesInstances[gameName].begin(), _gamesInstances[gameName].end(), instance + ) + ); FLAKKARI_LOG_INFO("game \"" + gameName + "\" removed"); } else if (instance->getPlayers().size() > minPlayers) @@ -252,14 +223,11 @@ void GameManager::removeClientFromGame(std::string gameName, std::shared_ptr client) { - auto &waitingClients = getInstance()->_waitingClients; - auto &gamesStore = getInstance()->_gamesStore; - if (gamesStore.find(gameName) == gamesStore.end()) + if (_gamesStore.find(gameName) == _gamesStore.end()) return FLAKKARI_LOG_ERROR("game not found"), -1; - auto tmpQueue = waitingClients[gameName]; - for (int i = 0; !tmpQueue.empty(); i++) - { + auto tmpQueue = _waitingClients[gameName]; + for (int i = 0; !tmpQueue.empty(); i++) { if (tmpQueue.front() == client) return FLAKKARI_LOG_INFO("client \"" + STR_ADDRESS + "\" found in waiting queue at " + std::to_string(i)), i; diff --git a/Flakkari/Server/Game/GameManager.hpp b/Flakkari/Server/Game/GameManager.hpp index a498efe3..e2662154 100644 --- a/Flakkari/Server/Game/GameManager.hpp +++ b/Flakkari/Server/Game/GameManager.hpp @@ -31,30 +31,20 @@ namespace Flakkari { -class GameManager { +class GameManager : public Singleton { private: - static std::shared_ptr _instance; - static std::mutex _mutex; + std::unordered_map> /*waitingClients*/> _waitingClients; + std::unordered_map> /*gamesInstances*/> _gamesInstances; + std::unordered_map /*data*/> _gamesStore; + std::string _game_dir; public: - std::unordered_map> /*waitingClients*/> - _waitingClients; - std::unordered_map> /*gamesInstances*/> _gamesInstances; - std::unordered_map /*data*/> _gamesStore; - std::string _game_dir; - - public: - GameManager(const GameManager &) = delete; - GameManager(const std::shared_ptr &) = delete; - void operator=(const GameManager &) = delete; - void operator=(const std::shared_ptr &) = delete; - - /** - * @brief Construct a new GameManager object and load all games - * already present in the Games folder - * - */ - GameManager(); + /** + * @brief Construct a new GameManager object and load all games + * already present in the Games folder + * + */ + explicit GameManager(); /** * @brief Destroy the GameManager object @@ -62,89 +52,82 @@ class GameManager { */ ~GameManager() = default; - /** - * @brief Get the instance of the GameManager - * - * @return std::shared_ptr instance of the GameManager - */ - static std::shared_ptr getInstance(); - - /** - * @brief Add a game to the GameManager and load it from the Games folder - * - * @param gameName Game to add - * @return 0 Game added - * @return 1 Game not added (already exists) - * @return 2 Game not added (certificate not valid) (not implemented) - * @return 3 Game not added (corrupted game) (not implemented) - */ - static int addGame(std::string gameName); - - /** - * @brief Get the Game object - * - * @param gameName Game to get - * @return std::shared_ptr Game - * - * @deprecated Use getGameInstance instead - */ - static std::shared_ptr getGame(std::string gameName); - - /** - * @brief Get the Games Instances object (all games loaded) - * - * @return std::vector> Games Instances - */ - static std::vector> getGamesInstances(); - - /** - * @brief Update a game from the GameManager - * - * @param gameName Game to update - * @return 0 Game updated - * @return 1 Game not updated (not found) - */ - static int updateGame(std::string gameName); - - /** - * @brief Remove a game from the GameManager - * - * @param gameName Game to remove - * @return 0 Game removed - * @return 1 Game not removed (not found) - */ - static int removeGame(std::string gameName); - - /** - * @brief List all games present in the GameManager - * - */ - static void listGames(); - - /** - * @brief Add a client to a game - * - * @param gameName Game to add the client to - * @param client Client to add to the game - */ - static void addClientToGame(std::string gameName, std::shared_ptr client); - - /** - * @brief Remove a client from a game - * - * @param gameName Game to remove the client from - * @param client Client to remove from the game - */ - static void removeClientFromGame(std::string gameName, std::shared_ptr client); - - /** - * @brief Get the index of a client in the waiting queue - * - * @param gameName Game to get the index from - * @param client Client to get the index of - * @return int Index of the client in the waiting queue - */ - static int getIndexInWaitingQueue(std::string gameName, std::shared_ptr client); + /** + * @brief Add a game to the GameManager and load it from the Games folder + * + * @param gameName Game to add + * @return 0 Game added + * @return 1 Game not added (already exists) + * @return 2 Game not added (certificate not valid) (not implemented) + * @return 3 Game not added (corrupted game) (not implemented) + */ + int addGame(std::string gameName); + + /** + * @brief Get the Game object + * + * @param gameName Game to get + * @return std::shared_ptr Game + * + * @deprecated Use getGameInstance instead + */ + std::shared_ptr getGame(std::string gameName); + + /** + * @brief Get the Games Instances object (all games loaded) + * + * @return std::vector> Games Instances + */ + std::vector> getGamesInstances(); + + /** + * @brief Update a game from the GameManager + * + * @param gameName Game to update + * @return 0 Game updated + * @return 1 Game not updated (not found) + */ + int updateGame(std::string gameName); + + /** + * @brief Remove a game from the GameManager + * + * @param gameName Game to remove + * @return 0 Game removed + * @return 1 Game not removed (not found) + */ + int removeGame(std::string gameName); + + /** + * @brief List all games present in the GameManager + * + */ + void listGames(); + + /** + * @brief Add a client to a game + * + * @param gameName Game to add the client to + * @param client Client to add to the game + */ + void addClientToGame(std::string gameName, std::shared_ptr client); + + /** + * @brief Remove a client from a game + * + * @param gameName Game to remove the client from + * @param client Client to remove from the game + */ + void removeClientFromGame(std::string gameName, std::shared_ptr client); + + /** + * @brief Get the index of a client in the waiting queue + * + * @param gameName Game to get the index from + * @param client Client to get the index of + * @return int Index of the client in the waiting queue + */ + int getIndexInWaitingQueue(std::string gameName, std::shared_ptr client); }; } /* namespace Flakkari */ diff --git a/Flakkari/Server/Internals/CommandManager.cpp b/Flakkari/Server/Internals/CommandManager.cpp index 661d6f91..90efef13 100644 --- a/Flakkari/Server/Internals/CommandManager.cpp +++ b/Flakkari/Server/Internals/CommandManager.cpp @@ -83,27 +83,27 @@ bool CommandManager::handleAdminCommand(const std::string &input) return true; } - if (std::regex_match(input, ADD_GAME_REGEX)) - { - GameManager::addGame(input.substr(8)); + if (std::regex_match(input, ADD_GAME_REGEX)) { + GameManager::GetInstance().addGame(input.substr(8)); + GameManager::UnlockInstance(); return true; } - if (std::regex_match(input, UPDATE_GAME_REGEX)) - { - GameManager::updateGame(input.substr(11)); + if (std::regex_match(input, UPDATE_GAME_REGEX)) { + GameManager::GetInstance().updateGame(input.substr(11)); + GameManager::UnlockInstance(); return true; } - if (std::regex_match(input, REMOVE_GAME_REGEX)) - { - GameManager::removeGame(input.substr(11)); + if (std::regex_match(input, REMOVE_GAME_REGEX)) { + GameManager::GetInstance().removeGame(input.substr(11)); + GameManager::UnlockInstance(); return true; } - if (input == "listGames") - { - GameManager::listGames(); + if (input == "listGames") { + GameManager::GetInstance().listGames(); + GameManager::UnlockInstance(); return true; } diff --git a/Flakkari/Server/UDPServer.cpp b/Flakkari/Server/UDPServer.cpp index 2fb7007b..7399eb18 100644 --- a/Flakkari/Server/UDPServer.cpp +++ b/Flakkari/Server/UDPServer.cpp @@ -27,12 +27,14 @@ UDPServer::UDPServer(std::string ip, unsigned short port) ClientManager::CreateInstance(_socket); ResourceManager::CreateInstance(); + GameManager::CreateInstance(); } UDPServer::~UDPServer() { ClientManager::DestroyInstance(); ResourceManager::DestroyInstance(); + GameManager::DestroyInstance(); Network::cleanup(); FLAKKARI_LOG_INFO("UDPServer is now stopped"); }