Bind player rankings and fair play to recorded season events (by Codex)
This commit is contained in:
1 parent
6b4aa9772d
commit
738ecc7a09
27 files changed
+436
-36
No files matched your search
@@ -169,7 +169,7 @@ struct PlayerRow {
|
||||
};
|
||||
|
||||
J player_stats(const ModernState& g, const std::vector<AlmanacSeason>& seasons, const std::string& competition,
|
||||
const std::string& season, const std::string& sort, size_t limit) {
|
||||
const std::string& season, const std::string& sort, size_t limit, const std::string& team = {}) {
|
||||
std::map<std::string, PlayerRow> rows;
|
||||
uint64_t order = 0;
|
||||
for (const auto& s : seasons) {
|
||||
@@ -178,7 +178,9 @@ J player_stats(const ModernState& g, const std::vector<AlmanacSeason>& seasons,
|
||||
++order;
|
||||
if (competition_of(g, s, m).id != competition) continue;
|
||||
auto row = [&](uint16_t player, int side) -> PlayerRow* {
|
||||
if (player >= s.players.size()) return nullptr;
|
||||
if (player >= s.players.size() || side < 0 || side > 1) return nullptr;
|
||||
const uint16_t club = side ? m.away : m.home;
|
||||
if (!team.empty() && (club >= s.clubs.size() || s.clubs[club].id != team)) return nullptr;
|
||||
const AlmanacPlayer& p = s.players[player];
|
||||
PlayerRow& r = rows[p.id];
|
||||
r.id = p.id, r.name = player_name(p);
|
||||
@@ -765,6 +767,82 @@ cli::Json query_statistics_screen(const ModernState& g, int manager, const cli::
|
||||
}
|
||||
|
||||
|
||||
// Current-season views for the existing player-statistics and fair-play presenters.
|
||||
cli::Json query_rankings(const ModernState& g, int manager, const cli::Json& request) {
|
||||
const J* kind = field(request, "kind"), *wanted = field(request, "leagueId"), *scope_value = field(request, "scope");
|
||||
if (!kind || !kind->string()) return error("invalid_rankings_request");
|
||||
const bool fair = *kind->string() == "fairplay";
|
||||
if (!fair && *kind->string() != "player-rankings") return error("invalid_rankings_request");
|
||||
if ((scope_value && (!scope_value->integer() || *scope_value->integer() < 0 || *scope_value->integer() > 1)) || manager < 0 || size_t(manager) >= g.manager_teams.size()) return error("invalid_rankings_request");
|
||||
const int scope = scope_value ? int(*scope_value->integer()) : 0;
|
||||
const int own = g.manager_teams[size_t(manager)];
|
||||
if (own < 0 || own >= league_clubs()) return error("no_club");
|
||||
const auto league_id = [&](int l) { return size_t(l) < g.world_league_id.size() && !g.world_league_id[size_t(l)].empty() ? g.world_league_id[size_t(l)] : "league:" + std::to_string(l); };
|
||||
int requested = -1;
|
||||
for (int l = 0; l < kLeagueCount; ++l) if (wanted && wanted->string() && *wanted->string() == league_id(l)) requested = l;
|
||||
if (!fair && requested < 0) return error("invalid_rankings_league");
|
||||
const int league = fair ? 0 : scope ? team_league(own) : requested;
|
||||
const std::string season = season_label(g.display_start_year + g.season);
|
||||
const auto team_id = [&](int t) {return size_t(t) < g.world_club_id.size() && !g.world_club_id[size_t(t)].empty() ? g.world_club_id[size_t(t)] : "team:" + std::to_string(t);};
|
||||
const auto team_name = [&](int t) {return size_t(t) < g.world_club_name.size() && !g.world_club_name[size_t(t)].empty() ? g.world_club_name[size_t(t)] : almanac_utf8(g.club_name(t));};
|
||||
try {
|
||||
const auto seasons = seasons_of(g);
|
||||
O out{{"ok", true}, {"kind", fair ? "fairplay" : "player-rankings"}, {"leagueId", league_id(league)}, {"season", season}, {"team", O{{"id", team_id(own)}, {"name", team_name(own)}}}};
|
||||
if (!fair) {
|
||||
out["requestLeagueId"] = league_id(requested); out["scope"] = scope;
|
||||
std::map<std::string, int> current_teams;
|
||||
for (int t = 0; t < league_clubs(); ++t) {
|
||||
const auto range = squad_range(t);
|
||||
for (size_t slot = range.first; slot < range.first + range.count && slot < g.squad_slots.size(); ++slot) {
|
||||
const size_t player = g.squad_slots[slot] & 0x0fff;
|
||||
if (player != 0x0fff && player < g.world_player_id.size() && !g.world_player_id[player].empty()) current_teams[g.world_player_id[player]] = t;
|
||||
}
|
||||
}
|
||||
A categories;
|
||||
for (const char* sort : {"goals", "assists", "yellow", "red"}) {
|
||||
J stats = player_stats(g, seasons, league_id(league), season, sort, 0, scope ? team_id(own) : std::string());
|
||||
for (auto& row : std::get<A>(stats.object()->at("players").value)) {
|
||||
auto& values = *row.object(); const auto& pid = *values.at("player").object()->at("id").string();
|
||||
const World* world = current_world(); const WorldPlayer* player = world ? world->player(pid) : nullptr;
|
||||
values["photoAssetId"] = player && !player->photo.empty() ? J(player->photo) : J(nullptr);
|
||||
const auto active = current_teams.find(pid);
|
||||
values["currentTeam"] = active == current_teams.end() ? J(nullptr) : J(O{{"id", team_id(active->second)}, {"name", team_name(active->second)}});
|
||||
values["value"] = std::string(sort) == "red" ? J(*values.at("red").integer() + *values.at("second_yellow").integer()) : values.at(std::string(sort));
|
||||
}
|
||||
categories.push_back(std::move(stats.object()->at("players")));
|
||||
}
|
||||
out["categories"] = std::move(categories);
|
||||
} else {
|
||||
// A presentation weighting, matching the approved UI; it does not affect discipline.
|
||||
out["weights"] = O{{"yellow", 1}, {"red", 4}};
|
||||
struct Cards { std::string id, name; int yellow = 0, red = 0; };
|
||||
std::vector<Cards> cards;
|
||||
for (int t = league_first_team(league); t < league_first_team(league) + kLeagueSizes[league]; ++t) cards.push_back({team_id(t), team_name(t)});
|
||||
int matches = 0;
|
||||
for (const auto& s : seasons) if (season_label(s.start_year) == season)
|
||||
for (const auto& m : s.matches) {
|
||||
if (competition_of(g, s, m).id != league_id(league)) continue;
|
||||
++matches;
|
||||
for (const auto& e : m.events) {
|
||||
if (e.side > 1) continue;
|
||||
const auto club = club_json(s, e.side ? m.away : m.home);
|
||||
if (!club.object()) continue;
|
||||
const auto& id = *club.object()->at("id").string();
|
||||
for (auto& c : cards) if (c.id == id) {
|
||||
if (e.kind == AlmanacEventKind::YellowCard) ++c.yellow;
|
||||
if (e.kind == AlmanacEventKind::RedCard || e.kind == AlmanacEventKind::SecondYellow) ++c.red;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::stable_sort(cards.begin(), cards.end(), [](const Cards& a, const Cards& b) {return a.yellow + 4 * a.red < b.yellow + 4 * b.red;});
|
||||
A rows;
|
||||
for (const auto& c : cards) rows.push_back(O{{"rank", int(rows.size() + 1)}, {"team", O{{"id", c.id}, {"name", c.name}}}, {"yellow", c.yellow}, {"red", c.red}, {"points", c.yellow + 4 * c.red}});
|
||||
out["rows"] = std::move(rows); out["recordedMatches"] = matches;
|
||||
}
|
||||
return out;
|
||||
} catch (const std::exception&) { return error("rankings_unavailable"); }
|
||||
}
|
||||
|
||||
// --- the live ticker of the Modern UI: the recorded matches of the last kickoff ---------------
|
||||
|
||||
namespace {
|
||||
|
||||
Reference in new issue
Block a user