// SPDX-License-Identifier: GPL-3.0-only bool App::live_open() { if (!source_) { message("Für diese Ansicht steht keine Live-Datenquelle bereit."); return false; } const auto snapshot = snap(); if (!live_.available() || live_source_ != source_.get() || live_world_ != snapshot.world || live_.fixture().at("source_revision") != snapshot.revision) { const auto fixture = source_->read_live({{"kind", "fixture"}, {"revision", snapshot.revision}}); const auto owner_id = "manager:" + std::to_string(demo_career_manager(snapshot.state).value("portrait", 0)); if (!fixture.value("ok", false) || !live_.open(fixture, owner_id)) { message("Live-Ticker nicht verfügbar: " + fixture.value("error", "invalid_live_fixture")); return false; } live_source_ = source_.get(); live_world_ = snapshot.world; postmatch_data_=Json::object();postmatch_scrolls_=Json::object();postmatch_playing_=false; live_drafts_ = Json::object(); live_dialog_.clear(); live_dialog_draft_ = Json(); live_coverage_ = "all"; live_filter_ = "all"; live_tab_ = "team"; live_selected_match_ = fixture.at("matches").front().at("id"); live_ticker_scroll_ = live_detail_scroll_ = 0; live_reset_ticker_ = live_reset_detail_ = true; live_feedback_.clear(); live_resume_speed_ = 0; } live_review_ = false; live_speed_dragging_ = false; live_last_tick_ = SDL_GetTicks64(); scene_ = "live"; menu_.clear(); if(live_.finished())return postmatch_open(); focus_after_render_ = "live-next"; refresh(); return true; } void App::live_close() { live_review_=false;postmatch_playing_=false; live_.speed(0); live_dialog_.clear(); live_dialog_draft_ = Json(); live_resume_speed_ = 0; live_speed_dragging_ = false; scene_ = "desktop"; menu_.clear(); focus_after_render_ = "object-app-kickoff"; refresh(); } void App::live_set_speed(int value) { if (!live_.speed(value)) return; live_feedback_.clear(); live_last_tick_ = SDL_GetTicks64(); if (auto *node = dynamic_cast(element("live-speed"))) { // Native vertical sliders increase downward; the user's logical speed is // deliberately inverted so the bottom position always means zero/pause. node->SetValue(std::to_string(4 - value)); node->SetAttribute("aria-valuenow", value); node->SetAttribute("aria-valuetext", value ? "Wiedergabe Stufe " + std::to_string(value) : "Pause"); } if (auto *node = element("live-speed-value")) node->SetInnerRML(esc(value ? std::to_string(value) + "×" : "Pause")); if (auto *node = element("live-pause")) node->SetInnerRML(live_text(value ? "Pause" : "Abspielen", "control-label")); const auto &frame = live_.fixture().at("frames")[live_.sequence()]; const std::string phase = live_.finished() ? "Beendet" : frame.at("phase") == "interval" ? "Halbzeit" : value ? "Aufzeichnung läuft" : "Gemeinsam pausiert"; if (auto *node = element("live-phase-status")) node->SetInnerRML(esc(phase)); if (auto *node = element("live-playback-status")) node->SetInnerRML(esc(phase + " · " + std::to_string(frame.at("minute").get()) + "′ · keine Spielwertänderung")); dirty_ = true; } void App::live_tick() { if (scene_ != "live" || !live_.available()) return; const auto now = SDL_GetTicks64(), elapsed = now - live_last_tick_; live_last_tick_ = now; if(live_review_){postmatch_tick(now);return;} if (!popup_.empty() || !live_dialog_.empty() || live_speed_dragging_ || live_review_) return; const auto current = source_ ? source_->snapshot() : Snapshot(); if (!source_ || source_.get() != live_source_ || current.world != live_world_ || current.revision != live_.fixture().at("source_revision").get()) { live_.speed(0); if (live_feedback_ != "Die Quelle hat sich geändert. Diese Aufzeichnung bleibt pausiert; öffne Anstoß erneut.") { live_feedback_ = "Die Quelle hat sich geändert. Diese Aufzeichnung bleibt pausiert; öffne Anstoß erneut."; needs_render_ = true; } return; } if (live_.elapse(elapsed)) needs_render_ = true; } void App::live_dialog_close(bool apply_draft) { if (live_dialog_.empty()) return; if (apply_draft) { auto proposed = live_dialog_draft_; if (live_dialog_ == "lineup") { const auto owner = live_owner(); const auto error = edit_demo_lineup(proposed, {{"kind", "swap"}, {"a", live_out_}, {"b", live_in_}}, *live_world_, owner.at("view").at("club_basis")); if (!error.empty()) { live_feedback_ = "Die Demovorschau konnte nicht geändert werden: " + error; refresh(); return; } } live_drafts_[live_.owner()] = std::move(proposed); live_feedback_ = "Lokale Demovorschau geändert. Spielverlauf und Quelle bleiben unverändert."; } const auto was = live_dialog_; live_dialog_.clear(); live_dialog_draft_ = Json(); live_out_.clear(); live_in_.clear(); live_speed_dragging_ = false; const int resume = live_resume_speed_; live_resume_speed_ = 0; if (resume && !live_.finished()) live_.speed(resume); live_last_tick_ = SDL_GetTicks64(); focus_after_render_ = was == "lineup" ? "live-lineup-edit" : "live-tactics-edit"; refresh(); } bool App::live_action(const std::string &action, Rml::Element *target) { if(action.starts_with("postmatch-"))return postmatch_action(action,target); if (action == "open:kickoff") { live_open(); return true; } if (!action.starts_with("live-")) return false; if (scene_ != "live" || !live_.available()) return true; const auto id = target ? target->GetAttribute("data-id", "") : ""; bool detail_only = false, ticker_only = false; if (!live_dialog_.empty() && action != "live-dialog-cancel" && action != "live-dialog-apply" && action != "live-tactic-cycle") return true; if (action == "live-back") { live_close(); return true; } if (action == "live-owner-prev" || action == "live-owner-next") { if (!live_.cycle_owner(action == "live-owner-prev" ? -1 : 1)) return true; live_reset_detail_ = detail_only = true; } else if (action == "live-tab") { if (id != "team" && id != "tactics" && id != "opponent" && id != "table" && id != "stats") return true; if (live_tab_ == id) return true; live_tab_ = id; live_reset_detail_ = detail_only = true; } else if (action == "live-filter") { if (id != "all" && id != "highlights" && id != "goal" && id != "yellow") return true; if (live_filter_ == id) return true; live_filter_ = id; live_reset_ticker_ = ticker_only = true; } else if (action == "live-coverage") { if (id != "all" && id != "single" && id != "conference") return true; live_coverage_ = id; live_reset_ticker_ = true; } else if (action == "live-match") { for (const auto &match : live_.fixture().at("matches")) if (match.at("id") == id) { live_selected_match_ = id; live_reset_ticker_ = true; break; } } else if (action == "live-match-prev" || action == "live-match-next") { const auto &matches = live_.fixture().at("matches"); for (size_t i = 0; i < matches.size(); ++i) if (matches[i].at("id") == live_selected_match_) { live_selected_match_ = matches[(i + matches.size() + (action == "live-match-prev" ? -1 : 1)) % matches.size()].at("id"); break; } live_reset_ticker_ = true; } else if (action == "live-next") { live_.next(); live_last_tick_ = SDL_GetTicks64(); } else if (action == "live-pause") { live_set_speed(live_.speed() ? 0 : 1); return true; } else if (action == "live-review" && live_.finished()) { postmatch_open();return true; } else if (action == "live-review-back") { postmatch_action("postmatch-ticker",target);return true; } else if (action == "live-intervene-lineup" || action == "live-intervene-tactics") { if (live_.finished()) return true; const auto owner = live_owner(); live_dialog_draft_ = live_owner_state(owner); live_dialog_ = action == "live-intervene-lineup" ? "lineup" : "tactics"; live_resume_speed_ = live_.speed(); live_.speed(0); live_last_tick_ = SDL_GetTicks64(); live_out_.clear(); live_in_.clear(); const auto rows = live_owner_rows(owner); const size_t count = live_.fixture().at("field_count"); if (!rows.empty()) live_out_ = rows[0].at("id"); if (rows.size() > count) live_in_ = rows[count].at("id"); focus_after_render_ = "live-dialog-cancel"; } else if (action == "live-dialog-cancel" || action == "live-dialog-apply") { live_dialog_close(action == "live-dialog-apply"); return true; } else if (action == "live-tactic-cycle" && live_dialog_ == "tactics") { try { const int index = std::stoi(id); if (index < 0 || index > 7) return true; const auto error = edit_demo_tactics(live_dialog_draft_["tactics"], {{"kind", "cycle"}, {"index", index}}); if (!error.empty()) return true; } catch (const std::exception &) { return true; } } else return true; if (target && focus_after_render_.empty()) focus_after_render_ = target->GetId(); if (detail_only || ticker_only) { live_detail_pending_ = live_detail_pending_ || detail_only; live_ticker_pending_ = live_ticker_pending_ || ticker_only; dirty_ = true; return true; } refresh(); return true; } bool App::live_event(Rml::Event &event) { if(live_review_)return postmatch_event(event); if (scene_ != "live" || !live_.available() || !popup_.empty()) return false; const auto type = event.GetType(); auto *target = event.GetTargetElement(); bool speed_control = false; for (auto *node = target; node && node != doc_; node = node->GetParentNode()) if (node->GetId() == "live-speed") speed_control = true; if (type == "mousedown" && speed_control) live_speed_dragging_ = true; if (type == "mouseup" && live_speed_dragging_) { live_speed_dragging_ = false; live_last_tick_ = SDL_GetTicks64(); focus_after_render_ = "live-speed"; dirty_ = true; } if ((type == "change" || type == "input") && speed_control) { if (setting_menu_markup_) return true; if (auto *input = dynamic_cast(element("live-speed"))) { try { live_set_speed(4 - std::stoi(input->GetValue())); } catch (const std::exception &) {} } return true; } if ((type == "change" || type == "input") && !live_dialog_.empty()) { if (setting_menu_markup_) return true; if (target->GetId() == "live-player-out") live_out_ = field("live-player-out"); if (target->GetId() == "live-player-in") live_in_ = field("live-player-in"); return true; } if (type == "mouseup" && event.GetParameter("button", 0) == 1) { if (!live_dialog_.empty()) live_dialog_close(false); else live_close(); event.StopImmediatePropagation(); return true; } if (type == "keydown") { const auto key = Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)); if (key == Rml::Input::KI_ESCAPE) { if (!live_dialog_.empty()) live_dialog_close(false); else live_close(); event.StopImmediatePropagation(); return true; } if (key == Rml::Input::KI_TAB) { Rml::ElementList controls; doc_->QuerySelectorAll(controls, live_dialog_.empty() ? "#live-stage button,#live-stage input" : "#live-dialog button,#live-dialog select"); Rml::ElementList visible; for (auto *node : controls) if (node->IsVisible(true) && !node->HasAttribute("disabled")) visible.push_back(node); if (!visible.empty()) { auto at = std::find(visible.begin(), visible.end(), context_->GetFocusElement()); int index = at == visible.end() ? -1 : int(at - visible.begin()); const bool reverse = event.GetParameter("shift_key", 0) != 0; index = (index + int(visible.size()) + (reverse ? -1 : 1)) % int(visible.size()); visible[size_t(index)]->Focus(true); } event.StopImmediatePropagation(); return true; } if (live_dialog_.empty() && key == Rml::Input::KI_P && !live_.finished()) { live_set_speed(live_.speed() ? 0 : 1); event.StopImmediatePropagation(); return true; } if (speed_control && (key == Rml::Input::KI_HOME || key == Rml::Input::KI_END || key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN)) { live_set_speed(key == Rml::Input::KI_HOME ? 0 : key == Rml::Input::KI_END ? 4 : std::clamp(live_.speed() + (key == Rml::Input::KI_UP ? 1 : -1), 0, 4)); event.StopImmediatePropagation(); return true; } } if (type == "click" && live_dialog_.empty()) for (auto *node = target; node && node != doc_; node = node->GetParentNode()) if (node->HasAttribute("data-live-match")) { live_selected_match_ = node->GetAttribute("data-live-match", ""); if (live_coverage_ == "single") live_reset_ticker_ = true; refresh(); return true; } return false; } Json App::live_snapshot() const { auto status = live_.status(); status["tab"] = live_tab_; status["coverage"] = live_coverage_; status["selected_match"] = live_selected_match_; status["filter"] = live_filter_; status["review"] = live_review_; status["postmatch"] = postmatch_snapshot(); status["dialog"] = live_dialog_; status["drafts"] = live_drafts_; status["dialog_draft"] = live_dialog_draft_; status["ticker_scroll"] = element("live-ticker") ? element("live-ticker")->GetScrollTop() : live_ticker_scroll_; status["frame"] = live_.frame(); // Diagnostic counts describe actual DOM updates, not hardware timing claims. status["ui_updates"] = {{"document", document_generation_}, {"detail", live_detail_updates_}, {"ticker", live_ticker_updates_}}; if (live_.available()) { status["owners"] = live_.fixture().at("owners"); status["matches"] = live_.fixture().at("matches"); } return status; }