Modern UI: the statistics screen reads the almanac
ModernSession::read_statistics_screen answers the statistics screen's filters with real data: eternal table, national tournaments (winners, scorers, history), series and records, winners by season and the own club per five minutes. Views the engine does not record yet (title chances, transfers, weekly curves, season finances) answer available false, reason not_recorded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e82ce1649f
commit
015fa65f56
6 files changed
+378
-1
No files matched your search
@@ -2,6 +2,7 @@
|
||||
#include "modern/content/statistics.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <set>
|
||||
@@ -11,6 +12,8 @@
|
||||
|
||||
#include "formats/executable_layout.hpp"
|
||||
#include "game/messages.hpp"
|
||||
#include "game/schedule.hpp"
|
||||
#include "modern/content/world_package.hpp"
|
||||
#include "modern/game/almanac.hpp"
|
||||
|
||||
namespace bmh::modern {
|
||||
@@ -398,4 +401,350 @@ cli::Json query_almanac(const ModernState& g, const cli::Json& request) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --- the statistics screen of the Modern UI (DataSource::read_statistics; the demo's filters) ---
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kSections[9] = {"Möglichkeiten", "Transferhistorie", "Analytics", "Ewige Tabelle", "WM-Statistiken",
|
||||
"Saisonfinanzen", "Serien u. Rekorde", "Sieger", "Minuten-Statistik"};
|
||||
constexpr const char* kWorldViews[6] = {"Sieger", "Gruppen", "Torschützen", "Finalrunde", "Tabelle", "Historie"};
|
||||
// The first league of the countries with leagues, in the screen's order (de, es, it, fr, en);
|
||||
// kEternal maps the eternal table's values (de, es, en, fr, it) to it.
|
||||
struct Country { const char* code; const char* name; int league; };
|
||||
constexpr Country kCountries[5] = {{"de", "Deutschland", 0}, {"es", "Spanien", 5}, {"it", "Italien", 8},
|
||||
{"fr", "Frankreich", 7}, {"en", "England", 6}};
|
||||
constexpr int kEternal[5] = {0, 1, 4, 3, 2};
|
||||
|
||||
J cell(const std::string& text) { return O{{"text", text}}; }
|
||||
J numeric(int64_t v) { return O{{"text", std::to_string(v)}, {"value", v}}; }
|
||||
J club_cell(const AlmanacClub& c) { return O{{"text", c.name}, {"club", c.id}}; }
|
||||
J nation_cell(const AlmanacClub& c) { return O{{"text", c.name}, {"nation", c.id}}; }
|
||||
|
||||
// The world's nation of the league's clubs; without a world "nation:<code>".
|
||||
std::string country_nation(const Country& c) {
|
||||
if (const World* w = current_world())
|
||||
for (const auto& l : w->leagues)
|
||||
if (l.index == c.league && !l.members.empty())
|
||||
if (const WorldClub* club = w->club(l.members.front()); club && !club->nation.empty()) return club->nation;
|
||||
return std::string("nation:") + c.code;
|
||||
}
|
||||
|
||||
void headings(O& out, std::initializer_list<const char*> names) {
|
||||
A& cols = std::get<A>(out["columns"].value);
|
||||
for (const char* n : names) cols.push_back(cell(n));
|
||||
}
|
||||
void row(O& out, A cells) { std::get<A>(out["rows"].value).push_back(O{{"cells", std::move(cells)}}); }
|
||||
|
||||
// The ids of a league's clubs as the running game has them.
|
||||
std::set<std::string> league_club_ids(const ModernState& g, int league) {
|
||||
std::set<std::string> ids;
|
||||
if (league < 0) return ids;
|
||||
const int first = league_first_team(league);
|
||||
for (int t = first; t < first + int(kLeagueSizes[league]); ++t) {
|
||||
const std::string id = t < int(g.world_club_id.size()) ? g.world_club_id[size_t(t)] : std::string{};
|
||||
ids.insert(id.empty() ? "team:" + std::to_string(t) : id);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
void eternal_table(O& out, const std::vector<AlmanacSeason>& seasons, int league) {
|
||||
headings(out, {"Verein", "Sp.", "S", "U", "N", "Punkte", "Tore"});
|
||||
struct Sum { AlmanacClub club; int played = 0, won = 0, drawn = 0, lost = 0, points = 0, gf = 0, ga = 0; };
|
||||
std::map<std::string, Sum> sums;
|
||||
for (const auto& s : seasons)
|
||||
for (const auto& t : s.tables) {
|
||||
if (t.league != league) continue;
|
||||
for (const auto& r : t.rows) {
|
||||
Sum& x = sums[s.clubs[r.club].id];
|
||||
x.club = s.clubs[r.club];
|
||||
x.played += r.played, x.won += r.won, x.drawn += r.drawn, x.lost += r.lost, x.points += r.points;
|
||||
x.gf += r.goals_for, x.ga += r.goals_against;
|
||||
}
|
||||
}
|
||||
std::vector<Sum> list;
|
||||
for (auto& [id, x] : sums) list.push_back(x);
|
||||
std::stable_sort(list.begin(), list.end(), [](const Sum& a, const Sum& b) {
|
||||
return a.points != b.points ? a.points > b.points : a.gf - a.ga > b.gf - b.ga;
|
||||
});
|
||||
for (const auto& x : list)
|
||||
row(out, A{club_cell(x.club), numeric(x.played), numeric(x.won), numeric(x.drawn), numeric(x.lost), numeric(x.points),
|
||||
cell(std::to_string(x.gf) + ":" + std::to_string(x.ga))});
|
||||
}
|
||||
|
||||
// The national tournaments: cup 6 is the final round (a final), cup 7 the qualification.
|
||||
bool national_match(const AlmanacMatch& m) { return m.type == 7 || m.type == 8; }
|
||||
|
||||
bool world_tournaments(O& out, const ModernState& g, const std::vector<AlmanacSeason>& seasons, int view) {
|
||||
if (view == 0) {
|
||||
headings(out, {"WM / EM", "Sieger", "Finalist", "Finale"});
|
||||
for (auto s = seasons.rbegin(); s != seasons.rend(); ++s)
|
||||
for (const auto& w : s->winners) {
|
||||
if (w.type != 7) continue;
|
||||
std::string final_score = "—";
|
||||
for (const auto& m : s->matches)
|
||||
if (m.type == 7 && ((m.home == w.winner && m.away == w.runner_up) || (m.away == w.winner && m.home == w.runner_up)))
|
||||
final_score = std::to_string(m.home_goals) + ":" + std::to_string(m.away_goals);
|
||||
const std::string when = std::to_string(s->start_year + 1);
|
||||
row(out, A{J(O{{"text", cup_name(g, 6) + " " + when}, {"season", when}, {"winner", s->clubs[w.winner].id}}),
|
||||
nation_cell(s->clubs[w.winner]), w.runner_up == 0xffff ? cell("—") : nation_cell(s->clubs[w.runner_up]),
|
||||
cell(final_score)});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (view == 2) {
|
||||
headings(out, {"Spieler", "Land", "Tore"});
|
||||
struct Scorer { int goals = 0; J player, nation; };
|
||||
std::map<std::string, Scorer> scorers;
|
||||
for (const auto& s : seasons)
|
||||
for (const auto& m : s.matches) {
|
||||
if (!national_match(m)) continue;
|
||||
for (const auto& e : m.events) {
|
||||
if (e.kind != AlmanacEventKind::Goal || e.player == kAlmanacNone || e.player >= s.players.size()) continue;
|
||||
const AlmanacPlayer& p = s.players[e.player];
|
||||
Scorer& x = scorers[p.id];
|
||||
++x.goals;
|
||||
x.player = O{{"text", player_name(p)}, {"player", p.id}};
|
||||
x.nation = nation_cell(s.clubs[e.side ? m.away : m.home]);
|
||||
}
|
||||
}
|
||||
std::vector<Scorer> list;
|
||||
for (auto& [id, x] : scorers) list.push_back(x);
|
||||
std::stable_sort(list.begin(), list.end(), [](const Scorer& a, const Scorer& b) { return a.goals > b.goals; });
|
||||
for (size_t i = 0; i < list.size() && i < 20; ++i) row(out, A{list[i].player, list[i].nation, numeric(list[i].goals)});
|
||||
return true;
|
||||
}
|
||||
if (view == 4 || view == 5) {
|
||||
headings(out, {"Land", "Spiele", "Siege", "Punkte"});
|
||||
struct Rec { AlmanacClub club; int games = 0, wins = 0, points = 0; };
|
||||
std::map<std::string, Rec> recs;
|
||||
for (const auto& s : seasons)
|
||||
for (const auto& m : s.matches) {
|
||||
if (!national_match(m)) continue;
|
||||
for (int side = 0; side < 2; ++side) {
|
||||
Rec& r = recs[s.clubs[side ? m.away : m.home].id];
|
||||
r.club = s.clubs[side ? m.away : m.home];
|
||||
const int own = side ? m.away_goals : m.home_goals, other = side ? m.home_goals : m.away_goals;
|
||||
++r.games;
|
||||
if (own > other) ++r.wins, r.points += 3;
|
||||
else if (own == other) r.points += 1;
|
||||
}
|
||||
}
|
||||
std::vector<Rec> list;
|
||||
for (auto& [id, r] : recs) list.push_back(r);
|
||||
std::stable_sort(list.begin(), list.end(), [](const Rec& a, const Rec& b) { return a.points > b.points; });
|
||||
for (const auto& r : list) row(out, A{nation_cell(r.club), numeric(r.games), numeric(r.wins), numeric(r.points)});
|
||||
return true;
|
||||
}
|
||||
return false; // groups, final round: not yet
|
||||
}
|
||||
|
||||
void series_and_records(O& out, const ModernState& g, const std::vector<AlmanacSeason>& seasons, int league) {
|
||||
headings(out, {"Serie / Rekord", "Verein", "Wert"});
|
||||
const auto ids = league_club_ids(g, league);
|
||||
struct Run { int unbeaten = 0, wins = 0, scoring = 0, best_unbeaten = 0, best_wins = 0, best_scoring = 0; AlmanacClub club; };
|
||||
std::map<std::string, Run> runs;
|
||||
struct Best { AlmanacClub club; std::array<int, 3> value{}; }; // home wins, away wins, goals in one season
|
||||
std::map<std::string, Best> season_best;
|
||||
int biggest = -1, crowd = -1;
|
||||
J biggest_club = cell("—"), crowd_club = cell("—");
|
||||
std::string big_score;
|
||||
for (const auto& s : seasons) {
|
||||
std::map<std::string, Best> this_season;
|
||||
for (const auto& m : s.matches) {
|
||||
if (m.type != 0 || m.league != league) continue;
|
||||
for (int side = 0; side < 2; ++side) {
|
||||
const AlmanacClub& c = s.clubs[side ? m.away : m.home];
|
||||
if (!ids.count(c.id)) continue;
|
||||
const int own = side ? m.away_goals : m.home_goals, other = side ? m.home_goals : m.away_goals;
|
||||
Run& r = runs[c.id];
|
||||
r.club = c;
|
||||
r.unbeaten = own >= other ? r.unbeaten + 1 : 0;
|
||||
r.wins = own > other ? r.wins + 1 : 0;
|
||||
r.scoring = own > 0 ? r.scoring + 1 : 0;
|
||||
r.best_unbeaten = std::max(r.best_unbeaten, r.unbeaten);
|
||||
r.best_wins = std::max(r.best_wins, r.wins);
|
||||
r.best_scoring = std::max(r.best_scoring, r.scoring);
|
||||
Best& x = this_season[c.id];
|
||||
x.club = c;
|
||||
if (own > other) ++x.value[size_t(side)];
|
||||
x.value[2] += own;
|
||||
if (own - other > biggest) biggest = own - other, biggest_club = club_cell(c), big_score = std::to_string(own) + " : " + std::to_string(other);
|
||||
if (side == 0 && int(m.attendance) > crowd) crowd = int(m.attendance), crowd_club = club_cell(c);
|
||||
}
|
||||
}
|
||||
for (auto& [id, x] : this_season) {
|
||||
Best& b = season_best[id];
|
||||
b.club = x.club;
|
||||
for (size_t k = 0; k < 3; ++k) b.value[k] = std::max(b.value[k], x.value[k]);
|
||||
}
|
||||
}
|
||||
const auto best_run = [&](int Run::*value, const char* name) {
|
||||
int best = 0;
|
||||
J who = cell("—");
|
||||
for (const auto& [id, r] : runs) if (r.*value > best) best = r.*value, who = club_cell(r.club);
|
||||
if (best > 0) row(out, A{cell(name), who, numeric(best)});
|
||||
};
|
||||
const auto best_season = [&](size_t k, const char* name) {
|
||||
int best = 0;
|
||||
J who = cell("—");
|
||||
for (const auto& [id, b] : season_best) if (b.value[k] > best) best = b.value[k], who = club_cell(b.club);
|
||||
if (best > 0) row(out, A{cell(name), who, numeric(best)});
|
||||
};
|
||||
best_run(&Run::best_unbeaten, "Ungeschlagen");
|
||||
best_run(&Run::best_wins, "Siege in Folge");
|
||||
best_season(0, "Heimsiege");
|
||||
best_season(1, "Auswärtssiege");
|
||||
best_season(2, "Tore in einer Saison");
|
||||
if (biggest > 0) row(out, A{cell("Höchster Sieg"), biggest_club, cell(big_score)});
|
||||
best_run(&Run::best_scoring, "Längste Torserie");
|
||||
if (crowd > 0) row(out, A{cell("Meiste Zuschauer"), crowd_club, numeric(crowd)});
|
||||
}
|
||||
|
||||
void winners_by_season(O& out, const std::vector<AlmanacSeason>& seasons, int category, int country) {
|
||||
struct Title { const char* text; const char* trophy; int type; int league; int detail; };
|
||||
std::vector<Title> titles;
|
||||
if (category == 0) {
|
||||
const Country& c = kCountries[country];
|
||||
titles.push_back({"Meisterschaft", c.league == 0 ? "championship" : "", 0, c.league, 0});
|
||||
if (c.league == 0) titles.push_back({"DFB-Pokal", "dfb-cup", 1, -1, 0});
|
||||
out["note"] = std::string(c.name) + " · Titelträger im Saisonvergleich";
|
||||
} else if (category == 1) {
|
||||
titles = {{"Champions League", "champions-league", 2, -1, 0}, {"Pokalsieger", "cup-winners", 3, -1, 0}, {"UEFA-Cup", "uefa-cup", 4, -1, 0}};
|
||||
out["note"] = "Europäische Titelträger im Saisonvergleich";
|
||||
} else {
|
||||
titles = {{"Sieger", "indoor-masters", 12, -1, 1}, {"Finalist", "", 12, -1, 2}};
|
||||
out["note"] = "Hallenmasters · Sieger und Finalisten";
|
||||
}
|
||||
headings(out, {"Saison"});
|
||||
for (const auto& t : titles) std::get<A>(out["columns"].value).push_back(O{{"text", t.text}, {"trophy", t.trophy}});
|
||||
for (auto s = seasons.rbegin(); s != seasons.rend(); ++s) {
|
||||
A cells{cell(season_label(s->start_year))};
|
||||
bool any = false;
|
||||
for (const auto& t : titles) {
|
||||
J value = cell("—");
|
||||
for (const auto& w : s->winners) {
|
||||
if (w.type != t.type || (t.type == 0 && w.league != t.league)) continue;
|
||||
if (t.detail >= 1 && w.detail != 1) continue;
|
||||
const uint16_t c = t.detail == 2 ? w.runner_up : w.winner;
|
||||
if (c != 0xffff) value = club_cell(s->clubs[c]), any = true;
|
||||
}
|
||||
cells.push_back(std::move(value));
|
||||
}
|
||||
if (any) row(out, std::move(cells));
|
||||
}
|
||||
}
|
||||
|
||||
void minute_buckets(O& out, const std::vector<AlmanacSeason>& seasons, const std::string& own) {
|
||||
headings(out, {"Ereignis"});
|
||||
for (int i = 0; i < 18; ++i) std::get<A>(out["columns"].value).push_back(cell(std::to_string(i * 5) + "."));
|
||||
std::array<std::array<int, 18>, 4> counts{};
|
||||
for (const auto& s : seasons)
|
||||
for (const auto& m : s.matches) {
|
||||
const int side = s.clubs[m.home].id == own ? 0 : s.clubs[m.away].id == own ? 1 : -1;
|
||||
if (side < 0) continue;
|
||||
for (const auto& e : m.events) {
|
||||
if (e.side != side || e.minute < 1 || e.minute > 90) continue;
|
||||
const size_t b = size_t(std::min(17, (e.minute - 1) / 5));
|
||||
if (e.kind == AlmanacEventKind::Goal) ++counts[0][b], ++counts[1][b];
|
||||
if (e.kind == AlmanacEventKind::Miss) ++counts[1][b];
|
||||
if (e.kind == AlmanacEventKind::YellowCard) ++counts[2][b];
|
||||
if (e.kind == AlmanacEventKind::RedCard || e.kind == AlmanacEventKind::SecondYellow) ++counts[3][b];
|
||||
}
|
||||
}
|
||||
const char* names[4] = {"Tore", "Chancen", "Gelb", "Rot"};
|
||||
for (size_t k = 0; k < 4; ++k) {
|
||||
A cells{cell(names[k])};
|
||||
for (size_t b = 0; b < 18; ++b) cells.push_back(numeric(counts[k][b]));
|
||||
row(out, std::move(cells));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
cli::Json query_statistics_screen(const ModernState& g, int manager, const cli::Json& request) {
|
||||
const auto reject = [](const char* e) { return J(O{{"ok", false}, {"fictional", false}, {"error", e}}); };
|
||||
const auto* o = request.object();
|
||||
if (!o) return reject("invalid_statistics_request");
|
||||
for (const auto& [key, value] : *o)
|
||||
if (key != "section" && key != "category" && key != "country" && key != "winner_country" && key != "absolute" && key != "curves")
|
||||
return reject("unknown_statistics_filter");
|
||||
const auto integer = [&](const char* key, int fallback, int maximum) -> int {
|
||||
const J* v = field(request, key);
|
||||
if (!v) return fallback;
|
||||
return v->integer() && *v->integer() >= 0 && *v->integer() <= maximum ? int(*v->integer()) : -1;
|
||||
};
|
||||
const int section = integer("section", 7, 8), category = integer("category", 0, 5), country = integer("country", 0, 5);
|
||||
if (section < 0 || category < 0 || country < 0 || (category != 0 && category != 1 && category != 5))
|
||||
return reject("invalid_statistics_filter");
|
||||
std::string winner_country = country_nation(kCountries[0]);
|
||||
if (const J* w = field(request, "winner_country")) {
|
||||
if (!w->string()) return reject("invalid_statistics_country");
|
||||
winner_country = *w->string();
|
||||
}
|
||||
int winner_index = -1;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
if (country_nation(kCountries[i]) == winner_country) winner_index = i;
|
||||
if (winner_index < 0) return reject("invalid_statistics_country");
|
||||
const J* abs = field(request, "absolute");
|
||||
const bool absolute = abs && std::holds_alternative<bool>(abs->value) && std::get<bool>(abs->value);
|
||||
A curves;
|
||||
for (int i = 0; i < 6; ++i) curves.push_back(true);
|
||||
if (const J* c = field(request, "curves")) {
|
||||
if (!c->array() || c->array()->size() != 6) return reject("invalid_statistics_curves");
|
||||
curves = *c->array();
|
||||
}
|
||||
try {
|
||||
const auto seasons = seasons_of(g);
|
||||
const int team = manager >= 0 && size_t(manager) < g.manager_teams.size() ? g.manager_teams[size_t(manager)] : -1;
|
||||
const bool club = team >= 0 && team < league_clubs();
|
||||
std::string own_id;
|
||||
if (club) {
|
||||
own_id = team < int(g.world_club_id.size()) ? g.world_club_id[size_t(team)] : std::string{};
|
||||
if (own_id.empty()) own_id = "team:" + std::to_string(team);
|
||||
}
|
||||
const int own_league = club ? team_league(team) : -1;
|
||||
const int year = g.display_start_year + g.season;
|
||||
std::string manager_name;
|
||||
const size_t at = size_t(std::max(0, manager)) * 0xb0c + 0xa8;
|
||||
for (size_t i = 0; i < 30 && at + i < g.manager_records.size() && g.manager_records[at + i]; ++i)
|
||||
manager_name.push_back(char(g.manager_records[at + i]));
|
||||
A sections, views, countries, eternal;
|
||||
for (const char* s : kSections) sections.push_back(s);
|
||||
for (const char* v : kWorldViews) views.push_back(v);
|
||||
for (const auto& c : kCountries) countries.push_back(O{{"id", country_nation(c)}, {"name", c.name}});
|
||||
for (int v = 0; v < 5; ++v)
|
||||
eternal.push_back(O{{"value", v}, {"nation", country_nation(kCountries[kEternal[v]])}, {"name", kCountries[kEternal[v]].name}});
|
||||
eternal.push_back(O{{"value", 5}, {"name", "Absolut"}});
|
||||
O out{{"ok", true}, {"fictional", false}, {"available", true}, {"section", section}, {"category", category},
|
||||
{"country", country}, {"winner_country", winner_country}, {"absolute", absolute}, {"curves", curves},
|
||||
{"title", kSections[section]}, {"season", season_label(year)}, {"previous_season", season_label(year - 1)},
|
||||
{"year", year}, {"manager", manager_name.empty() ? "—" : almanac_utf8(manager_name)},
|
||||
{"club", club ? J(own_id) : J(nullptr)}, {"columns", A{}}, {"rows", A{}}, {"countries", std::move(countries)},
|
||||
{"sections", std::move(sections)}, {"world_views", std::move(views)}, {"series", A{}},
|
||||
{"eternal_countries", std::move(eternal)}};
|
||||
// the views without a record of their own yet: title chances, transfers, weekly curves,
|
||||
// season finances; the groups and final round of the national tournaments
|
||||
bool known = true;
|
||||
if (section == 3) {
|
||||
eternal_table(out, seasons, country == 5 ? own_league : kCountries[kEternal[country]].league);
|
||||
} else if (section == 4) {
|
||||
known = world_tournaments(out, g, seasons, country);
|
||||
} else if (section == 6) {
|
||||
if (club) series_and_records(out, g, seasons, own_league);
|
||||
} else if (section == 7) {
|
||||
winners_by_season(out, seasons, category, winner_index);
|
||||
} else if (section == 8) {
|
||||
if (club) minute_buckets(out, seasons, own_id);
|
||||
} else {
|
||||
known = false;
|
||||
}
|
||||
if (!known) out["reason"] = "not_recorded";
|
||||
out["available"] = known && !std::get<A>(out["rows"].value).empty();
|
||||
return J(std::move(out));
|
||||
} catch (const std::exception&) {
|
||||
return reject("statistics_unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bmh::modern
|
||||
Reference in new issue
Block a user