Port statistics and the news tablet to the Modern UI (by Codex)
This commit is contained in:
1 parent
349c416f3a
commit
a45217c362
32 files changed
+2493
-23
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 demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.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 demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.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 reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss)
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss statistics.rcss news.rcss)
|
||||
configure_file("${CMAKE_SOURCE_DIR}/assets/modern/${asset}" "${modern_assets}/${asset}" COPYONLY)
|
||||
endforeach()
|
||||
|
||||
@@ -34,7 +34,7 @@ foreach(icon ${modern_icons})
|
||||
get_filename_component(icon_name "${icon}" NAME)
|
||||
configure_file("${icon}" "${modern_assets}/icons/${icon_name}" COPYONLY)
|
||||
endforeach()
|
||||
foreach(weight Regular Bold)
|
||||
foreach(weight Regular Bold Italic BoldItalic)
|
||||
configure_file("${rmlui_SOURCE_DIR}/Samples/assets/LatoLatin-${weight}.ttf"
|
||||
"${modern_assets}/LatoLatin-${weight}.ttf" COPYONLY)
|
||||
endforeach()
|
||||
@@ -99,3 +99,12 @@ foreach(component environment advertising toto lineup transfer stadium)
|
||||
endif()
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
|
||||
endforeach()
|
||||
|
||||
foreach(component statistics news)
|
||||
add_executable(modern_${component}_ui_tests ../../../tests/modern_${component}_ui_tests.cpp)
|
||||
target_link_libraries(modern_${component}_ui_tests PRIVATE bmh_modern_ui_core)
|
||||
if(MSVC)
|
||||
target_compile_options(modern_${component}_ui_tests PRIVATE /utf-8)
|
||||
endif()
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
|
||||
endforeach()
|
||||
@@ -133,7 +133,7 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
const auto type = event.GetType();
|
||||
// Native widgets can emit intermediate selected values while their options
|
||||
// are being constructed. Opening a squad menu must stay read-only.
|
||||
if (setting_menu_markup_ && (menu_ == "lineup" || menu_ == "transfer") &&
|
||||
if (setting_menu_markup_ && (menu_ == "lineup" || menu_ == "transfer" || menu_ == "news") &&
|
||||
(type == "change" || type == "input")) return;
|
||||
if (type == "keydown")
|
||||
keyboard_navigation_ = true;
|
||||
@@ -168,6 +168,7 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
if (report_detail_kind_.empty() && stadium_event(event)) return;
|
||||
if (popup_.empty() && contracts_event(event)) return;
|
||||
if (popup_.empty() && (advertising_event(event) || toto_event(event))) return;
|
||||
if (popup_.empty() && (statistics_event(event) || news_event(event))) return;
|
||||
if (popup_.empty() && (bank_event(event) || finance_event(event))) return;
|
||||
if (!planning_dialog_.empty() && popup_.empty()) {
|
||||
const bool key_event = type == "keydown";
|
||||
@@ -460,6 +461,7 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
|
||||
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
|
||||
if (lineup_action(action, target) || transfer_action(action, target)) return;
|
||||
if (career_report_action(action, target) || rankings_action(action, target)) return;
|
||||
if (statistics_action(action, target) || dispatch_news(action, target)) return;
|
||||
if (stadium_action(action, target)) return;
|
||||
if (environment_action(action, target) || advertising_action(action, target) || toto_action(action, target)) return;
|
||||
if (amount_action(action, target) || bank_action(action, target) || finance_action(action, target) || contracts_action(action, target)) return;
|
||||
@@ -1074,7 +1076,7 @@ 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", "league", "other", "postpone", "friendlies", "indoor", "almanac", "bank", "balance", "contracts", "environment", "advertising", "toto", "lineup", "transfer", "achievements", "scorers", "fairplay", "highscore", "stadium"};
|
||||
"save", "load", "training", "tactics", "calendar", "league", "other", "postpone", "friendlies", "indoor", "almanac", "bank", "balance", "contracts", "environment", "advertising", "toto", "lineup", "transfer", "achievements", "scorers", "fairplay", "highscore", "stadium", "statistics", "news"};
|
||||
if (!available.contains(route)) {
|
||||
message("Diese native Ansicht wird im nächsten Menüpaket übertragen.");
|
||||
return;
|
||||
@@ -1467,6 +1469,11 @@ void App::script() {
|
||||
{"transfer_view", transfer_view_}, {"transfer_dialog", transfer_dialog_}, {"transfer_draft", transfer_draft_},
|
||||
{"career_view", career_view_}, {"rankings_view", rankings_view_},
|
||||
{"stadium_view", stadium_view_},
|
||||
{"statistics_view", statistics_view_}, {"statistics_trophy", statistics_trophy_},
|
||||
{"statistics_scrolls", statistics_scrolls_},
|
||||
{"news_view", news_view_}, {"news_dialog", news_dialog_},
|
||||
{"news_undo", news_undo_}, {"news_scroll", element("news-feed") ? element("news-feed")->GetScrollTop() : news_scroll_},
|
||||
{"focus", context_->GetFocusElement() ? context_->GetFocusElement()->GetId() : ""},
|
||||
{"contracts_view", {{"dialog", contracts_dialog_}, {"player", contracts_player_}, {"tab", contracts_tab_}, {"draft", contract_draft_}}},
|
||||
{"amount_editor", amount_ ? Json{{"title", amount_->request.title}, {"raw", amount_->raw}, {"review", amount_->review}, {"error", amount_error()}} : Json()},
|
||||
{"source_revision", source_ ? Json(snap().revision) : Json()},
|
||||
|
||||
@@ -0,0 +1,366 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_news.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
bool news_keys(const Json &v, std::initializer_list<const char *> expected) {
|
||||
return v.is_object() && v.size() == expected.size() &&
|
||||
std::all_of(expected.begin(), expected.end(),
|
||||
[&](auto *key) { return v.contains(key); });
|
||||
}
|
||||
bool news_text(const Json &v, size_t cap, bool nonempty = false) {
|
||||
if (!v.is_string())
|
||||
return false;
|
||||
const auto &s = v.get_ref<const std::string &>();
|
||||
return s.size() <= cap && s.find('\0') == s.npos &&
|
||||
(!nonempty || s.find_first_not_of(" \t\r\n") != s.npos);
|
||||
}
|
||||
bool news_id(const Json &v) {
|
||||
if (!news_text(v, 64, true))
|
||||
return false;
|
||||
const auto &s = v.get_ref<const std::string &>();
|
||||
return s.find_first_not_of("abcdefghijklmnopqrstuvwxyz0123456789-") == s.npos;
|
||||
}
|
||||
bool news_folder(const Json &v, bool trash = true) {
|
||||
return v == "inbox" || v == "archive" || (trash && v == "trash");
|
||||
}
|
||||
std::optional<std::chrono::sys_days> news_date(const std::string &value) {
|
||||
using namespace std::chrono;
|
||||
if (value.size() != 10 || value[4] != '-' || value[7] != '-')
|
||||
return {};
|
||||
for (size_t i = 0; i < value.size(); ++i)
|
||||
if (i != 4 && i != 7 && (value[i] < '0' || value[i] > '9'))
|
||||
return {};
|
||||
const int y = std::stoi(value.substr(0, 4)),
|
||||
m = std::stoi(value.substr(5, 2)),
|
||||
d = std::stoi(value.substr(8, 2));
|
||||
const year_month_day date{year{y}, month{unsigned(m)}, day{unsigned(d)}};
|
||||
if (y < 1 || y > 9999 || !date.ok())
|
||||
return {};
|
||||
return sys_days{date};
|
||||
}
|
||||
std::string news_iso(std::chrono::sys_days date) {
|
||||
const std::chrono::year_month_day d{date};
|
||||
std::ostringstream out;
|
||||
out << std::setfill('0') << std::setw(4) << int(d.year()) << '-'
|
||||
<< std::setw(2) << unsigned(d.month()) << '-' << std::setw(2)
|
||||
<< unsigned(d.day());
|
||||
return out.str();
|
||||
}
|
||||
std::string news_category(const std::string &kind) {
|
||||
if (kind == "club")
|
||||
return "Verein";
|
||||
if (kind == "fixture")
|
||||
return "Spielbetrieb";
|
||||
if (kind == "transfer")
|
||||
return "Personal";
|
||||
if (kind == "finance")
|
||||
return "Finanzen";
|
||||
if (kind == "training")
|
||||
return "Training";
|
||||
if (kind == "stadium")
|
||||
return "Stadion";
|
||||
return {};
|
||||
}
|
||||
std::string news_replace(std::string text, const std::string &key,
|
||||
const std::string &value) {
|
||||
size_t at = 0;
|
||||
while ((at = text.find(key, at)) != text.npos) {
|
||||
text.replace(at, key.size(), value);
|
||||
at += value.size();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
std::string news_fold(std::string text) {
|
||||
for (auto &ch : text)
|
||||
if (static_cast<unsigned char>(ch) < 128)
|
||||
ch = char(std::tolower(static_cast<unsigned char>(ch)));
|
||||
for (const auto &[a, b] :
|
||||
std::array<std::pair<const char *, const char *>, 4>{
|
||||
{{"Ä", "ä"}, {"Ö", "ö"}, {"Ü", "ü"}, {"ẞ", "ß"}}})
|
||||
text = news_replace(std::move(text), a, b);
|
||||
return text;
|
||||
}
|
||||
Json news_pair(const Json &item) {
|
||||
return {{"read", item.at("read")}, {"folder", item.at("folder")}};
|
||||
}
|
||||
bool news_valid_pair(const Json &v) {
|
||||
return news_keys(v, {"read", "folder"}) && v.at("read").is_boolean() &&
|
||||
news_folder(v.at("folder"));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string news_display_date(const std::string &s) {
|
||||
return news_date(s)
|
||||
? s.substr(8, 2) + "." + s.substr(5, 2) + "." + s.substr(0, 4)
|
||||
: s;
|
||||
}
|
||||
Json demo_news(const Catalog &world, const Json &state) {
|
||||
const auto value =
|
||||
state.value("date", world.manifest().value("start_date", std::string()));
|
||||
const auto today = news_date(value);
|
||||
if (!today)
|
||||
throw editor::Error("invalid_news_date");
|
||||
struct Fixture {
|
||||
const char *id;
|
||||
int ago;
|
||||
const char *kind, *title, *body;
|
||||
bool read;
|
||||
const char *folder;
|
||||
};
|
||||
const std::array<Fixture, 12> fixtures{
|
||||
{{"season", 0, "club", "Die neue Saison kann kommen",
|
||||
"Willkommen zurück, {{manager}}. {{club}} startet in die "
|
||||
"Saisonvorbereitung. Mannschaft, Verein und Fans blicken voller "
|
||||
"Vorfreude auf die neue Spielzeit.",
|
||||
false, "inbox"},
|
||||
{"cup", 0, "fixture", "Der Pokal schreibt seine eigenen Geschichten",
|
||||
"64 Mannschaften, 32 Paarungen und ein gemeinsames Ziel: das Finale. "
|
||||
"Die erste Runde eröffnet die neue Pokalsaison.",
|
||||
false, "inbox"},
|
||||
{"contract", 1, "transfer", "Vertragsgespräche stehen an",
|
||||
"Nico Brandt möchte seine Zukunft frühzeitig klären. Die sportliche "
|
||||
"Leitung plant ein Gespräch mit dem Torwart über seine Rolle im Kader.",
|
||||
false, "inbox"},
|
||||
{"tickets", 1, "finance", "8.320 Dauerkarten für die neue Saison",
|
||||
"Der Vorverkauf läuft gut: 8.320 Fans haben sich bereits ihren "
|
||||
"Stammplatz gesichert. Die fiktiven Einnahmen aus dem "
|
||||
"Dauerkartenverkauf betragen 1.450.000 €.",
|
||||
false, "inbox"},
|
||||
{"camp", 2, "training", "Die Vorbereitung nimmt Fahrt auf",
|
||||
"Ein Trainingslager bietet Raum für Kondition, Technik und Teamgeist. "
|
||||
"Das Trainerteam hat erste Ziele für die gemeinsame Vorbereitung "
|
||||
"zusammengestellt.",
|
||||
false, "inbox"},
|
||||
{"shirt", 3, "finance", "Neue Optionen für die Trikotwerbung",
|
||||
"Mehrere Unternehmen interessieren sich für eine Partnerschaft mit "
|
||||
"{{club}}. Die Angebote können in Ruhe geprüft und verglichen werden.",
|
||||
true, "inbox"},
|
||||
{"friendly", 4, "fixture", "Ein erster Härtetest für die Mannschaft",
|
||||
"Ein Testspiel steht auf dem Vorbereitungsplan. Das Trainerteam möchte "
|
||||
"möglichst vielen Spielern Einsatzzeit geben.",
|
||||
true, "inbox"},
|
||||
{"pitch", 5, "stadium", "Der Rasen ist bereit für den Saisonstart",
|
||||
"Die Platzpflege hat die Sommerpause genutzt. Der Rasen präsentiert "
|
||||
"sich zum Trainingsauftakt in gutem Zustand.",
|
||||
true, "inbox"},
|
||||
{"fans", 6, "club", "Vorfreude auf den Rängen",
|
||||
"Die Fans von {{club}} stimmen sich auf die neue Saison ein. Zum "
|
||||
"Trainingsauftakt werden zahlreiche Besucher erwartet.",
|
||||
true, "inbox"},
|
||||
{"review", 7, "fixture", "Die Saison im Rückblick",
|
||||
"Spannende Spiele, große Gefühle und viele gemeinsame Momente: Eine "
|
||||
"ereignisreiche Spielzeit liegt hinter dem Verein.",
|
||||
true, "archive"},
|
||||
{"planning", 9, "transfer", "Sommerplanung mit klarem Kurs",
|
||||
"Die sportliche Leitung hat die Aufgaben für die Sommerpause "
|
||||
"zusammengestellt. Im Mittelpunkt stehen die Kaderplanung und die "
|
||||
"Entwicklung junger Spieler.",
|
||||
true, "archive"},
|
||||
{"together", 12, "club", "Gemeinsam in die nächste Spielzeit",
|
||||
"Vorstand und Trainerteam bedanken sich bei Mannschaft, Mitarbeitern "
|
||||
"und Fans für ihre Unterstützung.",
|
||||
true, "archive"}}};
|
||||
Json items = Json::array();
|
||||
for (const auto &f : fixtures) {
|
||||
const auto day = *today - std::chrono::days{f.ago};
|
||||
if (int(std::chrono::year_month_day{day}.year()) < 1)
|
||||
throw editor::Error("invalid_news_date");
|
||||
items.push_back({{"id", f.id},
|
||||
{"date", news_iso(day)},
|
||||
{"kind", f.kind},
|
||||
{"title", f.title},
|
||||
{"body", f.body},
|
||||
{"read", f.read},
|
||||
{"folder", f.folder}});
|
||||
}
|
||||
return {{"version", 1}, {"items", items}};
|
||||
}
|
||||
bool valid_demo_news(const Json &state) {
|
||||
try {
|
||||
const auto &news = state.at("news");
|
||||
if (!news_keys(news, {"version", "items"}) ||
|
||||
!news.at("version").is_number_integer() || news.at("version") != 1 ||
|
||||
!news.at("items").is_array() || news.at("items").size() > 100)
|
||||
return false;
|
||||
std::set<std::string> ids;
|
||||
for (const auto &m : news.at("items")) {
|
||||
if (!news_keys(
|
||||
m, {"id", "date", "kind", "title", "body", "read", "folder"}) ||
|
||||
!news_id(m.at("id")) ||
|
||||
!ids.insert(m.at("id").get<std::string>()).second ||
|
||||
!m.at("date").is_string() ||
|
||||
!news_date(m.at("date").get<std::string>()) ||
|
||||
!m.at("kind").is_string() ||
|
||||
news_category(m.at("kind").get<std::string>()).empty() ||
|
||||
!news_text(m.at("title"), 160, true) ||
|
||||
!news_text(m.at("body"), 1200) || !m.at("read").is_boolean() ||
|
||||
!news_folder(m.at("folder")))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool migrate_demo_news(Json &state, const Catalog &world) {
|
||||
try {
|
||||
if (!state.is_object())
|
||||
return false;
|
||||
if (state.contains("news"))
|
||||
return valid_demo_news(state);
|
||||
auto next = state;
|
||||
next["news"] = demo_news(world, state);
|
||||
if (!valid_demo_news(next))
|
||||
return false;
|
||||
state = std::move(next);
|
||||
return true;
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::string edit_demo_news(Json &state, const Json &action, const Catalog &) {
|
||||
if (!valid_demo_news(state))
|
||||
return "invalid_news_state";
|
||||
try {
|
||||
if (!action.is_object() || !action.contains("kind") ||
|
||||
!action.at("kind").is_string())
|
||||
return "invalid_news_action";
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
auto next = state;
|
||||
auto &items = next["news"]["items"];
|
||||
if (kind == "read-all") {
|
||||
if (!news_keys(action, {"kind", "folder"}) ||
|
||||
!news_folder(action.at("folder"), false))
|
||||
return "invalid_news_action";
|
||||
for (auto &item : items)
|
||||
if (item.at("folder") == action.at("folder"))
|
||||
item["read"] = true;
|
||||
} else if (kind == "read" || kind == "move") {
|
||||
if (!(kind == "read" ? news_keys(action, {"kind", "id"})
|
||||
: news_keys(action, {"kind", "id", "folder"})) ||
|
||||
!news_id(action.at("id")) ||
|
||||
(kind == "move" && !news_folder(action.at("folder"))))
|
||||
return "invalid_news_action";
|
||||
auto found = std::find_if(items.begin(), items.end(), [&](const auto &m) {
|
||||
return m.at("id") == action.at("id");
|
||||
});
|
||||
if (found == items.end() || found->at("folder") == "trash")
|
||||
return "news_not_found";
|
||||
if (kind == "read")
|
||||
(*found)["read"] = true;
|
||||
else {
|
||||
(*found)["folder"] = action.at("folder");
|
||||
if (action.at("folder") != "trash")
|
||||
(*found)["read"] = true;
|
||||
}
|
||||
} else if (kind == "undo") {
|
||||
if (!news_keys(action, {"kind", "patch"}) ||
|
||||
!action.at("patch").is_array() || action.at("patch").empty() ||
|
||||
action.at("patch").size() > 100)
|
||||
return "invalid_news_action";
|
||||
std::set<std::string> ids;
|
||||
for (const auto &p : action.at("patch")) {
|
||||
if (!news_keys(p, {"id", "before", "after"}) || !news_id(p.at("id")) ||
|
||||
!ids.insert(p.at("id").get<std::string>()).second ||
|
||||
!news_valid_pair(p.at("before")) ||
|
||||
!news_valid_pair(p.at("after")) || p.at("before") == p.at("after"))
|
||||
return "invalid_news_action";
|
||||
auto found =
|
||||
std::find_if(items.begin(), items.end(), [&](const auto &m) {
|
||||
return m.at("id") == p.at("id");
|
||||
});
|
||||
if (found == items.end() || news_pair(*found) != p.at("after"))
|
||||
return "news_undo_conflict";
|
||||
(*found)["read"] = p.at("before").at("read");
|
||||
(*found)["folder"] = p.at("before").at("folder");
|
||||
}
|
||||
} else
|
||||
return "invalid_news_action";
|
||||
if (!valid_demo_news(next))
|
||||
return "invalid_news_state";
|
||||
state = std::move(next);
|
||||
return {};
|
||||
} catch (const std::exception &) {
|
||||
return "invalid_news_action";
|
||||
}
|
||||
}
|
||||
int demo_news_unread(const Json &state) {
|
||||
if (!valid_demo_news(state))
|
||||
return 0;
|
||||
int count = 0;
|
||||
for (const auto &m : state.at("news").at("items"))
|
||||
if (m.at("folder") == "inbox" && !m.at("read").get<bool>())
|
||||
++count;
|
||||
return count;
|
||||
}
|
||||
Json demo_news_view(const Catalog &world, const Json &state,
|
||||
const std::string &folder, const std::string &query) {
|
||||
if (!valid_demo_news(state) || (folder != "inbox" && folder != "archive") ||
|
||||
query.size() > 120)
|
||||
return {{"ok", false}, {"error", "invalid_news_view"}};
|
||||
std::string manager, club_id, club_name;
|
||||
if (state.contains("managers") && state.at("managers").is_array() &&
|
||||
!state.at("managers").empty()) {
|
||||
const auto &m = state.at("managers")[0];
|
||||
manager = m.value("name", "");
|
||||
club_id = m.value("club", "");
|
||||
if (const auto *c = world.find("clubs", club_id))
|
||||
club_name = world.name(*c);
|
||||
}
|
||||
const auto resolve = [&](const std::string &v) {
|
||||
return news_replace(news_replace(v, "{{club}}", club_name), "{{manager}}",
|
||||
manager);
|
||||
};
|
||||
Json shown = Json::array(), counts = {{"inbox", 0}, {"archive", 0}};
|
||||
bool can_read = false;
|
||||
for (auto m : state.at("news").at("items")) {
|
||||
const auto location = m.at("folder").get<std::string>();
|
||||
if (location == "trash")
|
||||
continue;
|
||||
counts[location] = counts.at(location).get<int>() + 1;
|
||||
if (location != folder)
|
||||
continue;
|
||||
if (!m.at("read").get<bool>())
|
||||
can_read = true;
|
||||
m["title"] = resolve(m.at("title").get<std::string>());
|
||||
m["body"] = resolve(m.at("body").get<std::string>());
|
||||
m["category"] = news_category(m.at("kind").get<std::string>());
|
||||
m["display_date"] = news_display_date(m.at("date").get<std::string>());
|
||||
const auto haystack = news_fold(m.at("title").get<std::string>() + " " +
|
||||
m.at("body").get<std::string>() + " " +
|
||||
m.at("category").get<std::string>() + " " +
|
||||
m.at("display_date").get<std::string>());
|
||||
if (query.empty() || haystack.find(news_fold(query)) != haystack.npos)
|
||||
shown.push_back(std::move(m));
|
||||
}
|
||||
std::stable_sort(shown.begin(), shown.end(),
|
||||
[](const auto &a, const auto &b) {
|
||||
return a.at("date").template get<std::string>() >
|
||||
b.at("date").template get<std::string>();
|
||||
});
|
||||
int unread = 0;
|
||||
for (const auto &m : shown)
|
||||
if (!m.at("read").get<bool>())
|
||||
++unread;
|
||||
return {{"ok", true},
|
||||
{"fictional", true},
|
||||
{"items", shown},
|
||||
{"counts", counts},
|
||||
{"unread", demo_news_unread(state)},
|
||||
{"shown_unread", unread},
|
||||
{"can_read", can_read},
|
||||
{"club", club_id},
|
||||
{"club_name", club_name},
|
||||
{"manager", manager},
|
||||
{"date", news_display_date(state.value("date", std::string()))}};
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,23 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/catalog.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
// Presentation-only journal. Templates remain persisted, then resolve against
|
||||
// the current snapshot's first manager/club. No catalogue or engine mutation.
|
||||
Json demo_news(const Catalog &, const Json &full_state);
|
||||
bool valid_demo_news(const Json &full_state);
|
||||
bool migrate_demo_news(Json &full_state, const Catalog &);
|
||||
// read{id}, read-all{folder}, move{id,folder}, undo{patch:[{id,before,after}]}.
|
||||
// Patches contain only {read,folder}; every expected after-value must match.
|
||||
// The DataSource owns the outer revision check and atomic publication.
|
||||
std::string edit_demo_news(Json &full_state, const Json &action,
|
||||
const Catalog &);
|
||||
int demo_news_unread(const Json &full_state);
|
||||
// Read-only projection: counts, unread, shown items, can_read, club, manager,
|
||||
// date. Query and folder belong to the view and never enter a checkpoint.
|
||||
Json demo_news_view(const Catalog &, const Json &full_state,
|
||||
const std::string &folder = "inbox",
|
||||
const std::string &query = {});
|
||||
std::string news_display_date(const std::string &);
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,242 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_statistics.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
const std::array<const char *,9> sections{"Möglichkeiten","Transferhistorie","Analytics","Ewige Tabelle","WM-Statistiken","Saisonfinanzen","Serien u. Rekorde","Sieger","Minuten-Statistik"};
|
||||
const std::array<const char *,6> views{"Sieger","Gruppen","Torschützen","Finalrunde","Tabelle","Historie"};
|
||||
const std::array<const char *,6> curves{"Kontostand","Mannschaftsstärke","Zuschauer","Stimmung","Einnahmen","Ausgaben"};
|
||||
const std::array<const char *,6> colors{"#e3da6a","#79b5c8","#a9d67b","#e58a66","#ded9bd","#b295cd"};
|
||||
const std::array<std::array<int,3>,12> domestic{{{{0,0,1}},{{1,3,1}},{{3,1,3}},{{2,4,1}},{{1,1,4}},{{0,2,0}},{{3,3,1}},{{1,5,1}},{{2,1,2}},{{1,1,0}},{{3,0,3}},{{4,2,1}}}};
|
||||
const Json &field(const Json &value,const char *key) {
|
||||
static const Json missing;
|
||||
if(value.is_object())if(const auto i=value.find(key);i!=value.end())return *i;
|
||||
return missing;
|
||||
}
|
||||
std::string string(const Json &value,const std::string &fallback="") {
|
||||
return value.is_string()?value.get<std::string>():fallback;
|
||||
}
|
||||
std::optional<int> season_year(const Json &value) {
|
||||
if(!value.is_string())return {};
|
||||
const auto d=value.get<std::string>();
|
||||
if(d.size()!=10||d[4]!='-'||d[7]!='-')return {};
|
||||
for(size_t i=0;i<d.size();++i)if(i!=4&&i!=7&&(d[i]<'0'||d[i]>'9'))return {};
|
||||
const int y=std::stoi(d.substr(0,4)),m=std::stoi(d.substr(5,2)),day=std::stoi(d.substr(8,2));
|
||||
if(!std::chrono::year_month_day{std::chrono::year{y},std::chrono::month{unsigned(m)},std::chrono::day{unsigned(day)}}.ok())return {};
|
||||
return y-(m<7?1:0);
|
||||
}
|
||||
std::string season(int year) {
|
||||
auto end=std::to_string((year+1)%100);
|
||||
return std::to_string(year)+"/"+(end.size()<2?"0":"")+end;
|
||||
}
|
||||
Json cell(const std::string &text) {return {{"text",text}};}
|
||||
Json numeric(int value) {return {{"text",std::to_string(value)},{"value",value}};}
|
||||
Json club_cell(const Catalog &world,const Json *club) {
|
||||
if(!club)return cell("—");
|
||||
return {{"text",world.name(*club)},{"club",club->at("id")}};
|
||||
}
|
||||
Json nation_cell(const Catalog &world,const Json *nation) {
|
||||
return nation?Json{{"text",world.name(*nation)},{"nation",nation->at("id")}}:cell("—");
|
||||
}
|
||||
Json player_cell(const Catalog &world,const Json *player) {
|
||||
return player?Json{{"text",world.name(*player)},{"player",player->at("id")},{"club",player->value("team","")}}:cell("—");
|
||||
}
|
||||
void headings(Json &out,std::initializer_list<const char *> names) {
|
||||
for(const auto *name:names)out["columns"].push_back({{"text",name}});
|
||||
}
|
||||
void row(Json &out,Json cells) {out["rows"].push_back({{"cells",std::move(cells)}});}
|
||||
const Json *country_nation(const Catalog &world, const std::string &code) {
|
||||
if (const auto *nation = world.find("nations", "nation:" + code))
|
||||
return nation;
|
||||
// England has no ISO alpha-2 country of its own. OBW retains the stable
|
||||
// imported football-nation identity instead of replacing it with the UK.
|
||||
if (code == "en")
|
||||
for (const auto *id : {"nation:x-eng", "nation:eng"})
|
||||
if (const auto *nation = world.find("nations", id)) return nation;
|
||||
return nullptr;
|
||||
}
|
||||
std::string statistics_country_code(const Json *nation) {
|
||||
if (!nation) return {};
|
||||
const auto id = nation->value("id", "");
|
||||
const auto code = id.starts_with("nation:") ? id.substr(7) : id;
|
||||
return code == "x-eng" || code == "eng" ? "en" : code;
|
||||
}
|
||||
std::vector<const Json *> countries(const Catalog &world) {
|
||||
std::vector<const Json *> result;
|
||||
std::set<std::string> seen;
|
||||
// Match Gold's fixed domestic ordering; retain actual IDs and captions.
|
||||
for(const auto *code:{"de","es","it","fr","en"})
|
||||
if(const auto *nation=country_nation(world,code);nation&&!world.leagues(nation->at("id").get<std::string>()).empty()) {
|
||||
result.push_back(nation);seen.insert(nation->at("id").get<std::string>());
|
||||
}
|
||||
for(const auto *league:world.leagues()) {
|
||||
const auto id=league->value("nation","");
|
||||
if(const auto *nation=world.find("nations",id);nation&&seen.insert(id).second)result.push_back(nation);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::vector<const Json *> first_league_clubs(const Catalog &world,const Json *nation) {
|
||||
if(!nation)return {};
|
||||
const auto leagues=world.leagues(nation->at("id").get<std::string>());
|
||||
if(leagues.empty())return {};
|
||||
// Catalog order is canonical; lower-tier competitions are never mixed in.
|
||||
const Json *first=leagues.front();
|
||||
for(const auto *league:leagues)if(league->value("tier",1)<first->value("tier",1))first=league;
|
||||
return world.clubs(first->at("id").get<std::string>());
|
||||
}
|
||||
const Json *at(const std::vector<const Json *> &items,size_t index) {
|
||||
return items.empty()?nullptr:items[index%items.size()];
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json demo_statistics(const Catalog &world,const Json &state,const Json &request) {
|
||||
const auto reject=[](const std::string &error){return Json{{"ok",false},{"fictional",true},{"error",error}};};
|
||||
if(!request.is_object())return reject("invalid_statistics_request");
|
||||
for(const auto &[key,value]:request.items())if(key!="section"&&key!="category"&&key!="country"&&key!="winner_country"&&key!="absolute"&&key!="curves")return reject("unknown_statistics_filter");
|
||||
const auto integer=[&](const char *key,int fallback,int maximum)->std::optional<int>{
|
||||
if(!request.contains(key))return fallback;
|
||||
const auto &v=request.at(key);
|
||||
if(!v.is_number_integer()||v<0||v>maximum)return {};
|
||||
return v.get<int>();
|
||||
};
|
||||
const auto section=integer("section",7,8),category=integer("category",0,5),country=integer("country",0,5);
|
||||
if(!section||!category||!country||(*category!=0&&*category!=1&&*category!=5))return reject("invalid_statistics_filter");
|
||||
if(request.contains("absolute")&&!request.at("absolute").is_boolean())return reject("invalid_statistics_absolute");
|
||||
Json active=Json::array({true,true,true,true,true,true});
|
||||
if(request.contains("curves")) {
|
||||
const auto &value=request.at("curves");
|
||||
if(!value.is_array()||value.size()!=6||!std::all_of(value.begin(),value.end(),[](const Json &v){return v.is_boolean();}))return reject("invalid_statistics_curves");
|
||||
active=value;
|
||||
}
|
||||
const auto nations=countries(world);
|
||||
const auto winner=request.contains("winner_country")?string(request.at("winner_country")):nations.empty()?"":nations.front()->at("id").get<std::string>();
|
||||
if(request.contains("winner_country")&&(!request.at("winner_country").is_string()||std::none_of(nations.begin(),nations.end(),[&](const Json *n){return n->at("id")==winner;})))return reject("invalid_statistics_country");
|
||||
const Json *own=nullptr;std::string manager_name="—";
|
||||
const auto &managers=field(state,"managers");
|
||||
if(managers.is_array()&&!managers.empty()) {
|
||||
own=world.find("clubs",string(field(managers.front(),"club")));
|
||||
if(own&&own->value("type","club")!="club")own=nullptr;
|
||||
const auto name=string(field(managers.front(),"name"));if(!name.empty())manager_name=name;
|
||||
}
|
||||
int year=season_year(field(state,"date")).value_or(season_year(world.manifest().value("start_date",Json())).value_or(2027));
|
||||
const bool absolute=request.value("absolute",false);
|
||||
Json out={{"ok",true},{"fictional",true},{"available",true},{"section",*section},{"category",*category},{"country",*country},{"winner_country",winner},{"absolute",absolute},{"curves",active},{"title",sections[size_t(*section)]},{"season",season(year)},{"previous_season",season(year-1)},{"year",year},{"manager",manager_name},{"club",own?own->at("id"):Json()},{"columns",Json::array()},{"rows",Json::array()},{"countries",Json::array()},{"sections",sections},{"world_views",views},{"series",Json::array()}};
|
||||
for(const auto *n:nations)out["countries"].push_back({{"id",n->at("id")},{"name",world.name(*n)}});
|
||||
out["eternal_countries"] = Json::array();
|
||||
const std::array<const char *, 5> eternal_codes{"de", "es", "en", "fr", "it"};
|
||||
for (size_t i = 0; i < eternal_codes.size(); ++i)
|
||||
if (const auto *n = country_nation(world, eternal_codes[i]))
|
||||
out["eternal_countries"].push_back({{"value", i}, {"nation", n->at("id")}, {"name", world.name(*n)}});
|
||||
out["eternal_countries"].push_back({{"value", 5}, {"name", "Absolut"}});
|
||||
auto own_league=own?world.clubs(own->value("competition","")):std::vector<const Json *>{};
|
||||
if(own&&own_league.empty())own_league.push_back(own);
|
||||
if(!own&& !nations.empty())own_league=first_league_clubs(world,nations.front());
|
||||
// Gold uses one German/own-league cohort for possibilities and records.
|
||||
const auto clubs=own_league;
|
||||
const auto players=own?world.squad(own->at("id").get<std::string>()):std::vector<const Json *>{};
|
||||
if(*section==0) {
|
||||
headings(out,{"Verein","Meisterschaft","Pokal","Europapokal"});
|
||||
for(size_t i=0;i<std::min(size_t(14),clubs.size());++i)row(out,Json::array({club_cell(world,clubs[i]),cell(std::to_string(std::max(0,72-int(i)*5))+" %"),cell(std::to_string(std::max(0,35-int(i)*2))+" %"),cell(std::to_string(std::max(0,22-int(i)))+" %")}));
|
||||
} else if(*section==1) {
|
||||
headings(out,{"Saison / Spieler","Von","Nach","Ablöse"});
|
||||
for(size_t i=0;i<std::min(size_t(10),players.size());++i) {
|
||||
auto p=player_cell(world,players[i]);p["prefix"]=std::to_string(year-int(i%3))+" · ";
|
||||
row(out,Json::array({p,club_cell(world,at(clubs,i+1)),club_cell(world,own),{{"text",number(450000+int(i)*215000)+" €"},{"value",450000+int(i)*215000}}}));
|
||||
}
|
||||
} else if(*section==2) {
|
||||
for(size_t k=0;!clubs.empty()&&k<6;++k) {
|
||||
Json points=Json::array();
|
||||
for(int i=0;i<24;++i)points.push_back({50+i*140,absolute?700-(i*7+int(k)*22):650-(200+std::sin(i*.45+double(k))*145+i*6)});
|
||||
out["series"].push_back({{"name",curves[k]},{"color",colors[k]},{"active",active[k]},{"points",points}});
|
||||
}
|
||||
} else if(*section==3) {
|
||||
headings(out,{"Verein","Sp.","S","U","N","Punkte","Tore"});
|
||||
const auto selected=*country==5?clubs:first_league_clubs(world,country_nation(world,eternal_codes[size_t(*country)]));
|
||||
for(size_t i=0;i<selected.size();++i)row(out,Json::array({club_cell(world,selected[i]),numeric(158-int(i)*3-*country*4),numeric(91-int(i)*3),numeric(31+int(i%5)),numeric(36+int(i%7)),numeric(304-int(i)*12-*country*3),cell(std::to_string(219-int(i)*5)+":"+std::to_string(146+int(i)*4))}));
|
||||
} else if(*section==4) {
|
||||
std::vector<const Json *> ns;
|
||||
for(const auto *code:{"de","es","fr","it","en","nl","pt","be"})if(const auto *n=country_nation(world,code))ns.push_back(n);
|
||||
for(const auto &n:world.rows("nations"))if(ns.size()<8&&std::find(ns.begin(),ns.end(),&n)==ns.end())ns.push_back(&n);
|
||||
if(*country==0||*country==5) {
|
||||
headings(out,{"WM / EM","Sieger","Finalist","Finale"});
|
||||
for(size_t i=0;i<std::min(size_t(6),ns.size());++i) {
|
||||
auto tournament=cell(std::string(i%2?"EM ":"WM ")+std::to_string(year-1-int(i)*2));
|
||||
tournament["trophy"]=i%2?"european-championship":"world-cup";tournament["winner"]=ns[i]->at("id");tournament["season"]=std::to_string(year-1-int(i)*2);
|
||||
row(out,Json::array({tournament,nation_cell(world,ns[i]),nation_cell(world,at(ns,i+1)),cell(std::to_string(i%3+1)+":"+std::to_string(i%2))}));
|
||||
}
|
||||
} else if(*country==1) {
|
||||
headings(out,{"Gruppe","Mannschaft","Spiele","Punkte"});
|
||||
for(size_t i=0;i<ns.size();++i)row(out,Json::array({cell(std::string("Gruppe ")+char('A'+i/4)),nation_cell(world,ns[i]),numeric(3),numeric(9-int(i%4)*2)}));
|
||||
} else if(*country==2) {
|
||||
headings(out,{"Spieler","Land","Tore"});
|
||||
for(size_t i=0;i<std::min({size_t(8),players.size(),ns.size()});++i)row(out,Json::array({player_cell(world,players[i]),nation_cell(world,ns[i]),numeric(8-int(i))}));
|
||||
} else if(*country==3) {
|
||||
headings(out,{"Heim","Ergebnis","Auswärts"});out["columns"][0]["round"]="Runde";
|
||||
const std::array<const char *,4> rounds{"Finale","Halbfinale","Halbfinale","Viertelfinale"};
|
||||
for(size_t i=0;i<std::min(size_t(4),ns.size());++i) {
|
||||
auto home=nation_cell(world,ns[i]);home["round"]=rounds[i];
|
||||
row(out,Json::array({home,cell(std::to_string(i%3+1)+":"+std::to_string(i%2)),nation_cell(world,ns[ns.size()-1-i])}));
|
||||
}
|
||||
} else {
|
||||
headings(out,{"Land","Spiele","Siege","Punkte"});
|
||||
for(size_t i=0;i<ns.size();++i)row(out,Json::array({nation_cell(world,ns[i]),numeric(20-int(i)),numeric(15-int(i)),numeric(45-int(i)*3)}));
|
||||
}
|
||||
} else if(*section==5) {
|
||||
headings(out,{"Saison","Einnahmen","Ausgaben","Saldo"});
|
||||
if(own)for(int i=0;i<10;++i)row(out,Json::array({numeric(year-i),cell(number(14500000-i*125000)+" €"),cell(number(13800000-i*85000)+" €"),cell(number(700000-i*40000)+" €")}));
|
||||
} else if(*section==6) {
|
||||
headings(out,{"Serie / Rekord","Verein","Wert"});
|
||||
const std::array<const char *,8> names{"Ungeschlagen","Siege in Folge","Heimsiege","Auswärtssiege","Tore in einer Saison","Höchster Sieg","Längste Torserie","Meiste Zuschauer"};
|
||||
for(size_t i=0;i<std::min(size_t(8),clubs.size());++i)row(out,Json::array({cell(names[i]),club_cell(world,clubs[i]),cell(i<4?std::to_string(22-int(i)):i==5?"7 : 0":i==7?"48.500":std::to_string(75-int(i)))}));
|
||||
} else if(*section==7) {
|
||||
const auto *nation=world.find("nations",winner);const auto domestic_clubs=first_league_clubs(world,nation);
|
||||
const auto code=statistics_country_code(nation);
|
||||
std::vector<std::string> titles,trophies;
|
||||
if(*category==0) {
|
||||
if(code=="es"){titles={"Primera División","Copa del Rey","Supercopa"};trophies={"es-la-liga","es-copa-del-rey",""};}
|
||||
else if(code=="en"){titles={"Premier League","FA Cup","League Cup"};trophies={"en-premier-league","en-fa-cup",""};}
|
||||
else if(code=="fr"){titles={"Ligue 1","Coupe de France","Trophée des Champions"};trophies={"fr-ligue-1","fr-coupe-de-france",""};}
|
||||
else if(code=="it"){titles={"Serie A","Coppa Italia","Supercoppa"};trophies={"it-serie-a","it-coppa-italia",""};}
|
||||
else {titles={"Meisterschaft","DFB-Pokal","Supercup"};trophies={"championship","dfb-cup","supercup"};}
|
||||
out["note"]=(nation?world.name(*nation):"—")+" · Titelträger im Saisonvergleich";
|
||||
} else if(*category==1){titles={"Champions League","Pokalsieger","UEFA-Cup"};trophies={"champions-league","cup-winners","uefa-cup"};out["note"]="Europäische Titelträger im Saisonvergleich";}
|
||||
else {titles={"Sieger","Finalist"};trophies={"indoor-masters",""};out["note"]="Hallenmasters · Sieger und Finalisten";}
|
||||
headings(out,{"Saison"});
|
||||
std::vector<const Json *> international;
|
||||
const std::array<const char *,12> preferred{"club:es-real-madrid","club:en-manchester-city","club:it-ac-mailand","club:es-fc-barcelona","club:en-liverpool","club:de-bayern-muenchen","club:fr-paris-saint-germain","club:it-inter-mailand","club:de-borussia-dortmund","club:en-chelsea","club:it-juventus-turin","club:es-fc-sevilla"};
|
||||
for(const auto *id:preferred)if(const auto *club=world.find("clubs",id))international.push_back(club);
|
||||
if(international.empty())for(size_t i=0;i<12;++i) {
|
||||
if(nations.empty())break;
|
||||
const auto candidates=first_league_clubs(world,nations[i%nations.size()]);
|
||||
if(const auto *club=at(candidates,i/nations.size()))if(std::find(international.begin(),international.end(),club)==international.end())international.push_back(club);
|
||||
}
|
||||
for(size_t col=0;col<titles.size();++col)out["columns"].push_back({{"text",titles[col]},{"trophy",trophies[col]}});
|
||||
const bool available=*category==0?!domestic_clubs.empty():*category==1?!international.empty():!clubs.empty();
|
||||
if(available)for(size_t i=0;i<12;++i) {
|
||||
Json cells=Json::array({cell(season(year-1-int(i)))});
|
||||
for(size_t col=0;col<titles.size();++col) {
|
||||
const auto *club=*category==0?at(domestic_clubs,size_t(domestic[i][col])):*category==1?at(international,i+col*4):at(clubs,i*3+(col?7:2));
|
||||
cells.push_back(club_cell(world,club));
|
||||
if(i==0)out["columns"][col+1]["winner"]=club?club->at("id"):Json();
|
||||
}
|
||||
row(out,std::move(cells));
|
||||
}
|
||||
} else {
|
||||
headings(out,{"Ereignis"});for(int i=0;i<18;++i)out["columns"].push_back(cell(std::to_string(i*5)+"."));
|
||||
const std::array<const char *,4> names{"Tore","Chancen","Gelb","Rot"};
|
||||
if(own)for(int k=0;k<4;++k) {
|
||||
Json cells=Json::array({cell(names[size_t(k)])});
|
||||
for(int i=0;i<18;++i){auto c=numeric((i*3+k)%13);c["tone"]=(i+k)%5==0?"bad":(i+k)%3==0?"good":"";cells.push_back(c);}
|
||||
row(out,std::move(cells));
|
||||
}
|
||||
}
|
||||
out["available"]=*section==2?!clubs.empty():!out["rows"].empty();
|
||||
return out;
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,13 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/catalog.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
// Deterministic, presentation-only statistics. Request: section 0..8 (default
|
||||
// 7), category 0/1/5, country 0..5, winner_country an actual nation ID,
|
||||
// absolute boolean and curves an array of six booleans. No state is mutated.
|
||||
// Table cells carry text and optional actual club/player/nation references.
|
||||
// Unknown fields, malformed filters and unavailable countries are rejected.
|
||||
Json demo_statistics(const Catalog &, const Json &full_state,
|
||||
const Json &request = Json::object());
|
||||
} // namespace bmh::modern_ui
|
||||
+14
-1
@@ -11,6 +11,8 @@
|
||||
#include "modern/ui/demo_lineup.hpp"
|
||||
#include "modern/ui/demo_transfer.hpp"
|
||||
#include "modern/ui/demo_stadium.hpp"
|
||||
#include "modern/ui/demo_statistics.hpp"
|
||||
#include "modern/ui/demo_news.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
@@ -99,18 +101,27 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
throw editor::Error("invalid_demo_squad");
|
||||
if (!migrate_demo_stadium(state_, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_stadium");
|
||||
if (!migrate_demo_news(state_, *catalog_))
|
||||
throw editor::Error("invalid_demo_news");
|
||||
saved_ = state_;
|
||||
}
|
||||
Snapshot DemoSource::snapshot() const { return {revision_, catalog_, state_}; }
|
||||
Json DemoSource::read_almanac(const Json &request) const {
|
||||
return query_demo_almanac(almanac_, request, state_.at("date"));
|
||||
}
|
||||
Json DemoSource::read_statistics(const Json &request) const {
|
||||
return demo_statistics(*catalog_, state_, request);
|
||||
}
|
||||
ActionResult DemoSource::apply(const Action &action) {
|
||||
if (action.revision != revision_)
|
||||
return {false, revision_, "stale_revision"};
|
||||
auto next = state_;
|
||||
try {
|
||||
if (action.kind == "ticket") {
|
||||
if (action.kind == "news") {
|
||||
const auto error = edit_demo_news(next, action.value, *catalog_);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "ticket") {
|
||||
if (!action.value.is_number_integer() || action.value < 0 ||
|
||||
action.value > 25)
|
||||
return {false, revision_, "invalid_demo_ticket"};
|
||||
@@ -289,6 +300,8 @@ Json DemoSource::load(const fs::path &file) {
|
||||
throw editor::Error("invalid_demo_squad");
|
||||
if (!migrate_demo_stadium(data, *catalog_, club))
|
||||
throw editor::Error("invalid_demo_stadium");
|
||||
if (!migrate_demo_news(data, *catalog_))
|
||||
throw editor::Error("invalid_demo_news");
|
||||
auto next = data;
|
||||
auto desktop = next.value("desktop", Json::object());
|
||||
if (!migrate_desktop(desktop))
|
||||
|
||||
@@ -29,6 +29,9 @@ public:
|
||||
virtual Json read_almanac(const Json &) const {
|
||||
return {{"ok", false}, {"error", "almanac_unavailable"}};
|
||||
}
|
||||
virtual Json read_statistics(const Json &) const {
|
||||
return {{"ok", false}, {"error", "statistics_unavailable"}};
|
||||
}
|
||||
};
|
||||
class DemoSource final : public DataSource {
|
||||
public:
|
||||
@@ -36,6 +39,7 @@ public:
|
||||
Snapshot snapshot() const override;
|
||||
ActionResult apply(const Action &) override;
|
||||
Json read_almanac(const Json &) const override;
|
||||
Json read_statistics(const Json &) const override;
|
||||
void save(const fs::path &, const Json &desktop);
|
||||
Json load(const fs::path &);
|
||||
bool dirty() const { return state_ != saved_; }
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
void App::reset_news(bool preserve_view) {
|
||||
if (!preserve_view) {
|
||||
news_view_ = {{"folder", "inbox"}, {"query", ""}, {"feedback", ""}};
|
||||
news_world_.reset();
|
||||
news_source_ = nullptr;
|
||||
} else
|
||||
news_view_["feedback"] = "";
|
||||
news_dialog_.clear();
|
||||
news_return_focus_.clear();
|
||||
news_undo_ = Json();
|
||||
news_undo_revision_ = 0;
|
||||
news_scroll_ = 0;
|
||||
news_restore_scroll_ = false;
|
||||
}
|
||||
void App::news_before_render() {
|
||||
if (menu_ != "news") {
|
||||
if (last_rendered_menu_ == "news")
|
||||
reset_news(true);
|
||||
return;
|
||||
}
|
||||
if (last_rendered_menu_ != "news")
|
||||
return;
|
||||
if (auto *feed = element("news-feed"))
|
||||
news_scroll_ = feed->GetScrollTop();
|
||||
news_restore_scroll_ = true;
|
||||
if (focus_after_render_.empty()) {
|
||||
if (auto *focused = context_->GetFocusElement();
|
||||
focused && (focused->GetId().starts_with("news-") ||
|
||||
focused->GetId() == "window-close"))
|
||||
focus_after_render_ = focused->GetId();
|
||||
}
|
||||
}
|
||||
void App::news_after_render() {
|
||||
if (menu_ != "news")
|
||||
return;
|
||||
context_->Update();
|
||||
if (news_restore_scroll_) {
|
||||
if (auto *feed = element("news-feed"))
|
||||
feed->SetScrollTop(news_scroll_);
|
||||
news_restore_scroll_ = false;
|
||||
}
|
||||
if (!news_dialog_.empty()) {
|
||||
Rml::ElementList nodes;
|
||||
doc_->QuerySelectorAll(nodes, "#news-underlay button,#news-underlay "
|
||||
"input,.news-tablet>.titlebar button");
|
||||
for (auto *node : nodes) {
|
||||
node->SetProperty("pointer-events", "none");
|
||||
node->SetProperty("tab-index", "none");
|
||||
}
|
||||
}
|
||||
}
|
||||
void App::news_update_feed() {
|
||||
const auto s = snap();
|
||||
const auto data =
|
||||
demo_news_view(*s.world, s.state, news_view_.value("folder", "inbox"),
|
||||
news_view_.value("query", ""));
|
||||
if (!data.value("ok", false))
|
||||
return;
|
||||
if (auto *feed = element("news-feed")) {
|
||||
feed->SetInnerRML(render_news_feed(data));
|
||||
context_->Update();
|
||||
feed->SetScrollTop(0);
|
||||
news_scroll_ = 0;
|
||||
}
|
||||
if (auto *feedback = element("news-feedback"))
|
||||
feedback->SetInnerRML(esc(
|
||||
std::to_string(data.at("items").size()) +
|
||||
(data.at("items").size() == 1 ? " Nachricht" : " Nachrichten") + " · " +
|
||||
std::to_string(data.at("shown_unread").get<int>()) + " ungelesen"));
|
||||
dirty_ = true;
|
||||
}
|
||||
bool App::news_commit(const Json &action, const std::string &feedback,
|
||||
bool remember) {
|
||||
const auto before = snap();
|
||||
const auto result = source_->apply({before.revision, "news", action});
|
||||
if (!result.ok) {
|
||||
news_view_["feedback"] =
|
||||
"Änderung nicht gespeichert. Bitte erneut versuchen.";
|
||||
refresh();
|
||||
return false;
|
||||
}
|
||||
const auto after = snap();
|
||||
if (remember) {
|
||||
Json patch = Json::array();
|
||||
for (const auto &old : before.state.at("news").at("items")) {
|
||||
const auto &items = after.state.at("news").at("items");
|
||||
const auto found =
|
||||
std::find_if(items.begin(), items.end(), [&](const auto &m) {
|
||||
return m.at("id") == old.at("id");
|
||||
});
|
||||
if (found != items.end() && (old.at("read") != found->at("read") ||
|
||||
old.at("folder") != found->at("folder")))
|
||||
patch.push_back(
|
||||
{{"id", old.at("id")},
|
||||
{"before",
|
||||
{{"read", old.at("read")}, {"folder", old.at("folder")}}},
|
||||
{"after",
|
||||
{{"read", found->at("read")}, {"folder", found->at("folder")}}}});
|
||||
}
|
||||
if (!patch.empty())
|
||||
news_undo_ = std::move(patch);
|
||||
}
|
||||
// An article read can leave a prior undo patch usable. Its compare-before
|
||||
// guard still rejects a changed record, while the current revision binds UI.
|
||||
news_undo_revision_ = after.revision;
|
||||
news_view_["feedback"] = feedback;
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
bool App::dispatch_news(const std::string &action, Rml::Element *target) {
|
||||
if (!action.starts_with("news-"))
|
||||
return false;
|
||||
if (menu_ != "news" || !popup_.empty() || amount_)
|
||||
return true;
|
||||
const auto id =
|
||||
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
|
||||
if (action == "news-detail-close") {
|
||||
news_dialog_.clear();
|
||||
focus_after_render_ =
|
||||
news_return_focus_.empty()
|
||||
? "news-tab-" + news_view_.value("folder", "inbox")
|
||||
: news_return_focus_;
|
||||
news_return_focus_.clear();
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (!news_dialog_.empty() && action != "news-detail-move")
|
||||
return true;
|
||||
if (action == "news-folder") {
|
||||
if (id != "inbox" && id != "archive")
|
||||
return true;
|
||||
news_view_["folder"] = id;
|
||||
news_view_["query"] = "";
|
||||
news_view_["feedback"] = "";
|
||||
news_scroll_ = 0;
|
||||
if (auto *feed = element("news-feed"))
|
||||
feed->SetScrollTop(0);
|
||||
focus_after_render_ = "news-tab-" + id;
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
const auto before = snap();
|
||||
if (action == "news-undo") {
|
||||
if (!news_undo_.is_array() || news_undo_.empty())
|
||||
return true;
|
||||
if (news_world_ != before.world || news_source_ != source_.get() ||
|
||||
news_undo_revision_ != before.revision) {
|
||||
news_undo_ = Json();
|
||||
news_view_["feedback"] = "Die Nachricht wurde inzwischen geändert. "
|
||||
"Rückgängig ist nicht mehr möglich.";
|
||||
focus_after_render_ = "news-tab-" + news_view_.value("folder", "inbox");
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (news_commit({{"kind", "undo"}, {"patch", news_undo_}},
|
||||
"Letzte Änderung rückgängig gemacht.", false))
|
||||
news_undo_ = Json();
|
||||
focus_after_render_ = "news-tab-" + news_view_.value("folder", "inbox");
|
||||
return true;
|
||||
}
|
||||
if (action == "news-read-all") {
|
||||
const auto folder = news_view_.value("folder", "inbox");
|
||||
news_commit(
|
||||
{{"kind", "read-all"}, {"folder", folder}},
|
||||
"Alle Nachrichten " +
|
||||
std::string(folder == "inbox" ? "im Postfach" : "im Archiv") +
|
||||
" als gelesen markiert.");
|
||||
focus_after_render_ = "news-tab-" + folder;
|
||||
return true;
|
||||
}
|
||||
const auto requested = action == "news-detail-move" ? news_dialog_ : id;
|
||||
const auto &items = before.state.at("news").at("items");
|
||||
const auto found =
|
||||
std::find_if(items.begin(), items.end(), [&](const auto &m) {
|
||||
return m.at("id") == requested && m.at("folder") != "trash";
|
||||
});
|
||||
if (found == items.end())
|
||||
return true;
|
||||
if (action == "news-open") {
|
||||
if (!found->at("read").get<bool>() &&
|
||||
!news_commit({{"kind", "read"}, {"id", requested}}, "", false))
|
||||
return true;
|
||||
news_dialog_ = requested;
|
||||
news_return_focus_ = "news-read-" + requested;
|
||||
focus_after_render_ = "news-detail-close";
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (action == "news-move" || action == "news-delete" ||
|
||||
action == "news-detail-move") {
|
||||
const auto folder = action == "news-delete" ? "trash"
|
||||
: found->at("folder") == "archive" ? "inbox"
|
||||
: "archive";
|
||||
const auto feedback = std::string(folder) == "trash" ? "Nachricht gelöscht."
|
||||
: std::string(folder) == "inbox"
|
||||
? "Ins Postfach verschoben."
|
||||
: "Nachricht archiviert.";
|
||||
if (news_commit({{"kind", "move"}, {"id", requested}, {"folder", folder}},
|
||||
feedback)) {
|
||||
news_dialog_.clear();
|
||||
news_return_focus_.clear();
|
||||
focus_after_render_ = "news-tab-" + news_view_.value("folder", "inbox");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool App::news_event(Rml::Event &event) {
|
||||
if (menu_ != "news" || !popup_.empty() || amount_)
|
||||
return false;
|
||||
const auto type = event.GetType();
|
||||
auto *target = event.GetTargetElement();
|
||||
const auto key = type == "keydown"
|
||||
? Rml::Input::KeyIdentifier(
|
||||
event.GetParameter<int>("key_identifier", 0))
|
||||
: Rml::Input::KI_UNKNOWN;
|
||||
if (!news_dialog_.empty()) {
|
||||
if (key == Rml::Input::KI_ESCAPE ||
|
||||
(type == "mouseup" && event.GetParameter<int>("button", 0) == 1)) {
|
||||
dispatch_news("news-detail-close", nullptr);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
bool inside = false;
|
||||
for (auto *n = target; n && n != doc_; n = n->GetParentNode())
|
||||
if (n->GetId() == "news-detail-layer")
|
||||
inside = true;
|
||||
if (!inside && (type == "click" || type == "dblclick" ||
|
||||
type == "mousedown" || type == "mouseup" ||
|
||||
(type == "keydown" && key != Rml::Input::KI_TAB))) {
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ((type == "input" || type == "change") && target &&
|
||||
target->GetId() == "news-search" && news_dialog_.empty()) {
|
||||
if (auto *input = dynamic_cast<Rml::ElementFormControl *>(target)) {
|
||||
auto query = input->GetValue();
|
||||
if (query.size() > 120)
|
||||
query.resize(120);
|
||||
news_view_["query"] = query;
|
||||
news_view_["feedback"] = "";
|
||||
news_update_feed();
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (type == "keydown" && news_dialog_.empty() && target &&
|
||||
target->GetId().starts_with("news-tab-") &&
|
||||
(key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT ||
|
||||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
|
||||
const auto next = key == Rml::Input::KI_HOME ? "inbox"
|
||||
: key == Rml::Input::KI_END ? "archive"
|
||||
: news_view_.value("folder", "inbox") == "inbox"
|
||||
? "archive"
|
||||
: "inbox";
|
||||
dispatch_news("news-folder", element("news-tab-" + std::string(next)));
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (key == Rml::Input::KI_TAB) {
|
||||
Rml::ElementList controls;
|
||||
if (auto *root = element(news_dialog_.empty() ? "news-underlay"
|
||||
: "news-detail-layer"))
|
||||
root->QuerySelectorAll(controls, "button,input");
|
||||
if (news_dialog_.empty())
|
||||
if (auto *close = element("window-close"))
|
||||
controls.insert(controls.begin(), close);
|
||||
controls.erase(
|
||||
std::remove_if(controls.begin(), controls.end(),
|
||||
[](auto *n) {
|
||||
return !n->IsVisible(true) ||
|
||||
n->HasAttribute("disabled") ||
|
||||
n->template GetAttribute<Rml::String>(
|
||||
"tabindex", "") == "-1";
|
||||
}),
|
||||
controls.end());
|
||||
if (!controls.empty()) {
|
||||
const bool backwards = event.GetParameter<int>("shift_key", 0) != 0;
|
||||
const auto it = std::find(controls.begin(), controls.end(),
|
||||
context_->GetFocusElement());
|
||||
const int old = it == controls.end() ? (backwards ? 0 : -1)
|
||||
: int(it - controls.begin());
|
||||
auto *next =
|
||||
controls[size_t((old + (backwards ? -1 : 1) + int(controls.size())) %
|
||||
int(controls.size()))];
|
||||
next->Focus(true);
|
||||
for (auto *n = next; n && n != doc_; n = n->GetParentNode())
|
||||
if (n->GetId().starts_with("news-row-")) {
|
||||
n->ScrollIntoView(
|
||||
Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
|
||||
break;
|
||||
}
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
dirty_ = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
std::string App::render_news_art(const Json &message, double size) {
|
||||
const auto kind = message.at("kind").get<std::string>();
|
||||
std::string art;
|
||||
if (kind == "club")
|
||||
art = crest(club(snap()), size * .69);
|
||||
else {
|
||||
const auto key =
|
||||
kind == "fixture" ? "button.league"
|
||||
: kind == "transfer" ? "button.contracts"
|
||||
: kind == "finance"
|
||||
? (gui_.contains("news.cash") ? "news.cash" : "icon.coins")
|
||||
: kind == "training" ? "button.training"
|
||||
: "button.stadium";
|
||||
art = img(key, size * .76, size * .76, false, false, 256);
|
||||
}
|
||||
return "<div class=\"news-art news-art-" + esc(kind) +
|
||||
"\" style=\"width:" + scalar(size) + "dp;height:" + scalar(size) +
|
||||
"dp;\">" + art + "</div>";
|
||||
}
|
||||
std::string App::render_news_feed(const Json &data) {
|
||||
std::string out;
|
||||
for (const auto &m : data.at("items")) {
|
||||
const auto id = m.at("id").get<std::string>(),
|
||||
name = m.at("title").get<std::string>();
|
||||
const bool unread = !m.at("read").get<bool>(),
|
||||
archived = m.at("folder") == "archive";
|
||||
out += "<div id=\"news-row-" + id + "\" class=\"news-message" +
|
||||
(unread ? " is-unread" : "") + "\">";
|
||||
if (unread)
|
||||
out += "<span class=\"news-unread-dot\"></span>";
|
||||
out += render_news_art(m) +
|
||||
"<div class=\"news-message-copy\"><div "
|
||||
"class=\"news-message-meta\"><span>" +
|
||||
esc(m.at("display_date").get<std::string>()) +
|
||||
"</span><span class=\"news-category\">" +
|
||||
esc(m.at("category").get<std::string>()) + "</span>" +
|
||||
(unread ? "<span class=\"news-new\">Neu</span>" : "") + "</div>";
|
||||
out += btn("news-open", esc(name), "news-message-title",
|
||||
"id=\"news-read-" + id + "\" data-id=\"" + id +
|
||||
"\" aria-label=\"Nachricht lesen: " + esc(name) + "\"");
|
||||
out += "<p class=\"news-excerpt\">" + esc(m.at("body").get<std::string>()) +
|
||||
"</p></div><div class=\"news-row-actions\">";
|
||||
// Native CSS line-art has no private bitmap or SVG dependency.
|
||||
out += btn(
|
||||
"news-move",
|
||||
"<span class=\"news-symbol archive" +
|
||||
std::string(archived ? " restore" : "") +
|
||||
"\"><span class=\"news-archive-lid\"></span><span "
|
||||
"class=\"news-archive-handle\"></span></span>",
|
||||
"news-icon-button",
|
||||
"id=\"news-move-" + id + "\" data-id=\"" + id + "\" aria-label=\"" +
|
||||
std::string(archived ? "Ins Postfach verschieben: "
|
||||
: "Archivieren: ") +
|
||||
esc(name) + "\" title=\"" +
|
||||
std::string(archived ? "Ins Postfach verschieben" : "Archivieren") +
|
||||
"\"");
|
||||
out += btn("news-delete", glyph("trash", 36), "news-icon-button",
|
||||
"id=\"news-delete-" + id + "\" data-id=\"" + id +
|
||||
"\" aria-label=\"Löschen: " + esc(name) +
|
||||
"\" title=\"Löschen\"");
|
||||
out += "</div></div>";
|
||||
}
|
||||
if (out.empty()) {
|
||||
const bool query = !news_view_.value("query", "").empty(),
|
||||
archive = news_view_.value("folder", "inbox") == "archive";
|
||||
out = "<div class=\"news-empty\"><div "
|
||||
"class=\"news-empty-art\"><span></span></div><h3>" +
|
||||
std::string(query ? "Keine Nachrichten gefunden"
|
||||
: archive ? "Dein Archiv ist noch leer"
|
||||
: "Alles auf dem neuesten Stand") +
|
||||
"</h3><p>" +
|
||||
std::string(
|
||||
query ? "Versuche einen anderen Suchbegriff."
|
||||
: archive ? "Archivierte Meldungen bleiben hier zum Nachlesen."
|
||||
: "Hier erscheinen neue Meldungen aus deinem Verein.") +
|
||||
"</p></div>";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
std::string App::render_news_detail(const Json &message) {
|
||||
const auto name = message.at("title").get<std::string>();
|
||||
return "<div id=\"news-detail-layer\" class=\"news-detail-layer\"><div "
|
||||
"class=\"news-detail-shade\"></div><div class=\"news-detail-box\" "
|
||||
"role=\"dialog\" aria-modal=\"true\" "
|
||||
"aria-labelledby=\"news-detail-title\"><h2 id=\"news-detail-title\">" +
|
||||
esc(name) +
|
||||
"</h2><div class=\"news-detail\"><div class=\"news-detail-meta\">" +
|
||||
render_news_art(message) + "<div><p>" +
|
||||
esc(message.at("category").get<std::string>()) + "</p><p>" +
|
||||
esc(message.at("display_date").get<std::string>()) +
|
||||
"</p></div></div><p class=\"news-detail-body\">" +
|
||||
esc(message.at("body").get<std::string>()) +
|
||||
"</p><small>Fiktive Meldung für den "
|
||||
"Oberflächen-Prototyp.</small></div><div "
|
||||
"class=\"news-detail-actions\">" +
|
||||
btn("news-detail-close", "Schließen", {}, "id=\"news-detail-close\"") +
|
||||
btn("news-detail-move",
|
||||
message.at("folder") == "archive" ? "Ins Postfach" : "Archivieren",
|
||||
{}, "id=\"news-detail-move\"") +
|
||||
"</div></div></div>";
|
||||
}
|
||||
std::string App::render_news() {
|
||||
const auto s = snap();
|
||||
if (news_world_ != s.world || news_source_ != source_.get()) {
|
||||
reset_news();
|
||||
news_world_ = s.world;
|
||||
news_source_ = source_.get();
|
||||
}
|
||||
const auto data =
|
||||
demo_news_view(*s.world, s.state, news_view_.value("folder", "inbox"),
|
||||
news_view_.value("query", ""));
|
||||
if (!data.value("ok", false))
|
||||
return window("Nachrichten",
|
||||
"<p>Das Nachrichtenpostfach ist nicht verfügbar.</p>");
|
||||
const auto folder = news_view_.value("folder", "inbox");
|
||||
std::string content =
|
||||
"<div id=\"news-underlay\" class=\"news-layout\"><div "
|
||||
"class=\"news-masthead\"><div class=\"news-brand\"><span "
|
||||
"class=\"news-wordmark\">kicker</span><div><p>DEIN "
|
||||
"VEREINSJOURNAL</p><h2>Nachrichten</h2></div></div><div "
|
||||
"class=\"news-owner\"><div><strong>" +
|
||||
esc(data.at("club_name").get<std::string>()) + "</strong><p>" +
|
||||
esc(data.at("date").get<std::string>()) + "</p></div>" +
|
||||
crest(club(s), 88) + "</div></div>";
|
||||
content += "<div class=\"news-navigation\"><div class=\"news-tabs\" "
|
||||
"role=\"tablist\" aria-label=\"Postfach und Archiv\">";
|
||||
for (const auto &f : {std::string("inbox"), std::string("archive")})
|
||||
content +=
|
||||
btn("news-folder",
|
||||
"<span>" + std::string(f == "inbox" ? "Postfach" : "Archiv") +
|
||||
"</span><span class=\"news-tab-count\">" +
|
||||
std::to_string(data.at("counts").at(f).get<int>()) + "</span>",
|
||||
std::string("news-tab ") + (folder == f ? "active" : ""),
|
||||
"id=\"news-tab-" + f + "\" data-id=\"" + f +
|
||||
"\" role=\"tab\" aria-controls=\"news-feed\" aria-selected=\"" +
|
||||
(folder == f ? "true" : "false") + "\" tabindex=\"" +
|
||||
(folder == f ? "0" : "-1") + "\"");
|
||||
content += "</div><div class=\"news-search\"><span "
|
||||
"class=\"news-search-symbol\"><span></span></span><input "
|
||||
"id=\"news-search\" maxlength=\"120\" type=\"text\" "
|
||||
"aria-label=\"Nachrichten durchsuchen\" placeholder=\"Nachrichten "
|
||||
"durchsuchen\" value=\"" +
|
||||
esc(news_view_.value("query", "")) + "\"/></div>";
|
||||
content +=
|
||||
btn("news-read-all", glyph("check", 36) + "<span>Alle gelesen</span>",
|
||||
"news-read-all",
|
||||
"id=\"news-read-all\" " +
|
||||
std::string(data.at("can_read").get<bool>() ? "" : "disabled"));
|
||||
content += "</div><div id=\"news-feed\" class=\"news-feed\" "
|
||||
"role=\"tabpanel\" aria-labelledby=\"news-tab-" +
|
||||
folder + "\">" + render_news_feed(data) +
|
||||
"</div><div class=\"news-bottom\"><p id=\"news-feedback\" "
|
||||
"role=\"status\">";
|
||||
const auto feedback = news_view_.value("feedback", "");
|
||||
content += esc(
|
||||
feedback.empty()
|
||||
? std::to_string(data.at("items").size()) +
|
||||
(data.at("items").size() == 1 ? " Nachricht" : " Nachrichten") +
|
||||
" · " + std::to_string(data.at("shown_unread").get<int>()) +
|
||||
" ungelesen"
|
||||
: feedback);
|
||||
content +=
|
||||
"</p>" +
|
||||
btn("news-undo",
|
||||
glyph("undo", 32) + "<span>Rückgängig</span>",
|
||||
"news-undo",
|
||||
"id=\"news-undo\" style=\"display:" +
|
||||
std::string(news_undo_.is_array() && !news_undo_.empty()
|
||||
? "flex"
|
||||
: "none") +
|
||||
";\"") +
|
||||
"</div></div>";
|
||||
if (!news_dialog_.empty()) {
|
||||
const auto all = demo_news_view(*s.world, s.state, folder, "");
|
||||
const auto &items = all.at("items");
|
||||
const auto found =
|
||||
std::find_if(items.begin(), items.end(),
|
||||
[&](const auto &m) { return m.at("id") == news_dialog_; });
|
||||
if (found != items.end())
|
||||
content += render_news_detail(*found);
|
||||
else
|
||||
news_dialog_.clear();
|
||||
}
|
||||
return "<div class=\"window news-tablet\"><div "
|
||||
"class=\"news-camera\"></div><div class=\"titlebar\">" +
|
||||
btn("close", "<span>×</span>", "close",
|
||||
"id=\"window-close\" aria-label=\"Fenster schließen\"") +
|
||||
"<h1>Nachrichten</h1></div><div class=\"content\">" + content +
|
||||
"</div><div class=\"news-screen-footer\"><span>Fiktive "
|
||||
"Vereinsmeldungen · Modern UI "
|
||||
"Demo</span><span>OpenBMH</span></div><div "
|
||||
"class=\"news-homebar\"></div></div>";
|
||||
}
|
||||
|
||||
std::string App::render_news_widget(const Json &object) {
|
||||
const auto s = snap();
|
||||
const auto data = demo_news_view(*s.world, s.state, "inbox", "");
|
||||
if (!data.value("ok", false))
|
||||
return "";
|
||||
const bool small =
|
||||
object.value("w", 472) <= 472 && object.value("h", 472) <= 472;
|
||||
const int limit = small ? 2 : 3;
|
||||
std::string out =
|
||||
btn("open:news",
|
||||
"<span>Postfach · " + std::to_string(data.at("unread").get<int>()) +
|
||||
" ungelesen</span><span class=\"news-widget-arrow\">" +
|
||||
glyph("arrow-diagonal", 26) + "</span>",
|
||||
"news-widget-heading",
|
||||
"id=\"desktop-news-open\" data-interactive=\"true\" "
|
||||
"aria-label=\"Nachrichten öffnen\"");
|
||||
out += "<div class=\"news-widget-feed\">";
|
||||
int count = 0;
|
||||
// Gold's widget uses source order, independently of the open tablet's folder,
|
||||
// search and sorted list. Resolve templates through the same read projection.
|
||||
for (const auto &raw : s.state.at("news").at("items")) {
|
||||
if (raw.at("folder") != "inbox")
|
||||
continue;
|
||||
const auto &shown = data.at("items");
|
||||
const auto found =
|
||||
std::find_if(shown.begin(), shown.end(),
|
||||
[&](const auto &m) { return m.at("id") == raw.at("id"); });
|
||||
if (found == shown.end())
|
||||
continue;
|
||||
out += "<div class=\"news-widget-row" +
|
||||
std::string(found->at("read").get<bool>() ? "" : " unread") +
|
||||
"\"><span class=\"news-widget-dot\"></span><div "
|
||||
"class=\"news-widget-copy\"><span class=\"news-widget-date\">" +
|
||||
esc(found->at("display_date").get<std::string>()) +
|
||||
"</span><span class=\"news-widget-title\">" +
|
||||
esc(found->at("title").get<std::string>()) + "</span></div></div>";
|
||||
if (++count == limit)
|
||||
break;
|
||||
}
|
||||
return out + "</div>";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// Included within App: navigation is transient and has no save authority.
|
||||
Json news_view_ = {{"folder", "inbox"}, {"query", ""}, {"feedback", ""}};
|
||||
std::string news_dialog_, news_return_focus_;
|
||||
Json news_undo_;
|
||||
uint64_t news_undo_revision_ = 0;
|
||||
std::shared_ptr<const Catalog> news_world_;
|
||||
const DataSource *news_source_ = nullptr;
|
||||
float news_scroll_ = 0.f;
|
||||
bool news_restore_scroll_ = false;
|
||||
std::string render_news();
|
||||
std::string render_news_widget(const Json &);
|
||||
std::string render_news_feed(const Json &);
|
||||
std::string render_news_art(const Json &, double size = 150);
|
||||
std::string render_news_detail(const Json &);
|
||||
bool dispatch_news(const std::string &, Rml::Element *);
|
||||
bool news_event(Rml::Event &);
|
||||
void reset_news(bool preserve_view = false);
|
||||
void news_before_render();
|
||||
void news_after_render();
|
||||
void news_update_feed();
|
||||
bool news_commit(const Json &, const std::string &, bool remember = true);
|
||||
@@ -2,11 +2,12 @@
|
||||
namespace {
|
||||
bool report_picker_menu(const std::string &menu) {
|
||||
return menu == "tables" || menu == "strengths" || menu == "league" ||
|
||||
menu == "other" || menu == "calendar" || menu == "friendlies" || menu == "almanac" || menu == "scorers";
|
||||
menu == "other" || menu == "calendar" || menu == "friendlies" || menu == "almanac" || menu == "scorers" || menu == "statistics";
|
||||
}
|
||||
bool report_picker_kind(const std::string &kind) {
|
||||
return kind == "country" || kind == "league" || kind == "filter" ||
|
||||
kind == "competition" || kind == "year" || kind == "scorer-scope";
|
||||
kind == "competition" || kind == "year" || kind == "scorer-scope" ||
|
||||
kind == "stat-section" || kind == "stat-category" || kind == "stat-country" || kind == "winner-country";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -15,6 +16,8 @@ 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 (menu_ == "statistics")
|
||||
return statistics_picker_items(kind);
|
||||
if (kind == "scorer-scope") {
|
||||
if (menu_ == "scorers")
|
||||
items = {{"0", "Gesamte Liga"}, {"1", "Eigener Verein"}};
|
||||
@@ -70,6 +73,8 @@ App::report_picker_items(const std::string &kind) const {
|
||||
std::string App::report_picker_value(const std::string &kind) const {
|
||||
if (!world_ || !report_picker_kind(kind))
|
||||
return {};
|
||||
if (menu_ == "statistics")
|
||||
return statistics_picker_value(kind);
|
||||
if (kind == "year") return std::to_string(almanac_year_);
|
||||
if (menu_ == "scorers") {
|
||||
if (kind == "scorer-scope") return std::to_string(rankings_view_.value("scope", 0));
|
||||
@@ -104,7 +109,11 @@ std::string App::report_picker(const std::string &kind, double left,
|
||||
const bool open = report_picker_ == kind;
|
||||
const std::string menu_label = kind == "country" ? "Land"
|
||||
: kind == "league" ? "Liga"
|
||||
: kind == "filter" ? "Spiele" : kind == "year" ? "Kalenderjahr" : kind == "scorer-scope" ? "Bereich" : "Wettbewerb";
|
||||
: kind == "filter" ? "Spiele" : kind == "year" ? "Kalenderjahr" : kind == "scorer-scope" ? "Bereich"
|
||||
: kind == "stat-section" ? "Statistik"
|
||||
: kind == "stat-category" ? "Wettbewerbsgruppe"
|
||||
: kind == "winner-country" ? "Land"
|
||||
: kind == "stat-country" ? "Statistikbereich" : "Wettbewerb";
|
||||
std::string out =
|
||||
"<div class=\"report-picker-wrapper\" data-report-picker=\"" + kind +
|
||||
"\" style=\"left:" + scalar(left) + "dp;width:" + scalar(width) + "dp;\">" +
|
||||
@@ -198,7 +207,9 @@ bool App::report_picker_action(const std::string &action, Rml::Element *target)
|
||||
? Json(std::stoi(value)) : Json(value);
|
||||
close_report_picker(false);
|
||||
// Commit even an unchanged country: the Gold resets its first league.
|
||||
if (kind == "year" && menu_ == "almanac") {
|
||||
if (menu_ == "statistics") {
|
||||
statistics_picker_select(kind, value);
|
||||
} else if (kind == "year" && menu_ == "almanac") {
|
||||
almanac_year_ = std::stoi(value);
|
||||
almanac_day_.clear();
|
||||
almanac_match_.clear();
|
||||
|
||||
@@ -154,9 +154,7 @@ std::string App::widget(const std::string &id, const Json &o) {
|
||||
"style=\"font-size:80dp;padding-top:85dp;\">" +
|
||||
std::to_string(s.state.value("fans", 67)) + " %</p>";
|
||||
if (id == "news")
|
||||
return heading("Nachrichten") +
|
||||
"<p style=\"padding:90dp 30dp;font-size:34dp;\">Willkommen zur "
|
||||
"neuen Saison.</p>";
|
||||
return render_news_widget(o);
|
||||
if (id == "results")
|
||||
return heading("Letzte sechs Spiele") +
|
||||
"<div class=\"row\" style=\"padding:100dp "
|
||||
@@ -872,6 +870,8 @@ std::string App::render_save(bool save) {
|
||||
"</div>";
|
||||
}
|
||||
std::string App::render_menu(const std::string &key) {
|
||||
if (key == "news")
|
||||
return "<div class=\"fill\" style=\"z-index:100;\">" + render_news() + "</div>";
|
||||
std::string body;
|
||||
if (key == "tables" || key == "strengths" || key == "league" || key == "other")
|
||||
body = render_reports(key);
|
||||
@@ -897,6 +897,8 @@ std::string App::render_menu(const std::string &key) {
|
||||
body = render_highscore();
|
||||
else if (key == "stadium")
|
||||
body = render_stadium();
|
||||
else if (key == "statistics")
|
||||
body = render_statistics();
|
||||
else if (key == "environment")
|
||||
body = render_environment();
|
||||
else if (key == "advertising")
|
||||
@@ -933,5 +935,6 @@ std::string App::render_menu(const std::string &key) {
|
||||
if (labels.contains(bank_view_)) heading += " · " + labels.at(bank_view_);
|
||||
}
|
||||
return "<div class=\"fill\" style=\"z-index:100;\">" +
|
||||
window(heading, body) + "</div>";
|
||||
window(heading, body) +
|
||||
(key == "statistics" ? render_statistics_trophy() : "") + "</div>";
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
bool App::statistics_picker_select(const std::string &kind,const std::string &value) {
|
||||
if(menu_!="statistics"||!source_||statistics_trophy_.contains("asset"))return false;
|
||||
const auto items=statistics_picker_items(kind);
|
||||
if(std::none_of(items.begin(),items.end(),[&](const auto &item){return item.first==value;}))return false;
|
||||
statistics_remember_scroll();
|
||||
if(kind=="winner-country")statistics_view_["winner_country"]=value;
|
||||
else {
|
||||
const auto field=kind=="stat-section"?"section":kind=="stat-category"?"category":"country";
|
||||
statistics_view_[field]=std::stoi(value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool App::statistics_action(const std::string &action,Rml::Element *target) {
|
||||
if(!action.starts_with("statistics-"))return false;
|
||||
if(menu_!="statistics"||!source_)return true;
|
||||
if(action=="statistics-trophy-close") {
|
||||
if(statistics_trophy_.contains("asset")) {
|
||||
focus_after_render_=statistics_trophy_.value("return_focus","");statistics_trophy_=Json::object();refresh();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(!target||statistics_trophy_.contains("asset")||!report_detail_kind_.empty()||!report_picker_.empty())return true;
|
||||
const auto data=source_->read_statistics(statistics_view_);
|
||||
if(!data.value("ok",false))return true;
|
||||
statistics_remember_scroll();
|
||||
if(action=="statistics-absolute"&&data.at("section")==2)statistics_view_["absolute"]=!data.at("absolute").get<bool>();
|
||||
else if(action=="statistics-curve"&&data.at("section")==2) {
|
||||
const int index=target->GetAttribute<int>("data-index",-1);if(index<0||index>=6)return true;
|
||||
auto curves=data.at("curves");curves[size_t(index)]=!curves[size_t(index)].get<bool>();statistics_view_["curves"]=curves;
|
||||
} else if(action=="statistics-trophy") {
|
||||
const int r=target->GetAttribute<int>("data-row",-2),c=target->GetAttribute<int>("data-column",-1);
|
||||
if(c<0||c>=int(data.at("columns").size()))return true;
|
||||
Json entry;
|
||||
if(r==-1&&data.at("section")==7)entry=data.at("columns")[size_t(c)];
|
||||
else if(r>=0&&r<int(data.at("rows").size())&&data.at("section")==4)entry=data.at("rows")[size_t(r)].at("cells")[size_t(c)];
|
||||
else return true;
|
||||
const auto trophy=entry.value("trophy","");const auto asset=statistics_trophy_asset(trophy);
|
||||
if(asset.empty()||!entry.contains("winner")||!entry.at("winner").is_string())return true;
|
||||
const auto winner=entry.at("winner").get<std::string>();
|
||||
if(!world_||(!world_->find("clubs",winner)&&!world_->find("nations",winner)))return true;
|
||||
const std::map<std::string,std::string> captions{{"championship","Deutscher Meister"},{"dfb-cup","DFB-Pokalsieger"},{"supercup","Supercup-Sieger"},{"champions-league","Champions-League-Sieger"},{"cup-winners","Europapokalsieger"},{"uefa-cup","UEFA-Cup-Sieger"},{"world-cup","Weltmeister"},{"european-championship","Europameister"},{"indoor-masters","Hallenmasters-Sieger"},{"en-premier-league","Englischer Meister"},{"en-fa-cup","FA-Cup-Sieger"},{"fr-ligue-1","Französischer Meister"},{"fr-coupe-de-france","Coupe-de-France-Sieger"},{"it-serie-a","Italienischer Meister"},{"it-coppa-italia","Coppa-Italia-Sieger"},{"es-la-liga","Spanischer Meister"},{"es-copa-del-rey","Copa-del-Rey-Sieger"}};
|
||||
const auto title=captions.contains(trophy)?captions.at(trophy):entry.value("text","Sieger");
|
||||
statistics_trophy_={{"asset",asset},{"title",title},{"winner",winner},{"season",entry.value("season",data.value("previous_season",""))},{"return_focus",target->GetId()}};
|
||||
focus_after_render_="statistics-trophy-close";
|
||||
} else return true;
|
||||
if(action!="statistics-trophy")focus_after_render_=target->GetId();
|
||||
refresh();return true;
|
||||
}
|
||||
bool App::statistics_event(Rml::Event &event) {
|
||||
if(menu_!="statistics")return false;
|
||||
const auto type=event.GetType();const bool keyboard=type=="keydown";
|
||||
const auto key=keyboard?Rml::Input::KeyIdentifier(event.GetParameter<int>("key_identifier",0)):Rml::Input::KI_UNKNOWN;
|
||||
if(statistics_trophy_.contains("asset")) {
|
||||
if((keyboard&&key==Rml::Input::KI_ESCAPE)||(type=="mouseup"&&event.GetParameter<int>("button",0)==1)) {
|
||||
statistics_action("statistics-trophy-close",nullptr);event.StopImmediatePropagation();return true;
|
||||
}
|
||||
if(keyboard&&key==Rml::Input::KI_TAB) {
|
||||
if(auto *close=element("statistics-trophy-close"))close->Focus(true);
|
||||
event.StopImmediatePropagation();return true;
|
||||
}
|
||||
bool inside=false;
|
||||
for(auto *node=event.GetTargetElement();node&&node!=doc_;node=node->GetParentNode())if(node->GetId()=="statistics-trophy-layer")inside=true;
|
||||
if(!inside&&(type=="click"||type=="dblclick"||type=="mousedown"||type=="mouseup"||keyboard)) {event.StopImmediatePropagation();return true;}
|
||||
return false;
|
||||
}
|
||||
if(!keyboard||key!=Rml::Input::KI_TAB||!report_detail_kind_.empty()||!report_picker_.empty())return false;
|
||||
Rml::ElementList controls;
|
||||
if(auto *canvas=element("canvas"))canvas->QuerySelectorAll(controls,".window button,.window input,.window select");
|
||||
std::erase_if(controls,[](auto *node){return !node->IsVisible(true)||node->HasAttribute("disabled");});
|
||||
if(!controls.empty()) {
|
||||
const auto found=std::find(controls.begin(),controls.end(),context_->GetFocusElement());
|
||||
const int old=found==controls.end()?-1:int(found-controls.begin()),count=int(controls.size());
|
||||
const bool back=event.GetParameter<int>("shift_key",0)!=0;
|
||||
const int next=old<0?(back?count-1:0):(old+(back?-1:1)+count)%count;
|
||||
controls[size_t(next)]->Focus(true);controls[size_t(next)]->ScrollIntoView({Rml::ScrollAlignment::Nearest});dirty_=true;
|
||||
}
|
||||
event.StopImmediatePropagation();return true;
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
namespace {
|
||||
std::string statistics_text(const std::string &text) {
|
||||
return "<span class=\"statistics-cell-text\">"+esc(text)+"</span>";
|
||||
}
|
||||
std::string statistics_scroll_key(const Json &data) {
|
||||
return std::to_string(data.value("section",7))+":"+std::to_string(data.value("category",0))+":"+std::to_string(data.value("country",0))+":"+data.value("winner_country","");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<std::pair<std::string,std::string>> App::statistics_picker_items(const std::string &kind) const {
|
||||
std::vector<std::pair<std::string,std::string>> result;
|
||||
if(!source_)return result;
|
||||
const auto data=source_->read_statistics(statistics_view_);
|
||||
if(!data.value("ok",false))return result;
|
||||
const int section=data.at("section").get<int>();
|
||||
if(kind=="stat-section") {
|
||||
const auto &names=data.at("sections");
|
||||
for(size_t i=0;i<names.size();++i)result.emplace_back(std::to_string(i),names[i].get<std::string>());
|
||||
} else if(kind=="stat-category"&§ion==7)result={{"0","National"},{"1","International"},{"5","Hallenmasters"}};
|
||||
else if(kind=="winner-country"&§ion==7&&data.at("category")==0) {
|
||||
for(const auto &nation:data.at("countries"))result.emplace_back(nation.at("id").get<std::string>(),nation.at("name").get<std::string>());
|
||||
} else if(kind=="stat-country"&§ion==3) {
|
||||
for(const auto &nation:data.at("eternal_countries"))
|
||||
result.emplace_back(std::to_string(nation.at("value").get<int>()),nation.at("name").get<std::string>());
|
||||
} else if(kind=="stat-country"&§ion==4) {
|
||||
const auto &names=data.at("world_views");
|
||||
for(size_t i=0;i<names.size();++i)result.emplace_back(std::to_string(i),names[i].get<std::string>());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::string App::statistics_picker_value(const std::string &kind) const {
|
||||
if(!source_)return {};
|
||||
const auto data=source_->read_statistics(statistics_view_);
|
||||
if(!data.value("ok",false))return {};
|
||||
if(kind=="winner-country")return data.value("winner_country","");
|
||||
const auto key=kind=="stat-section"?"section":kind=="stat-category"?"category":"country";
|
||||
return std::to_string(data.value(key,0));
|
||||
}
|
||||
void App::statistics_remember_scroll() {
|
||||
if(auto *list=element("statistics-list")) {
|
||||
const auto key=list->GetAttribute<Rml::String>("data-scroll-key","");
|
||||
if(!key.empty())statistics_scrolls_[key]=list->GetScrollTop();
|
||||
}
|
||||
}
|
||||
void App::statistics_restore_scroll() {
|
||||
if(menu_!="statistics"||!statistics_restore_pending_)return;
|
||||
statistics_restore_pending_=false;
|
||||
if(auto *list=element("statistics-list")) {
|
||||
const auto key=list->GetAttribute<Rml::String>("data-scroll-key","");
|
||||
if(const auto i=statistics_scrolls_.find(key);i!=statistics_scrolls_.end()&&i->is_number())list->SetScrollTop(i->get<float>());
|
||||
}
|
||||
}
|
||||
std::string App::statistics_trophy_asset(const std::string &id) const {
|
||||
if(id.empty())return {};
|
||||
// Competition pictures belong to world content. The deterministic demo uses
|
||||
// semantic trophy roles; an engine adapter can supply a logical asset key.
|
||||
// Fixed prototype artwork is only a fallback, never an OBW requirement.
|
||||
if (world_) {
|
||||
if (id.starts_with("asset:")) {
|
||||
if (world_->find("assets", id)) return id;
|
||||
if (world_->find("assets", "asset:comp/generic/trophy"))
|
||||
return "asset:comp/generic/trophy";
|
||||
return {};
|
||||
}
|
||||
const auto key = "asset:comp/" + id + "/trophy";
|
||||
if (world_->find("assets", key)) return key;
|
||||
}
|
||||
if(gui_.contains("trophy."+id))return "trophy."+id;
|
||||
for(const auto &[key,item]:gui_.items().items()) {
|
||||
if(!item.is_object())continue;
|
||||
const auto file=item.value("file","");
|
||||
const auto base=fs::path(file).filename().string();
|
||||
if(base==id+"-v1.png"||base==id+"-v2.png")return key;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
std::string App::render_statistics() {
|
||||
statistics_remember_scroll();
|
||||
if(!source_||!world_)return "<p>Statistiken sind nicht verfügbar.</p>";
|
||||
const auto data=source_->read_statistics(statistics_view_);
|
||||
if(!data.value("ok",false))return "<p>Statistiken sind nicht verfügbar.</p>";
|
||||
const int section=data.at("section").get<int>(),category=data.at("category").get<int>();
|
||||
const bool winners=section==7,minutes=section==8,finals=section==4&&data.at("country")==3;
|
||||
std::string out="<section class=\"statistics-native statistics-section-"+std::to_string(section)+(finals?" statistics-finals":"")+"\"><div class=\"statistics-panel\"><h2 class=\"statistics-heading\">"+esc(data.at("title").get<std::string>())+"</h2>";
|
||||
if(winners)out+="<p class=\"statistics-winner-note\">"+statistics_text(data.value("note",""))+"</p>";
|
||||
if(section==2) {
|
||||
std::string svg="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 3400 780' width='2496' height='812' preserveAspectRatio='none'><path d='M30 10V730H3380' fill='none' stroke='"+std::string(theme_=="silver"?"#627c8f":"#7d9aae")+"' stroke-width='3'/>";
|
||||
const auto &series=data.at("series");
|
||||
for(const auto &curve:series)if(curve.at("active")==true) {
|
||||
svg+="<polyline points='";
|
||||
for(const auto &point:curve.at("points"))svg+=scalar(point[0].get<double>())+","+scalar(point[1].get<double>())+" ";
|
||||
svg+="' fill='none' stroke='"+curve.at("color").get<std::string>()+"' stroke-width='7'/>";
|
||||
}
|
||||
svg+="</svg>";
|
||||
out+="<div class=\"statistics-graph\" role=\"img\" aria-label=\"Sechs fiktive Verlaufsreihen · "+std::string(data.at("absolute")==true?"absolut":"relativ")+"\">"+picture(renderer_.diagram("statistics-analytics",svg,5100),2496,812)+"</div><div class=\"statistics-series\">";
|
||||
for(size_t k=0;k<series.size();++k) {
|
||||
const auto &curve=series[k];const bool active=curve.at("active").get<bool>();
|
||||
out+=btn("statistics-curve","<span class=\"statistics-series-dot\" style=\"background-color:"+curve.at("color").get<std::string>()+";\"></span><span class=\"statistics-series-label\">"+esc(curve.at("name").get<std::string>())+"</span>","statistics-action statistics-series-button",
|
||||
"id=\"statistics-curve-"+std::to_string(k)+"\" data-index=\""+std::to_string(k)+"\" aria-pressed=\""+(active?"true":"false")+"\" style=\""+rect(double(k%3)*854.666666667,double(k/3)*100,836.666666667,82)+"\"");
|
||||
}
|
||||
out+="</div>";
|
||||
} else {
|
||||
const auto &columns=data.at("columns"),&rows=data.at("rows");
|
||||
const size_t count=columns.size();
|
||||
const double table_width=2548;
|
||||
const double first=minutes?table_width*.12:winners?190:table_width*.4;
|
||||
const double rest=count>1?(table_width-first)/double(count-1):0;
|
||||
const auto width=[&](size_t column){return finals?(column==1?160:1194):column?rest:first;};
|
||||
const bool trophies=winners&&std::any_of(columns.begin(),columns.end(),[](const Json &column){return !column.value("trophy","").empty();});
|
||||
const double head_height=trophies?200:winners?78:66;
|
||||
out+="<div id=\"statistics-list\" class=\"statistics-list "+std::string(winners?"statistics-winners ":"")+(minutes?"statistics-minutes ":"")+"\" data-scroll-key=\""+esc(statistics_scroll_key(data))+"\"><div class=\"statistics-table\" role=\"table\"><div class=\"statistics-row statistics-head\" role=\"row\" style=\"height:"+scalar(head_height)+"dp;\">";
|
||||
for(size_t c=0;c<count;++c) {
|
||||
const auto &column=columns[c];
|
||||
const auto trophy=column.value("trophy","");const auto asset=statistics_trophy_asset(trophy);
|
||||
std::string heading=statistics_text(column.at("text").get<std::string>());
|
||||
if(column.contains("round"))heading="<span class=\"statistics-fixture-round\"><span>"+esc(column.at("round").get<std::string>())+"</span></span>"+heading;
|
||||
if(!trophy.empty()) {
|
||||
const auto art=asset.empty()?std::string():"<span class=\"statistics-trophy-icon\">"+img(asset,105,105,asset.starts_with("asset:"),false,320)+"</span>";
|
||||
heading=btn("statistics-trophy",art+"<span class=\"statistics-trophy-label\">"+esc(column.at("text").get<std::string>())+"</span>","statistics-trophy-heading",
|
||||
"id=\"statistics-trophy-heading-"+std::to_string(c)+"\" data-column=\""+std::to_string(c)+"\" data-row=\"-1\" aria-label=\""+esc(column.at("text").get<std::string>()+" · Siegerbild ansehen")+"\" "+(asset.empty()||!column.contains("winner")?"disabled":""));
|
||||
}
|
||||
out+="<div class=\"statistics-cell statistics-col-"+std::to_string(c)+"\" role=\"columnheader\" style=\"width:"+scalar(width(c))+"dp;height:"+scalar(head_height)+"dp;\">"+heading+"</div>";
|
||||
}
|
||||
out+="</div>";
|
||||
for(size_t r=0;r<rows.size();++r) {
|
||||
const auto &cells=rows[r].at("cells");
|
||||
const bool world_trophies=section==4&&(data.at("country")==0||data.at("country")==5);
|
||||
const double row_height=minutes?200:winners?100:world_trophies?78:66;
|
||||
out+="<div class=\"statistics-row "+std::string(r%2==0?"striped":"")+"\" role=\"row\" style=\"height:"+scalar(row_height)+"dp;\">";
|
||||
for(size_t c=0;c<cells.size();++c) {
|
||||
const auto &value=cells[c];const auto title=value.value("text","");const auto token=std::to_string(r)+"-"+std::to_string(c);
|
||||
std::string content;
|
||||
if(value.contains("player")) {
|
||||
content="<span class=\"statistics-player-prefix\">"+esc(value.value("prefix",""))+"</span>"+btn("player-detail",statistics_text(title),"statistics-player-link","id=\"statistics-player-"+token+"\" data-id=\""+esc(value.at("player").get<std::string>())+"\" data-club=\""+esc(value.value("club",""))+"\" title=\""+esc(title)+"\"");
|
||||
} else if(value.contains("club")) {
|
||||
const auto id=value.at("club").get<std::string>();const auto *club=world_->find("clubs",id);const bool reverse=section==1&&c==1;
|
||||
const auto name="<span class=\"report-club-name\">"+esc(title)+"</span>",badge=crest(club,42);
|
||||
content=btn("club-detail",reverse?name+badge:badge+name,"report-club-button statistics-club "+std::string(reverse?"name-first ":"")+(data.at("club")==id?"current":""),"id=\"statistics-club-"+token+"\" data-id=\""+esc(id)+"\" title=\""+esc(title)+"\"");
|
||||
} else if(value.contains("trophy")) {
|
||||
const auto asset=statistics_trophy_asset(value.at("trophy").get<std::string>());
|
||||
content=btn("statistics-trophy",(asset.empty()?std::string():"<span class=\"statistics-world-trophy-art\">"+img(asset,58,58,asset.starts_with("asset:"),false,192)+"</span>")+statistics_text(title),"statistics-world-trophy","id=\"statistics-trophy-row-"+token+"\" data-row=\""+std::to_string(r)+"\" data-column=\""+std::to_string(c)+"\" "+(asset.empty()?"disabled":""));
|
||||
} else content=statistics_text(value.value("prefix","")+title);
|
||||
if(value.contains("round"))content="<span class=\"statistics-fixture-round\"><span>"+esc(value.at("round").get<std::string>())+"</span></span>"+content;
|
||||
out+="<div class=\"statistics-cell statistics-col-"+std::to_string(c)+" "+esc(value.value("tone",""))+"\" role=\"cell\" style=\"width:"+scalar(width(c))+"dp;height:"+scalar(row_height)+"dp;\">"+content+"</div>";
|
||||
}
|
||||
out+="</div>";
|
||||
}
|
||||
if(rows.empty())out+="<p class=\"statistics-empty\">"+statistics_text("Keine Statistikdaten für diese Auswahl vorhanden.")+"</p>";
|
||||
out+="</div></div>";
|
||||
}
|
||||
out+="</div><nav class=\"report-footer statistics-footer\" aria-label=\"Statistik und Auswahl\">";
|
||||
const bool extra=winners||section==2||section==3||section==4;
|
||||
const bool three=winners&&category==0;
|
||||
const double width=three?854.666666667:extra?1294:2612;
|
||||
out+=report_picker("stat-section",0,width);
|
||||
if(winners) {
|
||||
out+=report_picker("stat-category",three?878.666666667:1318,width);
|
||||
if(three)out+=report_picker("winner-country",1757.333333333,width);
|
||||
} else if(section==3||section==4)out+=report_picker("stat-country",1318,1294);
|
||||
else if(section==2)out+=btn("statistics-absolute",statistics_text(data.at("absolute")==true?"ABS.":"REL."),"statistics-action statistics-absolute","id=\"statistics-absolute\" aria-label=\""+std::string(data.at("absolute")==true?"Absolute":"Relative")+" Darstellung; zum Umschalten klicken\" style=\""+rect(1318,0,1294,94)+"\"");
|
||||
statistics_restore_pending_=true;
|
||||
return out+"</nav></section>"+render_report_detail();
|
||||
}
|
||||
|
||||
std::string App::render_statistics_trophy() {
|
||||
if(!statistics_trophy_.contains("asset"))return {};
|
||||
const auto asset=statistics_trophy_.at("asset").get<std::string>();
|
||||
const bool world_image = asset.starts_with("asset:");
|
||||
if(world_image ? (!world_ || !world_->find("assets", asset)) : !gui_.contains(asset))return {};
|
||||
const auto winner=statistics_trophy_.value("winner","");
|
||||
const auto *club=world_?world_->find("clubs",winner):nullptr;
|
||||
const auto name=club?world_->name(*club):world_?world_->nation_name(winner):"—";
|
||||
const auto viewport=context_->GetDimensions();
|
||||
const double w=viewport.x/scale_,h=viewport.y/scale_,stage=std::min(w,h*16/9);
|
||||
const double art=std::min({h*.61,1200/scale_,w*.82});
|
||||
const double heading=std::clamp(h*.022,17/scale_,48/scale_),team=std::clamp(h*.035,25/scale_,76/scale_);
|
||||
const double season_font=std::clamp(h*.0145,13/scale_,30/scale_),button_font=std::clamp(h*.0165,14/scale_,36/scale_);
|
||||
const double heading_h=heading*1.25,team_h=team*1.5,season_h=season_font*1.25;
|
||||
const double button_h=std::max(h*.046,button_font*1.25+h*.018+2/scale_);
|
||||
const double gap=h*.018,copy_gap=h*.012;
|
||||
const double total=art+heading_h+team_h+season_h+2*copy_gap+button_h+2*gap+h*.008;
|
||||
const double top=(h-total)/2,heading_top=top+art+gap,team_top=heading_top+heading_h+copy_gap;
|
||||
const double season_top=team_top+team_h+copy_gap,button_top=season_top+season_h+gap+h*.008;
|
||||
return "<div id=\"statistics-trophy-layer\" class=\"statistics-trophy-layer\" style=\""+rect(-canvas_left_/scale_,-canvas_top_/scale_,w,h)+"\" role=\"dialog\" aria-modal=\"true\" aria-labelledby=\"statistics-trophy-title\"><div class=\"statistics-trophy-backdrop\">"+img("background",w,h,false,true,2048)+"</div><div class=\"statistics-trophy-shade\"></div><div class=\"statistics-trophy-large\" style=\""+rect((w-art)/2,top,art,art)+"\">"+img(asset,art,art,world_image,false,1600)+"</div><h2 id=\"statistics-trophy-title\" style=\""+rect((w-stage)/2,heading_top,stage,heading_h)+"font-size:"+scalar(heading)+"dp;line-height:"+scalar(heading_h)+"dp;\">"+esc(statistics_trophy_.value("title","Sieger"))+"</h2><div class=\"statistics-trophy-winner\" style=\""+rect((w-stage)/2,team_top,stage,team_h)+"font-size:"+scalar(team)+"dp;\">"+(club?crest(club,team_h):"")+"<span>"+esc(name)+"</span></div><p class=\"statistics-trophy-season\" style=\""+rect((w-stage)/2,season_top,stage,season_h)+"font-size:"+scalar(season_font)+"dp;line-height:"+scalar(season_h)+"dp;\">"+statistics_text(statistics_trophy_.value("season",""))+"</p>"+btn("statistics-trophy-close","<span>Weiter</span>","statistics-trophy-close","id=\"statistics-trophy-close\" style=\""+rect((w-h*.17)/2,button_top,h*.17,button_h)+"font-size:"+scalar(button_font)+"dp;border-radius:"+scalar(h*.011)+"dp;\"")+"</div>";
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
Json statistics_view_ = Json::object();
|
||||
Json statistics_trophy_ = Json::object();
|
||||
Json statistics_scrolls_ = Json::object();
|
||||
bool statistics_restore_pending_ = false;
|
||||
std::string render_statistics();
|
||||
std::vector<std::pair<std::string, std::string>> statistics_picker_items(const std::string &) const;
|
||||
std::string statistics_picker_value(const std::string &) const;
|
||||
bool statistics_picker_select(const std::string &, const std::string &);
|
||||
bool statistics_action(const std::string &, Rml::Element *);
|
||||
bool statistics_event(Rml::Event &);
|
||||
void statistics_remember_scroll();
|
||||
void statistics_restore_scroll();
|
||||
std::string statistics_trophy_asset(const std::string &) const;
|
||||
std::string render_statistics_trophy();
|
||||
+49
-2
@@ -17,6 +17,8 @@
|
||||
#include "modern/ui/demo_career_reports.hpp"
|
||||
#include "modern/ui/demo_rankings.hpp"
|
||||
#include "modern/ui/demo_stadium.hpp"
|
||||
#include "modern/ui/demo_statistics.hpp"
|
||||
#include "modern/ui/demo_news.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include "modern/ui/gui_assets.hpp"
|
||||
#include "modern/ui/model.hpp"
|
||||
@@ -374,6 +376,8 @@ class App final : public Rml::EventListener {
|
||||
uint64_t stadium_revision_ = 0;
|
||||
std::shared_ptr<const Catalog> stadium_world_;
|
||||
const DataSource *stadium_source_ = nullptr;
|
||||
#include "modern/ui/statistics_state.inc"
|
||||
#include "modern/ui/news_state.inc"
|
||||
float device_ratio_ = 1.f;
|
||||
Json desktop_ = Json::object(), saved_desktop_ = Json::object(),
|
||||
options_ = Json::object();
|
||||
@@ -593,6 +597,9 @@ class App final : public Rml::EventListener {
|
||||
transfer_dialog_.clear(); transfer_picker_.clear(); transfer_notice_.clear(); transfer_return_focus_.clear();
|
||||
transfer_poach_league_.clear(); transfer_poach_club_.clear(); transfer_world_.reset(); transfer_revision_ = 0;
|
||||
career_view_ = Json::object(); rankings_view_ = Json::object();
|
||||
statistics_view_ = Json::object(); statistics_trophy_ = Json::object();
|
||||
statistics_scrolls_ = Json::object();
|
||||
reset_news();
|
||||
stadium_view_ = Json::object(); stadium_revision_ = 0;
|
||||
stadium_world_.reset(); stadium_source_ = nullptr;
|
||||
advertising_selected_ = -1; advertising_offer_ = 0; advertising_revision_ = 0; advertising_world_.reset();
|
||||
@@ -836,13 +843,17 @@ class App final : public Rml::EventListener {
|
||||
ticket_->resize(device_ratio_);
|
||||
scale_chalk_shadow();
|
||||
dirty_ = true;
|
||||
if (statistics_trophy_.contains("asset"))
|
||||
needs_render_ = true;
|
||||
}
|
||||
void render() {
|
||||
news_before_render();
|
||||
fixture_half_width_ = -1.f;
|
||||
almanac_list_width_ = -1.f;
|
||||
// A window-close save prompt can leave a menu while its local dialog is
|
||||
// open. Never keep that menu's modal/input gate in the destination view.
|
||||
if (menu_ != last_rendered_menu_) {
|
||||
statistics_trophy_ = Json::object();
|
||||
planning_dialog_.clear();
|
||||
career_view_["dialog"] = ""; career_view_["return_focus"] = "";
|
||||
stadium_view_["dialog"] = ""; stadium_view_["draft"] = Json();
|
||||
@@ -901,7 +912,9 @@ class App final : public Rml::EventListener {
|
||||
markup += render_menu(menu_);
|
||||
setting_menu_markup_ = true;
|
||||
element("canvas")->SetInnerRML(markup);
|
||||
element("canvas")->SetProperty("overflow", statistics_trophy_.contains("asset") ? "visible" : "hidden");
|
||||
setting_menu_markup_ = false;
|
||||
news_after_render();
|
||||
if (!report_detail_kind_.empty() || !planning_dialog_.empty() || !contracts_dialog_.empty() || !toto_dialog_.empty() || !transfer_dialog_.empty() || !career_view_.value("dialog", "").empty() || !stadium_view_.value("dialog", "").empty()) {
|
||||
Rml::ElementList behind;
|
||||
doc_->QuerySelectorAll(behind,
|
||||
@@ -1058,6 +1071,7 @@ class App final : public Rml::EventListener {
|
||||
}
|
||||
if (menu_ == "transfer") transfer_restore_scroll();
|
||||
if (menu_ == "scorers" || menu_ == "fairplay") rankings_restore_scroll();
|
||||
if (menu_ == "statistics") statistics_restore_scroll();
|
||||
if (!report_option_to_scroll_.empty()) {
|
||||
if (auto *node = element(report_option_to_scroll_))
|
||||
node->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
|
||||
@@ -1079,7 +1093,7 @@ class App final : public Rml::EventListener {
|
||||
editor::images::encode_png({w, h, std::move(bytes)}));
|
||||
}
|
||||
void load_fonts() {
|
||||
bool regular = false, bold = false;
|
||||
bool regular = false, bold = false, italic = false, bold_italic = false;
|
||||
#ifdef _WIN32
|
||||
const char *base = std::getenv("WINDIR");
|
||||
if (base) {
|
||||
@@ -1090,6 +1104,12 @@ class App final : public Rml::EventListener {
|
||||
bold = Rml::LoadFontFace(rml::asset_path(folder / "segoeuib.ttf"),
|
||||
"Modern", Rml::Style::FontStyle::Normal,
|
||||
Rml::Style::FontWeight::Bold);
|
||||
italic = Rml::LoadFontFace(rml::asset_path(folder / "segoeuii.ttf"),
|
||||
"Modern", Rml::Style::FontStyle::Italic,
|
||||
Rml::Style::FontWeight::Normal);
|
||||
bold_italic = Rml::LoadFontFace(rml::asset_path(folder / "segoeuiz.ttf"),
|
||||
"Modern", Rml::Style::FontStyle::Italic,
|
||||
Rml::Style::FontWeight::Bold);
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
const fs::path system = "/System/Library/Fonts/SFNS.ttf";
|
||||
@@ -1100,6 +1120,15 @@ class App final : public Rml::EventListener {
|
||||
bold = Rml::LoadFontFace(rml::asset_path(system), "Modern",
|
||||
Rml::Style::FontStyle::Normal,
|
||||
Rml::Style::FontWeight::Bold);
|
||||
const fs::path slanted = "/System/Library/Fonts/SFNSItalic.ttf";
|
||||
if (fs::exists(slanted)) {
|
||||
italic = Rml::LoadFontFace(rml::asset_path(slanted), "Modern",
|
||||
Rml::Style::FontStyle::Italic,
|
||||
Rml::Style::FontWeight::Normal);
|
||||
bold_italic = Rml::LoadFontFace(rml::asset_path(slanted), "Modern",
|
||||
Rml::Style::FontStyle::Italic,
|
||||
Rml::Style::FontWeight::Bold);
|
||||
}
|
||||
}
|
||||
#else
|
||||
const fs::path fonts = fs::exists("/usr/share/fonts/truetype/liberation2")
|
||||
@@ -1112,6 +1141,12 @@ class App final : public Rml::EventListener {
|
||||
bold = Rml::LoadFontFace(
|
||||
rml::asset_path(fonts / "LiberationSans-Bold.ttf"), "Modern",
|
||||
Rml::Style::FontStyle::Normal, Rml::Style::FontWeight::Bold);
|
||||
italic = Rml::LoadFontFace(
|
||||
rml::asset_path(fonts / "LiberationSans-Italic.ttf"), "Modern",
|
||||
Rml::Style::FontStyle::Italic, Rml::Style::FontWeight::Normal);
|
||||
bold_italic = Rml::LoadFontFace(
|
||||
rml::asset_path(fonts / "LiberationSans-BoldItalic.ttf"), "Modern",
|
||||
Rml::Style::FontStyle::Italic, Rml::Style::FontWeight::Bold);
|
||||
}
|
||||
#endif
|
||||
if (!regular)
|
||||
@@ -1122,6 +1157,14 @@ class App final : public Rml::EventListener {
|
||||
bold = Rml::LoadFontFace(rml::asset_path(assets_ / "LatoLatin-Bold.ttf"),
|
||||
"Modern", Rml::Style::FontStyle::Normal,
|
||||
Rml::Style::FontWeight::Bold);
|
||||
if (!italic)
|
||||
italic = Rml::LoadFontFace(rml::asset_path(assets_ / "LatoLatin-Italic.ttf"),
|
||||
"Modern", Rml::Style::FontStyle::Italic,
|
||||
Rml::Style::FontWeight::Normal);
|
||||
if (!bold_italic)
|
||||
bold_italic = Rml::LoadFontFace(
|
||||
rml::asset_path(assets_ / "LatoLatin-BoldItalic.ttf"), "Modern",
|
||||
Rml::Style::FontStyle::Italic, Rml::Style::FontWeight::Bold);
|
||||
std::vector<fs::path> chalk_fonts;
|
||||
#ifdef _WIN32
|
||||
if (const char *windir = std::getenv("WINDIR")) {
|
||||
@@ -1143,7 +1186,7 @@ class App final : public Rml::EventListener {
|
||||
Rml::Style::FontStyle::Normal,
|
||||
Rml::Style::FontWeight::Normal))
|
||||
break;
|
||||
if (!regular || !bold)
|
||||
if (!regular || !bold || !italic || !bold_italic)
|
||||
throw std::runtime_error(
|
||||
"GUI-Schriften fehlen. Bitte das vollständige Paket entpacken.");
|
||||
}
|
||||
@@ -1334,6 +1377,10 @@ public:
|
||||
#include "modern/ui/rankings_controls.inc"
|
||||
#include "modern/ui/stadium_screens.inc"
|
||||
#include "modern/ui/stadium_controls.inc"
|
||||
#include "modern/ui/statistics_screens.inc"
|
||||
#include "modern/ui/statistics_controls.inc"
|
||||
#include "modern/ui/news_screens.inc"
|
||||
#include "modern/ui/news_controls.inc"
|
||||
} // namespace
|
||||
bool is_modern_world(const fs::path &file) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user