// SPDX-License-Identifier: GPL-3.0-only // Who plays this career as a human: the page "Spieler" exists only where there is more than one. Json App::bank_players() const { try { if (engine_mode_) return engine_manager_value_.is_object() && engine_manager_value_.contains("people") ? engine_career_context().at("active_managers") : Json::array(); if (source_ && world_) return demo_career_context(snap().state, *snap().world).at("active_managers"); } catch (const std::exception &) { } return Json::array(); } // Finanzen > Bank, Immobilien, Aktien, Sparbuch, Spieler: five pages of one menu. `bank_view_` // says which: "credit" (the bank with what the club owns and its loan), "properties", "stocks", // "savings" or "players". std::string App::render_bank() { const auto projected = engine_mode_ ? engine_bank_projection(engine_data_) : Json(); const auto snapshot = engine_mode_ ? Snapshot{0, world_, projected.at("state")} : snap(); const auto assets = engine_mode_ ? projected.at("assets") : demo_bank_assets(*world_); const auto summary = engine_mode_ ? projected.at("summary") : demo_bank_summary(snapshot.state, *world_); const auto bank_money = [&](int64_t value) { return engine_mode_ ? money(value, {{"code", "EUR"}, {"minor_units", 2}}) : cash(value); }; const int property_count = int(assets.at("properties").size()), stock_count = int(assets.at("stocks").size()); if (!summary.value("ok", false)) return {}; if (bank_view_ != "properties" && bank_view_ != "stocks" && bank_view_ != "savings" && bank_view_ != "players") bank_view_ = "credit"; if (bank_view_ == "players" && bank_players().size() < 2) bank_view_ = "credit"; const auto &finance = snapshot.state.at("finance"); bank_property_ = std::clamp(bank_property_, 0, property_count - 1); bank_stock_ = std::clamp(bank_stock_, 0, stock_count - 1); bank_owned_ = bank_property_; // the board has one chosen property: it is bought and sold const auto calc = [&](const char *label, const char *kind, const std::string &css, bool on = true) { return pg_button("bank-calc", label, css, "id=\"bank-" + std::string(kind) + "\" data-kind=\"" + kind + "\"" + (on ? "" : " disabled=\"disabled\"")); }; const auto kv = [](const std::string &key, const std::string &value, const std::string &css = std::string()) { return "
" + key + "" + value + "
"; }; const auto one = [](double value) { std::ostringstream text; text << std::fixed << std::setprecision(1) << value; auto result = text.str(); std::replace(result.begin(), result.end(), '.', ','); return result; }; // what the club owns, by where it lies const int64_t in_cash = summary.at("cash").get(), in_deposit = summary.at("deposit").get(), in_property = summary.at("properties").get(), in_stock = summary.at("stocks").get(); const int64_t wealth = std::max(0, in_cash) + in_deposit + in_property + in_stock; const auto share = [&](int64_t part) { return wealth > 0 ? one(std::max(0, part) * 100.0 / wealth) + " %" : std::string("—"); }; const bool light = theme_ == "silver"; const std::string ink = light ? "#2e2b25" : "#f0ece2", faint = light ? "#3a332b24" : "#f0ece224", up = light ? "#56633f" : "#a3ba7c", down = light ? "#a8452f" : "#f0a08a", gold = "#d08a1e", line = light ? "#8c491a" : "#e0a25f"; // an engine name arrives in capitals const auto calm_name = [](std::string name) { bool first = true; std::string result; for (size_t i = 0; i < name.size(); ++i) { const unsigned char c = static_cast(name[i]); if (c == 0xc3 && i + 1 < name.size()) { // Ä Ö Ü unsigned char next = static_cast(name[i + 1]); if (!first && (next == 0x84 || next == 0x96 || next == 0x9c)) next = static_cast(next + 0x20); result += name[i]; result += static_cast(next); ++i; first = false; continue; } if (std::isalpha(c)) { result += static_cast(first ? c : std::tolower(c)); first = false; } else { result += name[i]; if (c == ' ' || c == '-') first = true; } } return result; }; const auto factor = engine_mode_ ? 100 : finance_currency_factor(*world_); const auto course = [&](int index) { std::vector history; const auto &stock = assets.at("stocks")[size_t(index)]; if (engine_mode_) { for (const auto &value : stock.at("history")) if (value.get() > 0) history.push_back(value.get() / factor); } else { const double price = stock.at("price").get() / factor; for (int i = 0; i < 30; ++i) history.push_back(price * (.85 + .08 * std::sin(i * .6 + index) + i * .003)); } return history; }; // a picture with a line of writing on its lower edge: every page of the bank begins with one const auto banner = [&](const std::string &art, double w, double h, const std::string &kicker, const std::string &value, const std::string &detail) { return "
" + img(art, w * 2.25, h * 2.25, false, true, 1800) + "
" + esc(pg_upper(kicker)) + "" + esc(value) + "" + (detail.empty() ? std::string() : "" + esc(detail) + "") + "
"; }; std::string out = "
"; if (bank_view_ == "savings") { page_title_ = "Sparbuch"; page_hint_ = "Waldpark Bank"; const auto deposit = snapshot.state.at("deposit").get(); out += "
" + banner("bank.scene.savings", 640, 648, "Guthaben auf dem Sparbuch", bank_money(deposit), share(in_deposit) + " des Vermögens") + "
Waldpark Bank · Sparbuch"; // the page of the passbook: what is written in it today for (const auto &[label, value] : std::array, 4>{{{"Guthaben", bank_money(deposit)}, {"Auf dem Konto", bank_money(in_cash)}, {"Vermögen gesamt", bank_money(wealth)}, {"Anteil Sparbuch", share(in_deposit)}}}) out += "
" + std::string(label) + "" + esc(value) + "
"; out += "" + std::string(deposit > 0 ? "Liegt sicher. Rest bleibt auf dem Konto." : in_cash > 0 ? "Noch nichts zurückgelegt!" : "Erst muss Geld aufs Konto.") + "
" + std::string(engine_mode_ ? "Einzahlungen und Abhebungen werden nach Bestätigung im Spielstand verbucht." : "Einzahlungen und Abhebungen werden hier als fiktive Vorschau verbucht.") + "
" + calc("Einzahlen", "deposit", "primary block") + calc("Abheben", "withdraw", "block") + "
"; } else if (bank_view_ == "properties") { // the board of an estate agent: every house a note pinned to it; what the club owns is stamped static constexpr std::array coming{"Stadthaus", "Penthouse", "Siedlungshaus", "Altbau-Reihenhaus"}; int64_t houses = 0; for (const auto &held : finance.at("properties")) houses += held.get(); page_title_ = "Immobilien"; page_hint_ = number(houses) + " im Bestand · Wert " + bank_money(in_property); out += "
"; for (int i = 0; i < 10; ++i) { const bool real = i < property_count; const auto count = real ? finance.at("properties")[size_t(i)].get() : 0; const auto name = real ? calm_name(assets.at("properties")[size_t(i)].at("name").get()) : std::string(coming[size_t(std::min(3, i - property_count))]); const auto price = real ? bank_money(assets.at("properties")[size_t(i)].at("price").get()) : std::string("Bald im Angebot"); const bool chosen = real && i == bank_property_; out += btn(real ? "bank-property" : "bank-soon", "" + img("bank.home." + std::to_string(i), 492, 382, false, true, 800) + (count > 0 ? "EIGENTÜMER" + (count > 1 ? "" + number(count) + " Stück" : std::string()) + "" : std::string()) + "" + esc(name) + "" + esc(price) + "", std::string("bk-home") + (chosen ? " sel" : "") + (i % 2 ? " odd" : ""), "id=\"bank-property-" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" aria-pressed=\"" + (chosen ? "true" : "false") + "\" aria-label=\"" + esc(name + ", " + price + (count > 0 ? ", " + number(count) + " im Bestand" : "")) + "\" " + (real ? "" : "disabled=\"disabled\"")); } const auto &offer = assets.at("properties")[size_t(bank_property_)]; const auto held = finance.at("properties")[size_t(bank_property_)].get(); const auto price = offer.at("price").get(); out += "
" + pg_kicker("Ausgewählt") + "

