Complete Modern game setup and manager selection with staged confirmation (by Codex)
This commit is contained in:
1 parent
a0bb8b47dc
commit
967520190d
18 files changed
+1784
-531
No files matched your search
+211
-58
@@ -1,25 +1,140 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/model.hpp"
|
||||
#include "modern/ui/demo_almanac.hpp"
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include "modern/ui/demo_environment.hpp"
|
||||
#include "modern/ui/demo_advertising.hpp"
|
||||
#include "modern/ui/demo_toto.hpp"
|
||||
#include "modern/ui/demo_almanac.hpp"
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
#include "modern/ui/demo_reports.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/demo_environment.hpp"
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include "modern/ui/demo_lineup.hpp"
|
||||
#include "modern/ui/demo_transfer.hpp"
|
||||
#include "modern/ui/demo_news.hpp"
|
||||
#include "modern/ui/demo_reports.hpp"
|
||||
#include "modern/ui/demo_settings.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/demo_stadium.hpp"
|
||||
#include "modern/ui/demo_statistics.hpp"
|
||||
#include "modern/ui/demo_news.hpp"
|
||||
#include "modern/ui/demo_settings.hpp"
|
||||
#include "modern/ui/demo_toto.hpp"
|
||||
#include "modern/ui/demo_transfer.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
std::optional<std::string> normalized_manager_name(std::string_view raw) {
|
||||
const auto whitespace = [](uint32_t c) {
|
||||
return c == 0x20 || c == 0xa0 || c == 0x1680 ||
|
||||
(c >= 0x2000 && c <= 0x200a) || c == 0x2028 || c == 0x2029 ||
|
||||
c == 0x202f || c == 0x205f || c == 0x3000 || c == 0xfeff;
|
||||
};
|
||||
size_t first = raw.size(), last = 0, count = 0, pending_spaces = 0;
|
||||
for (size_t at = 0; at < raw.size();) {
|
||||
const auto start = at;
|
||||
const auto byte = uint8_t(raw[at++]);
|
||||
uint32_t value = byte;
|
||||
int remaining = 0;
|
||||
if (byte >= 0xc2 && byte <= 0xdf) {
|
||||
value &= 31;
|
||||
remaining = 1;
|
||||
} else if (byte >= 0xe0 && byte <= 0xef) {
|
||||
value &= 15;
|
||||
remaining = 2;
|
||||
} else if (byte >= 0xf0 && byte <= 0xf4) {
|
||||
value &= 7;
|
||||
remaining = 3;
|
||||
} else if (byte >= 128)
|
||||
return {};
|
||||
const auto encoded_length = remaining;
|
||||
while (remaining--) {
|
||||
if (at == raw.size() || (uint8_t(raw[at]) & 0xc0) != 0x80)
|
||||
return {};
|
||||
value = value * 64 + (uint8_t(raw[at++]) & 63);
|
||||
}
|
||||
if ((encoded_length == 1 && value < 0x80) ||
|
||||
(encoded_length == 2 && value < 0x800) ||
|
||||
(encoded_length == 3 && value < 0x10000) || value > 0x10ffff ||
|
||||
(value >= 0xd800 && value <= 0xdfff) || value < 32 ||
|
||||
(value >= 127 && value <= 159))
|
||||
return {};
|
||||
if (whitespace(value)) {
|
||||
if (count)
|
||||
pending_spaces = std::min(size_t(25), pending_spaces + 1);
|
||||
continue;
|
||||
}
|
||||
if (!count)
|
||||
first = start;
|
||||
count += pending_spaces + 1;
|
||||
pending_spaces = 0;
|
||||
if (count > 24)
|
||||
return {};
|
||||
last = at;
|
||||
}
|
||||
if (!count)
|
||||
return {};
|
||||
return std::string(raw.substr(first, last - first));
|
||||
}
|
||||
namespace {
|
||||
bool bounded_integer(const Json &value, int low, int high) {
|
||||
if (!value.is_number_integer())
|
||||
return false;
|
||||
if (value.is_number_unsigned())
|
||||
return value.get<uint64_t>() >= uint64_t(low) &&
|
||||
value.get<uint64_t>() <= uint64_t(high);
|
||||
return value.get<int64_t>() >= low && value.get<int64_t>() <= high;
|
||||
}
|
||||
bool valid_start_setup(const Json &value) {
|
||||
if (!value.is_object() || value.size() != 6)
|
||||
return false;
|
||||
for (const auto &[key, range] :
|
||||
std::vector<std::pair<std::string, std::pair<int, int>>>{
|
||||
{"duration", {1, 5}},
|
||||
{"difficulty", {1, 10}},
|
||||
{"players", {1, 4}},
|
||||
{"mode", {1, 3}}})
|
||||
if (!value.contains(key) ||
|
||||
!bounded_integer(value.at(key), range.first, range.second))
|
||||
return false;
|
||||
if (!value.contains("league") || !value.at("league").is_boolean() ||
|
||||
!value.contains("world") || !value.at("world").is_boolean())
|
||||
return false;
|
||||
const bool league = value.at("league").get<bool>(),
|
||||
world = value.at("world").get<bool>();
|
||||
return (league || world) && (league || value.at("players").get<int>() == 1);
|
||||
}
|
||||
std::string normalized_managers(const Json &raw, const Catalog &world,
|
||||
Json &output) {
|
||||
if (!raw.is_array() || raw.empty() || raw.size() > 4)
|
||||
return "invalid_managers";
|
||||
std::set<std::string> clubs;
|
||||
std::set<int> portraits;
|
||||
output = Json::array();
|
||||
for (const auto &value : raw) {
|
||||
if (!value.is_object() || !value.contains("club") ||
|
||||
!value.at("club").is_string() || !value.contains("name") ||
|
||||
!value.at("name").is_string())
|
||||
return "invalid_manager";
|
||||
const auto club = value.at("club").get<std::string>();
|
||||
if (!world.manageable(club))
|
||||
return "club_locked";
|
||||
const auto name =
|
||||
normalized_manager_name(value.at("name").get<std::string>());
|
||||
const Json portrait = value.value("portrait", Json(0)),
|
||||
strength = value.value("strength_choice", Json(1));
|
||||
const Json style = value.value("portrait_style", Json("photo"));
|
||||
if (!name || !bounded_integer(portrait, 0, 11) ||
|
||||
!bounded_integer(strength, 0, 2) || !style.is_string() ||
|
||||
(style.get<std::string>() != "photo" &&
|
||||
style.get<std::string>() != "painted") ||
|
||||
!clubs.insert(club).second ||
|
||||
!portraits.insert(portrait.get<int>()).second)
|
||||
return "invalid_manager";
|
||||
auto manager = value;
|
||||
manager["name"] = *name;
|
||||
manager["portrait"] = portrait;
|
||||
manager["portrait_style"] = style;
|
||||
manager["strength_choice"] = strength;
|
||||
output.push_back(std::move(manager));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
bool valid_date(const Json &value) {
|
||||
if (!value.is_string())
|
||||
return false;
|
||||
@@ -73,9 +188,11 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
{"schema", "openbmh-ui-demo@1"},
|
||||
{"world", catalog_->manifest().value("id", "")},
|
||||
{"date", catalog_->manifest().value("start_date", "2026-07-01")},
|
||||
{"managers",
|
||||
Json::array(
|
||||
{{{"name", "Alex Berger"}, {"club", club}, {"portrait", 0}}})},
|
||||
{"managers", Json::array({{{"name", "Alex Berger"},
|
||||
{"club", club},
|
||||
{"portrait", 0},
|
||||
{"portrait_style", "photo"},
|
||||
{"strength_choice", 1}}})},
|
||||
{"cash", 12450000},
|
||||
{"deposit", 600000},
|
||||
{"debt", 0},
|
||||
@@ -96,9 +213,12 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
state_["reports"] = demo_reports(*catalog_);
|
||||
if (!migrate_demo_finance(state_, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_finance");
|
||||
if (!migrate_demo_environment(state_, *catalog_) || !migrate_demo_advertising(state_, *catalog_) || !migrate_demo_toto(state_, *catalog_))
|
||||
if (!migrate_demo_environment(state_, *catalog_) ||
|
||||
!migrate_demo_advertising(state_, *catalog_) ||
|
||||
!migrate_demo_toto(state_, *catalog_))
|
||||
throw editor::Error("invalid_demo_club");
|
||||
if (!migrate_demo_lineup(state_, *catalog_, club) || !migrate_demo_transfer(state_, *catalog_, club))
|
||||
if (!migrate_demo_lineup(state_, *catalog_, club) ||
|
||||
!migrate_demo_transfer(state_, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_squad");
|
||||
if (!migrate_demo_stadium(state_, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_stadium");
|
||||
@@ -122,7 +242,8 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
try {
|
||||
if (action.kind == "settings") {
|
||||
const auto error = edit_demo_settings(next, action.value);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "news") {
|
||||
const auto error = edit_demo_news(next, action.value, *catalog_);
|
||||
if (!error.empty())
|
||||
@@ -132,25 +253,26 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
action.value > 25)
|
||||
return {false, revision_, "invalid_demo_ticket"};
|
||||
next["ticket"] = action.value;
|
||||
} else if (action.kind == "managers") {
|
||||
if (!action.value.is_array() || action.value.empty() ||
|
||||
action.value.size() > 4)
|
||||
} else if (action.kind == "managers" || action.kind == "start") {
|
||||
const bool start = action.kind == "start";
|
||||
if (start &&
|
||||
(!action.value.is_object() || !action.value.contains("setup") ||
|
||||
!action.value.contains("managers") ||
|
||||
!valid_start_setup(action.value.at("setup"))))
|
||||
return {false, revision_, "invalid_setup"};
|
||||
const auto &raw = start ? action.value.at("managers") : action.value;
|
||||
if (start &&
|
||||
(!raw.is_array() ||
|
||||
raw.size() !=
|
||||
size_t(action.value.at("setup").at("players").get<int>())))
|
||||
return {false, revision_, "invalid_managers"};
|
||||
std::set<std::string> clubs;
|
||||
std::set<int> portraits;
|
||||
for (const auto &m : action.value) {
|
||||
const auto club = m.at("club").get<std::string>();
|
||||
const auto name = m.at("name").get<std::string>();
|
||||
if (!catalog_->manageable(club))
|
||||
return {false, revision_, "club_locked"};
|
||||
const auto portrait = m.value("portrait", 0);
|
||||
if (name.find_first_not_of(" \t\r\n") == name.npos ||
|
||||
name.size() > 160 || portrait < 0 || portrait > 11 ||
|
||||
!clubs.insert(club).second || !portraits.insert(portrait).second)
|
||||
return {false, revision_, "invalid_manager"};
|
||||
}
|
||||
next["managers"] = action.value;
|
||||
const auto first = action.value.at(0).at("club").get<std::string>();
|
||||
Json managers;
|
||||
const auto error = normalized_managers(raw, *catalog_, managers);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
next["managers"] = std::move(managers);
|
||||
const auto first =
|
||||
next.at("managers").at(0).at("club").get<std::string>();
|
||||
if (next.at("training").at("club") != first) {
|
||||
next["training"] = demo_training(*catalog_, first);
|
||||
next["tactics"] = demo_tactics();
|
||||
@@ -159,26 +281,48 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
next["transfer"] = demo_transfer(*catalog_, first);
|
||||
next["stadium"] = demo_stadium(*catalog_, first);
|
||||
next["calendar"] = demo_calendar(*catalog_, next.at("date"), first);
|
||||
const auto error = edit_demo_reports(next["reports"],
|
||||
{{"kind", "league"}, {"value", next["calendar"]["league"]}}, *catalog_);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
const auto error = edit_demo_reports(
|
||||
next["reports"],
|
||||
{{"kind", "league"}, {"value", next["calendar"]["league"]}},
|
||||
*catalog_);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
}
|
||||
} else if (action.kind == "environment" || action.kind == "advertising" || action.kind == "toto") {
|
||||
const auto error = action.kind == "environment" ? edit_demo_environment(next, action.value, *catalog_)
|
||||
: action.kind == "advertising" ? edit_demo_advertising(next, action.value, *catalog_)
|
||||
: edit_demo_toto(next, action.value, *catalog_);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
if (start) {
|
||||
next["edits"]["setup"] = action.value.at("setup");
|
||||
next["opening"] = action.value.at("setup").at("league").get<bool>()
|
||||
? "pending"
|
||||
: "complete";
|
||||
next["opening_created"] = false;
|
||||
}
|
||||
} else if (action.kind == "environment" || action.kind == "advertising" ||
|
||||
action.kind == "toto") {
|
||||
const auto error =
|
||||
action.kind == "environment"
|
||||
? edit_demo_environment(next, action.value, *catalog_)
|
||||
: action.kind == "advertising"
|
||||
? edit_demo_advertising(next, action.value, *catalog_)
|
||||
: edit_demo_toto(next, action.value, *catalog_);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "lineup" || action.kind == "transfer") {
|
||||
const auto club = next["managers"][0]["club"].get<std::string>();
|
||||
const auto error = action.kind == "lineup" ? edit_demo_lineup(next, action.value, *catalog_, club)
|
||||
: edit_demo_transfer(next, action.value, *catalog_, club);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
const auto error =
|
||||
action.kind == "lineup"
|
||||
? edit_demo_lineup(next, action.value, *catalog_, club)
|
||||
: edit_demo_transfer(next, action.value, *catalog_, club);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "stadium") {
|
||||
const auto error = edit_demo_stadium(next, action.value, *catalog_, next["managers"][0]["club"]);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
const auto error = edit_demo_stadium(next, action.value, *catalog_,
|
||||
next["managers"][0]["club"]);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "finance") {
|
||||
const auto error = edit_demo_finance(next, action.value, *catalog_, next["managers"][0]["club"]);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
const auto error = edit_demo_finance(next, action.value, *catalog_,
|
||||
next["managers"][0]["club"]);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "training") {
|
||||
const auto error = edit_demo_training(next["training"], action.value);
|
||||
if (!error.empty())
|
||||
@@ -188,19 +332,22 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "calendar") {
|
||||
const auto error = edit_demo_calendar(next["calendar"], action.value,
|
||||
*catalog_, next["managers"][0]["club"]);
|
||||
const auto error =
|
||||
edit_demo_calendar(next["calendar"], action.value, *catalog_,
|
||||
next["managers"][0]["club"]);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
if (action.value.value("kind", "") == "league") {
|
||||
const auto report_error = edit_demo_reports(next["reports"],
|
||||
{{"kind", "league"}, {"value", next["calendar"]["league"]}}, *catalog_);
|
||||
const auto report_error = edit_demo_reports(
|
||||
next["reports"],
|
||||
{{"kind", "league"}, {"value", next["calendar"]["league"]}},
|
||||
*catalog_);
|
||||
if (!report_error.empty())
|
||||
return {false, revision_, report_error};
|
||||
}
|
||||
} else if (action.kind == "reports") {
|
||||
const auto error = edit_demo_reports(next["reports"], action.value,
|
||||
*catalog_);
|
||||
const auto error =
|
||||
edit_demo_reports(next["reports"], action.value, *catalog_);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
next["calendar"]["league"] = next["reports"]["league"];
|
||||
@@ -253,7 +400,8 @@ Json DemoSource::load(const fs::path &file) {
|
||||
return load_data(Json::parse(editor::read_file(file)));
|
||||
}
|
||||
bool DemoSource::mark_saved(uint64_t expected_revision) {
|
||||
if (revision_ != expected_revision) return false;
|
||||
if (revision_ != expected_revision)
|
||||
return false;
|
||||
saved_ = state_;
|
||||
return true;
|
||||
}
|
||||
@@ -265,6 +413,7 @@ Json DemoSource::load_data(Json data) {
|
||||
const auto result = validation.apply({0, "managers", data.at("managers")});
|
||||
if (!result.ok)
|
||||
throw editor::Error(result.error);
|
||||
data["managers"] = validation.snapshot().state.at("managers");
|
||||
const auto club = data.at("managers").at(0).at("club").get<std::string>();
|
||||
if (!data.contains("training"))
|
||||
data["training"] = demo_training(*catalog_, club);
|
||||
@@ -289,7 +438,8 @@ Json DemoSource::load_data(Json data) {
|
||||
throw editor::Error("invalid_demo_calendar");
|
||||
if (!data.contains("reports")) {
|
||||
data["reports"] = demo_reports(*catalog_);
|
||||
const auto error = edit_demo_reports(data["reports"],
|
||||
const auto error = edit_demo_reports(
|
||||
data["reports"],
|
||||
{{"kind", "league"}, {"value", data["calendar"]["league"]}}, *catalog_);
|
||||
if (!error.empty())
|
||||
throw editor::Error(error);
|
||||
@@ -308,9 +458,12 @@ Json DemoSource::load_data(Json data) {
|
||||
throw editor::Error("invalid_focus");
|
||||
if (!migrate_demo_finance(data, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_finance");
|
||||
if (!migrate_demo_environment(data, *catalog_) || !migrate_demo_advertising(data, *catalog_) || !migrate_demo_toto(data, *catalog_))
|
||||
if (!migrate_demo_environment(data, *catalog_) ||
|
||||
!migrate_demo_advertising(data, *catalog_) ||
|
||||
!migrate_demo_toto(data, *catalog_))
|
||||
throw editor::Error("invalid_demo_club");
|
||||
if (!migrate_demo_lineup(data, *catalog_, club) || !migrate_demo_transfer(data, *catalog_, club))
|
||||
if (!migrate_demo_lineup(data, *catalog_, club) ||
|
||||
!migrate_demo_transfer(data, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_squad");
|
||||
if (!migrate_demo_stadium(data, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_stadium");
|
||||
|
||||
Reference in new issue
Block a user