diff --git a/docs/new_engine/ui-anbindung.md b/docs/new_engine/ui-anbindung.md index 53a3b25..79ce43a 100644 --- a/docs/new_engine/ui-anbindung.md +++ b/docs/new_engine/ui-anbindung.md @@ -49,6 +49,16 @@ Die UI ruft diese über `DataSource::read_almanac` und `read_statistics` direkt - `ModernSession::read_statistics_screen`: der Statistikbildschirm mit den Filtern seiner Demo (`section` 0–8, `category`, `country`, `winner_country`, `absolute`, `curves`), `fictional: false`. Aus dem Almanach: 3 Ewige Tabelle (je Land die erste Liga, 5 die eigene), 4 WM-Statistiken (Sieger mit Finale, Torschützen, Länder-Historie), 6 Serien und Rekorde (eigene Liga), 7 Sieger (national, Europapokale, Hallen-Masters), 8 Minuten-Statistik (eigener Verein). Aus den Aufzeichnungen des Originals: 1 Transferhistorie (Käufe, Verkäufe, Leihen des Managers mit Ablöse) und 2 Analytics: die sechs Kurven des Originals mit ihren Namen, `values` je erfasster Woche und `absoluteMin`/`absoluteMax`; **die UI skaliert selbst** (`points` bleibt leer, die Demo lieferte Bildschirmkoordinaten). Noch ohne Aufzeichnung (`available: false`, `reason: not_recorded`): 0 Möglichkeiten, 5 Saisonfinanzen (das Original speichert nur die laufende Saison), bei 4 Gruppen und Finalrunde. - `ModernSession::read_statistics`: `competitions`, `winners`, `player_stats` ([Statistik](statistik.md)) für eine eigene Spieler- und Siegerstatistik. +## Live-Ticker (ohne Umschlag) + +`ModernSession::read_live` (für `DataSource::read_live`, Abfragen wie die Demo: `{kind: "fixture"}`, `{kind: "frame", sequence}`, optional `revision`, `fixture_id`). Die Engine rechnet einen Spieltag beim Anstoß im Ganzen; der Ticker spielt die **Aufzeichnung** der Spiele des letzten `session.advance` ab (`source: "engine-recording"`, Format `openbmh-live-fixture@1` der Demo): + +- `matches`: die Spiele mit menschlichem Manager (höchstens vier), `owners`: die Manager mit Team und `match_id` (`view` ist `null`; die Aufstellung kommt aus dem `lineup`-Read). +- `frames` in Minutenfolge: Anpfiff, jedes Ereignis (Tor, Chance, Karten, Verletzung, Wechsel, Elfmeter), Halbzeit, Wiederanpfiff, Abpfiff; je Spiel Stand, Chancen, Gelb, Rot, Elfmeter, Spieler auf dem Feld. `events` (fortlaufend) mit Überschrift und Satz, Spieler, Seite, Stand. +- `conference`: die übrigen Spiele des Tages im Almanach-Format. +- Noch leer: `standings` (Live-Tabelle). Fehler: `live_unavailable` (noch kein Anstoß), `live_no_human_match`, `stale_live_revision`, `stale_live_fixture`, `invalid_live_sequence`. +- Eingriffe während des Spiels (Wechsel, Taktik) brauchen einen schrittweisen Anstoß und folgen mit Lieferung E. + ## 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.cpp b/src/modern/app/modern_session.cpp index 51c718f..63894e9 100644 --- a/src/modern/app/modern_session.cpp +++ b/src/modern/app/modern_session.cpp @@ -8752,6 +8752,29 @@ std::string ui_text(const cli::Json& j, const char* key) { } // namespace +cli::Json ModernSession::read_live(const cli::Json& request) const { + using O = cli::Json::Object; + const auto fail = [](const char* e) { return cli::Json(O{{"ok", false}, {"error", e}}); }; + if (!request.object()) return fail("invalid_live_query"); + for (const auto& [key, value] : *request.object()) + if (key != "kind" && key != "revision" && key != "sequence" && key != "fixture_id") return fail("invalid_live_query"); + if (!ui_last_advance_) return fail("live_unavailable"); + if (const cli::Json* r = ui_field(request, "revision"); r && (!r->integer() || uint64_t(*r->integer()) != ui_last_advance_revision_)) + return fail("stale_live_revision"); + cli::Json fixture = modern::live_fixture(g_, ui_last_advance_->first, ui_last_advance_->second, ui_last_advance_revision_); + const auto* f = std::get_if(&fixture.value); + if (!f || !std::holds_alternative(f->at("ok").value) || !std::get(f->at("ok").value)) return fixture; + if (const cli::Json* id = ui_field(request, "fixture_id"); id && (!id->string() || *id->string() != *f->at("fixture_id").string())) + return fail("stale_live_fixture"); + const std::string kind = ui_text(request, "kind").empty() ? "fixture" : ui_text(request, "kind"); + if (kind == "fixture") return ui_field(request, "sequence") ? fail("invalid_live_query") : fixture; + if (kind != "frame") return fail("invalid_live_query"); + const cli::Json* seq = ui_field(request, "sequence"); + const auto& frames = *f->at("frames").array(); + if (!seq || !seq->integer() || *seq->integer() < 0 || size_t(*seq->integer()) >= frames.size()) return fail("invalid_live_sequence"); + return O{{"ok", true}, {"fixture_id", f->at("fixture_id")}, {"frame", frames[size_t(*seq->integer())]}}; +} + cli::Json ModernSession::read_statistics_screen(const cli::Json& request) const { using O = cli::Json::Object; using A = cli::Json::Array; @@ -9337,6 +9360,8 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision } modern::ensure_ui_identities(g_); revision = previous + 1; + ui_last_advance_ = std::make_pair(saved_state.date, g_.date); // what read_live replays + ui_last_advance_revision_ = revision; const char* what = r.kind == TurnDeskKind::SeasonEndDesk ? "Saisonende." : r.kind == TurnDeskKind::CareerComplete ? "Die Karriere ist zu Ende." : "Weiter zum nächsten Termin."; answer = modern::ui_command_result(request_id, previous, revision, diff --git a/src/modern/app/modern_session.hpp b/src/modern/app/modern_session.hpp index c645b18..f59d774 100644 --- a/src/modern/app/modern_session.hpp +++ b/src/modern/app/modern_session.hpp @@ -834,6 +834,9 @@ public: // 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 statistics screen for the manager at the desk (docs/new_engine/ui-anbindung.md). Read-only. + // The live ticker (DataSource::read_live): {kind: "fixture"} the recorded matches of the last + // session.advance, {kind: "frame", sequence} one of its frames; revision/fixture_id as in the demo. + cli::Json read_live(const cli::Json& request) const; // Sections 1 (transfer history) and 2 (the six curves) come from the original's own records. cli::Json read_statistics_screen(const cli::Json& request) const; // The read queries of the new interface (docs/new_engine/ui-abfragen.md): status, leagues, @@ -1001,6 +1004,9 @@ private: // ui_command: answers by requestId (the command as JSON, the answer) for repeated commands, // the last 64 in this process; after a restart expectedRevision prevents a second booking. std::map> ui_answers_; + // the day counters around the last session.advance and the revision after it (read_live) + std::optional> ui_last_advance_; + uint64_t ui_last_advance_revision_ = 0; std::vector ui_answer_order_; std::filesystem::path data_directory_; diff --git a/src/modern/content/statistics.cpp b/src/modern/content/statistics.cpp index d0ca9f4..5f3f335 100644 --- a/src/modern/content/statistics.cpp +++ b/src/modern/content/statistics.cpp @@ -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 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 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 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 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(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(std::get(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 diff --git a/src/modern/content/statistics.hpp b/src/modern/content/statistics.hpp index 5485fda..bcc6aeb 100644 --- a/src/modern/content/statistics.hpp +++ b/src/modern/content/statistics.hpp @@ -35,4 +35,11 @@ cli::Json query_almanac(const ModernState& g, const cli::Json& request); // not_recorded (title chances, transfers, weekly curves, season finances). cli::Json query_statistics_screen(const ModernState& g, int manager, const cli::Json& request); +// The live ticker (DataSource::read_live): the matches recorded between two day counters (the last +// kickoff) in the fixture form of the interface: the matches of human managers (at most four) +// with their owners, frames in minute order (kickoff, every event, half time, the end) with +// score, chances, cards and players per match, the event lines, and the other matches of the +// 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); + } // namespace bmh::modern diff --git a/tests/modern_ui_contract_tests.cpp b/tests/modern_ui_contract_tests.cpp index 34aa7fd..a2a86d0 100644 --- a/tests/modern_ui_contract_tests.cpp +++ b/tests/modern_ui_contract_tests.cpp @@ -424,6 +424,31 @@ 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 live ticker: advance until a kickoff played a match of the human manager, then its recording + { + J fixture; + for (int k = 0; k < 12; ++k) { + fixture = session.read_live(O{{"kind", "fixture"}}); + if (truth(at(fixture, "ok"))) break; + check(s(at(fixture, "error")) == "live_no_human_match", "no human match: said so"); + session.ui_command(cmd("request:lv" + std::to_string(k), "session.advance", O{}), rev); + } + const auto* frames = at(fixture, "frames").array(); + const auto* matches = at(fixture, "matches").array(); + check(truth(at(fixture, "ok")) && s(at(fixture, "source")) == "engine-recording" && frames && frames->size() >= 4 && + matches && !matches->empty() && at(fixture, "owners").array() && !at(fixture, "owners").array()->empty(), + "live fixture of the last kickoff"); + if (frames && frames->size() >= 4) { + const J& last = frames->back(); + const J one = session.read_live(O{{"kind", "frame"}, {"sequence", int64_t(frames->size() - 1)}}); + check(truth(at(one, "ok")) && cli::dump_json(at(one, "frame")) == cli::dump_json(last), "one frame by sequence"); + // the last frame's score is the recorded result of the match + const J m = at(at(last, "matches").array()->front(), "score"); + std::printf("(live: %zu frames, %zu matches, conference %zu, final %s)\n", frames->size(), matches->size(), + at(fixture, "conference").array() ? at(fixture, "conference").array()->size() : 0, cli::dump_json(m).c_str()); + } + check(s(at(session.read_live(O{{"kind", "frame"}, {"sequence", 99999}}), "error")) == "invalid_live_sequence", "bad sequence"); + } // almanac view after the kickoffs: index, calendar, day, match { const J index = session.read_almanac(O{{"kind", "index"}});