" + esc(calm_name(offer.at("name").get())) + "

" + esc(bank_money(price)) + "
" + kv("Im Bestand", number(held) + " Stück") + kv("Wert heute", esc(bank_money(held * price))) + kv("Anteil am Konto", in_cash > 0 ? one(price * 100.0 / in_cash) + " %" : std::string("—")) + (offer.contains("maxToBuy") ? kv("Bezahlbar", number(offer.at("maxToBuy").get()) + " Stück") : kv("Auf dem Konto", esc(bank_money(in_cash)))) + "
" + calc("Kaufen", "property-buy", "primary bk-wide") + calc("Verkaufen", "property-sell", "bk-wide", held > 0) + "Ein Haus am Brett anklicken, dann kaufen oder verkaufen.
"; } else if (bank_view_ == "stocks") { const auto move = [&](double from, double to) { if (from <= 0) return std::string("—"); const double change = (to - from) * 100.0 / from; return std::string(" 0.05 ? "up" : change < -0.05 ? "down" : "flat") + "\">" + (change > 0.05 ? "▲ +" : change < -0.05 ? "▼ −" : "") + one(std::abs(change)) + " %"; }; int held_papers = 0; for (const auto &held : finance.at("stocks")) if (held.get() > 0) ++held_papers; page_title_ = "Aktien"; page_hint_ = "Depotwert " + bank_money(in_stock); out += "
" + banner("bank.scene.stocks", 700, 96, "Depot", bank_money(in_stock), std::to_string(held_papers) + " von " + std::to_string(stock_count) + " Papieren im Bestand") + "
WERT30 TAGEKURSZUM VORTAGBESTAND
"; for (int i = 0; i < stock_count; ++i) { const auto &s = assets.at("stocks")[size_t(i)]; const auto history = course(i); std::string spark = ""; if (history.size() > 1) { const double lo = *std::min_element(history.begin(), history.end()), hi = std::max(lo + .01, *std::max_element(history.begin(), history.end())); std::string points; for (size_t at = 0; at < history.size(); ++at) points += scalar(4 + at * 232.0 / (history.size() - 1)) + "," + scalar(54 - (history[at] - lo) / (hi - lo) * 48) + " "; spark += "= history.front() ? up : down) + "\" stroke-width=\"4\" stroke-linejoin=\"round\" stroke-linecap=\"round\"/>"; } spark += ""; const bool chosen = i == bank_stock_; out += btn("bank-stock", "" + esc(s.at("name").get()) + "" + picture(renderer_.diagram("bank-spark-" + std::to_string(i) + "-" + theme_, spark, 480), 216, 54) + "" + number(s.at("price").get() / factor) + "" + move(s.at("previous").get(), s.at("price").get()) + "" + number(finance.at("stocks")[size_t(i)].get()) + "", std::string("bk-row") + (chosen ? " sel" : ""), "id=\"bank-stock-" + std::to_string(i) + "\" data-bank-stock=\"" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" aria-pressed=\"" + (chosen ? "true" : "false") + "\""); } out += "
"; // the chosen stock: its course with a five-day average, its range, and what the numbers say const auto &chosen = assets.at("stocks")[size_t(bank_stock_)]; const auto history = course(bank_stock_); const auto held = finance.at("stocks")[size_t(bank_stock_)].get(); out += "
" + pg_kicker("Ausgewähltes Papier") + "

