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:
1 parent
d943494ef4
commit
9c436c753e
5 files changed
+173
No files matched your search
@@ -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
|
||||
@@ -44,6 +44,15 @@ cli::Json query_statistics_screen(const ModernState& g, int manager, const cli::
|
||||
// day as the conference. source: "engine-recording".
|
||||
cli::Json live_fixture(const ModernState& g, uint32_t after_date, uint32_t until_date, uint64_t revision);
|
||||
|
||||
// The press events of a team (contract team id) with from <= date <= until (ISO dates), derived
|
||||
// from the almanac and so the same on every read: match.result, cup.round_result /
|
||||
// cup.final_result / cup.extra_time / cup.penalties, player.sent_off, player.injury, run.wins /
|
||||
// run.unbeaten / run.winless / run.losses (at fixed lengths), league.position /
|
||||
// league.champion, cup.won / indoor.won. Each {id, type, date, scope, data, variant: null};
|
||||
// the id is stable ("press-event:<type>:<occurrence>:<team>"), the facts are typed (score
|
||||
// {for, against} from the team's side, venue, trailed, minute of the deciding goal).
|
||||
cli::Json query_news(const ModernState& g, const std::string& team, const std::string& from, const std::string& until);
|
||||
|
||||
// A game of a matchday under way: runtime teams and goals so far.
|
||||
struct LiveGame { int home = 0, away = 0, home_goals = 0, away_goals = 0; };
|
||||
// The live table of a league during matchday `round` (1-based): the table before it plus the
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "game/state.hpp"
|
||||
#include "game/table.hpp"
|
||||
#include "modern/content/projections.hpp"
|
||||
#include "modern/content/statistics.hpp"
|
||||
#include "modern/content/world_package.hpp"
|
||||
#include "modern/game/almanac.hpp"
|
||||
#include "modern/game/desk_actions.hpp"
|
||||
@@ -370,6 +371,24 @@ cli::Json ui_read(const ModernState& g, const UiContext& c, const cli::Json& req
|
||||
if (kind == "session") return snapshot(request_id, std::move(m), session_data(g, c));
|
||||
if (kind == "desktop") return snapshot(request_id, std::move(m), desktop_data(g, c, today));
|
||||
if (kind == "camps") return snapshot(request_id, std::move(m), camps_data(g, c, std::to_string(c.state_revision)));
|
||||
if (kind == "news") {
|
||||
// the press events of a team (teamId, default the desk's team) from `from` to `until` (ISO,
|
||||
// default everything up to today), newest last, at most `limit` (default 50) of the newest
|
||||
const int team = requested_team(g, c, request);
|
||||
if (team < 0) return failure(request_id, "unknown_team", "teamId nennt kein Team.", c.state_revision);
|
||||
const std::string from = text_field(request, "from").empty() ? "0000-00-00" : text_field(request, "from");
|
||||
const std::string until = text_field(request, "until").empty() ? today : text_field(request, "until");
|
||||
const J* limit_field = field(request, "limit");
|
||||
const int64_t limit = limit_field && limit_field->integer() && *limit_field->integer() > 0 ? *limit_field->integer() : 50;
|
||||
J news = query_news(g, team_id(g, team), from, until);
|
||||
A events = std::move(std::get<A>(std::get<O>(news.value).at("events").value));
|
||||
const size_t total = events.size();
|
||||
if (events.size() > size_t(limit)) events.erase(events.begin(), events.end() - limit);
|
||||
return snapshot(request_id, std::move(m),
|
||||
O{{"kind", "news"}, {"teamId", team_id(g, team)}, {"from", from}, {"until", until}, {"total", int64_t(total)},
|
||||
{"events", std::move(events)},
|
||||
{"source", "almanac"}, {"note", "Spielereignisse, Serien und Saisonabschlüsse; Geschäftsereignisse folgen"}});
|
||||
}
|
||||
if (kind == "calendar") {
|
||||
// the season calendar of the desk's team: days from `from` to `until` (ISO dates, default
|
||||
// the current month) with the day's competitions and the team's league matches (planned
|
||||
|
||||
Reference in new issue
Block a user