// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/demo_settings.hpp" #include "modern/ui/engine_bridge.hpp" #include namespace bmh::modern_ui { namespace { void require_settings(bool yes, const char *message) { if (!yes) throw editor::Error(message); } } // namespace Json engine_settings_projection(const Json &bundle) { validate_engine_desk(bundle); const auto &read = bundle.at("settings"), &meta = bundle.at("desktop").at("meta"); require_settings(read.value("messageType", "") == "snapshot" && read.at("data").value("kind", "") == "settings", "Einstellungen sind nicht verfügbar."); for (auto *key : {"contractVersion", "careerId", "worldId", "worldRevision", "stateRevision", "scope"}) require_settings(read.at("meta").at(key) == meta.at(key), "Die Einstellungen sind veraltet."); const auto &data = read.at("data"); require_settings(data.at("cells").is_array() && data.at("cells").size() == 180, "Unvollständige Spieloptionen."); Json cells = Json::array(), capabilities = Json::object(), automation = Json::object(), auto_caps = Json::object(); for (size_t i = 0; i < 180; ++i) { const auto &row = data.at("cells")[i]; const auto code = row.at("code").get(); require_settings( settings_index(code) == int(i) && row.at("raw").is_number_integer() && row.at("raw") >= 0 && row.at("raw") <= 255 && row.at("displayRaw").is_number_integer() && row.at("displayRaw") >= 0 && row.at("displayRaw") <= 255 && row.at("editable").is_boolean(), "Ungültige Einstellungszelle."); const auto &value = row.at("value"); require_settings(value.is_null() || (value.is_number_integer() && value >= 0 && value <= 255), "Ungültiger Einstellungswert."); require_settings(row.at("min").is_number_integer() && row.at("max").is_number_integer() && row.at("min") >= 0 && row.at("max") >= row.at("min") && row.at("max") <= 255 && row.at("step") == 1, "Ungültige Einstellungsgrenze."); if (i % 9 < 7) { const int binary = row.at("raw").get() & 0x3f; require_settings( binary <= 1 ? value == 1 - binary : value.is_null(), "Die Schalterbedeutung passt nicht zu den Originalbytes."); } if (row.at("editable") == true) require_settings(value.is_number_integer() && value >= row.at("min") && value <= row.at("max"), "Schreibbarer Wert außerhalb der Grenzen."); cells.push_back(value); capabilities[code] = row; } const std::set keys{"lineup", "substitutions", "tactics", "marking", "training"}; for (const auto &row : data.at("automation")) { const auto key = row.at("id").get(); require_settings(keys.contains(key) && !auto_caps.contains(key) && row.at("enabled").is_boolean() && row.at("editable").is_boolean(), "Ungültige Automatik."); automation[key] = row.at("enabled"); auto_caps[key] = row; } require_settings(auto_caps.size() == 5 && data.at("formations").is_array(), "Unvollständige Automatik."); std::set slots; for (const auto &slot : data.at("formations")) require_settings(slot.at("value").is_number_integer() && slot.at("value") >= 1 && slot.at("value") <= 8 && slots.insert(slot.at("value").get()).second && slot.at("label").is_string(), "Ungültige gespeicherte Aufstellung."); require_settings(data.at("formation").is_number_integer() && (data.at("formation") == 0 || slots.contains(data.at("formation").get())), "Gespeicherte Aufstellung nicht verfügbar."); return {{"local", {{"version", 1}, {"cells", cells}, {"automation", automation}, {"formation", data.at("formation")}}}, {"cells", capabilities}, {"automation", auto_caps}, {"formations", data.at("formations")}}; } Json engine_settings_command(const Json &bundle, const Json &draft, const std::string &request_id) { const auto value = engine_settings_projection(bundle), &base = value.at("local"), &meta = bundle.at("desktop").at("meta"); const auto commands = meta.at("scope").at("allowedCommands"); require_settings(!request_id.empty() && std::find(commands.begin(), commands.end(), Json("settings.commit")) != commands.end(), "Einstellungsänderungen sind gesperrt."); require_settings(draft.at("version") == 1 && draft.at("cells").is_array() && draft.at("cells").size() == 180 && draft.at("automation").is_object() && draft.at("automation").size() == 5, "Ungültiger Einstellungsentwurf."); Json edits = Json::array(), autos = Json::array(); for (size_t i = 0; i < 180; ++i) { if (draft.at("cells")[i] == base.at("cells")[i]) continue; const auto code = std::string(1, char('A' + i / 9)) + char('1' + i % 9); const auto &cap = value.at("cells").at(code), &v = draft.at("cells")[i]; require_settings(cap.at("editable") == true && v.is_number_integer() && v >= cap.at("min") && v <= cap.at("max"), "Gesperrte oder ungültige Spieloption."); edits.push_back({{"code", code}, {"value", v}}); } for (const auto &[key, before] : base.at("automation").items()) { const auto &on = draft.at("automation").at(key); require_settings(on.is_boolean(), "Ungültige Automatik."); const bool slot_changed = key == "lineup" && on == true && draft.at("formation") != base.at("formation"); if (on == before && !slot_changed) continue; require_settings(value.at("automation").at(key).at("editable") == true, "Diese Automatik ist gesperrt."); Json row = {{"id", key}, {"enabled", on}}; if (key == "lineup" && on == true) { require_settings(draft.at("formation").is_number_integer(), "Bitte eine gespeicherte Aufstellung wählen."); bool found = false; for (const auto &slot : value.at("formations")) found |= slot.at("value") == draft.at("formation"); require_settings(found, "Diese Aufstellung ist nicht verfügbar."); row["formation"] = draft.at("formation"); } autos.push_back(row); } return {{"messageType", "command"}, {"requestId", request_id}, {"careerId", meta.at("careerId")}, {"expectedRevision", meta.at("stateRevision")}, {"owner", {{"managerId", meta.at("scope").at("managerId")}}}, {"kind", "settings.commit"}, {"payload", {{"edits", edits}, {"automation", autos}}}}; } } // namespace bmh::modern_ui