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 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 09:15:04 +01:00
1 parent 2ca6b21a94
commit f71f072774
3 files changed
+74 -1

No files matched your search

+52
View File
@@ -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<std::string, Expiry> 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<cli::Json::Object>(std::get<cli::Json::Object>(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);