Player12
An open source football management simulator
Loading...
Searching...
No Matches
data_hub.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 <array>
12#include <cstdint>
13#include <optional>
14#include <span>
15#include <string>
16#include <utility>
17#include <vector>
18
19#include "global/types.h"
20#include "model/gamedate.h"
23#include "model/match_report.h"
24
25class MatchEngine;
26
29{
31 std::uint8_t minutes = 0;
32 std::uint16_t passes_attempted = 0;
33 std::uint16_t passes_completed = 0;
34 std::uint16_t key_passes = 0;
35 std::uint16_t shots = 0;
36 float xg = 0.0f;
37 std::uint16_t tackles_won = 0;
38 std::uint16_t interceptions = 0;
39};
40
48{
52 bool managed_home = true;
53 std::array<std::uint16_t, 2> set_piece_shots{};
54 std::array<std::uint16_t, 2> set_piece_goals{};
55 std::array<std::uint16_t, 2> headed_shots{};
56 std::array<std::uint16_t, 2> corners{};
57 std::array<std::uint16_t, 2> progressive_passes{};
58 std::array<std::uint16_t, 2> passes_completed{};
59 std::vector<ShotRecord> shots;
60 std::vector<PlayerMatchSnapshot> players;
64
66 std::string toJson() const;
68 static std::optional<ManagedMatchSnapshot> fromJson(const std::string& json);
69};
70
73 const GameDateValue& date, TeamID home_id,
74 TeamID away_id, bool managed_home);
75
78{
81 bool home = true;
82 int goals_for = 0;
84 float xg_for = 0.0f;
85 float xg_against = 0.0f;
86 int shots_for = 0;
88};
89
91enum class HubMetric : std::uint8_t
92{
93 GoalsFor = 0,
95 XgFor,
101 Finishing,
107 COUNT
108};
109
110inline constexpr std::size_t HUB_METRIC_COUNT =
111 static_cast<std::size_t>(std::to_underlying(HubMetric::COUNT));
112
115{
116 float team = 0.0f;
117 float league = 0.0f;
118 int rank = 0;
120 bool lower_is_better = false;
121};
122
125{
126 int matches = 0;
127 int shots_for = 0;
128 int goals_for = 0;
132 int corners_for = 0;
133};
134
137{
139 std::vector<TeamTrendPoint> trend;
141 std::vector<float> rolling_xg_for;
142 std::vector<float> rolling_xg_against;
143 std::array<MetricComparison, HUB_METRIC_COUNT> metrics{};
149 std::vector<ShotRecord> shots_for;
150 std::vector<ShotRecord> shots_against;
154 std::vector<float> cumulative_finishing;
155 std::vector<float> cumulative_prevention;
157 float league_conversion = 0.0f;
158
159 bool hasEnoughMatches() const;
160};
161
164{
166 int appearances = 0;
167 int minutes = 0;
168 int goals = 0;
169 int assists = 0;
171 float average_rating = 0.0f;
173 std::vector<float> ratings;
175 float rating_trend = 0.0f;
176 // Tracked matches only (snapshots).
178 int shots = 0;
179 float xg = 0.0f;
182 int key_passes = 0;
184 float pass_share = 0.0f;
185 // Tracked matches with detail only (touch maps and passing network).
187 float xa = 0.0f;
189 int pressures = 0;
190 int touches = 0;
191
193 static float per90(float value, int minutes);
194};
195
198{
201 std::span<const MatchReport> team_reports;
203 std::span<const MatchReport> league_reports;
205 std::span<const ManagedMatchSnapshot> snapshots;
206};
207
208namespace DataHub
209{
211inline constexpr int MIN_MATCHES = 3;
212inline constexpr std::size_t ROLLING_WINDOW = 5;
213inline constexpr std::size_t RATING_HISTORY = 10;
214
217std::vector<PlayerAnalyticsRow> buildPlayerAnalytics(const DataHubInput& input);
218
220bool hasStatistics(const MatchReport& report);
221} // namespace DataHub
222
224enum class LeaderMetric : std::uint8_t
225{
226 ExpectedGoals = 0,
229 Pressures,
230 COUNT
231};
232
233inline constexpr std::size_t LEADER_METRIC_COUNT =
234 static_cast<std::size_t>(std::to_underlying(LeaderMetric::COUNT));
235
236namespace DataHub
237{
238inline constexpr std::size_t LEADERS = 5;
240inline constexpr int LEADER_MIN_MINUTES = 90;
242float leaderValue(const PlayerAnalyticsRow& row, LeaderMetric metric);
244int leaderMinutes(const PlayerAnalyticsRow& row, LeaderMetric metric);
246std::vector<std::size_t> leaders(std::span<const PlayerAnalyticsRow> rows,
247 LeaderMetric metric);
248} // namespace DataHub
249
250const char* leaderMetricKey(LeaderMetric metric);
251
253const char* hubMetricKey(HubMetric metric);
254const char* hubMetricHelpKey(HubMetric metric);
Definition match_engine.h:517
const char * hubMetricHelpKey(HubMetric metric)
Definition data_hub.cpp:601
HubMetric
Metrics compared with the league (per team and match).
Definition data_hub.h:92
constexpr std::size_t HUB_METRIC_COUNT
Definition data_hub.h:110
const char * leaderMetricKey(LeaderMetric metric)
Definition data_hub.cpp:589
constexpr std::size_t LEADER_METRIC_COUNT
Definition data_hub.h:233
ManagedMatchSnapshot captureSnapshot(const MatchEngine &engine, const GameDateValue &date, TeamID home_id, TeamID away_id, bool managed_home)
Definition data_hub.cpp:254
LeaderMetric
Player leaderboards of the data hub.
Definition data_hub.h:225
const char * hubMetricKey(HubMetric metric)
Definition data_hub.cpp:595
nlohmann::json json
Definition datagenerator.cpp:38
Definition data_hub.h:209
int leaderMinutes(const PlayerAnalyticsRow &row, LeaderMetric metric)
Definition data_hub.cpp:569
std::vector< std::size_t > leaders(std::span< const PlayerAnalyticsRow > rows, LeaderMetric metric)
Definition data_hub.cpp:575
std::vector< PlayerAnalyticsRow > buildPlayerAnalytics(const DataHubInput &input)
Definition data_hub.cpp:447
bool hasStatistics(const MatchReport &report)
Definition data_hub.cpp:321
constexpr std::size_t RATING_HISTORY
Definition data_hub.h:213
float leaderValue(const PlayerAnalyticsRow &row, LeaderMetric metric)
Definition data_hub.cpp:550
TeamAnalytics buildTeamAnalytics(const DataHubInput &input)
Definition data_hub.cpp:329
constexpr std::size_t LEADERS
Definition data_hub.h:238
constexpr int MIN_MATCHES
Definition data_hub.h:211
constexpr std::size_t ROLLING_WINDOW
Definition data_hub.h:212
constexpr int LEADER_MIN_MINUTES
Definition data_hub.h:240
Everything the data hub aggregates.
Definition data_hub.h:198
std::span< const MatchReport > league_reports
Definition data_hub.h:203
TeamID team_id
Definition data_hub.h:199
std::span< const MatchReport > team_reports
Definition data_hub.h:201
std::span< const ManagedMatchSnapshot > snapshots
Definition data_hub.h:205
Represents a specific date in the game world.
Definition gamedate.h:20
Detail of a managed match that the persisted report does not keep: shot positions,...
Definition data_hub.h:48
TeamID home_id
Definition data_hub.h:50
bool managed_home
Definition data_hub.h:52
MatchDetail detail
Definition data_hub.h:63
TeamID away_id
Definition data_hub.h:51
static std::optional< ManagedMatchSnapshot > fromJson(const std::string &json)
Definition data_hub.cpp:209
std::vector< PlayerMatchSnapshot > players
Definition data_hub.h:60
std::array< std::uint16_t, 2 > corners
Definition data_hub.h:56
std::string toJson() const
Definition data_hub.cpp:182
std::vector< ShotRecord > shots
Definition data_hub.h:59
std::array< std::uint16_t, 2 > progressive_passes
Definition data_hub.h:57
std::array< std::uint16_t, 2 > headed_shots
Definition data_hub.h:55
GameDateValue date
Definition data_hub.h:49
std::array< std::uint16_t, 2 > passes_completed
Definition data_hub.h:58
std::array< std::uint16_t, 2 > set_piece_shots
Definition data_hub.h:53
std::array< std::uint16_t, 2 > set_piece_goals
Definition data_hub.h:54
Per-match aggregates behind the match report's analysis: touch maps, the pass network,...
Definition match_insights.h:66
Structured summary of a played fixture, persisted per fixture.
Definition match_report.h:93
The club's value against the league average and its rank.
Definition data_hub.h:115
bool lower_is_better
Definition data_hub.h:120
float league
Definition data_hub.h:117
int rank
Definition data_hub.h:118
int ranked_teams
Definition data_hub.h:119
float team
Definition data_hub.h:116
One player of the analysed club.
Definition data_hub.h:164
int assists
Definition data_hub.h:169
int detail_minutes
Definition data_hub.h:186
int key_passes
Definition data_hub.h:182
int pressures
Definition data_hub.h:189
int passes_attempted
Definition data_hub.h:180
float rating_trend
Definition data_hub.h:175
float xa
Definition data_hub.h:187
float xg
Definition data_hub.h:179
int minutes
Definition data_hub.h:167
int shots
Definition data_hub.h:178
int rated_matches
Definition data_hub.h:170
float average_rating
Definition data_hub.h:171
int appearances
Definition data_hub.h:166
int goals
Definition data_hub.h:168
int passes_completed
Definition data_hub.h:181
float pass_share
Definition data_hub.h:184
std::vector< float > ratings
Definition data_hub.h:173
int touches
Definition data_hub.h:190
int progressive_passes
Definition data_hub.h:188
int tracked_minutes
Definition data_hub.h:177
static float per90(float value, int minutes)
Definition data_hub.cpp:316
PlayerID player
Definition data_hub.h:165
Pass and shot involvement of a managed player in one match.
Definition data_hub.h:29
std::uint8_t minutes
Definition data_hub.h:31
float xg
Definition data_hub.h:36
std::uint16_t key_passes
Definition data_hub.h:34
std::uint16_t passes_attempted
Definition data_hub.h:32
PlayerID player
Definition data_hub.h:30
std::uint16_t shots
Definition data_hub.h:35
std::uint16_t tackles_won
Definition data_hub.h:37
std::uint16_t passes_completed
Definition data_hub.h:33
std::uint16_t interceptions
Definition data_hub.h:38
Set-piece shots and goals in tracked (snapshot) matches.
Definition data_hub.h:125
int all_shots_for
Definition data_hub.h:131
int shots_for
Definition data_hub.h:127
int corners_for
Definition data_hub.h:132
int shots_against
Definition data_hub.h:129
int goals_for
Definition data_hub.h:128
int goals_against
Definition data_hub.h:130
int matches
Definition data_hub.h:126
Team analytics of the data hub.
Definition data_hub.h:137
std::vector< ShotRecord > shots_against
Definition data_hub.h:150
float league_conversion
Definition data_hub.h:157
int team_league_matches
Definition data_hub.h:146
bool hasEnoughMatches() const
Definition data_hub.cpp:311
std::array< MetricComparison, HUB_METRIC_COUNT > metrics
Definition data_hub.h:143
int tracked_matches
Definition data_hub.h:151
std::vector< float > rolling_xg_against
Definition data_hub.h:142
std::vector< ShotRecord > shots_for
Definition data_hub.h:149
std::vector< float > cumulative_finishing
Definition data_hub.h:154
std::vector< float > cumulative_prevention
Definition data_hub.h:155
std::vector< TeamTrendPoint > trend
Definition data_hub.h:139
std::vector< float > rolling_xg_for
Definition data_hub.h:141
int league_matches
Definition data_hub.h:144
SetPieceSummary set_pieces
Definition data_hub.h:147
One played match of the analysed club.
Definition data_hub.h:78
int goals_against
Definition data_hub.h:83
GameDateValue date
Definition data_hub.h:79
int shots_for
Definition data_hub.h:86
int goals_for
Definition data_hub.h:82
int shots_against
Definition data_hub.h:87
float xg_against
Definition data_hub.h:85
float xg_for
Definition data_hub.h:84
TeamID opponent
Definition data_hub.h:80
bool home
Definition data_hub.h:81
uint16_t TeamID
Type alias for Team identifiers.
Definition types.h:18
uint32_t PlayerID
Type alias for Player identifiers.
Definition types.h:21