In a live match the tactics board shows the attitude and the commitment with what they do; the play-pattern items have no effect on a match and are not offered there. When an own starter is injured the match stops and the strongest bench player of his position is offered as the change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
486 lines
28 KiB
C++
486 lines
28 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
||
namespace {
|
||
// A kickoff that reached a new season carries that season's draw for the opening view.
|
||
void engine_attach_opening(EngineBridge &link, const std::string &season_before, const Json &after, Json &response) {
|
||
try {
|
||
const std::string season_after = after.at("desktop").at("meta").at("season").at("id");
|
||
if (!season_before.empty() && season_after != season_before) response["seasonOpening"] = link.read({{"kind", "opening"}});
|
||
} catch (const std::exception &) {}
|
||
}
|
||
std::string engine_season_id(const Json &bundle) {
|
||
try { return bundle.at("desktop").at("meta").at("season").at("id"); } catch (const std::exception &) { return {}; }
|
||
}
|
||
} // namespace
|
||
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;
|
||
}
|
||
bool App::engine_live_open(const Json &recording) {
|
||
const auto fixture = engine_live_fixture(recording, engine_data_);
|
||
// after a kickoff that was followed live the ticker stays on the final whistle
|
||
const bool at_end = engine_live_at_end_ && scene_ == "live";
|
||
engine_live_at_end_ = false;
|
||
if (!fixture.value("ok", false) || !(at_end ? live_.follow(fixture) : live_.open(fixture, ""))) {
|
||
engine_notice_ = "Spieltag gespielt · Der Ticker kann diese Aufzeichnung nicht zeigen (" +
|
||
fixture.value("error", std::string("invalid_live_fixture")) + ").";
|
||
if (scene_ == "live") { scene_ = live_return_scene_; live_.close(); }
|
||
return false;
|
||
}
|
||
if (at_end) {
|
||
live_.speed(0); live_feedback_ = "Abpfiff."; live_reset_ticker_ = live_reset_detail_ = true;
|
||
live_selected_match_ = live_.fixture().at("matches").front().at("id");
|
||
for (const auto &owner : live_.fixture().at("owners")) if (owner.at("id") == live_.owner()) live_selected_match_ = owner.at("match_id");
|
||
live_dialog_.clear(); live_dialog_draft_ = Json(); postmatch_data_ = Json::object(); postmatch_scrolls_ = Json::object(); postmatch_playing_ = false;
|
||
focus_after_render_ = "live-review";
|
||
return true;
|
||
}
|
||
engine_live_enter();
|
||
live_.speed(2);
|
||
return true;
|
||
}
|
||
// The kickoff stands after its first minute: the ticker opens on it and plays on by live.step.
|
||
void App::engine_live_begin(const Json &state, const std::string &revision) {
|
||
engine_live_roster_ = engine_live_roster(engine_data_);
|
||
engine_live_revision_ = revision; engine_live_running_ = true; engine_live_busy_ = false;
|
||
engine_live_at_end_ = false; engine_live_elapsed_ = 0; engine_live_queue_ = Json(); engine_live_swap_ = Json();
|
||
engine_live_tactics_ = Json::array(); engine_live_tactics_sent_ = Json();
|
||
try {
|
||
for (const auto &control : engine_data_.at("tactics").at("data").at("controls"))
|
||
engine_live_tactics_.push_back({{"label", control.at("label").value("text", std::string("Taktik"))},
|
||
{"min", control.at("range").at("min")}, {"max", control.at("range").at("max")},
|
||
{"value", control.at("value")}});
|
||
} catch (const std::exception &) { engine_live_tactics_ = Json::array(); }
|
||
if (engine_live_tactics_.size() != 8) engine_live_tactics_ = Json::array();
|
||
try { engine_live_assistant_ = engine_data_.at("tactics").at("data").value("assistantAttitude", 0) != 0; }
|
||
catch (const std::exception &) { engine_live_assistant_ = false; }
|
||
const auto fixture = engine_live_progress(Json(), state, engine_data_, engine_live_roster_);
|
||
if (!fixture.value("ok", false) || !live_.open(fixture, "")) {
|
||
// nothing the ticker can show: the kickoff is simply played to its end
|
||
engine_notice_ = "Der Spieltag wird ohne Ticker zu Ende gespielt.";
|
||
engine_live_send("live.finish", Json::object());
|
||
return;
|
||
}
|
||
engine_live_enter();
|
||
live_.speed(2);
|
||
}
|
||
void App::engine_live_send(const std::string &kind, const Json &payload) {
|
||
if (!engine_live_running_ || !engine_link_) return;
|
||
if (engine_live_busy_) { engine_live_queue_ = {{"kind", kind}, {"payload", payload}}; return; }
|
||
const auto command = engine_live_command(engine_data_, engine_live_revision_, editor::new_id("request:ui-"), kind, payload);
|
||
auto link = engine_link_;
|
||
engine_live_busy_ = true;
|
||
engine_live_job_ = std::async(std::launch::async, [link, command, kind] {
|
||
Json result = {{"kind", kind}};
|
||
try {
|
||
result["response"] = link->command(command);
|
||
result["live"] = link->read_live({{"kind", "fixture"}});
|
||
} catch (const std::exception &e) { result["failure"] = e.what(); }
|
||
return result;
|
||
});
|
||
}
|
||
void App::engine_live_poll() {
|
||
if (!engine_live_busy_ || engine_live_job_.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) return;
|
||
engine_live_busy_ = false;
|
||
Json result;
|
||
try { result = engine_live_job_.get(); } catch (const std::exception &e) { result = {{"failure", e.what()}}; }
|
||
const std::string kind = result.value("kind", std::string());
|
||
const Json response = result.value("response", Json::object()), state = result.value("live", Json::object());
|
||
const bool accepted = response.value("messageType", "") == "command_result";
|
||
if (accepted) engine_live_revision_ = response.value("newRevision", engine_live_revision_);
|
||
else std::fprintf(stderr, "live command %s refused: %s%c", kind.c_str(), response.dump().c_str(), '\n');
|
||
if (kind == "live.substitute") {
|
||
if (accepted && engine_live_swap_.is_array() && engine_live_swap_.size() == 2) {
|
||
// the engine swapped the two rows of the line-up; the team tab follows
|
||
size_t a = engine_live_roster_.size(), b = engine_live_roster_.size();
|
||
for (size_t i = 0; i < engine_live_roster_.size(); ++i) {
|
||
if (engine_live_roster_[i].at("id") == engine_live_swap_[0]) a = i;
|
||
if (engine_live_roster_[i].at("id") == engine_live_swap_[1]) b = i;
|
||
}
|
||
if (a < engine_live_roster_.size() && b < engine_live_roster_.size()) {
|
||
std::swap(engine_live_roster_[a], engine_live_roster_[b]);
|
||
engine_live_roster_[a]["slot"] = a; engine_live_roster_[b]["slot"] = b;
|
||
}
|
||
live_feedback_ = "Wechsel ausgeführt.";
|
||
} else if (!accepted) {
|
||
const Json refusal = response.value("message", Json());
|
||
std::string reason = refusal.is_object() ? refusal.value("text", std::string()) : std::string();
|
||
if (reason.empty()) reason = response.value("code", std::string("substitution_refused"));
|
||
live_feedback_ = "Wechsel nicht möglich: " + reason;
|
||
}
|
||
engine_live_swap_ = Json();
|
||
}
|
||
if (kind == "live.tactics") {
|
||
if (accepted && engine_live_tactics_sent_.is_array() && engine_live_tactics_sent_.size() == engine_live_tactics_.size()) {
|
||
if (engine_live_tactics_[0].at("value") != engine_live_tactics_sent_[0]) engine_live_assistant_ = false;
|
||
for (size_t i = 0; i < engine_live_tactics_.size(); ++i) engine_live_tactics_[i]["value"] = engine_live_tactics_sent_[i];
|
||
live_feedback_ = "Taktik geändert.";
|
||
} else if (!accepted) {
|
||
const Json refusal = response.value("message", Json());
|
||
std::string reason = refusal.is_object() ? refusal.value("text", std::string()) : std::string();
|
||
if (reason.empty()) reason = response.value("code", std::string("invalid_tactic"));
|
||
live_feedback_ = "Taktik nicht geändert: " + reason;
|
||
}
|
||
engine_live_tactics_sent_ = Json();
|
||
}
|
||
const bool standing =
|
||
state.is_object() && state.value("ok", false) && state.value("live", false);
|
||
if (standing) {
|
||
if (accepted || kind != "live.step") {
|
||
const auto fixture = engine_live_progress(live_.fixture(), state, engine_data_, engine_live_roster_);
|
||
if (fixture.value("ok", false)) {
|
||
std::set<std::string> seen;
|
||
for (const auto &e : live_.fixture().at("frames").back().at("events")) seen.insert(e.at("id").get<std::string>());
|
||
live_.follow(fixture);
|
||
// An own starter is injured: the match stops and the nearest change is offered - the
|
||
// strongest bench player of his position, else the strongest on the bench.
|
||
try {
|
||
const auto owner = live_owner();
|
||
const auto &match = live_match_by_id(live_.fixture().at("matches"), owner.at("match_id"));
|
||
const std::string own_side = match.at("home").at("id") == owner.at("team").at("id") ? "home" : "away";
|
||
for (const auto &e : live_.fixture().at("frames").back().at("events")) {
|
||
if (seen.contains(e.at("id").get<std::string>()) || e.value("kind", std::string()) != "injury" ||
|
||
e.at("match_id") != match.at("id") || e.at("side") != own_side || !e.at("player").is_object() || !live_dialog_.empty()) continue;
|
||
const std::string hurt = e.at("player").value("id", std::string());
|
||
size_t out = engine_live_roster_.size();
|
||
for (size_t i = 0; i < engine_live_roster_.size() && i < 11; ++i) if (engine_live_roster_[i].at("id") == hurt) out = i;
|
||
if (out >= 11) continue;
|
||
int best = -1, best_same = -1;
|
||
const auto strength = [&](size_t i) { const auto &v = engine_live_roster_[i].at("strength"); return v.is_number_integer() ? v.get<int>() : 0; };
|
||
for (size_t i = 11; i < engine_live_roster_.size() && i < 16; ++i) {
|
||
if (best < 0 || strength(i) > strength(size_t(best))) best = int(i);
|
||
if (engine_live_roster_[i].value("position", Json()) == engine_live_roster_[out].value("position", Json()) &&
|
||
(best_same < 0 || strength(i) > strength(size_t(best_same)))) best_same = int(i);
|
||
}
|
||
const int in = best_same >= 0 ? best_same : best;
|
||
if (in < 0) continue;
|
||
live_resume_speed_ = live_.speed(); live_.speed(0);
|
||
live_dialog_ = "lineup"; live_dialog_draft_ = Json::object();
|
||
live_out_ = hurt; live_in_ = engine_live_roster_[size_t(in)].at("id").get<std::string>();
|
||
live_feedback_ = e.at("player").value("name", std::string("Ein Spieler")) + " ist verletzt.";
|
||
std::fprintf(stderr, "live: substitution offered after an injury: %s for %s%c", live_in_.c_str(), live_out_.c_str(), char(10));
|
||
focus_after_render_ = "live-dialog-apply";
|
||
}
|
||
} catch (const std::exception &) {}
|
||
}
|
||
} else {
|
||
live_.speed(0);
|
||
live_feedback_ = "Der Spieltag steht: " + response.value("code", std::string("live_step_refused"));
|
||
}
|
||
if (engine_live_queue_.is_object()) {
|
||
const Json next = engine_live_queue_; engine_live_queue_ = Json();
|
||
engine_live_send(next.at("kind"), next.at("payload"));
|
||
}
|
||
refresh();
|
||
return;
|
||
}
|
||
// the run is over: its recording replaces the frames composed on the way, the desk is read anew
|
||
engine_live_running_ = false; engine_live_queue_ = Json();
|
||
auto link = engine_link_;
|
||
if (accepted) {
|
||
engine_live_at_end_ = true;
|
||
const std::string season = engine_season_id(engine_data_);
|
||
engine_start("Abpfiff …", [link, state, season] {
|
||
Json finished = {{"messageType", "live_finished"}, {"liveRecording", state}, {"postmatch", link->read({{"kind", "postmatch"}})}};
|
||
auto after = link->snapshot();
|
||
engine_attach_opening(*link, season, after, finished);
|
||
return EngineReply{link, std::move(after), finished, nullptr};
|
||
}, true);
|
||
} else {
|
||
// the engine took the kickoff back (a question it could not ask in time): back to the desk
|
||
scene_ = live_return_scene_; live_.close();
|
||
engine_start("Der Spieltag wurde zurückgenommen …", [link, response] {
|
||
Json refused = response; refused["advanceRefused"] = true;
|
||
return EngineReply{link, link->snapshot(), refused, nullptr};
|
||
}, false);
|
||
}
|
||
refresh();
|
||
}
|
||
void App::engine_live_enter() {
|
||
try { live_.owner(engine_data_.at("desktop").at("meta").at("scope").at("managerId").get<std::string>()); }
|
||
catch (const std::exception &) {}
|
||
const auto &fixture = live_.fixture();
|
||
live_source_ = nullptr; live_world_ = 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");
|
||
for (const auto &owner : fixture.at("owners")) if (owner.at("id") == live_.owner()) live_selected_match_ = owner.at("match_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;
|
||
if (scene_ != "live") live_return_scene_ = scene_;
|
||
scene_ = "live"; menu_.clear();
|
||
live_last_tick_ = SDL_GetTicks64();
|
||
focus_after_render_ = "live-pause";
|
||
}
|
||
void App::live_close() {
|
||
if (engine_mode_ && engine_live_running_) {
|
||
// a kickoff under way cannot be left standing: it is played to its final whistle
|
||
live_dialog_.clear(); live_dialog_draft_ = Json(); live_.speed(0);
|
||
live_feedback_ = "Das Spiel wird zu Ende gespielt …";
|
||
engine_live_send("live.finish", Json::object());
|
||
refresh();
|
||
return;
|
||
}
|
||
live_review_=false;postmatch_playing_=false;
|
||
live_.speed(0); live_dialog_.clear(); live_dialog_draft_ = Json();
|
||
live_resume_speed_ = 0; live_speed_dragging_ = false;
|
||
if (engine_mode_) { scene_ = live_return_scene_; live_.close(); } else 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<Rml::ElementFormControl *>(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 bool recorded = live_.fixture().at("source") == "engine-recording";
|
||
const std::string phase = live_.finished() ? "Beendet" : frame.at("phase") == "interval" ? "Halbzeit" : value ? (recorded ? "Spiel läuft" : "Aufzeichnung läuft") : (recorded ? "Pausiert" : "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<int>()) + (recorded ? "′" : "′ · keine Spielwertänderung")));
|
||
dirty_ = true;
|
||
}
|
||
void App::live_tick() {
|
||
if (engine_live_busy_) engine_live_poll();
|
||
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_ && !engine_mode_ ? source_->snapshot() : Snapshot();
|
||
if (!engine_mode_ && (!source_ || source_.get() != live_source_ || current.world != live_world_ ||
|
||
current.revision != live_.fixture().at("source_revision").get<uint64_t>())) {
|
||
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 (engine_live_running_) {
|
||
// one engine minute per beat; the beat follows the speed control
|
||
if (engine_live_busy_ || !live_.speed()) return;
|
||
static constexpr std::array<uint64_t, 5> beats{0, 1600, 900, 450, 150};
|
||
engine_live_elapsed_ += std::min<uint64_t>(elapsed, 2000);
|
||
if (engine_live_elapsed_ >= beats[size_t(live_.speed())]) { engine_live_elapsed_ = 0; engine_live_send("live.step", {{"minutes", 1}}); }
|
||
return;
|
||
}
|
||
if (live_.elapse(elapsed)) needs_render_ = true;
|
||
}
|
||
void App::live_dialog_close(bool apply_draft) {
|
||
if (live_dialog_.empty()) return;
|
||
if (apply_draft && engine_live_running_) {
|
||
if (live_dialog_ == "lineup" && !live_out_.empty() && !live_in_.empty()) {
|
||
engine_live_swap_ = Json::array({live_out_, live_in_});
|
||
live_feedback_ = "Wechsel wird ausgeführt …";
|
||
engine_live_send("live.substitute", {{"outPlayerId", live_out_}, {"inPlayerId", live_in_}});
|
||
}
|
||
if (live_dialog_ == "tactics" && live_dialog_draft_.is_object() && live_dialog_draft_.contains("values")) {
|
||
engine_live_tactics_sent_ = live_dialog_draft_.at("values");
|
||
live_feedback_ = "Taktik wird geändert …";
|
||
engine_live_send("live.tactics", {{"values", engine_live_tactics_sent_}});
|
||
}
|
||
} else 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<Rml::String>("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") {
|
||
if (engine_live_running_) { engine_live_elapsed_ = 0; engine_live_send("live.step", {{"minutes", 1}}); return true; }
|
||
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() || (live_.fixture().at("source") == "engine-recording" && !live_.running())) return true;
|
||
if (live_.running() && action == "live-intervene-tactics" && engine_live_tactics_.size() != 8) return true;
|
||
const auto owner = live_owner(); live_dialog_draft_ = live_owner_state(owner);
|
||
if (live_.running() && action == "live-intervene-tactics") {
|
||
Json values = Json::array();
|
||
for (const auto &control : engine_live_tactics_) values.push_back(control.at("value"));
|
||
live_dialog_draft_ = {{"values", values}};
|
||
}
|
||
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;
|
||
if (engine_live_running_) {
|
||
// the next value of the engine's own range, wrapping to its lowest
|
||
const auto &control = engine_live_tactics_.at(size_t(index));
|
||
int value = live_dialog_draft_.at("values").at(size_t(index)).get<int>() + 1;
|
||
if (value > control.at("max").get<int>()) value = control.at("min").get<int>();
|
||
live_dialog_draft_["values"][size_t(index)] = value;
|
||
if (target) focus_after_render_ = target->GetId();
|
||
refresh(); 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<Rml::ElementFormControl *>(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<int>("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<int>("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<int>("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<Rml::String>("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;
|
||
}
|