// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/engine_bridge.hpp" #include #include namespace bmh::modern_ui { namespace { void check(bool yes, const char *message) { if (!yes) throw editor::Error(message); } const Json &read(const Json &bundle) { validate_engine_desk(bundle); const auto &r = bundle.at("stadium"); check(r.value("messageType", "") == "snapshot" && r.at("data").value("kind", "") == "stadium", "Stadion ist derzeit nicht verfügbar."); for (const auto *key : {"careerId", "worldId", "worldRevision", "stateRevision", "scope"}) check(r.at("meta").at(key) == bundle.at("desktop").at("meta").at(key), "Der Stadionkontext hat sich geändert."); return r.at("data"); } Json command(const Json &b, const std::string &kind, const Json &payload, const std::string &id) { const auto &m = b.at("desktop").at("meta"); const auto &allowed = m.at("scope").at("allowedCommands"); check(std::find(allowed.begin(), allowed.end(), Json(kind)) != allowed.end(), "Diese Stadionaktion ist derzeit gesperrt."); return {{"messageType", "command"}, {"contractVersion", m.at("contractVersion")}, {"careerId", m.at("careerId")}, {"requestId", id}, {"expectedRevision", m.at("stateRevision")}, {"owner", {{"managerId", m.at("scope").at("managerId")}}}, {"kind", kind}, {"payload", payload}}; } } // namespace Json engine_stadium_projection(const Json &bundle, bool future) { const auto &d = read(bundle); check(d.at("blocks").size() == 8 && d.at("facilities").size() == 8 && d.at("periphery").size() == 14, "Unvollständiges Stadion."); Json blocks = Json::array(), facilities = Json::array(), periphery = Json::array(); int64_t available = 0, planned = 0, physical = 0; bool super = true; for (size_t i = 0; i < 8; ++i) { const auto &b = d.at("blocks")[i]; const int weeks = b.at("constructionWeeks"); const bool busy = weeks > 0; const int64_t current = b.at("places"), target = busy ? b.at("placesAfterConstruction").get() : current; const bool seats = future && busy ? b.at("seatsAfterConstruction").get() : b.at("seats").get(), roof = future && busy ? b.at("roofAfterConstruction").get() : b.at("roof").get(); const int64_t display = busy && !future ? 0 : future ? target : current; blocks.push_back({{"id", b.at("id")}, {"index", i}, {"letter", std::string(1, char('A' + i))}, {"name", b.at("name")}, {"places", current}, {"max_places", b.at("maxPlaces")}, {"seats", b.at("seats")}, {"roof", b.at("roof")}, {"building", busy}, {"display_places", display}, {"display_seats", seats}, {"display_roof", roof}, {"weeks", 0}, {"job", busy ? Json{{"places", target}, {"seats", b.at("seatsAfterConstruction")}, {"roof", b.at("roofAfterConstruction")}, {"weeks", weeks}} : Json()}}); available += busy ? 0 : current; physical += current; planned += target; super = super && (future ? target : current) == b.at("maxPlaces").get() && seats && roof; } for (size_t i = 0; i < 8; ++i) { const auto &f = d.at("facilities")[i]; const int level = f.at("level"), weeks = f.at("constructionWeeks"), target = f.value("plannedLevel", level), shown = future && weeks > 0 ? target : level; Json labels = i == 2 || i == 3 ? Json({"—", "nicht vorhanden", "vorhanden"}) : i < 2 ? Json({"—", "klein", "mittel", "groß", "mega"}) : Json({"—", "schlecht", "ausreichend", "gut", "sehr gut"}); check(shown >= 0 && shown < int(labels.size()), "Ungültige Ausstattungsstufe."); facilities.push_back({{"id", f.at("id")}, {"index", i}, {"name", f.at("name")}, {"level", level}, {"max_level", f.at("maxLevel")}, {"display_level", shown}, {"display_label", labels[size_t(shown)]}, {"levels", labels}, {"weeks", 0}, {"rate", nullptr}, {"building", weeks > 0}, {"available", f.at("upgradeAvailable")}, {"job", weeks > 0 ? Json{{"level", target}, {"weeks", weeks}} : Json()}}); } for (size_t i = 0; i < 14; ++i) { const auto &p = d.at("periphery")[i]; const int level = i == 0 ? p.at("stage").get() : p.at("built") == true ? 1 : 0, max = i == 0 ? p.at("maxStage").get() : 1, days = p.at("constructionDays"); const bool busy = days > 0; const int target = std::min(max, level + 1); periphery.push_back({{"id", p.at("id")}, {"index", i}, {"name", p.at("name")}, {"level", level}, {"max_level", max}, {"display_level", future && busy ? target : level}, {"building", busy}, {"job", busy ? Json{{"level", target}, {"weeks", days}} : Json()}}); } return {{"ok", true}, {"club", d.at("teamId")}, {"name", d.value("name", std::string("Stadion"))}, {"budget", engine_money_minor(bundle.at("desktop").at("data").at("accounts").at("cash"))}, {"blocks", blocks}, {"facilities", facilities}, {"periphery", periphery}, {"available_capacity", available}, {"planned_capacity", planned}, {"physical_capacity", future ? planned : physical}, {"capacity", future ? planned : available}, {"super_stadium", super}}; } Json engine_stadium_quote(const Json &bundle, const Json &draft) { const auto &d = read(bundle); const auto &q = d.at("quote"); if (!q.is_object()) return {{"ok", false}, {"can_build", false}}; const auto kind = draft.at("kind").get(); check(q.at("kind") == kind, "Das Angebot gehört zu einer anderen Ausbauart."); const int index = draft.at("index"); const auto &row = d.at(kind == "block" ? "blocks" : kind == "facility" ? "facilities" : "periphery") .at(size_t(index)); check(q.at(kind == "block" ? "blockId" : kind == "facility" ? "facilityId" : "objectId") == row.at("id"), "Das Angebot gehört zu einem anderen Objekt."); const int64_t cost = engine_money_minor(q.at("price")); Json result = {{"ok", true}, {"can_build", cost > 0}, {"cost", cost}, {"company_percent", q.value("pricePercent", 100)}, {"weeks", q.value("weeks", 0)}, {"options", Json::array()}}; if (kind == "block") { check(q.at("places") == draft.at("places") && q.at("seats") == draft.at("seats") && q.at("roof") == draft.at("roof"), "Das Blockangebot passt nicht zum Entwurf."); for (const auto &part : q.at("breakdown")) { const auto code = part.at("code").get(); result[code == "places" ? "placesCost" : code == "seats" ? "seatsCost" : "roofCost"] = engine_money_minor(part.at("amount")); } } if (kind == "facility") { check(q.at("toLevel") == draft.at("level"), "Die Angebotsstufe hat sich geändert."); for (const auto &option : q.at("options")) result["options"].push_back( {{"level", option.at("toLevel")}, {"cost", engine_money_minor(option.at("price"))}}); } return result; } Json engine_stadium_request(const Json &bundle, const Json &draft, const std::string &id, bool update) { const auto &d = read(bundle); const auto kind = draft.at("kind").get(); check(kind == "block" || kind == "facility" || kind == "periphery", "Unbekannte Ausbauart."); const int index = draft.at("index"); const auto &rows = d.at(kind == "block" ? "blocks" : kind == "facility" ? "facilities" : "periphery"); check(index >= 0 && size_t(index) < rows.size(), "Unbekanntes Stadionobjekt."); const auto &row = rows[size_t(index)]; Json payload = {{"kind", kind}}; if (kind == "block") { payload["blockId"] = row.at("id"); payload["places"] = draft.at("places"); payload["seats"] = draft.at("seats"); payload["roof"] = draft.at("roof"); } else if (kind == "facility") { payload["facilityId"] = row.at("id"); payload["increment"] = draft.at("level").get() - row.at("level").get(); } else payload["objectId"] = row.at("id"); if (update) { check(kind != "periphery" && d.at("quote").is_object(), "Kein bearbeitbares Angebot vorhanden."); payload["quoteId"] = d.at("quote").at("id"); } return command(bundle, "quote.request", payload, id); } Json engine_stadium_commit(const Json &bundle, bool accept, const std::string &id) { const auto &d = read(bundle); check(d.at("quote").is_object(), "Kein Stadionangebot vorhanden."); return command(bundle, "stadium.commit", {{"quoteId", d.at("quote").at("id")}, {"accept", accept}, {"tip", false}}, id); } } // namespace bmh::modern_ui