Intentando guardar las estadisticas y logros online. Algo hay MUY MAL y no se que es. Ya lo miraré con calma.

This commit is contained in:
2023-10-09 15:28:51 +02:00
parent 6a526df9b3
commit 9b265c6cca
6 changed files with 116 additions and 14 deletions

View File

@@ -9,23 +9,29 @@ Cheevos::Cheevos(Screen *screen, options_t *options, std::string file)
this->screen = screen; this->screen = screen;
this->file = file; this->file = file;
// Crea objetos
online = new Online(options);
// Inicializa los logros // Inicializa los logros
init(); init();
// Inicializa variables // Inicializa variables
enabled = true; enabled = true;
// Carga el estado de los logros desde un fichero // Carga el estado de los logros
loadFromFile(); load();
} }
// Destructor // Destructor
Cheevos::~Cheevos() Cheevos::~Cheevos()
{ {
// Guarda el estado de los logros en un fichero // Guarda el estado de los logros
saveToFile(); save();
cheevos.clear(); cheevos.clear();
// Libera memoria
delete online;
} }
// Inicializa los logros // Inicializa los logros
@@ -138,7 +144,7 @@ void Cheevos::unlock(int id)
cheevos[index].completed = true; cheevos[index].completed = true;
screen->showNotification("ACHIEVEMENT UNLOCKED!", cheevos[index].caption, cheevos[index].icon); screen->showNotification("ACHIEVEMENT UNLOCKED!", cheevos[index].caption, cheevos[index].icon);
saveToFile(); save();
} }
// Invalida un logro // Invalida un logro
@@ -158,6 +164,32 @@ void Cheevos::enable(bool value)
enabled = value; enabled = value;
} }
// Carga el estado de los logros
void Cheevos::load()
{
if (options->online.enabled)
{ // Carga el estado de los logros desde el servidor online
loadFromServer();
}
else
{ // Carga el estado de los logros desde un fichero
loadFromFile();
}
}
// Guarda el estado de los logros
void Cheevos::save()
{
if (options->online.enabled)
{ // Guarda el estado de los logros en el servidor online
saveToServer();
}
else
{ // Guarda el estado de los logros en un fichero
saveToFile();
}
}
// Carga el estado de los logros desde un fichero // Carga el estado de los logros desde un fichero
void Cheevos::loadFromFile() void Cheevos::loadFromFile()
{ {
@@ -264,4 +296,30 @@ int Cheevos::unlocked()
int Cheevos::count() int Cheevos::count()
{ {
return cheevos.size(); return cheevos.size();
}
// Carga el estado de los logros desde el servidor online
void Cheevos::loadFromServer()
{
std::string cheevosData = online->getCheevos();
for (int i = 0; i < (int)cheevosData.length(); ++i)
{
bool value = cheevosData.substr(i, 1) == "1" ? true : false;
cheevos.at(i).completed = value;
}
}
// Guarda el estado de los logros en el servidor online
void Cheevos::saveToServer()
{
std::string cheevosData = "";
for (auto cheevo : cheevos)
{
std::string data = cheevo.completed ? "1" : "0";
cheevosData.append(data);
}
online->setCheevos(cheevosData);
} }

View File

@@ -2,6 +2,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "jail_engine/screen.h" #include "jail_engine/screen.h"
#include "jail_engine/utils.h" #include "jail_engine/utils.h"
#include "online.h"
#include <string> #include <string>
#include <vector> #include <vector>
@@ -24,6 +25,7 @@ private:
// Punteros y objetos // Punteros y objetos
Screen *screen; // Objeto encargado de dibujar en pantalla Screen *screen; // Objeto encargado de dibujar en pantalla
options_t *options; // Puntero a las opciones del juego options_t *options; // Puntero a las opciones del juego
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
// Variables // Variables
std::vector<cheevos_t> cheevos; // Listado de logros std::vector<cheevos_t> cheevos; // Listado de logros
@@ -36,12 +38,24 @@ private:
// Busca un logro por id y devuelve el indice // Busca un logro por id y devuelve el indice
int find(int id); int find(int id);
// Carga el estado de los logros
void load();
// Guarda el estado de los logros
void save();
// Carga el estado de los logros desde un fichero // Carga el estado de los logros desde un fichero
void loadFromFile(); void loadFromFile();
// Guarda el estado de los logros en un fichero // Guarda el estado de los logros en un fichero
void saveToFile(); void saveToFile();
// Carga el estado de los logros desde el servidor online
void loadFromServer();
// Guarda el estado de los logros en el servidor online
void saveToServer();
public: public:
// Constructor // Constructor
Cheevos(Screen *screen, options_t *options, std::string file); Cheevos(Screen *screen, options_t *options, std::string file);

View File

@@ -62,6 +62,7 @@ Director::Director(int argc, char *argv[])
screen = new Screen(window, renderer, asset, options); screen = new Screen(window, renderer, asset, options);
screen->setBorderColor(borderColor); screen->setBorderColor(borderColor);
debug = new Debug(renderer, screen, asset); debug = new Debug(renderer, screen, asset);
online = new Online(options);
music = JA_LoadMusic(asset->get("title.ogg").c_str()); music = JA_LoadMusic(asset->get("title.ogg").c_str());
} }
@@ -78,6 +79,7 @@ Director::~Director()
delete screen; delete screen;
delete debug; delete debug;
delete resource; delete resource;
delete online;
JA_DeleteMusic(music); JA_DeleteMusic(music);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);

View File