" + esc(chosen.at("name").get()) + "

" + number(chosen.at("price").get() / factor) + "" + move(chosen.at("previous").get(), chosen.at("price").get()) + "
"; if (history.size() > 1) { const double lo = *std::min_element(history.begin(), history.end()), hi = *std::max_element(history.begin(), history.end()); const double floor = lo * .97, ceiling = std::max(floor + 1., hi * 1.03); const auto px = [&](size_t at) { return 30 + at * 860.0 / (history.size() - 1); }; const auto py = [&](double value) { return 450 - (value - floor) / (ceiling - floor) * 420; }; std::string svg = ""; for (int row = 0; row < 5; ++row) svg += ""; std::string points, average; size_t high_at = 0, low_at = 0; for (size_t at = 0; at < history.size(); ++at) { points += scalar(px(at)) + "," + scalar(py(history[at])) + " "; if (history[at] > history[high_at]) high_at = at; if (history[at] < history[low_at]) low_at = at; if (at >= 4) { double sum = 0; for (size_t back = 0; back < 5; ++back) sum += history[at - back]; average += scalar(px(at)) + "," + scalar(py(sum / 5)) + " "; } } svg += ""; double mean = 0, spread = 0; for (const double value : history) mean += value; mean /= double(history.size()); for (const double value : history) spread += (value - mean) * (value - mean); spread = mean > 0 ? std::sqrt(spread / double(history.size())) * 100.0 / mean : 0; const double trend = history.front() > 0 ? (history.back() - history.front()) * 100.0 / history.front() : 0; const double below = hi > 0 ? (hi - history.back()) * 100.0 / hi : 0; const auto tile = [](const std::string &label, const std::string &value, const std::string &tone = std::string()) { return "
" + value + "" + label + "
"; }; out += "
()) + "\">" + picture(renderer_.diagram("bank-stock-" + theme_, svg, 1840), 1098, 561) + "
KursFünf-Tage-SchnittHochTief
" + tile("Hoch", number(int64_t(std::llround(hi)))) + tile("Tief", number(int64_t(std::llround(lo)))) + tile(std::to_string(history.size()) + " Tage", (trend > 0 ? "+" : trend < 0 ? "−" : "") + one(std::abs(trend)) + " %", trend > 0.05 ? "ad-plus" : trend < -0.05 ? "ad-minus" : "") + tile("Schwankung", one(spread) + " %") + tile("Depotwert", esc(bank_money(held * chosen.at("price").get()))) + (chosen.contains("maxToBuy") ? tile("Kaufbar", number(chosen.at("maxToBuy").get())) : tile("Bestand", number(held))) + "
" + std::string(trend > 5 ? "Aufwärtstrend: " : trend < -5 ? "Abwärtstrend: " : "Seitwärtsbewegung: ") + "über " + std::to_string(history.size()) + " Tage " + (trend > 0 ? "+" : trend < 0 ? "−" : "") + one(std::abs(trend)) + " %. " + (below < 0.5 ? "Der Kurs steht am Hoch dieses Zeitraums." : "Der Kurs liegt " + one(below) + " % unter dem Hoch dieses Zeitraums.") + " " + (spread > 12 ? "Das Papier schwankt stark." : spread > 5 ? "Das Papier schwankt mäßig." : "Das Papier läuft ruhig.") + ""; } else out += "
Für dieses Papier liegt noch kein Kursverlauf vor.
"; out += "
" + calc("Kaufen", "stock-buy", "primary block") + calc("Verkaufen", "stock-sell", "block", held > 0) + "
"; } else if (bank_view_ == "players") { // the other humans of this career; dealings between them are not in the game yet const auto people = bank_players(); page_title_ = "Spieler"; page_hint_ = std::to_string(people.size()) + " Manager an einem Tisch"; out += "
" + banner("bank.scene.players", 700, 648, "Unter Managern", "Geschäfte mit Mitspielern", "Noch nicht im Spiel") + "
" + pg_kicker("Mitspieler"); for (const auto &person : people) { const auto *club = world_->find("clubs", person.value("club", "")); out += "
" + (club ? crest(club, 72) : std::string()) + "
" + esc(person.value("name", "")) + "" + esc(club ? world_->name(*club) : std::string()) + "
"; } out += "
Hier sollen Manager einander Geld leihen und Geschäfte abschließen können. Das Spiel kennt solche Geschäfte noch nicht; die Seite zeigt vorerst nur, wer mitspielt.
"; } else { // the bank: where the money lies, and the loan page_title_ = "Bank"; page_hint_ = "Waldpark Bank · Vermögen und Kredit"; struct Part { const char *label; int64_t value; const char *colour; }; const std::array parts{{{"Konto", std::max(0, in_cash), "#2b6f8f"}, {"Sparbuch", in_deposit, "#d08a1e"}, {"Immobilien", in_property, "#56633f"}, {"Aktien", in_stock, "#a8452f"}}}; const double around = 2 * 3.14159265358979 * 300; std::string ring = ""; double turned = 0; for (const auto &part : parts) { if (wealth <= 0 || part.value <= 0) continue; const double length = around * double(part.value) / double(wealth); if (part.value == wealth) { ring += ""; break; } ring += ""; turned += length; } ring += ""; out += "
" + banner("bank.scene.overview", 590, 196, "Vermögen", bank_money(wealth), "Konto, Sparbuch, Immobilien und Aktien") + "
" + picture(renderer_.diagram("bank-ring-" + theme_, ring, 900), 400, 400) + "
"; const Part *largest = &parts[0]; for (const auto &part : parts) { if (part.value > largest->value) largest = ∂ const double width = wealth > 0 ? std::max(0, part.value) * 100.0 / wealth : 0; out += "
" + part.label + "" + esc(bank_money(std::string(part.label) == "Konto" ? in_cash : part.value)) + "" + share(part.value) + "
"; } out += engine_mode_ ? kv("Kreditlinie", esc(bank_money(summary.at("credit_line").get()))) : kv("Kredit", "−" + esc(bank_money(summary.at("debt").get()))) + kv("Saldo", esc(bank_money(summary.at("net").get()))); out += "
"; if (wealth > 0) { const int64_t invested = in_deposit + in_property + in_stock; out += "Größter Posten: " + std::string(largest->label) + " mit " + share(largest->value) + " des Vermögens. " + (invested > 0 ? "Angelegt in Sparbuch, Immobilien und Aktien sind " + share(invested) + "." : std::string("Bisher ist nichts angelegt: Das ganze Vermögen liegt auf dem Konto.")); } else out += "Es ist kein Vermögen vorhanden."; out += "
"; if (engine_mode_) out += engine_credit_view(); else { const int term = finance.at("term"); std::string select = "
" + kv("Monatsrate, Beispiel", esc(bank_money(summary.at("monthly_rate").get()))) + kv("Maximaler Kredit", esc(bank_money(5000000 * finance_currency_factor(*world_)))) + "
" + picture(renderer_.diagram("bank-credit-" + theme_, svg, 1800), 1200, 360) + "
" + esc(bank_money(principal)) + " über " + std::to_string(term) + " Monate · " + (summary.at("schedule_example") == true ? "Beispiel, kein laufender Kredit" : "aktueller Kredit") + ". Fiktiver Kredit ohne Zins- und Ratenbuchungen.
" + calc("Aufnehmen", "loan", "primary block") + calc("Tilgen", "repay", "block") + "
"; } out += ""; } return out + "
"; }