Files
Dreierpack-Manager/src/modern/ui/postmatch_eleven_markup.hpp
T

162 lines
7.1 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "modern/ui/controls.hpp"
#include <array>
#include <functional>
namespace bmh::modern_ui {
using PostmatchBadge = std::function<std::string(const Json &, double)>;
inline std::string postmatch_position_label(const std::string &position) {
return position == "gk" ? "TW"
: position == "def" ? "ABW"
: position == "mid" ? "MIT"
: "STU";
}
inline std::string postmatch_pitch_svg(const std::string &color) {
return "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 740 1110'><g "
"fill='none' stroke='" +
color +
"' stroke-width='2'><path d='M30 30h680v1050H30zM30 555h680'/><circle "
"cx='370' cy='555' r='91.5'/><path d='M168.4 30v165h403.2V30M278.4 "
"30v55h183.2V30M168.4 1080V915h403.2v165M278.4 "
"1080v-55h183.2v55M333.4 30V12h73.2v18M333.4 1080v18h73.2v-18M296.7 "
"195a91.5 91.5 0 0 0 146.6 0M296.7 915a91.5 91.5 0 0 1 146.6 "
"0'/></g><g fill='" +
color +
"'><circle cx='370' cy='555' r='3'/><circle cx='370' cy='140' "
"r='3'/><circle cx='370' cy='970' r='3'/></g></svg>";
}
inline std::string postmatch_eleven_markup(const Json &eleven,
const PostmatchBadge &badge,
const std::string &pitch) {
const auto text = [](const std::string &s) {
return "<span>" + editor::escaped(s) + "</span>";
};
const bool available =
eleven.value("available", false) && eleven.contains("rows") &&
eleven.at("rows").is_array() && eleven.at("rows").size() == 11;
std::string out =
"<section class=\"postmatch-eleven\"><div id=\"postmatch-pitch-holder\" "
"class=\"postmatch-pitch-holder\"><div id=\"postmatch-pitch-surface\" "
"class=\"postmatch-pitch-surface\"><div id=\"postmatch-pitch-art\" "
"class=\"postmatch-pitch-art\">" +
pitch + "</div>";
if (available) {
for (const auto &[position, top] :
std::array<std::pair<const char *, int>, 4>{
{{"gk", 89}, {"def", 68}, {"mid", 45}, {"fwd", 22}}}) {
size_t count = 0;
for (const auto &row : eleven.at("rows"))
if (row.at("position") == position)
++count;
size_t column = 0, index = 0;
for (const auto &row : eleven.at("rows")) {
const auto n = index++;
if (row.at("position") != position)
continue;
++column;
const auto name = row.at("player").at("name").get<std::string>();
const auto at = name.find_last_of(' ');
out += button_markup(
"postmatch-player",
"<span class=\"postmatch-field-badge\">" +
badge(row.at("team"), 60) + "</span><strong>" +
text(at == name.npos ? name : name.substr(at + 1)) +
"</strong>",
"postmatch-field-player",
"id=\"postmatch-nominee-" + std::to_string(n) + "\" data-id=\"" +
editor::escaped(row.at("player").at("id").get<std::string>()) +
"\" style=\"left:" +
std::to_string(100.0 * column / (count + 1)) +
"%;top:" + std::to_string(top) + "%;\" aria-label=\"" +
editor::escaped(name + " · " +
row.at("team").at("name").get<std::string>()) +
"\"");
}
}
}
out += "</div></div><div id=\"postmatch-eleven-list\" "
"class=\"postmatch-eleven-list\">";
if (!available)
out += "<h3>" + text("Keine Nominierungen aufgezeichnet") + "</h3><p>" +
text("Die feste Liveaufnahme enthält keine Spielernoten oder "
"Nominierungen. Es wird keine Elf aus Kaderstärken oder "
"vermuteten Ereignisakteuren erfunden.") +
"</p><p>" +
text("Ergebnisse und Bericht zeigen ausschließlich die vorhandene "
"Aufzeichnung.") +
"</p>";
else {
out += "<p class=\"postmatch-eleven-notice\">" +
text(eleven.value("notice", "Explizit gelieferte Nominierungen")) +
"</p><ol>";
size_t index = 0;
for (const auto &row : eleven.at("rows")) {
const auto n = index++;
const auto id = row.at("player").at("id").get<std::string>();
out +=
"<li class=\"postmatch-nominee-row\"><span "
"class=\"postmatch-position\">" +
text(postmatch_position_label(row.at("position"))) +
"</span><span class=\"postmatch-nominee-copy\">" +
button_markup(
"postmatch-player",
editor::escaped(row.at("player").at("name").get<std::string>()),
"postmatch-nominee-name",
"id=\"postmatch-nominee-name-" + std::to_string(n) +
"\" data-id=\"" + editor::escaped(id) + "\"") +
button_markup(
"postmatch-club",
badge(row.at("team"), 38) + text(row.at("team").at("name")),
"postmatch-nominee-club",
"data-id=\"" +
editor::escaped(row.at("team").at("id").get<std::string>()) +
"\"") +
"</span></li>";
}
out += "</ol>";
}
return out + "</div></section>";
}
inline void fit_postmatch_pitch(Rml::ElementDocument &doc) {
auto *holder = doc.GetElementById("postmatch-pitch-holder"),
*surface = doc.GetElementById("postmatch-pitch-surface");
if (!holder || !surface)
return;
const auto size = holder->GetBox().GetSize(Rml::BoxArea::Content);
const float height = std::min(size.y, size.x * 1110.f / 740),
width = height * 740 / 1110;
const auto set = [](Rml::Element *node, Rml::PropertyId id, float value) {
const auto *old = node->GetLocalProperty(id);
if (!old || old->unit != Rml::Unit::PX ||
std::abs(old->Get<float>() - value) > .02f)
node->SetProperty(id, Rml::Property(value, Rml::Unit::PX));
};
set(surface, Rml::PropertyId::Width, width);
set(surface, Rml::PropertyId::Height, height);
set(surface, Rml::PropertyId::Left, (size.x - width) / 2);
set(surface, Rml::PropertyId::Top, (size.y - height) / 2);
if (auto *art = doc.GetElementById("postmatch-pitch-art"))
for (auto *node :
{art, art->QuerySelector(".image-frame"), art->QuerySelector("img")})
if (node) {
set(node, Rml::PropertyId::Width, width);
set(node, Rml::PropertyId::Height, height);
}
}
inline std::string postmatch_nominee_detail(const Json &row,
const PostmatchBadge &badge) {
const auto text = [](const std::string &v) {
return "<span>" + editor::escaped(v) + "</span>";
};
return "<header class=\"postmatch-player-summary\">" +
badge(row.at("team"), 120) + "<p>" + text(row.at("team").at("name")) +
"</p></header><dl "
"class=\"postmatch-player-facts\"><div><dt>Position</dt><dd>" +
text(postmatch_position_label(row.at("position"))) +
"</dd></div><div><dt>Note</dt><dd>" +
text(row.at("rating").is_null() ? "—" : row.at("rating").dump()) +
"</dd></div></dl>";
}
} // namespace bmh::modern_ui