// 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 = "";
engine_assembly_target_ = yes;
engine_assembly_started_ = std::max(SDL_GetTicks64(), 1);
popup_body_ = "
";
}
// 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 = "
" +
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 += "
";
// 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 "
";
};
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 += "