Modern UI contract: envelope, career and manager ids, session and desktop reads

First engine delivery for the Modern interface handoff (docs/new_ui/integration,
issue #15): ModernSession::ui_read answers in the draft envelope with careerId,
worldId, stateRevision, date, season, scope and Money in minor units. Careers and
human managers get persistent ids (saved in state/modern.json, older saves get
them on load). Reads: session and desktop. Engine decisions on the P0 points in
docs/new_engine/ui-anbindung.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 08:17:18 +01:00
1 parent 9061123c7e
commit 3336d01004
9 files changed
+342 -2

No files matched your search

+218
View File
@@ -0,0 +1,218 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/content/ui_contract.hpp"
#include <algorithm>
#include <cstdio>
#include <random>
#include <string>
#include <variant>
#include "formats/executable_layout.hpp"
#include "game/table.hpp"
#include "modern/content/world_package.hpp"
#include "modern/game/almanac.hpp"
namespace bmh::modern {
namespace {
using J = cli::Json;
using A = J::Array;
using O = J::Object;
constexpr const char* kContractVersion = "bmh-modern-ui@draft1";
constexpr size_t kRecord = 0xb0c;
const std::string& entry(const std::vector<std::string>& v, size_t i) {
static const std::string none;
return i < v.size() ? v[i] : none;
}
const J* field(const J& request, const char* key) {
const auto* o = request.object();
if (!o) return nullptr;
const auto it = o->find(key);
return it == o->end() ? nullptr : &it->second;
}
std::string text_field(const J& request, const char* key) {
const J* v = field(request, key);
return v && v->string() ? *v->string() : std::string{};
}
J text(const std::string& s) { return O{{"key", nullptr}, {"text", s}, {"locale", "de"}, {"args", O{}}}; }
J available() { return O{{"status", "available"}, {"reasonCode", nullptr}, {"missingFields", A{}}}; }
J unavailable(const char* reason) { return O{{"status", "unavailable"}, {"reasonCode", reason}, {"missingFields", A{}}}; }
std::string iso(const AlmanacDate& d) {
char out[16];
std::snprintf(out, sizeof out, "%04d-%02d-%02d", d.year, d.month, d.day);
return out;
}
// Managers with a human at their desk: during the national coach's takeover the coach sits in
// slot 0 and the others wait (OriginalManagerCount keeps their number).
int human_managers(const ModernState& g) {
if (g.memory.has(::bmh::layout::CoachActive) && g.memory.u8(::bmh::layout::CoachActive) != 0 &&
g.memory.has(::bmh::layout::OriginalManagerCount))
return g.memory.u8(::bmh::layout::OriginalManagerCount);
return int(g.manager_teams.size());
}
std::string manager_name(const ModernState& g, int manager) {
std::string name;
const size_t at = size_t(manager) * kRecord + 0xa8;
for (size_t i = 0; i < 30 && at + i < g.manager_records.size() && g.manager_records[at + i]; ++i)
name.push_back(char(g.manager_records[at + i]));
return almanac_utf8(name);
}
int64_t money_of(const ModernState& g, int manager) {
const size_t at = size_t(manager) * kRecord + 0x314;
if (at + 3 >= g.manager_records.size()) return 0;
return int32_t(uint32_t(g.manager_records[at]) | uint32_t(g.manager_records[at + 1]) << 8 |
uint32_t(g.manager_records[at + 2]) << 16 | uint32_t(g.manager_records[at + 3]) << 24);
}
int team_of(const ModernState& g, int manager) {
return manager >= 0 && size_t(manager) < g.manager_teams.size() ? g.manager_teams[size_t(manager)] : -1;
}
std::string team_id(const ModernState& g, int team) {
std::string id = entry(g.world_club_id, size_t(team));
return id.empty() ? "team:" + std::to_string(team) : id;
}
// TeamRef of the contract: id, kind, name, shortName, nationId, crestAssetId, nameAsOf.
J team_ref(const ModernState& g, int team, const std::string& as_of) {
if (team < 0) return nullptr;
std::string name = entry(g.world_club_name, size_t(team)), short_name = entry(g.world_club_short, size_t(team));
if (name.empty()) name = almanac_utf8(g.club_name(team));
if (short_name.empty()) short_name = name;
const bool national = team >= no_team();
return O{{"id", team_id(g, team)}, {"kind", national ? "national" : "club"}, {"name", name},
{"shortName", short_name}, {"nationId", nullptr}, {"crestAssetId", nullptr}, {"nameAsOf", as_of}};
}
std::string manager_id(const ModernState& g, int manager) {
const std::string& id = entry(g.manager_ids, size_t(manager));
return id.empty() ? "manager:slot-" + std::to_string(manager) : id;
}
J manager_json(const ModernState& g, int manager, bool at_desk) {
const int team = team_of(g, manager);
const size_t at = size_t(manager) * kRecord;
const int portrait = at + 0x221 < g.manager_records.size() ? g.manager_records[at + 0x221] : 0;
return O{{"id", manager_id(g, manager)}, {"slot", manager}, {"name", manager_name(g, manager)},
{"control", "human"}, {"status", at_desk ? "active" : "waiting"},
{"teamId", team >= 0 ? J(team_id(g, team)) : J(nullptr)},
{"portraitGuiKey", "gui:manager/photo/" + std::to_string(portrait)}, {"portraitStyle", "photo"},
{"strengthChoice", nullptr}, {"employments", A{}}};
}
J meta(const ModernState& g, const UiContext& c) {
const AlmanacDate today = almanac_date(g.date, g.display_start_year);
const int start = g.display_start_year + g.season, next = (start + 1) % 100;
const std::string label = std::to_string(start) + "/" + (next < 10 ? "0" : "") + std::to_string(next);
const int team = team_of(g, c.manager);
const bool national = team == no_team();
const std::string revision = std::to_string(c.state_revision);
const World* world = current_world();
return O{{"contractVersion", kContractVersion}, {"draft", true}, {"sourceRevision", nullptr},
{"careerId", g.career_id.empty() ? J(nullptr) : J(g.career_id)},
{"worldId", world ? J(world->id) : J(nullptr)},
{"worldRevision", world ? J(world->data_revision) : J(nullptr)},
{"stateRevision", revision}, {"projectionRevision", revision}, {"locale", "de"},
{"date", iso(today)},
{"season", O{{"id", "season:" + std::to_string(start) + "-" + (next < 10 ? "0" : "") + std::to_string(next)},
{"startDate", iso(AlmanacDate{start, 7, 1})}, {"endDate", iso(AlmanacDate{start + 1, 6, 30})},
{"label", label}, {"completed", false}}},
{"scope", O{{"managerId", manager_id(g, c.manager)},
{"teamId", team >= 0 ? J(team_id(g, team)) : J(nullptr)},
{"role", national ? "national" : "club"},
{"scopeId", "scope:desk-" + revision + "-" + std::to_string(c.manager)},
{"allowedCommands", A{}}}},
{"availability", available()},
{"currency", O{{"code", "EUR"}, {"minorUnits", 2}}}};
}
J snapshot(const std::string& request_id, J meta_json, J data) {
return O{{"messageType", "snapshot"}, {"requestId", request_id}, {"meta", std::move(meta_json)}, {"data", std::move(data)}};
}
J failure(const std::string& request_id, const char* code, const std::string& message, uint64_t revision) {
return O{{"messageType", "error"}, {"requestId", request_id}, {"code", code}, {"message", text(message)},
{"currentRevision", std::to_string(revision)}, {"retryable", false}, {"stateChanged", false}};
}
J session_data(const ModernState& g, const UiContext& c) {
A managers;
for (int m = 0; m < human_managers(g); ++m) managers.push_back(manager_json(g, m, m == c.manager));
return O{{"kind", "session"}, {"sessionState", "desktop"}, {"activeManagerId", manager_id(g, c.manager)},
{"managers", std::move(managers)}};
}
J desktop_data(const ModernState& g, const UiContext& c, const std::string& today) {
const int team = team_of(g, c.manager);
const bool national = team == no_team();
O data{{"kind", "desktop"}, {"manager", manager_json(g, c.manager, true)}, {"team", team_ref(g, team, today)}};
// the account: the bank's savings and credit follow with the bank read
data["accounts"] = O{{"cash", ui_money(money_of(g, c.manager))}, {"savings", nullptr}, {"debt", nullptr},
{"netAssets", nullptr}};
J next = nullptr;
if (c.next_opponent && *c.next_opponent >= 0) {
J own = team_ref(g, team, today), other = team_ref(g, *c.next_opponent, today);
next = O{{"competition", text(c.next_competition)}, {"home", c.next_away ? other : own},
{"away", c.next_away ? own : other}, {"status", "scheduled"}, {"result", nullptr}};
}
data["nextFixture"] = std::move(next);
// ui/desk_state.cpp: at the national coach's desk there is no toto, bank, camp, training or stadium
O menus;
for (const char* menu : {"squad", "lineup", "tactics", "training", "camps", "transfer", "contracts", "bank", "balance",
"advertising", "environment", "stadium", "toto", "calendar", "statistics", "almanac"}) {
const std::string m = menu;
const bool closed = national && (m == "toto" || m == "bank" || m == "camps" || m == "training" || m == "stadium");
menus[m] = closed ? unavailable("national_desk") : available();
}
data["menuAvailability"] = std::move(menus);
return data;
}
} // namespace
void ensure_ui_identities(ModernState& g) {
if (g.career_id.empty()) {
std::random_device device;
const uint64_t value = uint64_t(device()) << 32 | device();
char id[32];
std::snprintf(id, sizeof id, "career:%016llx", static_cast<unsigned long long>(value));
g.career_id = id;
}
const size_t humans = size_t(std::max(0, human_managers(g)));
if (g.manager_ids.size() < humans) g.manager_ids.resize(humans);
for (auto& id : g.manager_ids)
if (id.empty()) id = "manager:" + std::to_string(++g.manager_serial);
}
cli::Json ui_money(int64_t euros) {
return O{{"minor", std::to_string(euros * 100)}, {"currency", "EUR"}, {"minorUnits", 2}};
}
cli::Json ui_read(const ModernState& g, const UiContext& c, const cli::Json& request) {
const std::string request_id = text_field(request, "requestId");
const std::string kind = text_field(request, "kind");
if (c.manager < 0 || c.manager >= int(g.manager_teams.size()))
return failure(request_id, "no_active_manager", "Kein Manager am Schreibtisch.", c.state_revision);
// A read names the manager it is for; it must be the one at the desk (the session decides
// who acts, not the request).
if (const std::string wanted = text_field(request, "managerId"); !wanted.empty() && wanted != manager_id(g, c.manager))
return failure(request_id, "manager_not_active", "Dieser Manager sitzt nicht am Schreibtisch.", c.state_revision);
J m = meta(g, c);
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));
return failure(request_id, "unknown_read", "Unbekannte Abfrage: " + kind, c.state_revision);
}
} // namespace bmh::modern