Files
Dreierpack-Manager/src/modern/ui/engine_assembly.inc
T
MichaelandClaude Fable 5.2 72e06aabdb Players wear shirt numbers the manager can change
Every player keeps a number from 1 to 99, unique within his squad. The
line-up shows it and changes it by a click; the tactics sketch and the
match sheet wear it too.

Engine: shirt_numbers.hpp, squad read player.shirtNumber, command
lineup.setNumber; saved in state/modern.json.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 16:58:34 +01:00

273 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-only
// The annual general meeting and the clubs interested in a manager
// (docs/new_engine/hauptversammlung.md), as notes over the notebook.
namespace {
// One group of members: its name, a bar and its approval in percent.
std::string assembly_group(const char *name, int value) {
value = std::clamp(value, 0, 100);
return "<div class=\"as-row\"><span class=\"as-name\">" + esc(name) + "</span><div class=\"as-track\"><div class=\"as-fill" + std::string(value < 50 ? " low" : "") + "\" style=\"width:" + std::to_string(value) +
"%;\"></div></div><span class=\"as-value\">" + std::to_string(value) + " %</span></div>";
}
} // namespace
// The kickoff at the end of a season stopped for the meeting: first its result, then - for a
// manager who was voted out - the clubs he can go to.
void App::engine_assembly_ask(const Json &args) {
engine_assembly_ = args;
const Json vote = args.value("vote", Json::object());
const bool confirmed = vote.value("confirmed", true), leave = !confirmed && args.value("dismissals", true);
const Json sent = engine_advance_payload_.value("assembly", Json::object());
if (sent.is_object() && sent.value("seen", false)) {
if (leave) engine_assembly_clubs(true);
return;
}
const int yes = std::clamp(vote.value("yes", 0), 0, 100);
const std::string verdict = confirmed ? "Die Mitglieder bestätigen dich im Amt."
: leave ? "Die Mitglieder haben dich abgewählt. Du musst den Verein verlassen."
: "Die Mehrheit stimmt gegen dich. Diesmal bleibt das ohne Folgen.";
const std::string club = args.value("club", Json::object()).value("name", std::string());
confirmation("Zum Saisonende stimmen die Mitglieder" + (club.empty() ? std::string() : " von " + club) + " über ihren Manager ab.", [this, leave] {
engine_advance_payload_["assembly"]["seen"] = true;
if (leave) engine_assembly_clubs(true);
else engine_kickoff();
});
popup_title_ = "Jahreshauptversammlung";
popup_accept_ = leave ? "Interessierte Vereine" : "Weiter";
popup_close_ = "Zum Schreibtisch";
// the dial: red on the left, green on the right, the line of the vote in the middle
std::string dial = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 112'>";
constexpr int parts = 48;
for (int i = 0; i < parts; ++i) {
const double from = 3.14159265358979 * (1.0 - double(i) / parts), to = 3.14159265358979 * (1.0 - double(i + 1) / parts + 0.002);
const double at = (i + 0.5) / parts;
const std::array<int, 3> red{196, 62, 46}, amber{226, 170, 52}, green{84, 140, 66};
const auto &lo = at < 0.5 ? red : amber, &hi = at < 0.5 ? amber : green;
const double k = std::pow(at < 0.5 ? at * 2 : (at - 0.5) * 2, at < 0.5 ? 2.2 : 0.45); // the colour turns close to the middle
char colour[8];
std::snprintf(colour, sizeof colour, "#%02x%02x%02x", int(lo[0] + (hi[0] - lo[0]) * k), int(lo[1] + (hi[1] - lo[1]) * k), int(lo[2] + (hi[2] - lo[2]) * k));
dial += "<path d='M" + scalar(100 + 80 * std::cos(from)) + " " + scalar(100 - 80 * std::sin(from)) + " A80 80 0 0 1 " + scalar(100 + 80 * std::cos(to)) + " " + scalar(100 - 80 * std::sin(to)) +
"' fill='none' stroke='" + colour + "' stroke-width='22'/>";
}
const std::string ink = theme_ != "silver" && theme_ != "matchday" ? "#f0ece2" : "#2a211a";
dial += "<path d='M100 6v28' stroke='" + ink + "' stroke-width='2.4'/><circle cx='100' cy='100' r='9' fill='" + ink + "'/></svg>";
engine_assembly_target_ = yes;
engine_assembly_started_ = std::max<uint64_t>(SDL_GetTicks64(), 1);
popup_body_ = "<div class=\"as-vote\"><div class=\"as-gauge\">" + picture(renderer_.diagram("assembly-dial-" + theme_, dial, 1280), 640, 358) +
"<div id=\"assembly-needle\" class=\"as-needle\"></div></div><div class=\"as-ends\"><span>Abwahl</span><span>Bestätigung</span></div>" +
"<div id=\"assembly-result\" style=\"visibility:hidden;\">" + assembly_group("Sportliche Leitung", vote.value("sport", 0)) + assembly_group("Kassenprüfer", vote.value("finance", 0)) +
assembly_group("Fans", vote.value("supporters", 0)) + "<div class=\"as-result" + std::string(confirmed ? "" : " low") + "\"><strong>" + std::to_string(yes) + " % Ja</strong><span>" +
std::to_string(100 - yes) + " % Nein</span></div><p class=\"as-verdict\">" + esc(verdict) + "</p></div></div>";
}
// The needle swings while the votes are counted and comes to rest on the result. It moves from
// the middle towards the result within a second and a half; the swing around it starts at a
// good fifth of the dial and dies away, so a clear result never looks like its opposite.
void App::engine_assembly_tick() {
if (!engine_assembly_started_) return;
if (popup_.empty()) { engine_assembly_started_ = 0; return; }
auto *needle = element("assembly-needle");
if (!needle) return; // the note is drawn with this frame
constexpr double duration = 7.5;
const double t = double(SDL_GetTicks64() - engine_assembly_started_) / 1000.0;
const double target = std::clamp(engine_assembly_target_, 0, 100);
double shown = target;
if (t < duration) {
const double x = std::min(t / 1.5, 1.0), approach = x * x * (3 - 2 * x);
const double swing = 22.0 * std::pow(1.0 - t / duration, 1.4);
const double phase = double(engine_assembly_started_ % 6283) / 1000.0;
const double wave = 0.55 * std::sin(t * 4.3 + phase) + 0.3 * std::sin(t * 7.9 + phase * 2.1) + 0.25 * std::sin(t * 2.1 + phase * 0.7);
shown = std::clamp(50 + (target - 50) * approach + swing * wave, 0.0, 100.0);
}
needle->SetProperty("transform", "rotate(" + scalar((shown - 50) * 1.8) + "deg)");
if (t >= duration) {
if (auto *result = element("assembly-result")) result->SetProperty("visibility", "visible");
engine_assembly_started_ = 0;
}
}
// The clubs that want the manager, on a sheet as large as a page of the notebook: the clubs on
// the left, and on the right what the chosen one is like beside the manager's own club.
void App::engine_assembly_clubs(bool forced) {
engine_assembly_forced_ = forced;
const Json offers = engine_assembly_.value("offers", Json::array());
if (offers.empty()) {
engine_assembly_open_ = false;
message("Zurzeit interessiert sich kein Verein für dich.");
return;
}
engine_assembly_open_ = true;
engine_assembly_pick_ = offers.front().value("teamId", std::string());
engine_assembly_scroll_ = 0.f;
refresh();
}
namespace {
// 1234567 -> 1.234.567
std::string assembly_count(int64_t n) {
std::string digits = std::to_string(n < 0 ? -n : n), out;
for (size_t i = 0; i < digits.size(); ++i) {
if (i && (digits.size() - i) % 3 == 0) out += '.';
out += digits[i];
}
return (n < 0 ? "−" : "") + out;
}
// Money of the contract ({minor: "…"}) in whole euros.
int64_t assembly_euros(const Json &money) {
try {
return std::stoll(money.at("minor").get<std::string>()) / 100;
} catch (const std::exception &) { return 0; }
}
// Two bars side by side for one part of the team: the manager's club and the other one.
std::string assembly_pair(const char *name, int own, int other) {
const auto bar = [](int value, const char *css) {
return "<div class=\"cs-col\"><span class=\"cs-num\">" + std::to_string(value) + "</span><div class=\"cs-bar " + std::string(css) + "\" style=\"height:" + scalar(std::clamp(value, 0, 99) * 4.6 + 6) + "dp;\"></div></div>";
};
return "<div class=\"cs-grp\"><div class=\"cs-bars\">" + bar(own, "own") + bar(other, "new") + "</div><span class=\"cs-cap\">" + esc(name) + "</span></div>";
}
} // namespace
std::string App::engine_assembly_sheet() {
const Json offers = engine_assembly_.value("offers", Json::array());
const Json own = engine_assembly_.value("club", Json::object());
const Json *chosen = nullptr;
for (const auto &offer : offers)
if (offer.value("teamId", std::string()) == engine_assembly_pick_) chosen = &offer;
if (!chosen && !offers.empty()) chosen = &offers.front();
const auto name_of = [&](const Json &row) {
const auto *club = world_->find("clubs", row.value("teamId", std::string()));
return club ? world_->name(*club) : row.value("name", std::string("Verein"));
};
const auto place_of = [](const Json &row) {
return row.contains("position") && row.at("position").is_number_integer() ? "Platz " + std::to_string(row.at("position").get<int>()) : std::string("noch ohne Platz");
};
const bool forced = engine_assembly_forced_;
std::string out = "<div id=\"assembly-layer\" class=\"pg-ov\"><section class=\"pg-dlg cs-sheet\" role=\"dialog\" aria-modal=\"true\"><header class=\"cs-head\"><span class=\"pg-k\">" +
std::string(forced ? "NACH DER ABWAHL" : "VEREINSWECHSEL") + "</span><h2>Interessierte Vereine</h2><span class=\"pg-dlg-sub\">" +
esc(forced ? "Einen dieser Vereine musst du übernehmen, bevor die neue Saison beginnt." : "Diese Vereine wollen dich verpflichten. Die Liste wechselt alle zwei Wochen.") +
"</span></header><div class=\"cs-body\"><div id=\"assembly-list\" class=\"cs-list\">";
for (const auto &offer : offers) {
const std::string id = offer.value("teamId", std::string());
const auto *club = world_->find("clubs", id);
const bool on = chosen && id == chosen->value("teamId", std::string());
out += "<button data-action=\"assembly-pick\" class=\"cs-club" + std::string(on ? " on" : "") + "\" id=\"assembly-pick-" + esc(id) + "\" data-id=\"" + esc(id) + "\" aria-pressed=\"" + (on ? "true" : "false") +
"\"><span class=\"cs-crest\">" + (club ? crest(club, 150) : std::string()) + "</span><div class=\"cs-text\"><div class=\"cs-name\">" + esc(name_of(offer)) + "</div><div class=\"cs-line\">" + esc(offer.value("league", std::string())) + " · " +
esc(place_of(offer)) + "</div><div class=\"cs-line\">Startkapital " + esc(offer.contains("cash") ? engine_price(offer.at("cash")) : std::string("—")) + "</div></div><div class=\"cs-power\"><div class=\"cs-figure\">" +
std::to_string(offer.value("strength", 0)) + "</div><div class=\"cs-line\">Stärke</div></div></button>";
}
out += "</div><div class=\"cs-detail\">";
if (chosen) {
const Json &c = *chosen;
const std::string name = name_of(c), mine = name_of(own);
// the team, part by part
const Json a = own.value("lines", Json::object()), b = c.value("lines", Json::object());
out += "<div class=\"cs-row\"><section class=\"cs-box cs-team\"><span class=\"pg-k\">MANNSCHAFT IM VERGLEICH</span><div class=\"cs-chart\">" + assembly_pair("Tor", a.value("goal", 0), b.value("goal", 0)) +
assembly_pair("Abwehr", a.value("defence", 0), b.value("defence", 0)) + assembly_pair("Mittelfeld", a.value("midfield", 0), b.value("midfield", 0)) +
assembly_pair("Angriff", a.value("attack", 0), b.value("attack", 0)) + assembly_pair("Elf gesamt", own.value("strength", 0), c.value("strength", 0)) + "</div></section>";
// the season so far, place by round
const Json places = c.value("positions", Json::array());
const int size = std::max(c.value("leagueSize", 18), 2);
std::string curve;
if (places.size() >= 2) {
std::string points;
for (size_t i = 0; i < places.size(); ++i)
points += scalar(12 + 576.0 * double(i) / double(places.size() - 1)) + "," + scalar(12 + 336.0 * double(std::clamp(places[i].get<int>(), 1, size) - 1) / double(size - 1)) + " ";
const std::string last_y = scalar(12 + 336.0 * double(std::clamp(places.back().get<int>(), 1, size) - 1) / double(size - 1));
curve = picture(renderer_.diagram("assembly-places-" + c.value("teamId", std::string()) + "-" + std::to_string(places.size()),
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 600 360'><path d='M12 12H588M12 180H588M12 348H588' stroke='#8a7d6b' stroke-opacity='.35' stroke-width='1.5'/><polyline points='" + points +
"' fill='none' stroke='#c2693a' stroke-width='5' stroke-linejoin='round' stroke-linecap='round'/><circle cx='588' cy='" + last_y + "' r='9' fill='#c2693a'/></svg>", 1200), 780, 468);
}
out += "<section class=\"cs-box cs-season\"><span class=\"pg-k\">TABELLENPLATZ IN DIESER SAISON</span>" +
(curve.empty() ? std::string("<div class=\"cs-none\">Noch zu wenige Spieltage.</div>")
: "<div class=\"cs-curve\"><span class=\"cs-axis top\">1.</span><span class=\"cs-axis bottom\">" + std::to_string(size) + ".</span>" + curve + "</div>") + "</section></div>";
// money, ground and squad
const int64_t now = assembly_euros(engine_assembly_.value("cash", Json::object())), then = assembly_euros(c.value("cash", Json::object()));
const double scale = double(std::max<int64_t>({std::llabs(now), std::llabs(then), 1}));
const auto money_bar = [&](const char *label, int64_t value, const char *css) {
return "<div class=\"cs-money\"><span class=\"cs-label\">" + std::string(label) + "</span><div class=\"cs-track\"><div class=\"cs-fill " + std::string(value < 0 ? "debt" : css) + "\" style=\"width:" +
scalar(std::max(2.0, 100.0 * double(std::llabs(value)) / scale)) + "%;\"></div></div><span class=\"cs-sum\">" + assembly_count(value) + " €</span></div>";
};
// the mean age of both squads
const auto age_bar = [&](const std::string &label, const Json &row, const char *css) {
const bool known = row.contains("averageAge") && row.at("averageAge").is_number_integer();
const int tenths = known ? row.at("averageAge").get<int>() : 0;
return "<div class=\"cs-money\"><span class=\"cs-label\">" + esc(label) + "</span><div class=\"cs-track\"><div class=\"cs-fill " + std::string(css) + "\" style=\"width:" +
scalar(known ? std::clamp((tenths - 160) / 2.0, 4.0, 100.0) : 0.0) + "%;\"></div></div><span class=\"cs-sum cs-age\">" +
(known ? std::to_string(tenths / 10) + "," + std::to_string(tenths % 10) + " Jahre" : std::string("—")) + "</span></div>";
};
const std::string ages = "<section class=\"cs-box cs-cash\"><span class=\"pg-k\">ALTERSSCHNITT DES KADERS</span>" + age_bar(mine, own, "own") + age_bar(name, c, "new") + "</section>";
// the last league matches: won, drawn or lost, with the score
std::string form = "<section class=\"cs-box cs-form\"><span class=\"pg-k\">LETZTE LIGASPIELE</span>";
const Json results = c.value("results", Json::array());
if (results.empty()) form += "<div class=\"cs-none small\">Noch kein Ligaspiel in dieser Saison.</div>";
else {
form += "<div class=\"cs-games\">";
for (const auto &game : results) {
const std::string outcome = game.value("outcome", std::string("draw"));
form += "<div class=\"cs-game\"><div class=\"cs-dot " + outcome + "\">" + (outcome == "win" ? "S" : outcome == "loss" ? "N" : "U") + "</div><div class=\"cs-score\">" + std::to_string(game.value("goalsFor", 0)) + ":" +
std::to_string(game.value("goalsAgainst", 0)) + "</div><div class=\"cs-where\">" + (game.value("home", false) ? "Heim" : "Auswärts") + "</div></div>";
}
form += "</div>";
}
form += "</section>";
out += "<div class=\"cs-row\"><section class=\"cs-box cs-cash\"><span class=\"pg-k\">VEREINSKASSE</span>" + money_bar("Dein Konto heute", now, "own") + money_bar("Startkapital dort", then, "new") +
"</section><section class=\"cs-box cs-fact\"><span class=\"pg-k\">STADION</span><strong>" +
(c.contains("stadiumCapacity") && c.at("stadiumCapacity").is_number_integer() ? assembly_count(c.at("stadiumCapacity").get<int64_t>()) : std::string("—")) +
"</strong><span>Plätze zum Start</span></section><section class=\"cs-box cs-fact\"><span class=\"pg-k\">KADER</span><strong>" + std::to_string(c.value("squad", 0)) + "</strong><span>Spieler</span></section></div>" +
"<div class=\"cs-row\">" + ages + form + "</div>" +
"<p class=\"cs-note\">Beim Wechsel bleiben dein Konto, deine Aktien, Fonds und Kredite zurück. Stadion, Verträge und Werbung beginnen beim neuen Verein neu.</p>";
}
// which colour is which club, beside the buttons
const std::string key = chosen ? "<div class=\"cs-key\"><span class=\"own\">" + esc(name_of(own)) + "</span><span class=\"new\">" + esc(name_of(*chosen)) + "</span></div>" : std::string();
out += "</div></div><nav class=\"pg-dlg-act\">" + key + pg_button("assembly-close", forced ? "Zum Schreibtisch" : "Abbrechen", "", "id=\"assembly-close\"") +
pg_button("assembly-take", chosen ? name_of(*chosen) + " übernehmen" : std::string("Übernehmen"), "primary", "id=\"assembly-take\" " + std::string(chosen ? "" : "disabled=\"disabled\"")) + "</nav></section></div>";
return out;
}
bool App::engine_assembly_action(const std::string &action, const std::string &id) {
if (action == "assembly-pick") {
engine_assembly_pick_ = id;
focus_after_render_ = "assembly-pick-" + id;
refresh();
return true;
}
if (action == "assembly-close") {
engine_assembly_open_ = false;
refresh();
return true;
}
if (action == "assembly-take" && !engine_busy_) {
engine_assembly_choose(engine_assembly_pick_);
return true;
}
return false;
}
// A club was taken: asked once more, because nothing of the old club comes along.
void App::engine_assembly_choose(const std::string &team) {
const auto *club = world_->find("clubs", team);
if (team.empty() || !club) return;
const std::string name = world_->name(*club);
const bool forced = engine_assembly_forced_;
confirmation("Wirklich zu " + name + " wechseln? Das lässt sich nicht zurücknehmen. Dein Konto, deine Aktien, Fonds und Kredite bleiben zurück; beim neuen Verein beginnst du mit dessen Startkapital und neuen Verträgen.", [this, team, forced] {
engine_assembly_open_ = false;
if (forced) {
engine_advance_payload_["assembly"] = {{"seen", true}, {"teamId", team}};
engine_kickoff();
return;
}
try {
engine_finance_submit(engine_club_change_command(engine_data_, team, editor::new_id("request:ui-")));
} catch (const std::exception &e) { message(e.what()); }
});
popup_title_ = "Vereinswechsel";
popup_accept_ = "Wechseln";
}
// Shirt numbers: what the squad read says a player wears.
std::string App::engine_shirt(const std::string &player, size_t fallback) const {
if (engine_mode_) try {
for (const auto &row : engine_data_.at("squad").at("data").at("players"))
if (row.at("player").at("id") == player && row.at("player").at("shirtNumber").is_number_integer()) return std::to_string(row.at("player").at("shirtNumber").get<int>());
} catch (const std::exception &) {}
return std::to_string(fallback);
}
std::string App::engine_shirt_at(size_t place) const {
if (engine_mode_) try {
const auto &rows = engine_data_.at("squad").at("data").at("players");
if (place < rows.size() && rows[place].at("player").at("shirtNumber").is_number_integer()) return std::to_string(rows[place].at("player").at("shirtNumber").get<int>());
} catch (const std::exception &) {}
return std::to_string(place + 1);
}