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

+108
View File
@@ -8734,4 +8734,112 @@ void ModernSession::set_loan_return_offer_presenter(LoanReturnOfferPresenter pre
};
}
// --- the interface contract: commands (docs/new_engine/ui-anbindung.md) ----------------------
namespace {
const cli::Json* ui_field(const cli::Json& j, const char* key) {
const auto* o = j.object();
if (!o) return nullptr;
const auto it = o->find(key);
return it == o->end() ? nullptr : &it->second;
}
std::string ui_text(const cli::Json& j, const char* key) {
const cli::Json* v = ui_field(j, key);
return v && v->string() ? *v->string() : std::string{};
}
} // namespace
cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision) {
const std::string request_id = ui_text(request, "requestId");
const std::string kind = ui_text(request, "kind");
const auto refuse = [&](const std::string& code, const std::string& message, bool retryable = false) {
return modern::ui_error(request_id, code, message, revision, retryable);
};
if (ui_text(request, "messageType") != "command" || request_id.empty())
return refuse("invalid_command", "Kein gültiger Befehl (messageType command und requestId nötig).");
// a repeated requestId: the stored answer for the same command, otherwise a conflict
const std::string body = cli::dump_json(request);
if (const auto it = ui_answers_.find(request_id); it != ui_answers_.end())
return it->second.first == body ? it->second.second
: refuse("duplicate_request", "Diese requestId gehört zu einem anderen Befehl.");
if (const std::string career = ui_text(request, "careerId"); !career.empty() && career != g_.career_id)
return refuse("wrong_career", "Der Befehl gehört zu einer anderen Karriere.");
if (ui_text(request, "expectedRevision") != std::to_string(revision))
return refuse("stale_revision", "Die Ansicht ist veraltet. Bitte neu laden.", true);
const int manager = int(g_.memory.has(::bmh::layout::ActiveManager) ? g_.memory.u8(::bmh::layout::ActiveManager) : 0);
if (manager < 0 || size_t(manager) >= g_.manager_teams.size())
return refuse("no_active_manager", "Kein Manager am Schreibtisch.");
const cli::Json* owner = ui_field(request, "owner");
if (!owner || ui_text(*owner, "managerId") != modern::ui_manager_id(g_, manager))
return refuse("manager_not_active", "Dieser Manager sitzt nicht am Schreibtisch.");
const cli::Json* payload = ui_field(request, "payload");
if (!payload || !payload->object()) return refuse("invalid_payload", "Der Befehl braucht ein payload-Objekt.");
// a refused engine action leaves the state and both generators as they were
const ModernState saved_state = g_;
const WatcomRandom saved_rand = rand_;
const GameRandom saved_game = game_;
const uint64_t previous = revision;
const auto restore = [&] { g_ = saved_state; rand_ = saved_rand; game_ = saved_game; revision = previous; };
cli::Json answer;
std::string error;
if (kind == "camp.book") {
// payload {campId, focus?: {condition, technique, play}}: focus first (recovery follows), then the booking
const std::string camp = ui_text(*payload, "campId");
int resort = -1;
for (size_t i = 0; i < g_.training_camps.size(); ++i)
if (g_.training_camps[i].id == camp) resort = int(i);
if (resort < 0) return refuse("unknown_camp", "Unbekanntes Trainingslager: " + camp);
ui::TrainingCampRequest r;
r.manager_slot = manager;
r.runtime_club = g_.manager_teams[size_t(manager)];
const size_t rec = size_t(manager) * 0xb0c;
r.focus = {g_.manager_records.at(rec + 0xc1), g_.manager_records.at(rec + 0xc2), g_.manager_records.at(rec + 0xc3),
g_.manager_records.at(rec + 0xc4)};
bool ok = true;
if (const cli::Json* focus = ui_field(*payload, "focus"); focus && focus->object()) {
std::array<uint8_t, 4> f = r.focus;
const char* keys[3] = {"condition", "technique", "play"};
for (int k = 0; k < 3; ++k) {
const cli::Json* v = ui_field(*focus, keys[k]);
if (!v || !v->integer() || *v->integer() < 0 || *v->integer() > 15) return refuse("invalid_focus", "Schwerpunkte 0 bis 15.");
f[size_t(k)] = uint8_t(*v->integer());
}
f[3] = uint8_t(15 - (unsigned(f[0]) + f[1] + f[2]) / 3);
if (f != r.focus) {
r.kind = ui::TrainingCampRequestKind::set_focus;
r.focus = f;
r.revision = revision;
ok = dispatch_training_camp(r, revision, error);
}
}
if (ok) {
r.kind = ui::TrainingCampRequestKind::book;
r.resort = resort;
r.revision = revision;
ok = dispatch_training_camp(r, revision, error);
}
if (!ok) {
restore();
return refuse(error, "Buchung nicht möglich: " + error);
}
// an engine step after the booking: one revision for the whole command
revision = previous + 1;
answer = modern::ui_command_result(request_id, previous, revision, {"desktop", "camps", "squad"},
"Trainingslager gebucht: " + g_.training_camps[size_t(resort)].name);
} else {
return refuse("unknown_command", "Unbekannter Befehl: " + kind);
}
ui_answers_[request_id] = {body, answer};
ui_answer_order_.push_back(request_id);
if (ui_answer_order_.size() > 64) {
ui_answers_.erase(ui_answer_order_.front());
ui_answer_order_.erase(ui_answer_order_.begin());
}
return answer;
}
} // namespace bmh::modern::app