From f71f0727748d814e7e43510efd09bc5615d73fe5 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 09:15:04 +0100 Subject: [PATCH] Modern UI contract: running-out contracts during session.advance The original asks for a running-out contract in the middle of the calendar. session.advance takes the answers in contractExpiry (renew with years and salary, or release); a contract without an answer rolls the run back and decision_pending names the player, the interface asks and sends the advance again. The test plays two seasons of kickoffs answering every request. Co-Authored-By: Claude Opus 5.5 --- docs/new_engine/ui-anbindung.md | 2 +- src/modern/app/modern_session.cpp | 52 ++++++++++++++++++++++++++++++ tests/modern_ui_contract_tests.cpp | 21 ++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/docs/new_engine/ui-anbindung.md b/docs/new_engine/ui-anbindung.md index 7337b0d..5bbc13f 100644 --- a/docs/new_engine/ui-anbindung.md +++ b/docs/new_engine/ui-anbindung.md @@ -55,7 +55,7 @@ Die UI ruft diese über `DataSource::read_almanac` und `read_statistics` direkt | Command | payload | Wirkung | |---|---|---| | `camp.book` | `campId`, optional `focus {condition, technique, play}` (0–15; Erholung folgt) | setzt die Schwerpunkte und bucht das Lager wie der Schreibtisch; Fehler `unknown_camp`, `invalid_focus`, `booked_this_week`, `visit_limit`, `insufficient_funds` | -| `session.advance` | `{}` | der Anstoß am Schreibtisch: der Kalender läuft bis zum nächsten Schreibtisch eines Menschen, zum Saisonende oder zum Karriereende (Spiele noch ohne Live-Ansicht); `outcome` `career_ended` am Ende; Fehler `decision_pending` (eine Entscheidung am Schreibtisch fehlt, z. B. ein auslaufender Vertrag), `advance_failed` | +| `session.advance` | optional `contractExpiry {playerId: {action: "renew", years, monthlySalary} | {action: "release"}}` | der Anstoß am Schreibtisch: der Kalender läuft bis zum nächsten Schreibtisch eines Menschen, zum Saisonende oder zum Karriereende (Spiele noch ohne Live-Ansicht); `outcome` `career_ended` am Ende. Läuft unterwegs ein Vertrag aus (das Original fragt in dem Moment), gilt die mitgeschickte Antwort; fehlt sie, wird der Lauf zurückgerollt und `decision_pending` nennt in `message.args` `{decision: "contract_expiry", playerId}` - die UI fragt und schickt den Anstoß mit der Antwort erneut. Sonst `advance_failed` | | `quote.request` | `kind: contract`, `playerId` | fragt die Forderungen des Spielers ab (zieht Zufall wie am Schreibtisch); `outcome: offer_pending`, die Forderungen stehen danach im `contracts`-Read | | `contract.offer` | `quoteId`, `years` 1–4, `monthlySalary` (`Money`) | das Angebot gegen die Forderung; `outcome` `applied` (verlängert) oder `offer_rejected`; das Angebot ist danach verbraucht (`stale_quote`). Versicherung und Prämien folgen | | `bank.commit` | `action` `deposit`/`withdraw` mit `amount` (`Money`, ganze Euro) oder `buy`/`sell` mit `assetId` (`stock:`, `fund:`) und `count` | wie die Bank am Schreibtisch; Fehler `invalid_amount`, `amount_unavailable`, `invalid_share_count`, `insufficient_funds`, `investment_limit`, `invalid_fund_count` u. a. | diff --git a/src/modern/app/modern_session.cpp b/src/modern/app/modern_session.cpp index 20b2c7d..7cca294 100644 --- a/src/modern/app/modern_session.cpp +++ b/src/modern/app/modern_session.cpp @@ -9141,13 +9141,65 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision } else if (kind == "session.advance") { // payload {}: the kickoff of the desk - the calendar runs (matches without live view) to the // next desk of a human manager, the season end desk or the end of the career + // contractExpiry {playerId: {action: "renew", years 1-4, monthlySalary: Money} | {action: "release"}}: + // the answers to contracts running out on the way (the original asks at that moment). A + // contract without an answer stops the run: it is rolled back and decision_pending names + // the player, the interface asks and sends the advance again with the answer. + struct Expiry { bool renew = false; int term = 0; uint32_t salary = 0; }; + std::map answers; + if (const cli::Json* e = ui_field(*payload, "contractExpiry"); e && e->object()) + for (const auto& [id, value] : *e->object()) { + Expiry x; + const std::string action = ui_text(value, "action"); + if (action == "renew") { + const cli::Json* years = ui_field(value, "years"); + const cli::Json* salary = ui_field(value, "monthlySalary"); + int64_t cents = -1; + try { cents = salary ? std::stoll(ui_text(*salary, "minor")) : -1; } catch (const std::exception&) { cents = -1; } + if (!years || !years->integer() || *years->integer() < 1 || *years->integer() > 4 || cents < 0 || cents % 100) + return refuse("invalid_payload", "contractExpiry: renew braucht years 1-4 und monthlySalary."); + x.renew = true, x.term = int(*years->integer() - 1), x.salary = uint32_t(cents / 100); + } else if (action != "release") { + return refuse("invalid_payload", "contractExpiry: action ist renew oder release."); + } + answers[id] = x; + } + std::string pending_player; + auto previous_hook = g_.contract_screen; + const bool previous_installed = contract_expiry_presenter_installed_; + set_contract_expiry_presenter([&](modern::ModernState& state, WatcomRandom& random, int m, int row) -> int { + const int team = m >= 0 && size_t(m) < state.manager_teams.size() ? state.manager_teams[size_t(m)] : -1; + const SquadRange range = squad_range(team); + const int p = size_t(row) < range.count ? state.squad_slots[range.first + size_t(row)] & 0x0fff : 0x0fff; + const std::string id = p == 0x0fff ? std::string{} : modern::ui_player_id(state, p); + const auto it = answers.find(id); + if (it == answers.end()) { pending_player = id; return -2; } // ask the interface + if (!it->second.renew) return 0; // released + try { + const auto demands = modern::contract_demands(state, m, row, false, random); + const uint32_t demand = demands[size_t(it->second.term)]; + if (demand == 0 || demand == UINT32_MAX) return 0; + return modern::contract_offer(state, m, row, it->second.term, it->second.salary, demand, false, true, random, nullptr) ? 1 : 0; + } catch (const std::exception&) { + return -2; + } + }); ui::DeskRequest kickoff; kickoff.kind = ui::DeskRequestKind::advance_turn; kickoff.manager_slot = manager; kickoff.revision = revision; const TurnAdvanceResult r = dispatch_kickoff(kickoff, revision, error); + g_.contract_screen = previous_hook; // the session's own presenter again + contract_expiry_presenter_installed_ = previous_installed; if (r.kind == TurnDeskKind::Failed || r.kind == TurnDeskKind::WaitingAtDesk) { restore(); + g_.contract_screen = previous_hook; + if (!pending_player.empty()) { + cli::Json pending = refuse("decision_pending", "Ein Vertrag läuft aus: Verlängern oder freigeben?"); + auto& message = std::get(std::get(pending.value)["message"].value); + message["args"] = cli::Json::Object{{"decision", "contract_expiry"}, {"playerId", pending_player}}; + return pending; + } const std::string reason = r.reason.empty() ? error : r.reason; return refuse(r.kind == TurnDeskKind::WaitingAtDesk ? "decision_pending" : "advance_failed", "Weiter nicht möglich: " + reason); diff --git a/tests/modern_ui_contract_tests.cpp b/tests/modern_ui_contract_tests.cpp index 9dab75b..47b22b3 100644 --- a/tests/modern_ui_contract_tests.cpp +++ b/tests/modern_ui_contract_tests.cpp @@ -273,6 +273,27 @@ int main(int argc, char** argv) { const std::string date_after = s(at(at(session.ui_read(O{{"kind", "desktop"}}, rev), "meta"), "date")); check(date_after > date_before, "the date moved on"); } + // a season of kickoffs: running-out contracts stop the run with decision_pending and the + // player; the advance is sent again with the answer (here: release) + { + O expiry; + int decisions = 0, advances = 0; + for (int k = 0; k < 200 && advances < 120; ++k) { + const uint64_t before = rev; + const J done = session.ui_command(cmd("request:s" + std::to_string(k), "session.advance", O{{"contractExpiry", expiry}}), rev); + if (s(at(done, "messageType")) == "command_result") { ++advances; continue; } + const std::string code = s(at(done, "code")); + const std::string player = s(at(at(at(done, "message"), "args"), "playerId")); + check(code == "decision_pending" && !player.empty() && rev == before, "a pending contract names the player, nothing changed"); + if (code != "decision_pending" || player.empty() || expiry.count(player)) break; + expiry[player] = O{{"action", "release"}}; + ++decisions; + } + check(advances == 120, "two seasons of kickoffs with answered contract decisions"); + check(decisions > 0, "running-out contracts were asked for"); + std::printf("(date now %s)\n", s(at(at(session.ui_read(O{{"kind", "desktop"}}, rev), "meta"), "date")).c_str()); + std::printf("(%d contract decisions answered)\n", decisions); + } // almanac view after the kickoffs: index, calendar, day, match { const J index = session.read_almanac(O{{"kind", "index"}});