107 lines
4.6 KiB
C++
107 lines
4.6 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_rankings.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <set>
|
|
|
|
namespace bmh::modern_ui {
|
|
namespace {
|
|
bool position(const Json &player, const std::string &role) {
|
|
const auto it = player.find("positions");
|
|
return it != player.end() && it->is_array() &&
|
|
std::find(it->begin(), it->end(), role) != it->end();
|
|
}
|
|
bool outfield(const Json &player) {
|
|
return position(player, "def") || position(player, "mid") ||
|
|
position(player, "fwd");
|
|
}
|
|
std::string photo(const Json &player) {
|
|
const auto explicit_photo = player.value("photo", std::string());
|
|
if (!explicit_photo.empty()) return explicit_photo;
|
|
const auto id = player.value("id", std::string());
|
|
// Catalog::asset resolves a world's declared silhouette fallback, if any.
|
|
return "asset:player/" + (id.starts_with("player:") ? id.substr(7) : id) +
|
|
"/photo";
|
|
}
|
|
} // namespace
|
|
|
|
Json demo_scorer_rows(const Catalog &world, const std::string &league,
|
|
const std::string &managed_club, int scope, int kind) {
|
|
Json rows = Json::array();
|
|
if (scope < 0 || scope > 1 || kind < 0 || kind > 3) return rows;
|
|
std::vector<const Json *> clubs;
|
|
if (scope == 1) {
|
|
if (const auto *club = world.find("clubs", managed_club);
|
|
club && club->value("type", "club") == "club") clubs.push_back(club);
|
|
} else {
|
|
const auto *competition = world.find("competitions", league);
|
|
if (!competition || competition->value("type", "") != "league") return rows;
|
|
clubs = world.clubs(league);
|
|
}
|
|
if (clubs.empty()) return rows;
|
|
const std::array<const char *, 10> preferred{
|
|
"fwd", "fwd", "fwd", "fwd", "mid", "mid", "mid", "mid", "def", "def"};
|
|
const std::array<int, 10> assists{9, 12, 7, 11, 6, 8, 4, 5, 3, 10},
|
|
yellow{2, 5, 3, 8, 7, 1, 9, 6, 4, 10},
|
|
red{0, 1, 1, 2, 0, 1, 3, 0, 2, 1};
|
|
std::set<std::string> used;
|
|
for (size_t i = 0; i < preferred.size(); ++i) {
|
|
const Json *selected = nullptr, *selected_club = nullptr;
|
|
// Round-robin clubs preserve Gold's ten-club preview; short worlds may
|
|
// supply additional distinct outfield players from their actual squads.
|
|
for (size_t offset = 0; offset < clubs.size() && !selected; ++offset) {
|
|
const auto *club = clubs[(i + offset) % clubs.size()];
|
|
const auto squad = world.squad(club->at("id").get<std::string>());
|
|
for (bool exact : {true, false}) {
|
|
for (const auto *player : squad)
|
|
if (outfield(*player) && !used.contains(player->at("id").get<std::string>()) &&
|
|
(!exact || position(*player, preferred[i]))) {
|
|
selected = player; selected_club = club; break;
|
|
}
|
|
if (selected) break;
|
|
}
|
|
}
|
|
if (!selected) break;
|
|
used.insert(selected->at("id").get<std::string>());
|
|
const std::array<int, 4> values{19 - int(i), assists[i], yellow[i], red[i]};
|
|
if (!values[size_t(kind)]) continue;
|
|
rows.push_back({{"player", selected->at("id")},
|
|
{"club", selected_club->at("id")},
|
|
{"name", world.name(*selected)},
|
|
{"club_name", world.name(*selected_club)},
|
|
{"photo", photo(*selected)},
|
|
{"crest", selected_club->value("crest", "")},
|
|
{"goals", values[0]}, {"assists", values[1]},
|
|
{"yellow", values[2]}, {"red", values[3]},
|
|
{"value", values[size_t(kind)]}, {"original", i}});
|
|
}
|
|
std::stable_sort(rows.begin(), rows.end(), [](const Json &a, const Json &b) {
|
|
return a.at("value").get<int>() > b.at("value").get<int>();
|
|
});
|
|
for (size_t i = 0; i < rows.size(); ++i)
|
|
rows[i]["rank"] = i && rows[i].at("value") == rows[i - 1].at("value")
|
|
? rows[i - 1].at("rank") : Json(i + 1);
|
|
return rows;
|
|
}
|
|
|
|
Json demo_fairplay_rows(const Catalog &world) {
|
|
Json rows = Json::array();
|
|
const auto leagues = world.leagues("nation:de");
|
|
if (leagues.empty()) return rows;
|
|
const auto clubs = world.clubs(leagues.front()->at("id").get<std::string>());
|
|
for (size_t i = 0; i < clubs.size(); ++i) {
|
|
const int yellow = 18 + int(i), red = int(i % 5);
|
|
rows.push_back({{"club", clubs[i]->at("id")},
|
|
{"name", world.name(*clubs[i])},
|
|
{"crest", clubs[i]->value("crest", "")},
|
|
{"yellow", yellow}, {"red", red},
|
|
{"points", yellow + 4 * red}, {"original", i}});
|
|
}
|
|
std::stable_sort(rows.begin(), rows.end(), [](const Json &a, const Json &b) {
|
|
return a.at("points").get<int>() < b.at("points").get<int>();
|
|
});
|
|
for (size_t i = 0; i < rows.size(); ++i) rows[i]["rank"] = i + 1;
|
|
return rows;
|
|
}
|
|
} // namespace bmh::modern_ui
|