forked from jaildesigner-jailgames/jaildoctors_dilemma
Trabajando en las estadisticas online
This commit is contained in:
113
source/stats.cpp
113
source/stats.cpp
@@ -1,7 +1,8 @@
|
||||
#include "stats.h"
|
||||
#include "common/jscore.h"
|
||||
#include <iostream>
|
||||
#include "stats.h"
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
@@ -10,7 +11,15 @@ Stats::Stats(std::string file, options_t *options)
|
||||
this->options = options;
|
||||
filePath = file;
|
||||
list.clear();
|
||||
loadFromFile();
|
||||
dictionary.clear();
|
||||
if (options->online.enabled)
|
||||
{
|
||||
loadFromServer();
|
||||
}
|
||||
else
|
||||
{
|
||||
loadFromFile();
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -18,15 +27,27 @@ Stats::~Stats()
|
||||
{
|
||||
checkWorstNightmare();
|
||||
#ifndef DEBUG
|
||||
saveToFile();
|
||||
if (options->online.enabled)
|
||||
{
|
||||
saveToServer();
|
||||
}
|
||||
else
|
||||
{
|
||||
saveToFile();
|
||||
}
|
||||
#endif
|
||||
saveToServer();
|
||||
saveToServer();
|
||||
|
||||
list.clear();
|
||||
dictionary.clear();
|
||||
}
|
||||
|
||||
// Añade una muerte a las estadisticas
|
||||
void Stats::addDeath(std::string name)
|
||||
{
|
||||
// Normaliza el nombre
|
||||
std::replace(name.begin(), name.end(), ' ', '_');
|
||||
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name);
|
||||
if (index != -1)
|
||||
@@ -48,6 +69,9 @@ void Stats::addDeath(std::string name)
|
||||
// Añade una visita a las estadisticas
|
||||
void Stats::addVisit(std::string name)
|
||||
{
|
||||
// Normaliza el nombre
|
||||
std::replace(name.begin(), name.end(), ' ', '_');
|
||||
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name);
|
||||
if (index != -1)
|
||||
@@ -64,6 +88,7 @@ void Stats::addVisit(std::string name)
|
||||
item.died = 0;
|
||||
list.push_back(item);
|
||||
}
|
||||
std::cout << "VISITADO: " << list.back().name << ";" << list.back().visited << ";" << list.back().died << std::endl;
|
||||
}
|
||||
|
||||
// Busca una entrada en la lista por nombre
|
||||
@@ -138,9 +163,46 @@ bool Stats::loadFromFile()
|
||||
// Carga las estadisticas desde un servidor
|
||||
void Stats::loadFromServer()
|
||||
{
|
||||
// jscore::setUserData(options->online.gameID, options->online.jailerID, "");
|
||||
|
||||
std::string data;
|
||||
if (options->online.enabled)
|
||||
{
|
||||
jscore::getUserData(options->online.gameID, options->online.jailerID);
|
||||
data = jscore::getUserData(options->online.gameID, options->online.jailerID);
|
||||
}
|
||||
|
||||
std::stringstream ss(data);
|
||||
std::string tmp;
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < (int)data.size(); ++i)
|
||||
{
|
||||
if (data[i] == ';')
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
stats_t stat;
|
||||
|
||||
// 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);
|
||||
count = count - 3;
|
||||
std::cout << stat.name << ";" << stat.visited << ";" << stat.died << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,13 +235,16 @@ void Stats::saveToFile()
|
||||
// Guarda las estadisticas en un servidor
|
||||
void Stats::saveToServer()
|
||||
{
|
||||
std::string data = "";
|
||||
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);
|
||||
// data = data + nameToNumber(item.name) + ";" + std::to_string(item.visited) + ";" + std::to_string(item.died) + ";";
|
||||
data = data + item.name + ";" + std::to_string(item.visited) + ";" + std::to_string(item.died) + ";";
|
||||
std::cout << "GUARDADO: " << item.name << ";" << item.visited << ";" << item.died << std::endl;
|
||||
}
|
||||
jscore::setUserData(options->online.gameID, options->online.jailerID, data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,4 +260,36 @@ void Stats::checkWorstNightmare()
|
||||
options->stats.worstNightmare = item.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Añade una entrada al diccionario
|
||||
void Stats::addDictionary(std::string number, std::string name)
|
||||
{
|
||||
dictionary.push_back({number, name});
|
||||
}
|
||||
|
||||
// Obtiene el nombre de una habitación a partir del número
|
||||
std::string Stats::numberToName(std::string number)
|
||||
{
|
||||
for (auto l : dictionary)
|
||||
{
|
||||
if (l.number == number)
|
||||
{
|
||||
return l.name;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Obtiene el número de una habitación a partir del nombre
|
||||
std::string Stats::nameToNumber(std::string name)
|
||||
{
|
||||
for (auto l : dictionary)
|
||||
{
|
||||
if (l.name == name)
|
||||
{
|
||||
return l.number;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
Reference in New Issue
Block a user