Modern UI contract: national friendlies
At the national coach's desk the friendly read quotes a match against another nation on a copy of the Watcom generator (price, payer, ratings, free dates); friendly.book draws the same ratings and books it. The opponent bound of the national quote follows the world layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d3d526c2e4
commit
d943494ef4
4 files changed
+107
-7
No files matched your search
@@ -8758,6 +8758,18 @@ std::string ui_text(const cli::Json& j, const char* key) {
|
||||
return v && v->string() ? *v->string() : std::string{};
|
||||
}
|
||||
|
||||
// The runtime team a contract team id names (a world id or "team:<n>"), or -1.
|
||||
int ui_team_index(const ModernState& g, const std::string& id) {
|
||||
if (id.empty()) return -1;
|
||||
for (int t = 0; t < int(g.world_club_id.size()); ++t)
|
||||
if (g.world_club_id[size_t(t)] == id) return t;
|
||||
if (id.starts_with("team:")) try {
|
||||
const int t = std::stoi(id.substr(5));
|
||||
if (t >= 0 && t < world_layout().total_teams()) return t;
|
||||
} catch (const std::exception&) {}
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
cli::Json ModernSession::read_live(const cli::Json& request) const {
|
||||
@@ -8929,10 +8941,42 @@ cli::Json ModernSession::ui_read(const cli::Json& request, uint64_t revision) co
|
||||
const int manager = context.manager;
|
||||
if (manager < 0 || size_t(manager) >= g_.manager_teams.size() ||
|
||||
(kind != "lineup" && kind != "tactics" && kind != "training" && kind != "bank" && kind != "contracts" &&
|
||||
kind != "advertising" && kind != "stadium" && kind != "transfer" && kind != "postmatch" && kind != "credit"))
|
||||
kind != "advertising" && kind != "stadium" && kind != "transfer" && kind != "postmatch" && kind != "credit" &&
|
||||
!(kind == "friendly" && g_.manager_teams[size_t(manager)] == no_team())))
|
||||
return modern::ui_read(g_, context, request);
|
||||
if (const std::string wanted = ui_text(request, "managerId"); !wanted.empty() && wanted != modern::ui_manager_id(g_, manager))
|
||||
return modern::ui_error(request_id, "manager_not_active", "Dieser Manager sitzt nicht am Schreibtisch.", revision, false);
|
||||
if (kind == "friendly") {
|
||||
// the national coach's friendly (menu 6): the quote rates both teams with a draw of the
|
||||
// Watcom generator; the read quotes on a copy of it (the session's generator stays as it
|
||||
// is), the booking later draws the same numbers. Price, payer and the free dates.
|
||||
const int opponent = ui_team_index(g_, ui_text(request, "opponentId"));
|
||||
const cli::Json* home_field = ui_field(request, "home");
|
||||
const bool home = !home_field || !std::holds_alternative<bool>(home_field->value) || std::get<bool>(home_field->value);
|
||||
if (opponent < 0) return modern::ui_error(request_id, "unknown_team", "opponentId nennt kein Team.", revision, false);
|
||||
const NationalFriendlyOwner owner{1, revision, manager, no_team(), 6}; // request_id 1: the contract has its own ids
|
||||
WatcomRandom staged = rand_;
|
||||
const NationalFriendlyQuote q = modern::quote_national_friendly_match(g_, owner, opponent, home, staged);
|
||||
if (q.terms.status != FriendlyQuoteStatus::quoted)
|
||||
return modern::ui_error(request_id, "friendly_unavailable", "Gegen dieses Team ist kein Freundschaftsspiel möglich.", revision, false);
|
||||
A dates;
|
||||
for (int day = g_.season_day() + 1; day <= 365; ++day) {
|
||||
const FriendlyDateStatus st = modern::national_friendly_match_date_status(g_, q, owner, staged, uint16_t(day));
|
||||
if (st != FriendlyDateStatus::available && st != FriendlyDateStatus::available_with_calendar_event) continue;
|
||||
const AlmanacDate d = almanac_date(g_.season_start + uint32_t(day), g_.display_start_year);
|
||||
char text[16];
|
||||
std::snprintf(text, sizeof text, "%04d-%02d-%02d", d.year, d.month, d.day);
|
||||
dates.push_back(O{{"date", text}, {"withCalendarEvent", st == FriendlyDateStatus::available_with_calendar_event}});
|
||||
}
|
||||
const auto team = [&](int t) {
|
||||
return cli::Json(O{{"id", modern::ui_team_id(g_, t)}, {"name", ui::original_to_utf8(g_.club_name(t))}});
|
||||
};
|
||||
return modern::ui_snapshot(g_, context, request_id,
|
||||
O{{"kind", "friendly"}, {"national", true}, {"home", team(q.terms.home_team)}, {"away", team(q.terms.away_team)},
|
||||
{"cost", modern::ui_money(q.terms.cost)},
|
||||
{"payerManagerId", q.terms.payer_manager >= 0 ? cli::Json(modern::ui_manager_id(g_, q.terms.payer_manager)) : cli::Json(nullptr)},
|
||||
{"opponentHuman", q.terms.opponent_manager >= 0}, {"ratings", A{q.own_rating, q.opponent_rating}}, {"dates", std::move(dates)}});
|
||||
}
|
||||
if (kind == "postmatch") {
|
||||
// the after-match steps of the interface for the last session.advance: results (every match
|
||||
// of the kickoff), report (the matches of human managers), the eleven of the day (the
|
||||
@@ -10162,7 +10206,8 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision
|
||||
}
|
||||
} else if (kind == "friendly.book" || kind == "fixture.reschedule") {
|
||||
const int team = g_.manager_teams[size_t(manager)];
|
||||
if (team < 0 || team >= league_clubs()) return refuse("no_club", "Am Schreibtisch des Bundestrainers gibt es das nicht.");
|
||||
const bool national = team == no_team() && kind == "friendly.book";
|
||||
if (!national && (team < 0 || team >= league_clubs())) return refuse("no_club", "Am Schreibtisch des Bundestrainers gibt es das nicht.");
|
||||
// a date of this season as its season day (0..365)
|
||||
const auto day_of = [&](const std::string& iso) -> int {
|
||||
for (int day = 0; day <= 365; ++day) {
|
||||
@@ -10177,7 +10222,38 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision
|
||||
const cli::Json* v = ui_field(*payload, key);
|
||||
return v && std::holds_alternative<bool>(v->value) && std::get<bool>(v->value);
|
||||
};
|
||||
if (kind == "friendly.book") {
|
||||
if (national) {
|
||||
// the national coach's friendly: the quote draws its ratings from the Watcom generator
|
||||
// (as the friendly read previewed), then the booking on the chosen date
|
||||
const int opponent = ui_team_index(g_, ui_text(*payload, "opponentId"));
|
||||
const int day = day_of(ui_text(*payload, "date"));
|
||||
if (opponent < 0 || day < 0) return refuse("invalid_payload", "opponentId und date (dieser Saison) nötig.");
|
||||
const cli::Json* home = ui_field(*payload, "home");
|
||||
const NationalFriendlyOwner owner{1, revision, manager, no_team(), 6}; // request_id 1: the contract has its own ids
|
||||
NationalFriendlyBookingRequest r;
|
||||
r.owner = owner;
|
||||
r.quote = modern::quote_national_friendly_match(g_, owner, opponent,
|
||||
!home || !std::holds_alternative<bool>(home->value) || std::get<bool>(home->value), rand_);
|
||||
if (r.quote.terms.status != FriendlyQuoteStatus::quoted) {
|
||||
restore();
|
||||
return refuse("friendly_unavailable", "Gegen dieses Team ist kein Freundschaftsspiel möglich.");
|
||||
}
|
||||
r.season_day = uint16_t(day);
|
||||
r.accept_opponent = r.quote.terms.opponent_manager < 0 || flag("opponentConsent");
|
||||
r.consenting_manager = r.quote.terms.opponent_manager;
|
||||
r.accept_cost = flag("acceptCost");
|
||||
r.approving_manager = r.quote.terms.payer_manager;
|
||||
const NationalFriendlyBookingResult result = modern::book_national_friendly_match(g_, r, rand_, game_);
|
||||
if (result.status != FriendlyBookingStatus::booked) {
|
||||
static constexpr const char* kStatus[11] = {"booked", "declined", "opponent_declined", "insufficient_funds", "invalid_manager",
|
||||
"invalid_opponent", "invalid_date", "date_unavailable", "plan_full", "stale_quote", "invalid_request"};
|
||||
restore();
|
||||
return refuse(kStatus[size_t(result.status)], "Das Freundschaftsspiel kam nicht zustande.");
|
||||
}
|
||||
revision = previous + 1;
|
||||
answer = modern::ui_command_result(request_id, previous, revision, {"calendar", "desktop"},
|
||||
result.national_bonus_awarded ? "Länderspiel vereinbart (Bonus)." : "Länderspiel vereinbart.");
|
||||
} else if (kind == "friendly.book") {
|
||||
// {opponentId, home?, date, acceptCost, opponentConsent?}: the quote of the friendly read
|
||||
// on a free date; the payer accepts the cost, a human opponent's manager consents
|
||||
const std::string opponent_id = ui_text(*payload, "opponentId");
|
||||
|
||||
Reference in new issue
Block a user