Modern UI contract: camps read and the first command, camp.book

ModernSession::ui_command checks careerId, expectedRevision and owner, runs
the engine action and answers command_result or error. A refused command
restores the state and both generators; a repeated requestId gets the stored
answer, a different command under the same id duplicate_request. The camps
read lists maps and camps with a binding, draw-free quote per camp, focus and
the season's stays. Tests cover stale revision, foreign owner, unknown camp,
booking, repeat, conflict and a refused second booking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 08:25:54 +01:00
1 parent fec4c6c7e7
commit 99a48a463d
7 files changed
+252 -2

No files matched your search

+70
View File
@@ -12,6 +12,7 @@
#include "game/table.hpp"
#include "modern/content/world_package.hpp"
#include "modern/game/almanac.hpp"
#include "modern/game/desk_actions.hpp"
namespace bmh::modern {
@@ -253,6 +254,56 @@ J squad_data(const ModernState& g, const UiContext& c, int team, const std::stri
return O{{"kind", "squad"}, {"team", team_ref(g, team, today)}, {"players", std::move(players)}};
}
// The training camps (docs/new_engine/trainingslager.md): catalogue, the price for the own
// squad (base price + 5000 per player, no random draw: a binding quote), focus and bookings.
J camps_data(const ModernState& g, const UiContext& c, const std::string& revision) {
const size_t rec = size_t(c.manager) * kRecord;
const TrainingCampBookingBlock block = training_camp_booking_block(g, c.manager);
const char* reason = block == TrainingCampBookingBlock::booked_this_week ? "booked_this_week"
: block == TrainingCampBookingBlock::visit_limit ? "visit_limit"
: block == TrainingCampBookingBlock::invalid_request ? "no_club"
: nullptr;
const int64_t cash = money_of(g, c.manager);
A maps;
for (const CampMap& m : g.camp_maps)
maps.push_back(O{{"id", m.id}, {"name", text(m.name)}, {"guiAssetKey", "gui:map/" + (m.id.starts_with("map:") ? m.id.substr(4) : m.id)},
{"width", m.width ? J(*m.width) : J(nullptr)}, {"height", m.height ? J(*m.height) : J(nullptr)}});
A camps;
for (size_t i = 0; i < g.training_camps.size(); ++i) {
const TrainingCamp& t = g.training_camps[i];
J quote = nullptr;
if (block != TrainingCampBookingBlock::invalid_request) {
const int64_t price = training_camp_price(g, c.manager, int(i));
const int64_t base = t.base_price;
const char* blocked = reason ? reason : nullptr;
// book_money allows the credit line too; the read names only the plain case
quote = O{{"id", "quote:camp-" + revision + "-" + std::to_string(i)}, {"ownerManagerId", manager_id(g, c.manager)},
{"teamId", team_id(g, team_of(g, c.manager))}, {"expectedRevision", revision},
{"scopeId", "scope:desk-" + revision + "-" + std::to_string(c.manager)}, {"expiresOn", nullptr},
{"total", ui_money(price)}, {"totalBasis", "immediate"}, {"chargesNow", ui_money(price)},
{"breakdown", A{O{{"code", "base"}, {"amount", ui_money(base)}},
O{{"code", "squad"}, {"amount", ui_money(price - base)}}}},
{"allowed", blocked == nullptr}, {"reasonCode", blocked ? J(blocked) : J(nullptr)},
{"exceedsCash", price > cash}};
}
camps.push_back(O{{"id", t.id}, {"mapId", t.map}, {"name", text(t.name)}, {"place", t.place},
{"nationId", t.country.empty() ? J(nullptr) : J(t.country)},
{"position", t.x && t.y ? J(O{{"x", *t.x}, {"y", *t.y}}) : J(nullptr)},
{"quality", O{{"pitch", int(t.quality[0])}, {"fitness", int(t.quality[1])},
{"catering", int(t.quality[2])}, {"lodging", int(t.quality[3])}}},
{"guiImageKey", "gui:camp/" + std::to_string(i)}, {"worldImageAssetId", nullptr},
{"sponsorChance", t.sponsor_chance}, {"quote", std::move(quote)}});
}
const uint8_t stays = rec + 0x21f < g.manager_records.size() ? g.manager_records[rec + 0x21f] : 0;
O focus;
if (rec + 0xc4 < g.manager_records.size())
focus = O{{"condition", int(g.manager_records[rec + 0xc1])}, {"technique", int(g.manager_records[rec + 0xc2])},
{"play", int(g.manager_records[rec + 0xc3])}, {"recovery", int(g.manager_records[rec + 0xc4])}};
return O{{"kind", "camps"}, {"maps", std::move(maps)}, {"camps", std::move(camps)}, {"focus", std::move(focus)},
{"staysThisSeason", int(stays & 0x7f)}, {"staysLimit", 3}, {"bookedThisWeek", (stays & 0x80) != 0},
{"availability", reason ? unavailable(reason) : available()}};
}
} // namespace
void ensure_ui_identities(ModernState& g) {
@@ -269,6 +320,24 @@ void ensure_ui_identities(ModernState& g) {
if (id.empty()) id = "manager:" + std::to_string(++g.manager_serial);
}
cli::Json ui_command_result(const std::string& request_id, uint64_t previous, uint64_t next,
const std::vector<std::string>& changed_reads, const std::string& feedback) {
A changed;
for (const auto& k : changed_reads) changed.push_back(k);
return O{{"messageType", "command_result"}, {"requestId", request_id}, {"accepted", true},
{"previousRevision", std::to_string(previous)}, {"newRevision", std::to_string(next)},
{"outcome", next == previous ? "no_change" : "applied"}, {"changedReadKinds", std::move(changed)},
{"ownershipChange", nullptr}, {"feedback", text(feedback)}};
}
cli::Json ui_error(const std::string& request_id, const std::string& code, const std::string& message,
uint64_t revision, bool retryable) {
return O{{"messageType", "error"}, {"requestId", request_id}, {"code", code}, {"message", text(message)},
{"currentRevision", std::to_string(revision)}, {"retryable", retryable}, {"stateChanged", false}};
}
std::string ui_manager_id(const ModernState& g, int manager) { return manager_id(g, manager); }
cli::Json ui_money(int64_t euros) {
return O{{"minor", std::to_string(euros * 100)}, {"currency", "EUR"}, {"minorUnits", 2}};
}
@@ -286,6 +355,7 @@ cli::Json ui_read(const ModernState& g, const UiContext& c, const cli::Json& req
const std::string today = *std::get<O>(m.value).at("date").string();
if (kind == "session") return snapshot(request_id, std::move(m), session_data(g, c));
if (kind == "desktop") return snapshot(request_id, std::move(m), desktop_data(g, c, today));
if (kind == "camps") return snapshot(request_id, std::move(m), camps_data(g, c, std::to_string(c.state_revision)));
if (kind == "squad") {
const int team = requested_team(g, c, request);
if (team < 0 || !fields_players(g, team))