// 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 "
" + esc(name) + "
" + std::to_string(value) + " %
"; } } // 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 = ""; 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 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 += ""; } const std::string ink = theme_ != "silver" && theme_ != "matchday" ? "#f0ece2" : "#2a211a"; dial += ""; engine_assembly_target_ = yes; engine_assembly_started_ = std::max(SDL_GetTicks64(), 1); popup_body_ = "
" + picture(renderer_.diagram("assembly-dial-" + theme_, dial, 1280), 640, 358) + "
AbwahlBestätigung
" + "
" + assembly_group("Sportliche Leitung", vote.value("sport", 0)) + assembly_group("Kassenprüfer", vote.value("finance", 0)) + assembly_group("Fans", vote.value("supporters", 0)) + "
" + std::to_string(yes) + " % Ja" + std::to_string(100 - yes) + " % Nein

" + esc(verdict) + "

"; } // 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()) / 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 "
" + std::to_string(value) + "
"; }; return "
" + bar(own, "own") + bar(other, "new") + "
" + esc(name) + "
"; } } // 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()) : std::string("noch ohne Platz"); }; const bool forced = engine_assembly_forced_; std::string out = "
" + std::string(forced ? "NACH DER ABWAHL" : "VEREINSWECHSEL") + "

Interessierte Vereine

" + esc(forced ? "Einen dieser Vereine musst du übernehmen, bevor die neue Saison beginnt." : "Diese Vereine wollen dich verpflichten. Die Liste wechselt alle zwei Wochen.") + "
"; 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 += ""; } out += "
"; 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 += "
MANNSCHAFT IM VERGLEICH
" + 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)) + "
"; // 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(), 1, size) - 1) / double(size - 1)) + " "; const std::string last_y = scalar(12 + 336.0 * double(std::clamp(places.back().get(), 1, size) - 1) / double(size - 1)); curve = picture(renderer_.diagram("assembly-places-" + c.value("teamId", std::string()) + "-" + std::to_string(places.size()), "", 1200), 780, 468); } out += "
TABELLENPLATZ IN DIESER SAISON" + (curve.empty() ? std::string("
Noch zu wenige Spieltage.
") : "
1." + std::to_string(size) + "." + curve + "
") + "
"; // 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({std::llabs(now), std::llabs(then), 1})); const auto money_bar = [&](const char *label, int64_t value, const char *css) { return "
" + std::string(label) + "
" + assembly_count(value) + " €
"; }; // 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() : 0; return "
" + esc(label) + "
" + (known ? std::to_string(tenths / 10) + "," + std::to_string(tenths % 10) + " Jahre" : std::string("—")) + "
"; }; const std::string ages = "
ALTERSSCHNITT DES KADERS" + age_bar(mine, own, "own") + age_bar(name, c, "new") + "
"; // the last league matches: won, drawn or lost, with the score std::string form = "
LETZTE LIGASPIELE"; const Json results = c.value("results", Json::array()); if (results.empty()) form += "
Noch kein Ligaspiel in dieser Saison.
"; else { form += "
"; for (const auto &game : results) { const std::string outcome = game.value("outcome", std::string("draw")); form += "
" + (outcome == "win" ? "S" : outcome == "loss" ? "N" : "U") + "
" + std::to_string(game.value("goalsFor", 0)) + ":" + std::to_string(game.value("goalsAgainst", 0)) + "
" + (game.value("home", false) ? "Heim" : "Auswärts") + "
"; } form += "
"; } form += "
"; out += "
VEREINSKASSE" + money_bar("Dein Konto heute", now, "own") + money_bar("Startkapital dort", then, "new") + "
STADION" + (c.contains("stadiumCapacity") && c.at("stadiumCapacity").is_number_integer() ? assembly_count(c.at("stadiumCapacity").get()) : std::string("—")) + "Plätze zum Start
KADER" + std::to_string(c.value("squad", 0)) + "Spieler
" + "
" + ages + form + "
" + "

Beim Wechsel bleiben dein Konto, deine Aktien, Fonds und Kredite zurück. Stadion, Verträge und Werbung beginnen beim neuen Verein neu.

"; } // which colour is which club, beside the buttons const std::string key = chosen ? "
" + esc(name_of(own)) + "" + esc(name_of(*chosen)) + "
" : std::string(); out += "
"; 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()); } 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()); } catch (const std::exception &) {} return std::to_string(place + 1); }