Modern UI: the live ticker replays the last kickoff's matches

ModernSession::read_live answers the ticker's fixture and frame queries with
the engine's recording of the last session.advance: the matches of human
managers with their owners, frames in minute order (kickoff, every event,
half time, the end) with score, chances, cards and players on the pitch, the
event lines, and the day's other matches as the conference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 09:37:37 +01:00
1 parent 1400848387
commit fda4b72967
6 files changed
+213

No files matched your search

+140
View File
@@ -747,4 +747,144 @@ cli::Json query_statistics_screen(const ModernState& g, int manager, const cli::
}
}
// --- the live ticker of the Modern UI: the recorded matches of the last kickoff ---------------
namespace {
// The ticker's lines for an event of the almanac (German, as the interface's demo).
std::pair<std::string, std::string> ticker_text(const AlmanacSeason& s, const AlmanacMatch& m, const AlmanacEvent& e,
int home, int away) {
const std::string team = s.clubs[e.side ? m.away : m.home].name;
const std::string who = e.player != kAlmanacNone && e.player < s.players.size() ? player_name(s.players[e.player]) : team;
const std::string other = e.other != kAlmanacNone && e.other < s.players.size() ? player_name(s.players[e.other]) : std::string{};
const std::string score = std::to_string(home) + ":" + std::to_string(away);
switch (e.kind) {
case AlmanacEventKind::Goal:
return {"Tor für " + team, who + " trifft zum " + score + "." + (other.empty() ? "" : " Vorlage: " + other + ".")};
case AlmanacEventKind::Miss: return {"Chance für " + team, who + " vergibt."};
case AlmanacEventKind::YellowCard: return {"Gelbe Karte", who + " (" + team + ") sieht Gelb."};
case AlmanacEventKind::SecondYellow: return {"Gelb-Rot", who + " (" + team + ") muss vom Platz."};
case AlmanacEventKind::RedCard: return {"Rote Karte", who + " (" + team + ") fliegt vom Platz."};
case AlmanacEventKind::Injury: return {"Verletzung", who + " (" + team + ") ist verletzt."};
case AlmanacEventKind::Substitution:
return {"Wechsel bei " + team, who + (other.empty() ? " kommt ins Spiel." : " kommt für " + other + ".")};
case AlmanacEventKind::ShootoutKick:
return {"Elfmeterschießen", who + (e.detail ? " verwandelt." : " verschießt.")};
}
return {"", ""};
}
const char* event_kind(AlmanacEventKind k) {
static constexpr const char* kKinds[8] = {"goal", "yellow", "red", "second_yellow", "injury", "substitution", "shootout", "miss"};
return kKinds[size_t(k) & 7];
}
} // namespace
cli::Json live_fixture(const ModernState& g, uint32_t after_date, uint32_t until_date, uint64_t revision) {
// the matches of the last kickoff (dates after_date <= d < until_date) from the almanac: the
// ones with a human manager are the ticker's matches (at most four), all others the conference
struct Ref { const AlmanacSeason* season; size_t index; };
const auto seasons = seasons_of(g);
std::vector<Ref> human, others;
for (const auto& s : seasons)
for (size_t i = 0; i < s.matches.size(); ++i) {
const AlmanacMatch& m = s.matches[i];
if (m.date < after_date || m.date >= until_date) continue; // played from the desk day on, before the next desk
((m.home_manager >= 0 || m.away_manager >= 0) && human.size() < 4 ? human : others).push_back({&s, i});
}
if (human.empty()) return O{{"ok", false}, {"error", "live_no_human_match"}};
A owners, matches, conference;
std::map<std::string, std::string> owner_match;
for (const Ref& r : human) {
const AlmanacSeason& s = *r.season;
const AlmanacMatch& m = s.matches[r.index];
const std::string id = match_id(s, r.index);
A match_owners;
for (int side = 0; side < 2; ++side) {
const int8_t mi = side ? m.away_manager : m.home_manager;
if (mi < 0 || size_t(mi) >= s.managers.size()) continue;
const AlmanacManager& a = s.managers[size_t(mi)];
std::string mid = a.index >= 0 && size_t(a.index) < g.manager_ids.size() ? g.manager_ids[size_t(a.index)] : std::string{};
if (mid.empty()) mid = "manager:slot-" + std::to_string(a.index);
if (owner_match.count(mid)) continue;
owner_match[mid] = id;
match_owners.push_back(mid);
const AlmanacClub& c = s.clubs[side ? m.away : m.home];
const size_t rec = size_t(std::max(0, a.index)) * 0xb0c + 0x221;
owners.push_back(O{{"id", mid}, {"portrait", rec < g.manager_records.size() ? int(g.manager_records[rec]) : 0},
{"name", a.name}, {"team", O{{"id", c.id}, {"name", c.name}, {"role", c.id.starts_with("nt:") ? "national" : "club"}}},
{"match_id", id}, {"view", nullptr}});
}
const Competition c = competition_of(g, s, m);
matches.push_back(O{{"id", id}, {"home", O{{"id", s.clubs[m.home].id}, {"name", s.clubs[m.home].name}}},
{"away", O{{"id", s.clubs[m.away].id}, {"name", s.clubs[m.away].name}}},
{"owners", std::move(match_owners)}, {"competition", c.name}});
}
for (const Ref& r : others) conference.push_back(almanac_match(g, *r.season, r.index));
// the frames: kickoff, every event of a ticker match in minute order, half time, the end
struct Moment { int minute; int order; const Ref* ref; const AlmanacEvent* event; const char* phase; };
std::vector<Moment> moments;
bool extra = false, shootout = false;
for (const Ref& r : human) {
const AlmanacMatch& m = r.season->matches[r.index];
extra |= m.decided >= 1, shootout |= m.decided == 2;
for (const auto& e : m.events) moments.push_back({e.minute, 1, &r, &e, nullptr});
}
moments.push_back({0, 0, nullptr, nullptr, "kickoff"});
moments.push_back({45, 2, nullptr, nullptr, "interval"});
moments.push_back({46, 0, nullptr, nullptr, "restart"});
moments.push_back({extra ? 120 : 90, 3, nullptr, nullptr, shootout ? "shootout_end" : "fulltime"});
std::stable_sort(moments.begin(), moments.end(), [](const Moment& a, const Moment& b) {
return a.minute != b.minute ? a.minute < b.minute : a.order < b.order;
});
struct Tally { int score[2] = {0, 0}, chances[2] = {0, 0}, yellow[2] = {0, 0}, red[2] = {0, 0}, shootout[2] = {0, 0}; };
std::vector<Tally> tally(human.size());
A frames, events;
for (const Moment& mo : moments) {
if (mo.event) {
const size_t k = size_t(mo.ref - human.data());
const AlmanacSeason& s = *mo.ref->season;
const AlmanacMatch& m = s.matches[mo.ref->index];
const AlmanacEvent& e = *mo.event;
Tally& t = tally[k];
const size_t side = e.side ? 1 : 0;
if (e.kind == AlmanacEventKind::Goal) ++t.score[side], ++t.chances[side];
if (e.kind == AlmanacEventKind::Miss) ++t.chances[side];
if (e.kind == AlmanacEventKind::YellowCard) ++t.yellow[side];
if (e.kind == AlmanacEventKind::RedCard || e.kind == AlmanacEventKind::SecondYellow) ++t.red[side];
if (e.kind == AlmanacEventKind::ShootoutKick && e.detail) ++t.shootout[side];
const auto [headline, body] = ticker_text(s, m, e, t.score[0], t.score[1]);
const std::string mid = match_id(s, mo.ref->index);
events.push_back(O{{"id", mid + ":event:" + std::to_string(events.size())}, {"sequence", int64_t(frames.size())},
{"match_id", mid}, {"minute", mo.minute}, {"kind", event_kind(e.kind)}, {"headline", headline},
{"body", body}, {"score", A{J(t.score[0]), J(t.score[1])}}, {"side", side ? "away" : "home"},
{"player", almanac_person(s, e.player)}, {"other", almanac_person(s, e.other)}});
}
A scores;
for (size_t k = 0; k < human.size(); ++k) {
const Tally& t = tally[k];
scores.push_back(O{{"match_id", match_id(*human[k].season, human[k].index)},
{"score", A{J(t.score[0]), J(t.score[1])}}, {"chances", A{J(t.chances[0]), J(t.chances[1])}},
{"yellow", A{J(t.yellow[0]), J(t.yellow[1])}}, {"red", A{J(t.red[0]), J(t.red[1])}},
{"shootout", A{J(t.shootout[0]), J(t.shootout[1])}},
{"players", A{J(11 - t.red[0]), J(11 - t.red[1])}}, {"standings", A{}}});
}
const char* phase = mo.phase ? mo.phase : mo.minute <= 45 ? "first_half" : mo.minute <= 90 ? "second_half"
: mo.minute <= 120 ? "extra_time" : "shootout";
frames.push_back(O{{"sequence", int64_t(frames.size())}, {"minute", mo.minute}, {"phase", phase},
{"matches", std::move(scores)}, {"events", events}});
}
const AlmanacSeason& first = *human.front().season;
return O{{"ok", true}, {"schema", "openbmh-live-fixture@1"}, {"source", "engine-recording"},
{"fixture_id", "live:" + std::to_string(first.start_year) + "-" + std::to_string(human.front().index)},
{"source_revision", int64_t(revision)}, {"date", iso_date(first.matches[human.front().index].date, first.display_start_year)},
{"role", owners.empty() ? "club" : std::get<O>(std::get<O>(owners.front().value).at("team").value).at("role")},
{"surface", "outdoor"}, {"duration", extra ? 120 : 90}, {"field_count", 11}, {"owners", std::move(owners)},
{"matches", std::move(matches)}, {"frames", std::move(frames)}, {"conference", std::move(conference)},
{"notice", "Aufzeichnung der Engine: die Spiele des letzten Anstoßes"}};
}
} // namespace bmh::modern