Player12
An open source football management simulator
Loading...
Searching...
No Matches
gamedata.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <cstdint>
12#include <functional>
13#include <memory>
14#include <optional>
15#include <unordered_map>
16#include <vector>
17
18#include "gamedate.h"
19#include "global/global.h"
20#include "global/stats_config.h"
21#include "global/types.h"
22#include "model/league.h"
23#include "model/player.h"
24#include "model/squad_numbers.h"
25#include "model/staff.h"
26#include "model/team.h"
27#include "model/training.h"
28
29struct TransferListing;
30
32
43{
44 public:
49
50 GameData(const GameData&) = delete;
51 GameData& operator=(const GameData&) = delete;
52
53 // ---------------- DB ----------------
59 bool loadFromDB(std::shared_ptr<DatabaseConnection> database_ptr);
60
65 bool saveToDB() const;
66
67 // ---------------- StatsConfig ----------------
68 const StatsConfig& getStatsConfig() const;
69
70 // ---------------- League ----------------
71 void addLeague(LeagueID id, const League& league);
72
73 std::optional<std::reference_wrapper<const League>> getLeague(
74 LeagueID id) const;
75
76 const std::unordered_map<LeagueID, League>& getLeagues() const;
77
78 std::unordered_map<LeagueID, League>& getLeagues();
79
80 const std::vector<std::reference_wrapper<const League>>& getLeaguesVector()
81 const;
82
83 // ---------------- Team ----------------
84 void addTeam(TeamID id, const Team& team);
85
86 std::optional<std::reference_wrapper<Team>> getTeam(TeamID id);
87
88 std::optional<std::reference_wrapper<const Team>> getTeam(TeamID id) const;
89
90 const std::unordered_map<TeamID, Team>& getTeams() const;
91
92 std::unordered_map<TeamID, Team>& getTeams();
93
94 const std::vector<std::reference_wrapper<const Team>>& getTeamsVector() const;
95
96 void ageAllPlayers();
97
103 std::vector<PlayerID> advanceContractsAndReleasePlayers(
104 TeamID keep_selection = FREE_AGENTS_TEAM_ID);
105
106 void addPlayer(PlayerID id, const Player& player);
107 std::optional<std::reference_wrapper<const Player>> getPlayer(
108 PlayerID id) const;
109 const std::unordered_map<PlayerID, Player>& getPlayers() const;
110 std::unordered_map<PlayerID, Player>& getPlayers();
111 const std::vector<std::reference_wrapper<const Player>>& getPlayersVector()
112 const;
115 const std::vector<std::reference_wrapper<const Player>>& getPlayersForTeam(
116 TeamID team_id) const;
119 const std::vector<std::reference_wrapper<const Player>>& getAcademyForTeam(
120 TeamID team_id) const;
125 void setAcademyMember(PlayerID id, bool academy);
126 bool removePlayer(PlayerID id);
130 void transferPlayer(PlayerID id, TeamID new_team_id);
131
139 void assignSquadNumbers(TeamID team_id, PlayerID newcomer = 0);
140
141 // ---------------- World state ----------------
149 void setWorldSeed(std::uint64_t seed);
150
152 std::uint64_t getWorldSeed() const;
153
156
159
161 const std::vector<PlayerID>& getRemovedPlayerIds() const;
162
165
166 // ---------------- Staff & training ----------------
169 const StaffRoster& getStaff() const { return staff; }
170
173 const TrainingRegistry& getTraining() const { return training; }
174
175 // ---------------- Transfer Market ----------------
179 void saveTransferListing(const TransferListing& listing) const;
180
184 void deleteTransferListing(PlayerID player_id) const;
185
189 std::unordered_map<PlayerID, TransferListing> loadAllTransferListings() const;
190
191 private:
192 std::unordered_map<LeagueID, League> _leagues;
193 std::vector<std::reference_wrapper<const League>> _leaguesVec;
194 std::unordered_map<TeamID, Team> _teams;
195 std::vector<std::reference_wrapper<const Team>> _teamsVec;
196 std::unordered_map<PlayerID, Player> _players;
197 std::vector<std::reference_wrapper<const Player>> _playersVec;
198 std::unordered_map<TeamID, std::vector<std::reference_wrapper<const Player>>>
200 std::unordered_map<TeamID, std::vector<std::reference_wrapper<const Player>>>
203 std::shared_ptr<DatabaseConnection> db_conn;
204 std::uint64_t world_seed = 0;
205 bool world_seed_set = false;
207 std::vector<PlayerID> removed_player_ids;
210
211 void loadStatsConfig();
213 void loadExistingData();
214 void migrateSchema() const;
215 void restoreWorldState();
217};
Manages the SQLite database connection and transaction lifecycle.
Definition database_connection.h:24
Singleton class that acts as an in-memory cache for the database state.
Definition gamedata.h:43
void loadStatsConfig()
Definition gamedata.cpp:880
void generateAndSaveInitialData()
Definition gamedata.cpp:184
StaffRoster & getStaff()
Coaches, medics and scouts of every club plus the staff market.
Definition gamedata.h:168
StatsConfig stats_config
Definition gamedata.h:202
const std::unordered_map< LeagueID, League > & getLeagues() const
Definition gamedata.cpp:484
bool world_seed_set
Definition gamedata.h:205
bool loadFromDB(std::shared_ptr< DatabaseConnection > database_ptr)
Loads the initial game state from the database.
Definition gamedata.cpp:116
std::uint64_t getWorldSeed() const
Seed of every random stream of the world simulation.
Definition gamedata.cpp:786
std::unordered_map< LeagueID, League > _leagues
Definition gamedata.h:192
std::vector< PlayerID > advanceContractsAndReleasePlayers(TeamID keep_selection=FREE_AGENTS_TEAM_ID)
Definition gamedata.cpp:577
std::vector< PlayerID > removed_player_ids
Definition gamedata.h:207
PlayerID next_player_id
Definition gamedata.h:206
void addLeague(LeagueID id, const League &league)
Definition gamedata.cpp:471
std::shared_ptr< DatabaseConnection > db_conn
Definition gamedata.h:203
void migrateSchema() const
Definition gamedata.cpp:299
const std::vector< std::reference_wrapper< const League > > & getLeaguesVector() const
Definition gamedata.cpp:495
const std::vector< std::reference_wrapper< const Player > > & getAcademyForTeam(TeamID team_id) const
Definition gamedata.cpp:628
GameData(const GameData &)=delete
std::unordered_map< TeamID, std::vector< std::reference_wrapper< const Player > > > _teamPlayers
Definition gamedata.h:199
void addPlayer(PlayerID id, const Player &player)
Definition gamedata.cpp:534
const std::vector< std::reference_wrapper< const Team > > & getTeamsVector() const
Definition gamedata.cpp:528
std::optional< std::reference_wrapper< const Player > > getPlayer(PlayerID id) const
Definition gamedata.cpp:547
GameData()
Constructs a new GameData instance.
void restoreStaffAndTraining()
Definition gamedata.cpp:167
std::unordered_map< PlayerID, TransferListing > loadAllTransferListings() const
Loads all transfer listings from the database into a map.
Definition gamedata.cpp:835
StaffRoster staff
Definition gamedata.h:208
void restoreWorldState()
Definition gamedata.cpp:306
const std::vector< PlayerID > & getRemovedPlayerIds() const
Players removed (retired) since the last save.
Definition gamedata.cpp:792
bool saveToDB() const
Saves the current in-memory state back to the database.
Definition gamedata.cpp:448
const TrainingRegistry & getTraining() const
Definition gamedata.h:173
bool removePlayer(PlayerID id)
Definition gamedata.cpp:656
TrainingRegistry & getTraining()
Training plans, familiarity and player workloads.
Definition gamedata.h:172
GameData & operator=(const GameData &)=delete
void clearRemovedPlayerIds()
Forgets removed players once their deletion is persisted.
Definition gamedata.cpp:797
void setAcademyMember(PlayerID id, bool academy)
Definition gamedata.cpp:636
PlayerID allocatePlayerId()
Returns a new, never reused player id.
Definition gamedata.cpp:788
void transferPlayer(PlayerID id, TeamID new_team_id)
Definition gamedata.cpp:680
void addTeam(TeamID id, const Team &team)
Definition gamedata.cpp:501
const std::unordered_map< TeamID, Team > & getTeams() const
Definition gamedata.cpp:520
void ageAllPlayers()
Definition gamedata.cpp:569
std::unordered_map< PlayerID, Player > _players
Definition gamedata.h:196
std::optional< std::reference_wrapper< Team > > getTeam(TeamID id)
Definition gamedata.cpp:507
std::vector< std::reference_wrapper< const Team > > _teamsVec
Definition gamedata.h:195
const std::unordered_map< PlayerID, Player > & getPlayers() const
Definition gamedata.cpp:554
const std::vector< std::reference_wrapper< const Player > > & getPlayersVector() const
Definition gamedata.cpp:564
const StatsConfig & getStatsConfig() const
Definition gamedata.cpp:891
std::unordered_map< TeamID, std::vector< std::reference_wrapper< const Player > > > _teamAcademies
Definition gamedata.h:201
void loadExistingData()
Definition gamedata.cpp:365
void deleteTransferListing(PlayerID player_id) const
Deletes a transfer listing from the database.
Definition gamedata.cpp:823
void setWorldSeed(std::uint64_t seed)
Seeds the generation of a new world (call before loadFromDB).
Definition gamedata.cpp:780
SquadNumbers::Change setSquadNumber(PlayerID id, int number)
Definition gamedata.cpp:752
void saveTransferListing(const TransferListing &listing) const
Saves a transfer listing to the database (UPSERT).
Definition gamedata.cpp:800
void assignSquadNumbers(TeamID team_id, PlayerID newcomer=0)
Definition gamedata.cpp:711
std::vector< std::reference_wrapper< const League > > _leaguesVec
Definition gamedata.h:193
std::optional< std::reference_wrapper< const League > > getLeague(LeagueID id) const
Definition gamedata.cpp:477
PlayerID peekNextPlayerId() const
The id the next allocatePlayerId() call will return.
Definition gamedata.cpp:790
std::unordered_map< TeamID, Team > _teams
Definition gamedata.h:194
std::vector< std::reference_wrapper< const Player > > _playersVec
Definition gamedata.h:197
std::uint64_t world_seed
Definition gamedata.h:204
TrainingRegistry training
Definition gamedata.h:209
const StaffRoster & getStaff() const
Definition gamedata.h:169
const std::vector< std::reference_wrapper< const Player > > & getPlayersForTeam(TeamID team_id) const
Definition gamedata.cpp:617
Represents a football league containing teams and a leaderboard.
Definition league.h:41
Persistent career identity, attributes, traits and day-to-day state.
Definition player.h:104
Every staff member of the world with per-club effect caches. Concurrent effects queries return indepe...
Definition staff.h:138
Represents a football team.
Definition team.h:47
Training plans of every club and training state of every player.
Definition training.h:242
constexpr uint8_t FREE_AGENTS_TEAM_ID
Team ID reserved for free agents.
Definition global.h:22
Change
Definition squad_numbers.h:42
Contains structures to simplify JSON parsing and loading of player statistics.
Configuration mapping roles to their focus areas and listing all possible stats.
Definition stats_config.h:42
Represents a player listed on the transfer market.
Definition transfer_listing.h:25
Weekly training microcycle, workload, individual focus and tactical familiarity.
uint8_t LeagueID
Type alias for League identifiers.
Definition types.h:15
uint16_t TeamID
Type alias for Team identifiers.
Definition types.h:18
uint32_t PlayerID
Type alias for Player identifiers.
Definition types.h:21