Trabajando en las estadisticas online

This commit is contained in:
2022-11-18 20:06:07 +01:00
parent 2665b93b70
commit fd0bebf533
6 changed files with 158 additions and 24 deletions

View File

@@ -41,7 +41,6 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
music = JA_LoadMusic(asset->get("game.ogg").c_str()); music = JA_LoadMusic(asset->get("game.ogg").c_str());
deathSound = JA_LoadSound(asset->get("death.wav").c_str()); deathSound = JA_LoadSound(asset->get("death.wav").c_str());
stats = new Stats(asset->get("stats.csv"), options); stats = new Stats(asset->get("stats.csv"), options);
stats->addVisit(room->getName());
// Inicializa el resto de variables // Inicializa el resto de variables
ticks = 0; ticks = 0;
@@ -57,6 +56,8 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
blackScreen = false; blackScreen = false;
blackScreenCounter = 0; blackScreenCounter = 0;
totalItems = getTotalItems(); totalItems = getTotalItems();
initStats();
stats->addVisit(room->getName());
section.name = SECTION_PROG_GAME; section.name = SECTION_PROG_GAME;
section.subsection = 0; section.subsection = 0;
@@ -620,3 +621,15 @@ void Game::checkRestoringJail()
JA_PlaySound(deathSound); JA_PlaySound(deathSound);
} }
} }
// Inicializa el diccionario de las estadísticas
void Game::initStats()
{
std::vector<res_room_t> *rooms = new std::vector<res_room_t>;
rooms = resource->getAllRooms();
for (auto room : *rooms)
{
stats->addDictionary(room.room->number, room.room->name);
}
}

View File

@@ -130,6 +130,9 @@ private:
// Da vidas al jugador cuando está en la Jail // Da vidas al jugador cuando está en la Jail
void checkRestoringJail(); void checkRestoringJail();
// Inicializa el diccionario de las estadísticas
void initStats();
public: public:
// Constructor // Constructor
Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Input *input, Debug *debug); Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Input *input, Debug *debug);

View File

@@ -63,7 +63,9 @@ room_t loadRoomFile(std::string file_path, bool verbose)
room.itemColor2 = "magenta"; room.itemColor2 = "magenta";
room.autoSurfaceDirection = 1; room.autoSurfaceDirection = 1;
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1); const std::string fileName = file_path.substr(file_path.find_last_of("\\/") + 1);
room.number = fileName.substr(0, fileName.find_last_of("."));
std::string line; std::string line;
std::ifstream file(file_path); std::ifstream file(file_path);
@@ -94,7 +96,7 @@ room_t loadRoomFile(std::string file_path, bool verbose)
{ {
if (verbose) if (verbose)
{ {
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: file " << fileName.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
} }
} }
} while (line != "[/enemy]"); } while (line != "[/enemy]");
@@ -123,7 +125,7 @@ room_t loadRoomFile(std::string file_path, bool verbose)
{ {
if (verbose) if (verbose)
{ {
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: file " << fileName.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
} }
} }
@@ -143,7 +145,7 @@ room_t loadRoomFile(std::string file_path, bool verbose)
{ {
if (verbose) if (verbose)
{ {
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: file " << fileName.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
} }
} }
} }
@@ -152,7 +154,7 @@ room_t loadRoomFile(std::string file_path, bool verbose)
// Cierra el fichero // Cierra el fichero
if (verbose) if (verbose)
{ {
std::cout << "Room loaded: " << filename.c_str() << std::endl; std::cout << "Room loaded: " << fileName.c_str() << std::endl;
} }
file.close(); file.close();
} }
@@ -160,7 +162,7 @@ room_t loadRoomFile(std::string file_path, bool verbose)
else else
{ {
{ {
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; std::cout << "Warning: Unable to open " << fileName.c_str() << " file" << std::endl;
} }
} }
@@ -400,6 +402,7 @@ Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, o
this->debug = debug; this->debug = debug;
this->options = options; this->options = options;
number = room->number;
name = room->name; name = room->name;
bgColor = room->bgColor; bgColor = room->bgColor;
borderColor = room->borderColor; borderColor = room->borderColor;

View File

