235 lines
9.2 KiB
C++
235 lines
9.2 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_opening.hpp"
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <set>
|
|
|
|
namespace bmh::modern_ui {
|
|
namespace {
|
|
bool integer(const Json &value, int low, int high) {
|
|
if (!value.is_number_integer())
|
|
return false;
|
|
if (value.is_number_unsigned())
|
|
return value.get<uint64_t>() >= uint64_t(low) &&
|
|
value.get<uint64_t>() <= uint64_t(high);
|
|
return value.get<int64_t>() >= low && value.get<int64_t>() <= high;
|
|
}
|
|
int tier(const Json *league) {
|
|
const auto value = league->value("tier", Json());
|
|
return integer(value, 0, 1000) ? value.get<int>()
|
|
: std::numeric_limits<int>::max();
|
|
}
|
|
struct Participants {
|
|
std::vector<std::string> pool;
|
|
Json entrants = {{"uefa", Json::array()},
|
|
{"champions", Json::array()},
|
|
{"winners", Json::array()}};
|
|
};
|
|
Participants participants(const Catalog &world) {
|
|
auto leagues = world.leagues("nation:de");
|
|
// Tier determines the top available league; equal/missing tiers retain the
|
|
// authored catalogue order, as do clubs within each league.
|
|
std::stable_sort(
|
|
leagues.begin(), leagues.end(),
|
|
[](const auto *a, const auto *b) { return tier(a) < tier(b); });
|
|
Participants result;
|
|
std::set<std::string> used;
|
|
bool top_found = false;
|
|
for (const auto *league : leagues) {
|
|
std::vector<std::string> clubs;
|
|
for (const auto *club : world.clubs(league->value("id", ""))) {
|
|
const auto id = club->value("id", "");
|
|
if (world.manageable(id) && used.insert(id).second)
|
|
clubs.push_back(id);
|
|
}
|
|
if (!top_found && !clubs.empty()) {
|
|
top_found = true;
|
|
for (size_t index = 0; index < clubs.size() && index < 6; ++index)
|
|
result
|
|
.entrants[index < 4 ? "uefa"
|
|
: index == 4 ? "champions"
|
|
: "winners"]
|
|
.push_back(clubs[index]);
|
|
}
|
|
for (const auto &id : clubs)
|
|
if (result.pool.size() < 64)
|
|
result.pool.push_back(id);
|
|
}
|
|
// A tiny world gets real pairs only: do not duplicate a team or invent a bye.
|
|
if (result.pool.size() % 2)
|
|
result.pool.pop_back();
|
|
return result;
|
|
}
|
|
bool valid(const Json &state, const Catalog &world) {
|
|
if (!state.is_object() || !state.contains("opening_flow") ||
|
|
!state.contains("opening") || !state.at("opening").is_string() ||
|
|
!state.contains("opening_created") ||
|
|
!state.at("opening_created").is_boolean())
|
|
return false;
|
|
const auto &flow = state.at("opening_flow");
|
|
if (!flow.is_object() || flow.size() != 7 ||
|
|
!integer(flow.at("schema"), 1, 1) || !flow.at("created").is_boolean() ||
|
|
!flow.at("phase").is_string() || !flow.at("playing").is_boolean() ||
|
|
!flow.at("order").is_array() || !flow.at("entrants").is_object())
|
|
return false;
|
|
const auto expected = participants(world);
|
|
const auto &order = flow.at("order");
|
|
if (order.size() != expected.pool.size() || order.size() > 64 ||
|
|
order.size() % 2 || !integer(flow.at("revealed"), 0, int(order.size())) ||
|
|
flow.at("entrants").size() != 3)
|
|
return false;
|
|
for (const auto *group : {"uefa", "champions", "winners"})
|
|
if (!flow.at("entrants").contains(group) ||
|
|
flow.at("entrants").at(group) != expected.entrants.at(group))
|
|
return false;
|
|
std::set<std::string> ids;
|
|
for (const auto &id : order) {
|
|
if (!id.is_string() || !ids.insert(id.get<std::string>()).second)
|
|
return false;
|
|
}
|
|
if (ids != std::set<std::string>(expected.pool.begin(), expected.pool.end()))
|
|
return false;
|
|
const auto phase = flow.at("phase").get<std::string>();
|
|
const bool created = flow.at("created").get<bool>(),
|
|
playing = flow.at("playing").get<bool>();
|
|
const int revealed = flow.at("revealed").get<int>();
|
|
if (phase == "uefa" || phase == "champions" || phase == "winners") {
|
|
if (revealed != 0 || playing)
|
|
return false;
|
|
} else if (phase == "draw") {
|
|
if (order.empty() || revealed >= int(order.size()))
|
|
return false;
|
|
} else if (phase == "overview" || phase == "done") {
|
|
if (revealed != int(order.size()) || playing)
|
|
return false;
|
|
} else
|
|
return false;
|
|
if (!created && phase != "done")
|
|
return false;
|
|
return state.at("opening").get<std::string>() ==
|
|
(phase == "done" ? "complete" : "pending") &&
|
|
state.at("opening_created").get<bool>() ==
|
|
(phase == "done" && created);
|
|
}
|
|
} // namespace
|
|
Json make_demo_opening(const Catalog &world, const Json & /*state*/,
|
|
bool created) {
|
|
auto data = participants(world);
|
|
uint32_t seed = 20270701u;
|
|
for (size_t count = data.pool.size(); count > 1; --count) {
|
|
seed = seed * 1664525u + 1013904223u;
|
|
std::swap(data.pool[count - 1], data.pool[size_t(seed) % count]);
|
|
}
|
|
return {{"schema", 1},
|
|
{"created", created},
|
|
{"phase", created ? "uefa" : "done"},
|
|
{"revealed", created ? size_t(0) : data.pool.size()},
|
|
{"playing", false},
|
|
{"order", data.pool},
|
|
{"entrants", data.entrants}};
|
|
}
|
|
bool migrate_demo_opening(Json &state, const Catalog &world) {
|
|
try {
|
|
if (!state.is_object())
|
|
return false;
|
|
auto next = state;
|
|
if (!next.contains("opening"))
|
|
next["opening"] = "complete";
|
|
if (!next.contains("opening_created"))
|
|
next["opening_created"] = false;
|
|
if (!next.at("opening").is_string() ||
|
|
!next.at("opening_created").is_boolean())
|
|
return false;
|
|
const auto phase = next.at("opening").get<std::string>();
|
|
if (phase != "pending" && phase != "complete")
|
|
return false;
|
|
if (!next.contains("opening_flow")) {
|
|
next["opening_flow"] = make_demo_opening(world, next, phase == "pending");
|
|
if (phase == "complete")
|
|
next["opening_flow"]["created"] = next.at("opening_created");
|
|
}
|
|
if (!valid(next, world))
|
|
return false;
|
|
state = std::move(next);
|
|
return true;
|
|
} catch (const std::exception &) {
|
|
return false;
|
|
}
|
|
}
|
|
std::string advance_opening_presentation(Json &source,const std::string &kind) {
|
|
try {
|
|
auto flow=source;
|
|
if(!flow.at("order").is_array()||!flow.at("revealed").is_number_integer()||flow.at("revealed").get<int64_t>()<0||flow.at("revealed").get<uint64_t>()>flow.at("order").size()||!flow.at("playing").is_boolean())return "Ungültiger Darstellungszustand der Saisonvorbereitung.";
|
|
const auto phase = flow.at("phase").get<std::string>();
|
|
const auto total = flow.at("order").size();
|
|
if (kind == "next") {
|
|
if (phase == "uefa")
|
|
flow["phase"] = "champions";
|
|
else if (phase == "champions")
|
|
flow["phase"] = "winners";
|
|
else if (phase == "winners") {
|
|
flow["phase"] = total ? "draw" : "overview";
|
|
flow["playing"] = total != 0;
|
|
} else
|
|
return "Weiter ist nur auf den ersten drei Vorbereitungsseiten "
|
|
"möglich.";
|
|
} else if (kind == "reveal") {
|
|
if (phase != "draw" || !flow.at("playing").get<bool>())
|
|
return "Die Auslosung läuft gerade nicht.";
|
|
const auto count = flow.at("revealed").get<size_t>() + 1;
|
|
flow["revealed"] = count;
|
|
if (count == total) {
|
|
flow["phase"] = "overview";
|
|
flow["playing"] = false;
|
|
}
|
|
} else if (kind == "toggle" || kind == "pause") {
|
|
if (phase != "draw")
|
|
return "Nur eine laufende Auslosung kann pausiert oder fortgesetzt "
|
|
"werden.";
|
|
flow["playing"] =
|
|
kind == "toggle" ? !flow.at("playing").get<bool>() : false;
|
|
} else if (kind == "skip") {
|
|
if (phase != "draw")
|
|
return "Paarungen können nur während der Auslosung eingeblendet "
|
|
"werden.";
|
|
flow["revealed"] = total;
|
|
flow["playing"] = false;
|
|
flow["phase"] = "overview";
|
|
} else if (kind == "finish") {
|
|
if (phase != "overview" && phase != "done")
|
|
return "Bitte zuerst die Paarungsübersicht öffnen.";
|
|
flow["created"] = true;
|
|
flow["phase"] = "done";
|
|
flow["revealed"] = total;
|
|
flow["playing"] = false;
|
|
} else
|
|
return "Unbekannte Aktion der Saisonvorbereitung.";
|
|
source=std::move(flow);return {};
|
|
}catch(const std::exception&){return "Ungültiger Darstellungszustand der Saisonvorbereitung.";}
|
|
}
|
|
std::string edit_demo_opening(Json &state, const Json &action,
|
|
const Catalog &world) {
|
|
try {
|
|
if (!action.is_object() || action.size() != 1 || !action.contains("kind") ||
|
|
!action.at("kind").is_string())
|
|
return "Ungültige Aktion der Saisonvorbereitung.";
|
|
auto next = state;
|
|
if (!migrate_demo_opening(next, world))
|
|
return "Die gespeicherte Saisonvorbereitung ist ungültig.";
|
|
auto &flow = next.at("opening_flow");
|
|
const auto error=advance_opening_presentation(flow,action.at("kind"));
|
|
if(!error.empty())return error;
|
|
const bool done = flow.at("phase").get<std::string>() == "done";
|
|
next["opening"] = done ? "complete" : "pending";
|
|
next["opening_created"] = done && flow.at("created").get<bool>();
|
|
if (!valid(next, world))
|
|
return "Die neue Saisonvorbereitung ist inkonsistent.";
|
|
state = std::move(next);
|
|
return {};
|
|
} catch (const std::exception &) {
|
|
return "Die Aktion der Saisonvorbereitung konnte nicht übernommen werden.";
|
|
}
|
|
}
|
|
} // namespace bmh::modern_ui
|