Modern UI contract: press events from the almanac

The news read lists a team's press events derived from the almanac, so
they are the same on every read with stable occurrence ids: match and cup
results (final, extra time, penalties), send-offs and injuries, runs,
league position, champion and titles, with typed facts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 18:06:35 +01:00
1 parent d943494ef4
commit 9c436c753e
5 files changed
+173

No files matched your search

+120
View File
@@ -980,4 +980,124 @@ cli::Json almanac_matches_between(const ModernState& g, uint32_t from_date, uint
return out;
}
cli::Json query_news(const ModernState& g, const std::string& team, const std::string& from, const std::string& until) {
A events;
const auto entity = [](const AlmanacClub& c) {
return J(O{{"id", c.id}, {"name", c.name}, {"kind", c.id.starts_with("nt:") ? "national" : "club"}});
};
const auto competition = [](const AlmanacSeason& s, const Competition& c, const AlmanacMatch* m) {
const std::string kind = c.type == "league" ? "league" : m && m->type - 1 == 10 ? "friendly" : "cup";
return J(O{{"id", c.id}, {"name", c.name}, {"kind", kind}, {"season", season_label(s.start_year)}});
};
const auto add = [&](const std::string& type, const std::string& key, const std::string& date, const J& scope, O data) {
if (date < from || date > until) return;
events.push_back(O{{"id", "press-event:" + type + ":" + key + ":" + team}, {"type", type}, {"date", date},
{"scope", scope}, {"data", std::move(data)}, {"variant", nullptr}});
};
for (const AlmanacSeason& s : seasons_of(g)) {
int own_club = -1;
for (size_t c = 0; c < s.clubs.size(); ++c)
if (s.clubs[c].id == team) own_club = int(c);
if (own_club < 0) continue;
const J me = entity(s.clubs[size_t(own_club)]);
const J scope = O{{"kind", std::get<O>(me.value).at("kind")}, {"id", team}, {"name", s.clubs[size_t(own_club)].name}};
// the runs over the competitive matches: wins, unbeaten, winless, losses
int wins = 0, unbeaten = 0, winless = 0, losses = 0;
for (size_t i = 0; i < s.matches.size(); ++i) {
const AlmanacMatch& m = s.matches[i];
const int side = m.home == own_club ? 0 : m.away == own_club ? 1 : -1;
if (side < 0) continue;
const AlmanacClub& other = s.clubs[side ? m.home : m.away];
const std::string id = match_id(s, i), date = iso_date(m.date, s.display_start_year);
const Competition c = competition_of(g, s, m);
const int own = side ? m.away_goals : m.home_goals, opp = side ? m.home_goals : m.away_goals;
// trailing at some point, and the minute of the deciding goal (the goal that put the
// winner one ahead of the loser's final total; none for a draw)
bool trailed = false;
int score[2] = {0, 0}, decisive = -1;
for (const auto& e : m.events) {
if (e.kind != AlmanacEventKind::Goal) continue;
const int k = e.side == side ? 0 : 1;
++score[k];
if (score[0] < score[1]) trailed = true;
if (own > opp && k == 0 && score[0] == opp + 1) decisive = e.minute;
if (opp > own && k == 1 && score[1] == own + 1) decisive = e.minute;
}
const bool shootout_won = side ? m.away_shootout > m.home_shootout : m.home_shootout > m.away_shootout;
O data{{"team", me}, {"opponent", entity(other)}, {"competition", competition(s, c, &m)}, {"matchId", id},
{"score", O{{"for", own}, {"against", opp}}}, {"venue", side ? "away" : "home"}, {"trailed", trailed},
{"minute", decisive >= 0 ? J(decisive) : J(nullptr)}, {"outcome", own > opp ? "win" : own < opp ? "loss" : "draw"}};
const bool friendly = m.type != 0 && m.type - 1 == 10, cup = m.type != 0 && !friendly;
bool final_match = false;
for (const AlmanacWinner& w : s.winners)
if (cup && w.type == m.type && w.detail == 0 && w.date == m.date && w.runner_up != 0xffff &&
((w.winner == m.home && w.runner_up == m.away) || (w.winner == m.away && w.runner_up == m.home)))
final_match = true;
if (cup) {
data["round"] = int(m.round);
if (m.decided == 2) {
O pens = data;
pens["shootout"] = O{{"for", int(side ? m.away_shootout : m.home_shootout)},
{"against", int(side ? m.home_shootout : m.away_shootout)}};
pens["outcome"] = shootout_won ? "win" : "loss";
add("cup.penalties", id, date, scope, std::move(pens));
} else if (m.decided == 1) {
add("cup.extra_time", id, date, scope, data);
}
add(final_match ? "cup.final_result" : "cup.round_result", id, date, scope, std::move(data));
} else {
add("match.result", id, date, scope, std::move(data));
}
// the team's players: sent off, injured
for (size_t k = 0; k < m.events.size(); ++k) {
const AlmanacEvent& e = m.events[k];
if (e.side != side) continue;
const std::string key = id + "-" + std::to_string(k);
if (e.kind == AlmanacEventKind::RedCard || e.kind == AlmanacEventKind::SecondYellow)
add("player.sent_off", key, date, scope, O{{"player", almanac_person(s, e.player)}, {"team", me}, {"minute", int(e.minute)}});
if (e.kind == AlmanacEventKind::Injury)
add("player.injury", key, date, scope, O{{"player", almanac_person(s, e.player)}, {"team", me}, {"minute", int(e.minute)}});
}
if (friendly) continue; // friendlies do not count for the runs
const bool won = own > opp || (m.decided == 2 && shootout_won);
const bool lost = own < opp || (m.decided == 2 && !shootout_won);
wins = won ? wins + 1 : 0, losses = lost ? losses + 1 : 0;
unbeaten = lost ? 0 : unbeaten + 1, winless = won ? 0 : winless + 1;
const auto run = [&](const char* type, int count, std::initializer_list<int> marks) {
for (int mark : marks)
if (count == mark) add(type, id, date, scope, O{{"team", me}, {"matches", count}});
};
run("run.wins", wins, {3, 5, 8, 10, 15});
run("run.unbeaten", unbeaten, {5, 10, 15, 20, 30});
run("run.winless", winless, {5, 8, 10, 15});
run("run.losses", losses, {3, 5, 8, 10});
}
// the season's end: league position and champion (dated on the league's last match day), titles
for (const AlmanacTable& t : s.tables)
for (const AlmanacStanding& r : t.rows) {
if (r.club != own_club) continue;
const Competition c = league_competition(s, t.league);
uint32_t last = 0;
for (const auto& m : s.matches)
if (m.type == 0 && m.league == t.league) last = std::max(last, m.date);
const std::string date = iso_date(last, s.display_start_year), key = season_label(s.start_year) + "-" + c.id;
O data{{"team", me}, {"competition", competition(s, c, nullptr)}, {"season", season_label(s.start_year)},
{"position", int(r.rank)}, {"points", int(r.points)}};
add("league.position", key, date, scope, data);
if (r.rank == 1) add("league.champion", key, date, scope, std::move(data));
}
for (const AlmanacWinner& w : s.winners) {
if (w.type == 0 || w.winner != own_club) continue;
const Competition c = competition_of(g, s, w);
add(w.detail == 1 ? "indoor.won" : "cup.won", season_label(s.start_year) + "-" + c.id, iso_date(w.date, s.display_start_year),
scope, O{{"team", me}, {"competition", competition(s, c, nullptr)}, {"season", season_label(s.start_year)}});
}
}
std::stable_sort(events.begin(), events.end(), [](const J& a, const J& b) {
return *std::get<O>(a.value).at("date").string() < *std::get<O>(b.value).at("date").string();
});
return O{{"ok", true}, {"events", std::move(events)}};
}
} // namespace bmh::modern