Model native demo career roles, manager handover and separate indoor boards (by Codex)
This commit is contained in:
1 parent
b02ee52dee
commit
d88bf2872a
11 files changed
+996
-137
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp demo_opening.cpp)
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp demo_career.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp demo_opening.cpp)
|
||||
target_include_directories(bmh_modern_ui_core PUBLIC ../..)
|
||||
target_link_libraries(bmh_modern_ui_core PUBLIC bmh_asset_io)
|
||||
add_library(bmh_modern_ui_mods STATIC mod_library.cpp)
|
||||
@@ -38,7 +38,7 @@ endif()
|
||||
set(modern_assets "${CMAKE_BINARY_DIR}/assets/modern")
|
||||
FetchContent_GetProperties(rmlui)
|
||||
file(MAKE_DIRECTORY "${modern_assets}")
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss statistics.rcss news.rcss settings.rcss widgets.rcss files.rcss front.rcss entry.rcss opening.rcss select_menu.rcss)
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss statistics.rcss news.rcss settings.rcss widgets.rcss files.rcss front.rcss entry.rcss opening.rcss select_menu.rcss career.rcss)
|
||||
configure_file("${CMAKE_SOURCE_DIR}/assets/modern/${asset}" "${modern_assets}/${asset}" COPYONLY)
|
||||
endforeach()
|
||||
|
||||
@@ -124,7 +124,7 @@ foreach(component environment advertising toto lineup transfer stadium)
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
|
||||
endforeach()
|
||||
|
||||
foreach(component statistics news settings desktop files entry opening)
|
||||
foreach(component statistics news settings desktop files entry opening career)
|
||||
add_executable(modern_${component}_ui_tests ../../../tests/modern_${component}_ui_tests.cpp)
|
||||
target_link_libraries(modern_${component}_ui_tests PRIVATE bmh_modern_ui_core)
|
||||
if(MSVC)
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_career.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <set>
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
const std::array<const char *, 20> view_keys = {
|
||||
"cash", "deposit", "debt", "ticket", "fans",
|
||||
"camp", "training", "tactics", "calendar", "reports",
|
||||
"lineup", "transfer", "contracts", "stadium", "finance",
|
||||
"balance", "environment", "advertising", "toto", "indoor_lineup"};
|
||||
Json setup(const Json &state) {
|
||||
return state.value("edits", Json::object())
|
||||
.value("setup", Json{{"duration", 5},
|
||||
{"difficulty", 3},
|
||||
{"players", int(state.at("managers").size())},
|
||||
{"mode", 3},
|
||||
{"league", true},
|
||||
{"world", true}});
|
||||
}
|
||||
bool has(const std::set<std::string> &set, const std::string &key) {
|
||||
return set.contains(key);
|
||||
}
|
||||
} // namespace
|
||||
Json demo_career_defaults(const Json &managers, const Json &options) {
|
||||
return {{"schema", 1},
|
||||
{"active_portrait", managers.at(0).value("portrait", 0)},
|
||||
{"role", options.value("league", true) ? "club" : "national"},
|
||||
{"surface", "outdoor"},
|
||||
{"ai_teams", Json::array()},
|
||||
{"ended", false}};
|
||||
}
|
||||
const Json &demo_career_manager(const Json &state) {
|
||||
const auto &rows = state.at("managers");
|
||||
const int active =
|
||||
state.value("career", Json::object())
|
||||
.value("active_portrait", rows.at(0).value("portrait", 0));
|
||||
for (const auto &m : rows)
|
||||
if (m.value("portrait", 0) == active)
|
||||
return m;
|
||||
return rows.at(0);
|
||||
}
|
||||
Json demo_career_context(const Json &state, const Catalog &world) {
|
||||
const auto options = setup(state),
|
||||
career = state.value(
|
||||
"career", demo_career_defaults(state.at("managers"), options));
|
||||
const auto &manager = demo_career_manager(state);
|
||||
const bool national = career.value("role", "") == "national",
|
||||
ended = career.value("ended", false);
|
||||
Json team = {{"id", "national-de"},
|
||||
{"name", "Deutschland"},
|
||||
{"role", "national"},
|
||||
{"gui_image", "career.national-de"}};
|
||||
if (!national) {
|
||||
const auto *club = world.find("clubs", manager.value("club", ""));
|
||||
team = {{"id", manager.value("club", "")},
|
||||
{"name", club ? world.name(*club) : ""},
|
||||
{"role", "club"}};
|
||||
}
|
||||
return {{"role", career.value("role", "club")},
|
||||
{"surface", career.value("surface", "outdoor")},
|
||||
{"current_manager", manager},
|
||||
{"current_team", team},
|
||||
{"active_managers", ended ? Json::array() : state.at("managers")},
|
||||
{"ended", ended},
|
||||
{"available_roles", options.value("league", true)
|
||||
? (options.value("world", true)
|
||||
? Json::array({"club", "national"})
|
||||
: Json::array({"club"}))
|
||||
: Json::array({"national"})}};
|
||||
}
|
||||
bool migrate_demo_career(Json &state, const Catalog &world) {
|
||||
try {
|
||||
auto options = setup(state);
|
||||
if (!options.is_object() || options.size() != 6)
|
||||
return false;
|
||||
for (const auto &[key, max] :
|
||||
std::array<std::pair<const char *, int>, 4>{{{"duration", 5},
|
||||
{"difficulty", 10},
|
||||
{"players", 4},
|
||||
{"mode", 3}}})
|
||||
if (!options.contains(key) || !options.at(key).is_number_integer() ||
|
||||
options.at(key) < 1 || options.at(key) > max)
|
||||
return false;
|
||||
if (!options.contains("league") || !options.at("league").is_boolean() ||
|
||||
!options.contains("world") || !options.at("world").is_boolean() ||
|
||||
(!options.at("league").get<bool>() &&
|
||||
!options.at("world").get<bool>()) ||
|
||||
(!options.at("league").get<bool>() && options.at("players") != 1))
|
||||
return false;
|
||||
options["mode"] = 3;
|
||||
state["edits"]["setup"] = options;
|
||||
if (!state.contains("career"))
|
||||
state["career"] = demo_career_defaults(state.at("managers"), options);
|
||||
const auto &c = state.at("career");
|
||||
if (!c.is_object() || c.size() != 6 || c.at("schema") != 1 ||
|
||||
!c.at("active_portrait").is_number_integer() ||
|
||||
!c.at("ended").is_boolean() || !c.at("ai_teams").is_array() ||
|
||||
c.at("ai_teams").size() > 128)
|
||||
return false;
|
||||
bool selected = false;
|
||||
for (const auto &m : state.at("managers"))
|
||||
selected |= m.value("portrait", 0) == c.at("active_portrait");
|
||||
if (!selected ||
|
||||
(c.at("ended") == true && state.at("managers").size() != 1))
|
||||
return false;
|
||||
const auto role = c.at("role").get<std::string>(),
|
||||
surface = c.at("surface").get<std::string>();
|
||||
if ((role != "club" && role != "national") ||
|
||||
(surface != "outdoor" && surface != "indoor") ||
|
||||
(role == "club" && !options.value("league", true)) ||
|
||||
(role == "national" &&
|
||||
(!options.value("world", true) || surface != "outdoor")))
|
||||
return false;
|
||||
std::set<std::string> seen;
|
||||
for (const auto &id : c.at("ai_teams"))
|
||||
if (!id.is_string() || !world.manageable(id.get<std::string>()) ||
|
||||
!seen.insert(id.get<std::string>()).second)
|
||||
return false;
|
||||
if (!state.contains("career_views"))
|
||||
state["career_views"] = Json::object();
|
||||
if (!state.at("career_views").is_object() ||
|
||||
state.at("career_views").size() > 32)
|
||||
return false;
|
||||
for (const auto &[id, view] : state.at("career_views").items())
|
||||
if (!world.manageable(id) || !valid_demo_career_view(view))
|
||||
return false;
|
||||
return true;
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::string edit_demo_career(Json &state, const Json &action,
|
||||
const Catalog &world) {
|
||||
try {
|
||||
if (!migrate_demo_career(state, world) || !action.is_object() ||
|
||||
!action.contains("kind") || !action.at("kind").is_string())
|
||||
return "invalid_demo_career";
|
||||
auto &c = state["career"];
|
||||
if (c.at("ended") == true)
|
||||
return "career_ended";
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
if (kind == "select_manager" || kind == "resign") {
|
||||
if (action.size() != 2 || !action.contains("portrait") ||
|
||||
!action.at("portrait").is_number_integer())
|
||||
return "invalid_career_manager";
|
||||
const int portrait = action.at("portrait");
|
||||
auto &m = state["managers"];
|
||||
auto i = std::find_if(m.begin(), m.end(), [&](const auto &v) {
|
||||
return v.value("portrait", 0) == portrait;
|
||||
});
|
||||
if (i == m.end())
|
||||
return "invalid_career_manager";
|
||||
if (kind == "select_manager")
|
||||
c["active_portrait"] = portrait;
|
||||
else {
|
||||
const auto club = i->value("club", "");
|
||||
if (std::find(c["ai_teams"].begin(), c["ai_teams"].end(), club) ==
|
||||
c["ai_teams"].end())
|
||||
c["ai_teams"].push_back(club);
|
||||
if (m.size() == 1)
|
||||
c["ended"] = true;
|
||||
else {
|
||||
const auto position = size_t(i - m.begin());
|
||||
m.erase(i);
|
||||
if (c.at("active_portrait") == portrait)
|
||||
c["active_portrait"] =
|
||||
m[std::min(position, m.size() - 1)].value("portrait", 0);
|
||||
}
|
||||
}
|
||||
} else if (kind == "role") {
|
||||
if (action.size() != 2 || !action.contains("value") ||
|
||||
!action.at("value").is_string())
|
||||
return "invalid_career_role";
|
||||
const auto value = action.at("value").get<std::string>();
|
||||
const auto options = setup(state);
|
||||
if ((value == "club" && !options.value("league", true)) ||
|
||||
(value == "national" && !options.value("world", true)) ||
|
||||
(value != "club" && value != "national"))
|
||||
return "invalid_career_role";
|
||||
c["role"] = value;
|
||||
if (value == "national")
|
||||
c["surface"] = "outdoor";
|
||||
} else if (kind == "surface") {
|
||||
if (action.size() != 2 || !action.contains("value") ||
|
||||
!action.at("value").is_string() || c.at("role") != "club")
|
||||
return "invalid_career_surface";
|
||||
const auto value = action.at("value").get<std::string>();
|
||||
if (value != "outdoor" && value != "indoor")
|
||||
return "invalid_career_surface";
|
||||
c["surface"] = value;
|
||||
} else
|
||||
return "invalid_career_action";
|
||||
return migrate_demo_career(state, world) ? "" : "invalid_demo_career";
|
||||
} catch (const std::exception &) {
|
||||
return "invalid_demo_career";
|
||||
}
|
||||
}
|
||||
bool demo_career_menu_available(const Json &state, const std::string &route) {
|
||||
static const std::set<std::string> club = {
|
||||
"league", "indoor", "postpone", "toto", "bank",
|
||||
"balance", "camp", "training", "transfer", "advertising",
|
||||
"stadium", "environment", "contracts", "fairplay"};
|
||||
const auto c = state.value("career", Json::object());
|
||||
return !c.value("ended", false) &&
|
||||
(c.value("role", "club") != "national" || !has(club, route));
|
||||
}
|
||||
bool demo_career_widget_available(const Json &state, const std::string &id) {
|
||||
static const std::set<std::string> club = {
|
||||
"account", "warnings", "fans", "own-curve",
|
||||
"opponent-curve", "comparison-curve", "results"};
|
||||
const auto c = state.value("career", Json::object());
|
||||
return !c.value("ended", false) &&
|
||||
(c.value("role", "club") != "national" || !has(club, id));
|
||||
}
|
||||
int demo_career_field_count(const Json &state) {
|
||||
return state.value("career", Json::object()).value("surface", "outdoor") ==
|
||||
"indoor"
|
||||
? 6
|
||||
: 11;
|
||||
}
|
||||
Json demo_career_view(const Json &state) {
|
||||
Json out = Json::object();
|
||||
for (auto key : view_keys)
|
||||
if (state.contains(key))
|
||||
out[key] = state.at(key);
|
||||
return out;
|
||||
}
|
||||
bool valid_demo_career_view(const Json &view) {
|
||||
if (!view.is_object())
|
||||
return false;
|
||||
for (const auto &[key, value] : view.items()) {
|
||||
(void)value;
|
||||
if (std::find(view_keys.begin(), view_keys.end(), key) == view_keys.end())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void restore_demo_career_view(Json &state, const Json &view) {
|
||||
for (auto key : view_keys)
|
||||
state.erase(key);
|
||||
for (const auto &[key, value] : view.items())
|
||||
state[key] = value;
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,17 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/catalog.hpp"
|
||||
namespace bmh::modern_ui {
|
||||
// Presentation-only career identity, never an engine session or national squad.
|
||||
const Json &demo_career_manager(const Json &state);
|
||||
Json demo_career_context(const Json &state, const Catalog &);
|
||||
Json demo_career_defaults(const Json &managers, const Json &setup);
|
||||
bool migrate_demo_career(Json &, const Catalog &);
|
||||
std::string edit_demo_career(Json &, const Json &, const Catalog &);
|
||||
bool demo_career_menu_available(const Json &, const std::string &);
|
||||
bool demo_career_widget_available(const Json &, const std::string &);
|
||||
int demo_career_field_count(const Json &);
|
||||
Json demo_career_view(const Json &);
|
||||
bool valid_demo_career_view(const Json &);
|
||||
void restore_demo_career_view(Json &, const Json &);
|
||||
} // namespace bmh::modern_ui
|
||||
+353
-122
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_lineup.hpp"
|
||||
#include "modern/ui/demo_career.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
@@ -9,225 +10,455 @@
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
bool keys(const Json &v, std::initializer_list<const char *> list) {
|
||||
return v.is_object() && v.size() == list.size() && std::all_of(list.begin(), list.end(),
|
||||
[&](auto key) { return v.contains(key); });
|
||||
return v.is_object() && v.size() == list.size() &&
|
||||
std::all_of(list.begin(), list.end(),
|
||||
[&](auto key) { return v.contains(key); });
|
||||
}
|
||||
bool integer(const Json &v, int lo, int hi) {
|
||||
if (v.is_number_unsigned()) return v.get<uint64_t>() <= uint64_t(hi) &&
|
||||
(lo <= 0 || v.get<uint64_t>() >= uint64_t(lo));
|
||||
return v.is_number_integer() && v.get<int64_t>() >= lo && v.get<int64_t>() <= hi;
|
||||
if (v.is_number_unsigned())
|
||||
return v.get<uint64_t>() <= uint64_t(hi) &&
|
||||
(lo <= 0 || v.get<uint64_t>() >= uint64_t(lo));
|
||||
return v.is_number_integer() && v.get<int64_t>() >= lo &&
|
||||
v.get<int64_t>() <= hi;
|
||||
}
|
||||
bool integers(const Json &v, size_t count, int lo, int hi) {
|
||||
return v.is_array() && v.size() == count && std::all_of(v.begin(), v.end(),
|
||||
[&](const auto &n) { return integer(n, lo, hi); });
|
||||
return v.is_array() && v.size() == count &&
|
||||
std::all_of(v.begin(), v.end(),
|
||||
[&](const auto &n) { return integer(n, lo, hi); });
|
||||
}
|
||||
bool points(const Json &v) {
|
||||
if (!v.is_array() || v.size() != 11) return false;
|
||||
std::set<std::pair<int,int>> seen;
|
||||
if (!v.is_array() || v.size() != 11)
|
||||
return false;
|
||||
std::set<std::pair<int, int>> seen;
|
||||
for (const auto &p : v) {
|
||||
if (!p.is_array() || p.size() != 2 || !integer(p[0], 0, 18) || !integer(p[1], 0, 8) ||
|
||||
!seen.emplace(p[0].get<int>(), p[1].get<int>()).second) return false;
|
||||
if (!p.is_array() || p.size() != 2 || !integer(p[0], 0, 18) ||
|
||||
!integer(p[1], 0, 8) ||
|
||||
!seen.emplace(p[0].get<int>(), p[1].get<int>()).second)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::string own(const Catalog &world, const std::string &club, const Json *state = nullptr) {
|
||||
if (!club.empty()) return world.manageable(club) ? club : std::string();
|
||||
std::string own(const Catalog &world, const std::string &club,
|
||||
const Json *state = nullptr) {
|
||||
if (!club.empty())
|
||||
return world.manageable(club) ? club : std::string();
|
||||
if (state && state->is_object() && state->contains("managers")) {
|
||||
const auto &m = state->at("managers");
|
||||
if (!m.is_array() || m.empty() || !m[0].is_object() || !m[0].contains("club") ||
|
||||
!m[0]["club"].is_string()) return {};
|
||||
const auto id = m[0]["club"].get<std::string>();
|
||||
if (!m.is_array() || m.empty() || !m[0].is_object() ||
|
||||
!m[0].contains("club") || !m[0]["club"].is_string())
|
||||
return {};
|
||||
const auto id = demo_career_manager(*state).value("club", "");
|
||||
return world.manageable(id) ? id : std::string();
|
||||
}
|
||||
for (auto *c : world.clubs()) if (world.manageable(c->value("id", ""))) return c->value("id", "");
|
||||
for (auto *c : world.clubs())
|
||||
if (world.manageable(c->value("id", "")))
|
||||
return c->value("id", "");
|
||||
return {};
|
||||
}
|
||||
std::string position(const Json &p) {
|
||||
if (!p.contains("positions") || !p.at("positions").is_array()) return {};
|
||||
if (!p.contains("positions") || !p.at("positions").is_array())
|
||||
return {};
|
||||
const auto &pos = p.at("positions");
|
||||
if (std::find(pos.begin(), pos.end(), "gk") != pos.end()) return "gk";
|
||||
for (const auto &v : pos) if (v.is_string()) return v.get<std::string>();
|
||||
if (std::find(pos.begin(), pos.end(), "gk") != pos.end())
|
||||
return "gk";
|
||||
for (const auto &v : pos)
|
||||
if (v.is_string())
|
||||
return v.get<std::string>();
|
||||
return {};
|
||||
}
|
||||
Json attribute(const Json &p, const char *key) {
|
||||
if (!p.contains("attributes") || !p.at("attributes").is_object()) return nullptr;
|
||||
if (!p.contains("attributes") || !p.at("attributes").is_object())
|
||||
return nullptr;
|
||||
const auto found = p.at("attributes").find(key);
|
||||
return found != p.at("attributes").end() && found->is_number() &&
|
||||
std::isfinite(found->get<double>()) ? *found : Json(nullptr);
|
||||
std::isfinite(found->get<double>())
|
||||
? *found
|
||||
: Json(nullptr);
|
||||
}
|
||||
bool date(const Json &v) {
|
||||
if (!v.is_string()) return false;
|
||||
if (!v.is_string())
|
||||
return false;
|
||||
const auto &s = v.get_ref<const std::string &>();
|
||||
if (s.size() != 10 || s[4] != '-' || s[7] != '-') return false;
|
||||
for (size_t i = 0; i < s.size(); ++i) if (i != 4 && i != 7 && (s[i] < '0' || s[i] > '9')) return false;
|
||||
return 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)))}}.ok();
|
||||
if (s.size() != 10 || s[4] != '-' || s[7] != '-')
|
||||
return false;
|
||||
for (size_t i = 0; i < s.size(); ++i)
|
||||
if (i != 4 && i != 7 && (s[i] < '0' || s[i] > '9'))
|
||||
return false;
|
||||
return 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)))}}
|
||||
.ok();
|
||||
}
|
||||
Json age(const Json &p, const Json &reference) {
|
||||
const Json born = p.value("born", p.value("birth_date", Json()));
|
||||
if (date(born) && date(reference)) {
|
||||
const auto b = born.get<std::string>(), r = reference.get<std::string>();
|
||||
const int n = std::stoi(r.substr(0,4)) - std::stoi(b.substr(0,4)) - int(r.substr(5) < b.substr(5));
|
||||
const int n = std::stoi(r.substr(0, 4)) - std::stoi(b.substr(0, 4)) -
|
||||
int(r.substr(5) < b.substr(5));
|
||||
return n >= 0 && n <= 100 ? Json(n) : Json(nullptr);
|
||||
}
|
||||
const auto initial = p.value("age_at_start", Json());
|
||||
return integer(initial, 0, 100) ? initial : Json(nullptr);
|
||||
}
|
||||
void clean_takers(Json &lineup) {
|
||||
void clean_takers(Json &lineup, size_t limit = 11) {
|
||||
const auto &order = lineup.at("order");
|
||||
for (auto &taker : lineup["takers"]) {
|
||||
bool active = false;
|
||||
for (size_t i = 0; i < std::min(size_t(11), order.size()); ++i) if (order[i] == taker) active = true;
|
||||
if (!active) taker = "";
|
||||
for (size_t i = 0; i < std::min(limit, order.size()); ++i)
|
||||
if (order[i] == taker)
|
||||
active = true;
|
||||
if (!active)
|
||||
taker = "";
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
Json demo_lineup_formations() {
|
||||
return Json::array({
|
||||
{{0,4},{5,1},{5,3},{5,5},{5,7},{10,1},{10,3},{10,5},{10,7},{16,3},{16,5}},
|
||||
{{0,4},{5,1},{5,3},{5,5},{5,7},{10,2},{10,4},{10,6},{16,1},{16,4},{16,7}},
|
||||
{{0,4},{5,2},{5,4},{5,6},{10,0},{10,2},{10,4},{10,6},{10,8},{16,3},{16,5}},
|
||||
{{0,4},{4,0},{4,2},{4,4},{4,6},{4,8},{10,2},{10,4},{10,6},{16,3},{16,5}}
|
||||
});
|
||||
Json demo_lineup_formations(bool indoor) {
|
||||
if (indoor)
|
||||
return Json::array({{{0, 4},
|
||||
{5, 2},
|
||||
{5, 6},
|
||||
{10, 4},
|
||||
{16, 2},
|
||||
{16, 6},
|
||||
{18, 0},
|
||||
{18, 2},
|
||||
{18, 4},
|
||||
{18, 6},
|
||||
{18, 8}},
|
||||
{{0, 4},
|
||||
{5, 1},
|
||||
{5, 4},
|
||||
{5, 7},
|
||||
{15, 2},
|
||||
{15, 6},
|
||||
{18, 0},
|
||||
{18, 2},
|
||||
{18, 4},
|
||||
{18, 6},
|
||||
{18, 8}},
|
||||
{{0, 4},
|
||||
{5, 2},
|
||||
{5, 6},
|
||||
{12, 1},
|
||||
{12, 4},
|
||||
{12, 7},
|
||||
{18, 0},
|
||||
{18, 2},
|
||||
{18, 4},
|
||||
{18, 6},
|
||||
{18, 8}},
|
||||
{{0, 4},
|
||||
{5, 4},
|
||||
{10, 1},
|
||||
{10, 7},
|
||||
{16, 2},
|
||||
{16, 6},
|
||||
{18, 0},
|
||||
{18, 2},
|
||||
{18, 4},
|
||||
{18, 6},
|
||||
{18, 8}}});
|
||||
return Json::array({{{0, 4},
|
||||
{5, 1},
|
||||
{5, 3},
|
||||
{5, 5},
|
||||
{5, 7},
|
||||
{10, 1},
|
||||
{10, 3},
|
||||
{10, 5},
|
||||
{10, 7},
|
||||
{16, 3},
|
||||
{16, 5}},
|
||||
{{0, 4},
|
||||
{5, 1},
|
||||
{5, 3},
|
||||
{5, 5},
|
||||
{5, 7},
|
||||
{10, 2},
|
||||
{10, 4},
|
||||
{10, 6},
|
||||
{16, 1},
|
||||
{16, 4},
|
||||
{16, 7}},
|
||||
{{0, 4},
|
||||
{5, 2},
|
||||
{5, 4},
|
||||
{5, 6},
|
||||
{10, 0},
|
||||
{10, 2},
|
||||
{10, 4},
|
||||
{10, 6},
|
||||
{10, 8},
|
||||
{16, 3},
|
||||
{16, 5}},
|
||||
{{0, 4},
|
||||
{4, 0},
|
||||
{4, 2},
|
||||
{4, 4},
|
||||
{4, 6},
|
||||
{4, 8},
|
||||
{10, 2},
|
||||
{10, 4},
|
||||
{10, 6},
|
||||
{16, 3},
|
||||
{16, 5}}});
|
||||
}
|
||||
Json demo_lineup(const Catalog &world, const std::string &managed) {
|
||||
const auto club = own(world, managed);
|
||||
Json order = Json::array(), orders = Json::object();
|
||||
const auto squad = world.squad(club);
|
||||
std::set<std::string> used;
|
||||
for (const auto &[role, count] : std::array<std::pair<const char *, int>,4>{{{"gk",1},{"def",4},{"mid",4},{"fwd",2}}}) {
|
||||
for (const auto &[role, count] : std::array<std::pair<const char *, int>, 4>{
|
||||
{{"gk", 1}, {"def", 4}, {"mid", 4}, {"fwd", 2}}}) {
|
||||
int n = 0;
|
||||
for (auto *p : squad) if (position(*p) == role && n < count && used.insert(p->at("id").get<std::string>()).second) {
|
||||
order.push_back(p->at("id")); ++n;
|
||||
}
|
||||
for (auto *p : squad)
|
||||
if (position(*p) == role && n < count &&
|
||||
used.insert(p->at("id").get<std::string>()).second) {
|
||||
order.push_back(p->at("id"));
|
||||
++n;
|
||||
}
|
||||
}
|
||||
for (auto *p : squad) if (used.insert(p->at("id").get<std::string>()).second) order.push_back(p->at("id"));
|
||||
for (auto *p : squad) orders[p->at("id").get<std::string>()] = {20,20,20,20};
|
||||
return {{"club", club}, {"order", order}, {"points", demo_lineup_formations()[0]},
|
||||
{"formation", 0}, {"custom", {nullptr,nullptr,nullptr,nullptr}},
|
||||
{"weights", {1,2,2}}, {"takers", {"","",""}}, {"orders", orders}};
|
||||
for (auto *p : squad)
|
||||
if (used.insert(p->at("id").get<std::string>()).second)
|
||||
order.push_back(p->at("id"));
|
||||
for (auto *p : squad)
|
||||
orders[p->at("id").get<std::string>()] = {20, 20, 20, 20};
|
||||
return {{"club", club},
|
||||
{"order", order},
|
||||
{"points", demo_lineup_formations()[0]},
|
||||
{"formation", 0},
|
||||
{"custom", {nullptr, nullptr, nullptr, nullptr}},
|
||||
{"weights", {1, 2, 2}},
|
||||
{"takers", {"", "", ""}},
|
||||
{"orders", orders}};
|
||||
}
|
||||
const Json &demo_lineup_visible(const Json &state) {
|
||||
return demo_career_field_count(state) == 6 && state.contains("indoor_lineup")
|
||||
? state.at("indoor_lineup")
|
||||
: state.at("lineup");
|
||||
}
|
||||
Json demo_lineup_active(const Json &state) {
|
||||
Json active = Json::array();
|
||||
if (!state.is_object() || !state.contains("lineup") || !state.at("lineup").is_object() ||
|
||||
!state.at("lineup").contains("order") || !state.at("lineup").at("order").is_array()) return active;
|
||||
const auto &order = state.at("lineup").at("order");
|
||||
for (size_t i = 0; i < std::min(size_t(11), order.size()); ++i) {
|
||||
if (!order[i].is_string()) return Json::array();
|
||||
if (!state.is_object() || !state.contains("lineup") ||
|
||||
!state.at("lineup").is_object() ||
|
||||
!state.at("lineup").contains("order") ||
|
||||
!demo_lineup_visible(state).at("order").is_array())
|
||||
return active;
|
||||
const auto &order = demo_lineup_visible(state).at("order");
|
||||
for (size_t i = 0;
|
||||
i < std::min(size_t(demo_career_field_count(state)), order.size());
|
||||
++i) {
|
||||
if (!order[i].is_string())
|
||||
return Json::array();
|
||||
active.push_back(order[i]);
|
||||
}
|
||||
return active;
|
||||
}
|
||||
bool valid_demo_lineup(const Json &state, const Catalog &world, const std::string &managed) {
|
||||
bool valid_demo_lineup(const Json &state, const Catalog &world,
|
||||
const std::string &managed) {
|
||||
try {
|
||||
if (!state.is_object() || !state.contains("lineup")) return false;
|
||||
const auto &l = state.at("lineup");
|
||||
if (!state.is_object() || !state.contains("lineup"))
|
||||
return false;
|
||||
const auto club = own(world, managed, &state);
|
||||
if (club.empty() || !keys(l, {"club","order","points","formation","custom","weights","takers","orders"}) ||
|
||||
l.at("club") != club || !l.at("order").is_array() || !points(l.at("points")) ||
|
||||
!integer(l.at("formation"), -1, 7) || !l.at("custom").is_array() || l.at("custom").size() != 4 ||
|
||||
!integers(l.at("weights"), 3, 0, 3) || !l.at("takers").is_array() || l.at("takers").size() != 3 ||
|
||||
!l.at("orders").is_object()) return false;
|
||||
for (const auto &p : l.at("custom")) if (!p.is_null() && !points(p)) return false;
|
||||
const int formation = l.at("formation").get<int>();
|
||||
if (formation >= 0 && l.at("points") != (formation < 4 ? demo_lineup_formations()[size_t(formation)] : l.at("custom")[size_t(formation-4)])) return false;
|
||||
std::set<std::string> expected, actual;
|
||||
for (auto *p : world.squad(club)) expected.insert(p->at("id").get<std::string>());
|
||||
if (l.at("order").size() != expected.size() || l.at("orders").size() != expected.size()) return false;
|
||||
for (const auto &id : l.at("order")) if (!id.is_string() || !actual.insert(id.get<std::string>()).second) return false;
|
||||
if (actual != expected) return false;
|
||||
for (const auto &id : expected) if (!l.at("orders").contains(id) || !integers(l.at("orders").at(id), 4, 2, 40)) return false;
|
||||
const auto active = demo_lineup_active(state);
|
||||
for (const auto &id : l.at("takers")) if (!id.is_string() || (id != "" && std::find(active.begin(), active.end(), id) == active.end())) return false;
|
||||
return true;
|
||||
} catch (const std::exception &) { return false; }
|
||||
if (club.empty())
|
||||
return false;
|
||||
const auto valid = [&](const Json &l, bool indoor) {
|
||||
if (!keys(l, {"club", "order", "points", "formation", "custom", "weights",
|
||||
"takers", "orders"}) ||
|
||||
l.at("club") != club || !l.at("order").is_array() ||
|
||||
!points(l.at("points")) || !integer(l.at("formation"), -1, 7) ||
|
||||
!l.at("custom").is_array() || l.at("custom").size() != 4 ||
|
||||
!integers(l.at("weights"), 3, 0, 3) || !l.at("takers").is_array() ||
|
||||
l.at("takers").size() != 3 || !l.at("orders").is_object())
|
||||
return false;
|
||||
for (const auto &p : l.at("custom"))
|
||||
if (!p.is_null() && !points(p))
|
||||
return false;
|
||||
const int f = l.at("formation");
|
||||
if (f >= 0 &&
|
||||
l.at("points") != (f < 4 ? demo_lineup_formations(indoor)[size_t(f)]
|
||||
: l.at("custom")[size_t(f - 4)]))
|
||||
return false;
|
||||
std::set<std::string> expected, actual;
|
||||
for (auto *p : world.squad(club))
|
||||
expected.insert(p->at("id").get<std::string>());
|
||||
if (l.at("order").size() != expected.size() ||
|
||||
l.at("orders").size() != expected.size())
|
||||
return false;
|
||||
for (const auto &id : l.at("order"))
|
||||
if (!id.is_string() || !actual.insert(id.get<std::string>()).second)
|
||||
return false;
|
||||
if (actual != expected)
|
||||
return false;
|
||||
for (const auto &id : expected)
|
||||
if (!l.at("orders").contains(id) ||
|
||||
!integers(l.at("orders").at(id), 4, 2, 40))
|
||||
return false;
|
||||
for (const auto &id : l.at("takers")) {
|
||||
if (!id.is_string())
|
||||
return false;
|
||||
bool active = id == "";
|
||||
for (size_t i = 0;
|
||||
i < std::min(size_t(indoor ? 6 : 11), l.at("order").size()); ++i)
|
||||
active |= l.at("order")[i] == id;
|
||||
if (!active)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
return valid(state.at("lineup"), false) &&
|
||||
(!state.contains("indoor_lineup") ||
|
||||
valid(state.at("indoor_lineup"), true));
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool migrate_demo_lineup(Json &state, const Catalog &world, const std::string &managed) {
|
||||
if (!state.is_object()) return false;
|
||||
bool migrate_demo_lineup(Json &state, const Catalog &world,
|
||||
const std::string &managed) {
|
||||
if (!state.is_object())
|
||||
return false;
|
||||
auto next = state;
|
||||
if (!next.contains("lineup")) next["lineup"] = demo_lineup(world, own(world, managed, &state));
|
||||
if (!valid_demo_lineup(next, world, managed)) return false;
|
||||
if (!next.contains("lineup"))
|
||||
next["lineup"] = demo_lineup(world, own(world, managed, &state));
|
||||
if (demo_career_field_count(next) == 6 && !next.contains("indoor_lineup")) {
|
||||
next["indoor_lineup"] = demo_lineup(world, own(world, managed, &state));
|
||||
next["indoor_lineup"]["points"] = demo_lineup_formations(true)[0];
|
||||
}
|
||||
if (!valid_demo_lineup(next, world, managed))
|
||||
return false;
|
||||
state = std::move(next);
|
||||
return true;
|
||||
}
|
||||
std::string edit_demo_lineup(Json &state, const Json &action, const Catalog &world, const std::string &managed) {
|
||||
if (!valid_demo_lineup(state, world, managed)) return "invalid_lineup_state";
|
||||
if (!action.is_object() || !action.contains("kind") || !action.at("kind").is_string()) return "invalid_lineup_action";
|
||||
std::string edit_demo_lineup(Json &state, const Json &action,
|
||||
const Catalog &world, const std::string &managed) {
|
||||
if (!valid_demo_lineup(state, world, managed))
|
||||
return "invalid_lineup_state";
|
||||
if (!action.is_object() || !action.contains("kind") ||
|
||||
!action.at("kind").is_string())
|
||||
return "invalid_lineup_action";
|
||||
auto next = state;
|
||||
auto &l = next["lineup"];
|
||||
const bool indoor = demo_career_field_count(state) == 6;
|
||||
const int field_count = indoor ? 6 : 11;
|
||||
auto &l = next[indoor ? "indoor_lineup" : "lineup"];
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
const auto slot = [&](const Json &id) -> int {
|
||||
if (!id.is_string()) return -1;
|
||||
if (!id.is_string())
|
||||
return -1;
|
||||
const auto it = std::find(l.at("order").begin(), l.at("order").end(), id);
|
||||
return it == l.at("order").end() ? -1 : int(it - l.at("order").begin());
|
||||
};
|
||||
if (kind == "swap") {
|
||||
if (!keys(action, {"kind","a","b"})) return "invalid_lineup_action";
|
||||
if (!keys(action, {"kind", "a", "b"}))
|
||||
return "invalid_lineup_action";
|
||||
const int a = slot(action.at("a")), b = slot(action.at("b"));
|
||||
if (a < 0 || b < 0) return "invalid_lineup_player";
|
||||
if (a < 0 || b < 0)
|
||||
return "invalid_lineup_player";
|
||||
std::swap(l["order"][size_t(a)], l["order"][size_t(b)]);
|
||||
clean_takers(l);
|
||||
clean_takers(l, size_t(field_count));
|
||||
} else if (kind == "move") {
|
||||
if (!keys(action, {"kind","player","point"})) return "invalid_lineup_action";
|
||||
if (!keys(action, {"kind", "player", "point"}))
|
||||
return "invalid_lineup_action";
|
||||
const int from = slot(action.at("player"));
|
||||
const auto &p = action.at("point");
|
||||
if (from < 0 || from >= 11 || !p.is_array() || p.size() != 2 || !integer(p[0],0,18) || !integer(p[1],0,8)) return "invalid_lineup_point";
|
||||
if (from < 0 || from >= field_count || !p.is_array() || p.size() != 2 ||
|
||||
!integer(p[0], 0, 18) || !integer(p[1], 0, 8))
|
||||
return "invalid_lineup_point";
|
||||
const auto it = std::find(l.at("points").begin(), l.at("points").end(), p);
|
||||
const int hit = int(it - l.at("points").begin());
|
||||
if (hit != from && hit < int(std::min(size_t(11), l.at("order").size()))) {
|
||||
if (hit != from &&
|
||||
hit < int(std::min(size_t(field_count), l.at("order").size()))) {
|
||||
std::swap(l["order"][size_t(from)], l["order"][size_t(hit)]);
|
||||
clean_takers(l);
|
||||
} else if (hit < 11 && hit != from) { std::swap(l["points"][size_t(from)],l["points"][size_t(hit)]); l["formation"]=-1; }
|
||||
else { l["points"][size_t(from)] = p; l["formation"] = -1; }
|
||||
clean_takers(l, size_t(field_count));
|
||||
} else if (hit < 11 && hit != from) {
|
||||
std::swap(l["points"][size_t(from)], l["points"][size_t(hit)]);
|
||||
l["formation"] = -1;
|
||||
} else {
|
||||
l["points"][size_t(from)] = p;
|
||||
l["formation"] = -1;
|
||||
}
|
||||
} else if (kind == "formation") {
|
||||
if (!keys(action, {"kind","value"}) || !integer(action.at("value"),0,7)) return "invalid_lineup_formation";
|
||||
if (!keys(action, {"kind", "value"}) || !integer(action.at("value"), 0, 7))
|
||||
return "invalid_lineup_formation";
|
||||
const int i = action.at("value").get<int>();
|
||||
const auto p = i < 4 ? demo_lineup_formations()[size_t(i)] : l.at("custom")[size_t(i-4)];
|
||||
if (p.is_null()) return "missing_lineup_formation";
|
||||
l["points"] = p; l["formation"] = i;
|
||||
const auto p = i < 4 ? demo_lineup_formations(indoor)[size_t(i)]
|
||||
: l.at("custom")[size_t(i - 4)];
|
||||
if (p.is_null())
|
||||
return "missing_lineup_formation";
|
||||
l["points"] = p;
|
||||
l["formation"] = i;
|
||||
} else if (kind == "custom-save") {
|
||||
if (!keys(action, {"kind","index"}) || !integer(action.at("index"),0,3)) return "invalid_lineup_formation";
|
||||
if (!keys(action, {"kind", "index"}) || !integer(action.at("index"), 0, 3))
|
||||
return "invalid_lineup_formation";
|
||||
const int i = action.at("index").get<int>();
|
||||
l["custom"][size_t(i)] = l.at("points"); l["formation"] = i + 4;
|
||||
l["custom"][size_t(i)] = l.at("points");
|
||||
l["formation"] = i + 4;
|
||||
} else if (kind == "weight") {
|
||||
if (!keys(action, {"kind","index"}) || !integer(action.at("index"),0,2)) return "invalid_lineup_weight";
|
||||
if (!keys(action, {"kind", "index"}) || !integer(action.at("index"), 0, 2))
|
||||
return "invalid_lineup_weight";
|
||||
const size_t i = action.at("index").get<size_t>();
|
||||
l["weights"][i] = (l.at("weights")[i].get<int>()+1)%4;
|
||||
l["weights"][i] = (l.at("weights")[i].get<int>() + 1) % 4;
|
||||
} else if (kind == "order") {
|
||||
if (!keys(action, {"kind","player","index","value"}) || slot(action.at("player")) < 0 || slot(action.at("player")) >= 11 ||
|
||||
!integer(action.at("index"),0,3) || !integer(action.at("value"),2,40)) return "invalid_lineup_order";
|
||||
l["orders"][action.at("player").get<std::string>()][action.at("index").get<size_t>()] = action.at("value");
|
||||
if (!keys(action, {"kind", "player", "index", "value"}) ||
|
||||
slot(action.at("player")) < 0 ||
|
||||
slot(action.at("player")) >= field_count ||
|
||||
!integer(action.at("index"), 0, 3) ||
|
||||
!integer(action.at("value"), 2, 40))
|
||||
return "invalid_lineup_order";
|
||||
l["orders"][action.at("player").get<std::string>()]
|
||||
[action.at("index").get<size_t>()] = action.at("value");
|
||||
} else if (kind == "taker") {
|
||||
if (!keys(action, {"kind","index","player"}) || !integer(action.at("index"),0,2) || !action.at("player").is_string()) return "invalid_lineup_taker";
|
||||
if (!keys(action, {"kind", "index", "player"}) ||
|
||||
!integer(action.at("index"), 0, 2) || !action.at("player").is_string())
|
||||
return "invalid_lineup_taker";
|
||||
const int n = slot(action.at("player"));
|
||||
if (action.at("player") != "" && (n < 0 || n >= 11)) return "invalid_lineup_taker";
|
||||
if (action.at("player") != "" && (n < 0 || n >= field_count))
|
||||
return "invalid_lineup_taker";
|
||||
l["takers"][action.at("index").get<size_t>()] = action.at("player");
|
||||
} else if (kind == "takers-auto") {
|
||||
if (!keys(action, {"kind"})) return "invalid_lineup_action";
|
||||
l["takers"] = {"","",""};
|
||||
} else return "unknown_lineup_action";
|
||||
if (!valid_demo_lineup(next, world, managed)) return "invalid_lineup_state";
|
||||
if (!keys(action, {"kind"}))
|
||||
return "invalid_lineup_action";
|
||||
l["takers"] = {"", "", ""};
|
||||
} else
|
||||
return "unknown_lineup_action";
|
||||
if (!valid_demo_lineup(next, world, managed))
|
||||
return "invalid_lineup_state";
|
||||
state = std::move(next);
|
||||
return {};
|
||||
}
|
||||
Json demo_lineup_rows(const Json &state, const Catalog &world, const std::string &managed) {
|
||||
if (!valid_demo_lineup(state, world, managed)) return {{"ok",false},{"error","invalid_lineup_state"}};
|
||||
Json demo_lineup_rows(const Json &state, const Catalog &world,
|
||||
const std::string &managed) {
|
||||
if (!valid_demo_lineup(state, world, managed))
|
||||
return {{"ok", false}, {"error", "invalid_lineup_state"}};
|
||||
Json rows = Json::array();
|
||||
const auto reference = state.value("date", world.manifest().value("start_date",Json()));
|
||||
const auto &l = state.at("lineup");
|
||||
const auto reference =
|
||||
state.value("date", world.manifest().value("start_date", Json()));
|
||||
const auto &l = demo_lineup_visible(state);
|
||||
size_t slot = 0;
|
||||
for (const auto &id : l.at("order")) {
|
||||
const auto *p = world.find("players", id.get<std::string>());
|
||||
if (!p) return {{"ok",false},{"error","missing_lineup_player"}};
|
||||
Json row = {{"id",id},{"name",world.name(*p)},{"position",position(*p)},
|
||||
{"slot",slot},{"starter",slot < 11},{"age",age(*p,reference)},
|
||||
{"nation",p->value("nationalities",Json::array()).is_array() && !p->value("nationalities",Json::array()).empty() ? p->at("nationalities")[0] : p->value("nation",Json())}, {"positions",p->value("positions",Json::array())}};
|
||||
for (const auto *key : {"condition","technique","form","strength","motivation","goals","assists",
|
||||
"corner","free_kick","tackling","passing","shooting","speed","teamplay","games","grade"})
|
||||
row[key] = attribute(*p,key);
|
||||
rows.push_back(std::move(row)); ++slot;
|
||||
if (!p)
|
||||
return {{"ok", false}, {"error", "missing_lineup_player"}};
|
||||
Json row = {
|
||||
{"id", id},
|
||||
{"name", world.name(*p)},
|
||||
{"position", position(*p)},
|
||||
{"slot", slot},
|
||||
{"starter", slot < size_t(demo_career_field_count(state))},
|
||||
{"age", age(*p, reference)},
|
||||
{"nation", p->value("nationalities", Json::array()).is_array() &&
|
||||
!p->value("nationalities", Json::array()).empty()
|
||||
? p->at("nationalities")[0]
|
||||
: p->value("nation", Json())},
|
||||
{"positions", p->value("positions", Json::array())}};
|
||||
for (const auto *key :
|
||||
{"condition", "technique", "form", "strength", "motivation", "goals",
|
||||
"assists", "corner", "free_kick", "tackling", "passing", "shooting",
|
||||
"speed", "teamplay", "games", "grade"})
|
||||
row[key] = attribute(*p, key);
|
||||
rows.push_back(std::move(row));
|
||||
++slot;
|
||||
}
|
||||
return {{"ok",true},{"club",l.at("club")},{"rows",rows}};
|
||||
return {{"ok", true}, {"club", l.at("club")}, {"rows", rows}};
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -6,12 +6,16 @@ namespace bmh::modern_ui {
|
||||
// Presentation-only lineup. Stable catalogue IDs, all real squad members, no
|
||||
// squad-size cap or world/engine mutation. FULL state is used by all editors.
|
||||
Json demo_lineup(const Catalog &, const std::string &club = {});
|
||||
Json demo_lineup_formations();
|
||||
Json demo_lineup_formations(bool indoor = false);
|
||||
const Json &demo_lineup_visible(const Json &);
|
||||
bool migrate_demo_lineup(Json &, const Catalog &, const std::string &club = {});
|
||||
bool valid_demo_lineup(const Json &, const Catalog &, const std::string &club = {});
|
||||
std::string edit_demo_lineup(Json &, const Json &, const Catalog &, const std::string &club = {});
|
||||
bool valid_demo_lineup(const Json &, const Catalog &,
|
||||
const std::string &club = {});
|
||||
std::string edit_demo_lineup(Json &, const Json &, const Catalog &,
|
||||
const std::string &club = {});
|
||||
// Shared read-side starting eleven; missing/empty lineups yield an empty array.
|
||||
Json demo_lineup_active(const Json &);
|
||||
// {ok,club,rows:[{id,name,position,...actual nullable catalogue attributes}]}.
|
||||
Json demo_lineup_rows(const Json &, const Catalog &, const std::string &club = {});
|
||||
Json demo_lineup_rows(const Json &, const Catalog &,
|
||||
const std::string &club = {});
|
||||
} // namespace bmh::modern_ui
|
||||
+57
-8
@@ -3,6 +3,7 @@
|
||||
#include "modern/ui/demo_advertising.hpp"
|
||||
#include "modern/ui/demo_almanac.hpp"
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
#include "modern/ui/demo_career.hpp"
|
||||
#include "modern/ui/demo_environment.hpp"
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include "modern/ui/demo_lineup.hpp"
|
||||
@@ -229,6 +230,8 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
throw editor::Error("invalid_demo_settings");
|
||||
if (!migrate_demo_opening(state_, *catalog_))
|
||||
throw editor::Error("Ungültige Saisonvorbereitung.");
|
||||
if (!migrate_demo_career(state_, *catalog_))
|
||||
throw editor::Error("invalid_demo_career");
|
||||
saved_ = state_;
|
||||
}
|
||||
Snapshot DemoSource::snapshot() const { return {revision_, catalog_, state_}; }
|
||||
@@ -243,7 +246,25 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
return {false, revision_, "stale_revision"};
|
||||
auto next = state_;
|
||||
try {
|
||||
if (action.kind == "settings") {
|
||||
if (action.kind == "career") {
|
||||
const auto previous_club = demo_career_manager(next).value("club", "");
|
||||
const auto old_view = demo_career_view(next);
|
||||
const auto error = edit_demo_career(next, action.value, *catalog_);
|
||||
if (!error.empty()) return {false, revision_, error};
|
||||
const auto current_club = demo_career_manager(next).value("club", "");
|
||||
if (current_club != previous_club) {
|
||||
next["career_views"][previous_club] = old_view;
|
||||
if (next["career_views"].contains(current_club))
|
||||
restore_demo_career_view(next, next["career_views"].at(current_club));
|
||||
else {
|
||||
DemoSource fresh(catalog_);
|
||||
const auto result = fresh.apply({0,"managers",Json::array({demo_career_manager(next)})});
|
||||
if (!result.ok) return {false,revision_,result.error};
|
||||
restore_demo_career_view(next,demo_career_view(fresh.snapshot().state));
|
||||
}
|
||||
}
|
||||
if(!migrate_demo_lineup(next,*catalog_,current_club))return {false,revision_,"invalid_demo_lineup"};
|
||||
} else if (action.kind == "settings") {
|
||||
const auto error = edit_demo_settings(next, action.value);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
@@ -273,14 +294,29 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
const auto error = normalized_managers(raw, *catalog_, managers);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
if (!start && next.contains("career") && next["career"].value("ended",false))
|
||||
return {false,revision_,"career_ended"};
|
||||
if (!start && !next.at("edits").value("setup",Json::object()).value("league",true) && managers.size()>1)
|
||||
return {false,revision_,"national_one_manager"};
|
||||
next["managers"] = std::move(managers);
|
||||
const auto first =
|
||||
next.at("managers").at(0).at("club").get<std::string>();
|
||||
if (start) {
|
||||
auto options=action.value.at("setup");options["mode"]=3;
|
||||
next["edits"]["setup"]=options;
|
||||
next["career"]=demo_career_defaults(next.at("managers"),options);
|
||||
next["career_views"]=Json::object();
|
||||
} else if (next.contains("career")) {
|
||||
const int active=next["career"].value("active_portrait",0);
|
||||
if (std::none_of(next["managers"].begin(),next["managers"].end(),[&](const auto &m){return m.value("portrait",0)==active;}))
|
||||
next["career"]["active_portrait"]=next["managers"][0].value("portrait",0);
|
||||
}
|
||||
const auto first=demo_career_manager(next).value("club", "");
|
||||
if (next.at("training").at("club") != first) {
|
||||
next["training"] = demo_training(*catalog_, first);
|
||||
next["tactics"] = demo_tactics();
|
||||
next["contracts"] = demo_contracts(*catalog_, first);
|
||||
next["lineup"] = demo_lineup(*catalog_, first);
|
||||
next["indoor_lineup"] = demo_lineup(*catalog_, first);
|
||||
next["indoor_lineup"]["points"] = demo_lineup_formations(true)[0];
|
||||
next["transfer"] = demo_transfer(*catalog_, first);
|
||||
next["stadium"] = demo_stadium(*catalog_, first);
|
||||
next["calendar"] = demo_calendar(*catalog_, next.at("date"), first);
|
||||
@@ -293,6 +329,7 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
}
|
||||
if (start) {
|
||||
next["edits"]["setup"] = action.value.at("setup");
|
||||
next["edits"]["setup"]["mode"] = 3;
|
||||
next["opening"] = action.value.at("setup").at("league").get<bool>()
|
||||
? "pending"
|
||||
: "complete";
|
||||
@@ -310,7 +347,7 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
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 club = demo_career_manager(next).value("club", "");
|
||||
const auto error =
|
||||
action.kind == "lineup"
|
||||
? edit_demo_lineup(next, action.value, *catalog_, club)
|
||||
@@ -319,12 +356,12 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "stadium") {
|
||||
const auto error = edit_demo_stadium(next, action.value, *catalog_,
|
||||
next["managers"][0]["club"]);
|
||||
demo_career_manager(next).value("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"]);
|
||||
demo_career_manager(next).value("club", ""));
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "training") {
|
||||
@@ -338,7 +375,7 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
} else if (action.kind == "calendar") {
|
||||
const auto error =
|
||||
edit_demo_calendar(next["calendar"], action.value, *catalog_,
|
||||
next["managers"][0]["club"]);
|
||||
demo_career_manager(next).value("club", ""));
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
if (action.value.value("kind", "") == "league") {
|
||||
@@ -401,6 +438,7 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
} catch (const Json::exception &) {
|
||||
return {false, revision_, "invalid_action"};
|
||||
}
|
||||
if (!migrate_demo_career(next,*catalog_)) return {false,revision_,"invalid_demo_career"};
|
||||
sync_demo_settings_training(next);
|
||||
state_ = std::move(next);
|
||||
return {true, ++revision_, {}};
|
||||
@@ -431,7 +469,10 @@ Json DemoSource::load_data(Json data, bool saved) {
|
||||
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("edits") && data.at("edits").contains("setup") && !valid_start_setup(data.at("edits").at("setup")))
|
||||
throw editor::Error("invalid_setup");
|
||||
if (!migrate_demo_career(data,*catalog_)) throw editor::Error("invalid_demo_career");
|
||||
const auto club = demo_career_manager(data).value("club", "");
|
||||
if (!data.contains("training"))
|
||||
data["training"] = demo_training(*catalog_, club);
|
||||
if (!data.contains("tactics"))
|
||||
@@ -490,6 +531,14 @@ Json DemoSource::load_data(Json data, bool saved) {
|
||||
throw editor::Error("invalid_demo_settings");
|
||||
if (!migrate_demo_opening(data, *catalog_))
|
||||
throw editor::Error("Ungültige oder inkonsistente Saisonvorbereitung.");
|
||||
for (const auto &[id,view]:data.at("career_views").items()) {
|
||||
auto probe=data;probe["career_views"]=Json::object();probe["desktop"]=default_desktop();
|
||||
probe["managers"]=Json::array({{{"name","Vorschau"},{"club",id},{"portrait",0}}});
|
||||
const Json setup={{"duration",5},{"difficulty",3},{"players",1},{"mode",3},{"league",true},{"world",true}};
|
||||
probe["edits"]["setup"]=setup;probe["career"]=demo_career_defaults(probe.at("managers"),setup);
|
||||
restore_demo_career_view(probe,view);sync_demo_settings_training(probe);
|
||||
DemoSource validator(catalog_);validator.load_data(std::move(probe));
|
||||
}
|
||||
auto next = data;
|
||||
auto desktop = next.value("desktop", Json::object());
|
||||
if (!migrate_desktop(desktop))
|
||||
|
||||
Reference in new issue
Block a user