diff --git a/docs/new_engine/ui-anbindung.md b/docs/new_engine/ui-anbindung.md index 716a7e9..98d44f3 100644 --- a/docs/new_engine/ui-anbindung.md +++ b/docs/new_engine/ui-anbindung.md @@ -35,6 +35,13 @@ Revision kommt vom Aufrufer (Konsole bzw. AppController), der Read ändert nicht Fehler: `unknown_read`, `manager_not_active`, `no_active_manager`, `unknown_team`. +## Almanach und Statistik (ohne Umschlag) + +Die UI ruft diese über `DataSource::read_almanac` und `read_statistics` direkt auf; Antwort `{ok, ...}` wie in ihrer Demo. + +- `ModernSession::read_almanac`: `index` (Jahre, Anzahl, erstes/letztes Datum), `calendar {year}` (Spieltage mit Anzahl und Höhepunkten), `day {date}` (alle Spiele des Tages), `match {id}` (`match:-`). Ein Spiel trägt Datum, Saison, Wettbewerb (`league`/`cup`/`friendly`), Runde, beide Seiten (mit Manager, wenn menschlich), Ergebnis getrennt nach regulärer Zeit, Verlängerung (über die Torminuten) und Elfmeterschießen, Schiedsrichter, Zuschauer, Wetter, Aufstellungen (unter elf Spielern `null`) und Ereignisse (`goal`, `miss`, `yellow`, `second_yellow`, `red`, `injury`, `substitution`, `shootout`). +- `ModernSession::read_statistics`: `competitions`, `winners`, `player_stats` ([Statistik](statistik.md)). **Offen:** Der Statistikbildschirm der UI fragt andere Arten ab (Kategorie, Bereich, Verlaufskurven); der Abgleich folgt. + ## Commands `ModernSession::ui_command(request, revision)`. Ein Command (`messageType: command`) braucht `requestId`, `expectedRevision` (gleich der aktuellen Revision), `owner.managerId` (der Manager am Schreibtisch), `kind` und `payload`; `careerId` wird geprüft, wenn angegeben. Antwort: `command_result` mit `previousRevision`, `newRevision` (ein Schritt je Command), `outcome`, `changedReadKinds`, oder `error`. diff --git a/src/modern/app/modern_session.hpp b/src/modern/app/modern_session.hpp index be0095d..6c27181 100644 --- a/src/modern/app/modern_session.hpp +++ b/src/modern/app/modern_session.hpp @@ -831,6 +831,8 @@ public: // The game statistics menu (docs/new_engine/statistik.md): winners by season, player // statistics per competition. Read-only; answers {ok, ...} or {ok: false, error}. cli::Json read_statistics(const cli::Json& request) const { return query_statistics(g_, request); } + // The almanac screen (index, calendar, day, match; docs/new_engine/ui-anbindung.md). Read-only. + cli::Json read_almanac(const cli::Json& request) const { return query_almanac(g_, request); } // The read queries of the new interface (docs/new_engine/ui-abfragen.md): status, leagues, // table, fixtures, squad and the statistics, for the manager at the desk. Read-only. cli::Json read(const cli::Json& request) const { diff --git a/src/modern/content/statistics.cpp b/src/modern/content/statistics.cpp index 05cf2c4..3acb242 100644 --- a/src/modern/content/statistics.cpp +++ b/src/modern/content/statistics.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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(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(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 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> 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 diff --git a/src/modern/content/statistics.hpp b/src/modern/content/statistics.hpp index ef17858..b3d5dd3 100644 --- a/src/modern/content/statistics.hpp +++ b/src/modern/content/statistics.hpp @@ -19,4 +19,13 @@ namespace bmh::modern { cli::Json query_statistics(const ModernState& g, const cli::Json& request); +// The almanac screen of the Modern UI (DataSource::read_almanac), the request kinds of its demo: +// {kind: "index"} years with matches, their number, first and last date +// {kind: "calendar", year} the match days of a year with counts and highlights +// {kind: "day", date} all matches of a day (ISO date) +// {kind: "match", id} one match ("match:-") +// A match: date, season, competition, round, both sides (manager if human), result split into +// regulation / extra time / penalties, referee, attendance, weather, line-ups, events. +cli::Json query_almanac(const ModernState& g, const cli::Json& request); + } // namespace bmh::modern diff --git a/tests/modern_ui_contract_tests.cpp b/tests/modern_ui_contract_tests.cpp index 07c9f89..4930c19 100644 --- a/tests/modern_ui_contract_tests.cpp +++ b/tests/modern_ui_contract_tests.cpp @@ -248,6 +248,28 @@ int main(int argc, char** argv) { const std::string date_after = s(at(at(session.ui_read(O{{"kind", "desktop"}}, rev), "meta"), "date")); check(date_after > date_before, "the date moved on"); } + // almanac view after the kickoffs: index, calendar, day, match + { + const J index = session.read_almanac(O{{"kind", "index"}}); + const auto* years = at(index, "years").array(); + check(truth(at(index, "ok")) && years && !years->empty() && *at(index, "match_count").integer() > 0, "almanac index"); + const J cal = years && !years->empty() ? session.read_almanac(O{{"kind", "calendar"}, {"year", (*years)[0]}}) : J{}; + const auto* days = at(cal, "days").array(); + check(days && !days->empty(), "almanac calendar"); + const J day = days && !days->empty() ? session.read_almanac(O{{"kind", "day"}, {"date", at((*days)[0], "date")}}) : J{}; + const auto* matches = at(day, "matches").array(); + check(matches && !matches->empty(), "almanac day"); + if (matches && !matches->empty()) { + const J& m = (*matches)[0]; + const auto* final_score = at(at(m, "result"), "final").array(); + check(final_score && final_score->size() == 2 && at(at(m, "home"), "id").string() && + at(m, "events").array(), "almanac match form"); + const J one = session.read_almanac(O{{"kind", "match"}, {"id", at(m, "id")}}); + check(truth(at(one, "ok")) && s(at(at(one, "match"), "id")) == s(at(m, "id")), "almanac match by id"); + } + check(s(at(session.read_almanac(O{{"kind", "match"}, {"id", "match:0-0"}}), "error")) == "almanac_match_not_found", + "unknown match"); + } } catch (const std::exception& ex) { std::fprintf(stderr, "FAIL: %s\n", ex.what()); return 1;