diff --git a/source/cheevos.cpp b/source/cheevos.cpp index 53ac20c..cad9743 100644 --- a/source/cheevos.cpp +++ b/source/cheevos.cpp @@ -2,15 +2,13 @@ #include // Constructor -Cheevos::Cheevos(Screen *screen, options_t *options, std::string file) +Cheevos::Cheevos(Screen *screen, options_t *options, std::string file, Online *online) { // Copia la dirección de los objetos this->options = options; this->screen = screen; this->file = file; - - // Crea objetos - online = new Online(options); + this->online = online; // Inicializa los logros init(); @@ -29,14 +27,13 @@ Cheevos::~Cheevos() save(); cheevos.clear(); - - // Libera memoria - delete online; } // Inicializa los logros void Cheevos::init() { + cheevos.clear(); + cheevos_t c; c.completed = false; c.valid = true; @@ -303,9 +300,19 @@ void Cheevos::loadFromServer() { std::string cheevosData = online->getCheevos(); + // Gestiona los posibles errores + const bool noData = cheevosData == "" ? true : false; + const bool incompleteData = cheevosData.length() != cheevos.size() ? true : false; + if (noData || incompleteData) + { + init(); + return; + } + + // Asigna los valores leídos desde el servidor for (int i = 0; i < (int)cheevosData.length(); ++i) { - bool value = cheevosData.substr(i, 1) == "1" ? true : false; + bool value = cheevosData.at(i) == '1' ? true : false; cheevos.at(i).completed = value; } } @@ -314,7 +321,7 @@ void Cheevos::loadFromServer() void Cheevos::saveToServer() { std::string cheevosData = ""; - + // cheevos[2].completed = true; for (auto cheevo : cheevos) { std::string data = cheevo.completed ? "1" : "0"; @@ -322,4 +329,10 @@ void Cheevos::saveToServer() } online->setCheevos(cheevosData); +} + +// Vuelve a cargar los logros desde el origen +void Cheevos::reload() +{ + load(); } \ No newline at end of file diff --git a/source/cheevos.h b/source/cheevos.h index d2c2500..2613c9a 100644 --- a/source/cheevos.h +++ b/source/cheevos.h @@ -58,7 +58,7 @@ private: public: // Constructor - Cheevos(Screen *screen, options_t *options, std::string file); + Cheevos(Screen *screen, options_t *options, std::string file, Online *online); // Destructor ~Cheevos(); @@ -80,6 +80,9 @@ public: // Devuelve el número total de logros int count(); + + // Vuelve a cargar los logros desde el origen + void reload(); }; #endif diff --git a/source/director.cpp b/source/director.cpp index aafecb0..a6d4227 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -62,8 +62,8 @@ Director::Director(int argc, char *argv[]) screen = new Screen(window, renderer, asset, options); screen->setBorderColor(borderColor); debug = new Debug(renderer, screen, asset); - online = new Online(options); music = JA_LoadMusic(asset->get("title.ogg").c_str()); + online = new Online(options); } Director::~Director() @@ -1762,7 +1762,7 @@ void Director::runTitle() JA_PlayMusic(music); } loadResources(section); - title = new Title(renderer, screen, resource, asset, input, options, section); + title = new Title(renderer, screen, resource, asset, input, online, options, section); title->run(); delete title; resource->free(); @@ -1847,7 +1847,7 @@ void Director::runGame() } JA_StopMusic(); loadResources(section); - game = new Game(renderer, screen, resource, asset, options, input, section, debug); + game = new Game(renderer, screen, resource, asset, online, options, input, section, debug); game->run(); delete game; resource->free(); diff --git a/source/director.h b/source/director.h index bb37651..794ff97 100644 --- a/source/director.h +++ b/source/director.h @@ -41,11 +41,11 @@ private: Intro *intro; // Objeto para gestionar la introducción del juego Credits *credits; // Objeto para gestionar los creditos del juego Demo *demo; // Objeto para gestionar el modo demo, en el que se ven pantallas del juego - Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto Ending *ending; // Objeto para gestionar el final del juego Ending2 *ending2; // Objeto para gestionar el final del juego GameOver *gameOver; // Objeto para gestionar el final de la partida Debug *debug; // Objeto para getsionar la información de debug + Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto struct options_t *options; // Variable con todas las opciones del programa section_t *section; // Sección y subsección actual del programa; diff --git a/source/game.cpp b/source/game.cpp index 3f04586..0ab4ce9 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -2,12 +2,13 @@ #include // Constructor -Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Input *input, section_t *section, Debug *debug) +Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Online *online, options_t *options, Input *input, section_t *section, Debug *debug) { // Copia los punteros this->resource = resource; this->renderer = renderer; this->asset = asset; + this->online = online; this->screen = screen; this->input = input; this->debug = debug; @@ -30,7 +31,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as #endif // Crea los objetos - cheevos = new Cheevos(screen, options, asset->get("cheevos.bin")); + cheevos = new Cheevos(screen, options, asset->get("cheevos.bin"), online); scoreboard = new ScoreBoard(renderer, resource, asset, options, &board); itemTracker = new ItemTracker(); roomTracker = new RoomTracker(); @@ -43,7 +44,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); 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"), asset->get("stats_buffer.csv"), options); + stats = new Stats(asset->get("stats.csv"), asset->get("stats_buffer.csv"), options, online); // Crea la textura para poner el nombre de la habitación roomNameTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, text->getCharacterSize() * 2); diff --git a/source/game.h b/source/game.h index 30c7be0..1319ee3 100644 --- a/source/game.h +++ b/source/game.h @@ -19,6 +19,7 @@ #include "room.h" #include "scoreboard.h" #include "stats.h" +#include "online.h" #ifndef GAME_H #define GAME_H @@ -37,6 +38,7 @@ private: Asset *asset; // Objeto con la ruta a todos los ficheros de recursos Input *input; // Objeto pata gestionar la entrada Text *text; // Objeto para los textos del juego + Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador Cheevos *cheevos; // Objeto encargado de gestionar los logros del juego Resource *resource; // Objeto con los recursos @@ -151,7 +153,7 @@ private: public: // Constructor - Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Input *input, section_t *section, Debug *debug); + Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Online *online, options_t *options, Input *input, section_t *section, Debug *debug); // Destructor ~Game(); diff --git a/source/online.cpp b/source/online.cpp index 93038e7..83be9e1 100644 --- a/source/online.cpp +++ b/source/online.cpp @@ -9,36 +9,63 @@ Online::Online(options_t *options) { this->options = options; + if (options->console) + { + std::cout << "ONLINE object created\n" + << std::endl; + } + allData = ""; statsData = ""; cheevosData = ""; - STATS_FLAG_INI = "STATS_FLAG_INI"; - STATS_FLAG_END = "STATS_FLAG_END"; - CHEEVOS_FLAG_INI = "CHEEVOS_FLAG_INI"; - CHEEVOS_FLAG_END = "CHEEVOS_FLAG_END"; + STATS_FLAG_INI = ";STATS_FLAG_INI;"; + STATS_FLAG_END = ";STATS_FLAG_END;"; + CHEEVOS_FLAG_INI = ";CHEEVOS_FLAG_INI;"; + CHEEVOS_FLAG_END = ";CHEEVOS_FLAG_END;"; - getAllData(); - std::cout << "allData: " << allData << std::endl; - std::cout << "statsData: " << statsData << std::endl; - std::cout << "cheevosData: " << cheevosData << std::endl; + dataCached = false; + dataSaved = ""; + getData(); } // Destructor Online::~Online() { + sendData(); } // Obtiene todos los datos y los coloca en sus respectivas variables -void Online::getAllData() +void Online::getData() { - if (!options->online.enabled) + // Si el usuario es distinto del que hay cacheado, marca la cache como invalida y borra los datos + if (jailerID.compare(options->online.jailerID) != 0) { - allData = jscore::getUserData(options->online.gameID, options->online.jailerID); + dataCached = false; + clearData(); } + // Si los datos ya estan cacheados, no hace nada + if (dataCached) + { + return; + } + + // Si las opciones online estan activadas, obtiene los datos desde el servidor + if (options->online.enabled) + { + allData = jscore::getUserData(options->online.gameID, options->online.jailerID); + jailerID = options->online.jailerID; + } + + // Si no ha podido obtener los datos del servidor, no hace nada if (allData.length() == 0) { + if (options->console) + { + std::cout << "NO DATA\n" + << std::endl; + } return; } @@ -59,44 +86,84 @@ void Online::getAllData() // Obtiene la cadena con los logros if (cheevosIni != std::string::npos && cheevosEnd != std::string::npos) cheevosData = allData.substr(cheevosIni, cheevosDataLenght); + + dataCached = true; + printData("LOADING"); } // Coloca todos los datos desde las variables en la cadena allData -void Online::setAllData() +void Online::sendData() { allData = STATS_FLAG_INI + statsData + STATS_FLAG_END + CHEEVOS_FLAG_INI + cheevosData + CHEEVOS_FLAG_END; - if (options->online.enabled) + const bool newData = allData.compare(dataSaved) == 0 ? false : true; + if (options->online.enabled && newData) { jscore::setUserData(options->online.gameID, options->online.jailerID, allData); + dataSaved = allData; + printData("SAVING"); } } // Obtiene las estadísticas guardadas en el servidor std::string Online::getStats() { - getAllData(); + getData(); return statsData; } // Guarda las estadísticas en el servidor void Online::setStats(std::string data) { - getAllData(); + // getData(); statsData = data; - setAllData(); + // setAllData(); } // Obtiene los logros guardadas en el servidor std::string Online::getCheevos() { - getAllData(); + getData(); return cheevosData; } // Guarda los logros en el servidor void Online::setCheevos(std::string data) { - getAllData(); + getData(); cheevosData = data; - setAllData(); + sendData(); +} + +// Imprime información de diagnóstico +void Online::printData(std::string text) +{ + static int counter = 0; + if (options->console) + { + std::cout << "mode is: " << text << " (" << counter << ")" << std::endl; + std::cout << "allData: " << allData << std::endl; + std::cout << "statsData: " << statsData << std::endl; + std::cout << "cheevosData: " << cheevosData << std::endl; + std::cout << "options->online.jailerID: " << options->online.jailerID << std::endl; + std::cout << "options->online.enabled: " << options->online.enabled << std::endl; + std::cout << std::endl; + counter++; + } +} + +// Elimina los datos del servidor +void Online::eraseServerData() +{ + if (options->online.enabled) + { + jscore::setUserData(options->online.gameID, options->online.jailerID, ""); + } +} + +// Limpia los datos almacenados en la caché +void Online::clearData() +{ + allData = ""; + statsData = ""; + cheevosData = ""; } \ No newline at end of file diff --git a/source/online.h b/source/online.h index 76aaf78..3f5abf6 100644 --- a/source/online.h +++ b/source/online.h @@ -23,11 +23,19 @@ private: std::string CHEEVOS_FLAG_INI; // Marca para establecer el inicio de los logros std::string CHEEVOS_FLAG_END; // Marca para establecer el final de los logros - // Obtiene todos los datos y los coloca en sus respectivas variables - void getAllData(); + bool dataCached; // Indica si se han obtenido los datos del servidor + std::string dataSaved; // Contiene los datos que se han salvado en el servidor + std::string jailerID; // ID del usuario cuyos datos estan cacheados - // Coloca todos los datos desde las variables en la cadena allData - void setAllData(); + + // Imprime información de diagnóstico + void printData(std::string text); + + // Elimina los datos del servidor + void eraseServerData(); + + // Limpia los datos almacenados en la caché + void clearData(); public: // Constructor @@ -36,6 +44,12 @@ public: // Destructor ~Online(); + // Obtiene todos los datos y los coloca en sus respectivas variables + void getData(); + + // Coloca todos los datos desde las variables en la cadena allData + void sendData(); + // Obtiene las estadísticas guardadas en el servidor std::string getStats(); diff --git a/source/stats.cpp b/source/stats.cpp index c593d6f..2af6b19 100644 --- a/source/stats.cpp +++ b/source/stats.cpp @@ -6,17 +6,15 @@ #include // Constructor -Stats::Stats(std::string file, std::string buffer, options_t *options) +Stats::Stats(std::string file, std::string buffer, options_t *options, Online *online) { this->options = options; + this->online = online; bufferPath = buffer; filePath = file; bufferList.clear(); list.clear(); dictionary.clear(); - - // Crea objetos - online = new Online(options); } // Destructor @@ -36,9 +34,6 @@ Stats::~Stats() bufferList.clear(); list.clear(); dictionary.clear(); - - // Libera memoria - delete options; } // Inicializador @@ -334,10 +329,4 @@ void Stats::updateListFromBuffer() saveToFile(bufferPath, bufferList); saveToFile(filePath, list); -} - -// Limpia los datos del servidor -void Stats::eraseServerData() -{ - jscore::setUserData(options->online.gameID, options->online.jailerID, ""); } \ No newline at end of file diff --git a/source/stats.h b/source/stats.h index d583ec3..638c809 100644 --- a/source/stats.h +++ b/source/stats.h @@ -62,12 +62,9 @@ private: // Vuelca los datos del buffer en la lista de estadisticas void updateListFromBuffer(); - // Limpia los datos del servidor - void eraseServerData(); - public: // Constructor - Stats(std::string file, std::string buffer, options_t *options); + Stats(std::string file, std::string buffer, options_t *options, Online *online); // Destructor ~Stats(); diff --git a/source/title.cpp b/source/title.cpp index a4954bb..c6272f0 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -1,7 +1,7 @@ #include "title.h" // Constructor -Title::Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section) +Title::Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, Online *online, options_t *options, section_t *section) { // Copia la dirección de los objetos this->resource = resource; @@ -9,12 +9,13 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset * this->screen = screen; this->asset = asset; this->input = input; + this->online = online; this->options = options; this->section = section; // Reserva memoria para los punteros eventHandler = new SDL_Event(); - cheevos = new Cheevos(screen, options, asset->get("cheevos.bin")); + cheevos = new Cheevos(screen, options, asset->get("cheevos.bin"), online); if (options->palette == p_zxspectrum) { texture = resource->getTexture("loading_screen_color.png"); @@ -118,6 +119,7 @@ void Title::checkEvents() case SDL_SCANCODE_3: runEnterID(); + cheevos->reload(); fillTexture(); createCheevosTexture(); break; diff --git a/source/title.h b/source/title.h index 086de42..cdc1b94 100644 --- a/source/title.h +++ b/source/title.h @@ -11,6 +11,7 @@ #include "jail_engine/sprite.h" #include "jail_engine/text.h" #include "jail_engine/utils.h" +#include "online.h" #include "const.h" #include @@ -33,6 +34,7 @@ private: Resource *resource; // Objeto con los recursos Asset *asset; // Objeto con los ficheros de recursos Input *input; // Objeto pata gestionar la entrada + Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto SDL_Event *eventHandler; // Manejador de eventos Texture *texture; // Textura con los graficos Sprite *sprite; // Sprite para manejar la textura @@ -103,7 +105,7 @@ private: public: // Constructor - Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section); + Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, Online *online, options_t *options, section_t *section); // Destructor ~Title();