From fd0bebf53391a677921b3ae1fc436102338fd9b6 Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Fri, 18 Nov 2022 20:06:07 +0100 Subject: [PATCH] Trabajando en las estadisticas online --- source/game.cpp | 15 ++++++- source/game.h | 3 ++ source/room.cpp | 15 ++++--- source/room.h | 2 + source/stats.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++---- source/stats.h | 34 ++++++++++---- 6 files changed, 158 insertions(+), 24 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index deae981..0a3ab2b 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -41,7 +41,6 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as music = JA_LoadMusic(asset->get("game.ogg").c_str()); deathSound = JA_LoadSound(asset->get("death.wav").c_str()); stats = new Stats(asset->get("stats.csv"), options); - stats->addVisit(room->getName()); // Inicializa el resto de variables ticks = 0; @@ -57,6 +56,8 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as blackScreen = false; blackScreenCounter = 0; totalItems = getTotalItems(); + initStats(); + stats->addVisit(room->getName()); section.name = SECTION_PROG_GAME; section.subsection = 0; @@ -619,4 +620,16 @@ void Game::checkRestoringJail() board.lives++; JA_PlaySound(deathSound); } +} + +// Inicializa el diccionario de las estadísticas +void Game::initStats() +{ + std::vector *rooms = new std::vector; + rooms = resource->getAllRooms(); + + for (auto room : *rooms) + { + stats->addDictionary(room.room->number, room.room->name); + } } \ No newline at end of file diff --git a/source/game.h b/source/game.h index 62e1bc9..2ed281b 100644 --- a/source/game.h +++ b/source/game.h @@ -130,6 +130,9 @@ private: // Da vidas al jugador cuando está en la Jail void checkRestoringJail(); + // Inicializa el diccionario de las estadísticas + void initStats(); + public: // Constructor Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Input *input, Debug *debug); diff --git a/source/room.cpp b/source/room.cpp index 8e98824..2553186 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -63,7 +63,9 @@ room_t loadRoomFile(std::string file_path, bool verbose) room.itemColor2 = "magenta"; 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::ifstream file(file_path); @@ -94,7 +96,7 @@ room_t loadRoomFile(std::string file_path, bool 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]"); @@ -123,7 +125,7 @@ room_t loadRoomFile(std::string file_path, bool 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) { - 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 if (verbose) { - std::cout << "Room loaded: " << filename.c_str() << std::endl; + std::cout << "Room loaded: " << fileName.c_str() << std::endl; } file.close(); } @@ -160,7 +162,7 @@ room_t loadRoomFile(std::string file_path, bool verbose) 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->options = options; + number = room->number; name = room->name; bgColor = room->bgColor; borderColor = room->borderColor; diff --git a/source/room.h b/source/room.h index b2637dc..87e17f5 100644 --- a/source/room.h +++ b/source/room.h @@ -36,6 +36,7 @@ struct aTile_t struct room_t { + std::string number; // Numero de la habitación std::string name; // Nombre de la habitación std::string bgColor; // Color de fondo de la habitación std::string borderColor; // Color del borde de la pantalla @@ -89,6 +90,7 @@ private: options_t *options; // Puntero a las opciones del juego // Variables + std::string number; // Numero de la habitación std::string name; // Nombre de la habitación std::string bgColor; // Color de fondo de la habitación std::string borderColor; // Color del borde de la pantalla diff --git a/source/stats.cpp b/source/stats.cpp index 4b4eb31..f6dd287 100644 --- a/source/stats.cpp +++ b/source/stats.cpp @@ -1,7 +1,8 @@ -#include "stats.h" #include "common/jscore.h" -#include +#include "stats.h" +#include #include +#include #include // 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 ""; } \ No newline at end of file diff --git a/source/stats.h b/source/stats.h index 495a52b..681b622 100644 --- a/source/stats.h +++ b/source/stats.h @@ -7,22 +7,29 @@ #ifndef 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 { 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 options_t *options; // Variables - std::vector list; // Lista con las estadisticas por habitación - std::string filePath; // Fichero con las estadísticas + std::vector dictionary; // Lista con la equivalencia nombre-numero de habitacion + std::vector list; // Lista con las estadisticas por habitación + std::string filePath; // Fichero con las estadísticas // Busca una entrada en la lista por nombre int findByName(std::string name); @@ -42,6 +49,12 @@ private: // Calcula cual es la habitación con más muertes 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: // Constructor Stats(std::string file, options_t *options); @@ -54,6 +67,9 @@ public: // Añade una visita a las estadisticas void addVisit(std::string name); + + // Añade una entrada al diccionario + void addDictionary(std::string number, std::string name); }; #endif