Port stadium blocks and facilities without periphery construction (by Codex)
This commit is contained in:
1 parent
0f36b507d4
commit
349c416f3a
21 files changed
+1185
-12
No files matched your search
@@ -0,0 +1,261 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_stadium.hpp"
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
constexpr int64_t budget_units = 50000000;
|
||||
constexpr std::array<int, 8> capacities{8000,18000,8000,14000,8000,18000,8000,14000};
|
||||
constexpr std::array<int, 8> initial_places{3000,6000,3000,5000,3000,6000,3000,7000};
|
||||
constexpr std::array<int, 8> initial_levels{1,2,1,1,2,2,2,1};
|
||||
constexpr std::array<int, 8> block_rates{300000,375000,300000,350000,300000,375000,300000,350000};
|
||||
constexpr std::array<int, 8> block_weeks{30,37,30,25,30,37,30,25};
|
||||
constexpr std::array<const char *, 8> block_names{
|
||||
"Nordwest-Kurve", "Nord-Tribüne", "Nordost-Kurve", "Ost-Block",
|
||||
"Südost-Kurve", "Süd-Tribüne", "Südwest-Kurve", "West-Block"};
|
||||
constexpr std::array<const char *, 8> facility_names{
|
||||
"Flutlichtanlage", "Anzeigetafel", "Rasenheizung", "V.I.P.-Lounge",
|
||||
"Rasenzustand", "Stadionkomfort", "Stadionzustand", "Stadionsicherheit"};
|
||||
constexpr std::array<int, 8> facility_rates{250000,350000,850000,1200000,180000,200000,300000,400000};
|
||||
constexpr std::array<int, 8> facility_weeks{12,15,16,18,8,5,10,14};
|
||||
bool keys(const Json &value, std::initializer_list<const char *> expected) {
|
||||
return value.is_object() && value.size() == expected.size() &&
|
||||
std::all_of(expected.begin(), expected.end(), [&](auto *key) { return value.contains(key); });
|
||||
}
|
||||
bool integer(const Json &value, int64_t low, int64_t high) {
|
||||
if (value.is_number_unsigned())
|
||||
return high >= 0 && value.get<uint64_t>() <= uint64_t(high) &&
|
||||
(low <= 0 || value.get<uint64_t>() >= uint64_t(low));
|
||||
return value.is_number_integer() && value.get<int64_t>() >= low && value.get<int64_t>() <= high;
|
||||
}
|
||||
int facility_cap(size_t index) { return index == 2 || index == 3 ? 2 : 4; }
|
||||
Json error(const char *code) { return {{"ok", false}, {"can_build", false}, {"error", code}}; }
|
||||
std::string owner(const Catalog &world, const Json *state, std::string requested) {
|
||||
if (requested.empty() && state && state->is_object()) {
|
||||
const auto managers = state->find("managers");
|
||||
if (managers != state->end() && managers->is_array() && !managers->empty() && (*managers)[0].is_object()) {
|
||||
const auto club = (*managers)[0].find("club");
|
||||
if (club != (*managers)[0].end() && club->is_string()) requested = club->get<std::string>();
|
||||
}
|
||||
}
|
||||
if (!requested.empty()) {
|
||||
if (!world.manageable(requested)) throw editor::Error("invalid_stadium_club");
|
||||
return requested;
|
||||
}
|
||||
for (const auto *club : world.clubs())
|
||||
if (world.manageable(club->at("id").get<std::string>())) return club->at("id").get<std::string>();
|
||||
return {};
|
||||
}
|
||||
bool block_target(const Json &base, const Json &target, size_t index) {
|
||||
return integer(target.at("places"), base.at("places").get<int64_t>(), capacities[index]) &&
|
||||
target.at("places").get<int64_t>() % 1000 == 0 &&
|
||||
target.at("seats").is_boolean() && target.at("roof").is_boolean() &&
|
||||
!(base.at("seats").get<bool>() && !target.at("seats").get<bool>()) &&
|
||||
!(base.at("roof").get<bool>() && !target.at("roof").get<bool>());
|
||||
}
|
||||
Json block_price(const Json &base, const Json &target, size_t index, int64_t factor) {
|
||||
const auto added = target.at("places").get<int64_t>() - base.at("places").get<int64_t>();
|
||||
const int64_t rate = block_rates[index] * factor;
|
||||
const int64_t places = added / 1000 * rate;
|
||||
// All fixed rates and legal thousand-place steps divide exactly. This is
|
||||
// Gold's nearest-integer calculation without floating-point currency drift.
|
||||
const int64_t seats = target.at("seats").get<bool>()
|
||||
? (base.at("seats").get<bool>() ? added : target.at("places").get<int64_t>()) / 1000 * rate * 45 / 100 : 0;
|
||||
const int64_t roof = target.at("roof").get<bool>()
|
||||
? (base.at("roof").get<bool>() ? added : target.at("places").get<int64_t>()) / 1000 * rate * 30 / 100 : 0;
|
||||
return {{"cost", places + seats + roof}, {"weeks", block_weeks[index]},
|
||||
{"placesCost", places}, {"seatsCost", seats}, {"roofCost", roof}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json demo_stadium(const Catalog &world, const std::string &club) {
|
||||
Json blocks = Json::array(), facilities = Json::array();
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
blocks.push_back({{"places", initial_places[i]}, {"seats", false}, {"roof", false}, {"job", nullptr}});
|
||||
facilities.push_back({{"level", initial_levels[i]}, {"job", nullptr}});
|
||||
}
|
||||
return {{"club", owner(world, nullptr, club)}, {"budget", budget_units * finance_currency_factor(world)},
|
||||
{"blocks", blocks}, {"facilities", facilities}};
|
||||
}
|
||||
|
||||
bool valid_demo_stadium(const Json &state, const Catalog &world, const std::string &club) {
|
||||
try {
|
||||
if (!state.is_object()) return false;
|
||||
const int64_t factor = finance_currency_factor(world), cap = budget_units * factor;
|
||||
const auto &stadium = state.at("stadium");
|
||||
if (!keys(stadium, {"club", "budget", "blocks", "facilities"}) ||
|
||||
!stadium.at("club").is_string() || stadium.at("club") != owner(world, &state, club) ||
|
||||
!integer(stadium.at("budget"), 0, cap) ||
|
||||
!stadium.at("blocks").is_array() || stadium.at("blocks").size() != 8 ||
|
||||
!stadium.at("facilities").is_array() || stadium.at("facilities").size() != 8) return false;
|
||||
int64_t pending = 0;
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
const auto &base = stadium.at("blocks")[i];
|
||||
if (!keys(base, {"places", "seats", "roof", "job"}) ||
|
||||
!integer(base.at("places"), 0, capacities[i]) || base.at("places").get<int64_t>() % 1000 ||
|
||||
!base.at("seats").is_boolean() || !base.at("roof").is_boolean()) return false;
|
||||
const auto &job = base.at("job");
|
||||
if (job.is_null()) continue;
|
||||
if (!keys(job, {"places", "seats", "roof", "weeks", "cost"}) ||
|
||||
!block_target(base, job, i) || !integer(job.at("cost"), 1, cap)) return false;
|
||||
const auto quote = block_price(base, job, i, factor);
|
||||
if (!integer(job.at("weeks"), block_weeks[i], block_weeks[i]) ||
|
||||
job.at("cost") != quote.at("cost")) return false;
|
||||
const auto cost = job.at("cost").get<int64_t>();
|
||||
if (cost > cap - pending) return false;
|
||||
pending += cost;
|
||||
}
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
const auto &base = stadium.at("facilities")[i];
|
||||
if (!keys(base, {"level", "job"}) || !integer(base.at("level"), 1, facility_cap(i))) return false;
|
||||
const auto &job = base.at("job");
|
||||
if (job.is_null()) continue;
|
||||
if (!keys(job, {"level", "weeks", "cost"}) ||
|
||||
!integer(job.at("level"), base.at("level").get<int64_t>() + 1, facility_cap(i)) ||
|
||||
!integer(job.at("weeks"), facility_weeks[i], facility_weeks[i]) ||
|
||||
!integer(job.at("cost"), 1, cap)) return false;
|
||||
const int64_t cost = (job.at("level").get<int64_t>() - base.at("level").get<int64_t>()) * facility_rates[i] * factor;
|
||||
if (job.at("cost") != cost || cost > cap - pending) return false;
|
||||
pending += cost;
|
||||
}
|
||||
return stadium.at("budget").get<int64_t>() == cap - pending;
|
||||
} catch (const std::exception &) { return false; }
|
||||
}
|
||||
|
||||
bool migrate_demo_stadium(Json &state, const Catalog &world, const std::string &club) {
|
||||
try {
|
||||
if (!state.is_object()) return false;
|
||||
if (state.contains("stadium")) return valid_demo_stadium(state, world, club);
|
||||
auto next = state;
|
||||
next["stadium"] = demo_stadium(world, owner(world, &state, club));
|
||||
if (!valid_demo_stadium(next, world, club)) return false;
|
||||
state = std::move(next);
|
||||
return true;
|
||||
} catch (const std::exception &) { return false; }
|
||||
}
|
||||
|
||||
Json demo_stadium_quote(const Json &state, const Json &request, const Catalog &world, const std::string &club) {
|
||||
if (!valid_demo_stadium(state, world, club)) return error("invalid_stadium_state");
|
||||
try {
|
||||
if (!request.is_object() || !request.contains("kind") || !request.at("kind").is_string() ||
|
||||
!request.contains("index") || !integer(request.at("index"), 0, 7)) return error("invalid_stadium_request");
|
||||
const auto kind = request.at("kind").get<std::string>();
|
||||
const auto index = request.at("index").get<size_t>();
|
||||
const auto &stadium = state.at("stadium");
|
||||
const int64_t factor = finance_currency_factor(world);
|
||||
Json quote;
|
||||
bool pending = false;
|
||||
if (kind == "block") {
|
||||
const auto &base = stadium.at("blocks")[index];
|
||||
if (!keys(request, {"kind", "index", "places", "seats", "roof"}) ||
|
||||
!block_target(base, request, index)) return error("invalid_stadium_block_target");
|
||||
quote = block_price(base, request, index, factor);
|
||||
pending = !base.at("job").is_null();
|
||||
} else if (kind == "facility") {
|
||||
const auto &base = stadium.at("facilities")[index];
|
||||
if (!keys(request, {"kind", "index", "level"}) ||
|
||||
!integer(request.at("level"), base.at("level").get<int64_t>(), facility_cap(index)))
|
||||
return error("invalid_stadium_facility_target");
|
||||
quote = {{"cost", (request.at("level").get<int64_t>() - base.at("level").get<int64_t>()) * facility_rates[index] * factor},
|
||||
{"weeks", facility_weeks[index]}, {"placesCost", 0}, {"seatsCost", 0}, {"roofCost", 0}};
|
||||
pending = !base.at("job").is_null();
|
||||
} else return error("unknown_stadium_request");
|
||||
const auto cost = quote.at("cost").get<int64_t>(), budget = stadium.at("budget").get<int64_t>();
|
||||
const auto reason = pending ? "stadium_pending" : !cost ? "stadium_no_change"
|
||||
: cost > budget ? "stadium_budget_exceeded" : "";
|
||||
quote["ok"] = true; quote["can_build"] = !*reason; quote["error"] = reason;
|
||||
quote["kind"] = kind; quote["index"] = index; quote["request"] = request;
|
||||
quote["budget"] = budget; quote["remaining"] = budget - cost;
|
||||
return quote;
|
||||
} catch (const std::exception &) { return error("invalid_stadium_request"); }
|
||||
}
|
||||
|
||||
std::string edit_demo_stadium(Json &state, const Json &action, const Catalog &world, const std::string &club) {
|
||||
if (!valid_demo_stadium(state, world, club)) return "invalid_stadium_state";
|
||||
try {
|
||||
if (!keys(action, {"kind", "request"}) || action.at("kind") != "build") return "invalid_stadium_action";
|
||||
const auto quote = demo_stadium_quote(state, action.at("request"), world, club);
|
||||
if (!quote.value("ok", false) || !quote.value("can_build", false)) return quote.value("error", std::string("invalid_stadium_request"));
|
||||
const auto &request = quote.at("request");
|
||||
const auto index = quote.at("index").get<size_t>();
|
||||
Json job = {{"weeks", quote.at("weeks")}, {"cost", quote.at("cost")}};
|
||||
auto next = state;
|
||||
if (quote.at("kind") == "block") {
|
||||
for (const auto *key : {"places", "seats", "roof"}) job[key] = request.at(key);
|
||||
next["stadium"]["blocks"][index]["job"] = job;
|
||||
} else {
|
||||
job["level"] = request.at("level");
|
||||
next["stadium"]["facilities"][index]["job"] = job;
|
||||
}
|
||||
next["stadium"]["budget"] = quote.at("remaining");
|
||||
if (!valid_demo_stadium(next, world, club)) return "invalid_stadium_state";
|
||||
state = std::move(next);
|
||||
return {};
|
||||
} catch (const std::exception &) { return "invalid_stadium_action"; }
|
||||
}
|
||||
|
||||
Json demo_stadium_rules(const Catalog &world) {
|
||||
const auto factor = finance_currency_factor(world);
|
||||
Json blocks = Json::array(), facilities = Json::array();
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
blocks.push_back({{"index", i}, {"letter", std::string(1, char('A' + i))}, {"name", block_names[i]},
|
||||
{"max_places", capacities[i]}, {"rate", block_rates[i] * factor}, {"weeks", block_weeks[i]}});
|
||||
const Json levels = i == 2 || i == 3 ? Json({"", "nicht vorhanden", "vorhanden"})
|
||||
: i < 2 ? Json({"", "klein", "mittel", "groß", "mega"})
|
||||
: Json({"", "schlecht", "ausreichend", "gut", "sehr gut"});
|
||||
facilities.push_back({{"index", i}, {"name", facility_names[i]}, {"max_level", facility_cap(i)},
|
||||
{"rate", facility_rates[i] * factor}, {"weeks", facility_weeks[i]}, {"levels", levels}});
|
||||
}
|
||||
return {{"budget_cap", budget_units * factor}, {"blocks", blocks}, {"facilities", facilities}};
|
||||
}
|
||||
|
||||
Json demo_stadium_view(const Json &state, const Catalog &world, bool future, const std::string &club) {
|
||||
if (!valid_demo_stadium(state, world, club)) return error("invalid_stadium_state");
|
||||
const auto &stadium = state.at("stadium");
|
||||
const auto id = stadium.at("club").get<std::string>();
|
||||
const auto *team = world.find("clubs", id);
|
||||
const Json *record = nullptr;
|
||||
if (team) {
|
||||
if (auto ref = team->find("stadium"); ref != team->end() && ref->is_string()) record = world.find("stadiums", ref->get<std::string>());
|
||||
if (!record) for (const auto &candidate : world.rows("stadiums")) {
|
||||
const auto ref = candidate.find("owner");
|
||||
if (ref != candidate.end() && ref->is_string() && ref->get<std::string>() == id) { record = &candidate; break; }
|
||||
}
|
||||
}
|
||||
auto rules = demo_stadium_rules(world);
|
||||
int64_t pending = 0, available = 0, planned = 0;
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
const auto &base = stadium.at("blocks")[i];
|
||||
const auto &job = base.at("job");
|
||||
const bool building = !job.is_null();
|
||||
const auto &display = future && building ? job : base;
|
||||
auto &row = rules["blocks"][i];
|
||||
for (const auto *key : {"places", "seats", "roof", "job"}) row[key] = base.at(key);
|
||||
row["building"] = building;
|
||||
row["display_places"] = building && !future ? Json(0) : display.at("places");
|
||||
row["display_seats"] = display.at("seats"); row["display_roof"] = display.at("roof");
|
||||
available += building ? 0 : base.at("places").get<int64_t>();
|
||||
planned += building ? job.at("places").get<int64_t>() : base.at("places").get<int64_t>();
|
||||
if (building) pending += job.at("cost").get<int64_t>();
|
||||
}
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
const auto &base = stadium.at("facilities")[i]; const auto &job = base.at("job");
|
||||
const bool building = !job.is_null();
|
||||
auto &row = rules["facilities"][i];
|
||||
row["level"] = base.at("level"); row["job"] = job; row["building"] = building;
|
||||
const int level = (future && building ? job : base).at("level").get<int>();
|
||||
row["display_level"] = level; row["display_label"] = row.at("levels")[size_t(level)];
|
||||
if (building) pending += job.at("cost").get<int64_t>();
|
||||
}
|
||||
rules["ok"] = true; rules["club"] = id; rules["club_name"] = team ? world.name(*team) : "—";
|
||||
rules["stadium"] = record ? record->at("id") : Json();
|
||||
const auto name = record ? world.name(*record) : "";
|
||||
rules["name"] = name.empty() ? "Stadion" : name;
|
||||
rules["budget"] = stadium.at("budget"); rules["pending_cost"] = pending;
|
||||
rules["available_capacity"] = available; rules["planned_capacity"] = planned;
|
||||
rules["capacity"] = future ? planned : available;
|
||||
return rules;
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user