arreglos en stats.cpp
This commit is contained in:
@@ -189,7 +189,8 @@ namespace Resource {
|
|||||||
if (!items.is_sequence()) {
|
if (!items.is_sequence()) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Warning: Type '%s' in category '%s' is not a sequence, skipping",
|
"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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,18 +209,22 @@ namespace Resource {
|
|||||||
} else {
|
} else {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Warning: Invalid item in type '%s', category '%s', skipping",
|
"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) {
|
} catch (const std::exception& e) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Error parsing asset in category '%s', type '%s': %s",
|
"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) {
|
} catch (const std::exception& e) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Error parsing type in category '%s': %s",
|
"Error parsing type in category '%s': %s",
|
||||||
category.c_str(), e.what());
|
category.c_str(),
|
||||||
|
e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (category_assets.is_sequence()) {
|
} else if (category_assets.is_sequence()) {
|
||||||
@@ -252,7 +257,8 @@ namespace Resource {
|
|||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Error parsing asset in category '%s': %s",
|
"Error parsing asset in category '%s': %s",
|
||||||
category.c_str(), e.what());
|
category.c_str(),
|
||||||
|
e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -15,17 +15,6 @@ Stats::Stats(std::string file, std::string buffer)
|
|||||||
Stats::~Stats() {
|
Stats::~Stats() {
|
||||||
// Vuelca los datos del buffer en la lista de estadisticas
|
// Vuelca los datos del buffer en la lista de estadisticas
|
||||||
updateListFromBuffer();
|
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
|
// Inicializador
|
||||||
@@ -90,12 +79,9 @@ auto Stats::findByName(const std::string& name, const std::vector<RoomData>& lis
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Carga las estadisticas desde un fichero
|
// Carga las estadisticas desde un fichero
|
||||||
auto Stats::loadFromFile(const std::string& file_path, std::vector<RoomData>& list) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
void Stats::loadFromFile(const std::string& file_path, std::vector<RoomData>& list) { // NOLINT(readability-convert-member-functions-to-static)
|
||||||
list.clear();
|
list.clear();
|
||||||
|
|
||||||
// Indicador de éxito en la carga
|
|
||||||
bool success = true;
|
|
||||||
|
|
||||||
// Variables para manejar el fichero
|
// Variables para manejar el fichero
|
||||||
std::ifstream file(file_path);
|
std::ifstream file(file_path);
|
||||||
|
|
||||||
@@ -108,8 +94,11 @@ auto Stats::loadFromFile(const std::string& file_path, std::vector<RoomData>& li
|
|||||||
if (!line.empty() && line.back() == '\r') {
|
if (!line.empty() && line.back() == '\r') {
|
||||||
line.pop_back();
|
line.pop_back();
|
||||||
}
|
}
|
||||||
// Comprueba que la linea no sea un comentario
|
// Comprueba que la linea no sea un comentario ni esté vacía
|
||||||
if (!line.starts_with("#")) {
|
if (line.empty() || line.starts_with("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
RoomData stat;
|
RoomData stat;
|
||||||
std::stringstream ss(line);
|
std::stringstream ss(line);
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
@@ -118,17 +107,20 @@ auto Stats::loadFromFile(const std::string& file_path, std::vector<RoomData>& li
|
|||||||
getline(ss, tmp, ';');
|
getline(ss, tmp, ';');
|
||||||
stat.name = tmp;
|
stat.name = tmp;
|
||||||
|
|
||||||
// Obtiene las visitas
|
// Obtiene las visitas y muertes
|
||||||
|
try {
|
||||||
getline(ss, tmp, ';');
|
getline(ss, tmp, ';');
|
||||||
stat.visited = std::stoi(tmp);
|
stat.visited = std::stoi(tmp);
|
||||||
|
|
||||||
// Obtiene las muertes
|
|
||||||
getline(ss, tmp, ';');
|
getline(ss, tmp, ';');
|
||||||
stat.died = std::stoi(tmp);
|
stat.died = std::stoi(tmp);
|
||||||
|
} catch (const std::exception&) {
|
||||||
|
// Línea con formato incorrecto, la ignora
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
list.push_back(stat);
|
list.push_back(stat);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Cierra el fichero
|
// Cierra el fichero
|
||||||
file.close();
|
file.close();
|
||||||
@@ -139,8 +131,6 @@ auto Stats::loadFromFile(const std::string& file_path, std::vector<RoomData>& li
|
|||||||
// Crea el fichero con los valores por defecto
|
// Crea el fichero con los valores por defecto
|
||||||
saveToFile(file_path, list);
|
saveToFile(file_path, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guarda las estadisticas en un fichero
|
// 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
|
// Vuelca los datos del buffer en la lista de estadisticas
|
||||||
void Stats::updateListFromBuffer() {
|
void Stats::updateListFromBuffer() {
|
||||||
// Actualiza list_ desde buffer_list_
|
// 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(buffer_path_, buffer_list_);
|
||||||
saveToFile(file_path_, list_);
|
saveToFile(file_path_, list_);
|
||||||
}
|
}
|
||||||
@@ -11,13 +11,7 @@ class Stats {
|
|||||||
int died; // Cuenta las veces que se ha muerto en una habitación
|
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
|
// Variables
|
||||||
std::vector<Dictionary> dictionary_; // Lista con la equivalencia nombre-numero de habitacion
|
|
||||||
std::vector<RoomData> buffer_list_; // Lista con las estadisticas temporales por habitación
|
std::vector<RoomData> buffer_list_; // Lista con las estadisticas temporales por habitación
|
||||||
std::vector<RoomData> list_; // Lista con las estadisticas completas por habitación
|
std::vector<RoomData> list_; // Lista con las estadisticas completas por habitación
|
||||||
std::string buffer_path_; // Fichero con las estadísticas temporales
|
std::string buffer_path_; // Fichero con las estadísticas temporales
|
||||||
@@ -27,7 +21,7 @@ class Stats {
|
|||||||
static auto findByName(const std::string& name, const std::vector<RoomData>& list) -> int;
|
static auto findByName(const std::string& name, const std::vector<RoomData>& list) -> int;
|
||||||
|
|
||||||
// Carga las estadisticas desde un fichero
|
// Carga las estadisticas desde un fichero
|
||||||
static auto loadFromFile(const std::string& file_path, std::vector<RoomData>& list) -> bool;
|
static void loadFromFile(const std::string& file_path, std::vector<RoomData>& list);
|
||||||
|
|
||||||
// Guarda las estadisticas en un fichero
|
// Guarda las estadisticas en un fichero
|
||||||
static void saveToFile(const std::string& file_path, const std::vector<RoomData>& list);
|
static void saveToFile(const std::string& file_path, const std::vector<RoomData>& list);
|
||||||
@@ -39,14 +33,13 @@ class Stats {
|
|||||||
void updateListFromBuffer();
|
void updateListFromBuffer();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructostd::string nst stdstd::string nst std::string& buffer);
|
// Constructor
|
||||||
Stats(std::string file, std::string buffer);
|
Stats(std::string file, std::string buffer);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Stats();
|
~Stats();
|
||||||
|
|
||||||
// Inicializador
|
// Inicializador
|
||||||
// Se debe llamar a este procedimiento una vez se haya creado el diccionario numero-nombre
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
// Añade una muerte a las estadisticas
|
// Añade una muerte a las estadisticas
|
||||||
@@ -54,7 +47,4 @@ class Stats {
|
|||||||
|
|
||||||
// Añade una visita a las estadisticas
|
// Añade una visita a las estadisticas
|
||||||
void addVisit(const std::string& name);
|
void addVisit(const std::string& name);
|
||||||
|
|
||||||
// Añade una entrada al diccionario
|
|
||||||
void addDictionary(const std::string& number, const std::string& name);
|
|
||||||
};
|
};
|
||||||
@@ -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)
|
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();
|
stats_->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user