// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/demo_sports.hpp" #include #include #include #include #include namespace bmh::modern_ui { namespace { bool integers(const Json &a, size_t size, int low, int high) { if (!a.is_array() || a.size() != size) return false; return std::all_of(a.begin(), a.end(), [&](const auto &v) { return v.is_number_integer() && v >= low && v <= high; }); } int sum(const Json &a, size_t begin, size_t end) { int n = 0; for (size_t i = begin; i < end; ++i) n += a.at(i).template get(); return n; } constexpr std::array tactic_limits = {3, 3, 3, 3, 1, 1, 1, 11}; } // namespace Json demo_training(const Catalog &world, const std::string &club) { Json players = Json::object(); size_t i = 0; for (auto *player : world.squad(club)) { players[player->value("id", "")] = {{"intensity", i % 5 == 0 ? 1 : 2}, {"focus", int(i % 8)}}; ++i; } return {{"club", club}, {"areas", {2, 2, 1, 1, 2, 2, 2, 2, 3}}, {"types", {2, 2, 2, 2}}, {"autos", {false, false, false, false}}, {"players", players}}; } bool valid_demo_training(const Json &value, const Catalog &world, const std::string &club) { try { if (!value.is_object() || value.at("club") != club || !integers(value.at("areas"), 9, 1, 3) || !integers(value.at("types"), 4, 1, 3)) return false; if (sum(value["types"], 0, 4) > 8 || sum(value["areas"], 0, 2) > 4 || sum(value["areas"], 2, 9) > 18) return false; const auto &autos = value.at("autos"); if (!autos.is_array() || autos.size() != 4) return false; for (const auto &v : autos) if (!v.is_boolean()) return false; const auto &players = value.at("players"); const auto squad = world.squad(club); if (!players.is_object() || players.size() != squad.size()) return false; for (auto *p : squad) { const auto &v = players.at(p->value("id", "")); if (!v.at("intensity").is_number_integer() || v["intensity"] < 0 || v["intensity"] > 3 || !v.at("focus").is_number_integer() || v["focus"] < 0 || v["focus"] > 7) return false; } return true; } catch (const Json::exception &) { return false; } } std::string edit_demo_training(Json &state, const Json &action) { for (const auto *key : {"index", "value"}) if (action.contains(key) && (!action[key].is_number_integer() || action[key] < 0 || action[key] > 100)) return "invalid_training_value"; auto next = state; const auto kind = action.at("kind").get(); if (kind == "auto" || kind == "auto-all") { const int group = kind == "auto-all" ? -1 : action.at("index").get(); if (group < -1 || group > 3) return "invalid_training_group"; const bool enabled = kind == "auto-all" ? !std::all_of(next["autos"].begin(), next["autos"].end(), [](const auto &v) { return v == true; }) : !next["autos"][size_t(group)].get(); for (int i = 0; i < 4; ++i) if (group == -1 || group == i) { next["autos"][size_t(i)] = enabled; if (!enabled) continue; if (i == 0) next["types"] = {2, 2, 2, 2}; if (i == 1) { next["areas"][0] = 2; next["areas"][1] = 2; } if (i == 2) for (size_t j = 2; j < 9; ++j) next["areas"][j] = 2; if (i == 3) for (auto &[id, p] : next["players"].items()) { (void)id; p["intensity"] = 2; } } } else if (kind == "focus") { const auto player = action.at("player").get(); const int value = action.at("value").get(); if (!next["players"].contains(player) || value < 0 || value > 7) return "invalid_training_player"; if (next["autos"][3] == true) return "training_automatic"; next["players"][player]["focus"] = value; } else if (kind == "ball") { const auto group = action.at("group").get(); const int value = action.at("value").get(); if (value < 1 || value > 3) return "invalid_training_value"; if (group == "player") { const auto player = action.at("player").get(); if (!next["players"].contains(player)) return "invalid_training_player"; if (next["autos"][3] == true) return "training_automatic"; auto &v = next["players"][player]["intensity"]; v = v == value ? 0 : value; } else { const int index = action.at("index").get(); const bool area = group == "area"; if ((!area && group != "type") || index < 0 || index >= (area ? 9 : 4)) return "invalid_training_group"; if (next["autos"][size_t(area ? (index < 2 ? 1 : 2) : 0)] == true) return "training_automatic"; auto &list = next[area ? "areas" : "types"]; list[size_t(index)] = value; if (sum(next["types"], 0, 4) > 8 || sum(next["areas"], 0, 2) > 4 || sum(next["areas"], 2, 9) > 18) return "training_preview_budget"; } } else return "unknown_training_action"; state = std::move(next); return {}; } Json demo_tactics() { return {{"values", {2, 1, 1, 0, 1, 0, 1, 6}}}; } bool valid_demo_tactics(const Json &value) { try { const auto &v = value.at("values"); if (!integers(v, 8, 0, 11)) return false; for (size_t i = 0; i < 8; ++i) if (v[i] > tactic_limits[i]) return false; return true; } catch (const Json::exception &) { return false; } } std::string edit_demo_tactics(Json &state, const Json &action) { for (const auto *key : {"index", "value"}) if (action.contains(key) && (!action[key].is_number_integer() || action[key] < 0 || action[key] > 100)) return "invalid_tactic_value"; const auto kind = action.at("kind").get(); const int index = kind == "effort" ? 7 : action.at("index").get(); if (index < 0 || index > 7) return "invalid_tactic_index"; if (kind == "cycle") state["values"][size_t(index)] = (state["values"][size_t(index)].get() + 1) % (tactic_limits[size_t(index)] + 1); else if (kind == "effort") { const int v = action.at("value").get(); if (v < 0 || v > 11) return "invalid_tactic_value"; state["values"][7] = v; } else return "unknown_tactic_action"; return {}; } std::string tactics_diagram(const Json &state, const std::string &theme, const Json &lineup_points) { const auto values = state.at("values").get>(); const std::string line = theme == "silver" ? "#536f66" : "#dfe8bf"; const auto field_line = theme == "silver" ? "#536f66" : "#d7edbf", man_line = theme == "silver" ? "#536f66" : "#c9e6ae", curve_line = theme == "silver" ? "#536f66" : "#d4e7ba"; const std::string defence = theme == "silver" ? "#85600c" : "#ffdd6b"; std::ostringstream out; out.imbue(std::locale::classic()); out << ""; const auto field = [&](double x, double y, double w, double h) { out << ""; }; const auto dot = [&](double x, double y, bool opponent = false) { out << ""; }; const auto arrow = [&](double x, double y, double dx, double dy, bool dashed = false) { out << ""; }; field(19, 9, 104, 62); field(19, 82, 104, 62); field(137, 10, 58, 37); field(205, 10, 58, 37); std::array, 10> formation = {{{5, 1}, {5, 3}, {5, 5}, {5, 7}, {10, 1}, {10, 3}, {10, 5}, {10, 7}, {16, 3}, {16, 5}}}; if (!lineup_points.is_null()) { if (!lineup_points.is_array() || lineup_points.size() != 11) throw editor::Error("invalid_lineup_points"); std::set> occupied; for (size_t i = 0; i < lineup_points.size(); ++i) { const auto &p = lineup_points[i]; if (!p.is_array() || p.size() != 2 || !p[0].is_number_integer() || !p[1].is_number_integer() || p[0] < 0 || p[0] > 18 || p[1] < 0 || p[1] > 8) throw editor::Error("invalid_lineup_points"); const std::pair point{p[0].get(), p[1].get()}; if (!occupied.insert(point).second) throw editor::Error("invalid_lineup_points"); if (i) formation[i - 1] = point; } } for (const auto &[x, y] : formation) { const double px = 25 + x / 18.0 * 82, py = 16 + y / 8.0 * 49; arrow(px, py, std::array{-16, -7, 7, 16}[size_t(values[0])], 0); dot(px, py); } for (int k = 0; k < 2; ++k) { const int x = 137 + k * 68; dot(x + 12, 27); dot(x + 25, 20); dot(x + 25, 37); dot(x + 43, 28, true); if (values[size_t(5 + k)] == 0) out << ""; } dot(29, 111); dot(76, 94); dot(76, 119); dot(101, 118); dot(111, 120, true); arrow(29, 111, 10 + values[3] * 10, -values[3] * 5, true); arrow(76, 94, 10 + values[2] * 8, 12 - values[2] * 5); arrow(76, 119, 10 + values[1] * 8, -8 + values[1] * 4, true); for (int x : {181, 222}) out << ""; out << ""; return out.str(); } } // namespace bmh::modern_ui