198 lines
4.1 KiB
C++
198 lines
4.1 KiB
C++
#include "stats.h"
|
|
#include "common/jscore.h"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
// Constructor
|
|
Stats::Stats(std::string file, options_t *options)
|
|
{
|
|
this->options = options;
|
|
filePath = file;
|
|
list.clear();
|
|
loadFromFile();
|
|
}
|
|
|
|
// Destructor
|
|
Stats::~Stats()
|
|
{
|
|
checkWorstNightmare();
|
|
#ifndef DEBUG
|
|
saveToFile();
|
|
#endif
|
|
saveToServer();
|
|
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;
|
|
}
|
|
|
|
// Carga las estadisticas desde un servidor
|
|
void Stats::loadFromServer()
|
|
{
|
|
if (options->online.enabled)
|
|
{
|
|
jscore::getUserData(options->online.gameID, options->online.jailerID);
|
|
}
|
|
}
|
|
|
|
// Guarda las estadisticas en un fichero
|
|
void Stats::saveToFile()
|
|
{
|
|
// Crea y abre el fichero de texto
|
|
std::ofstream file(filePath);
|
|
|
|
if (file.good())
|
|
{
|
|
std::cout << filePath << " open for writing" << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << filePath << " can't be opened" << std::endl;
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
|
|
// Guarda las estadisticas en un servidor
|
|
void Stats::saveToServer()
|
|
{
|
|
if (options->online.enabled)
|
|
{
|
|
for (auto item : list)
|
|
{
|
|
const std::string data = item.name + ";" + std::to_string(item.visited) + ";" + std::to_string(item.died);
|
|
jscore::setUserData(options->online.gameID, options->online.jailerID, data);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Calcula cual es la habitación con más muertes
|
|
void Stats::checkWorstNightmare()
|
|
{
|
|
int deaths = 0;
|
|
for (auto item : list)
|
|
{
|
|
if (item.died > deaths)
|
|
{
|
|
deaths = item.died;
|
|
options->stats.worstNightmare = item.name;
|
|
}
|
|
}
|
|
} |