210 lines
5.7 KiB
C++
210 lines
5.7 KiB
C++
#ifndef POKEMON_TABLE_H
|
|
#define POKEMON_TABLE_H
|
|
|
|
#include "pokemon.h"
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <optional>
|
|
|
|
namespace PokEng {
|
|
|
|
// Forward declaration
|
|
struct PokemonSpecies;
|
|
|
|
/**
|
|
* @brief A high-performance Pokemon data table that provides O(1) lookup by ID
|
|
*
|
|
* This class loads all Pokemon species data from JSON files and stores them
|
|
* in a way that allows for extremely fast runtime access by Pokemon ID.
|
|
* The table is designed to be loaded once at startup and then used throughout
|
|
* the application's lifetime.
|
|
*/
|
|
class PokemonTable {
|
|
public:
|
|
/**
|
|
* @brief Default constructor
|
|
*
|
|
* Creates an empty Pokemon table. Use loadFromDataDirectory() or
|
|
* loadFromFiles() to populate the table with data.
|
|
*/
|
|
PokemonTable() = default;
|
|
|
|
/**
|
|
* @brief Destructor
|
|
*/
|
|
~PokemonTable() = default;
|
|
|
|
// Delete copy operations to prevent accidental copies
|
|
PokemonTable(const PokemonTable&) = delete;
|
|
PokemonTable& operator=(const PokemonTable&) = delete;
|
|
|
|
// Allow move operations
|
|
PokemonTable(PokemonTable&&) = default;
|
|
PokemonTable& operator=(PokemonTable&&) = default;
|
|
|
|
/**
|
|
* @brief Load Pokemon data from the standard data directory
|
|
*
|
|
* This method loads all Pokemon data from the JSON files in the
|
|
* data/pokemon/ directory, parsing all generations.
|
|
*
|
|
* @return true if loading was successful, false otherwise
|
|
*/
|
|
bool loadFromDataDirectory();
|
|
|
|
/**
|
|
* @brief Load Pokemon data from specific JSON files
|
|
*
|
|
* @param filePaths Vector of paths to JSON files containing Pokemon data
|
|
* @return true if loading was successful, false otherwise
|
|
*/
|
|
bool loadFromFiles(const std::vector<std::string>& filePaths);
|
|
|
|
/**
|
|
* @brief Get a Pokemon species by its ID
|
|
*
|
|
* This is the primary lookup method and provides O(1) access time.
|
|
*
|
|
* @param id The Pokemon ID (1-based, e.g., 1 = Bulbasaur)
|
|
* @return Pointer to the PokemonSpecies if found, nullptr otherwise
|
|
*/
|
|
const PokemonSpecies* getPokemon(uint16_t id) const;
|
|
|
|
/**
|
|
* @brief Get a Pokemon species by its name
|
|
*
|
|
* This method provides O(1) access time using an internal hash map.
|
|
*
|
|
* @param name The Pokemon name (case-sensitive)
|
|
* @return Pointer to the PokemonSpecies if found, nullptr otherwise
|
|
*/
|
|
const PokemonSpecies* getPokemonByName(std::string_view name) const;
|
|
|
|
/**
|
|
* @brief Check if a Pokemon ID exists in the table
|
|
*
|
|
* @param id The Pokemon ID to check
|
|
* @return true if the Pokemon exists, false otherwise
|
|
*/
|
|
bool hasPokemon(uint16_t id) const;
|
|
|
|
/**
|
|
* @brief Get the total number of Pokemon species in the table
|
|
*
|
|
* @return The number of Pokemon species loaded
|
|
*/
|
|
size_t size() const;
|
|
|
|
/**
|
|
* @brief Check if the table is empty
|
|
*
|
|
* @return true if no Pokemon data has been loaded, false otherwise
|
|
*/
|
|
bool empty() const;
|
|
|
|
/**
|
|
* @brief Get the highest Pokemon ID in the table
|
|
*
|
|
* @return The maximum Pokemon ID, or 0 if the table is empty
|
|
*/
|
|
uint16_t getMaxId() const;
|
|
|
|
/**
|
|
* @brief Clear all data from the table
|
|
*/
|
|
void clear();
|
|
|
|
/**
|
|
* @brief Get all Pokemon species (for iteration)
|
|
*
|
|
* @return A const reference to the internal vector of Pokemon species
|
|
*/
|
|
const std::vector<PokemonSpecies>& getAllPokemon() const;
|
|
|
|
/**
|
|
* @brief Validate that the table contains valid data
|
|
*
|
|
* This method performs basic validation checks on the loaded data.
|
|
*
|
|
* @return true if the data appears valid, false otherwise
|
|
*/
|
|
bool validate() const;
|
|
|
|
private:
|
|
/**
|
|
* @brief Parse a single Pokemon JSON file
|
|
*
|
|
* @param filePath Path to the JSON file to parse
|
|
* @return true if parsing was successful, false otherwise
|
|
*/
|
|
bool parsePokemonFile(const std::string& filePath);
|
|
|
|
/**
|
|
* @brief Parse a single Pokemon object from JSON
|
|
*
|
|
* @param pokemonJson The JSON value representing a Pokemon
|
|
* @return The parsed PokemonSpecies object
|
|
*/
|
|
PokemonSpecies parsePokemonJson(const void* pokemonJson);
|
|
|
|
/**
|
|
* @brief Parse base stats from JSON
|
|
*
|
|
* @param statsJson The JSON value containing the stats object
|
|
* @return The parsed BaseStats object
|
|
*/
|
|
BaseStats parseBaseStats(const void* statsJson);
|
|
|
|
/**
|
|
* @brief Parse Pokemon types from JSON
|
|
*
|
|
* @param typesJson The JSON array containing the types
|
|
* @return The parsed PokemonTypes object
|
|
*/
|
|
PokemonTypes parseTypes(const void* typesJson);
|
|
|
|
private:
|
|
// Storage for Pokemon species - indexed by ID (1-based)
|
|
// Index 0 is unused, index 1 = Bulbasaur, etc.
|
|
std::vector<PokemonSpecies> m_pokemonSpecies;
|
|
|
|
// Hash map for name-based lookups
|
|
std::unordered_map<std::string, uint16_t> m_nameToIdMap;
|
|
|
|
// Track the maximum ID for validation
|
|
uint16_t m_maxId = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief Global Pokemon table instance
|
|
*
|
|
* This provides a convenient global access point to the Pokemon data.
|
|
* The table should be initialized once at application startup.
|
|
*/
|
|
extern std::unique_ptr<PokemonTable> g_pokemonTable;
|
|
|
|
/**
|
|
* @brief Initialize the global Pokemon table
|
|
*
|
|
* This function should be called once at application startup to load
|
|
* all Pokemon data. It loads from the standard data directory.
|
|
*
|
|
* @return true if initialization was successful, false otherwise
|
|
*/
|
|
bool initializePokemonTable();
|
|
|
|
/**
|
|
* @brief Shutdown the global Pokemon table
|
|
*
|
|
* This function should be called at application shutdown to clean up
|
|
* the global Pokemon table resources.
|
|
*/
|
|
void shutdownPokemonTable();
|
|
|
|
} // namespace PokEng
|
|
|
|
#endif // POKEMON_TABLE_H
|