247 lines
10 KiB
C++
247 lines
10 KiB
C++
// 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
|