#include "jail_engine/jscore.h" #include "online.h" #include #include #include // Constructor 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;"; dataCached = false; dataSaved = ""; getData(); } // Destructor Online::~Online() { sendData(); } // Obtiene todos los datos y los coloca en sus respectivas variables void Online::getData() { // 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) { 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; } // 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 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 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::sendData() { allData = STATS_FLAG_INI + statsData + STATS_FLAG_END + CHEEVOS_FLAG_INI + cheevosData + CHEEVOS_FLAG_END; 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() { getData(); return statsData; } // Guarda las estadísticas en el servidor void Online::setStats(std::string data) { // getData(); statsData = data; // setAllData(); } // Obtiene los logros guardadas en el servidor std::string Online::getCheevos() { getData(); return cheevosData; } // Guarda los logros en el servidor void Online::setCheevos(std::string data) { getData(); cheevosData = data; 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 = ""; }