// SPDX-License-Identifier: GPL-3.0-only Json App::stadium_overview(bool future) const { if (!engine_mode_) { const auto s = snap(); return demo_stadium_view(s.state, *s.world, future, stadium_managed(s)); } auto value = engine_stadium_projection(engine_data_, future); const auto club = value.at("club").get(); const auto *team = world_->find("clubs", club); const Json *stadium = nullptr; if (team && team->value("stadium", Json()).is_string()) stadium = world_->find("stadiums", team->at("stadium")); if (!stadium) for (const auto &row : world_->rows("stadiums")) if (row.value("owner", Json()) == club) { stadium = &row; break; } if (stadium) value["name"] = world_->name(*stadium); return value; } Json App::stadium_quote_value() const { if (engine_mode_) { try { return engine_stadium_quote(engine_data_, stadium_view_.at("draft")); } catch (const std::exception &) { return {{"ok", false}, {"can_build", false}}; } } const auto s = snap(); return demo_stadium_quote(s.state, stadium_view_.at("draft"), *s.world, stadium_managed(s)); } std::string App::stadium_cash(const Json &value) const { return engine_mode_ ? (value.is_number_integer() ? money(value.get(), {{"code", "EUR"}, {"minor_units", 2}}) : "—") : stadium_money(value, *world_); } std::string App::stadium_offer_feedback(const Json "e, bool invalid) const { if (!engine_mode_) return stadium_feedback(quote, invalid); if (invalid) return "Bitte die Anzahl der Plätze prüfen."; if (!quote.value("ok", false)) return "Angebot wird aktualisiert …"; if (!quote.value("can_build", false)) return "Bitte einen Ausbau auswählen."; const int percent = quote.value("company_percent", 100); return percent != 100 ? "Firmenangebot: " + std::to_string(percent) + " % des Grundpreises. Bauauftrag erst nach Bestätigung." : "Bauauftrag erst nach Bestätigung des Gesamtbetrags."; } void App::engine_stadium_sync() { stadium_view_defaults(stadium_view_); engine_stadium_value_ = Json(); try { engine_stadium_value_ = stadium_overview(stadium_view_.at("future")); } catch (const std::exception &) { } engine_stadium_context_ = engine_data_.at("desktop").at("meta"); } void App::engine_stadium_poll() { engine_stadium_sync(); if (engine_stadium_operation_.empty()) return; const auto operation = std::exchange(engine_stadium_operation_, std::string()); const bool success = engine_response_.value("messageType", "") == "command_result"; if (!success) { engine_stadium_exit_ = false; if (engine_stadium_confirmed_.is_object()) stadium_view_["draft"] = engine_stadium_confirmed_; stadium_view_["feedback"] = engine_notice_; return; } if (operation == "quote" || operation == "update") { engine_stadium_confirmed_ = stadium_view_.at("draft"); stadium_view_["feedback"] = ""; focus_after_render_ = stadium_view_.at("dialog") == "block" ? "stadium-place-range" : "stadium-build"; return; } if (operation == "build" || operation == "cancel") { const auto focus = stadium_view_.value("return_focus", std::string("stadium-data")); bool built = true; if (operation == "build") { const auto &d = stadium_view_.at("draft"); const auto now = stadium_overview(false); built = now.at(d.at("kind") == "block" ? "blocks" : d.at("kind") == "facility" ? "facilities" : "periphery") .at(d.at("index").get()) .at("building"); if (!built) { auto reason = engine_text(engine_response_.at("feedback")); if (reason.empty() || reason == "Der Bau beginnt." || reason == "—") reason = "Der Ausbau wurde nicht begonnen."; engine_notice_ = reason; message(reason); } if (built && d.at("kind") != "periphery") stadium_view_["future"] = true; } stadium_view_["dialog"] = ""; stadium_view_["draft"] = Json(); stadium_view_["feedback"] = ""; stadium_view_["invalid_places"] = false; stadium_view_["hint"] = ""; engine_stadium_confirmed_ = Json(); focus_after_render_ = focus; if (engine_stadium_exit_) { engine_stadium_exit_ = false; engine_quit(); } } } void App::engine_stadium_send(const Json &request, const std::string &operation) { if (engine_busy_ || !engine_fresh_) return; engine_stadium_operation_ = operation; auto link = engine_link_; engine_start(operation == "build" ? "Stadionbau wird beauftragt …" : operation == "cancel" ? "Angebot wird geschlossen …" : "Stadionangebot wird berechnet …", [link, request] { const auto response = link->command(request); return EngineReply{link, link->snapshot(), response, nullptr}; }); } std::string App::engine_stadium_data() { const auto &data = engine_data_.at("stadium").at("data"); std::string svg = ""; constexpr std::array colors{"#8fbaa6", "#8cacc9", "#adb7c1", "#cf9088", "#c5b384", "#729aae"}, names{"Zuschauer", "Eintritt", "Stadionwert", "Zus. Stadion-Einnahmen", "Einnahmen", "Stadionkosten"}; for (size_t curve = 0; curve < 6; ++curve) if (stadium_view_.at("curves")[curve] == true) { svg += ""; const auto &points = data.at("history")[curve].at( stadium_view_.at("relative") == true ? "relative" : "absolute"); if (points.size() == 1) svg += ""; } svg += ""; std::string out = "
" + picture(renderer_.diagram("stadium-engine-graph", svg, 5120), 2560, 456) + "
"; for (size_t i = 0; i < 6; ++i) out += btn("stadium-curve", "" + esc(data.at("history")[i].value("name", std::string(names[i]))) + "", "stadium-curve", "id=\"stadium-curve-" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" aria-pressed=\"" + (stadium_view_.at("curves")[i] == true ? "true" : "false") + "\" style=\"" + rect((i % 3) * 754.666666667, 660 + (i / 3) * 90, 732.666666667, 74) + "\""); for (int i = 0; i < 2; ++i) out += btn("stadium-scale", i ? "Relativ" : "Absolut", "stadium-scale", "id=\"stadium-scale-" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" aria-pressed=\"" + (stadium_view_.at("relative") == bool(i) ? "true" : "false") + "\" style=\"" + rect(2282, 660 + i * 90, 330, 74) + "\""); const std::array labels{"", "Max. Zuschauer", "Max. Einnahmen (€)", "Heimspiele", "Ø Zuschauer", "Ges. Einnahmen (€)"}; out += "
"; for (size_t row = 0; row < 6; ++row) { out += "
"; for (size_t col = 0; col < 4; ++col) { std::string value; if (!col) value = labels[row]; else if (!row) value = col == 1 ? "Liga" : col == 2 ? "DFB-Pokal" : "Europapokal"; else { const size_t source = row == 1 ? 0 : row == 2 ? 2 : row + 1; value = data.at("table")[source][col - 1]; if (row <= 2) { const auto other = data.at("table")[source + 1][col - 1].get(); if (!other.empty()) value += " · " + other; } } out += "
" + esc(value) + "
"; } out += "
"; } out += "
"; const int ticket = int(engine_money_minor(data.at("ticketPrice")) / 100); out += "