@@ -36,6 +36,7 @@ struct aTile_t
struct room_t struct room_t
{ {
std::string number; // Numero de la habitación
std::string name; // Nombre de la habitación std::string name; // Nombre de la habitación
std::string bgColor; // Color de fondo de la habitación std::string bgColor; // Color de fondo de la habitación
std::string borderColor; // Color del borde de la pantalla std::string borderColor; // Color del borde de la pantalla
@@ -89,6 +90,7 @@ private:
options_t *options; // Puntero a las opciones del juego options_t *options; // Puntero a las opciones del juego
// Variables // Variables
std::string number; // Numero de la habitación
std::string name; // Nombre de la habitación std::string name; // Nombre de la habitación
std::string bgColor; // Color de fondo de la habitación std::string bgColor; // Color de fondo de la habitación
std::string borderColor; // Color del borde de la pantalla std::string borderColor; // Color del borde de la pantalla

View File

@@ -1,7 +1,8 @@
#include "stats.h"
#include "common/jscore.h" #include "common/jscore.h"
#include <iostream> #include "stats.h"
#include <algorithm>
#include <fstream> #include <fstream>
#include <iostream>
#include <sstream> #include <sstream>
// Constructor // Constructor
@@ -10,7 +11,15 @@ Stats::Stats(std::string file, options_t *options)
this->options = options; this->options = options;
filePath = file; filePath = file;
list.clear(); list.clear();
loadFromFile(); dictionary.clear();
if (options->online.enabled)
{
loadFromServer();
}
else
{
loadFromFile();
}
} }
// Destructor // Destructor
@@ -18,15 +27,27 @@ Stats::~Stats()
{ {
checkWorstNightmare(); checkWorstNightmare();
#ifndef DEBUG #ifndef DEBUG
saveToFile(); if (options->online.enabled)
{
saveToServer();
}
else
{
saveToFile();
}
#endif #endif
saveToServer(); saveToServer();
list.clear(); list.clear();
dictionary.clear();
} }
// Añade una muerte a las estadisticas // Añade una muerte a las estadisticas
void Stats::addDeath(std::string name) 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 // Primero busca si ya hay una entrada con ese nombre
const int index = findByName(name); const int index = findByName(name);
if (index != -1) if (index != -1)
@@ -48,6 +69,9 @@ void Stats::addDeath(std::string name)
// Añade una visita a las estadisticas // Añade una visita a las estadisticas
void Stats::addVisit(std::string name) 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 // Primero busca si ya hay una entrada con ese nombre
const int index = findByName(name); const int index = findByName(name);
if (index != -1) if (index != -1)
@@ -64,6 +88,7 @@ void Stats::addVisit(std::string name)
item.died = 0; item.died = 0;
list.push_back(item); 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 // Busca una entrada en la lista por nombre
@@ -138,9 +163,46 @@ bool Stats::loadFromFile()
// Carga las estadisticas desde un servidor // Carga las estadisticas desde un servidor
void Stats::loadFromServer() void Stats::loadFromServer()
{ {
// jscore::setUserData(options->online.gameID, options->online.jailerID, "");
std::string data;
if (options->online.enabled) 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 // Guarda las estadisticas en un servidor
void Stats::saveToServer() void Stats::saveToServer()
{ {
std::string data = "";
if (options->online.enabled) if (options->online.enabled)
{ {
for (auto item : list) for (auto item : list)
{ {
const std::string data = item.name + ";" + std::to_string(item.visited) + ";" + std::to_string(item.died); // data = data + nameToNumber(item.name) + ";" + std::to_string(item.visited) + ";" + std::to_string(item.died) + ";";
jscore::setUserData(options->online.gameID, options->online.jailerID, data); 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);
} }
} }
@@ -196,3 +261,35 @@ void Stats::checkWorstNightmare()
} }
} }
} }
// 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 "";
}

View File

@@ -7,22 +7,29 @@
#ifndef STATS_H #ifndef STATS_H
#define STATS_H #define STATS_H
struct stats_t
{
std::string name; // Nombre de la habitación donde se encuentra el objeto
int visited; // Cuenta las veces que se ha visitado una habitación
int died; // Cuenta las veces que se ha muerto en una habitación
};
class Stats class Stats
{ {
private: private:
struct stats_t
{
std::string name; // Nombre de la habitación
int visited; // Cuenta las veces que se ha visitado una habitación
int died; // Cuenta las veces que se ha muerto en una habitación
};
struct stats_dictionary_t
{
std::string number; // Numero de la habitación
std::string name; // Nombre de la habitación
};
// Punteros y objetos // Punteros y objetos
options_t *options; options_t *options;
// Variables // Variables
std::vector<stats_t> list; // Lista con las estadisticas por habitación std::vector<stats_dictionary_t> dictionary; // Lista con la equivalencia nombre-numero de habitacion
std::string filePath; // Fichero con las estadísticas std::vector<stats_t> list; // Lista con las estadisticas por habitación
std::string filePath; // Fichero con las estadísticas
// Busca una entrada en la lista por nombre // Busca una entrada en la lista por nombre
int findByName(std::string name); int findByName(std::string name);
@@ -42,6 +49,12 @@ private:
// Calcula cual es la habitación con más muertes // Calcula cual es la habitación con más muertes
void checkWorstNightmare(); void checkWorstNightmare();
// Obtiene el nombre de una habitación a partir del número
std::string numberToName(std::string number);
// Obtiene el número de una habitación a partir del nombre
std::string nameToNumber(std::string name);
public: public:
// Constructor // Constructor
Stats(std::string file, options_t *options); Stats(std::string file, options_t *options);
@@ -54,6 +67,9 @@ public:
// Añade una visita a las estadisticas // Añade una visita a las estadisticas
void addVisit(std::string name); void addVisit(std::string name);
// Añade una entrada al diccionario
void addDictionary(std::string number, std::string name);
}; };
#endif #endif