Añadido soporte preliminar para estadisticas
This commit is contained in:
149
source/stats.cpp
Normal file
149
source/stats.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
#include "stats.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
Stats::Stats(std::string file)
|
||||
{
|
||||
filePath = file;
|
||||
list.clear();
|
||||
loadFromFile();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Stats::~Stats()
|
||||
{
|
||||
saveToFile();
|
||||
list.clear();
|
||||
}
|
||||
|
||||
// Añade una muerte a las estadisticas
|
||||
void Stats::addDeath(std::string name)
|
||||
{
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name);
|
||||
if (index != -1)
|
||||
{
|
||||
list.at(index).died++;
|
||||
}
|
||||
|
||||
// En caso contrario crea la entrada
|
||||
else
|
||||
{
|
||||
stats_t item;
|
||||
item.name = name;
|
||||
item.visited = 0;
|
||||
item.died = 1;
|
||||
list.push_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
// Añade una visita a las estadisticas
|
||||
void Stats::addVisit(std::string name)
|
||||
{
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name);
|
||||
if (index != -1)
|
||||
{
|
||||
list.at(index).visited++;
|
||||
}
|
||||
|
||||
// En caso contrario crea la entrada
|
||||
else
|
||||
{
|
||||
stats_t item;
|
||||
item.name = name;
|
||||
item.visited = 1;
|
||||
item.died = 0;
|
||||
list.push_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
// Busca una entrada en la lista por nombre
|
||||
int Stats::findByName(std::string name)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (auto l : list)
|
||||
{
|
||||
if (l.name == name)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Carga las estadisticas desde un fichero
|
||||
bool Stats::loadFromFile()
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Variables para manejar el fichero
|
||||
std::string line;
|
||||
std::ifstream file(filePath);
|
||||
|
||||
// Si el fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
// Comprueba que la linea no sea un comentario
|
||||
if (line.substr(0, 1) != "#")
|
||||
{
|
||||
stats_t stat;
|
||||
std::stringstream ss(line);
|
||||
std::string tmp;
|
||||
|
||||
// Obtiene el nombre
|
||||
getline(ss, tmp, ';');
|
||||
stat.name = tmp;
|
||||
|
||||
// Obtiene las visitas
|
||||
getline(ss, tmp, ';');
|
||||
stat.visited = std::stoi(tmp);
|
||||
|
||||
// Obtiene las muertes
|
||||
getline(ss, tmp, ';');
|
||||
stat.died = std::stoi(tmp);
|
||||
|
||||
list.push_back(stat);
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
file.close();
|
||||
}
|
||||
|
||||
// El fichero no existe
|
||||
else
|
||||
{ // Crea el fichero con los valores por defecto
|
||||
saveToFile();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Guarda las estadisticas en un fichero
|
||||
void Stats::saveToFile()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
// Crea y abre el fichero de texto
|
||||
std::ofstream file(filePath);
|
||||
|
||||
// Escribe en el fichero
|
||||
file << "# NOMBRE DE LA HABITACION;VISITAS;MUERTES" << std::endl;
|
||||
for (auto item : list)
|
||||
{
|
||||
file << item.name << ";" << item.visited << ";" << item.died << std::endl;
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
file.close();
|
||||
}
|
||||
Reference in New Issue
Block a user