@@ -7,14 +7,21 @@
// Constructor // Constructor
Online::Online(options_t *options) Online::Online(options_t *options)
{ {
this->options = options;
allData = ""; allData = "";
statsData = ""; statsData = "";
cheevosData = ""; cheevosData = "";
STATS_FLAG_INI = "STATS_FLAG_INI"; STATS_FLAG_INI = "STATS_FLAG_INI";
STATS_FLAG_END = "STATS_FLAG_END"; STATS_FLAG_END = "STATS_FLAG_END";
CHEEVOS_FLAG_INI = "CHEEVOS_FLAG_INI"; CHEEVOS_FLAG_INI = "CHEEVOS_FLAG_INI";
CHEEVOS_FLAG_END = "CHEEVOS_FLAG_END"; 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;
} }
// Destructor // Destructor
@@ -25,24 +32,33 @@ Online::~Online()
// Obtiene todos los datos y los coloca en sus respectivas variables // Obtiene todos los datos y los coloca en sus respectivas variables
void Online::getAllData() void Online::getAllData()
{ {
if (options->online.enabled) if (!options->online.enabled)
{ {
allData = jscore::getUserData(options->online.gameID, options->online.jailerID); allData = jscore::getUserData(options->online.gameID, options->online.jailerID);
} }
if (allData.length() == 0)
{
return;
}
// Obtiene el inicio y el fin de la cadena con las estadisticas // Obtiene el inicio y el fin de la cadena con las estadisticas
const size_t statsIni = allData.find(STATS_FLAG_INI) + STATS_FLAG_INI.length(); const size_t statsIni = allData.find(STATS_FLAG_INI) + STATS_FLAG_INI.length();
const size_t statsEnd = allData.find(STATS_FLAG_END); const size_t statsEnd = allData.find(STATS_FLAG_END);
const size_t statsDataLenght = statsEnd - statsIni;
// Obtiene la cadena con las estadisticas // Obtiene la cadena con las estadisticas
statsData = allData.substr(statsIni, statsEnd); if (statsIni != std::string::npos && statsEnd != std::string::npos)
statsData = allData.substr(statsIni, statsDataLenght);
// Obtiene el inicio y el fin de la cadena con los logros // Obtiene el inicio y el fin de la cadena con los logros
const size_t cheevosIni = allData.find(CHEEVOS_FLAG_INI) + CHEEVOS_FLAG_INI.length(); const size_t cheevosIni = allData.find(CHEEVOS_FLAG_INI) + CHEEVOS_FLAG_INI.length();
const size_t cheevosEnd = allData.find(CHEEVOS_FLAG_END); const size_t cheevosEnd = allData.find(CHEEVOS_FLAG_END);
const size_t cheevosDataLenght = cheevosEnd - cheevosIni;
// Obtiene la cadena con los logros // Obtiene la cadena con los logros
cheevosData = allData.substr(cheevosIni, cheevosEnd); if (cheevosIni != std::string::npos && cheevosEnd != std::string::npos)
cheevosData = allData.substr(cheevosIni, cheevosDataLenght);
} }
// Coloca todos los datos desde las variables en la cadena allData // Coloca todos los datos desde las variables en la cadena allData
@@ -65,6 +81,7 @@ std::string Online::getStats()
// Guarda las estadísticas en el servidor // Guarda las estadísticas en el servidor
void Online::setStats(std::string data) void Online::setStats(std::string data)
{ {
getAllData();
statsData = data; statsData = data;
setAllData(); setAllData();
} }
@@ -79,6 +96,7 @@ std::string Online::getCheevos()
// Guarda los logros en el servidor // Guarda los logros en el servidor
void Online::setCheevos(std::string data) void Online::setCheevos(std::string data)
{ {
getAllData();
cheevosData = data; cheevosData = data;
setAllData(); setAllData();
} }

View File

@@ -14,6 +14,9 @@ Stats::Stats(std::string file, std::string buffer, options_t *options)
bufferList.clear(); bufferList.clear();
list.clear(); list.clear();
dictionary.clear(); dictionary.clear();
// Crea objetos
online = new Online(options);
} }
// Destructor // Destructor
@@ -33,6 +36,9 @@ Stats::~Stats()
bufferList.clear(); bufferList.clear();
list.clear(); list.clear();
dictionary.clear(); dictionary.clear();
// Libera memoria
delete options;
} }
// Inicializador // Inicializador
@@ -180,7 +186,8 @@ void Stats::loadFromServer()
std::string data; std::string data;
if (options->online.enabled) if (options->online.enabled)
{ {
data = jscore::getUserData(options->online.gameID, options->online.jailerID); //data = jscore::getUserData(options->online.gameID, options->online.jailerID);
data = online->getStats();
} }
std::stringstream ss(data); std::stringstream ss(data);
@@ -244,7 +251,8 @@ void Stats::saveToServer()
{ {
data = data + nameToNumber(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); //jscore::setUserData(options->online.gameID, options->online.jailerID, data);
online->setStats(data);
} }
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "jail_engine/utils.h" #include "jail_engine/utils.h"
#include "online.h"
#include <string> #include <string>
#include <vector> #include <vector>
@@ -24,7 +25,8 @@ private:
}; };
// Punteros y objetos // Punteros y objetos
options_t *options; options_t *options; // Puntero a la variable con todas las opciones del programa
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
// Variables // Variables
std::vector<stats_dictionary_t> dictionary; // Lista con la equivalencia nombre-numero de habitacion std::vector<stats_dictionary_t> dictionary; // Lista con la equivalencia nombre-numero de habitacion