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->file = file;
// Crea objetos
online = new Online(options);
// Inicializa los logros
init();
// Inicializa variables
enabled = true;
// Carga el estado de los logros desde un fichero
loadFromFile();
// Carga el estado de los logros
load();
}
// Destructor
Cheevos::~Cheevos()
{
// Guarda el estado de los logros en un fichero
saveToFile();
// Guarda el estado de los logros
save();
cheevos.clear();
// Libera memoria
delete online;
}
// Inicializa los logros
@@ -138,7 +144,7 @@ void Cheevos::unlock(int id)
cheevos[index].completed = true;
screen->showNotification("ACHIEVEMENT UNLOCKED!", cheevos[index].caption, cheevos[index].icon);
saveToFile();
save();
}
// Invalida un logro
@@ -158,6 +164,32 @@ void Cheevos::enable(bool 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
void Cheevos::loadFromFile()
{
@@ -265,3 +297,29 @@ int Cheevos::count()
{
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 "jail_engine/screen.h"
#include "jail_engine/utils.h"
#include "online.h"
#include <string>
#include <vector>
@@ -24,6 +25,7 @@ private:
// Punteros y objetos
Screen *screen; // Objeto encargado de dibujar en pantalla
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
std::vector<cheevos_t> cheevos; // Listado de logros
@@ -36,12 +38,24 @@ private:
// Busca un logro por id y devuelve el indice
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
void loadFromFile();
// Guarda el estado de los logros en un fichero
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:
// Constructor
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->setBorderColor(borderColor);
debug = new Debug(renderer, screen, asset);
online = new Online(options);
music = JA_LoadMusic(asset->get("title.ogg").c_str());
}
@@ -78,6 +79,7 @@ Director::~Director()
delete screen;
delete debug;
delete resource;
delete online;
JA_DeleteMusic(music);
SDL_DestroyRenderer(renderer);

View File

@@ -7,6 +7,8 @@
// Constructor
Online::Online(options_t *options)
{
this->options = options;
allData = "";
statsData = "";
cheevosData = "";
@@ -15,6 +17,11 @@ Online::Online(options_t *options)
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;
}
// Destructor
@@ -25,24 +32,33 @@ Online::~Online()
// Obtiene todos los datos y los coloca en sus respectivas variables
void Online::getAllData()
{
if (options->online.enabled)
if (!options->online.enabled)
{
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
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 statsDataLenght = statsEnd - statsIni;
// 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
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 cheevosDataLenght = cheevosEnd - cheevosIni;
// 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
@@ -65,6 +81,7 @@ std::string Online::getStats()
// Guarda las estadísticas en el servidor
void Online::setStats(std::string data)
{
getAllData();
statsData = data;
setAllData();
}
@@ -79,6 +96,7 @@ std::string Online::getCheevos()
// Guarda los logros en el servidor
void Online::setCheevos(std::string data)
{
getAllData();
cheevosData = data;
setAllData();
}

View File

@@ -14,6 +14,9 @@ Stats::Stats(std::string file, std::string buffer, options_t *options)
bufferList.clear();
list.clear();
dictionary.clear();
// Crea objetos
online = new Online(options);
}
// Destructor
@@ -33,6 +36,9 @@ Stats::~Stats()
bufferList.clear();
list.clear();
dictionary.clear();
// Libera memoria
delete options;
}
// Inicializador
@@ -180,7 +186,8 @@ void Stats::loadFromServer()
std::string data;
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);
@@ -244,7 +251,8 @@ void Stats::saveToServer()
{
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
#include <SDL2/SDL.h>
#include "jail_engine/utils.h"
#include "online.h"
#include <string>
#include <vector>
@@ -24,7 +25,8 @@ private:
};
// 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
std::vector<stats_dictionary_t> dictionary; // Lista con la equivalencia nombre-numero de habitacion