// SPDX-License-Identifier: GPL-3.0-only 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 {}; 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_ = std::clamp(bank_owned_, 0, property_count - 1); if (finance.at("properties")[size_t(bank_owned_)] == 0) for (int i = 0; i < property_count; ++i) if (finance.at("properties")[size_t(i)] != 0) { bank_owned_ = i; break; } const auto actions = [&](const char *left, const char *lk, const char *right, const char *rk) { return ""; }; // A data set may carry the newer hall picture or only the first one; each has its own // places for the five services. const bool newer_hall = !renderer_.image("art.bank-hall-v2.png", false, 3000).token.empty(); std::string out = "
" + img(newer_hall ? "art.bank-hall-v2.png" : "art.bank-hall-v1.png", 2608, 1466.016746, false, false, 3000); if (bank_view_ == "entrance") { struct Service { const char *id, *label, *hint; double x, y, w, h; }; const std::array services = newer_hall ? std::array{{ {"properties", "Immobilien", "Immobilien – Gebäude ansehen, kaufen oder verkaufen.", 14, 23, 23, 43}, {"stocks", "Aktien", "Aktien – Kurse und Bestände ansehen, Aktien kaufen oder verkaufen.", 49, 4, 45, 26}, {"overview", "Finanzübersicht", "Finanzübersicht – Konto, Sparbuch, Immobilien, Aktien und Kredit im Überblick.", 44, 48, 14, 17}, {"savings", "Sparbuch", "Sparbuch – Guthaben ansehen, Geld einzahlen oder abheben.", 78, 55, 12, 11}, {"credit", "Kredit", "Kredit – Laufzeit wählen, einen Kredit aufnehmen oder tilgen.", 81, 32, 15, 22}}} : std::array{{ {"properties", "Immobilien", "Immobilien – Gebäude ansehen, kaufen oder verkaufen.", .5, 18.5, 17.5, 43.5}, {"stocks", "Aktien", "Aktien – Kurse und Bestände ansehen, Aktien kaufen oder verkaufen.", 30, 30, 13.5, 19.5}, {"overview", "Finanzübersicht", "Finanzübersicht – Konto, Sparbuch, Immobilien, Aktien und Kredit im Überblick.", 30.5, 49.5, 13, 14.5}, {"savings", "Sparbuch", "Sparbuch – Guthaben ansehen, Geld einzahlen oder abheben.", 85, 60, 6.5, 7.5}, {"credit", "Kredit", "Kredit – Laufzeit wählen, einen Kredit aufnehmen oder tilgen.", 85.3, 31.5, 13.7, 27.5}}}; out += "
"; for (const auto &s : services) out += btn("bank-view", std::string("") + s.label + "", "bank-hotspot", "id=\"bank-view-" + std::string(s.id) + "\" data-view=\"" + s.id + "\" data-bank-hint=\"" + esc(s.hint) + "\" aria-label=\"" + s.label + "\" style=\"left:" + scalar(s.x) + "%;top:" + scalar(s.y) + "%;width:" + scalar(s.w) + "%;height:" + scalar(s.h) + "%;\""); out += "
"; } out += "
"; if (bank_view_ == "entrance") return out + "

" + esc(bank_hint_.empty() ? "Ein Element in der Bankhalle auswählen." : bank_hint_) + "

"; // Every service of the bank shares one head: the house's name, the service, and tabs that // lead to the other services without a walk back through the hall. const std::array, 5> bank_tabs{{{"overview", "Übersicht"}, {"stocks", "Aktien"}, {"properties", "Immobilien"}, {"savings", "Sparbuch"}, {"credit", "Kredit"}}}; std::string service; for (const auto &[id, label] : bank_tabs) if (bank_view_ == id) service = label; out += "
Waldpark Bank" + service + "
"; 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 std::string ink = theme_ == "silver" ? "#233443" : "#e8eef2", faint = theme_ == "silver" ? "#4a433430" : "#dfeaf530", up = theme_ == "silver" ? "#3f7d63" : "#8fd3ad", down = theme_ == "silver" ? "#a23f45" : "#f0a6a7", gold = theme_ == "silver" ? "#9a7a3a" : "#d9bd7c", line = theme_ == "silver" ? "#25617a" : "#9ac7d8"; // 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; }; if (bank_view_ == "savings") { out += "
" + img("art.bank-passbook-v1.png", 2100, 1293.109375, false, false, 2400) + "

Waldpark Bank

Sparbuch

Bitte beachten Sie:

" "

