diff --git a/Flakkari/Server/Game/GameManager.cpp b/Flakkari/Server/Game/GameManager.cpp index ff92c7b5..d9885648 100644 --- a/Flakkari/Server/Game/GameManager.cpp +++ b/Flakkari/Server/Game/GameManager.cpp @@ -138,7 +138,8 @@ void GameManager::listGames() void GameManager::addClientToGame(std::string gameName, std::shared_ptr client) { - if (_gamesStore.find(gameName) == _gamesStore.end()) { + if (_gamesStore.find(gameName) == _gamesStore.end()) + { FLAKKARI_LOG_ERROR("game not found"); client.reset(); return; @@ -189,16 +190,15 @@ void GameManager::removeClientFromGame(std::string gameName, std::shared_ptrat("minPlayers").get(); - for (auto &instance : _gamesInstances[gameName]) { + for (auto &instance : _gamesInstances[gameName]) + { if (!instance->removePlayer(client)) continue; - if (instance->getPlayers().empty()) { + if (instance->getPlayers().empty()) + { _gamesInstances[gameName].erase( - std::find( - _gamesInstances[gameName].begin(), _gamesInstances[gameName].end(), instance - ) - ); + std::find(_gamesInstances[gameName].begin(), _gamesInstances[gameName].end(), instance)); FLAKKARI_LOG_INFO("game \"" + gameName + "\" removed"); } else if (instance->getPlayers().size() > minPlayers) @@ -227,7 +227,8 @@ int GameManager::getIndexInWaitingQueue(std::string gameName, std::shared_ptr { private: - std::unordered_map> /*waitingClients*/> _waitingClients; - std::unordered_map> /*gamesInstances*/> _gamesInstances; - std::unordered_map /*data*/> _gamesStore; - std::string _game_dir; + std::unordered_map> /*waitingClients*/> + _waitingClients; + std::unordered_map> /*gamesInstances*/> _gamesInstances; + std::unordered_map /*data*/> _gamesStore; + std::string _game_dir; public: - /** - * @brief Construct a new GameManager object and load all games - * already present in the Games folder - * - */ - explicit GameManager(); + /** + * @brief Construct a new GameManager object and load all games + * already present in the Games folder + * + */ + explicit GameManager(); /** * @brief Destroy the GameManager object @@ -52,82 +53,82 @@ class GameManager : public Singleton { */ ~GameManager() = default; - /** - * @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); + /** + * @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 90efef13..089be4fb 100644 --- a/Flakkari/Server/Internals/CommandManager.cpp +++ b/Flakkari/Server/Internals/CommandManager.cpp @@ -83,25 +83,29 @@ bool CommandManager::handleAdminCommand(const std::string &input) return true; } - if (std::regex_match(input, ADD_GAME_REGEX)) { + 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)) { + 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)) { + if (std::regex_match(input, REMOVE_GAME_REGEX)) + { GameManager::GetInstance().removeGame(input.substr(11)); GameManager::UnlockInstance(); return true; } - if (input == "listGames") { + if (input == "listGames") + { GameManager::GetInstance().listGames(); GameManager::UnlockInstance(); return true;