Modern UI: the almanac screen reads the recorded matches

ModernSession::read_almanac answers the almanac screen's request kinds
(index, calendar, day, match) from the engine's almanac in the demo's match
form: competition, sides with human managers, result split into regulation,
extra time (by the goal minutes) and shoot-out, referee, attendance, weather,
line-ups and events. Unknown line-ups stay null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 08:47:41 +01:00
1 parent ae817c6d6d
commit e82ce1649f
5 files changed
+170

No files matched your search

+130
View File
@@ -4,6 +4,7 @@
#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
#include <string>
#include <variant>
#include <vector>
@@ -268,4 +269,133 @@ cli::Json query_statistics(const ModernState& g, const cli::Json& request) {
}
}
// --- the almanac view of the Modern UI (DataSource::read_almanac; the demo's request kinds) ------
namespace {
J almanac_person(const AlmanacSeason& s, uint16_t player) {
if (player == kAlmanacNone || player >= s.players.size()) return nullptr;
const AlmanacPlayer& p = s.players[player];
return O{{"id", p.id}, {"name", player_name(p)}};
}
J almanac_side(const ModernState& g, const AlmanacSeason& s, uint16_t club, int8_t manager) {
J m = nullptr;
if (manager >= 0 && size_t(manager) < s.managers.size()) {
const AlmanacManager& a = s.managers[size_t(manager)];
std::string id = a.index >= 0 && size_t(a.index) < g.manager_ids.size() ? g.manager_ids[size_t(a.index)] : std::string{};
if (id.empty()) id = "manager:slot-" + std::to_string(a.index);
m = O{{"id", id}, {"name", a.name}};
}
return O{{"id", s.clubs[club].id}, {"name", s.clubs[club].name}, {"crest", nullptr}, {"manager", std::move(m)}};
}
std::string match_id(const AlmanacSeason& s, size_t index) {
return "match:" + std::to_string(s.start_year) + "-" + std::to_string(index);
}
// One recorded match in the form the almanac screen reads. The score after 90 minutes and the
// extra time are told apart by the goal minutes; a line-up shorter than eleven is not known (null).
J almanac_match(const ModernState& g, const AlmanacSeason& s, size_t index) {
static constexpr const char* kKinds[8] = {"goal", "yellow", "red", "second_yellow", "injury", "substitution", "shootout", "miss"};
const AlmanacMatch& m = s.matches[index];
const Competition c = competition_of(g, s, m);
const std::string type = m.type == 0 ? "league" : m.type - 1 == 10 ? "friendly" : "cup";
int regulation[2] = {0, 0};
for (const auto& e : m.events)
if (e.kind == AlmanacEventKind::Goal && e.minute <= 90 && e.side < 2) ++regulation[e.side];
const bool extra = m.decided >= 1, shootout = m.decided == 2;
J lineups = O{};
for (int side = 0; side < 2; ++side) {
const auto& l = m.lineup[size_t(side)];
if (l.size() < 11) {
std::get<O>(lineups.value)[side ? "away" : "home"] = nullptr;
continue;
}
A starters, bench;
for (size_t i = 0; i < l.size(); ++i) (i < 11 ? starters : bench).push_back(almanac_person(s, l[i]));
std::get<O>(lineups.value)[side ? "away" : "home"] = O{{"starters", std::move(starters)}, {"bench", std::move(bench)}};
}
A events;
for (const auto& e : m.events)
events.push_back(O{{"kind", kKinds[size_t(e.kind) & 7]}, {"minute", int(e.minute)}, {"side", e.side ? "away" : "home"},
{"player", almanac_person(s, e.player)}, {"other", almanac_person(s, e.other)}, {"detail", int(e.detail)}});
return O{{"id", match_id(s, index)}, {"date", iso_date(m.date, s.display_start_year)}, {"season", season_label(s.start_year)},
{"competition", O{{"id", c.id}, {"name", c.name}, {"type", type}}}, {"round", int(m.round)},
{"home", almanac_side(g, s, m.home, m.home_manager)}, {"away", almanac_side(g, s, m.away, m.away_manager)},
{"result", O{{"regulation", extra ? J(A{J(regulation[0]), J(regulation[1])}) : J(A{J(int(m.home_goals)), J(int(m.away_goals))})},
{"extra", extra ? J(A{J(int(m.home_goals) - regulation[0]), J(int(m.away_goals) - regulation[1])}) : J(nullptr)},
{"penalties", shootout ? J(A{J(int(m.home_shootout)), J(int(m.away_shootout))}) : J(nullptr)},
{"final", A{J(int(m.home_goals)), J(int(m.away_goals))}}, {"extra_played", extra}, {"penalties_played", shootout}}},
{"referee", m.referee > 0 && m.referee < s.referees.size() ? J(s.referees[m.referee]) : J(nullptr)},
{"attendance", int64_t(m.attendance)},
{"weather", O{{"temperature", int(m.temperature)}, {"rain", int(m.rain)}}},
{"lineups", std::move(lineups)}, {"events", std::move(events)}};
}
} // namespace
cli::Json query_almanac(const ModernState& g, const cli::Json& request) {
const J* kind = field(request, "kind");
if (!kind || !kind->string()) return error("invalid_almanac_request");
try {
const auto seasons = seasons_of(g);
const std::string k = *kind->string();
if (k == "index") {
std::set<int> years;
std::string first, last;
size_t count = 0;
for (const auto& s : seasons)
for (const auto& m : s.matches) {
const std::string d = iso_date(m.date, s.display_start_year);
years.insert(std::stoi(d.substr(0, 4)));
if (first.empty() || d < first) first = d;
if (d > last) last = d;
++count;
}
A listed;
for (int y : years) listed.push_back(y);
return O{{"ok", true}, {"years", std::move(listed)}, {"match_count", int64_t(count)},
{"first_date", first.empty() ? J(nullptr) : J(first)}, {"last_date", last.empty() ? J(nullptr) : J(last)}};
}
if (k == "calendar") {
const J* year = field(request, "year");
if (!year || !year->integer()) return error("invalid_almanac_request");
std::map<std::string, std::pair<int, A>> days;
for (const auto& s : seasons)
for (size_t i = 0; i < s.matches.size(); ++i) {
const AlmanacMatch& m = s.matches[i];
const std::string d = iso_date(m.date, s.display_start_year);
if (std::stoi(d.substr(0, 4)) != *year->integer()) continue;
auto& day = days[d];
++day.first;
if (m.decided >= 1 || m.home_goals + m.away_goals >= 4) day.second.push_back(match_id(s, i));
}
A listed;
for (auto& [d, day] : days) listed.push_back(O{{"date", d}, {"count", day.first}, {"highlights", std::move(day.second)}});
return O{{"ok", true}, {"year", *year->integer()}, {"days", std::move(listed)}};
}
if (k == "day") {
const J* date = field(request, "date");
if (!date || !date->string()) return error("invalid_almanac_request");
A matches;
for (const auto& s : seasons)
for (size_t i = 0; i < s.matches.size(); ++i)
if (iso_date(s.matches[i].date, s.display_start_year) == *date->string()) matches.push_back(almanac_match(g, s, i));
return O{{"ok", true}, {"date", *date->string()}, {"matches", std::move(matches)}};
}
if (k == "match") {
const J* id = field(request, "id");
if (!id || !id->string()) return error("invalid_almanac_request");
for (const auto& s : seasons)
for (size_t i = 0; i < s.matches.size(); ++i)
if (match_id(s, i) == *id->string()) return O{{"ok", true}, {"match", almanac_match(g, s, i)}};
return error("almanac_match_not_found");
}
return error("invalid_almanac_request");
} catch (const std::exception&) {
return error("almanac_unavailable");
}
}
} // namespace bmh::modern