diff --git a/source/core/resources/resource_list.cpp b/source/core/resources/resource_list.cpp index 56e6de4..5abc54b 100644 --- a/source/core/resources/resource_list.cpp +++ b/source/core/resources/resource_list.cpp @@ -189,7 +189,8 @@ namespace Resource { if (!items.is_sequence()) { SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: Type '%s' in category '%s' is not a sequence, skipping", - type_str.c_str(), category.c_str()); + type_str.c_str(), + category.c_str()); continue; } @@ -208,18 +209,22 @@ namespace Resource { } else { SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: Invalid item in type '%s', category '%s', skipping", - type_str.c_str(), category.c_str()); + type_str.c_str(), + category.c_str()); } } catch (const std::exception& e) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error parsing asset in category '%s', type '%s': %s", - category.c_str(), type_str.c_str(), e.what()); + category.c_str(), + type_str.c_str(), + e.what()); } } } catch (const std::exception& e) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error parsing type in category '%s': %s", - category.c_str(), e.what()); + category.c_str(), + e.what()); } } } else if (category_assets.is_sequence()) { @@ -252,7 +257,8 @@ namespace Resource { } catch (const std::exception& e) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error parsing asset in category '%s': %s", - category.c_str(), e.what()); + category.c_str(), + e.what()); } } } else { diff --git a/source/core/resources/resource_list.hpp b/source/core/resources/resource_list.hpp index b293b28..10ba6ba 100644 --- a/source/core/resources/resource_list.hpp +++ b/source/core/resources/resource_list.hpp @@ -33,8 +33,8 @@ namespace Resource { // --- Métodos para la gestión de recursos --- void add(const std::string& file_path, Type type, bool required = true, bool absolute = false); - void addAsset(const std::string& path, Type type); // Añade al mapa y persiste en assets.yaml - void removeAsset(const std::string& filename); // Quita del mapa y persiste en assets.yaml + void addAsset(const std::string& path, Type type); // Añade al mapa y persiste en assets.yaml + void removeAsset(const std::string& filename); // Quita del mapa y persiste en assets.yaml void loadFromFile(const std::string& config_file_path, const std::string& prefix = "", const std::string& system_folder = ""); // Con soporte para variables void loadFromString(const std::string& config_content, const std::string& prefix = "", const std::string& system_folder = ""); // Para cargar desde pack (release) [[nodiscard]] auto get(const std::string& filename) const -> std::string; // Obtiene la ruta completa diff --git a/source/game/gameplay/stats.cpp b/source/game/gameplay/stats.cpp index 9b0674b..755664f 100644 --- a/source/game/gameplay/stats.cpp +++ b/source/game/gameplay/stats.cpp @@ -15,17 +15,6 @@ Stats::Stats(std::string file, std::string buffer) Stats::~Stats() { // Vuelca los datos del buffer en la lista de estadisticas updateListFromBuffer(); - - // Calcula cual es la habitación con más muertes - checkWorstNightmare(); - - // Guarda las estadísticas - saveToFile(buffer_path_, buffer_list_); - saveToFile(file_path_, list_); - - buffer_list_.clear(); - list_.clear(); - dictionary_.clear(); } // Inicializador @@ -90,12 +79,9 @@ auto Stats::findByName(const std::string& name, const std::vector& lis } // Carga las estadisticas desde un fichero -auto Stats::loadFromFile(const std::string& file_path, std::vector& list) -> bool { // NOLINT(readability-convert-member-functions-to-static) +void Stats::loadFromFile(const std::string& file_path, std::vector& list) { // NOLINT(readability-convert-member-functions-to-static) list.clear(); - // Indicador de éxito en la carga - bool success = true; - // Variables para manejar el fichero std::ifstream file(file_path); @@ -108,26 +94,32 @@ auto Stats::loadFromFile(const std::string& file_path, std::vector& li if (!line.empty() && line.back() == '\r') { line.pop_back(); } - // Comprueba que la linea no sea un comentario - if (!line.starts_with("#")) { - RoomData stat; - std::stringstream ss(line); - std::string tmp; + // Comprueba que la linea no sea un comentario ni esté vacía + if (line.empty() || line.starts_with("#")) { + continue; + } - // Obtiene el nombre - getline(ss, tmp, ';'); - stat.name = tmp; + RoomData stat; + std::stringstream ss(line); + std::string tmp; - // Obtiene las visitas + // Obtiene el nombre + getline(ss, tmp, ';'); + stat.name = tmp; + + // Obtiene las visitas y muertes + try { getline(ss, tmp, ';'); stat.visited = std::stoi(tmp); - // Obtiene las muertes getline(ss, tmp, ';'); stat.died = std::stoi(tmp); - - list.push_back(stat); + } catch (const std::exception&) { + // Línea con formato incorrecto, la ignora + continue; } + + list.push_back(stat); } // Cierra el fichero @@ -139,8 +131,6 @@ auto Stats::loadFromFile(const std::string& file_path, std::vector& li // Crea el fichero con los valores por defecto saveToFile(file_path, list); } - - return success; } // Guarda las estadisticas en un fichero @@ -169,11 +159,6 @@ void Stats::checkWorstNightmare() { // NOLINT(readability-convert-member-functi } } -// Añade una entrada al diccionario -void Stats::addDictionary(const std::string& number, const std::string& name) { - dictionary_.push_back({.number = number, .name = name}); -} - // Vuelca los datos del buffer en la lista de estadisticas void Stats::updateListFromBuffer() { // Actualiza list_ desde buffer_list_ @@ -192,6 +177,13 @@ void Stats::updateListFromBuffer() { } } + // Limpia el buffer después de volcarlo + buffer_list_.clear(); + + // Calcula cual es la habitación con más muertes + checkWorstNightmare(); + + // Guarda las estadísticas saveToFile(buffer_path_, buffer_list_); saveToFile(file_path_, list_); } \ No newline at end of file diff --git a/source/game/gameplay/stats.hpp b/source/game/gameplay/stats.hpp index beb3817..ce05833 100644 --- a/source/game/gameplay/stats.hpp +++ b/source/game/gameplay/stats.hpp @@ -11,23 +11,17 @@ class Stats { int died; // Cuenta las veces que se ha muerto en una habitación }; - struct Dictionary { - std::string number; // Numero de la habitación - std::string name; // Nombre de la habitación - }; - // Variables - std::vector dictionary_; // Lista con la equivalencia nombre-numero de habitacion - std::vector buffer_list_; // Lista con las estadisticas temporales por habitación - std::vector list_; // Lista con las estadisticas completas por habitación - std::string buffer_path_; // Fichero con las estadísticas temporales - std::string file_path_; // Fichero con las estadísticas completas + std::vector buffer_list_; // Lista con las estadisticas temporales por habitación + std::vector list_; // Lista con las estadisticas completas por habitación + std::string buffer_path_; // Fichero con las estadísticas temporales + std::string file_path_; // Fichero con las estadísticas completas // Busca una entrada en la lista por nombre static auto findByName(const std::string& name, const std::vector& list) -> int; // Carga las estadisticas desde un fichero - static auto loadFromFile(const std::string& file_path, std::vector& list) -> bool; + static void loadFromFile(const std::string& file_path, std::vector& list); // Guarda las estadisticas en un fichero static void saveToFile(const std::string& file_path, const std::vector& list); @@ -39,14 +33,13 @@ class Stats { void updateListFromBuffer(); public: - // Constructostd::string nst stdstd::string nst std::string& buffer); + // Constructor Stats(std::string file, std::string buffer); // Destructor ~Stats(); // Inicializador - // Se debe llamar a este procedimiento una vez se haya creado el diccionario numero-nombre void init(); // Añade una muerte a las estadisticas @@ -54,7 +47,4 @@ class Stats { // Añade una visita a las estadisticas void addVisit(const std::string& name); - - // Añade una entrada al diccionario - void addDictionary(const std::string& number, const std::string& name); }; \ No newline at end of file diff --git a/source/game/scenes/game.cpp b/source/game/scenes/game.cpp index a78f0e4..03fee35 100644 --- a/source/game/scenes/game.cpp +++ b/source/game/scenes/game.cpp @@ -914,14 +914,8 @@ void Game::checkRestoringJail(float delta_time) { } } -// Inicializa el diccionario de las estadísticas +// Inicializa las estadísticas void Game::initStats() { // NOLINT(readability-convert-member-functions-to-static) - auto list = Resource::Cache::get()->getRooms(); - - for (const auto& room : list) { - stats_->addDictionary(room.room->number, room.room->name); - } - stats_->init(); }