Player12
An open source football management simulator
Loading...
Searching...
No Matches
GameData Class Reference

Singleton class that acts as an in-memory cache for the database state. More...

#include <gamedata.h>

Public Member Functions

 GameData ()
 Constructs a new GameData instance.
 
 GameData (const GameData &)=delete
 
GameData & operator= (const GameData &)=delete
 
bool loadFromDB (std::shared_ptr< DatabaseConnection > database_ptr)
 Loads the initial game state from the database.
 
bool saveToDB () const
 Saves the current in-memory state back to the database.
 
const StatsConfig & getStatsConfig () const
 
void addLeague (LeagueID id, const League &league)
 
std::optional< std::reference_wrapper< const League > > getLeague (LeagueID id) const
 
const std::unordered_map< LeagueID, League > & getLeagues () const
 
std::unordered_map< LeagueID, League > & getLeagues ()
 
const std::vector< std::reference_wrapper< const League > > & getLeaguesVector () const
 
void addTeam (TeamID id, const Team &team)
 
std::optional< std::reference_wrapper< Team > > getTeam (TeamID id)
 
std::optional< std::reference_wrapper< const Team > > getTeam (TeamID id) const
 
const std::unordered_map< TeamID, Team > & getTeams () const
 
std::unordered_map< TeamID, Team > & getTeams ()
 
const std::vector< std::reference_wrapper< const Team > > & getTeamsVector () const
 
void ageAllPlayers ()
 
std::vector< PlayerID > advanceContractsAndReleasePlayers (TeamID keep_selection=FREE_AGENTS_TEAM_ID)
 
void addPlayer (PlayerID id, const Player &player)
 
std::optional< std::reference_wrapper< const Player > > getPlayer (PlayerID id) const
 
const std::unordered_map< PlayerID, Player > & getPlayers () const
 
std::unordered_map< PlayerID, Player > & getPlayers ()
 
const std::vector< std::reference_wrapper< const Player > > & getPlayersVector () const
 
const std::vector< std::reference_wrapper< const Player > > & getPlayersForTeam (TeamID team_id) const
 
const std::vector< std::reference_wrapper< const Player > > & getAcademyForTeam (TeamID team_id) const
 
void setAcademyMember (PlayerID id, bool academy)
 
bool removePlayer (PlayerID id)
 
void transferPlayer (PlayerID id, TeamID new_team_id)
 
SquadNumbers::Change setSquadNumber (PlayerID id, int number)
 
void assignSquadNumbers (TeamID team_id, PlayerID newcomer=0)
 
void setWorldSeed (std::uint64_t seed)
 Seeds the generation of a new world (call before loadFromDB).
 
std::uint64_t getWorldSeed () const
 Seed of every random stream of the world simulation.
 
PlayerID allocatePlayerId ()
 Returns a new, never reused player id.
 
PlayerID peekNextPlayerId () const
 The id the next allocatePlayerId() call will return.
 
const std::vector< PlayerID > & getRemovedPlayerIds () const
 Players removed (retired) since the last save.
 
void clearRemovedPlayerIds ()
 Forgets removed players once their deletion is persisted.
 
StaffRoster & getStaff ()
 Coaches, medics and scouts of every club plus the staff market.
 
const StaffRoster & getStaff () const
 
TrainingRegistry & getTraining ()
 Training plans, familiarity and player workloads.
 
const TrainingRegistry & getTraining () const
 
void saveTransferListing (const TransferListing &listing) const
 Saves a transfer listing to the database (UPSERT).
 
void deleteTransferListing (PlayerID player_id) const
 Deletes a transfer listing from the database.
 
std::unordered_map< PlayerID, TransferListing > loadAllTransferListings () const
 Loads all transfer listings from the database into a map.
 

Private Member Functions

void loadStatsConfig ()
 
void generateAndSaveInitialData ()
 
void loadExistingData ()
 
void migrateSchema () const
 
void restoreWorldState ()
 
void restoreStaffAndTraining ()
 

Private Attributes

std::unordered_map< LeagueID, League > _leagues
 
std::vector< std::reference_wrapper< const League > > _leaguesVec
 
std::unordered_map< TeamID, Team > _teams
 
std::vector< std::reference_wrapper< const Team > > _teamsVec
 
std::unordered_map< PlayerID, Player > _players
 
std::vector< std::reference_wrapper< const Player > > _playersVec
 
std::unordered_map< TeamID, std::vector< std::reference_wrapper< const Player > > > _teamPlayers
 
std::unordered_map< TeamID, std::vector< std::reference_wrapper< const Player > > > _teamAcademies
 
