262 lines
12 KiB
C++
262 lines
12 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
||
// Read-only presentation of canonical DemoSource opening_flow. No extra App
|
||
// API. Timer, phase changes, persistence, keyboard handling and completion
|
||
// belong to the controller. No participants or pairings are invented by the
|
||
// renderer.
|
||
std::string App::render_opening() {
|
||
const auto state = engine_mode_?engine_opening_state_:snap().state;
|
||
const auto &flow = state.at("opening_flow");
|
||
const auto phase = flow.at("phase").get<std::string>();
|
||
const bool playing = flow.value("playing", false) && !opening_io_blocked_;
|
||
std::set<std::string> own;
|
||
for (const auto &m : state.at("managers"))
|
||
own.insert(m.value("club", ""));
|
||
std::vector<const Json *> order;
|
||
std::set<std::string> seen;
|
||
for (const auto &id : flow.at("order")) {
|
||
if (!id.is_string())
|
||
continue;
|
||
const auto key = id.get<std::string>();
|
||
if (const auto *c = world_->find("clubs", key);
|
||
c && seen.insert(key).second)
|
||
order.push_back(c);
|
||
}
|
||
const int total = int(order.size()), pairs = total / 2;
|
||
const int revealed = std::clamp(flow.value("revealed", 0), 0, total);
|
||
const auto date = state.value(
|
||
"date", world_->manifest().value("start_date", std::string()));
|
||
std::string season = "–";
|
||
if (date.size() >= 4 &&
|
||
std::all_of(date.begin(), date.begin() + 4,
|
||
[](char c) { return c >= '0' && c <= '9'; })) {
|
||
const int year = std::stoi(date.substr(0, 4));
|
||
const int following = (year + 1) % 100;
|
||
season = std::to_string(year) + "/" + (following < 10 ? "0" : "") +
|
||
std::to_string(following);
|
||
}
|
||
const auto owned = [&](const Json *c) {
|
||
return c && own.contains(c->value("id", ""));
|
||
};
|
||
const auto heading = [&](const std::string &title,
|
||
const std::string &subtitle,
|
||
const std::string &detail) {
|
||
return "<header class=\"opening-heading\"><div><h2><span>" + esc(title) +
|
||
"</span></h2><p><span>" + esc(subtitle) +
|
||
"</span></p></div>"
|
||
"<span class=\"opening-heading-detail\">" +
|
||
esc(detail) + "</span></header>";
|
||
};
|
||
const auto note = [&](const std::string &text) {
|
||
if(engine_mode_)return std::string();
|
||
return "<span class=\"opening-demo-note\">" + esc(text) + "</span>";
|
||
};
|
||
const auto pair = [&](int index, int known, int logo_size,
|
||
const std::string &extra) {
|
||
if (index < 0 || index >= pairs)
|
||
return std::string();
|
||
const auto *home = order[size_t(index * 2)],
|
||
*away = order[size_t(index * 2 + 1)];
|
||
const auto side = [&](const Json *c, bool home_side, bool visible) {
|
||
const auto name = visible ? world_->name(*c)
|
||
: home_side ? "Heim"
|
||
: "Noch nicht gezogen";
|
||
const auto text = "<span class=\"opening-pair-name " +
|
||
std::string(visible ? "" : "opening-unknown") +
|
||
"\" title=\"" + esc(name) + "\"><span>" + esc(name) +
|
||
"</span></span>";
|
||
const auto logo = visible ? "<span class=\"opening-pair-crest\">" +
|
||
crest(c, logo_size) + "</span>"
|
||
: "";
|
||
return "<span class=\"opening-pair-side " +
|
||
std::string(home_side ? "home" : "away") + "\">" +
|
||
(home_side ? text + logo : logo + text) + "</span>";
|
||
};
|
||
return "<div class=\"opening-pair-row " + extra +
|
||
(owned(home) || owned(away) ? " own-pair" : "") +
|
||
"\" data-index=\"" + std::to_string(index) + "\" data-home=\"" +
|
||
esc(home->value("id", "")) + "\" data-away=\"" +
|
||
esc(away->value("id", "")) +
|
||
"\">"
|
||
"<span class=\"opening-pair-number\"><span>" +
|
||
std::to_string(index + 1) + "</span></span>" +
|
||
side(home, true, known > 0) +
|
||
"<span class=\"opening-pair-versus\"><span>–</span></span>" +
|
||
side(away, false, known > 1) +
|
||
"<span class=\"opening-pair-counter\" "
|
||
"aria-hidden=\"true\"></span></div>";
|
||
};
|
||
const bool entrants_page =
|
||
phase == "uefa" || phase == "champions" || phase == "winners";
|
||
std::string body, titlebar;
|
||
if (entrants_page) {
|
||
const int page = phase == "uefa" ? 0 : phase == "champions" ? 1 : 2;
|
||
const std::array<const char *, 3> titles = {"UEFA-Cup", "Champions League",
|
||
"Europapokal der Pokalsieger"};
|
||
std::vector<const Json *> entrants;
|
||
std::set<std::string> entered;
|
||
const auto &ids = flow.at("entrants").at(phase);
|
||
for (const auto &id : ids) {
|
||
if (!id.is_string())
|
||
continue;
|
||
const auto key = id.get<std::string>();
|
||
if (const auto *c = world_->find("clubs", key);
|
||
c && entered.insert(key).second)
|
||
entrants.push_back(c);
|
||
}
|
||
const bool german =
|
||
!entrants.empty() &&
|
||
std::all_of(entrants.begin(), entrants.end(), [](const auto *c) {
|
||
return c->value("nation", "") == "nation:de";
|
||
});
|
||
titlebar = german ? "Deutsche Teams" : "Teilnehmende Teams";
|
||
body = "<section class=\"opening-layout opening-entrants\" data-phase=\"" +
|
||
phase + "\">" +
|
||
heading(titles[size_t(page)], titlebar + " · Saison " + season,
|
||
std::to_string(page + 1) + " / 3") +
|
||
"<div class=\"opening-entrant-grid\" id=\"opening-entrants\">";
|
||
if (entrants.empty()) {
|
||
body += "<div class=\"opening-empty-entrants\"><h3><span>Keine "
|
||
"Teilnehmer eingetragen</span></h3>"
|
||
"<p><span>"+std::string(engine_mode_?"Für diesen Wettbewerb sind derzeit keine deutschen Vereine gemeldet.":"Für diesen Wettbewerb sind in dieser Demo-Welt keine Vereine vorgesehen.")+"</span></p></div>";
|
||
} else {
|
||
const bool single = entrants.size() == 1;
|
||
const auto card = [&](const Json *c) {
|
||
return "<article class=\"opening-entrant " +
|
||
std::string(single ? "single-entrant " : "") +
|
||
(owned(c) ? "own-entrant" : "") + "\" data-entrant-club=\"" +
|
||
esc(c->value("id", "")) + "\">" +
|
||
"<div class=\"opening-entrant-crest\">" +
|
||
crest(c, single ? 620 : 360) +
|
||
"</div>"
|
||
"<h3><span>" +
|
||
esc(world_->name(*c)) + "</span></h3>" +
|
||
(owned(c) ? "<p><span>Dein Verein</span></p>" : "") +
|
||
"</article>";
|
||
};
|
||
body += "<div class=\"opening-entrant-content " +
|
||
std::string(single ? "single-entrant-content" : "") + "\">";
|
||
if (single) {
|
||
body += card(entrants.front());
|
||
} else {
|
||
const size_t rows = std::max(size_t(2), (entrants.size() + 1) / 2);
|
||
for (size_t row = 0; row < rows; ++row) {
|
||
body += "<div class=\"opening-entrant-row\">";
|
||
for (size_t column = 0; column < 2; ++column) {
|
||
const size_t index = row * 2 + column;
|
||
body += index < entrants.size()
|
||
? card(entrants[index])
|
||
: "<div class=\"opening-entrant-spacer\" "
|
||
"aria-hidden=\"true\"></div>";
|
||
}
|
||
body += "</div>";
|
||
}
|
||
}
|
||
body += "</div>";
|
||
}
|
||
body += "</div><nav class=\"opening-actions\">" +
|
||
note("Fiktive Demo-Qualifikation") +
|
||
btn("opening-next", "Weiter", {}, "id=\"opening-next\"") +
|
||
"</nav></section>";
|
||
} else if (phase == "draw") {
|
||
titlebar = "DFB-Pokal";
|
||
body =
|
||
"<section class=\"opening-layout opening-draw\" data-phase=\"draw\">" +
|
||
heading("1. Runde", "DFB-Pokal · Saison " + season,
|
||
std::to_string(revealed) + " / " + std::to_string(total) +
|
||
" Mannschaften") +
|
||
"<div class=\"opening-draw-scene " +
|
||
std::string(playing ? "is-drawing" : "") + "\">";
|
||
// a data set may carry a newer picture of the draw
|
||
auto art = renderer_.image("art.cup-draw-v2.png", false, 1800);
|
||
if (art.token.empty()) art = renderer_.image("art.cup-draw-v1.png", false, 1800);
|
||
if (!art.token.empty() && art.width > 0 && art.height > 0) {
|
||
const double ratio = double(art.width) / art.height;
|
||
const double width = std::min(2608.0, 846.0 * ratio),
|
||
height = width / ratio;
|
||
body += "<div class=\"opening-draw-art\" role=\"img\" "
|
||
"aria-label=\"Fiktive Pokal-Auslosung am Tisch mit Glaskugel, "
|
||
"Loszieher und Pressefotografen\" style=\"width:" +
|
||
scalar(width) + "dp;height:" + scalar(height) + "dp;\">" +
|
||
picture(art, width, height, false) + "</div>";
|
||
} else {
|
||
body += "<p class=\"opening-art-missing\"><span>Die Illustration der "
|
||
"Auslosung ist nicht verfügbar.</span></p>";
|
||
}
|
||
body +=
|
||
"<span id=\"opening-draw-indicator\" class=\"opening-draw-indicator " +
|
||
std::string(playing ? "is-playing" : "is-paused") +
|
||
"\" aria-hidden=\"true\"></span></div>"
|
||
"<div id=\"opening-current-pair\" class=\"opening-current-pair\" "
|
||
"aria-live=\"polite\" aria-atomic=\"true\">";
|
||
if (pairs) {
|
||
const int current = std::min(pairs - 1, std::max(0, (revealed - 1) / 2));
|
||
const int known = std::clamp(revealed - current * 2, 0, 2);
|
||
body += pair(current, known, 66, "current") +
|
||
"</div><section class=\"opening-recent\" aria-label=\"Zuletzt "
|
||
"gezogene Paarungen\">";
|
||
const int first = std::max(0, current - 3);
|
||
for (int i = first; i < current; ++i)
|
||
body += "<div class=\"opening-recent-row\" style=\"top:" +
|
||
std::to_string((i - first) * 181) + "dp;\">" +
|
||
pair(i, 2, 42, "recent") + "</div>";
|
||
body += "</section>";
|
||
} else {
|
||
body += "<p class=\"opening-no-pairs\"><span>Für diese Welt ist keine "
|
||
"vollständige Paarung vorhanden.</span></p></div>";
|
||
}
|
||
body +=
|
||
"<nav class=\"opening-actions\">" +
|
||
note(total % 2
|
||
? "Fiktive Demo-Auslosung · 1 Verein ohne vollständige Paarung"
|
||
: "Fiktive Demo-Auslosung") +
|
||
btn("opening-play",
|
||
opening_io_blocked_ ? "Erneut versuchen"
|
||
: playing ? "Pause"
|
||
: "Fortsetzen",
|
||
{},
|
||
"id=\"opening-play\" aria-pressed=\"" +
|
||
std::string(playing ? "true" : "false") + "\" " +
|
||
(revealed >= total ? "disabled" : "")) +
|
||
btn("opening-all", "Alle Paarungen anzeigen", {},
|
||
"id=\"opening-all\"") +
|
||
"</nav></section>";
|
||
} else {
|
||
titlebar = "DFB-Pokal";
|
||
body = "<section class=\"opening-layout opening-overview\" data-phase=\"" +
|
||
esc(phase) + "\">" +
|
||
heading("DFB-Pokal", "1. Runde · Saison " + season,
|
||
std::to_string(pairs) +
|
||
(pairs == 1 ? " Paarung" : " Paarungen")) +
|
||
"<div id=\"opening-overview\" class=\"opening-pair-columns\">";
|
||
if (!pairs) {
|
||
body += "<div class=\"opening-empty-overview\"><p><span>Für diese Welt "
|
||
"sind keine vollständigen Paarungen vorhanden.</span></p></div>";
|
||
} else {
|
||
const int columns = pairs > 1 ? 2 : 1,
|
||
per_column = (pairs + columns - 1) / columns;
|
||
for (int column = 0; column < columns; ++column) {
|
||
const int first = column * per_column,
|
||
last = std::min(pairs, first + per_column);
|
||
body += "<section class=\"opening-overview-column " +
|
||
std::string(columns == 1 ? "single-column" : "") +
|
||
"\" aria-label=\"Paarungen " + std::to_string(first + 1) +
|
||
" bis " + std::to_string(last) +
|
||
"\" style=\"left:" + std::to_string(column * 1323) +
|
||
"dp;\"><div class=\"opening-overview-list\">";
|
||
for (int i = first; i < last; ++i)
|
||
body += "<div class=\"opening-overview-row\">" +
|
||
pair(i, 2, 36, "compact") + "</div>";
|
||
body += "</div></section>";
|
||
}
|
||
}
|
||
body += "</div><nav class=\"opening-actions\">" +
|
||
note(total % 2 ? "Fiktive Demo-Auslosung · ohne Paarung: " +
|
||
world_->name(*order.back())
|
||
: "Fiktive Demo-Auslosung") +
|
||
btn("opening-finish", "Weiter zum Desktop", "primary",
|
||
"id=\"opening-finish\"") +
|
||
"</nav></section>";
|
||
}
|
||
return window(titlebar, body, false);
|
||
}
|