Benötigter Zuschauerschnitt bei " + std::to_string(ticket) + " € Eintritt: " + esc(data.at("requiredAttendanceText").get()) + "

Eintritt:"; const int low = int(engine_money_minor(data.at("ticketPriceRange").at("min")) / 100), high = int(engine_money_minor(data.at("ticketPriceRange").at("max")) / 100); for (int price = low; price <= high; ++price) out += btn( "stadium-ticket", (price < 10 ? "0" : "") + std::to_string(price), "stadium-ticket-price", "id=\"stadium-ticket-" + std::to_string(price) + "\" data-id=\"" + std::to_string(price) + "\" aria-pressed=\"" + (ticket == price ? "true" : "false") + "\""); return out + "
"; } bool App::engine_stadium_action(const std::string &action, Rml::Element *target) { if (!action.starts_with("stadium-")) return false; if (menu_ != "stadium" || engine_busy_ || !engine_fresh_ || !engine_stadium_value_.is_object() || amount_ || !popup_.empty()) return true; stadium_view_defaults(stadium_view_); const auto value = target ? target->GetAttribute("data-id", "") : std::string(); const auto index = stadium_integer(value, 255); if (action == "stadium-cancel") { if (engine_data_.at("stadium").at("data").at("quote").is_object()) engine_stadium_send(engine_stadium_commit(engine_data_, false, editor::new_id("request:ui-")), "cancel"); else { stadium_view_["dialog"] = ""; stadium_view_["draft"] = Json(); refresh(); } return true; } if (action == "stadium-build") { const auto q = stadium_quote_value(); if (!q.value("can_build", false)) return true; const auto request = engine_stadium_commit(engine_data_, true, editor::new_id("request:ui-")), context = engine_data_.at("desktop").at("meta"); confirmation("Diesen Ausbau für " + stadium_cash(q.at("cost")) + " beauftragen?", [this, request, context] { if (engine_data_.at("desktop").at("meta") != context) { message("Das Angebot hat sich geändert. Bitte erneut öffnen."); return; } engine_stadium_send(request, "build"); }); return true; } if (action == "stadium-option" && stadium_view_.at("dialog") == "facility" && index) { auto d = stadium_view_.at("draft"); d["level"] = *index; stadium_view_["draft"] = d; engine_stadium_send( engine_stadium_request(engine_data_, d, editor::new_id("request:ui-"), true), "update"); return true; } if (action == "stadium-places-editor" && stadium_view_.at("dialog") == "block") { const auto overview = stadium_overview(false); const auto row = overview.at("blocks").at(stadium_view_.at("draft").at("index").get()); const auto context = engine_data_.at("desktop").at("meta"); AmountRequest r; r.title = "Neue Gesamtplätze"; r.unit = "Plätze"; r.value = stadium_view_.at("draft").at("places"); r.min = row.at("places"); r.max = row.at("max_places"); r.step = 1000; r.suggestion_step = 1000; r.return_focus = "stadium-place-number"; r.review = false; r.accept = [this, context](int64_t places) { if (engine_data_.at("desktop").at("meta") != context || places % 1000) return false; stadium_view_["draft"]["places"] = places; engine_stadium_send(engine_stadium_request(engine_data_, stadium_view_.at("draft"), editor::new_id("request:ui-"), true), "update"); return true; }; open_amount(std::move(r)); return true; } if (stadium_view_.at("dialog") != "") return true; if (action == "stadium-view") { stadium_view_["view"] = "panorama"; stadium_view_["hint"] = ""; refresh(); return true; } if (action == "stadium-back") { stadium_view_["view"] = "inside"; stadium_view_["hint"] = ""; refresh(); return true; } if (action == "stadium-future") { stadium_view_["future"] = !stadium_view_.at("future").get(); refresh(); return true; } if (action == "stadium-data") { stadium_view_["view"] = "data"; refresh(); return true; } if (action == "stadium-curve" && index && *index < 6) { stadium_view_["curves"][size_t(*index)] = !stadium_view_.at("curves")[size_t(*index)].get(); refresh(); return true; } if (action == "stadium-scale" && index && *index < 2) { stadium_view_["relative"] = bool(*index); refresh(); return true; } if (action == "stadium-ticket" && index) { const auto request = engine_ticket_command(engine_data_, *index, editor::new_id("request:ui-")); confirmation("Eintritt auf " + std::to_string(*index) + " € ändern?", [this, request] { engine_stadium_send(request, "ticket"); }); return true; } const bool block = action == "stadium-block", facility = action == "stadium-facility", periphery = action == "stadium-periphery"; if (!block && !facility && !periphery) return true; if (!index || *index >= (periphery ? 14 : 8)) return true; const auto overview = stadium_overview(false); const auto &row = overview.at(block ? "blocks" : facility ? "facilities" : "periphery")[size_t(*index)]; if (row.at("building") == true) return true; if (facility && row.at("available") != true) return true; if (periphery && row.at("level").get() >= row.at("max_level").get()) return true; Json d = {{"kind", block ? "block" : facility ? "facility" : "periphery"}, {"index", *index}}; if (block) { d["places"] = row.at("places"); d["seats"] = row.at("seats"); d["roof"] = row.at("roof"); } else d["level"] = row.at("level").get() + 1; stadium_view_["draft"] = d; stadium_view_["dialog"] = d.at("kind"); stadium_view_["feedback"] = ""; stadium_view_["return_focus"] = target->GetId(); engine_stadium_confirmed_ = Json(); engine_stadium_send(engine_stadium_request(engine_data_, d, editor::new_id("request:ui-")), "quote"); return true; } bool App::engine_stadium_event(Rml::Event &event) { if (menu_ != "stadium" || amount_ || !popup_.empty()) return false; stadium_view_defaults(stadium_view_); const auto type = event.GetType(); auto *target = event.GetTargetElement(); const bool key_event = type == "keydown"; const auto key = key_event ? Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)) : Rml::Input::KI_UNKNOWN; const bool dialog = stadium_view_.at("dialog") != ""; if (engine_busy_) { event.StopImmediatePropagation(); return true; } if ((key_event && key == Rml::Input::KI_ESCAPE) || (dialog && type == "mouseup" && event.GetParameter("button", 0) == 1)) { if (dialog) engine_stadium_action("stadium-cancel", nullptr); else if (stadium_view_.at("view") != "inside") engine_stadium_action("stadium-back", nullptr); else return false; event.StopImmediatePropagation(); return true; } if (!dialog && stadium_view_.at("view") == "panorama" && (type == "mousemove" || type == "focus" || type == "blur")) { std::string hint; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->HasAttribute("data-stadium-hint")) { hint = n->GetAttribute("data-stadium-hint", ""); break; } if (type == "blur") hint.clear(); if (stadium_view_.at("hint") != hint) { stadium_view_["hint"] = hint; if (auto *n = element("stadium-interaction-hint")) n->SetInnerRML("" + esc(hint.empty() ? "Ein Element im Stadionumfeld auswählen." : hint) + ""); dirty_ = true; } } Rml::Element *range = nullptr; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->GetId() == "stadium-place-range") { range = n; break; } const auto update = [&] { engine_stadium_send(engine_stadium_request(engine_data_, stadium_view_.at("draft"), editor::new_id("request:ui-"), true), "update"); }; if (dialog && stadium_view_.at("dialog") == "block") { if (range && (type == "input" || type == "change")) { if (auto *control = dynamic_cast(range)) { const auto places = stadium_integer(control->GetValue(), 255000); if (places) { stadium_view_["draft"]["places"] = *places; sync_stadium_draft(); } } event.StopImmediatePropagation(); return true; } if (range && type == "mouseup") { if (stadium_view_.at("draft") != engine_stadium_confirmed_) update(); event.StopImmediatePropagation(); return true; } if (range && key_event && (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 auto row = stadium_overview(false).at("blocks").at( stadium_view_.at("draft").at("index").get()); int places = stadium_view_.at("draft").at("places"); places = key == Rml::Input::KI_HOME ? row.at("places").get() : key == Rml::Input::KI_END ? row.at("max_places").get() : places + ((key == Rml::Input::KI_LEFT || key == Rml::Input::KI_DOWN) ? -1000 : 1000); stadium_view_["draft"]["places"] = std::clamp(places, row.at("places").get(), row.at("max_places").get()); update(); event.StopImmediatePropagation(); return true; } if (type == "change" && target && target->HasAttribute("data-stadium-feature")) { const auto field = target->GetAttribute("data-stadium-feature", ""); if (field == "seats" || field == "roof") { stadium_view_["draft"][field] = target->HasAttribute("checked"); update(); event.StopImmediatePropagation(); return true; } } } if (dialog && key_event && key == Rml::Input::KI_TAB) { Rml::ElementList nodes; if (auto *root = element("stadium-draft")) root->QuerySelectorAll(nodes, "button,input"); std::erase_if(nodes, [](auto *n) { return n->HasAttribute("disabled") || !n->IsVisible(true); }); if (!nodes.empty()) { const auto at = std::find(nodes.begin(), nodes.end(), context_->GetFocusElement()); const int current = at == nodes.end() ? -1 : int(at - nodes.begin()), count = int(nodes.size()); nodes[size_t((current + count + (event.GetParameter("shift_key", 0) ? -1 : 1)) % count)] ->Focus(true); } event.StopImmediatePropagation(); return true; } if (dialog) { bool inside = false; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->GetId() == "stadium-modal") inside = true; if (!inside && (type == "click" || type == "mousedown" || type == "mouseup" || key_event)) { event.StopImmediatePropagation(); return true; } } return false; }