StatsConfig stats_config
 
std::shared_ptr< DatabaseConnection > db_conn
 
std::uint64_t world_seed = 0
 
bool world_seed_set = false
 
PlayerID next_player_id = 1
 
std::vector< PlayerID > removed_player_ids
 
StaffRoster staff
 
TrainingRegistry training
 

Detailed Description

Singleton class that acts as an in-memory cache for the database state.

GameData is responsible for loading the initial game state from SQLite, holding all core entities (Leagues, Teams, Players), and facilitating high-performance read/write access during gameplay without hitting the disk.

Constructor & Destructor Documentation

◆ GameData() [1/2]

GameData::GameData ( )
default

Constructs a new GameData instance.

◆ GameData() [2/2]

GameData::GameData ( const GameData &  )
delete

Member Function Documentation

◆ addLeague()

void GameData::addLeague ( LeagueID  id,
const League &  league 
)

◆ addPlayer()

void GameData::addPlayer ( PlayerID  id,
const Player &  player 
)

◆ addTeam()

void GameData::addTeam ( TeamID  id,
const Team &  team 
)

◆ advanceContractsAndReleasePlayers()

std::vector< PlayerID > GameData::advanceContractsAndReleasePlayers ( TeamID  keep_selection = FREE_AGENTS_TEAM_ID)

