// SPDX-License-Identifier: GPL-3.0-only void App::engine_start(std::string label, std::function job, bool mutating) { if (engine_busy_) return; engine_base_dirty_ = engine_dirty_; if (mutating) { engine_dirty_ = true; engine_fresh_ = false; } engine_operation_ = std::move(label); engine_busy_ = true; engine_job_ = std::async(std::launch::async, std::move(job)); refresh(); } void App::engine_accept(EngineReply result) { engine_fresh_ = true; if (result.catalog) { world_ = std::move(result.catalog); bind_world(world_); } engine_link_ = std::move(result.connection); engine_data_ = std::move(result.bundle); engine_response_ = std::move(result.response); if (engine_response_.value("messageType", "") == "command_result") { engine_dirty_ = engine_base_dirty_ || engine_response_.at("newRevision") != engine_response_.at("previousRevision"); engine_notice_ = engine_text(engine_response_.at("feedback")) + (engine_dirty_ ? " · Änderungen noch nicht gespeichert." : ""); } else if (engine_response_.value("messageType", "") == "error") { engine_dirty_ = engine_base_dirty_; auto reason = engine_text(engine_response_.value("message", Json())); if (reason.empty() || reason == "—") reason = engine_response_.value("code", "unknown_error"); engine_notice_ = "Änderung abgewiesen: " + reason; } else if (engine_response_.value("messageType", "") == "saved") { engine_dirty_ = false; engine_notice_ = "Spielstand als neue OBS-Datei gespeichert."; if(engine_exit_after_save_){engine_exit_after_save_=false;closed_=true;} } else if(engine_response_.value("messageType","")=="created"){engine_dirty_=true;engine_notice_="Neue Partie · Noch nicht gespeichert.";} else engine_notice_ = "Spielstand geladen · Weitere Menüs werden schrittweise angebunden."; if (engine_response_.value("messageType", "") == "command_result") { advertising_selected_ = -1; advertising_offer_ = 0; } engine_credit_ = engine_data_.value("credit", Json()); engine_credit_edits_ = Json::array(); engine_reports_sync(); engine_almanac_sync(); engine_ticket_bind(); engine_contracts_poll(); engine_calendar_sync(); engine_friendly_sync(); engine_sports_reset(); const auto &f = engine_data_.at("camps").at("data").at("focus"); engine_focus_ = {{"condition", f.at("condition")}, {"technique", f.at("technique")}, {"play", f.at("play")}}; if (engine_camp_.empty() && !engine_data_.at("camps").at("data").at("camps").empty()) engine_camp_ = engine_data_.at("camps").at("data").at("camps").front().at("id"); } void App::engine_poll() { if (!engine_busy_ || engine_job_.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) return; engine_busy_ = false; try { auto result = engine_job_.get(); validate_engine_desk(result.bundle); if(result.response.value("messageType","")=="reports_preview"){engine_reports_poll(result);refresh();return;} if(result.response.value("messageType","")=="almanac_preview"){engine_almanac_poll(result);refresh();return;} if (result.response.value("messageType", "") == "friendly_preview") { const auto &read = result.response.at("read"); if (read.value("messageType", "") == "error") { planning_notice_ = engine_text(read.value("message", Json())); if (planning_notice_.empty() || planning_notice_ == "—") planning_notice_ = "Dieses Freundschaftsspiel ist derzeit nicht verfügbar."; planning_dialog_ = "friendly-error"; focus_after_render_ = "planning-close"; } else { const auto quote = engine_friendly_projection(result.bundle,read,result.response.at("opponent"),result.response.at("home")); engine_friendly_read_ = read; planning_dialog_ = "quote"; focus_after_render_ = quote.at("days").empty() ? "planning-close" : "friendly-quote-accept"; } refresh(); return; } if (result.response.value("messageType", "") == "credit_preview") { const auto &read = result.response.at("read"); if (read.value("messageType", "") == "error") engine_notice_ = "Kreditvorschau abgewiesen: " + read.value("code", "unknown_error"); else { (void)engine_credit_projection(result.bundle, read); engine_credit_ = read; engine_credit_edits_ = result.response.at("edits"); engine_notice_ = "Kreditvorschau berechnet · Noch keine Buchung."; } refresh(); return; } engine_accept(std::move(result)); } catch (const std::exception &e) { engine_notice_ = e.what(); if(engine_exit_after_save_){engine_exit_after_save_=false;message("Der Spielstand konnte nicht gespeichert werden: "+std::string(e.what()));} } refresh(); } void App::engine_frame() { engine_poll(); const bool rendered = needs_render_; if (needs_render_) engine_render(); engine_sports_update(); if (ticket_) ticket_->update(); render_popup(); context_->Update(); if(center_fixture_columns())context_->Update(); if (rendered && menu_ == "friendlies") if (auto *list = element("friendly-opponents")) list->SetScrollTop(engine_friendly_scroll_); if (menu_ == "contracts" && engine_contract_scroll_) { if (auto *roster = element("contracts-roster-scroll")) roster->SetScrollTop(*engine_contract_scroll_); engine_contract_scroll_.reset(); } if(rendered&&(menu_=="scorers"||menu_=="fairplay")){rankings_restore_scroll();context_->Update();} if (center_selects(*doc_)) context_->Update(); if (menu_ == "lineup" && lineup_roster_scroll_) { if (auto *roster = element("lineup-roster-scroll")) roster->SetScrollTop(*lineup_roster_scroll_); lineup_roster_scroll_.reset(); } if (!engine_busy_ && popup_.empty() && !focus_after_render_.empty()) { if (auto *node = element(focus_after_render_)) { node->Focus(keyboard_navigation_); if(node->GetId().starts_with("report-club-")||node->GetId().starts_with("report-option-"))node->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest)); if(menu_=="almanac"&&node->GetId().starts_with("alm-match-")) { node->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest)); if(auto *list=element("alm-day-list"))list->SetScrollLeft(0); } } focus_after_render_.clear(); } if (amount_select_all_) { if (auto *input = dynamic_cast(element("calculator-value"))) input->Select(); amount_select_all_ = false; } if (amount_caret_end_) { if (auto *input = dynamic_cast(element("calculator-value"))) input->SetSelectionRange(int(input->GetValue().size()), int(input->GetValue().size())); amount_caret_end_ = false; } renderer_.Clear(); renderer_.BeginFrame(); context_->Render(); renderer_.EndFrame(); video_.present(); dirty_ = false; } void App::engine_save() { if (engine_busy_ || !engine_link_) return; auto output = config_.file_export; if (output.empty()) { NFD::Guard guard; NFD::UniquePath chosen; const nfdfilteritem_t filters[] = {{"OpenBMH-Spielstand", "obs"}}; const auto result = NFD::SaveDialog(chosen, filters, 1, nullptr, "openbmh-career.obs"); if (result == NFD_CANCEL) {engine_exit_after_save_=false;return;} if (result != NFD_OKAY) throw editor::Error("Die Dateiauswahl konnte nicht geöffnet werden."); output = editor::path(chosen.get()); } auto link = engine_link_; engine_start("Spielstand wird gespeichert …", [link, output] { auto response = link->save_copy(output); return EngineReply{link, link->snapshot(), response, nullptr}; }); } void App::engine_quit() { if (engine_busy_) { engine_notice_ = "Bitte den laufenden Vorgang abwarten."; dirty_ = true; return; } if (engine_dirty_) { confirmation("Die Partie enthält ungespeicherte Änderungen. Vor dem Beenden speichern?",[this]{closed_=true;}); popup_accept_="Ohne Speichern beenden"; popup_extra_=btn("engine-save-exit","Speichern und beenden","primary","id=\"engine-save-exit\""); } else closed_ = true; } void App::engine_event(Rml::Event &event) { const auto type = event.GetType(); auto *target = event.GetTargetElement(); if (type == "keydown") keyboard_navigation_ = true; if (type == "mousedown") keyboard_navigation_ = false; for (auto *node = target; node && node != doc_; node = node->GetParentNode()) if (node->GetId() == "native-ticket-layer") return; // owned by the compact control if (ticket_ && ticket_->is_open()) return; if (amount_event(event)) return; if (!engine_busy_ && bank_event(event)) return; if (!engine_busy_ && advertising_event(event)) return; if (!engine_busy_ && popup_.empty() && contracts_event(event)) return; if(engine_opening_active()&&popup_.empty()) { if(type=="keydown") {const auto key=Rml::Input::KeyIdentifier(event.GetParameter("key_identifier",0)); if(key==Rml::Input::KI_TAB){Rml::ElementList nodes;doc_->QuerySelectorAll(nodes,".opening-actions button");std::erase_if(nodes,[](auto *n){return n->HasAttribute("disabled");});if(!nodes.empty()){const auto it=std::find(nodes.begin(),nodes.end(),context_->GetFocusElement());const int i=it==nodes.end()?-1:int(it-nodes.begin()),n=int(nodes.size());nodes[size_t((i+n+(event.GetParameter("shift_key",0)?-1:1))%n)]->Focus(true);}event.StopImmediatePropagation();return;} if(key==Rml::Input::KI_RETURN||key==Rml::Input::KI_SPACE){for(auto *n=target;n&&n!=doc_;n=n->GetParentNode())if(n->GetTagName()=="button"&&!n->HasAttribute("disabled")){n->DispatchEvent("click",{});break;}}} if(type=="click")for(auto *n=target;n&&n!=doc_;n=n->GetParentNode())if(n->HasAttribute("data-action")){opening_action(n->GetAttribute("data-action",""),n);break;} event.StopImmediatePropagation();return; } if (engine_reports_event(event)) return; if(!engine_busy_&&popup_.empty()&&rankings_event(event))return; if (!engine_busy_ && popup_.empty() && engine_friendly_event(event)) return; if (!engine_busy_ && planning_dialog_.empty() && popup_.empty() && (menu_ == "friendlies" || menu_ == "almanac" || menu_=="tables" || menu_=="strengths" || menu_=="league" || menu_=="other" || menu_=="scorers") && report_picker_event(event)) return; if (engine_sports_event(event)) return; Rml::ElementFormControl *range = nullptr; for (auto *node = target; node && node != doc_; node = node->GetParentNode()) if (node->GetId().starts_with("camp-focus-") && !node->HasAttribute("disabled")) { range = dynamic_cast(node); break; } if (popup_.empty() && !engine_busy_ && range && type == "mouseup") { focus_after_render_ = range->GetId(); dirty_ = true; } if (popup_.empty() && !engine_busy_ && range && type == "keydown") { const auto key = Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)); if (key == Rml::Input::KI_HOME || key == Rml::Input::KI_END || key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT || key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN) { const int index = range->GetAttribute("data-id", -1); if (index >= 0 && index < 3) { const int value = key == Rml::Input::KI_HOME ? 0 : key == Rml::Input::KI_END ? 15 : std::clamp(std::stoi(range->GetValue()) + (key == Rml::Input::KI_LEFT || key == Rml::Input::KI_DOWN ? -1 : 1), 0, 15); range->SetValue(std::to_string(value)); engine_focus_[std::array{"condition", "technique", "play"}[size_t(index)]] = value; element("camp-value-" + std::to_string(index))->SetInnerRML(std::to_string(value) + " / 15"); element("camp-value-3")->SetInnerRML("—"); element("camp-focus-3")->SetProperty("visibility", "hidden"); dirty_ = true; event.StopImmediatePropagation(); return; } } } if (type == "keydown" && event.GetParameter("key_identifier", 0) == Rml::Input::KI_ESCAPE) { if (menu_ == "almanac" && engine_busy_) { event.StopImmediatePropagation(); return; } if (!popup_.empty()) { popup_.clear(); confirm_ = {}; dirty_ = true; } else if (!engine_busy_ && !engine_player_.empty()) { focus_after_render_ = "lineup-detail-" + engine_player_ + "-0"; engine_player_.clear(); refresh(); } else if (!engine_busy_ && menu_ == "almanac") engine_almanac_action("alm-back",nullptr); else if (!engine_busy_ && !menu_.empty()) { if (menu_ == "training" || menu_ == "tactics") engine_sports_reset(); if(menu_=="scorers"||menu_=="fairplay")rankings_remember_scroll(); menu_.clear(); refresh(); } else engine_quit(); event.StopImmediatePropagation(); return; } if (type == "keydown" && event.GetParameter("key_identifier", 0) == Rml::Input::KI_TAB) { auto *root = !popup_.empty() ? element("popup") : !engine_player_.empty() ? element("engine-player-dialog") : !menu_.empty() ? doc_->QuerySelector(".window") : element("canvas"); Rml::ElementList controls; if (!root) return; root->QuerySelectorAll(controls, "button,input"); controls.erase(std::remove_if(controls.begin(), controls.end(), [](auto *node) { return !node->IsVisible(true) || node->HasAttribute("disabled"); }), controls.end()); if (!controls.empty()) { const auto found = std::find(controls.begin(), controls.end(), context_->GetFocusElement()); const int index = found == controls.end() ? -1 : int(found - controls.begin()); auto *next=controls[size_t((index + int(controls.size()) + (event.GetParameter("shift_key", 0) ? -1 : 1)) % int(controls.size()))]; next->Focus(true); if(next->GetId().starts_with("report-club-"))next->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest)); if(menu_=="almanac"&&next->GetId().starts_with("alm-match-")) { next->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest)); if(auto *list=element("alm-day-list"))list->SetScrollLeft(0); } } event.StopImmediatePropagation(); return; } if (engine_busy_) return; if ((type == "input" || type == "change") && menu_ == "camp" && popup_.empty() && target->GetId().starts_with("camp-focus-")) { const int index = target->GetAttribute("data-id", -1); if (index < 0 || index > 2) return; const int value = std::stoi(dynamic_cast(target)->GetValue()); if (value < 0 || value > 15) return; engine_focus_[std::array{"condition", "technique", "play"}[size_t(index)]] = value; element("camp-value-" + std::to_string(index))->SetInnerRML(std::to_string(value) + " / 15"); element("camp-value-3")->SetInnerRML("—"); element("camp-focus-3")->SetProperty("visibility", "hidden"); dirty_ = true; return; } if(type=="keydown") { const auto key=Rml::Input::KeyIdentifier(event.GetParameter("key_identifier",0)); if(key==Rml::Input::KI_RETURN||key==Rml::Input::KI_SPACE) { for(auto *node=target;node&&node!=doc_;node=node->GetParentNode())if(node->GetTagName()=="button") { if(!node->HasAttribute("disabled"))node->DispatchEvent("click",{}); event.StopImmediatePropagation();return; } } } if (type != "click") return; for (auto *node = target; node && node != doc_; node = node->GetParentNode()) { if (!node->HasAttribute("data-action") || node->HasAttribute("disabled")) continue; const auto action = node->GetAttribute("data-action", ""), id = node->GetAttribute("data-id", ""); if (!popup_.empty()) { if (action == "popup-close") { popup_.clear(); confirm_ = {}; dirty_ = true; } if(action=="engine-save-exit"){reset_popup();popup_.clear();engine_exit_after_save_=true;try{engine_save();}catch(const std::exception &e){engine_exit_after_save_=false;message(e.what());}return;} if (action == "popup-confirm") { auto callback = std::move(confirm_); popup_.clear(); dirty_ = true; if (callback) callback(); } return; } if(action.starts_with("rankings-")&&menu_=="scorers"){rankings_action(action,node);return;} if (engine_reports_action(action,node)) return; if (almanac_action(action,node)) return; if (engine_friendly_action(action, node)) return; if ((menu_ == "friendlies" || menu_ == "almanac" || menu_=="tables" || menu_=="strengths" || menu_=="league" || menu_=="other" || menu_=="scorers") && report_picker_action(action,node)) return; if (engine_calendar_action(action, node)) return; if (contracts_action(action, node)) return; if (environment_action(action, node)) return; if (advertising_action(action, node)) return; if (bank_action(action, node)) return; if (engine_sports_action(action, id, node)) return; if (engine_lineup_action(action, id, node)) return; if (action == "close") { if(menu_=="scorers"||menu_=="fairplay")rankings_remember_scroll(); menu_.clear(); refresh(); } else if (action == "engine-ticket" && engine_fresh_) { const auto price = engine_ticket_projection(engine_data_); ticket_->open(node, device_ratio_, price.at("min"), price.at("max")); dirty_ = true; } else if (action == "engine-open" && (id == "environment" || id == "advertising")) { menu_ = id; advertising_selected_ = -1; advertising_offer_ = 0; refresh(); } else if(action=="engine-open"&&id=="scorers"){menu_=id;engine_reports_sync();engine_rankings_load(engine_rankings_state_.at("league"),engine_rankings_state_.at("scope"));} else if(action=="engine-open"&&id=="fairplay"){menu_=id;engine_reports_sync();engine_fairplay_load();} else if(action=="engine-open"&&id=="other"){menu_=id;engine_reports_sync();engine_other_load(engine_other_value_.is_object()?engine_other_value_.at("viewId").get():"",engine_other_value_.is_object()?engine_other_value_.at("round").get():0);} else if(action=="engine-open"&&id=="league"){menu_=id;engine_reports_sync();const auto league=engine_reports_state_.at("league").get();engine_fixtures_load(league,engine_fixtures_value_.is_object()&&engine_fixtures_value_.at("leagueId")==league?engine_fixtures_value_.at("round").get():0);} else if(action=="engine-open"&&(id=="tables"||id=="strengths")){menu_=id;engine_reports_sync();engine_reports_load(engine_reports_state_.at("league"),engine_reports_state_.at("filter"));} else if (action == "engine-open" && id == "almanac") { menu_=id;engine_almanac_sync();engine_almanac_load({{"kind","index"}}); } else if (action == "engine-open" && id == "friendlies") { menu_ = id; planning_dialog_.clear(); engine_friendly_sync(); refresh(); } else if (action == "engine-open" && (id == "calendar" || id == "postpone")) { menu_ = id; engine_move_id_.clear(); engine_calendar_sync(); refresh(); } else if (action == "engine-open" && id == "contracts") { menu_ = id; engine_contracts_clear(false); contracts_player_.clear(); engine_contract_scroll_ = 0.f; refresh(); } else if (action == "engine-open" && id == "bank") { bank_view_ = "entrance"; bank_hint_.clear(); menu_ = "bank"; refresh(); } else if (action == "engine-open" && id == "save") engine_save(); else if (action == "engine-open" && (id == "training" || id == "tactics")) { engine_sports_reset(); menu_ = id; refresh(); } else if (action == "engine-open" && id == "lineup") { menu_ = "lineup"; lineup_selected_.clear(); lineup_reset_scroll_ = true; refresh(); } else if (action == "engine-open" && id == "camp") { menu_ = "camp"; refresh(); } else if (action == "camp-region") { selections_["camp-region"] = id; refresh(); } else if (action == "camp-select") { engine_camp_ = id; refresh(); } else if (action == "camp-remember" && engine_fresh_) { const auto command = engine_camp_command(engine_data_, engine_camp_, engine_focus_, editor::new_id("request:ui-")); const auto &camps = engine_data_.at("camps").at("data").at("camps"); const auto chosen = std::find_if(camps.begin(), camps.end(), [&](const auto &camp) { return camp.at("id") == engine_camp_; }); confirmation(engine_text(chosen->at("name")) + " für " + engine_price(chosen->at("quote").at("total")) + " verbindlich buchen?", [this, command] { auto link = engine_link_; engine_start("Trainingslager wird gebucht …", [link, command] { auto response = link->command(command); return EngineReply{link, link->snapshot(), response, nullptr}; }, true); }); } return; } } Json App::engine_snapshot() const { return {{"mode", engine_mode_}, {"creation_busy",engine_creation_busy_}, {"busy", engine_busy_}, {"dirty", engine_dirty_}, {"fresh", engine_fresh_}, {"bundle", engine_data_}, {"response", engine_response_}, {"opening_read",engine_opening_read_},{"opening_state",engine_opening_state_},{"rankings_read",engine_rankings_read_},{"rankings_state",engine_rankings_state_},{"fairplay_read",engine_fairplay_read_},{"other_index",engine_other_index_},{"other_read",engine_other_read_},{"fixtures_read",engine_fixtures_read_},{"reports_state",engine_reports_state_},{"reports_index",engine_reports_index_},{"reports_read",engine_reports_read_},{"reports_roster",engine_reports_roster_},{"almanac_cache",engine_almanac_cache_}, {"almanac_context",engine_almanac_context_}, {"friendly_quote", engine_friendly_read_}, {"friendly_league", engine_friendly_league_}, {"friendly_opponent", engine_friendly_opponent_}, {"friendly_date", engine_friendly_date_}, {"friendly_home", engine_friendly_home_}, {"friendly_stage", engine_friendly_stage_}, {"friendly_month", engine_friendly_month_}, {"calendar_day", engine_calendar_day_}, {"move_id", engine_move_id_}, {"credit_preview", engine_credit_}, {"credit_edits", engine_credit_edits_}, {"training_draft", engine_training_draft_}, {"tactics_draft", engine_tactics_draft_}, {"player_profile", engine_player_}, {"lineup_selected", lineup_selected_}, {"focus", engine_focus_}, {"selected_camp", engine_camp_}, {"notice", engine_notice_}}; }