" + std::string(engine_mode_ ? "Einzahlungen und Abhebungen werden nach Bestätigung im Spielstand verbucht." : "Einzahlungen und Abhebungen werden hier als fiktive Vorschau verbucht.") + "

" "

" + (engine_mode_ ? "Anteil am Vermögen: " + share(in_deposit) : std::string("2,5 % Beispielzins")) + "

Guthaben

" + esc(bank_money(snapshot.state.at("deposit").get())) + "

Auf dem Konto: " + esc(bank_money(in_cash)) + "

" + actions("Einzahlen", "deposit", "Abheben", "withdraw"); } else if (bank_view_ == "properties") { const auto &owned = assets.at("properties")[size_t(bank_owned_)], &offer = assets.at("properties")[size_t(bank_property_)]; const auto count = finance.at("properties")[size_t(bank_owned_)].get(); int64_t houses = 0; for (const auto &held : finance.at("properties")) houses += held.get(); const auto fact = [](const std::string &label, const std::string &value) { return "
" + label + "" + value + "
"; }; out += "

Dein Bestand

" + btn("property-owned-next", count ? img(engine_mode_ ? owned.at("image").get() : "bank.property." + std::to_string(bank_owned_), 1150, 653.5, false, false, 1400) : "Noch keine Immobilie im Bestand", "property-picture", "id=\"property-owned-next\" aria-label=\"Nächste eigene Immobilie\" " + std::string(count ? "" : "disabled")) + (count ? "

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

" + fact("Im Bestand", number(count) + " Stück") + fact("Wert heute", esc(bank_money(count * owned.at("price").get()))) : "

Kein Bestand

" + fact("Immobilienwert", esc(bank_money(in_property))) + fact("Anteil am Vermögen", share(in_property))) + "
Preise im Vergleich · " + number(houses) + " im Bestand
"; { int64_t dearest = 1; for (const auto &item : assets.at("properties")) dearest = std::max(dearest, item.at("price").get()); for (int i = 0; i < property_count; ++i) { const auto &item = assets.at("properties")[size_t(i)]; out += "
()) + "\">" + esc(calm_name(item.at("name").get())) + "
() * 100.0 / dearest) + "%;\">
" + esc(bank_money(item.at("price").get())) + "
"; } } out += "
"; out += "

Kaufangebot " + std::to_string(bank_property_ + 1) + " von " + std::to_string(property_count) + "

" + btn("property-next", img(engine_mode_ ? offer.at("image").get() : "bank.property." + std::to_string(bank_property_), 1150, 653.5, false, false, 1400), "property-picture", "id=\"property-next\" aria-label=\"Nächstes Immobilienangebot\"") + "

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

" + fact("Kaufpreis", esc(bank_money(offer.at("price").get()))) + fact("Anteil am Konto", in_cash > 0 ? one(offer.at("price").get() * 100.0 / in_cash) + " %" : std::string("—")) + (offer.contains("maxToBuy") ? fact("Bezahlbar", number(offer.at("maxToBuy").get()) + " Stück") : std::string()) + "

Das Bild anklicken, um das nächste Angebot zu sehen.

" + actions("Kaufen", "property-buy", "Verkaufen", "property-sell"); } 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)) + " %"; }; out += "
"; for (const auto *label : {"Wert", "30 Tage", "Kurs", "zum Vortag", "Bestand"}) out += "
" + std::string(label) + "
"; out += "
"; 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 += ""; out += "
" + btn("bank-stock", esc(s.at("name").get()), "stock-name", "id=\"bank-stock-" + std::to_string(i) + "\" data-bank-stock=\"" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" aria-pressed=\"" + (i == bank_stock_ ? "true" : "false") + "\"") + "
" + picture(renderer_.diagram("bank-spark-" + std::to_string(i), spark, 480), 240, 60) + "
" + number(s.at("price").get() / factor) + "
" + move(s.at("previous").get(), s.at("price").get()) + "
" + number(finance.at("stocks")[size_t(i)].get()) + "
"; } 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 double price = chosen.at("price").get() / factor; out += "

" + 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 470 - (value - floor) / (ceiling - floor) * 430; }; 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 held = finance.at("stocks")[size_t(bank_stock_)].get(); const auto tile = [](const std::string &label, const std::string &value, const char *tone = "") { return "
" + value + "" + label + "
"; }; out += "
()) + "\">" + picture(renderer_.diagram("bank-stock", svg, 1840), 920, 500) + "
" "
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 ? "up" : trend < -0.05 ? "down" : "") + 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))) + "
" "

Analyse

" + 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.") + "

