180 lines
7.7 KiB
C++
180 lines
7.7 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
||
// OpenBMH deviates from the original: shows recorded manager history between
|
||
// seasons without inventing future results.
|
||
#include "modern/ui/engine_bridge.hpp"
|
||
#include <algorithm>
|
||
#include <array>
|
||
#include <set>
|
||
namespace bmh::modern_ui {
|
||
namespace {
|
||
void check(bool yes, const char *text) {
|
||
if (!yes)
|
||
throw editor::Error(text);
|
||
}
|
||
std::string season(int year) {
|
||
return std::to_string(year) + "/" + ((year + 1) % 100 < 10 ? "0" : "") +
|
||
std::to_string((year + 1) % 100);
|
||
}
|
||
} // namespace
|
||
Json engine_achievements_projection(const Json &bundle, const Json &leagues,
|
||
int page) {
|
||
validate_engine_desk(bundle);
|
||
const auto &read = bundle.at("achievements"),
|
||
&meta = bundle.at("desktop").at("meta");
|
||
check(read.value("messageType", "") == "snapshot" &&
|
||
read.at("data").value("kind", "") == "achievements",
|
||
"Erfolge sind hier nicht verfügbar.");
|
||
for (const auto *key : {"contractVersion", "careerId", "worldId",
|
||
"worldRevision", "stateRevision", "scope"})
|
||
check(read.at("meta").at(key) == meta.at(key),
|
||
"Der Karrierekontext hat sich geändert.");
|
||
const auto &d = read.at("data");
|
||
check(meta.at("scope").at("role") == "club" &&
|
||
d.at("managerId") == meta.at("scope").at("managerId") &&
|
||
d.at("teamId") == meta.at("scope").at("teamId"),
|
||
"Die Erfolgsaufzeichnungen gehören einem anderen Manager.");
|
||
check(d.at("records").is_array() && d.at("records").size() <= 50,
|
||
"Ungültiger Karriereumfang.");
|
||
const int max_page =
|
||
d.at("records").empty() ? 0 : int((d.at("records").size() - 1) / 8);
|
||
check(page >= 0 && page <= max_page, "Diese Karriereseite existiert nicht.");
|
||
Json result = {
|
||
{"ok", true},
|
||
{"available", d.at("available") == true && !d.at("records").empty()},
|
||
{"fictional", false},
|
||
{"club", d.at("teamId")},
|
||
{"manager", bundle.at("desktop").at("data").at("manager").at("name")},
|
||
{"page", page},
|
||
{"max_page", max_page},
|
||
{"charts", Json::array()},
|
||
{"history", Json::array()},
|
||
{"range", "—"}};
|
||
if (!result.at("available").get<bool>())
|
||
return result;
|
||
const int current = d.at("currentSeasonYear");
|
||
std::set<int> years;
|
||
std::array<std::set<std::string>, 4> bands;
|
||
for (const auto &[id, l] : leagues.items()) {
|
||
(void)id;
|
||
const auto t = l.value("tier", Json());
|
||
if (t.is_number_integer() && t >= 1 && t <= 4)
|
||
bands[size_t(t.get<int>() - 1)].insert(l.at("name").get<std::string>());
|
||
}
|
||
Json history = Json::array();
|
||
for (const auto &record : d.at("records")) {
|
||
const int year = record.at("year");
|
||
check(year >= 1 && year <= current && years.insert(year).second,
|
||
"Doppeltes oder zukünftiges Karrierejahr.");
|
||
check(record.at("completed") == Json(year < current),
|
||
"Der Saisonstatus ist widersprüchlich.");
|
||
const auto label = season(year);
|
||
Json values = Json::array();
|
||
const auto &league = record.at("league");
|
||
Json value = league.at("position"), tier = nullptr;
|
||
std::string league_name = "—",
|
||
placement = league.value("label", std::string());
|
||
if (placement.empty())
|
||
placement = "Nicht erfasst";
|
||
if (!league.at("leagueId").is_null()) {
|
||
const auto id = league.at("leagueId").get<std::string>();
|
||
check(leagues.contains(id),
|
||
"Historische Liga fehlt im aktuellen Regelprofil.");
|
||
const auto &l = leagues.at(id);
|
||
check(
|
||
l.at("tier").is_number_integer() && l.at("tier").get<int>() >= 1 &&
|
||
l.at("tier").get<int>() <= 4,
|
||
"Die Spielklasse kann in diesem Diagramm nicht dargestellt werden.");
|
||
tier = l.at("tier");
|
||
league_name = l.at("name");
|
||
check(value.is_number_integer() &&
|
||
league.at("teams").is_number_integer() && value >= 1 &&
|
||
value <= league.at("teams"),
|
||
"Ungültige historische Platzierung.");
|
||
placement = std::to_string(value.get<int>()) + ". Platz";
|
||
bands[size_t(tier.get<int>() - 1)].insert(league_name);
|
||
} else
|
||
check(value.is_null(), "Platzierung ohne Liga.");
|
||
values.push_back({{"year", year},
|
||
{"season", label},
|
||
{"value", value},
|
||
{"max_position", league.at("teams")},
|
||
{"tier", tier},
|
||
{"tier_name", league_name},
|
||
{"label", placement},
|
||
{"caption", value.is_number_integer()
|
||
? std::to_string(value.get<int>()) + "."
|
||
: "—"},
|
||
{"winner", value == 1},
|
||
{"completed", record.at("completed")},
|
||
{"fictional", false}});
|
||
for (const auto *key : {"cup", "europe"}) {
|
||
const auto &cup = record.at(key);
|
||
const int stage = cup.at("stage");
|
||
check(stage >= 0 && stage <= 31 && cup.at("winner") == Json(stage == 7),
|
||
"Ungültiger Pokalstatus.");
|
||
Json progress = stage == 0 ? Json(0)
|
||
: stage <= 7 ? Json(1 << (7 - stage))
|
||
: Json();
|
||
const auto text = cup.at("label").get<std::string>();
|
||
const std::array<const char *, 8> captions{
|
||
"–", "1. Runde", "2. Runde", "Achtelfinale", "Viertelfinale",
|
||
"Halbfinale", "Finale", "Sieger"};
|
||
values.push_back({{"year", year},
|
||
{"season", label},
|
||
{"value", progress},
|
||
{"tier", nullptr},
|
||
{"tier_name", nullptr},
|
||
{"label", text.empty() ? "Nicht erfasst" : text},
|
||
{"caption", stage <= 7 ? captions[size_t(stage)] : "—"},
|
||
{"winner", cup.at("winner")},
|
||
{"completed", record.at("completed")},
|
||
{"fictional", false}});
|
||
}
|
||
history.push_back({{"year", year},
|
||
{"season", label},
|
||
{"completed", record.at("completed")},
|
||
{"results", values}});
|
||
}
|
||
std::sort(history.begin(), history.end(), [](const auto &a, const auto &b) {
|
||
return a.at("year") < b.at("year");
|
||
});
|
||
const size_t end = history.size() - size_t(page) * 8,
|
||
start = end > 8 ? end - 8 : 0;
|
||
result["range"] =
|
||
start + 1 == end
|
||
? history[start].at("season")
|
||
: Json(history[start].at("season").get<std::string>() + " – " +
|
||
history[end - 1].at("season").get<std::string>());
|
||
const std::array<const char *, 3> titles{"Meisterschaft", "DFB-Pokal",
|
||
"Europapokal"};
|
||
Json labels = Json::array();
|
||
for (size_t i = 0; i < 4; ++i) {
|
||
std::string name;
|
||
for (const auto &n : bands[i]) {
|
||
if (!name.empty())
|
||
name += " · ";
|
||
name += n;
|
||
}
|
||
labels.push_back(name.empty() ? "Spielklasse " + std::to_string(i + 1)
|
||
: name);
|
||
}
|
||
for (size_t kind = 0; kind < 3; ++kind) {
|
||
Json rows = Json::array();
|
||
for (size_t i = start; i < end; ++i)
|
||
rows.push_back(history[i].at("results")[kind]);
|
||
result["charts"].push_back({{"kind", kind},
|
||
{"title", titles[kind]},
|
||
{"latest", rows.back()},
|
||
{"seasons", rows},
|
||
{"bands", labels}});
|
||
}
|
||
std::reverse(history.begin(), history.end());
|
||
result["history"] = history;
|
||
result["history_caption"] =
|
||
std::to_string(history.size()) + " erfasste Saison" +
|
||
(history.size() == 1 ? "" : "s") + " · Karriere von " +
|
||
result.at("manager").get<std::string>();
|
||
return result;
|
||
}
|
||
} // namespace bmh::modern_ui
|