Files
coffee-crisis-ae/source/manage_hiscore_table.hpp
T

59 lines
2.7 KiB
C++

#pragma once
#include <SDL3/SDL.h> // Para SDL_IOStream
#include <string> // Para std::string
#include <vector> // Para std::vector
// --- Estructuras ---
struct HiScoreEntry {
std::string name; // Nombre
int score; // Puntuación
bool one_credit_complete; // Indica si se ha conseguido 1CC
// Constructor
explicit HiScoreEntry(const std::string& name = "", int score = 0, bool one_credit_complete = false)
: name(name.substr(0, 6)),
score(score),
one_credit_complete(one_credit_complete) {}
};
// --- Tipos ---
using Table = std::vector<HiScoreEntry>; // Tabla de puntuaciones
// --- Clase ManageHiScoreTable ---
class ManageHiScoreTable {
public:
// --- Constantes ---
static constexpr int NO_ENTRY = -1;
static constexpr int FILE_VERSION = 1;
static constexpr int MAX_TABLE_SIZE = 100;
static constexpr int MAX_NAME_SIZE = 50;
static constexpr int MAX_SCORE = 999999999;
// --- Constructor y destructor ---
explicit ManageHiScoreTable(Table& table) // Constructor con referencia a tabla
: table_(table) {}
~ManageHiScoreTable() = default; // Destructor
// --- Métodos públicos ---
void clear(); // Resetea la tabla a los valores por defecto
auto add(const HiScoreEntry& entry) -> int; // Añade un elemento a la tabla (devuelve la posición en la que se inserta)
auto loadFromFile(const std::string& file_path) -> bool; // Carga la tabla con los datos de un fichero
auto saveToFile(const std::string& file_path) -> bool; // Guarda la tabla en un fichero
private:
// --- Variables privadas ---
Table& table_; // Referencia a la tabla con los records
// --- Métodos privados ---
void sort(); // Ordena la tabla
static auto calculateChecksum(const Table& table) -> unsigned int; // Calcula checksum de la tabla
// Métodos auxiliares para loadFromFile
static auto validateMagicNumber(SDL_IOStream* file, const std::string& file_path) -> bool;
static auto validateVersion(SDL_IOStream* file, const std::string& file_path) -> bool;
static auto readTableSize(SDL_IOStream* file, const std::string& file_path, int& table_size) -> bool;
static auto readEntry(SDL_IOStream* file, const std::string& file_path, int index, HiScoreEntry& entry) -> bool;
static auto verifyChecksum(SDL_IOStream* file, const std::string& file_path, const Table& temp_table) -> bool;
};