arreglos en stats.cpp
This commit is contained in:
@@ -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<RoomData>& lis
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
// 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<RoomData>& 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<RoomData>& 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_);
|
||||
}
|
||||
Reference in New Issue
Block a user