Files
Dreierpack-Manager/src/modern/ui/postmatch_controls.inc
T
MichaelandClaude Fable 5.2 1732a2477b Liven up the match day: varied ticker wording, whistles, results of the day, eleven of the day
The ticker picks its sentence from several variants by the event's facts
and names equalisers, leads and late goals; kickoff, half time and the
final whistle are reported, with the same wording while the kickoff runs
live. The ticker's third tab lists the other results of the day. Modern
chooses an eleven of the day per league from the recorded match facts and
the review shows it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 11:34:47 +01:00

472 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-only
bool App::postmatch_open() {
if (!live_.available() || !live_.finished())
return false;
auto data = make_demo_post_match(live_.fixture(), live_.sequence());
if (!data.value("ok", false)) {
message("Die Auswertung dieser Aufnahme ist nicht verfügbar.");
return false;
}
if (live_.fixture().at("source") == "engine-recording") {
// the eleven of the day as the engine chose it for this league
const auto records = engine_post_match_records(engine_postmatch_read_, live_.fixture());
if (records.is_object()) {
auto with_eleven = make_demo_post_match(live_.fixture(), live_.sequence(), records);
if (with_eleven.value("ok", false)) data = std::move(with_eleven);
}
}
if (live_.fixture().at("source") != "engine-recording") try {
const auto current = snap();
if (source_.get() != live_source_ || current.world != live_world_ ||
current.revision != live_.fixture().at("source_revision")) {
message("Die Quelle dieser Aufnahme hat sich geändert. Eine neue "
"Auswertung wird nicht angenommen.");
return false;
}
const auto reply = source_->read_post_match(
{{"kind", "review"},
{"fixture_id", live_.fixture().at("fixture_id")},
{"revision", current.revision},
{"completed_sequence", live_.sequence()}});
if (reply.is_object() && reply.value("ok", false)) {
for (const auto *key :
{"schema", "source", "fixture_id", "source_revision",
"completed_sequence", "world", "role", "surface", "duration",
"owners", "tabs", "matches", "tables", "overview"})
if (!reply.contains(key) || reply.at(key) != data.at(key))
throw editor::Error("incompatible_review_reply");
if (reply.at("kind") != "review")
throw editor::Error("invalid_review_kind");
const auto &eleven = reply.at("eleven");
if (eleven.is_object() && eleven.value("available", false)) {
auto checked = make_demo_post_match(
live_.fixture(), live_.sequence(),
{{"schema", "openbmh-post-match-records@1"},
{"fixture_id", data.at("fixture_id")},
{"source_revision", data.at("source_revision")},
{"eleven", eleven}});
if (!checked.value("ok", false))
throw editor::Error("invalid_review_records");
data = std::move(checked);
} else if (eleven != data.at("eleven"))
throw editor::Error("invalid_unavailable_review");
} else if (!(reply.is_object() &&
reply.value("error", "") == "post_match_unavailable" &&
live_.fixture().value("source", "") == "ui-demo-fixture"))
throw editor::Error("review_source_unavailable");
} catch (const std::exception &) {
message("Die Auswertungsdaten sind veraltet, unvollständig oder nicht "
"verfügbar. Die gehaltene Aufnahme bleibt unverändert.");
return false;
}
if (!postmatch_data_.value("ok", false) ||
postmatch_data_.at("fixture_id") != data.at("fixture_id")) {
postmatch_data_ = data;
postmatch_tab_ = "results";
postmatch_owner_ = live_.owner();
postmatch_match_ = live_selected_match_;
if (std::none_of(postmatch_data_.at("matches").begin(),
postmatch_data_.at("matches").end(), [&](const auto &m) {
return m.at("id") == postmatch_match_;
}))
postmatch_match_ = postmatch_data_.at("matches")[0].at("id");
for (const auto &g : postmatch_groups())
if (std::find(g.at("match_ids").begin(), g.at("match_ids").end(),
Json(postmatch_match_)) != g.at("match_ids").end()) {
postmatch_country_ = g.at("country");
postmatch_league_ = g.at("id");
break;
}
postmatch_minute_ = postmatch_data_.at("duration");
postmatch_scrolls_ = Json::object();
postmatch_rendered_scope_.clear();
}
postmatch_data_ = std::move(data);
if (auto *n = element("live-ticker"))
live_ticker_scroll_ = n->GetScrollTop();
if (auto *n = element("live-detail-body"))
live_detail_scroll_ = n->GetScrollTop();
postmatch_playing_ = false;
postmatch_elapsed_ = 0;
postmatch_dialog_.clear();
live_review_ = true;
focus_after_render_ = "postmatch-tab-" + postmatch_tab_;
refresh();
return true;
}
void App::postmatch_seek(int minute, bool stop) {
if (!postmatch_data_.value("ok", false))
return;
if (stop) {
postmatch_playing_ = false;
postmatch_elapsed_ = 0;
}
postmatch_minute_ =
std::clamp(minute, 0, postmatch_data_.at("duration").get<int>());
postmatch_update_report();
}
void App::postmatch_update_report() {
if (!live_review_ || postmatch_tab_ != "report")
return;
const auto report = postmatch_report();
if (!report.value("ok", false))
return;
const auto &score = report.at("score");
if (auto *n = element("postmatch-score"))
n->SetInnerRML(live_text(std::to_string(score[0].get<int>())) +
live_text(":", "separator") +
live_text(std::to_string(score[1].get<int>())));
if (auto *n = element("postmatch-events")) {
const float scroll = n->GetScrollTop();
n->SetInnerRML(postmatch_events(report, "home") +
postmatch_events(report, "away"));
context_->Update();
n->SetScrollTop(scroll);
}
size_t index = 0;
for (const auto &event : report.at("timeline")) {
if (auto *n = element("postmatch-marker-" + std::to_string(index)))
n->SetClass("is-visible", event.at("sequence") <= report.at("sequence"));
++index;
}
postmatch_updating_ = true;
if (auto *n =
dynamic_cast<Rml::ElementFormControl *>(element("postmatch-minute")))
n->SetValue(std::to_string(postmatch_minute_));
postmatch_updating_ = false;
if (auto *n = element("postmatch-minute-label"))
n->SetInnerRML(std::to_string(postmatch_minute_) + "′");
if (auto *n = element("postmatch-recorded-note"))
n->SetInnerRML(
live_text("Aufgezeichneter Stand: " +
std::to_string(report.at("recorded_minute").get<int>()) +
"′ · keine Zwischenberechnung"));
if (auto *n = element("postmatch-play")) {
n->SetInnerRML(live_text(
postmatch_playing_ ? "Pause" : "Rückblick abspielen", "control-label"));
n->SetAttribute("aria-pressed", postmatch_playing_ ? "true" : "false");
}
dirty_ = true;
}
void App::postmatch_tick(uint64_t now) {
const auto elapsed = postmatch_last_tick_ ? now - postmatch_last_tick_ : 0;
postmatch_last_tick_ = now;
if (!live_review_ || !postmatch_playing_ || postmatch_tab_ != "report" ||
!popup_.empty() || !postmatch_dialog_.empty())
return;
const auto flags = SDL_GetWindowFlags(video_.window);
if (!(flags & SDL_WINDOW_INPUT_FOCUS) ||
(flags & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED))) {
postmatch_playing_ = false;
postmatch_update_report();
return;
}
postmatch_elapsed_ += std::min<uint64_t>(elapsed, 1000);
if (postmatch_elapsed_ >= 180) {
const int step = int(postmatch_elapsed_ / 180);
postmatch_elapsed_ %= 180;
const int duration = postmatch_data_.at("duration");
if (postmatch_minute_ + step >= duration)
postmatch_playing_ = false;
postmatch_seek(std::min(duration, postmatch_minute_ + step), false);
}
}
bool App::postmatch_action(const std::string &action, Rml::Element *target) {
if (!action.starts_with("postmatch-"))
return false;
if (!live_review_ || !postmatch_data_.value("ok", false) || !popup_.empty())
return true;
const auto id =
target ? target->GetAttribute<Rml::String>("data-id", "") : "";
if (!postmatch_dialog_.empty() && action != "postmatch-detail-close" &&
action != "postmatch-detail-report")
return true;
if (action == "postmatch-ticker") {
postmatch_playing_ = false;
live_review_ = false;
live_last_tick_ = SDL_GetTicks64();
focus_after_render_ = "live-review";
refresh();
return true;
}
if (action == "postmatch-desktop") {
postmatch_playing_ = false;
live_close();
return true;
}
if (action == "postmatch-detail-close") {
postmatch_dialog_.clear();
focus_after_render_ = "postmatch-heading";
refresh();
return true;
}
if (action == "postmatch-detail-report") {
for (const auto &m : postmatch_data_.at("matches"))
if (m.at("home").at("id") == postmatch_detail_team_ ||
m.at("away").at("id") == postmatch_detail_team_) {
postmatch_match_ = m.at("id");
for (const auto &g : postmatch_groups())
if (std::find(g.at("match_ids").begin(), g.at("match_ids").end(),
m.at("id")) != g.at("match_ids").end()) {
postmatch_country_ = g.at("country");
postmatch_league_ = g.at("id");
break;
}
postmatch_dialog_.clear();
postmatch_tab_ = "report";
postmatch_minute_ = postmatch_data_.at("duration");
postmatch_playing_ = false;
focus_after_render_ = "postmatch-heading";
refresh();
break;
}
return true;
}
if (action == "postmatch-player" || action == "postmatch-club") {
const auto &eleven = postmatch_data_.at("eleven");
if (eleven.value("available", false))
for (const auto &row : eleven.at("rows"))
if ((action == "postmatch-player" && row.at("player").at("id") == id) ||
(action == "postmatch-club" && row.at("team").at("id") == id)) {
postmatch_detail_team_ = row.at("team").at("id");
postmatch_detail_player_ =
action == "postmatch-player"
? row.at("player").at("id").get<std::string>()
: "";
postmatch_dialog_ = action == "postmatch-player" ? "player" : "club";
postmatch_playing_ = false;
focus_after_render_ = "postmatch-detail-close";
refresh();
break;
}
return true;
}
if (!postmatch_dialog_.empty())
return true;
const auto &tabs = postmatch_data_.at("tabs");
if (action == "postmatch-tab") {
if (std::find(tabs.begin(), tabs.end(), Json(id)) == tabs.end())
return true;
postmatch_tab_ = id;
postmatch_playing_ = false;
focus_after_render_ = "postmatch-tab-" + id;
} else if (action == "postmatch-prev" || action == "postmatch-next") {
const auto current =
std::find(tabs.begin(), tabs.end(), Json(postmatch_tab_));
int at =
int(current - tabs.begin()) + (action == "postmatch-next" ? 1 : -1);
if (at < 0)
return true;
if (at >= int(tabs.size())) {
postmatch_playing_ = false;
live_close();
return true;
}
postmatch_tab_ = tabs[size_t(at)];
postmatch_playing_ = false;
focus_after_render_ = "postmatch-heading";
} else if (action == "postmatch-fixture" ||
action == "postmatch-fixture-prev" ||
action == "postmatch-fixture-next") {
const auto matches = postmatch_matches();
std::string chosen = id;
if (action != "postmatch-fixture")
for (size_t i = 0; i < matches.size(); ++i)
if (matches[i].at("id") == postmatch_match_) {
const int next =
int(i) + (action == "postmatch-fixture-prev" ? -1 : 1);
if (next >= 0 && next < int(matches.size()))
chosen = matches[size_t(next)].at("id");
break;
}
if (std::none_of(matches.begin(), matches.end(),
[&](const auto &m) { return m.at("id") == chosen; }))
return true;
postmatch_match_ = chosen;
postmatch_tab_ = "report";
postmatch_minute_ = postmatch_data_.at("duration");
postmatch_playing_ = false;
focus_after_render_ = "postmatch-heading";
} else if (action == "postmatch-event") {
const auto report = postmatch_report();
for (const auto &e : report.at("timeline"))
if (e.at("id") == id) {
postmatch_seek(e.at("minute"));
break;
}
return true;
} else if (action == "postmatch-play") {
postmatch_playing_ = !postmatch_playing_;
postmatch_last_tick_ = SDL_GetTicks64();
postmatch_elapsed_ = 0;
if (postmatch_playing_ &&
postmatch_minute_ == postmatch_data_.at("duration"))
postmatch_minute_ = 0;
postmatch_update_report();
return true;
} else
return true;
refresh();
return true;
}
bool App::postmatch_event(Rml::Event &event) {
if (scene_ != "live" || !live_review_ || !menu_.empty() || !popup_.empty())
return false;
const auto type = event.GetType();
auto *target = event.GetTargetElement();
if ((type == "change" || type == "input") && target) {
if (postmatch_updating_ || setting_menu_markup_)
return true;
const auto id = target->GetId();
if (id == "postmatch-minute") {
try {
postmatch_seek(std::stoi(field(id)));
} catch (const std::exception &) {
}
return true;
}
if (id == "postmatch-country" || id == "postmatch-league") {
const auto value = field(id);
const auto groups = postmatch_groups();
for (const auto &g : groups)
if ((id == "postmatch-country" && g.at("country") == value) ||
(id == "postmatch-league" && g.at("id") == value &&
g.at("country") == postmatch_country_)) {
postmatch_country_ = g.at("country");
postmatch_league_ = g.at("id");
postmatch_match_ = g.at("match_ids")[0];
postmatch_minute_ = postmatch_data_.at("duration");
postmatch_playing_ = false;
focus_after_render_ = id;
refresh();
break;
}
return true;
}
}
bool minute_control = false;
for (auto *n = target; n && n != doc_; n = n->GetParentNode())
if (n->GetId() == "postmatch-minute")
minute_control = true;
const auto key = type == "keydown"
? Rml::Input::KeyIdentifier(
event.GetParameter<int>("key_identifier", 0))
: Rml::Input::KI_UNKNOWN;
if (minute_control &&
(key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT ||
key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN ||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
postmatch_seek(key == Rml::Input::KI_HOME ? 0
: key == Rml::Input::KI_END
? postmatch_data_.at("duration").get<int>()
: postmatch_minute_ + ((key == Rml::Input::KI_RIGHT ||
key == Rml::Input::KI_UP)
? 1
: -1));
event.StopImmediatePropagation();
return true;
}
if (key == Rml::Input::KI_ESCAPE ||
(type == "mouseup" && event.GetParameter<int>("button", 0) == 1)) {
postmatch_action(postmatch_dialog_.empty() ? "postmatch-ticker"
: "postmatch-detail-close",
nullptr);
event.StopImmediatePropagation();
return true;
}
if (key == Rml::Input::KI_TAB) {
Rml::ElementList rows;
doc_->QuerySelectorAll(rows,
postmatch_dialog_.empty()
? ".postmatch-window button,.postmatch-window "
"input,.postmatch-window select"
: "#postmatch-dialog-layer button");
std::erase_if(rows, [](auto *n) {
return !n->IsVisible(true) || n->HasAttribute("disabled") ||
n->template GetAttribute<Rml::String>("tabindex", "") == "-1";
});
if (!rows.empty()) {
auto at =
std::find(rows.begin(), rows.end(), context_->GetFocusElement());
const bool back = event.GetParameter<int>("shift_key", 0) != 0;
int index = at == rows.end() ? (back ? 0 : -1) : int(at - rows.begin());
rows[size_t((index + int(rows.size()) + (back ? -1 : 1)) %
int(rows.size()))]
->Focus(true);
}
event.StopImmediatePropagation();
return true;
}
if (postmatch_dialog_.empty() && target &&
target->GetId().starts_with("postmatch-tab-") &&
(key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT ||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
const auto &tabs = postmatch_data_.at("tabs");
const auto at = std::find(tabs.begin(), tabs.end(), Json(postmatch_tab_));
int index = key == Rml::Input::KI_HOME ? 0
: key == Rml::Input::KI_END
? int(tabs.size()) - 1
: (int(at - tabs.begin()) + int(tabs.size()) +
(key == Rml::Input::KI_RIGHT ? 1 : -1)) %
int(tabs.size());
postmatch_action(
"postmatch-tab",
element("postmatch-tab-" + tabs[size_t(index)].get<std::string>()));
event.StopImmediatePropagation();
return true;
}
if (type == "click")
for (auto *n = target; n && n != doc_; n = n->GetParentNode())
if (n->HasAttribute("data-action")) {
const auto action = n->GetAttribute<Rml::String>("data-action", "");
if (action.starts_with("postmatch-")) {
if (!n->HasAttribute("disabled"))
postmatch_action(action, n);
event.StopImmediatePropagation();
return true;
}
}
return false;
}
void App::postmatch_after_render() {
if (scene_ != "live" || !live_review_)
return;
fit_postmatch_pitch(*doc_);
if (postmatch_restore_scroll_) {
for (const auto *id : {"postmatch-results", "postmatch-events",
"postmatch-eleven-list", "postmatch-table"})
if (auto *n = element(id)) {
const auto key = postmatch_rendered_scope_ + "|" + id;
if (postmatch_scrolls_.contains(key))
n->SetScrollTop(postmatch_scrolls_.at(key).get<float>());
}
postmatch_restore_scroll_ = false;
}
if (!postmatch_dialog_.empty()) {
Rml::ElementList underlay;
doc_->QuerySelectorAll(underlay,
"#postmatch-underlay button,#postmatch-underlay "
"input,#postmatch-underlay select,#postmatch-close");
for (auto *n : underlay) {
n->SetProperty("pointer-events", "none");
n->SetAttribute("tabindex", -1);
}
}
Rml::ElementList behind;
doc_->QuerySelectorAll(behind,
".desktop-layer button,.desktop-layer [data-action]");
for (auto *n : behind) {
n->SetProperty("pointer-events", "none");
n->SetAttribute("tabindex", -1);
}
}
Json App::postmatch_snapshot() const {
if (!postmatch_data_.value("ok", false))
return Json();
return {{"tab", postmatch_tab_}, {"match_id", postmatch_match_},
{"minute", postmatch_minute_}, {"playing", postmatch_playing_},
{"dialog", postmatch_dialog_}, {"country", postmatch_country_},
{"league", postmatch_league_}, {"scroll", postmatch_scrolls_},
{"data", postmatch_data_}, {"report", postmatch_report()}};
}