465 lines
17 KiB
C++
465 lines
17 KiB
C++
// 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>
|
|
#include <cmath>
|
|
#include <set>
|
|
|
|
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); });
|
|
}
|
|
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;
|
|
}
|
|
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); });
|
|
}
|
|
bool points(const Json &v) {
|
|
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;
|
|
}
|
|
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();
|
|
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 = 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", "");
|
|
return {};
|
|
}
|
|
std::string position(const Json &p) {
|
|
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>();
|
|
return {};
|
|
}
|
|
Json attribute(const Json &p, const char *key) {
|
|
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);
|
|
}
|
|
bool date(const Json &v) {
|
|
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();
|
|
}
|
|
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));
|
|
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, 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(limit, order.size()); ++i)
|
|
if (order[i] == taker)
|
|
active = true;
|
|
if (!active)
|
|
taker = "";
|
|
}
|
|
}
|
|
} // namespace
|
|
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}}}) {
|
|
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 (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") ||
|
|
!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) {
|
|
try {
|
|
if (!state.is_object() || !state.contains("lineup"))
|
|
return false;
|
|
const auto club = own(world, managed, &state);
|
|
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;
|
|
auto next = state;
|
|
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";
|
|
auto next = state;
|
|
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;
|
|
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";
|
|
const int a = slot(action.at("a")), b = slot(action.at("b"));
|
|
if (a < 0 || b < 0)
|
|
return "invalid_lineup_player";
|
|
std::swap(l["order"][size_t(a)], l["order"][size_t(b)]);
|
|
clean_takers(l, size_t(field_count));
|
|
} else if (kind == "move") {
|
|
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 >= 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(field_count), l.at("order").size()))) {
|
|
std::swap(l["order"][size_t(from)], l["order"][size_t(hit)]);
|
|
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";
|
|
const int i = action.at("value").get<int>();
|
|
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";
|
|
const int i = action.at("index").get<int>();
|
|
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";
|
|
const size_t i = action.at("index").get<size_t>();
|
|
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")) >= 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";
|
|
const int n = slot(action.at("player"));
|
|
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";
|
|
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 rows = Json::array();
|
|
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 < 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}};
|
|
}
|
|
} // namespace bmh::modern_ui
|