"; (void)price; } else out += "

Für dieses Papier liegt noch kein Kursverlauf vor.

"; out += "
" + actions("Kaufen", "stock-buy", "Verkaufen", "stock-sell"); } else if (bank_view_ == "credit") { if (engine_mode_) return out + engine_credit_view() + "
"; out += "
" + img("art.bank-adviser-v1.png", 620, 515, false, false, 1000) + "
"; const auto fact = [&](const char *label, const std::string &value, int row) { constexpr std::array y{0, 83, 166, 273, 356}; return "
" + label + "" + value + "
"; }; out += fact("Restschuld", esc(bank_money(summary.at("debt").get())), 0) + fact("Beispielzins", "8 % pro Jahr", 1); std::string select = "", 2) + fact("Monatsrate, Beispiel", esc(bank_money(summary.at("monthly_rate").get())), 3) + fact("Maximaler Kredit", esc(bank_money(5000000 * finance_currency_factor(*world_))), 4) + "
"; const int term = finance.at("term"); const auto principal = summary.at("schedule_principal").get(); std::string svg = "" "" ""; for (int i = 0; i < 5; ++i) { const auto y = 30 + i * 77.5; svg += "" "" + esc(bank_money((principal * (4 - i) + 2) / 4)) + ""; } const int interval = term <= 12 ? 3 : term <= 24 ? 6 : 12; std::vector labels; for (int month = 0; month <= term; month += interval) labels.push_back(month); if (labels.back() != term) labels.push_back(term); for (const int month : labels) { svg += "" + (month == 0 ? "Heute" : std::to_string(month) + (month == term ? " Monate" : "")) + ""; } std::string points; for (const auto &point : summary.at("schedule")) points += scalar(220 + point.at("month").get() / term * 1960) + "," + scalar(340 - point.at("remaining").get() / principal * 310) + " "; svg += "" ""; out += "

Restschuld über die Laufzeit

" + esc(bank_money(principal)) + " → " + esc(bank_money(0)) + "

" + std::to_string(term) + " Monate

" + (summary.at("schedule_example") == true ? "Beispiel · kein laufender Kredit" : "Aktueller Kredit") + "

" + picture(renderer_.diagram("bank-credit", svg, 2400), 2432, 420) + "

Gleichmäßige monatliche Tilgung · Restschuld ohne Zinsen

" "

Fiktiver Kredit. Im POC erfolgen keine Zins- oder Ratenbuchungen.

" + actions("Aufnehmen", "loan", "Tilgen", "repay"); } else if (bank_view_ == "overview") { // where the money lies: a ring by share, the four places with their sums, and a reading of it struct Part { const char *label; int64_t value; const char *colour; }; const std::array parts{{{"Konto", std::max(0, in_cash), "#2f6f8f"}, {"Sparbuch", in_deposit, "#c9a24a"}, {"Immobilien", in_property, "#6f8a52"}, {"Aktien", in_stock, "#a8574f"}}}; 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 += "
" + picture(renderer_.diagram("bank-ring", ring, 1600), 860, 860) + "
Vermögen" + esc(bank_money(wealth)) + "
"; 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) + "
"; } if (engine_mode_) out += "
Kreditlinie" + esc(bank_money(summary.at("credit_line").get())) + "
"; else out += "
Kredit−" + esc(bank_money(summary.at("debt").get())) + "
" "
Saldo" + esc(bank_money(summary.at("net").get())) + "
"; out += "

Analyse

"; 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 += "

"; // the market at a glance: every stock's change over its recorded days std::vector> market; double widest = 1; for (int i = 0; i < stock_count; ++i) { const auto history = course(i); if (history.size() < 2 || history.front() <= 0) continue; const double change = (history.back() - history.front()) * 100.0 / history.front(); market.emplace_back(assets.at("stocks")[size_t(i)].at("name").get(), change); widest = std::max(widest, std::abs(change)); } if (!market.empty()) { out += "

Aktienmarkt · Veränderung über die aufgezeichneten Tage

"; for (const auto &[name, change] : market) { const double height = std::abs(change) / widest * 120; out += "
" + (change > 0 ? "+" + one(change) + " %
" : std::string()) + "
" + (change <= 0 ? "
−" + one(std::abs(change)) + " %" : std::string()) + "
" + esc(name) + "
"; } out += "
"; } out += "

" + std::string(engine_mode_ ? "Bestände und Kurse aus dem Spielstand." : "Erfundene Bestände · Änderungen betreffen ausschließlich die Bankvorschau.") + "

"; } return out + ""; }