diff --git a/docs/new_engine/ui-anbindung.md b/docs/new_engine/ui-anbindung.md index faba777..b158b86 100644 --- a/docs/new_engine/ui-anbindung.md +++ b/docs/new_engine/ui-anbindung.md @@ -35,6 +35,7 @@ Revision kommt vom Aufrufer (Konsole bzw. AppController), der Read ändert nicht | `advertising` | 14 Banden (`ad:board/`), Trikot (`ad:shirt`), TV-Rechte (`ad:tv`) je mit Vertrag (Sponsor, Monatsbetrag, Restmonate, Verlängerung, Option) und den offenen Angeboten (`offer:ad--`, Sponsor, Betrag, Banden, Monate, `allowed`); die drei Budgets (Merchandising, Presse, Fanarbeit) mit Stand, Höchstbetrag, Lieferwochen; die Finanzvorschau des Originals | | `stadium` | Umfeld (14 Objekte mit Namen und Bautagen; Verkehrsanbindung mit `stage` und `maxStage`: Classic 3, Modern-Welt 4; sonst `built`), die acht Anlagen (Stufe, Höchststufe, Bauwochen, ob ausbaubar), die acht Tribünenblöcke (Plätze, Höchstplätze, Sitzplätze, Dach, Bauwochen, Plätze nach dem Bau), Eintrittspreis (8–25 €); das offene Angebot des Stadion-Dialogs (`quote`: Umfeld mit Preis, oder Anlage mit Zielstufe, Preis, Bauwochen und dem Trinkgeld-Bereich 6–14 %) | | `transfer` | die Transferliste (`listing:`: Spieler, Alter, Position, Stärke, Verkäufer, Ablöseforderung, Gehaltsreferenz pro Jahr, Gehaltsforderung in %, Zahl der Angebote, eigene Listung, eigenes Gebot), der eigene Kader (gelistet oder nicht), Kontostand, Kreditrahmen | +| `news` | Presseereignisse eines Teams (`teamId`, Vorgabe das Team am Schreibtisch), `from`/`until` (ISO, Vorgabe bis heute), `limit` (Vorgabe 50, die neuesten): `events` je `{id, type, date, scope, data, variant: null}` nach [Pressevertrag](../new_ui/integration/PRESS.md), `total`. Quelle ist der Almanach, darum gleich bei jedem Lesen und mit stabiler ID (`press-event:::`). Typen: `match.result`, `cup.round_result`, `cup.final_result`, `cup.extra_time`, `cup.penalties`, `player.sent_off`, `player.injury`, `run.wins` (3/5/8/10/15), `run.unbeaten` (5/10/15/20/30), `run.winless` (5/8/10/15), `run.losses` (3/5/8/10), `league.position`, `league.champion`, `cup.won`, `indoor.won`. Fakten: `team`, `opponent`, `competition` (mit `kind`, `season`), `matchId`, `score {for, against}` aus Sicht des Teams, `venue`, `trailed`, `minute` (das entscheidende Tor), `outcome`, `round`, `shootout`, `player`, `matches`, `position`, `points`. Noch nicht: Derby, Auf-/Abstieg, Geschäftsereignisse (Transfers, Verträge, Finanzen, Stadion) | | `calendar` | der Saisonkalender des Teams am Schreibtisch, `from`/`until` (ISO, Vorgabe der laufende Monat): `days` (Datum, `seasonDay`, `today`, `eventKinds`: `league`, `cup:`, `replay`, `indoor`, `special`, `national`, `friendly`; `fixtureIds`) und `fixtures` (die eigenen Ligaspiele: `fixture:league---`, Wettbewerb, Runde, `originalDate`, `scheduledDate`, Heim/Gast, `status` `scheduled`/`played`/`rescheduled`/`postponed`, `result`). Verlegte und im Winter ausgefallene Spiele stehen am Nachholtag. Pokalspiele nur als `eventKinds`; Fehler `invalid_range` | | `scheduling` | die verlegbaren eigenen Ligaspiele (`move:---`, Datum, Runde, Heim/Gast, mögliche Zieldaten) und die verlegten, die sich zurücknehmen lassen | | `friendly` | mit `opponentId`, optional `home`: das Angebot für ein Freundschaftsspiel (Heim/Gast, Kosten, Zahler, ob der Gegner einen menschlichen Manager hat) und die freien Daten der Saison; kein Zufall. Am Schreibtisch des Bundestrainers ein Länderspiel (`national: true`, Gegner ein anderes Nationalteam, dazu `ratings` beider Teams): das Original zieht für die Bewertung Zufall; der Read rechnet auf einer Kopie des Generators, die Buchung zieht dieselben Zahlen | diff --git a/src/modern/content/statistics.cpp b/src/modern/content/statistics.cpp index 62fde1b..7b4d5f1 100644 --- a/src/modern/content/statistics.cpp +++ b/src/modern/content/statistics.cpp @@ -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(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 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(a.value).at("date").string() < *std::get(b.value).at("date").string(); + }); + return O{{"ok", true}, {"events", std::move(events)}}; +} + } // namespace bmh::modern diff --git a/src/modern/content/statistics.hpp b/src/modern/content/statistics.hpp index 56086e8..93d7bd6 100644 --- a/src/modern/content/statistics.hpp +++ b/src/modern/content/statistics.hpp @@ -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:::"), 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 diff --git a/src/modern/content/ui_contract.cpp b/src/modern/content/ui_contract.cpp index 75be005..465c2cc 100644 --- a/src/modern/content/ui_contract.cpp +++ b/src/modern/content/ui_contract.cpp @@ -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(std::get(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 diff --git a/tests/modern_ui_contract_tests.cpp b/tests/modern_ui_contract_tests.cpp index c71c6e6..4d6b92b 100644 --- a/tests/modern_ui_contract_tests.cpp +++ b/tests/modern_ui_contract_tests.cpp @@ -696,6 +696,30 @@ int main(int argc, char** argv) { std::printf("(date now %s)\n", s(at(at(session.ui_read(O{{"kind", "desktop"}}, rev), "meta"), "date")).c_str()); std::printf("(%d contract decisions answered)\n", decisions); } + // the press events of the desk's team: stable ids, typed facts, the season's end + { + const J news = session.ui_read(O{{"kind", "news"}, {"limit", 5000}}, rev); + const J again = session.ui_read(O{{"kind", "news"}, {"limit", 5000}}, rev); + const auto* events = at(at(news, "data"), "events").array(); + std::set ids; + std::map types; + bool facts = true; + if (events) + for (const auto& e : *events) { + ids.insert(s(at(e, "id"))); + ++types[s(at(e, "type"))]; + if (s(at(e, "type")) == "match.result") + facts &= at(at(at(e, "data"), "score"), "for").integer() && !s(at(at(at(e, "data"), "opponent"), "id")).empty(); + } + std::string summary; + for (const auto& [t, n] : types) summary += t + " " + std::to_string(n) + ", "; + std::printf("(news: %zu events: %s)\n", events ? events->size() : 0, summary.c_str()); + check(s(at(news, "messageType")) == "snapshot" && events && ids.size() == events->size(), "news with unique ids"); + check(cli::dump_json(at(news, "data")) == cli::dump_json(at(again, "data")), "news the same on every read"); + check(types["match.result"] > 0 && types["league.position"] > 0 && facts, "match results and the season's end in the news"); + const J few = session.ui_read(O{{"kind", "news"}, {"limit", 3}}, rev); + check(at(at(few, "data"), "events").array() && at(at(few, "data"), "events").array()->size() == 3, "news limit"); + } // almanac view after the kickoffs: index, calendar, day, match { const J index = session.read_almanac(O{{"kind", "index"}});