Port report menu package and club/player profiles (by Codex)
This commit is contained in:
1 parent
201eef88e4
commit
13461c693a
23 files changed
+2122
-137
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp)
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp)
|
||||
target_include_directories(bmh_modern_ui_core PUBLIC ../..)
|
||||
target_link_libraries(bmh_modern_ui_core PUBLIC bmh_asset_io)
|
||||
add_library(bmh_modern_ui STATIC view.cpp ticket_popover.cpp)
|
||||
@@ -14,7 +14,7 @@ endif()
|
||||
set(modern_assets "${CMAKE_BINARY_DIR}/assets/modern")
|
||||
FetchContent_GetProperties(rmlui)
|
||||
file(MAKE_DIRECTORY "${modern_assets}")
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss)
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss)
|
||||
configure_file("${CMAKE_SOURCE_DIR}/assets/modern/${asset}" "${modern_assets}/${asset}" COPYONLY)
|
||||
endforeach()
|
||||
file(GLOB modern_icons CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/assets/modern/icons/*.svg")
|
||||
@@ -41,6 +41,12 @@ if(MSVC)
|
||||
target_compile_options(modern_ui_core_tests PRIVATE /utf-8)
|
||||
endif()
|
||||
add_test(NAME modern_ui_core_tests COMMAND modern_ui_core_tests)
|
||||
add_executable(modern_reports_tests ../../../tests/modern_reports_tests.cpp)
|
||||
target_link_libraries(modern_reports_tests PRIVATE bmh_modern_ui_core)
|
||||
if(MSVC)
|
||||
target_compile_options(modern_reports_tests PRIVATE /utf-8)
|
||||
endif()
|
||||
add_test(NAME modern_reports_tests COMMAND modern_reports_tests)
|
||||
add_executable(modern_ticket_document_tests ../../../tests/modern_ticket_document_tests.cpp ticket_popover.cpp)
|
||||
target_link_libraries(modern_ticket_document_tests PRIVATE bmh_modern_ui_core RmlUi::Core)
|
||||
if(MSVC)
|
||||
|
||||
+65
-31
@@ -148,6 +148,27 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
n = n->GetParentNode())
|
||||
if (n->GetId() == "native-ticket-layer")
|
||||
return;
|
||||
if (!report_detail_kind_.empty() && popup_.empty()) {
|
||||
const bool keyboard = type == "keydown";
|
||||
const auto key = keyboard
|
||||
? Rml::Input::KeyIdentifier(event.GetParameter<int>("key_identifier", 0))
|
||||
: Rml::Input::KI_UNKNOWN;
|
||||
if ((keyboard && key == Rml::Input::KI_ESCAPE) ||
|
||||
(type == "mouseup" && event.GetParameter<int>("button", 0) == 1)) {
|
||||
report_detail_action("report-detail-close", nullptr);
|
||||
event.StopImmediatePropagation();
|
||||
return;
|
||||
}
|
||||
bool inside = false;
|
||||
for (auto *n = event.GetTargetElement(); n && n != doc_; n = n->GetParentNode())
|
||||
if (n->GetId() == "report-detail-layer") inside = true;
|
||||
if (!inside && (type == "click" || type == "dblclick" || type == "mousedown" ||
|
||||
type == "mouseup" || (keyboard && key != Rml::Input::KI_TAB))) {
|
||||
event.StopImmediatePropagation();
|
||||
return;
|
||||
}
|
||||
} else if (report_detail_kind_.empty() && popup_.empty() && report_picker_event(event))
|
||||
return;
|
||||
if (type == "mouseup" && event.GetParameter<int>("button", 0) == 1) {
|
||||
if (scene_ == "desktop" && !menu_.empty() && popup_.empty()) {
|
||||
menu_.clear();
|
||||
@@ -291,14 +312,16 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
if (key == Rml::Input::KI_TAB &&
|
||||
(!menu_.empty() || !popup_.empty() || scene_ != "desktop")) {
|
||||
Rml::ElementList controls;
|
||||
auto *root = !popup_.empty() ? element("popup") : element("canvas");
|
||||
auto *root = !popup_.empty() ? element("popup")
|
||||
: !report_detail_kind_.empty() ? element("report-detail-layer")
|
||||
: element("canvas");
|
||||
root->QuerySelectorAll(
|
||||
controls, !popup_.empty()
|
||||
controls, !popup_.empty() || !report_detail_kind_.empty()
|
||||
? "button,input,select"
|
||||
: ".window button,.window input,.window select");
|
||||
controls.erase(std::remove_if(controls.begin(), controls.end(),
|
||||
[](auto *n) {
|
||||
return !n->IsVisible() ||
|
||||
return !n->IsVisible(true) ||
|
||||
n->HasAttribute("disabled");
|
||||
}),
|
||||
controls.end());
|
||||
@@ -310,6 +333,10 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
const auto next = (old + (backwards ? -1 : 1) + int(controls.size())) %
|
||||
int(controls.size());
|
||||
controls[size_t(next)]->Focus(true);
|
||||
if (controls[size_t(next)]->GetId().starts_with("report-detail-player-") ||
|
||||
controls[size_t(next)]->GetId().starts_with("report-club-"))
|
||||
controls[size_t(next)]->ScrollIntoView(
|
||||
Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
|
||||
event.StopImmediatePropagation();
|
||||
}
|
||||
return;
|
||||
@@ -347,6 +374,31 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
void App::dispatch(const std::string &action, Rml::Element *target) {
|
||||
const auto id =
|
||||
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
|
||||
if (report_detail_action(action, target)) {
|
||||
close_report_picker(false);
|
||||
return;
|
||||
}
|
||||
if (report_picker_action(action, target))
|
||||
return;
|
||||
if (action == "report-sort" || action == "report-reset" || action == "report-round") {
|
||||
Json change;
|
||||
if (action == "report-sort")
|
||||
change = {{"kind", "sort"}, {"view", menu_}, {"key", id}};
|
||||
else if (action == "report-reset")
|
||||
change = {{"kind", "reset"}};
|
||||
else {
|
||||
if (menu_ == "other" && snap().state.value("opening_created", false) &&
|
||||
snap().state.at("reports").value("competition", 0) == 3)
|
||||
return;
|
||||
change = {{"kind", "round"}, {"step", std::stoi(id)}, {"other", menu_ == "other"}};
|
||||
}
|
||||
if (apply("reports", change)) {
|
||||
close_report_picker(false);
|
||||
focus_after_render_ = target->GetId();
|
||||
refresh();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (action == "popup-close") {
|
||||
popup_.clear();
|
||||
confirm_ = {};
|
||||
@@ -932,12 +984,14 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
|
||||
}
|
||||
static const std::set<std::string> available = {
|
||||
"tables", "strengths", "camp", "settings",
|
||||
"save", "load", "training", "tactics", "calendar"};
|
||||
"save", "load", "training", "tactics", "calendar", "league", "other"};
|
||||
if (!available.contains(route)) {
|
||||
message("Diese native Ansicht wird im nächsten Menüpaket übertragen.");
|
||||
return;
|
||||
}
|
||||
menu_ = route;
|
||||
report_picker_.clear();
|
||||
report_detail_kind_.clear();
|
||||
if (route == "settings") {
|
||||
settings_category_ = "display";
|
||||
draft_preferences_ = nullptr;
|
||||
@@ -1124,19 +1178,6 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (action == "player-detail") {
|
||||
if (const auto *p = world_->find("players", id))
|
||||
message(world_->name(*p) + " · Stärke " +
|
||||
std::to_string(
|
||||
p->value("attributes", Json::object()).value("strength", 0)));
|
||||
return;
|
||||
}
|
||||
if (action == "club-detail") {
|
||||
if (const auto *c = world_->find("clubs", id))
|
||||
message(world_->name(*c) + " · " +
|
||||
std::to_string(world_->squad(id).size()) + " Spieler");
|
||||
return;
|
||||
}
|
||||
if (action == "squad-tactics") {
|
||||
menu_ = "tactics";
|
||||
refresh();
|
||||
@@ -1187,13 +1228,11 @@ void App::change(Rml::Element *target) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if ((kind == "manager-country" || kind == "table-country") &&
|
||||
value == country_)
|
||||
if (kind == "manager-country" && value == country_)
|
||||
return;
|
||||
if ((kind == "manager-league" || kind == "table-league") && value == league_)
|
||||
if (kind == "manager-league" && value == league_)
|
||||
return;
|
||||
if (kind == "manager-country" || kind == "manager-league" ||
|
||||
kind == "table-country" || kind == "table-league")
|
||||
if (kind == "manager-country" || kind == "manager-league")
|
||||
focus_after_render_ = target->GetId();
|
||||
if (kind == "manager-country" || kind == "manager-league") {
|
||||
if (!manager_picker_)
|
||||
@@ -1206,15 +1245,6 @@ void App::change(Rml::Element *target) {
|
||||
refresh();
|
||||
} else if (kind == "manager-strength") {
|
||||
selections_["manager-strength"] = value;
|
||||
} else if (kind == "table-country") {
|
||||
country_ = value;
|
||||
league_.clear();
|
||||
refresh();
|
||||
} else if (kind == "table-league") {
|
||||
league_ = value;
|
||||
refresh();
|
||||
} else if (kind == "table-scope") {
|
||||
selections_["table-scope"] = value;
|
||||
}
|
||||
}
|
||||
void App::input(Rml::Element *target) {
|
||||
@@ -1331,6 +1361,10 @@ void App::script() {
|
||||
editor::path(command.at("path").get<std::string>()),
|
||||
Json{{"scene", scene_},
|
||||
{"menu", menu_},
|
||||
{"report_picker", report_picker_},
|
||||
{"report_detail", {{"kind", report_detail_kind_},
|
||||
{"club", report_detail_club_},
|
||||
{"player", report_detail_player_}}},
|
||||
{"desktop", desktop_},
|
||||
{"demo", source_ ? snap().state : Json()}}
|
||||
.dump(2));
|
||||
|
||||
@@ -0,0 +1,436 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_reports.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
constexpr int played_rounds = 26;
|
||||
constexpr int other_rounds = 34;
|
||||
constexpr std::array<int, 9> other_pairs = {9, 8, 4, 8, 4, 4, 8, 8, 1};
|
||||
struct Fixture {
|
||||
int home, away, home_goals, away_goals, round;
|
||||
};
|
||||
bool integer(const Json &v, int low, int high) {
|
||||
return v.is_number_integer() && v >= low && v <= high;
|
||||
}
|
||||
bool keys(const Json &v, std::initializer_list<const char *> required,
|
||||
std::initializer_list<const char *> optional = {}) {
|
||||
if (!v.is_object())
|
||||
return false;
|
||||
std::set<std::string> allowed;
|
||||
for (const auto *key : required) {
|
||||
if (!v.contains(key))
|
||||
return false;
|
||||
allowed.emplace(key);
|
||||
}
|
||||
for (const auto *key : optional)
|
||||
allowed.emplace(key);
|
||||
for (const auto &entry : v.items())
|
||||
if (!allowed.contains(entry.key()))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
int rounds(size_t size) {
|
||||
if (size < 2)
|
||||
return 1;
|
||||
if (size > size_t(std::numeric_limits<int>::max() / 2 - 1))
|
||||
return 1;
|
||||
return 2 * (int(size) + int(size % 2) - 1);
|
||||
}
|
||||
std::string initial_league(const Catalog &world) {
|
||||
auto leagues = world.leagues("nation:de");
|
||||
if (leagues.empty())
|
||||
leagues = world.leagues();
|
||||
return leagues.empty() ? "" : leagues.front()->at("id").get<std::string>();
|
||||
}
|
||||
bool league_exists(const Catalog &world, const std::string &id) {
|
||||
const auto leagues = world.leagues();
|
||||
if (leagues.empty())
|
||||
return id.empty();
|
||||
return std::any_of(leagues.begin(), leagues.end(), [&](const auto *league) {
|
||||
return league->at("id") == id;
|
||||
});
|
||||
}
|
||||
int league_rounds(const Catalog &world, const std::string &id) {
|
||||
return id.empty() ? 1 : rounds(world.clubs(id).size());
|
||||
}
|
||||
int league_index(const Catalog &world, const std::string &id) {
|
||||
const auto leagues = world.leagues();
|
||||
const auto found = std::find_if(leagues.begin(), leagues.end(), [&](auto *l) {
|
||||
return l->at("id") == id;
|
||||
});
|
||||
return found == leagues.end() ? 0 : int(found - leagues.begin());
|
||||
}
|
||||
bool sort_key(const std::string &view, const std::string &key) {
|
||||
static const std::set<std::string> table_keys = {
|
||||
"original", "rank", "played", "wins", "draw", "loss", "form",
|
||||
"points", "gf", "diff"};
|
||||
static const std::set<std::string> strength_keys = {
|
||||
"original", "club", "keeper", "defence", "midfield", "attack",
|
||||
"strength"};
|
||||
return view == "tables" ? table_keys.contains(key)
|
||||
: view == "strengths" && strength_keys.contains(key);
|
||||
}
|
||||
std::vector<Fixture> fixtures(size_t club_count, int league) {
|
||||
if (club_count < 2 || club_count > size_t(std::numeric_limits<int>::max() / 2 - 1))
|
||||
return {};
|
||||
const int count = int(club_count), ring_size = count + count % 2;
|
||||
std::vector<int> ring(size_t(ring_size), 0);
|
||||
std::iota(ring.begin(), ring.end(), 0);
|
||||
std::vector<Fixture> result;
|
||||
for (int round = 0; round < ring_size - 1; ++round) {
|
||||
for (int pair = 0; pair < ring_size / 2; ++pair) {
|
||||
int home = ring[size_t(pair)], away = ring[size_t(ring_size - 1 - pair)];
|
||||
if ((pair == 0 ? round : pair) % 2)
|
||||
std::swap(home, away);
|
||||
if (home < count && away < count)
|
||||
result.push_back({home, away, 0, 0, round});
|
||||
}
|
||||
std::rotate(ring.begin() + 1, ring.end() - 1, ring.end());
|
||||
}
|
||||
const auto first_size = result.size();
|
||||
for (size_t i = 0; i < first_size; ++i) {
|
||||
const auto first = result[i];
|
||||
result.push_back({first.away, first.home, 0, 0,
|
||||
first.round + ring_size - 1});
|
||||
}
|
||||
for (auto &match : result) {
|
||||
// HTML preview arithmetic, deliberately independent of every game RNG.
|
||||
const uint32_t seed = uint32_t(match.round + 1) * 97u +
|
||||
uint32_t(match.home + 3) * 37u +
|
||||
uint32_t(match.away + 5) * 19u +
|
||||
uint32_t(league) * 53u;
|
||||
match.home_goals = int((seed ^ (seed >> 3)) % 5);
|
||||
match.away_goals = int((seed ^ (seed >> 5)) % 4);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// Deterministic name ordering without a host locale dependency. Common Latin
|
||||
// accents are primary-equivalent, and digit runs compare numerically.
|
||||
std::string name_key(std::string name) {
|
||||
for (const auto &[from, to] :
|
||||
std::initializer_list<std::pair<std::string_view, std::string_view>>{
|
||||
{"Ä", "a"}, {"ä", "a"}, {"Ö", "o"}, {"ö", "o"},
|
||||
{"Ü", "u"}, {"ü", "u"}, {"ß", "ss"}, {"ẞ", "ss"},
|
||||
{"Á", "a"}, {"á", "a"}, {"À", "a"}, {"à", "a"},
|
||||
{"Â", "a"}, {"â", "a"}, {"É", "e"}, {"é", "e"},
|
||||
{"È", "e"}, {"è", "e"}, {"Ê", "e"}, {"ê", "e"},
|
||||
{"Í", "i"}, {"í", "i"}, {"Ó", "o"}, {"ó", "o"},
|
||||
{"Ô", "o"}, {"ô", "o"}, {"Ú", "u"}, {"ú", "u"},
|
||||
{"Ç", "c"}, {"ç", "c"}, {"Ñ", "n"}, {"ñ", "n"}}) {
|
||||
size_t at = 0;
|
||||
while ((at = name.find(from, at)) != std::string::npos) {
|
||||
name.replace(at, from.size(), to);
|
||||
at += to.size();
|
||||
}
|
||||
}
|
||||
for (char &c : name)
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
c += 'a' - 'A';
|
||||
return name;
|
||||
}
|
||||
int name_compare(const std::string &a, const std::string &b) {
|
||||
const auto x = name_key(a), y = name_key(b);
|
||||
size_t i = 0, j = 0;
|
||||
const auto digit = [](char c) { return c >= '0' && c <= '9'; };
|
||||
while (i < x.size() && j < y.size()) {
|
||||
if (digit(x[i]) && digit(y[j])) {
|
||||
size_t ei = i, ej = j;
|
||||
while (ei < x.size() && digit(x[ei])) ++ei;
|
||||
while (ej < y.size() && digit(y[ej])) ++ej;
|
||||
while (i < ei && x[i] == '0') ++i;
|
||||
while (j < ej && y[j] == '0') ++j;
|
||||
if (ei - i != ej - j)
|
||||
return ei - i < ej - j ? -1 : 1;
|
||||
const int n = x.compare(i, ei - i, y, j, ej - j);
|
||||
if (n)
|
||||
return n < 0 ? -1 : 1;
|
||||
i = ei;
|
||||
j = ej;
|
||||
continue;
|
||||
}
|
||||
if (x[i] != y[j])
|
||||
return static_cast<unsigned char>(x[i]) < static_cast<unsigned char>(y[j]) ? -1 : 1;
|
||||
++i;
|
||||
++j;
|
||||
}
|
||||
return i == x.size() ? (j == y.size() ? 0 : -1) : 1;
|
||||
}
|
||||
Json sorted(std::vector<Json> rows, const Json &sort) {
|
||||
const auto key = sort.at("key").get<std::string>();
|
||||
const bool desc = sort.at("desc").get<bool>();
|
||||
std::stable_sort(rows.begin(), rows.end(), [&](const Json &a, const Json &b) {
|
||||
const auto &x = a.at(key == "club" ? "name" : key);
|
||||
const auto &y = b.at(key == "club" ? "name" : key);
|
||||
if (x.is_null() != y.is_null())
|
||||
return !x.is_null(); // Missing OBW data remains last in both directions.
|
||||
const int comparison = x.is_null() ? 0
|
||||
: key == "club" ? name_compare(x.get<std::string>(), y.get<std::string>())
|
||||
: x == y ? 0 : x < y ? -1 : 1;
|
||||
return comparison ? (desc ? comparison > 0 : comparison < 0)
|
||||
: a.at("original") < b.at("original");
|
||||
});
|
||||
return Json(rows);
|
||||
}
|
||||
Json average(const std::vector<const Json *> &squad, const std::string &position) {
|
||||
double sum = 0;
|
||||
int count = 0;
|
||||
for (auto *player : squad) {
|
||||
const auto attrs = player->find("attributes");
|
||||
if (attrs == player->end() || !attrs->is_object())
|
||||
continue;
|
||||
const auto strength = attrs->find("strength");
|
||||
if (strength == attrs->end() || !strength->is_number())
|
||||
continue;
|
||||
const double value = strength->get<double>();
|
||||
if (!std::isfinite(value) || value < 0 || value > 100)
|
||||
continue;
|
||||
if (!position.empty()) {
|
||||
const auto positions = player->find("positions");
|
||||
if (positions == player->end() || !positions->is_array() ||
|
||||
std::find(positions->begin(), positions->end(), position) == positions->end())
|
||||
continue;
|
||||
}
|
||||
sum += value;
|
||||
++count;
|
||||
}
|
||||
return count ? Json(int(std::lround(sum / count))) : Json();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json demo_reports(const Catalog &world) {
|
||||
const auto league = initial_league(world);
|
||||
return {{"league", league},
|
||||
{"round", std::min(26, league_rounds(world, league) - 1)},
|
||||
{"filter", 0}, {"competition", 0},
|
||||
{"sorts", {{"tables", {{"key", "points"}, {"desc", true}}},
|
||||
{"strengths", {{"key", "original"}, {"desc", false}}}}}};
|
||||
}
|
||||
bool valid_demo_reports(const Json &state, const Catalog &world) {
|
||||
try {
|
||||
if (!keys(state, {"league", "round", "filter", "competition", "sorts"}) ||
|
||||
!state.at("league").is_string() ||
|
||||
!league_exists(world, state.at("league").get<std::string>()) ||
|
||||
!integer(state.at("filter"), 0, 4) ||
|
||||
!integer(state.at("competition"), 0, 8))
|
||||
return false;
|
||||
const int count = std::max(league_rounds(world, state.at("league")),
|
||||
state.at("competition") == 0 ? 1 : other_rounds);
|
||||
if (!integer(state.at("round"), 0, count - 1) ||
|
||||
!keys(state.at("sorts"), {"tables", "strengths"}))
|
||||
return false;
|
||||
for (const auto *view : {"tables", "strengths"}) {
|
||||
const auto &sort = state.at("sorts").at(view);
|
||||
if (!keys(sort, {"key", "desc"}) || !sort.at("key").is_string() ||
|
||||
!sort.at("desc").is_boolean() || !sort_key(view, sort.at("key")))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (const Json::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int report_round_count(const Catalog &world, const Json &state, bool other) {
|
||||
if (!valid_demo_reports(state, world))
|
||||
return 1;
|
||||
return other && state.at("competition") != 0
|
||||
? other_rounds : league_rounds(world, state.at("league"));
|
||||
}
|
||||
std::string edit_demo_reports(Json &state, const Json &action, const Catalog &world) {
|
||||
if (!valid_demo_reports(state, world))
|
||||
return "invalid_reports_state";
|
||||
try {
|
||||
if (!action.is_object() || !action.contains("kind") || !action.at("kind").is_string())
|
||||
return "invalid_reports_action";
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
auto next = state;
|
||||
if (kind == "country" || kind == "league") {
|
||||
if (!keys(action, {"kind", "value"}) || !action.at("value").is_string())
|
||||
return "invalid_reports_action";
|
||||
const auto value = action.at("value").get<std::string>();
|
||||
if (kind == "country") {
|
||||
if (!world.find("nations", value))
|
||||
return "invalid_reports_country";
|
||||
const auto leagues = world.leagues(value);
|
||||
if (leagues.empty())
|
||||
return "invalid_reports_country";
|
||||
next["league"] = leagues.front()->at("id");
|
||||
} else {
|
||||
if (value.empty() || !league_exists(world, value))
|
||||
return "invalid_reports_league";
|
||||
next["league"] = value;
|
||||
}
|
||||
next["round"] = std::min(next.at("round").get<int>(),
|
||||
league_rounds(world, next.at("league")) - 1);
|
||||
} else if (kind == "filter" || kind == "competition") {
|
||||
if (!keys(action, {"kind", "value"}) ||
|
||||
!integer(action.at("value"), 0, kind == "filter" ? 4 : 8))
|
||||
return "invalid_reports_action";
|
||||
next[kind] = action.at("value").get<int>();
|
||||
if (kind == "competition")
|
||||
next["round"] = std::min(next.at("round").get<int>(),
|
||||
(next.at("competition") == 0
|
||||
? league_rounds(world, next.at("league"))
|
||||
: other_rounds) - 1);
|
||||
} else if (kind == "round") {
|
||||
if (!keys(action, {"kind", "step"}, {"other"}) ||
|
||||
!integer(action.at("step"), -1, 1) || action.at("step") == 0 ||
|
||||
(action.contains("other") && !action.at("other").is_boolean()))
|
||||
return "invalid_reports_action";
|
||||
const int count = report_round_count(world, next, action.value("other", false));
|
||||
const int current = std::clamp(next.at("round").get<int>(), 0, count - 1);
|
||||
next["round"] = (current + action.at("step").get<int>() + count) % count;
|
||||
} else if (kind == "sort") {
|
||||
if (!keys(action, {"kind", "view", "key"}) ||
|
||||
!action.at("view").is_string() || !action.at("key").is_string())
|
||||
return "invalid_reports_action";
|
||||
const auto view = action.at("view").get<std::string>(), key = action.at("key").get<std::string>();
|
||||
if (!sort_key(view, key))
|
||||
return "invalid_reports_sort";
|
||||
auto &sort = next["sorts"][view];
|
||||
sort["desc"] = sort.at("key") == key ? !sort.at("desc").get<bool>()
|
||||
: key != "club" && key != "rank" && key != "original";
|
||||
sort["key"] = key;
|
||||
} else if (kind == "reset") {
|
||||
if (!keys(action, {"kind"}))
|
||||
return "invalid_reports_action";
|
||||
next["league"] = initial_league(world);
|
||||
next["filter"] = 0;
|
||||
next["sorts"]["tables"] = {{"key", "points"}, {"desc", true}};
|
||||
next["round"] = std::min(next.at("round").get<int>(),
|
||||
league_rounds(world, next.at("league")) - 1);
|
||||
} else {
|
||||
return "unknown_reports_action";
|
||||
}
|
||||
// OpenBMH deviates from the HTML preview by clamping shared round state
|
||||
// when a league/competition changes. The other-competition 34-round range
|
||||
// is a bounded presentation fixture, never an engine scheduling rule.
|
||||
if (!valid_demo_reports(next, world))
|
||||
return "invalid_reports_selection";
|
||||
state = std::move(next);
|
||||
return {};
|
||||
} catch (const Json::exception &) {
|
||||
return "invalid_reports_action";
|
||||
}
|
||||
}
|
||||
Json report_table_rows(const Catalog &world, const Json &state) {
|
||||
if (!valid_demo_reports(state, world))
|
||||
return Json::array();
|
||||
const auto league = state.at("league").get<std::string>();
|
||||
const auto clubs = league.empty() ? std::vector<const Json *>{} : world.clubs(league);
|
||||
std::vector<Json> rows;
|
||||
for (size_t i = 0; i < clubs.size(); ++i)
|
||||
rows.push_back({{"club", clubs[i]->at("id")}, {"original", i}, {"rank", 0},
|
||||
{"played", 0}, {"wins", 0}, {"draw", 0}, {"loss", 0},
|
||||
{"gf", 0}, {"ga", 0}, {"points", 0}, {"diff", 0},
|
||||
{"form", 0}, {"form_text", ""}});
|
||||
const int filter = state.at("filter"), half = league_rounds(world, league) / 2;
|
||||
for (const auto &match : fixtures(clubs.size(), league_index(world, league))) {
|
||||
if (match.round >= played_rounds || (filter == 1 && match.round >= half) ||
|
||||
(filter == 2 && match.round < half))
|
||||
continue;
|
||||
for (int side = 0; side < 2; ++side) {
|
||||
if ((filter == 3 && side != 0) || (filter == 4 && side != 1))
|
||||
continue;
|
||||
auto &row = rows[size_t(side == 0 ? match.home : match.away)];
|
||||
const int gf = side == 0 ? match.home_goals : match.away_goals,
|
||||
ga = side == 0 ? match.away_goals : match.home_goals;
|
||||
row["played"] = row["played"].get<int>() + 1;
|
||||
row["gf"] = row["gf"].get<int>() + gf;
|
||||
row["ga"] = row["ga"].get<int>() + ga;
|
||||
const char form = gf > ga ? 'S' : gf == ga ? 'U' : 'N';
|
||||
const char *metric = form == 'S' ? "wins" : form == 'U' ? "draw" : "loss";
|
||||
row[metric] = row[metric].get<int>() + 1;
|
||||
row["points"] = row["points"].get<int>() + (form == 'S' ? 3 : form == 'U' ? 1 : 0);
|
||||
auto text = row["form_text"].get<std::string>() + form;
|
||||
if (text.size() > 8)
|
||||
text.erase(0, text.size() - 8);
|
||||
row["form_text"] = text;
|
||||
}
|
||||
}
|
||||
for (auto &row : rows) {
|
||||
row["diff"] = row["gf"].get<int>() - row["ga"].get<int>();
|
||||
int form = 0;
|
||||
for (char c : row["form_text"].get<std::string>())
|
||||
form += c == 'S' ? 3 : c == 'U' ? 1 : 0;
|
||||
row["form"] = form;
|
||||
}
|
||||
std::stable_sort(rows.begin(), rows.end(), [](const Json &a, const Json &b) {
|
||||
for (const char *key : {"points", "diff", "gf"})
|
||||
if (a.at(key) != b.at(key)) return a.at(key) > b.at(key);
|
||||
return a.at("original") < b.at("original");
|
||||
});
|
||||
for (size_t i = 0; i < rows.size(); ++i) {
|
||||
rows[i]["rank"] = i + 1;
|
||||
rows[i]["original"] = i; // Gold's stable default-table order, not name order.
|
||||
}
|
||||
return sorted(std::move(rows), state.at("sorts").at("tables"));
|
||||
}
|
||||
Json report_strength_rows(const Catalog &world, const Json &state) {
|
||||
if (!valid_demo_reports(state, world))
|
||||
return Json::array();
|
||||
const auto league = state.at("league").get<std::string>();
|
||||
const auto clubs = league.empty() ? std::vector<const Json *>{} : world.clubs(league);
|
||||
std::vector<Json> rows;
|
||||
for (size_t i = 0; i < clubs.size(); ++i) {
|
||||
const auto id = clubs[i]->at("id").get<std::string>();
|
||||
const auto squad = world.squad(id);
|
||||
rows.push_back({{"club", id}, {"id", id}, {"name", world.name(*clubs[i])},
|
||||
{"original", i}, {"keeper", average(squad, "gk")},
|
||||
{"defence", average(squad, "def")}, {"midfield", average(squad, "mid")},
|
||||
{"attack", average(squad, "fwd")}, {"strength", average(squad, "")}});
|
||||
}
|
||||
return sorted(std::move(rows), state.at("sorts").at("strengths"));
|
||||
}
|
||||
Json report_match_rows(const Catalog &world, const Json &state, bool other) {
|
||||
Json result = Json::array();
|
||||
if (!valid_demo_reports(state, world))
|
||||
return result;
|
||||
const int round = std::clamp(state.at("round").get<int>(), 0,
|
||||
report_round_count(world, state, other) - 1);
|
||||
const auto league = state.at("league").get<std::string>();
|
||||
const auto add = [&](const Json &home, const Json &away, int hg, int ag) {
|
||||
result.push_back({{"home", home}, {"away", away}, {"home_goals", hg},
|
||||
{"away_goals", ag}, {"round", round}});
|
||||
};
|
||||
if (!other || state.at("competition") == 0) {
|
||||
const auto clubs = league.empty() ? std::vector<const Json *>{} : world.clubs(league);
|
||||
for (const auto &match : fixtures(clubs.size(), league_index(world, league)))
|
||||
if (match.round == round)
|
||||
add(clubs[size_t(match.home)]->at("id"), clubs[size_t(match.away)]->at("id"),
|
||||
match.home_goals, match.away_goals);
|
||||
} else {
|
||||
const auto leagues = world.leagues("nation:de");
|
||||
if (leagues.empty()) return result;
|
||||
const auto clubs = world.clubs(leagues.front()->at("id"));
|
||||
if (clubs.size() < 2) return result;
|
||||
const int competition = state.at("competition"), seed = round + competition * 3;
|
||||
for (int i = 0; i < other_pairs[size_t(competition)]; ++i)
|
||||
add(clubs[size_t(i * 2 + seed) % clubs.size()]->at("id"),
|
||||
clubs[size_t(i * 2 + 1 + seed) % clubs.size()]->at("id"),
|
||||
(i + seed) % 4, (i * 3 + seed) % 3);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Json report_opening_pairs(const Catalog &world) {
|
||||
std::vector<const Json *> pool;
|
||||
for (const auto *league : world.leagues("nation:de"))
|
||||
for (const auto *club : world.clubs(league->at("id")))
|
||||
if (pool.size() < 64)
|
||||
pool.push_back(club);
|
||||
Json result = Json::array();
|
||||
for (size_t i = 0; i + 1 < pool.size(); i += 2)
|
||||
result.push_back({{"home", pool[i]->at("id")},
|
||||
{"away", pool[i + 1]->at("id")},
|
||||
{"home_goals", nullptr}, {"away_goals", nullptr},
|
||||
{"round", 0}});
|
||||
return result;
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,25 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/catalog.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
// Deterministic presentation fixtures only: no session, engine, or RNG access.
|
||||
// Each function receives the reports subobject, not the complete DemoSource state.
|
||||
Json demo_reports(const Catalog &);
|
||||
bool valid_demo_reports(const Json &, const Catalog &);
|
||||
// Returns an empty string on success, an explicit error on rejection. Rejections
|
||||
// leave the reports object unchanged. Actions use kind/value, or kind=round with
|
||||
// step (+/-1) and optional other, or kind=sort with view/key.
|
||||
std::string edit_demo_reports(Json &, const Json &action, const Catalog &);
|
||||
// Rows retain stable catalog club IDs. Numeric strength components are null when
|
||||
// the corresponding squad positions have no usable OBW strength information.
|
||||
Json report_table_rows(const Catalog &, const Json &);
|
||||
Json report_strength_rows(const Catalog &, const Json &);
|
||||
Json report_match_rows(const Catalog &, const Json &, bool other);
|
||||
// The presentation-only opening draw: adjacent pairs from at most 64 German
|
||||
// league clubs in catalog order; goals are null until a future runtime owns them.
|
||||
Json report_opening_pairs(const Catalog &);
|
||||
// A count (not a last index), always at least 1. Shared round state is clamped
|
||||
// on selection changes; non-league other previews deliberately use 34 rounds.
|
||||
int report_round_count(const Catalog &, const Json &, bool other = false);
|
||||
} // namespace bmh::modern_ui
|
||||
+34
-2
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/model.hpp"
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
#include "modern/ui/demo_reports.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include <algorithm>
|
||||
@@ -72,6 +73,7 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
{"camp",
|
||||
{{"selected", "resort:4"}, {"confirmed", ""}, {"focus", {5, 4, 6}}}},
|
||||
{"opening", "complete"},
|
||||
{"opening_created", false},
|
||||
{"preferences",
|
||||
{{"theme", "midnight"}, {"volume", 80}, {"effects", true}}},
|
||||
{"edits", Json::object()}};
|
||||
@@ -80,6 +82,7 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
if (!valid_date(state_.at("date")))
|
||||
throw editor::Error("invalid_demo_date");
|
||||
state_["calendar"] = demo_calendar(*catalog_, state_.at("date"));
|
||||
state_["reports"] = demo_reports(*catalog_);
|
||||
saved_ = state_;
|
||||
}
|
||||
Snapshot DemoSource::snapshot() const { return {revision_, catalog_, state_}; }
|
||||
@@ -129,6 +132,18 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
*catalog_);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
if (action.value.value("kind", "") == "league") {
|
||||
const auto report_error = edit_demo_reports(next["reports"],
|
||||
{{"kind", "league"}, {"value", next["calendar"]["league"]}}, *catalog_);
|
||||
if (!report_error.empty())
|
||||
return {false, revision_, report_error};
|
||||
}
|
||||
} else if (action.kind == "reports") {
|
||||
const auto error = edit_demo_reports(next["reports"], action.value,
|
||||
*catalog_);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
next["calendar"]["league"] = next["reports"]["league"];
|
||||
} else if (action.kind == "camp.select") {
|
||||
if (!camp_id(action.value))
|
||||
return {false, revision_, "camp_unknown"};
|
||||
@@ -142,9 +157,13 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
next["camp"]["focus"] = action.value;
|
||||
} else if (action.kind == "camp.remember")
|
||||
next["camp"]["confirmed"] = next["camp"]["selected"];
|
||||
else if (action.kind == "opening")
|
||||
else if (action.kind == "opening") {
|
||||
if (!action.value.is_string())
|
||||
return {false, revision_, "invalid_opening_state"};
|
||||
next["opening"] = action.value;
|
||||
else if (action.kind == "preferences") {
|
||||
if (action.value == "complete")
|
||||
next["opening_created"] = true;
|
||||
} else if (action.kind == "preferences") {
|
||||
if (!preferences(action.value))
|
||||
return {false, revision_, "invalid_preferences"};
|
||||
next["preferences"] = action.value;
|
||||
@@ -200,6 +219,19 @@ Json DemoSource::load(const fs::path &file) {
|
||||
data["calendar"] = demo_calendar(*catalog_, data.at("date"));
|
||||
if (!valid_demo_calendar(data.at("calendar"), *catalog_))
|
||||
throw editor::Error("invalid_demo_calendar");
|
||||
if (!data.contains("reports")) {
|
||||
data["reports"] = demo_reports(*catalog_);
|
||||
const auto error = edit_demo_reports(data["reports"],
|
||||
{{"kind", "league"}, {"value", data["calendar"]["league"]}}, *catalog_);
|
||||
if (!error.empty())
|
||||
throw editor::Error(error);
|
||||
}
|
||||
if (!data.contains("opening_created"))
|
||||
data["opening_created"] = false;
|
||||
if (!valid_demo_reports(data.at("reports"), *catalog_) ||
|
||||
!data.at("opening_created").is_boolean())
|
||||
throw editor::Error("invalid_demo_reports");
|
||||
data["calendar"]["league"] = data["reports"]["league"];
|
||||
const auto &focus = data.at("camp").at("focus");
|
||||
if (!focus.is_array() || focus.size() != 3)
|
||||
throw editor::Error("invalid_focus");
|
||||
|
||||
@@ -0,0 +1,292 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
namespace {
|
||||
std::optional<std::array<int, 3>> report_detail_date(const Json &value) {
|
||||
if (!value.is_string())
|
||||
return {};
|
||||
const auto text = value.get<std::string>();
|
||||
if (text.size() != 10 || text[4] != '-' || text[7] != '-')
|
||||
return {};
|
||||
for (size_t i = 0; i < text.size(); ++i)
|
||||
if (i != 4 && i != 7 && (text[i] < '0' || text[i] > '9'))
|
||||
return {};
|
||||
const std::array<int, 3> date = {std::stoi(text.substr(0, 4)),
|
||||
std::stoi(text.substr(5, 2)),
|
||||
std::stoi(text.substr(8, 2))};
|
||||
if (!std::chrono::year_month_day{std::chrono::year{date[0]},
|
||||
std::chrono::month{unsigned(date[1])},
|
||||
std::chrono::day{unsigned(date[2])}}.ok())
|
||||
return {};
|
||||
return date;
|
||||
}
|
||||
std::string report_detail_position(const Json &player) {
|
||||
std::string result;
|
||||
const auto positions = player.find("positions");
|
||||
if (positions == player.end() || !positions->is_array())
|
||||
return "—";
|
||||
for (const auto &position : *positions) {
|
||||
if (!position.is_string())
|
||||
continue;
|
||||
const auto value = position.get<std::string>();
|
||||
if (value.empty())
|
||||
continue;
|
||||
if (!result.empty())
|
||||
result += " / ";
|
||||
result += value == "gk" ? "TW" : value == "def" ? "ABW"
|
||||
: value == "mid" ? "MIT"
|
||||
: value == "fwd" ? "STU" : value;
|
||||
}
|
||||
return result.empty() ? "—" : result;
|
||||
}
|
||||
std::string report_detail_strength(const Json &player) {
|
||||
const auto attributes = player.find("attributes");
|
||||
if (attributes == player.end() || !attributes->is_object())
|
||||
return "—";
|
||||
const auto value = attributes->find("strength");
|
||||
return value != attributes->end() && value->is_number_integer()
|
||||
? number(value->get<int64_t>()) : "—";
|
||||
}
|
||||
std::string report_detail_age(const Json &player, const Json &date,
|
||||
bool with_unit) {
|
||||
const auto current = report_detail_date(date);
|
||||
const auto born = report_detail_date(player.value("born", Json()));
|
||||
if (born && current && *born <= *current) {
|
||||
const int years = (*current)[0] - (*born)[0] -
|
||||
(std::pair{(*current)[1], (*current)[2]} <
|
||||
std::pair{(*born)[1], (*born)[2]});
|
||||
return std::to_string(years) + (with_unit ? " Jahre" : "");
|
||||
}
|
||||
const auto initial = player.find("age_at_start");
|
||||
if (initial != player.end() && initial->is_object() &&
|
||||
initial->contains("years") && (*initial)["years"].is_number_integer()) {
|
||||
const auto on = report_detail_date(initial->value("on", Json()));
|
||||
if (on && (*initial)["years"].get<int>() >= 0) {
|
||||
const auto iso = (*initial)["on"].get<std::string>();
|
||||
// An imported age has no birthday. Keep its known reference date visible.
|
||||
return std::to_string((*initial)["years"].get<int>()) +
|
||||
(with_unit ? " Jahre" : "") + " (" + iso.substr(8, 2) + "." +
|
||||
iso.substr(5, 2) + "." + iso.substr(0, 4) + ")";
|
||||
}
|
||||
}
|
||||
return "—";
|
||||
}
|
||||
std::string report_detail_term(const Catalog &world, const Json &player,
|
||||
const Json &date) {
|
||||
const auto current = report_detail_date(date);
|
||||
if (!current)
|
||||
return "—";
|
||||
for (const auto &contract : world.rows("contracts")) {
|
||||
if (contract.value("person", "") != player.value("id", "") ||
|
||||
contract.value("team", "") != player.value("team", ""))
|
||||
continue;
|
||||
const auto start = report_detail_date(contract.value("start", Json()));
|
||||
const auto end = report_detail_date(contract.value("end", Json()));
|
||||
if (!end || (start && *start > *current))
|
||||
continue;
|
||||
// Remaining complete calendar months; no Classic initializer is executed.
|
||||
int months = ((*end)[0] - (*current)[0]) * 12 + (*end)[1] - (*current)[1];
|
||||
if ((*end)[2] < (*current)[2])
|
||||
--months;
|
||||
return std::to_string(std::max(0, months)) + " Monate";
|
||||
}
|
||||
return "—";
|
||||
}
|
||||
std::string report_detail_fields(
|
||||
const std::vector<std::pair<std::string, std::string>> &fields,
|
||||
int spacing) {
|
||||
std::string result = "<dl class=\"report-detail-values\">";
|
||||
for (size_t i = 0; i < fields.size(); ++i)
|
||||
result += "<div class=\"report-detail-field\" style=\"top:" +
|
||||
std::to_string(i * spacing) + "dp;\"><dt>" +
|
||||
esc(fields[i].first) + "</dt><dd>" + esc(fields[i].second) +
|
||||
"</dd></div>";
|
||||
return result + "</dl>";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string App::render_report_detail() {
|
||||
if (report_detail_kind_.empty() || !world_)
|
||||
return {};
|
||||
const auto snapshot = snap();
|
||||
const auto current_date = snapshot.state.value("date", Json());
|
||||
const auto *club = world_->find("clubs", report_detail_club_);
|
||||
const auto *player = world_->find("players", report_detail_player_);
|
||||
const auto display_name = [&](const Json *row) {
|
||||
if (!row)
|
||||
return std::string("—");
|
||||
const auto name = world_->name(*row);
|
||||
return name.empty() ? std::string("—") : name;
|
||||
};
|
||||
const auto close = [&](const std::string &caption) {
|
||||
return btn("report-detail-close", esc(caption), "report-detail-action",
|
||||
"id=\"report-detail-close\"");
|
||||
};
|
||||
std::string title, body, actions;
|
||||
if (report_detail_kind_ == "club" && club) {
|
||||
title = display_name(club);
|
||||
const Json *stadium = nullptr;
|
||||
if (const auto id = club->find("stadium");
|
||||
id != club->end() && id->is_string())
|
||||
stadium = world_->find("stadiums", id->get<std::string>());
|
||||
if (!stadium)
|
||||
for (const auto &candidate : world_->rows("stadiums"))
|
||||
if (candidate.value("owner", Json()) == report_detail_club_) {
|
||||
stadium = &candidate;
|
||||
break;
|
||||
}
|
||||
std::string capacity = "—", strength = "—", manager_name = "—";
|
||||
if (stadium) {
|
||||
const auto value = stadium->value("capacity", Json());
|
||||
if (value.is_number_integer())
|
||||
capacity = number(value.get<int64_t>());
|
||||
else if (value.is_object() && !value.empty()) {
|
||||
int64_t total = 0;
|
||||
bool valid = true;
|
||||
for (const auto &[area, seats] : value.items()) {
|
||||
if (!seats.is_number_integer() || seats.get<int64_t>() < 0) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
total += seats.get<int64_t>();
|
||||
}
|
||||
if (valid)
|
||||
capacity = number(total);
|
||||
}
|
||||
}
|
||||
if (const auto value = club->find("strength");
|
||||
value != club->end() && value->is_number_integer())
|
||||
strength = number(value->get<int64_t>());
|
||||
else {
|
||||
int64_t sum = 0, count = 0;
|
||||
for (const auto *member : world_->squad(report_detail_club_)) {
|
||||
const auto attributes = member->value("attributes", Json::object());
|
||||
if (attributes.is_object() && attributes.contains("strength") &&
|
||||
attributes["strength"].is_number_integer()) {
|
||||
sum += attributes["strength"].get<int64_t>();
|
||||
++count;
|
||||
}
|
||||
}
|
||||
if (count)
|
||||
strength = number(sum / count);
|
||||
}
|
||||
for (const auto &candidate : world_->rows("staff"))
|
||||
if (candidate.value("role", "") == "manager" &&
|
||||
candidate.value("team", "") == report_detail_club_)
|
||||
manager_name = display_name(&candidate);
|
||||
for (const auto &candidate : snapshot.state.at("managers"))
|
||||
if (candidate.value("club", "") == report_detail_club_)
|
||||
manager_name = candidate.value("name", std::string("—"));
|
||||
if (manager_name.empty())
|
||||
manager_name = "—";
|
||||
body = "<div class=\"report-detail-badge\">" + crest(club, 192) +
|
||||
"</div><p class=\"report-detail-note\">Vereinsprofil</p>" +
|
||||
report_detail_fields({{"Stadion", display_name(stadium)},
|
||||
{"Plätze", capacity}, {"Stärke", strength},
|
||||
{"Manager", manager_name}}, 72);
|
||||
actions = btn("report-club-roster", "Spieler", "report-detail-action",
|
||||
"id=\"report-detail-roster\"") + close("Zurück");
|
||||
} else if (report_detail_kind_ == "roster" && club) {
|
||||
title = display_name(club) + " · Kader";
|
||||
body = "<div class=\"report-detail-roster-scroll\" id=\"report-detail-roster-scroll\">"
|
||||
"<div class=\"report-detail-table\"><div class=\"report-detail-row report-detail-table-head\">";
|
||||
for (const auto &heading : {"Spieler", "Pos.", "Stärke", "Alter"})
|
||||
body += "<div class=\"report-detail-cell\">" + std::string(heading) + "</div>";
|
||||
body += "</div>";
|
||||
const auto squad = world_->squad(report_detail_club_);
|
||||
for (size_t i = 0; i < squad.size(); ++i) {
|
||||
const auto *member = squad[i];
|
||||
body += "<div class=\"report-detail-row " +
|
||||
std::string(i % 2 == 0 ? "report-detail-striped" : "") +
|
||||
"\"><div class=\"report-detail-cell\">" +
|
||||
btn("player-detail", esc(display_name(member)),
|
||||
"report-detail-player-link",
|
||||
"id=\"report-detail-player-" + std::to_string(i) +
|
||||
"\" data-id=\"" + esc(member->value("id", "")) + "\"") +
|
||||
"</div><div class=\"report-detail-cell\">" +
|
||||
esc(report_detail_position(*member)) +
|
||||
"</div><div class=\"report-detail-cell\">" +
|
||||
esc(report_detail_strength(*member)) +
|
||||
"</div><div class=\"report-detail-cell\">" +
|
||||
esc(report_detail_age(*member, current_date, false)) + "</div></div>";
|
||||
}
|
||||
body += "</div>";
|
||||
if (squad.empty())
|
||||
body += "<p class=\"report-detail-empty\">Keine Spieler vorhanden.</p>";
|
||||
body += "</div>";
|
||||
actions = close("Zurück");
|
||||
} else if (report_detail_kind_ == "player" && player) {
|
||||
title = display_name(player);
|
||||
std::string photo = player->value("photo", "");
|
||||
if (photo.empty()) {
|
||||
const auto id = player->value("id", "");
|
||||
// Catalog resolves the world's silhouette fallback for an absent photo key.
|
||||
photo = "asset:player/" + (id.starts_with("player:") ? id.substr(7) : id) +
|
||||
"/photo";
|
||||
}
|
||||
body = "<div class=\"report-detail-portrait\">" +
|
||||
img(photo, 496, 662.667, true, true, 1024) +
|
||||
"</div><p class=\"report-detail-photo-caption\">Porträt</p>" +
|
||||
report_detail_fields({{"Position", report_detail_position(*player)},
|
||||
{"Alter", report_detail_age(*player, current_date, true)},
|
||||
{"Stärke", report_detail_strength(*player)},
|
||||
{"Verein", display_name(club)},
|
||||
{"Vertrag", report_detail_term(*world_, *player, current_date)}}, 73);
|
||||
actions = close("Schließen");
|
||||
} else
|
||||
return {};
|
||||
return "<div id=\"report-detail-layer\" class=\"report-detail-layer\">"
|
||||
"<section id=\"report-detail-dialog\" class=\"report-detail-card report-detail-" +
|
||||
esc(report_detail_kind_) +
|
||||
"\" role=\"dialog\" aria-modal=\"true\" aria-labelledby=\"report-detail-title\">"
|
||||
"<h2 id=\"report-detail-title\">" + esc(title) + "</h2>" + body +
|
||||
"<nav class=\"report-detail-actions\">" + actions + "</nav></section></div>";
|
||||
}
|
||||
|
||||
bool App::report_detail_action(const std::string &action, Rml::Element *target) {
|
||||
if (action == "report-detail-close") {
|
||||
if (report_detail_kind_.empty())
|
||||
return false;
|
||||
const auto restore = report_detail_return_focus_;
|
||||
report_detail_kind_.clear();
|
||||
report_detail_club_.clear();
|
||||
report_detail_player_.clear();
|
||||
report_detail_return_focus_.clear();
|
||||
focus_after_render_ = restore;
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (action != "club-detail" && action != "player-detail" &&
|
||||
action != "report-club-roster")
|
||||
return false;
|
||||
if (!world_ || !target)
|
||||
return true;
|
||||
const auto id = target->GetAttribute<Rml::String>("data-id", "");
|
||||
if (action == "club-detail") {
|
||||
if (!world_->find("clubs", id))
|
||||
return true;
|
||||
if (report_detail_kind_.empty())
|
||||
report_detail_return_focus_ = target->GetId();
|
||||
report_detail_kind_ = "club";
|
||||
report_detail_club_ = id;
|
||||
report_detail_player_.clear();
|
||||
focus_after_render_ = "report-detail-roster";
|
||||
} else if (action == "report-club-roster") {
|
||||
if (!world_->find("clubs", report_detail_club_))
|
||||
return true;
|
||||
report_detail_kind_ = "roster";
|
||||
report_detail_player_.clear();
|
||||
focus_after_render_ = world_->squad(report_detail_club_).empty()
|
||||
? "report-detail-close" : "report-detail-player-0";
|
||||
} else {
|
||||
const auto *player = world_->find("players", id);
|
||||
if (!player)
|
||||
return true;
|
||||
if (report_detail_kind_.empty())
|
||||
report_detail_return_focus_ = target->GetId();
|
||||
report_detail_kind_ = "player";
|
||||
report_detail_player_ = id;
|
||||
report_detail_club_ = player->value("team", "");
|
||||
focus_after_render_ = "report-detail-close";
|
||||
}
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
namespace {
|
||||
bool report_picker_menu(const std::string &menu) {
|
||||
return menu == "tables" || menu == "strengths" || menu == "league" ||
|
||||
menu == "other";
|
||||
}
|
||||
bool report_picker_kind(const std::string &kind) {
|
||||
return kind == "country" || kind == "league" || kind == "filter" ||
|
||||
kind == "competition";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<std::pair<std::string, std::string>>
|
||||
App::report_picker_items(const std::string &kind) const {
|
||||
std::vector<std::pair<std::string, std::string>> items;
|
||||
if (!world_ || !report_picker_kind(kind))
|
||||
return items;
|
||||
if (kind == "filter" || kind == "competition") {
|
||||
const std::vector<std::string> captions =
|
||||
kind == "filter"
|
||||
? std::vector<std::string>{"Alle Spiele", "Hinrunde", "Rückrunde",
|
||||
"Heim", "Auswärts"}
|
||||
: std::vector<std::string>{"Meisterschaft", "Champ. League", "EM",
|
||||
"DFB-Pokal", "Pokalsieger", "Hallenmasters",
|
||||
"UEFA-Cup", "WM", "Super-Cup"};
|
||||
for (size_t i = 0; i < captions.size(); ++i)
|
||||
items.emplace_back(std::to_string(i), captions[i]);
|
||||
return items;
|
||||
}
|
||||
if (kind == "country") {
|
||||
std::set<std::string> seen;
|
||||
for (const auto *league : world_->leagues()) {
|
||||
const auto nation = league->value("nation", "");
|
||||
if (!nation.empty() && seen.insert(nation).second)
|
||||
items.emplace_back(nation, world_->nation_name(nation));
|
||||
}
|
||||
const std::array<std::string, 5> gold_order = {
|
||||
"Deutschland", "Spanien", "Italien", "Frankreich", "England"};
|
||||
const auto rank = [&](const std::string &caption) {
|
||||
return std::find(gold_order.begin(), gold_order.end(), caption) -
|
||||
gold_order.begin();
|
||||
};
|
||||
std::stable_sort(items.begin(), items.end(), [&](const auto &a, const auto &b) {
|
||||
const auto first = rank(a.second), second = rank(b.second);
|
||||
return first != second ? first < second : a.second < b.second;
|
||||
});
|
||||
return items;
|
||||
}
|
||||
const auto reports = snap().state.at("reports");
|
||||
const auto *league = world_->find("competitions", reports.at("league").get<std::string>());
|
||||
if (league)
|
||||
for (const auto *candidate : world_->leagues(league->value("nation", "")))
|
||||
items.emplace_back(candidate->value("id", ""), world_->name(*candidate));
|
||||
return items;
|
||||
}
|
||||
|
||||
std::string App::report_picker_value(const std::string &kind) const {
|
||||
if (!world_ || !report_picker_kind(kind))
|
||||
return {};
|
||||
const auto reports = snap().state.at("reports");
|
||||
if (kind == "filter" || kind == "competition")
|
||||
return std::to_string(reports.at(kind).get<int>());
|
||||
const auto league = reports.at("league").get<std::string>();
|
||||
if (kind == "league")
|
||||
return league;
|
||||
const auto *row = world_->find("competitions", league);
|
||||
return row ? row->value("nation", "") : "";
|
||||
}
|
||||
|
||||
std::string App::report_picker(const std::string &kind, double left,
|
||||
double width) {
|
||||
if (!report_picker_menu(menu_) || !report_picker_kind(kind))
|
||||
return {};
|
||||
const auto items = report_picker_items(kind);
|
||||
const auto selected = report_picker_value(kind);
|
||||
std::string caption = "—";
|
||||
for (const auto &[value, label] : items)
|
||||
if (value == selected)
|
||||
caption = label;
|
||||
const bool open = report_picker_ == kind;
|
||||
const std::string menu_label = kind == "country" ? "Land"
|
||||
: kind == "league" ? "Liga"
|
||||
: kind == "filter" ? "Spiele" : "Wettbewerb";
|
||||
std::string out =
|
||||
"<div class=\"report-picker-wrapper\" data-report-picker=\"" + kind +
|
||||
"\" style=\"left:" + scalar(left) + "dp;width:" + scalar(width) + "dp;\">" +
|
||||
btn("report-picker", "<span class=\"report-picker-label\">" + esc(caption) +
|
||||
"</span><span class=\"report-picker-arrow\" aria-hidden=\"true\">"
|
||||
"<span></span></span>",
|
||||
"report-picker-trigger",
|
||||
"id=\"report-picker-" + kind + "\" data-kind=\"" + kind +
|
||||
"\" aria-haspopup=\"menu\" aria-expanded=\"" +
|
||||
(open ? "true" : "false") +
|
||||
"\" aria-controls=\"report-picker-options-" + kind + "\" " +
|
||||
(items.empty() ? "disabled" : "")) +
|
||||
"<div class=\"report-picker-popup\" id=\"report-picker-options-" + kind +
|
||||
"\" role=\"menu\" aria-label=\"" + menu_label + "\" style=\"display:" +
|
||||
(open ? "block" : "none") + ";\">";
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
const bool checked = items[i].first == selected;
|
||||
const auto marker =
|
||||
"<span class=\"report-picker-check-slot\" aria-hidden=\"true\">" +
|
||||
std::string(checked ? "<span class=\"report-picker-check\"></span>" : "") +
|
||||
"</span>";
|
||||
out += btn("report-choice", marker + "<span class=\"report-picker-option-label\">" +
|
||||
esc(items[i].second) + "</span>",
|
||||
"report-picker-option",
|
||||
"id=\"report-option-" + kind + "-" + std::to_string(i) +
|
||||
"\" data-kind=\"" + kind + "\" data-value=\"" +
|
||||
esc(items[i].first) + "\" data-index=\"" + std::to_string(i) +
|
||||
"\" role=\"menuitemradio\" aria-checked=\"" +
|
||||
(checked ? "true" : "false") + "\" style=\"tab-index:none;\"");
|
||||
}
|
||||
return out + "</div></div>";
|
||||
}
|
||||
|
||||
void App::close_report_picker(bool focus) {
|
||||
if (report_picker_.empty())
|
||||
return;
|
||||
const auto kind = report_picker_;
|
||||
report_picker_.clear();
|
||||
if (auto *panel = element("report-picker-options-" + kind))
|
||||
panel->SetProperty("display", "none");
|
||||
auto *trigger = element("report-picker-" + kind);
|
||||
if (trigger)
|
||||
trigger->SetAttribute("aria-expanded", "false");
|
||||
// Commit visibility before the parent Tab trap examines ancestor visibility.
|
||||
// No node is replaced, so an outside mouse event retains its original target.
|
||||
context_->Update();
|
||||
if (focus && trigger)
|
||||
trigger->Focus(keyboard_navigation_);
|
||||
dirty_ = true;
|
||||
}
|
||||
|
||||
bool App::report_picker_action(const std::string &action, Rml::Element *target) {
|
||||
if (action != "report-picker" && action != "report-choice")
|
||||
return false;
|
||||
if (!report_picker_menu(menu_) || !target)
|
||||
return true;
|
||||
const auto kind = target->GetAttribute<Rml::String>("data-kind", "");
|
||||
if (!report_picker_kind(kind))
|
||||
return true;
|
||||
const auto items = report_picker_items(kind);
|
||||
if (items.empty())
|
||||
return true;
|
||||
if (action == "report-picker") {
|
||||
if (report_picker_ == kind) {
|
||||
close_report_picker(true);
|
||||
return true;
|
||||
}
|
||||
close_report_picker(false);
|
||||
report_picker_ = kind;
|
||||
const auto selected = report_picker_value(kind);
|
||||
const auto found = std::find_if(items.begin(), items.end(), [&](const auto &item) {
|
||||
return item.first == selected;
|
||||
});
|
||||
const auto index = found == items.end() ? 0 : found - items.begin();
|
||||
focus_after_render_ = "report-option-" + kind + "-" + std::to_string(index);
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (report_picker_ != kind)
|
||||
return true;
|
||||
const auto value = target->GetAttribute<Rml::String>("data-value", "");
|
||||
if (std::none_of(items.begin(), items.end(), [&](const auto &item) {
|
||||
return item.first == value;
|
||||
}))
|
||||
return true;
|
||||
const Json selection = kind == "filter" || kind == "competition"
|
||||
? Json(std::stoi(value)) : Json(value);
|
||||
close_report_picker(false);
|
||||
// Commit even an unchanged country: the Gold resets its first league.
|
||||
apply("reports", {{"kind", kind}, {"value", selection}});
|
||||
focus_after_render_ = "report-picker-" + kind;
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool App::report_picker_event(Rml::Event &event) {
|
||||
if (!report_picker_menu(menu_))
|
||||
return false;
|
||||
const auto type = event.GetType();
|
||||
const auto ancestor = [&](const std::string &attribute) -> Rml::Element * {
|
||||
for (auto *node = event.GetTargetElement(); node && node != doc_;
|
||||
node = node->GetParentNode())
|
||||
if (node->HasAttribute(attribute))
|
||||
return node;
|
||||
return nullptr;
|
||||
};
|
||||
if (!report_picker_.empty()) {
|
||||
// Defer the right-button dismissal to mouseup so the same gesture cannot
|
||||
// also fall through to the menu's right-button close handler.
|
||||
if ((type == "mousedown" || type == "mouseup") &&
|
||||
event.GetParameter<int>("button", 0) == 1) {
|
||||
if (type == "mouseup")
|
||||
close_report_picker(true);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (type == "contextmenu") {
|
||||
close_report_picker(true);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (type == "mousedown") {
|
||||
auto *wrapper = ancestor("data-report-picker");
|
||||
if (!wrapper || wrapper->GetAttribute<Rml::String>("data-report-picker", "") !=
|
||||
report_picker_)
|
||||
close_report_picker(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (type != "keydown")
|
||||
return false;
|
||||
const auto key = Rml::Input::KeyIdentifier(
|
||||
event.GetParameter<int>("key_identifier", 0));
|
||||
if (!report_picker_.empty()) {
|
||||
if (key == Rml::Input::KI_ESCAPE) {
|
||||
keyboard_navigation_ = true;
|
||||
close_report_picker(true);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (key == Rml::Input::KI_TAB) {
|
||||
keyboard_navigation_ = true;
|
||||
close_report_picker(true);
|
||||
return false;
|
||||
}
|
||||
if (key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN ||
|
||||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END) {
|
||||
const auto items = report_picker_items(report_picker_);
|
||||
if (items.empty())
|
||||
return false;
|
||||
int current = -1;
|
||||
for (auto *node = context_->GetFocusElement(); node && node != doc_;
|
||||
node = node->GetParentNode())
|
||||
if (node->HasAttribute("data-index") &&
|
||||
node->GetAttribute<Rml::String>("data-kind", "") == report_picker_) {
|
||||
current = node->GetAttribute<int>("data-index", -1);
|
||||
break;
|
||||
}
|
||||
const int count = int(items.size());
|
||||
const int next = key == Rml::Input::KI_HOME ? 0
|
||||
: key == Rml::Input::KI_END ? count - 1
|
||||
: (current + (key == Rml::Input::KI_UP ? -1 : 1) + count) % count;
|
||||
if (auto *node = element("report-option-" + report_picker_ + "-" +
|
||||
std::to_string(next))) {
|
||||
keyboard_navigation_ = true;
|
||||
node->Focus(true);
|
||||
node->ScrollIntoView({Rml::ScrollAlignment::Nearest});
|
||||
dirty_ = true;
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
// Enter/Space use the shared native button click handler, without a draft mutation.
|
||||
return false;
|
||||
}
|
||||
auto *trigger = ancestor("data-kind");
|
||||
if (!trigger || trigger->GetAttribute<Rml::String>("data-action", "") !=
|
||||
"report-picker" ||
|
||||
(key != Rml::Input::KI_UP && key != Rml::Input::KI_DOWN))
|
||||
return false;
|
||||
const auto kind = trigger->GetAttribute<Rml::String>("data-kind", "");
|
||||
keyboard_navigation_ = true;
|
||||
report_picker_action("report-picker", trigger);
|
||||
if (key == Rml::Input::KI_UP && report_picker_ == kind) {
|
||||
const auto items = report_picker_items(kind);
|
||||
if (!items.empty())
|
||||
focus_after_render_ = "report-option-" + kind + "-" +
|
||||
std::to_string(items.size() - 1);
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
namespace {
|
||||
std::string report_number(const Json &value) {
|
||||
return value.is_number_integer() ? number(value.get<int64_t>()) : "—";
|
||||
}
|
||||
std::string report_zone(int rank, int count, int tier) {
|
||||
if (rank > count - 3)
|
||||
return "relegation";
|
||||
if (tier == 1) {
|
||||
if (rank <= 4) return "champions";
|
||||
if (rank == 5) return "europa";
|
||||
if (rank == 6) return "conference";
|
||||
} else {
|
||||
if (rank <= (tier == 4 ? 1 : 2)) return "promotion";
|
||||
if (tier != 4 && rank == 3) return "playoff";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string App::report_club_button(const std::string &id, bool away) {
|
||||
const auto *c = world_->find("clubs", id);
|
||||
if (!c)
|
||||
return "<span>—</span>";
|
||||
const auto name = esc(world_->name(*c));
|
||||
const auto text = "<span class=\"report-club-name\" title=\"" + name +
|
||||
"\">" + name + "</span>";
|
||||
return btn("club-detail", away ? text + crest(c, 42) : crest(c, 42) + text,
|
||||
std::string("report-club-button ") + (away ? "away " : "") +
|
||||
(manager(snap()).value("club", "") == id ? "current" : ""),
|
||||
"id=\"report-club-" + esc(id) + "\" data-id=\"" + esc(id) + "\"");
|
||||
}
|
||||
|
||||
std::string App::render_reports(const std::string &mode) {
|
||||
const auto snapshot = snap();
|
||||
const auto &state = snapshot.state.at("reports");
|
||||
const auto *league = world_->find("competitions", state.value("league", ""));
|
||||
const int tier = league ? league->value("tier", 1) : 1;
|
||||
const bool strengths = mode == "strengths", matches = mode == "league" || mode == "other";
|
||||
const bool opening = mode == "other" && state.value("competition", 0) == 3 &&
|
||||
snapshot.state.value("opening_created", false);
|
||||
const auto rows = matches ? (opening ? report_opening_pairs(*world_)
|
||||
: report_match_rows(*world_, state, mode == "other"))
|
||||
: strengths ? report_strength_rows(*world_, state)
|
||||
: report_table_rows(*world_, state);
|
||||
const std::vector<std::pair<std::string, std::string>> headings =
|
||||
matches ? std::vector<std::pair<std::string, std::string>>{
|
||||
{"", "Heim"}, {"", "Ergebnis"}, {"", "Auswärts"}}
|
||||
: strengths ? std::vector<std::pair<std::string, std::string>>{
|
||||
{"club", "Verein"}, {"keeper", "TW"}, {"defence", "Abwehr"},
|
||||
{"midfield", "Mittelfeld"}, {"attack", "Sturm"}, {"strength", "Stärke"}}
|
||||
: std::vector<std::pair<std::string, std::string>>{
|
||||
{"rank", "Pl. / Verein"}, {"played", "Sp."}, {"wins", "S"},
|
||||
{"draw", "U"}, {"loss", "N"}, {"form", "Letzte 8"},
|
||||
{"points", "Punkte"}, {"gf", "Tore"}, {"diff", "Diff."}};
|
||||
std::vector<double> widths;
|
||||
if (matches)
|
||||
widths = {1104, 400, 1104}; // Match card has two 2dp borders.
|
||||
else if (strengths) {
|
||||
widths.push_back(2612 * .38);
|
||||
for (int i = 0; i < 5; ++i) widths.push_back(2612 * .62 / 5);
|
||||
} else {
|
||||
for (int i = 0; i < 9; ++i)
|
||||
widths.push_back(i == 0 ? 2612 * .32 : i == 5 ? 2612 * .14 : 2612 * .54 / 7);
|
||||
}
|
||||
const double heading_height = matches ? 80 : strengths ? (rows.size() >= 20 ? 64 : 66)
|
||||
: rows.size() >= 20 ? 64 : 80;
|
||||
const double row_height = matches ? 118 : strengths ? (rows.size() >= 20 ? 64 : 66)
|
||||
: rows.size() >= 20 ? (1682 - heading_height) / rows.size() : 80;
|
||||
std::string body = "<div id=\"report-underlay\" class=\"report-underlay\">";
|
||||
if (matches) {
|
||||
const int round = std::clamp(state.value("round", 0), 0,
|
||||
report_round_count(*world_, state, mode == "other") - 1);
|
||||
std::string subtitle;
|
||||
if (mode == "league" || state.value("competition", 0) == 0)
|
||||
subtitle = (league ? world_->nation_name(league->value("nation", "")) : "") +
|
||||
" · " + (league ? world_->name(*league) : "");
|
||||
else {
|
||||
const auto choices = report_picker_items("competition");
|
||||
subtitle = choices.at(size_t(state.value("competition", 0))).second;
|
||||
}
|
||||
body += "<header class=\"report-match-heading\"><h2>" +
|
||||
(opening ? std::string("1. Runde") : std::to_string(round + 1) + ". Spieltag") +
|
||||
"</h2><p>" + esc(subtitle) + "</p></header>";
|
||||
}
|
||||
body += "<div id=\"report-list\" class=\"report-list " +
|
||||
std::string(matches ? "match-list" : strengths ? "strength-list" : "standings-list") +
|
||||
"\"><div class=\"report-table\" role=\"table\"><div class=\"report-row report-head\" "
|
||||
"role=\"row\" style=\"height:" + scalar(heading_height) + "dp;\">";
|
||||
for (size_t column = 0; column < headings.size(); ++column) {
|
||||
const auto &[key, label] = headings[column];
|
||||
const auto sorting = matches ? Json::object() : state.at("sorts").at(mode);
|
||||
const bool current = !matches && sorting.value("key", "") == key;
|
||||
const bool descending = sorting.value("desc", false);
|
||||
body += "<div class=\"report-cell " + std::string(column == 0 ? "first" : "") +
|
||||
(matches && column == 1 ? " score" : "") + "\" role=\"columnheader\" "
|
||||
"aria-sort=\"" + (current ? descending ? "descending" : "ascending" : "none") +
|
||||
"\" style=\"width:" + scalar(widths[column]) + "dp;\">";
|
||||
if (matches)
|
||||
body += "<span class=\"report-value\">" + esc(label) + "</span>";
|
||||
else {
|
||||
std::string indicator = "<span class=\"report-sort-symbol\">";
|
||||
if (!current || !descending)
|
||||
indicator += "<span class=\"report-sort-arrow up\"></span>";
|
||||
if (!current || descending)
|
||||
indicator += "<span class=\"report-sort-arrow down\"></span>";
|
||||
indicator += "</span>";
|
||||
body += btn("report-sort", "<span>" + esc(label) + "</span>" + indicator,
|
||||
"report-sort", "id=\"report-sort-" + key + "\" data-id=\"" + key +
|
||||
"\" aria-label=\"Nach " + esc(label) + " sortieren\"");
|
||||
}
|
||||
body += "</div>";
|
||||
}
|
||||
body += "</div>";
|
||||
for (size_t i = 0; i < rows.size(); ++i) {
|
||||
const auto &row = rows[i];
|
||||
body += "<div class=\"report-row " + std::string(i % 2 == 0 ? "striped" : "") +
|
||||
"\" role=\"row\" data-club=\"" + esc(row.value("club", "")) +
|
||||
"\" style=\"height:" + scalar(row_height) + "dp;\">";
|
||||
for (size_t column = 0; column < headings.size(); ++column) {
|
||||
body += "<div class=\"report-cell " + std::string(column == 0 ? "first" : "") +
|
||||
(matches && column == 1 ? " score" : "") +
|
||||
(!matches && !strengths && column == 5 ? " form" : "") +
|
||||
"\" role=\"cell\" style=\"width:" + scalar(widths[column]) + "dp;\">";
|
||||
if (matches && column != 1)
|
||||
body += report_club_button(row.at(column == 0 ? "home" : "away").get<std::string>(), column == 2);
|
||||
else if (!matches && column == 0) {
|
||||
if (!strengths) {
|
||||
const auto zone = report_zone(row.value("rank", 0), int(rows.size()), tier);
|
||||
body += "<span class=\"report-zone " + zone + "\"></span><span class=\"report-rank\"><span>" +
|
||||
std::to_string(row.value("rank", 0)) + ".</span></span>";
|
||||
}
|
||||
body += report_club_button(row.at("club").get<std::string>());
|
||||
} else {
|
||||
std::string value;
|
||||
if (matches)
|
||||
value = row.at("home_goals").is_null() || row.at("away_goals").is_null()
|
||||
? "–" : report_number(row["home_goals"]) + " : " + report_number(row["away_goals"]);
|
||||
else if (!strengths && column == 5)
|
||||
value = row.value("form_text", "");
|
||||
else if (!strengths && column == 7)
|
||||
value = report_number(row["gf"]) + ":" + report_number(row["ga"]);
|
||||
else
|
||||
value = report_number(row.at(headings[column].first));
|
||||
body += "<span class=\"report-value\">" + esc(value) + "</span>";
|
||||
}
|
||||
body += "</div>";
|
||||
}
|
||||
body += "</div>";
|
||||
}
|
||||
body += "</div></div>";
|
||||
if (!matches && !strengths) {
|
||||
const auto legend = tier == 1
|
||||
? std::vector<std::pair<std::string, std::string>>{
|
||||
{"champions", "Champions League"}, {"europa", "Europa League"},
|
||||
{"conference", "Conference League"}, {"relegation", "Abstieg"}}
|
||||
: tier == 4 ? std::vector<std::pair<std::string, std::string>>{
|
||||
{"promotion", "Aufstieg"}, {"relegation", "Abstieg"}}
|
||||
: std::vector<std::pair<std::string, std::string>>{
|
||||
{"promotion", "Aufstieg"}, {"playoff", "Relegation"}, {"relegation", "Abstieg"}};
|
||||
body += "<div class=\"report-legend\">";
|
||||
for (const auto &[kind, label] : legend)
|
||||
body += "<span class=\"report-legend-item\"><span class=\"report-zone " + kind +
|
||||
"\"></span><span>" + esc(label) + "</span></span>";
|
||||
body += "</div>";
|
||||
}
|
||||
body += "<nav class=\"report-footer\">";
|
||||
if (matches) {
|
||||
for (int i = 0; i < 2; ++i)
|
||||
body += btn("report-round", i ? "›" : "‹", "report-round",
|
||||
"id=\"report-round-" + std::string(i ? "next" : "previous") +
|
||||
"\" data-id=\"" + (i ? "1" : "-1") + "\" style=\"" +
|
||||
rect(i * 130, 0, 110, 94) + "\" " + (opening ? "disabled" : ""));
|
||||
body += mode == "league" ? report_picker("country", 260, 1060) + report_picker("league", 1340, 1272)
|
||||
: report_picker("competition", 260, 2352);
|
||||
} else if (strengths)
|
||||
body += report_picker("country", 0, 1294) + report_picker("league", 1318, 1294);
|
||||
else
|
||||
body += btn("report-reset", "Gesamt", "report-reset", "id=\"report-reset\" style=\"" +
|
||||
rect(0, 0, 635, 94) + "\"") +
|
||||
report_picker("filter", 659, 635) + report_picker("country", 1318, 635) +
|
||||
report_picker("league", 1977, 635);
|
||||
return body + "</nav></div>" + render_report_detail();
|
||||
}
|
||||
@@ -635,84 +635,6 @@ std::string App::render_opening() {
|
||||
return window(opening_page_ < 3 ? "Deutsche Teams" : "DFB-Pokal", body,
|
||||
false);
|
||||
}
|
||||
std::string App::render_table(bool strengths, bool indoor) {
|
||||
auto leagues = world_->leagues(country_);
|
||||
if (std::none_of(leagues.begin(), leagues.end(),
|
||||
[&](auto *r) { return r->value("id", "") == league_; }))
|
||||
league_ = leagues.empty() ? "" : leagues.front()->value("id", "");
|
||||
const auto clubs = world_->clubs(league_);
|
||||
std::string body = "<div class=\"list\" "
|
||||
"style=\"position:absolute;left:0;right:0;top:0;bottom:"
|
||||
"160dp;\"><div class=\"table\"><div class=\"tr\"><div "
|
||||
"class=\"th\" style=\"width:42%;\">" +
|
||||
btn("table-sort", "Pl. / Verein", {}, "data-id=\"rank\"") +
|
||||
"</div>";
|
||||
const std::vector<std::string> headers =
|
||||
strengths ? std::vector<std::string>{"Tor", "Abwehr", "Mittelfeld",
|
||||
"Sturm", "Gesamt"}
|
||||
: std::vector<std::string>{"Sp.", "S", "U", "N",
|
||||
"Punkte", "Tore", "Diff."};
|
||||
for (const auto &h : headers)
|
||||
body += "<div class=\"th center\">" +
|
||||
btn("table-sort", esc(h), {}, "data-id=\"" + esc(h) + "\"") +
|
||||
"</div>";
|
||||
body += "</div>";
|
||||
for (size_t i = 0; i < clubs.size(); ++i) {
|
||||
auto *c = clubs[i];
|
||||
body += "<div class=\"tr " + std::string(i % 2 ? "" : "odd") +
|
||||
"\"><div class=\"td\">" +
|
||||
btn("club-detail",
|
||||
"<span class=\"club-label\"><span class=\"rank\">" +
|
||||
std::to_string(i + 1) + ".</span>" + crest(c, 40) +
|
||||
"<span>" + esc(world_->name(*c)) + "</span></span>",
|
||||
{}, "data-id=\"" + esc(c->value("id", "")) + "\"") +
|
||||
"</div>";
|
||||
if (strengths) {
|
||||
const auto players = world_->squad(c->value("id", ""));
|
||||
for (const auto &position : {"gk", "def", "mid", "fwd", "all"}) {
|
||||
int sum = 0, n = 0;
|
||||
for (auto *p : players) {
|
||||
const auto positions = p->value("positions", Json::array());
|
||||
if (std::string(position) == "all" ||
|
||||
std::find(positions.begin(), positions.end(), position) !=
|
||||
positions.end()) {
|
||||
sum += p->value("attributes", Json::object()).value("strength", 0);
|
||||
++n;
|
||||
}
|
||||
}
|
||||
body += "<div class=\"td center\">" +
|
||||
(n ? std::to_string(sum / n) : "–") + "</div>";
|
||||
}
|
||||
} else
|
||||
for (int n = 0; n < 7; ++n)
|
||||
body += "<div class=\"td center\">" +
|
||||
std::string(n == 5 ? "0:0" : "0") + "</div>";
|
||||
body += "</div>";
|
||||
}
|
||||
body += "</div></div><div class=\"bottom-controls\">";
|
||||
if (!strengths && !indoor)
|
||||
body += btn("table-overall", "Gesamt") + select("table-scope",
|
||||
{{"all", "Alle Spiele"},
|
||||
{"first", "Hinrunde"},
|
||||
{"second", "Rückrunde"},
|
||||
{"home", "Heim"},
|
||||
{"away", "Auswärts"}},
|
||||
selections_["table-scope"],
|
||||
"table-scope");
|
||||
std::vector<std::pair<std::string, std::string>> countries, league_rows;
|
||||
std::set<std::string> seen;
|
||||
for (auto *l : world_->leagues()) {
|
||||
const auto id = l->value("nation", "");
|
||||
if (seen.insert(id).second)
|
||||
countries.emplace_back(id, world_->nation_name(id));
|
||||
if (id == country_)
|
||||
league_rows.emplace_back(l->value("id", ""), world_->name(*l));
|
||||
}
|
||||
body += select("table-country", countries, country_, "table-country") +
|
||||
select("table-league", league_rows, league_, "table-league") +
|
||||
"</div>";
|
||||
return body;
|
||||
}
|
||||
std::string App::render_camp() {
|
||||
const auto index_file =
|
||||
Json::parse(editor::read_file(gui_.root() / "gui-assets.json"));
|
||||
@@ -961,8 +883,8 @@ std::string App::render_save(bool save) {
|
||||
}
|
||||
std::string App::render_menu(const std::string &key) {
|
||||
std::string body;
|
||||
if (key == "tables" || key == "strengths")
|
||||
body = render_table(key == "strengths", false);
|
||||
if (key == "tables" || key == "strengths" || key == "league" || key == "other")
|
||||
body = render_reports(key);
|
||||
else if (key == "training")
|
||||
body = render_training();
|
||||
else if (key == "tactics")
|
||||
|
||||
+45
-1
@@ -6,6 +6,7 @@
|
||||
#include "launch/rml_video.hpp"
|
||||
#include "modern/ui/controls.hpp"
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
#include "modern/ui/demo_reports.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include "modern/ui/gui_assets.hpp"
|
||||
@@ -27,6 +28,7 @@
|
||||
#include <iomanip>
|
||||
#include <nfd.hpp>
|
||||
#include <optional>
|
||||
#include <RmlUi/Core/ScrollTypes.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
@@ -304,6 +306,10 @@ class App final : public Rml::EventListener {
|
||||
bool keyboard_navigation_ = false;
|
||||
std::string last_rendered_menu_;
|
||||
std::optional<float> training_scroll_;
|
||||
std::optional<float> report_scroll_;
|
||||
std::string last_report_state_, report_picker_, report_detail_kind_,
|
||||
report_detail_club_, report_detail_player_, report_detail_return_focus_,
|
||||
report_option_to_scroll_;
|
||||
float device_ratio_ = 1.f;
|
||||
Json desktop_ = Json::object(), saved_desktop_ = Json::object(),
|
||||
options_ = Json::object();
|
||||
@@ -595,7 +601,17 @@ class App final : public Rml::EventListener {
|
||||
std::string render_desktop();
|
||||
void init_desktop();
|
||||
std::string widget(const std::string &, const Json &);
|
||||
std::string render_table(bool strengths = false, bool indoor = false);
|
||||
std::string render_reports(const std::string &);
|
||||
std::string report_club_button(const std::string &, bool away = false);
|
||||
std::string render_report_detail();
|
||||
bool report_detail_action(const std::string &, Rml::Element *);
|
||||
std::string report_picker(const std::string &, double, double);
|
||||
std::vector<std::pair<std::string, std::string>>
|
||||
report_picker_items(const std::string &) const;
|
||||
std::string report_picker_value(const std::string &) const;
|
||||
bool report_picker_event(Rml::Event &);
|
||||
bool report_picker_action(const std::string &, Rml::Element *);
|
||||
void close_report_picker(bool focus = false);
|
||||
std::string render_camp();
|
||||
std::string render_training();
|
||||
std::string render_tactics();
|
||||
@@ -663,6 +679,11 @@ class App final : public Rml::EventListener {
|
||||
if (menu_ == "training" && last_rendered_menu_ == "training")
|
||||
if (auto *roster = element("training-roster"))
|
||||
training_scroll_ = roster->GetScrollTop();
|
||||
report_scroll_.reset();
|
||||
const auto report_state = source_ ? snap().state.at("reports").dump() : "";
|
||||
if (menu_ == last_rendered_menu_ && report_state == last_report_state_)
|
||||
if (auto *list = element("report-list"))
|
||||
report_scroll_ = list->GetScrollTop();
|
||||
if (ticket_)
|
||||
ticket_->close(false);
|
||||
context_open_ = false;
|
||||
@@ -684,6 +705,13 @@ class App final : public Rml::EventListener {
|
||||
if (!menu_.empty())
|
||||
markup += render_menu(menu_);
|
||||
element("canvas")->SetInnerRML(markup);
|
||||
if (!report_detail_kind_.empty()) {
|
||||
Rml::ElementList behind;
|
||||
doc_->QuerySelectorAll(behind,
|
||||
"#report-underlay button,#report-underlay input,#report-underlay select,.window .titlebar button");
|
||||
for (auto *node : behind)
|
||||
node->SetProperty("pointer-events", "none");
|
||||
}
|
||||
const auto size = context_->GetDimensions();
|
||||
const auto background =
|
||||
img("background", size.x / scale_, size.y / scale_, false, true, 2048);
|
||||
@@ -697,6 +725,7 @@ class App final : public Rml::EventListener {
|
||||
: "saturate(0.34) brightness(0.70)");
|
||||
needs_render_ = false;
|
||||
last_rendered_menu_ = menu_;
|
||||
last_report_state_ = report_state;
|
||||
scale_chalk_shadow();
|
||||
dirty_ = true;
|
||||
if (focus_ticket_) {
|
||||
@@ -707,6 +736,8 @@ class App final : public Rml::EventListener {
|
||||
if (!focus_after_render_.empty()) {
|
||||
if (auto *node = element(focus_after_render_))
|
||||
node->Focus(keyboard_navigation_);
|
||||
if (focus_after_render_.starts_with("report-option-"))
|
||||
report_option_to_scroll_ = focus_after_render_;
|
||||
focus_after_render_.clear();
|
||||
}
|
||||
}
|
||||
@@ -756,6 +787,16 @@ class App final : public Rml::EventListener {
|
||||
roster->SetScrollTop(*training_scroll_);
|
||||
training_scroll_.reset();
|
||||
}
|
||||
if (report_scroll_) {
|
||||
if (auto *list = element("report-list"))
|
||||
list->SetScrollTop(*report_scroll_);
|
||||
report_scroll_.reset();
|
||||
}
|
||||
if (!report_option_to_scroll_.empty()) {
|
||||
if (auto *node = element(report_option_to_scroll_))
|
||||
node->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
|
||||
report_option_to_scroll_.clear();
|
||||
}
|
||||
renderer_.Clear();
|
||||
renderer_.BeginFrame();
|
||||
context_->Render();
|
||||
@@ -995,6 +1036,9 @@ public:
|
||||
#include "modern/ui/screens.inc"
|
||||
#include "modern/ui/sports_screens.inc"
|
||||
#include "modern/ui/calendar_screen.inc"
|
||||
#include "modern/ui/report_pickers.inc"
|
||||
#include "modern/ui/report_details.inc"
|
||||
#include "modern/ui/reports_screens.inc"
|
||||
} // namespace
|
||||
bool is_modern_world(const fs::path &file) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user