// SPDX-License-Identifier: GPL-3.0-only // OpenBMH deviates from the original: viewing a balance is read-only and publishing a changed plan requires explicit confirmation. #include "modern/ui/engine_bridge.hpp" #include #include namespace bmh::modern_ui { namespace { void check(bool yes, const char *text) { if (!yes) throw editor::Error(text); } const Json &balance_read(const Json &b, const Json &r) { validate_engine_desk(b); check(r.value("messageType", "") == "snapshot" && r.at("data").value("kind", "") == "balance", "Die Bilanz ist hier nicht verfügbar."); for (auto *k : {"contractVersion", "careerId", "worldId", "worldRevision", "stateRevision", "scope"}) check(r.at("meta").at(k) == b.at("desktop").at("meta").at(k), "Die Bilanz gehört zu einem anderen Spielstand."); check(r.at("data").at("teamId") == b.at("desktop").at("meta").at("scope").at("teamId") && r.at("meta").at("scope").at("role") == "club", "Die Bilanz gehört einem anderen Verein."); return r.at("data"); } } // namespace Json engine_balance_projection(const Json &b, const Json &r) { const auto &d = balance_read(b, r); check(d.at("items").size() == 18 && d.at("summaries").size() == 6, "Die Bilanz ist unvollständig."); Json income = Json::array(), costs = Json::array(), plan = Json::array(); std::set ids; for (const auto &row : d.at("items")) { const auto id = row.at("id").get(); check(ids.insert(id).second, "Doppelter Bilanzposten."); Json entry = { {"id", id}, {"name", row.at("label")}, {"amount", engine_money_minor(row.at("amount"))}, {"editable", row.at("editable")}, {"maximum", row.at("maximum").is_null() ? Json() : Json(engine_money_minor(row.at("maximum")) / 100)}}; if (row.at("section") == "income") income.push_back(entry); else { check(row.at("section") == "costs", "Unbekannter Bilanzbereich."); costs.push_back(entry); plan.push_back(entry); } } const int64_t dep = engine_money_minor(d.at("depreciation")); Json depreciation = {{"id", "balance:depreciation"}, {"name", d.at("depreciationLabel")}, {"amount", dep < 0 ? -dep : dep}, {"editable", false}, {"maximum", nullptr}}; if (dep < 0) income.push_back(depreciation); else costs.push_back(depreciation); const auto &s = d.at("summaries"); return {{"ok", true}, {"income", income}, {"costs", costs}, {"plan", plan}, {"plan_total", engine_money_minor(d.at("costs"))}, {"league_home_games", s[0].at("value")}, {"other_home_games", s[1].at("value")}, {"attendance_average", s[4].at("value")}, {"ticket", engine_money_minor(s[3].at("value"))}, {"cash", engine_money_minor(s[5].at("value"))}, {"balance", engine_money_minor(d.at("net"))}, {"warning", d.at("warning")}, {"warning_text", d.at("warningText")}}; } Json engine_balance_command(const Json &b, const Json &r, const std::string &id) { const auto &d = balance_read(b, r), &m = b.at("desktop").at("meta"); const auto &commands = m.at("scope").at("allowedCommands"); check(!id.empty() && std::find(commands.begin(), commands.end(), Json("balance.commit")) != commands.end(), "Planänderungen sind hier gesperrt."); check(d.at("view") == "future" && d.at("edits").is_array() && !d.at("edits").empty(), "Kein bestätigter Planentwurf vorhanden."); (void)engine_balance_projection(b, r); return {{"messageType", "command"}, {"requestId", id}, {"careerId", m.at("careerId")}, {"expectedRevision", m.at("stateRevision")}, {"owner", {{"managerId", m.at("scope").at("managerId")}}}, {"kind", "balance.commit"}, {"payload", {{"view", "future"}, {"edits", d.at("edits")}, {"acceptWarning", d.at("warning")}}}}; } } // namespace bmh::modern_ui