Port scoped National UI routes and isolated dummy planning (by Codex)
This commit is contained in:
1 parent
4fddecb06e
commit
1eb5e99847
19 files changed
+1075
-8
No files matched your search
@@ -0,0 +1,272 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_national_ui.hpp"
|
||||
#include "modern/ui/demo_career.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
std::optional<std::chrono::sys_days> day(const Json &v) {
|
||||
if (!v.is_string())
|
||||
return {};
|
||||
const auto s = v.get<std::string>();
|
||||
if (s.size() != 10 || s[4] != '-' || s[7] != '-')
|
||||
return {};
|
||||
for (size_t i = 0; i < s.size(); ++i)
|
||||
if (i != 4 && i != 7 && (s[i] < '0' || s[i] > '9'))
|
||||
return {};
|
||||
const auto ymd = std::chrono::year_month_day{
|
||||
std::chrono::year{std::stoi(s.substr(0, 4))},
|
||||
std::chrono::month{unsigned(std::stoi(s.substr(5, 2)))},
|
||||
std::chrono::day{unsigned(std::stoi(s.substr(8, 2)))}};
|
||||
if (!ymd.ok() || int(ymd.year()) < 1)
|
||||
return {};
|
||||
return std::chrono::sys_days{ymd};
|
||||
}
|
||||
std::string iso(std::chrono::sys_days value) {
|
||||
const auto ymd = std::chrono::year_month_day{value};
|
||||
std::ostringstream out;
|
||||
out << int(ymd.year()) << '-' << std::setfill('0') << std::setw(2)
|
||||
<< unsigned(ymd.month()) << '-' << std::setw(2) << unsigned(ymd.day());
|
||||
return out.str();
|
||||
}
|
||||
bool opponent(const Json &v) {
|
||||
return v.is_string() &&
|
||||
std::any_of(national_ui_opponents().begin(),
|
||||
national_ui_opponents().end(),
|
||||
[&](const auto &row) { return row.at("id") == v; });
|
||||
}
|
||||
bool integer(const Json &v, int low, int high) {
|
||||
return v.is_number_integer() && v >= low && v <= high;
|
||||
}
|
||||
int season(const Json &state) {
|
||||
const auto date = day(state.at("date"));
|
||||
if (!date)
|
||||
throw editor::Error("invalid_national_date");
|
||||
const auto ymd = std::chrono::year_month_day{*date};
|
||||
return int(ymd.year()) - (unsigned(ymd.month()) < 7);
|
||||
}
|
||||
bool season_date(const Json &state, const Json &v) {
|
||||
const auto d = day(v);
|
||||
if (!d)
|
||||
return false;
|
||||
const int year = season(state);
|
||||
return *d >= std::chrono::sys_days{std::chrono::year{year} / 7 / 1} &&
|
||||
*d <= std::chrono::sys_days{std::chrono::year{year + 1} / 6 / 30};
|
||||
}
|
||||
Json attribute(const Json &p, const char *key) {
|
||||
if (p.contains("attributes") && p.at("attributes").is_object()) {
|
||||
const auto i = p.at("attributes").find(key);
|
||||
if (i != p.at("attributes").end() && i->is_number_integer() &&
|
||||
std::isfinite(i->get<double>()) && *i >= 0)
|
||||
return *i;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
const Json &national_ui_opponents() {
|
||||
static const Json rows = [] {
|
||||
Json out = Json::array();
|
||||
int i = 0;
|
||||
for (const auto &[code, name] :
|
||||
std::array<std::pair<const char *, const char *>, 8>{
|
||||
{{"fr", "Frankreich"},
|
||||
{"it", "Italien"},
|
||||
{"es", "Spanien"},
|
||||
{"en", "England"},
|
||||
{"nl", "Niederlande"},
|
||||
{"pl", "Polen"},
|
||||
{"se", "Schweden"},
|
||||
{"at", "Österreich"}}}) {
|
||||
out.push_back({{"id", std::string("national-") + code},
|
||||
{"name", name},
|
||||
{"code", code},
|
||||
{"gui_image", std::string("career.national-") + code},
|
||||
{"strength", 85 - (i++ % 5) * 2}});
|
||||
}
|
||||
return out;
|
||||
}();
|
||||
return rows;
|
||||
}
|
||||
Json default_national_ui() {
|
||||
return {{"opponent", "national-fr"},
|
||||
{"home", true},
|
||||
{"day", ""},
|
||||
{"month", 0},
|
||||
{"stage", "opponents"},
|
||||
{"scorer_kind", 0},
|
||||
{"friendlies", Json::array()}};
|
||||
}
|
||||
bool valid_national_ui(const Json &state) {
|
||||
try {
|
||||
if (!day(state.at("date")))
|
||||
return false;
|
||||
const auto &n = state.at("national");
|
||||
if (!n.is_object() || n.size() != 7 || !opponent(n.at("opponent")) ||
|
||||
!n.at("home").is_boolean() || !n.at("day").is_string() ||
|
||||
(!n.at("day").get<std::string>().empty() &&
|
||||
!season_date(state, n.at("day"))) ||
|
||||
!integer(n.at("month"), 0, 11) || !n.at("stage").is_string() ||
|
||||
(n.at("stage") != "opponents" && n.at("stage") != "calendar") ||
|
||||
!integer(n.at("scorer_kind"), 0, 3) || !n.at("friendlies").is_array() ||
|
||||
n.at("friendlies").size() > 12)
|
||||
return false;
|
||||
std::set<std::string> used;
|
||||
for (const auto &row : n.at("friendlies"))
|
||||
if (!row.is_object() || row.size() != 3 ||
|
||||
!season_date(state, row.at("date")) ||
|
||||
!opponent(row.at("opponent")) || !row.at("home").is_boolean() ||
|
||||
!used.insert(row.at("date").get<std::string>()).second)
|
||||
return false;
|
||||
if (n.at("day") != "") {
|
||||
const auto dates = national_ui_dates(state);
|
||||
if (std::find(dates.begin(), dates.end(), n.at("day")) == dates.end())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool migrate_national_ui(Json &state) {
|
||||
auto next = state;
|
||||
if (!next.contains("national"))
|
||||
next["national"] = default_national_ui();
|
||||
if (!valid_national_ui(next))
|
||||
return false;
|
||||
state = std::move(next);
|
||||
return true;
|
||||
}
|
||||
Json national_ui_dates(const Json &state) {
|
||||
Json dates = Json::array();
|
||||
try {
|
||||
const auto current = day(state.at("date"));
|
||||
if (!current)
|
||||
return dates;
|
||||
for (int i = 0; i < 12; ++i) {
|
||||
const auto value = iso(*current + std::chrono::days{7 + i * 14});
|
||||
if (!season_date(state, value))
|
||||
continue;
|
||||
const auto &bookings = state.at("national").at("friendlies");
|
||||
if (std::none_of(bookings.begin(), bookings.end(),
|
||||
[&](const auto &r) { return r.at("date") == value; }))
|
||||
dates.push_back(value);
|
||||
}
|
||||
} catch (const std::exception &) {
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
std::string edit_national_ui(Json &state, const Json &action) {
|
||||
try {
|
||||
if (!valid_national_ui(state) || !action.is_object() ||
|
||||
!action.contains("kind") || !action.at("kind").is_string() ||
|
||||
!action.contains("manager_portrait") ||
|
||||
!action.at("manager_portrait").is_number_integer())
|
||||
return "invalid_national_action";
|
||||
if (state.at("career").value("role", "club") != "national" ||
|
||||
state.at("career").value("ended", false) ||
|
||||
action.at("manager_portrait") !=
|
||||
demo_career_manager(state).value("portrait", 0))
|
||||
return "national_context_changed";
|
||||
auto next = state;
|
||||
auto &n = next["national"];
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
if (kind == "opponent") {
|
||||
if (action.size() != 4 || !opponent(action.at("opponent")) ||
|
||||
!action.at("home").is_boolean())
|
||||
return "invalid_national_opponent";
|
||||
n["opponent"] = action.at("opponent");
|
||||
n["home"] = action.at("home");
|
||||
n["stage"] = "calendar";
|
||||
n["day"] = "";
|
||||
const auto ymd = std::chrono::year_month_day{*day(state.at("date"))};
|
||||
n["month"] = (int(ymd.year()) - season(state)) * 12 +
|
||||
int(unsigned(ymd.month())) - 7;
|
||||
} else if (kind == "month") {
|
||||
if (action.size() != 3 || !integer(action.at("value"), 0, 11))
|
||||
return "invalid_national_month";
|
||||
n["month"] = action.at("value");
|
||||
} else if (kind == "day") {
|
||||
const auto dates = national_ui_dates(state);
|
||||
if (action.size() != 3 || !action.at("value").is_string() ||
|
||||
std::find(dates.begin(), dates.end(), action.at("value")) ==
|
||||
dates.end())
|
||||
return "invalid_national_day";
|
||||
n["day"] = action.at("value");
|
||||
} else if (kind == "book") {
|
||||
const auto dates = national_ui_dates(state);
|
||||
if (action.size() != 2 || n.at("friendlies").size() >= 12 ||
|
||||
std::find(dates.begin(), dates.end(), n.at("day")) == dates.end())
|
||||
return "invalid_national_booking";
|
||||
n["friendlies"].push_back({{"date", n.at("day")},
|
||||
{"opponent", n.at("opponent")},
|
||||
{"home", n.at("home")}});
|
||||
n["day"] = "";
|
||||
} else if (kind == "back") {
|
||||
if (action.size() != 2)
|
||||
return "invalid_national_action";
|
||||
n["stage"] = "opponents";
|
||||
} else if (kind == "scorer_kind") {
|
||||
if (action.size() != 3 || !integer(action.at("value"), 0, 3))
|
||||
return "invalid_national_stat";
|
||||
n["scorer_kind"] = action.at("value");
|
||||
} else
|
||||
return "invalid_national_action";
|
||||
if (!valid_national_ui(next))
|
||||
return "invalid_national_state";
|
||||
state = std::move(next);
|
||||
return {};
|
||||
} catch (const std::exception &) {
|
||||
return "invalid_national_action";
|
||||
}
|
||||
}
|
||||
Json query_national_ui(const Catalog &world, const Json &state,
|
||||
const std::string &menu) {
|
||||
if (!valid_national_ui(state))
|
||||
return {{"ok", false}, {"error", "invalid_national_state"}};
|
||||
const auto context = demo_career_context(state, world);
|
||||
if (context.at("role") != "national" || context.at("ended") == true)
|
||||
return {{"ok", false}, {"error", "national_context_changed"}};
|
||||
Json out = {{"ok", true},
|
||||
{"fictional", true},
|
||||
{"source", "ui-demo-unrecorded-national"},
|
||||
{"team", context.at("current_team")},
|
||||
{"manager", context.at("current_manager")},
|
||||
{"planning", state.at("national")},
|
||||
{"opponents", national_ui_opponents()},
|
||||
{"available_dates", national_ui_dates(state)},
|
||||
{"season", season(state)}};
|
||||
if (menu == "achievements") {
|
||||
out["tournaments"] =
|
||||
Json::array({{{"name", "Weltmeisterschaft"},
|
||||
{"gui_image", "trophy.world-cup"},
|
||||
{"titles", nullptr}},
|
||||
{{"name", "Europameisterschaft"},
|
||||
{"gui_image", "trophy.european-championship"},
|
||||
{"titles", nullptr}}});
|
||||
out["history"] = Json::array();
|
||||
} else if (menu == "scorers") {
|
||||
out["recorded_matches"] = 0;
|
||||
out["rows"] = Json::array();
|
||||
out["roster_basis_club"] = demo_career_manager(state).value("club", "");
|
||||
for (auto *p : world.squad(out.at("roster_basis_club").get<std::string>()))
|
||||
out["rows"].push_back({{"id", p->at("id")},
|
||||
{"name", world.name(*p)},
|
||||
{"position", p->value("positions", Json::array())},
|
||||
{"goals", nullptr},
|
||||
{"assists", nullptr},
|
||||
{"yellow", nullptr},
|
||||
{"red", nullptr},
|
||||
{"caps", attribute(*p, "caps")},
|
||||
{"catalog_international_goals",
|
||||
attribute(*p, "international_goals")}});
|
||||
} else if (menu != "friendlies")
|
||||
return {{"ok", false}, {"error", "unsupported_national_view"}};
|
||||
return out;
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user