pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
#include "manage_hiscore_table.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_ReadIO, SDL_WriteIO, SDL_CloseIO, SDL_GetE...
|
||||
#include <SDL3/SDL.h> // Para SDL_ReadIO, SDL_WriteIO, SDL_CloseIO, SDL_GetE...
|
||||
|
||||
#include <algorithm> // Para find_if, sort
|
||||
#include <iterator> // Para distance
|
||||
|
||||
#include "utils.h" // Para getFileName
|
||||
#include "utils.h" // Para getFileName
|
||||
|
||||
// Resetea la tabla a los valores por defecto
|
||||
void ManageHiScoreTable::clear()
|
||||
{
|
||||
void ManageHiScoreTable::clear() {
|
||||
// Limpia la tabla
|
||||
table_.clear();
|
||||
|
||||
@@ -28,8 +28,7 @@ void ManageHiScoreTable::clear()
|
||||
}
|
||||
|
||||
// Añade un elemento a la tabla
|
||||
int ManageHiScoreTable::add(const HiScoreEntry &entry)
|
||||
{
|
||||
int ManageHiScoreTable::add(const HiScoreEntry &entry) {
|
||||
// Añade la entrada a la tabla
|
||||
table_.push_back(entry);
|
||||
|
||||
@@ -37,26 +36,22 @@ int ManageHiScoreTable::add(const HiScoreEntry &entry)
|
||||
sort();
|
||||
|
||||
// Encontrar la posición del nuevo elemento
|
||||
auto it = std::find_if(table_.begin(), table_.end(), [&](const HiScoreEntry &e)
|
||||
{ return e.name == entry.name &&
|
||||
e.score == entry.score &&
|
||||
e.one_credit_complete == entry.one_credit_complete; });
|
||||
auto it = std::find_if(table_.begin(), table_.end(), [&](const HiScoreEntry &e) { return e.name == entry.name &&
|
||||
e.score == entry.score &&
|
||||
e.one_credit_complete == entry.one_credit_complete; });
|
||||
|
||||
int position = -1;
|
||||
if (it != table_.end())
|
||||
{
|
||||
if (it != table_.end()) {
|
||||
position = std::distance(table_.begin(), it);
|
||||
}
|
||||
|
||||
// Deja solo las 10 primeras entradas
|
||||
if (table_.size() > 10)
|
||||
{
|
||||
if (table_.size() > 10) {
|
||||
table_.resize(10);
|
||||
|
||||
// Si el nuevo elemento quedó fuera del top 10
|
||||
if (position >= 10)
|
||||
{
|
||||
position = -1; // No entró en el top 10
|
||||
if (position >= 10) {
|
||||
position = -1; // No entró en el top 10
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +60,7 @@ int ManageHiScoreTable::add(const HiScoreEntry &entry)
|
||||
}
|
||||
|
||||
// Ordena la tabla
|
||||
void ManageHiScoreTable::sort()
|
||||
{
|
||||
void ManageHiScoreTable::sort() {
|
||||
struct
|
||||
{
|
||||
bool operator()(const HiScoreEntry &a, const HiScoreEntry &b) const { return a.score > b.score; }
|
||||
@@ -76,23 +70,20 @@ void ManageHiScoreTable::sort()
|
||||
}
|
||||
|
||||
// Carga la tabla desde un fichero
|
||||
bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
|
||||
{
|
||||
bool ManageHiScoreTable::loadFromFile(const std::string &file_path) {
|
||||
clear();
|
||||
auto success = true;
|
||||
auto file = SDL_IOFromFile(file_path.c_str(), "rb");
|
||||
|
||||
if (file)
|
||||
{
|
||||
table_.clear(); // Limpia la tabla actual
|
||||
if (file) {
|
||||
table_.clear(); // Limpia la tabla actual
|
||||
|
||||
// Lee el número de entradas en la tabla
|
||||
int table_size = 0;
|
||||
SDL_ReadIO(file, &table_size, sizeof(int));
|
||||
|
||||
// Lee los datos de cada entrada
|
||||
for (int i = 0; i < table_size; ++i)
|
||||
{
|
||||
for (int i = 0; i < table_size; ++i) {
|
||||
HiScoreEntry entry;
|
||||
|
||||
// Lee la puntuación
|
||||
@@ -104,7 +95,7 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
|
||||
|
||||
std::vector<char> name_buffer(name_size + 1);
|
||||
SDL_ReadIO(file, name_buffer.data(), name_size);
|
||||
name_buffer[name_size] = '\0'; // Asegurar el fin de la cadena
|
||||
name_buffer[name_size] = '\0'; // Asegurar el fin de la cadena
|
||||
entry.name = std::string(name_buffer.data());
|
||||
|
||||
// Lee el valor de one_credit_complete
|
||||
@@ -118,9 +109,7 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(file_path).c_str());
|
||||
SDL_CloseIO(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to load %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
@@ -128,20 +117,17 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
|
||||
}
|
||||
|
||||
// Guarda la tabla en un fichero
|
||||
bool ManageHiScoreTable::saveToFile(const std::string &file_path)
|
||||
{
|
||||
bool ManageHiScoreTable::saveToFile(const std::string &file_path) {
|
||||
auto success = true;
|
||||
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
if (file)
|
||||
{
|
||||
if (file) {
|
||||
// Guarda el número de entradas en la tabla
|
||||
int table_size = static_cast<int>(table_.size());
|
||||
SDL_WriteIO(file, &table_size, sizeof(int));
|
||||
|
||||
// Guarda los datos de cada entrada
|
||||
for (int i = 0; i < table_size; ++i)
|
||||
{
|
||||
for (int i = 0; i < table_size; ++i) {
|
||||
const HiScoreEntry &entry = table_.at(i);
|
||||
|
||||
// Guarda la puntuación
|
||||
@@ -159,9 +145,7 @@ bool ManageHiScoreTable::saveToFile(const std::string &file_path)
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file: %s", getFileName(file_path).c_str());
|
||||
SDL_CloseIO(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user