A club analysis in the statistics: the season as charts; larger trophies
The engine counts the manager's club's running season from the almanac (read "analysis"): matches, the points course beside the league's pace, records by venue, what the first goal decided, goals by quarter of an hour, scorers and goal kinds. The statistics open with it; the archive tables keep their place with larger type and trophies. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
9006b7a05d
commit
8f607a9d89
13 files changed
+444
-9
No files matched your search
@@ -1165,6 +1165,107 @@ cli::Json recent_results(const ModernState& g, const std::string& club_id, size_
|
||||
return rows;
|
||||
}
|
||||
|
||||
cli::Json club_analysis(const ModernState& g, const std::string& club_id) {
|
||||
const auto seasons = seasons_of(g);
|
||||
A history;
|
||||
for (const auto& s : seasons)
|
||||
for (const auto& table : s.tables)
|
||||
for (const auto& row : table.rows)
|
||||
if (row.club < s.clubs.size() && s.clubs[row.club].id == club_id) {
|
||||
std::string league = "Liga";
|
||||
for (const auto& l : s.leagues) if (l.index == int(table.league)) league = l.name;
|
||||
history.push_back(O{{"year", s.start_year}, {"rank", int(row.rank)}, {"points", int(row.points)},
|
||||
{"goalsFor", int(row.goals_for)}, {"goalsAgainst", int(row.goals_against)}, {"league", league}});
|
||||
}
|
||||
if (seasons.empty() || seasons.back().season != g.almanac.current.season || g.almanac.current.season < 0)
|
||||
return O{{"available", false}, {"history", std::move(history)}};
|
||||
const auto& s = seasons.back();
|
||||
struct Record { int played = 0, won = 0, drawn = 0, lost = 0, goals_for = 0, goals_against = 0; };
|
||||
const auto record = [](const Record& r) {
|
||||
return O{{"played", r.played}, {"won", r.won}, {"drawn", r.drawn}, {"lost", r.lost}, {"goalsFor", r.goals_for},
|
||||
{"goalsAgainst", r.goals_against}, {"points", r.won * 3 + r.drawn}};
|
||||
};
|
||||
Record home_record, away_record, first_for, first_against;
|
||||
int buckets[6][2] = {}, kinds[4][2] = {}, chances[2] = {}, goals[2] = {}, yellow[2] = {}, red[2] = {}, league = -1;
|
||||
std::map<uint16_t, std::pair<int, int>> scorers; // goals, assists
|
||||
A matches, points;
|
||||
int total = 0;
|
||||
for (const auto& m : s.matches) {
|
||||
if (m.home >= s.clubs.size() || m.away >= s.clubs.size()) continue;
|
||||
const bool home = s.clubs[m.home].id == club_id;
|
||||
if (!home && s.clubs[m.away].id != club_id) continue;
|
||||
const int own_side = home ? 0 : 1;
|
||||
const int own = home ? m.home_goals : m.away_goals, other = home ? m.away_goals : m.home_goals;
|
||||
auto& side_record = home ? home_record : away_record;
|
||||
++side_record.played; side_record.goals_for += own; side_record.goals_against += other;
|
||||
(own > other ? side_record.won : own < other ? side_record.lost : side_record.drawn)++;
|
||||
int first = -1; // who scored the first goal: 0 own, 1 opponent
|
||||
for (const auto& e : m.events) {
|
||||
const int mine = e.side == own_side ? 0 : 1;
|
||||
if (e.kind == AlmanacEventKind::Goal || e.kind == AlmanacEventKind::Miss) ++chances[mine];
|
||||
if (e.kind == AlmanacEventKind::Goal) {
|
||||
++goals[mine];
|
||||
++buckets[std::clamp((int(e.minute) - 1) / 15, 0, 5)][mine];
|
||||
++kinds[std::min<int>(e.detail, 3)][mine];
|
||||
if (first < 0) first = mine;
|
||||
if (!mine) {
|
||||
if (e.player != kAlmanacNone) ++scorers[e.player].first;
|
||||
if (e.other != kAlmanacNone) ++scorers[e.other].second;
|
||||
}
|
||||
} else if (e.kind == AlmanacEventKind::YellowCard) ++yellow[mine];
|
||||
else if (e.kind == AlmanacEventKind::RedCard || e.kind == AlmanacEventKind::SecondYellow) ++red[mine];
|
||||
}
|
||||
if (first >= 0) {
|
||||
auto& r = first ? first_against : first_for;
|
||||
++r.played; (own > other ? r.won : own < other ? r.lost : r.drawn)++;
|
||||
}
|
||||
if (m.type == 0) {
|
||||
league = m.league;
|
||||
total += own > other ? 3 : own == other ? 1 : 0;
|
||||
points.push_back(total);
|
||||
}
|
||||
matches.push_back(O{{"opponent", s.clubs[home ? m.away : m.home].name}, {"home", home}, {"league", m.type == 0},
|
||||
{"round", int(m.round)}, {"goalsFor", own}, {"goalsAgainst", other}, {"attendance", int64_t(m.attendance)}});
|
||||
}
|
||||
// the league beside it: goals and chances per team and match, points per team and match
|
||||
int64_t league_matches = 0, league_goals = 0, league_chances = 0, league_points = 0;
|
||||
if (league >= 0)
|
||||
for (const auto& m : s.matches) {
|
||||
if (m.type != 0 || m.league != league) continue;
|
||||
++league_matches;
|
||||
league_points += m.home_goals == m.away_goals ? 2 : 3;
|
||||
for (const auto& e : m.events) {
|
||||
if (e.kind == AlmanacEventKind::Goal || e.kind == AlmanacEventKind::Miss) ++league_chances;
|
||||
if (e.kind == AlmanacEventKind::Goal) ++league_goals;
|
||||
}
|
||||
}
|
||||
std::vector<std::pair<uint16_t, std::pair<int, int>>> ranked(scorers.begin(), scorers.end());
|
||||
std::stable_sort(ranked.begin(), ranked.end(), [](const auto& a, const auto& b) {
|
||||
return a.second.first * 2 + a.second.second > b.second.first * 2 + b.second.second;
|
||||
});
|
||||
A scorer_rows;
|
||||
for (size_t i = 0; i < ranked.size() && i < 8; ++i) {
|
||||
const uint16_t p = ranked[i].first;
|
||||
if (p >= s.players.size()) continue;
|
||||
const auto& player = s.players[p];
|
||||
scorer_rows.push_back(O{{"id", player.id}, {"name", player.first.empty() ? player.last : player.first + " " + player.last},
|
||||
{"goals", ranked[i].second.first}, {"assists", ranked[i].second.second}});
|
||||
}
|
||||
A bucket_rows, kind_rows;
|
||||
for (int b = 0; b < 6; ++b) bucket_rows.push_back(O{{"from", b * 15 + 1}, {"for", buckets[b][0]}, {"against", buckets[b][1]}});
|
||||
for (int k = 0; k < 4; ++k) kind_rows.push_back(O{{"kind", k}, {"for", kinds[k][0]}, {"against", kinds[k][1]}});
|
||||
std::string league_name;
|
||||
for (const auto& l : s.leagues) if (l.index == league) league_name = l.name;
|
||||
return O{{"available", home_record.played + away_record.played > 0}, {"year", s.start_year}, {"leagueName", league_name},
|
||||
{"matches", std::move(matches)}, {"points", std::move(points)}, {"buckets", std::move(bucket_rows)},
|
||||
{"kinds", std::move(kind_rows)}, {"home", record(home_record)}, {"away", record(away_record)},
|
||||
{"firstFor", record(first_for)}, {"firstAgainst", record(first_against)},
|
||||
{"chances", O{{"for", chances[0]}, {"against", chances[1]}}}, {"goals", O{{"for", goals[0]}, {"against", goals[1]}}},
|
||||
{"cards", O{{"yellow", yellow[0]}, {"red", red[0]}, {"yellowAgainst", yellow[1]}, {"redAgainst", red[1]}}},
|
||||
{"league", O{{"matches", league_matches}, {"goals", league_goals}, {"chances", league_chances}, {"points", league_points}}},
|
||||
{"scorers", std::move(scorer_rows)}, {"history", std::move(history)}};
|
||||
}
|
||||
|
||||
cli::Json team_of_the_day(const ModernState& g, uint32_t from_date, uint32_t until_date) {
|
||||
std::map<std::string, int> runtime; // world player id -> runtime index
|
||||
for (size_t p = 0; p < g.world_player_id.size(); ++p)
|
||||
|
||||
Reference in new issue
Block a user