Bind environment and advertising to confirmed engine actions (by Codex)
This commit is contained in:
1 parent
dcf42f2da7
commit
e9483048f4
19 files changed
+395
-23
No files matched your search
@@ -387,4 +387,119 @@ Json engine_credit_command(const Json &bundle, const Json &read, const Json &edi
|
||||
// The engine replays this ordered edit list and owns fees and all loan maths.
|
||||
return sports_command(bundle, "bank.commit", {{"action", "credit"}, {"lenderId", "bank"}, {"edits", edits}, {"acceptFees", true}}, request_id);
|
||||
}
|
||||
|
||||
Json engine_environment_projection(const Json &bundle) {
|
||||
validate_engine_desk(bundle);
|
||||
const auto &data = lineup_read(bundle, "environment");
|
||||
require(bundle.at("desktop").at("meta").at("scope").at("role") == "club", "Umfeld ist nur am Vereinsschreibtisch verfügbar.");
|
||||
const std::array<const char *, 6> keys{"youth_youngest", "youth_academy", "youth_oldest", "co_trainer", "doctor", "physio"};
|
||||
const std::array<const char *, 6> ids{"youth-a", "youth-b", "youth-c", "coach", "doctor", "physio"};
|
||||
const std::array<const char *, 6> names{"Nachwuchs · Jüngste", "Nachwuchs · Mittlere", "Nachwuchs · Ältere", "Co-Trainer", "Vereinsarzt", "Masseur"};
|
||||
require(data.at("items").is_array() && data.at("items").size() == 6 && data.at("youth").is_array() && data.at("youth").size() == 3, "Unvollständiges Umfeld.");
|
||||
Json rows = Json::array(); std::set<std::string> seen;
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
const std::string id = std::string("environment:") + keys[i];
|
||||
const Json *item = nullptr, *youth = nullptr;
|
||||
for (const auto &r : data.at("items")) if (r.at("id") == id) { require(!item, "Doppelte Umfeld-ID."); item = &r; }
|
||||
require(item, "Unbekannte Umfeldzuordnung.");
|
||||
const int max = whole_in_range(item->at("maxLevel"), 0, 6), level = whole_in_range(item->at("level"), 0, max);
|
||||
const auto cost = engine_money_minor(item->at("monthly")), step = engine_money_minor(item->at("stepCost"));
|
||||
require(cost >= 0 && step >= 0 && step <= std::numeric_limits<int64_t>::max() / 6, "Ungültige Umfeldkosten.");
|
||||
Json row = {{"id", ids[i]}, {"engine_id", id}, {"name", names[i]}, {"level", level}, {"max", max}, {"cost", cost}, {"step", step},
|
||||
{"image", i < 3 ? "youth" : "staff"}, {"cell", i < 3 ? int(i) : int(i - 3)}, {"rank", nullptr}, {"league", nullptr}};
|
||||
if (i < 3) {
|
||||
for (const auto &y : data.at("youth")) if (y.at("id") == std::string("youth:") + keys[i]) { require(!youth, "Doppelte Nachwuchs-ID."); youth = &y; }
|
||||
require(youth, "Nachwuchsmannschaft fehlt.");
|
||||
// Like the original presenter: the stored place/league are zero-based.
|
||||
row["rank"] = whole_in_range(youth->at("place"), 0, 11) + 1;
|
||||
row["league"] = whole_in_range(youth->at("league"), 0, 2) + 1;
|
||||
}
|
||||
rows.push_back(row);
|
||||
}
|
||||
return {{"ok", true}, {"rows", rows}};
|
||||
}
|
||||
Json engine_environment_command(const Json &bundle, const std::string &id, int level, const std::string &request_id) {
|
||||
const auto view = engine_environment_projection(bundle);
|
||||
const Json *item = nullptr;
|
||||
for (const auto &row : view.at("rows")) if (row.at("engine_id") == id) item = &row;
|
||||
require(item && level >= 0 && level <= item->at("max").get<int>(), "Unbekannte Umfeldstufe.");
|
||||
return sports_command(bundle, "environment.commit", {{"itemId", id}, {"level", level}}, request_id);
|
||||
}
|
||||
namespace {
|
||||
std::string business_text(const Json &v) {
|
||||
if (v.is_null()) return "—";
|
||||
return v.is_string() ? v.get<std::string>() : v.value("text", "—");
|
||||
}
|
||||
}
|
||||
Json engine_advertising_projection(const Json &bundle) {
|
||||
validate_engine_desk(bundle);
|
||||
const auto &data = lineup_read(bundle, "advertising");
|
||||
require(data.at("teamId") == bundle.at("desktop").at("meta").at("scope").at("teamId"), "Werbung gehört einer anderen Mannschaft.");
|
||||
require(data.at("boards").is_array() && data.at("boards").size() == 14 && data.at("budgets").is_array() && data.at("budgets").size() == 3, "Unvollständige Werbung.");
|
||||
const auto commands = bundle.at("desktop").at("meta").at("scope").value("allowedCommands", Json::array());
|
||||
const bool contracts_allowed = commands.is_array() && std::find(commands.begin(), commands.end(), Json("advertising.accept")) != commands.end();
|
||||
const bool budgets_allowed = commands.is_array() && std::find(commands.begin(), commands.end(), Json("budget.commit")) != commands.end();
|
||||
Json slots = Json::array(), spending = Json::array();
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
const std::string id = i < 14 ? "ad:board/" + std::to_string(i) : i == 14 ? "ad:shirt" : "ad:tv";
|
||||
const std::string label = i < 14 ? "Bande " + std::to_string(i + 1) : i == 14 ? "Trikotwerbung" : "TV-Rechte";
|
||||
const Json *raw = i == 14 ? &data.at("shirt") : i == 15 ? &data.at("television") : nullptr;
|
||||
if (i < 14) for (const auto &row : data.at("boards")) if (row.at("id") == id) { require(!raw, "Doppelte Werbefläche."); raw = &row; }
|
||||
require(raw && raw->at("id") == id && raw->at("active").is_boolean(), "Ungültige Werbefläche.");
|
||||
const bool active = raw->at("active");
|
||||
const auto amount = engine_money_minor(raw->at("monthly"));
|
||||
require(amount >= 0, "Ungültiger Werbebetrag.");
|
||||
Json slot = {{"id", id}, {"slot", i}, {"label", label}, {"active", active}, {"name", active ? business_text(raw->at("sponsor")) : "Platz frei"},
|
||||
{"sponsor", active ? 0 : -1}, {"amount", amount}, {"months", whole_in_range(raw->at("monthsRemaining"), 0, 255)}, {"choices", Json::array()}};
|
||||
// sponsor here is only an artwork switch; commands use the exact offer ID.
|
||||
auto current = slot; current.erase("choices"); current["can_accept"] = false; current["boards"] = 1; current["offer_id"] = nullptr;
|
||||
if (active) slot["choices"].push_back(current);
|
||||
require(raw->at("offers").is_array() && raw->at("offers").size() <= 200, "Ungültige Angebotsliste.");
|
||||
std::set<std::string> seen;
|
||||
for (const auto &offer : raw->at("offers")) {
|
||||
const auto offer_id = offer.at("id").get<std::string>(), prefix = "offer:ad-" + std::to_string(i) + "-";
|
||||
require(offer_id.starts_with(prefix) && offer_id.size() > prefix.size() &&
|
||||
offer_id.find_first_not_of("0123456789", prefix.size()) == std::string::npos && seen.insert(offer_id).second, "Angebot gehört einer anderen Werbefläche.");
|
||||
require(offer.at("allowed").is_boolean(), "Angebotssperre fehlt.");
|
||||
const auto monthly = engine_money_minor(offer.at("monthly")); require(monthly >= 0, "Ungültiger Angebotspreis.");
|
||||
slot["choices"].push_back({{"offer_id", offer_id}, {"name", business_text(offer.at("sponsor"))}, {"label", label}, {"sponsor", 0},
|
||||
{"amount", monthly}, {"months", whole_in_range(offer.at("months"), 1, 255)}, {"boards", whole_in_range(offer.at("boards"), 1, 14)}, {"active", false}, {"can_accept", offer.at("allowed") == true && contracts_allowed}});
|
||||
}
|
||||
if (slot["choices"].empty()) { current["name"] = "Keine Angebote"; slot["choices"].push_back(current); }
|
||||
slots.push_back(slot);
|
||||
}
|
||||
const std::array<const char *, 3> budget_ids{"budget:merchandise", "budget:press", "budget:fans"}, names{"Fanartikel", "Öffentlichkeitsarbeit", "Fanbetreuung"};
|
||||
for (size_t i = 0; i < 3; ++i) {
|
||||
const Json *row = nullptr;
|
||||
for (const auto &r : data.at("budgets")) if (r.at("id") == budget_ids[i]) { require(!row, "Doppeltes Werbebudget."); row = &r; }
|
||||
require(row && row->at("available").is_boolean(), "Werbebudget fehlt.");
|
||||
const auto current = engine_money_minor(row->at("current")), max = engine_money_minor(row->at("maximum"));
|
||||
require(current >= 0 && current % 100 == 0 && max >= 0 && max % 100 == 0, "Ungültige Budgetgrenzen.");
|
||||
spending.push_back({{"id", budget_ids[i]}, {"name", names[i]}, {"amount", current}, {"max", max}, {"available", row->at("available") == true && budgets_allowed},
|
||||
{"delivery_weeks", whole_in_range(row->at("deliveryWeeks"), 0, 255)}, {"suggestion_step", i == 0 ? 50000 : 5000}});
|
||||
}
|
||||
return {{"ok", true}, {"slots", slots}, {"spending", spending}, {"income", engine_money_minor(data.at("preview").at("income"))},
|
||||
{"expenses", engine_money_minor(data.at("preview").at("expenses"))}, {"balance", engine_money_minor(data.at("preview").at("balance"))}};
|
||||
}
|
||||
Json engine_advertising_quote(const Json &bundle, int slot, int choice) {
|
||||
const auto view = engine_advertising_projection(bundle);
|
||||
require(slot >= 0 && slot < 16, "Unbekannte Werbefläche.");
|
||||
const auto &choices = view.at("slots")[size_t(slot)].at("choices");
|
||||
require(choice >= 0 && size_t(choice) < choices.size(), "Unbekanntes Werbeangebot.");
|
||||
auto out = choices[size_t(choice)]; out["can_cycle"] = choices.size() > 1;
|
||||
return out;
|
||||
}
|
||||
Json engine_advertising_command(const Json &bundle, const std::string &id, const std::string &request_id) {
|
||||
const auto view = engine_advertising_projection(bundle); bool allowed = false;
|
||||
for (const auto &s : view.at("slots")) for (const auto &c : s.at("choices")) if (c.at("offer_id") == id) allowed = c.at("can_accept");
|
||||
require(allowed, "Dieses Werbeangebot ist nicht mehr verfügbar.");
|
||||
return sports_command(bundle, "advertising.accept", {{"offerId", id}}, request_id);
|
||||
}
|
||||
Json engine_budget_command(const Json &bundle, const std::string &id, int64_t euros, const std::string &request_id) {
|
||||
const auto view = engine_advertising_projection(bundle); const Json *row = nullptr;
|
||||
for (const auto &r : view.at("spending")) if (r.at("id") == id) row = &r;
|
||||
require(row && row->at("available") == true && row->at("delivery_weeks") == 0 && euros >= 0 && euros <= 99999999 && euros <= row->at("max").get<int64_t>() / 100,
|
||||
"Budget ist gesperrt oder außerhalb des verfügbaren Bereichs.");
|
||||
return sports_command(bundle, "budget.commit", {{"budgetId", id}, {"amount", {{"currency", "EUR"}, {"minorUnits", 2}, {"minor", std::to_string(euros * 100)}}}}, request_id);
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user