1#include "games_storage.h"
2#include <boost/filesystem.hpp>
3#include <boost/range/iterator_range.hpp>
8namespace cavoke::server::model {
10GamesStorage::GamesStorage(GamesStorageConfig config)
11 : m_config(std::move(config)) {
12 if (!boost::filesystem::exists(m_config.games_directory)) {
14 boost::filesystem::create_directories(m_config.games_directory);
16 throw std::invalid_argument(
"Cannot create games directory " +
17 m_config.games_directory.string() +
22 if (!boost::filesystem::is_directory(m_config.games_directory)) {
23 throw std::invalid_argument(
"Invalid games directory " +
24 m_config.games_directory.string() +
"\n");
30std::vector<Game> GamesStorage::list_games() {
33 std::vector<Game> result;
36 std::shared_lock lock(m_games_mtx);
37 for (
const auto &e : m_games) {
39 result.emplace_back(e.second);
46void GamesStorage::update() {
47 std::unique_lock lock(m_games_mtx);
50 for (
auto &entry : boost::make_iterator_range(
51 boost::filesystem::directory_iterator(m_config.games_directory),
53 if (!Game::is_game_directory(entry, m_config)) {
56 <<
" entity from games directory is not a valid game"
59 Game game(entry, m_config);
60 m_games.emplace(game.config.id, game);
65std::optional<Game> GamesStorage::get_game_by_id(
const std::string &game_id) {
66 std::shared_lock lock(m_games_mtx);
68 if (m_games.count(game_id) > 0) {
69 return m_games[game_id];