Advances contracts and releases newly out-of-contract players. Clubs that lose a player get a new line-up, except keep_selection (the manager's club), whose gaps the caller fills.

◆ ageAllPlayers()

void GameData::ageAllPlayers ( )

◆ allocatePlayerId()

PlayerID GameData::allocatePlayerId ( )

Returns a new, never reused player id.

◆ assignSquadNumbers()

void GameData::assignSquadNumbers ( TeamID  team_id,
PlayerID  newcomer = 0 
)

Numbers the club's senior players who have none or share one. The number of newcomer (just arrived) only counts as his wish.

◆ clearRemovedPlayerIds()

void GameData::clearRemovedPlayerIds ( )

Forgets removed players once their deletion is persisted.

◆ deleteTransferListing()

void GameData::deleteTransferListing ( PlayerID  player_id) const

Deletes a transfer listing from the database.

◆ generateAndSaveInitialData()

void GameData::generateAndSaveInitialData ( )
private

◆ getAcademyForTeam()

const std::vector< std::reference_wrapper< const Player > > & GameData::getAcademyForTeam ( TeamID  team_id) const

Academy players of a club (still club players for contracts and wages, but not part of the senior squad).

◆ getLeague()

std::optional< std::reference_wrapper< const League > > GameData::getLeague ( LeagueID  id) const

◆ getLeagues() [1/2]

std::unordered_map< LeagueID, League > & GameData::getLeagues ( )

◆ getLeagues() [2/2]

const std::unordered_map< LeagueID, League > & GameData::getLeagues ( ) const

◆ getLeaguesVector()

const std::vector< std::reference_wrapper< const League > > & GameData::getLeaguesVector ( ) const

◆ getPlayer()

std::optional< std::reference_wrapper< const Player > > GameData::getPlayer ( PlayerID  id) const

◆ getPlayers() [1/2]

std::unordered_map< PlayerID, Player > & GameData::getPlayers ( )

◆ getPlayers() [2/2]

const std::unordered_map< PlayerID, Player > & GameData::getPlayers ( ) const

◆ getPlayersForTeam()

const std::vector< std::reference_wrapper< const Player > > & GameData::getPlayersForTeam ( TeamID  team_id) const

Senior squad of a club: academy players (U18 squad and intake trialists) are listed by getAcademyForTeam().

◆ getPlayersVector()

const std::vector< std::reference_wrapper< const Player > > & GameData::getPlayersVector ( ) const

◆ getRemovedPlayerIds()

const std::vector< PlayerID > & GameData::getRemovedPlayerIds ( ) const

Players removed (retired) since the last save.

◆ getStaff() [1/2]

StaffRoster & GameData::getStaff ( )
inline

Coaches, medics and scouts of every club plus the staff market.

◆ getStaff() [2/2]

const StaffRoster & GameData::getStaff ( ) const
inline

◆ getStatsConfig()

const StatsConfig & GameData::getStatsConfig ( ) const

◆ getTeam() [1/2]

std::optional< std::reference_wrapper< Team > > GameData::getTeam ( TeamID  id)

◆ getTeam() [2/2]

std::optional< std::reference_wrapper< const Team > > GameData::getTeam ( TeamID  id) const

◆ getTeams() [1/2]

std::unordered_map< TeamID, Team > & GameData::getTeams ( )

◆ getTeams() [2/2]

const std::unordered_map< TeamID, Team > & GameData::getTeams ( ) const

◆ getTeamsVector()

const std::vector< std::reference_wrapper< const Team > > & GameData::getTeamsVector ( ) const

◆ getTraining() [1/2]

TrainingRegistry & GameData::getTraining ( )
inline

Training plans, familiarity and player workloads.

◆ getTraining() [2/2]

const TrainingRegistry & GameData::getTraining ( ) const
inline

◆ getWorldSeed()

std::uint64_t GameData::getWorldSeed ( ) const

Seed of every random stream of the world simulation.

◆ loadAllTransferListings()

std::unordered_map< PlayerID, TransferListing > GameData::loadAllTransferListings ( ) const

Loads all transfer listings from the database into a map.

◆ loadExistingData()

void GameData::loadExistingData ( )
private

◆ loadFromDB()

bool GameData::loadFromDB ( std::shared_ptr< DatabaseConnection >  database_ptr)

Loads the initial game state from the database.

Parameters
database_ptrShared pointer to the active DatabaseConnection.
Returns
True if successful, false otherwise.

◆ loadStatsConfig()

void GameData::loadStatsConfig ( )
private

◆ migrateSchema()

void GameData::migrateSchema ( ) const
private

◆ operator=()

GameData & GameData::operator= ( const GameData &  )
delete

◆ peekNextPlayerId()

PlayerID GameData::peekNextPlayerId ( ) const

The id the next allocatePlayerId() call will return.

◆ removePlayer()

bool GameData::removePlayer ( PlayerID  id)

◆ restoreStaffAndTraining()

void GameData::restoreStaffAndTraining ( )
private

◆ restoreWorldState()

void GameData::restoreWorldState ( )
private

◆ saveToDB()

bool GameData::saveToDB ( ) const

Saves the current in-memory state back to the database.

Returns
True if successful, false otherwise.

◆ saveTransferListing()

void GameData::saveTransferListing ( const TransferListing &  listing) const

Saves a transfer listing to the database (UPSERT).

◆ setAcademyMember()

void GameData::setAcademyMember ( PlayerID  id,
bool  academy 
)

Moves a player of his club between the senior squad and the academy: sets Player::isAcademyPlayer() and keeps the club's lists in sync.

◆ setSquadNumber()

SquadNumbers::Change GameData::setSquadNumber ( PlayerID  id,
int  number 
)

Gives id squad number number (1-99) at his club; a teammate wearing it takes his old number.

◆ setWorldSeed()

void GameData::setWorldSeed ( std::uint64_t  seed)

Seeds the generation of a new world (call before loadFromDB).

Without an explicit seed a new world uses FM_WORLD_SEED from the environment or, failing that, the system clock. Loaded saves use their stored seed.

◆ transferPlayer()

void GameData::transferPlayer ( PlayerID  id,
TeamID  new_team_id 
)

Moves a player to another club's senior squad (academy membership does not travel with him). He keeps his squad number when it is free at the new club, otherwise he gets a free one (SquadNumbers).

Member Data Documentation

◆ _leagues

std::unordered_map<LeagueID, League> GameData::_leagues
private

◆ _leaguesVec

std::vector<std::reference_wrapper<const League> > GameData::_leaguesVec
private

◆ _players

std::unordered_map<PlayerID, Player> GameData::_players
private

◆ _playersVec

std::vector<std::reference_wrapper<const Player> > GameData::_playersVec
private

◆ _teamAcademies

std::unordered_map<TeamID, std::vector<std::reference_wrapper<const Player> > > GameData::_teamAcademies
private

◆ _teamPlayers

std::unordered_map<TeamID, std::vector<std::reference_wrapper<const Player> > > GameData::_teamPlayers
private

◆ _teams

std::unordered_map<TeamID, Team> GameData::_teams
private

◆ _teamsVec

std::vector<std::reference_wrapper<const Team> > GameData::_teamsVec
private

◆ db_conn

std::shared_ptr<DatabaseConnection> GameData::db_conn
private

◆ next_player_id

PlayerID GameData::next_player_id = 1
private

◆ removed_player_ids

std::vector<PlayerID> GameData::removed_player_ids
private

◆ staff

StaffRoster GameData::staff
private

◆ stats_config

StatsConfig GameData::stats_config
private

◆ training

TrainingRegistry GameData::training
private

◆ world_seed

std::uint64_t GameData::world_seed = 0
private

◆ world_seed_set

bool GameData::world_seed_set = false
private

The documentation for this class was generated from the following files: