Modern UI contract: squad read and contract tests

The squad read lists a squad (own club, any league club or a national team
with a nominated squad) with ids, positions, values, absences and, for the
manager's own club, monthly salary and remaining contract months.
modern_ui_contract_tests checks envelope, identities, money, errors and
that reads leave the state unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 08:19:55 +01:00
1 parent 3336d01004
commit fec4c6c7e7
5 files changed
+207 -2

No files matched your search

+8
View File
@@ -665,6 +665,14 @@ target_link_libraries(season_sim PRIVATE bmhui)
if(MSVC)
target_compile_options(season_sim PRIVATE /utf-8)
endif()
# The engine side of the Modern interface contract (docs/new_engine/ui-anbindung.md).
add_executable(modern_ui_contract_tests tests/modern_ui_contract_tests.cpp)
target_link_libraries(modern_ui_contract_tests PRIVATE bmh_modern_session)
if(MSVC)
target_compile_options(modern_ui_contract_tests PRIVATE /utf-8 /bigobj)
endif()
bmh_add_data_test(NAME modern_ui_contract_tests COMMAND modern_ui_contract_tests "${BMH_DATA}")
# The Modern fork of season_sim; with Classic data its output must equal season_sim's.
add_executable(modern_season_sim tests/modern_season_sim.cpp)
target_link_libraries(modern_season_sim PRIVATE bmh_modern_session)
+4 -1
View File
@@ -26,8 +26,11 @@ Revision kommt vom Aufrufer (Konsole bzw. AppController), der Read ändert nicht
|---|---|
| `session` | `sessionState`, `activeManagerId`, alle menschlichen Manager (ID, Platz, Name, Team, Porträt, `active`/`waiting`) |
| `desktop` | Manager, Team (`TeamRef`), Kontostand (`accounts.cash`; Sparguthaben und Kredit folgen mit dem Bank-Read), nächstes Spiel, `menuAvailability` |
| `squad` | ein Kader (`teamId`, sonst der eigene Verein; auch Nationalteams mit Kader): Spieler-ID, Name, Nation, Position, Rolle (`starter`/`bench`/`reserve`), Verfügbarkeit, Verletzung und Sperre in Wochen, Alter, Werte (Stärke, Kondition, Technik, Form), Fähigkeiten; für den eigenen Verein Monatsgehalt und Restlaufzeit in Monaten (fremde Vereine: `null`) |
Fehler: `unknown_read`, `manager_not_active`, `no_active_manager`.
Fehler: `unknown_read`, `manager_not_active`, `no_active_manager`, `unknown_team`.
Test: `modern_ui_contract_tests` (Umschlag, IDs, Geld, Fehler, Reads ändern den Spielstand nicht; braucht Spieldaten).
Noch nicht gefüllt: `nationId`, `crestAssetId` der Teams, `employments` (Amtszeiten), `strengthChoice`, `allowedCommands`.
+80
View File
@@ -8,6 +8,7 @@
#include <variant>
#include "formats/executable_layout.hpp"
#include "game/state.hpp"
#include "game/table.hpp"
#include "modern/content/world_package.hpp"
#include "modern/game/almanac.hpp"
@@ -179,6 +180,79 @@ J desktop_data(const ModernState& g, const UiContext& c, const std::string& toda
return data;
}
J metric(int value) {
return O{{"value", value}, {"unit", "rating"}, {"scale", nullptr}, {"availability", available()}};
}
// The team a request names (teamId: a world id or "team:<n>"), or the manager's own team.
int requested_team(const ModernState& g, const UiContext& c, const J& request) {
const std::string wanted = text_field(request, "teamId");
if (wanted.empty()) return team_of(g, c.manager);
for (int t = 0; t < int(g.world_club_id.size()); ++t)
if (g.world_club_id[size_t(t)] == wanted) return t;
if (wanted.starts_with("team:")) {
try {
const int t = std::stoi(wanted.substr(5));
if (t >= 0 && t < world_layout().total_teams()) return t;
} catch (const std::exception&) {
}
}
return -1;
}
// A squad: every player with identity, position, values, absence and (for the manager's own
// club, from his tactics board) monthly salary and remaining contract months.
J squad_data(const ModernState& g, const UiContext& c, int team, const std::string& today) {
static constexpr const char* kPositions[4] = {"gk", "def", "mid", "fwd"};
static constexpr const char* kSkills[7] = {"teamplay", "speed", "shooting", "passing", "tackling", "free_kick", "corner"};
const bool own = team == team_of(g, c.manager) && team < league_clubs();
const size_t board = size_t(c.manager) * 0x840;
const SquadRange r = squad_range(team);
A players;
for (size_t s = r.first; s < r.first + r.count && s < g.squad_slots.size(); ++s) {
const uint16_t e = g.squad_slots[s];
const int id = e & 0x0fff;
if (id == 0x0fff) continue;
const Player& p = g.players.at(size_t(id));
std::string pid = entry(g.world_player_id, size_t(id));
if (pid.empty()) pid = "player:legacy-" + std::to_string(id);
const std::string& first = entry(g.player_first, size_t(id));
std::string last = entry(g.player_last, size_t(id));
if (last.empty()) last = almanac_utf8(g.player_name(id));
const std::string& nation = entry(g.player_nation, size_t(id));
const uint32_t absence = p.w364 >> 16 & 0x1f; // weeks out; bit 4: banned instead of injured
const int injured = (absence & 0x10) ? 0 : int(absence), banned = (absence & 0x10) ? int(absence & 0xf) : 0;
O skills;
for (int k = 0; k < 7; ++k) skills[kSkills[k]] = int(p.w368 >> (2 * k) & 3);
O row{{"player", O{{"id", pid}, {"name", first.empty() ? last : first + " " + last}, {"photoAssetId", nullptr},
{"nationIds", nation.empty() ? A{} : A{J(nation)}}, {"positions", A{J(kPositions[p.position()])}},
{"shirtNumber", nullptr}}},
{"clubId", team < league_clubs() ? J(team_id(g, team)) : J(nullptr)},
{"squadTeamId", team_id(g, team)},
{"role", (e & 0x8000) ? "starter" : (e & 0x4000) ? "bench" : "reserve"},
{"availability", injured ? "injured" : banned ? "suspended" : "fit"},
{"injuryRemaining", O{{"value", injured}, {"unit", "weeks"}}},
{"suspensionRemaining", O{{"value", banned}, {"unit", "weeks"}}},
{"age", p.age()},
{"metrics", O{{"strength", metric(p.strength())}, {"condition", metric(p.condition())},
{"technique", metric(p.technique())}, {"form", metric(p.form())}}},
{"skills", std::move(skills)}};
const size_t at = board + (s - r.first) * 0x58;
if (own && at + 0x58 <= g.tactics.size()) {
const int64_t salary = int64_t(uint32_t(g.tactics[at + 0x50]) | uint32_t(g.tactics[at + 0x51]) << 8 |
uint32_t(g.tactics[at + 0x52]) << 16 | uint32_t(g.tactics[at + 0x53]) << 24);
row["salaryMonthly"] = ui_money(salary);
row["contractRemaining"] = O{{"value", int(g.tactics[at + 0x0c])}, {"unit", "months"}};
} else {
row["salaryMonthly"] = nullptr;
row["contractRemaining"] = nullptr;
}
row["contractUntil"] = nullptr;
players.push_back(std::move(row));
}
return O{{"kind", "squad"}, {"team", team_ref(g, team, today)}, {"players", std::move(players)}};
}
} // namespace
void ensure_ui_identities(ModernState& g) {
@@ -212,6 +286,12 @@ 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 == "squad") {
const int team = requested_team(g, c, request);
if (team < 0 || !fields_players(g, team))
return failure(request_id, "unknown_team", "Dieses Team hat keinen Kader.", c.state_revision);
return snapshot(request_id, std::move(m), squad_data(g, c, team, today));
}
return failure(request_id, "unknown_read", "Unbekannte Abfrage: " + kind, c.state_revision);
}
+1 -1
View File
@@ -743,7 +743,7 @@ int main(int argc, char** argv) {
read({{"kind", "squad"}});
read({{"kind", "table"}, {"league", 42}});
// the interface contract (docs/new_engine/ui-anbindung.md)
for (const char* kind : {"session", "desktop", "nonsense"}) {
for (const char* kind : {"session", "desktop", "squad", "nonsense"}) {
const auto request = cli::Json::Object{{"kind", kind}, {"requestId", std::string("request:sim-") + kind}};
std::printf("ui_read %s\n -> %s\n", kind, cli::dump_json(session.ui_read(request, revision)).c_str());
}
+114
View File
@@ -0,0 +1,114 @@
// SPDX-License-Identifier: GPL-3.0-only
// The engine side of the Modern interface contract (docs/new_engine/ui-anbindung.md): the
// envelope, identities, money, errors and that a read changes nothing. Needs game data
// (a Modern session on the Classic data).
#include <cstdio>
#include <string>
#include <variant>
#include "cli/json.hpp"
#include "modern/app/modern_session.hpp"
#include "modern/content/ui_contract.hpp"
#include "modern/game/save.hpp"
using namespace bmh;
using J = cli::Json;
using O = J::Object;
namespace {
int failures = 0;
void check(bool ok, const char* what) {
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", what);
++failures;
}
}
const J& at(const J& j, const char* key) {
static const J none;
const auto* o = j.object();
if (!o) return none;
const auto it = o->find(key);
return it == o->end() ? none : it->second;
}
std::string s(const J& j) { return j.string() ? *j.string() : std::string{}; }
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: modern_ui_contract_tests <BMH data>\n");
return 2;
}
try {
modern::app::ModernSession session(argv[1], 1u);
ui::SetupOptions options;
ui::ManagerChoice choice;
choice.name = "TEST";
choice.master_club = 0;
std::string error;
if (!session.confirm_manager(options, choice, error)) throw std::runtime_error("manager: " + error);
ui::CareerInitializationResult init;
if (!session.finish(init, error)) throw std::runtime_error("career start: " + error);
const uint64_t revision = init.desktop.revision;
const auto before = modern::save_stream(session.state());
const std::string career = session.state().career_id;
// session: envelope and identities
const J sess = session.ui_read(O{{"kind", "session"}, {"requestId", "request:1"}}, revision);
check(s(at(sess, "messageType")) == "snapshot", "session is a snapshot");
check(s(at(sess, "requestId")) == "request:1", "request id echoed");
const J& meta = at(sess, "meta");
check(s(at(meta, "contractVersion")) == "bmh-modern-ui@draft1", "contract version");
check(s(at(meta, "careerId")).starts_with("career:") && s(at(meta, "careerId")).size() == 23, "career id");
check(s(at(meta, "stateRevision")) == std::to_string(revision), "state revision as decimal string");
check(s(at(at(meta, "scope"), "managerId")) == "manager:1", "first manager id");
check(s(at(at(meta, "currency"), "code")) == "EUR" && at(at(meta, "currency"), "minorUnits").integer() &&
*at(at(meta, "currency"), "minorUnits").integer() == 2, "currency");
const auto* managers = at(at(sess, "data"), "managers").array();
check(managers && managers->size() == 1 && s(at((*managers)[0], "status")) == "active", "one active manager");
// desktop: money in minor units, menus
const J desk = session.ui_read(O{{"kind", "desktop"}}, revision);
const J status = session.read(O{{"kind", "status"}});
const int64_t euros = at(status, "money").integer() ? *at(status, "money").integer() : -1;
check(s(at(at(at(at(desk, "data"), "accounts"), "cash"), "minor")) == std::to_string(euros * 100), "cash in cents");
const auto* menus = at(at(desk, "data"), "menuAvailability").object();
check(menus && menus->size() == 16 && s(at(menus->at("bank"), "status")) == "available", "menus at a club desk");
// squad: own club with salaries, another club without
const J own = session.ui_read(O{{"kind", "squad"}}, revision);
const auto* players = at(at(own, "data"), "players").array();
check(players && players->size() >= 11, "own squad");
check(players && !players->empty() && at((*players)[0], "salaryMonthly").object(), "own salaries known");
const J other = session.ui_read(O{{"kind", "squad"}, {"teamId", "team:5"}}, revision);
const auto* others = at(at(other, "data"), "players").array();
check(others && !others->empty() && std::holds_alternative<std::nullptr_t>(at((*others)[0], "salaryMonthly").value), "foreign salaries unknown");
// errors
check(s(at(session.ui_read(O{{"kind", "nonsense"}}, revision), "code")) == "unknown_read", "unknown read");
check(s(at(session.ui_read(O{{"kind", "desktop"}, {"managerId", "manager:7"}}, revision), "code")) ==
"manager_not_active", "other manager refused");
check(s(at(session.ui_read(O{{"kind", "squad"}, {"teamId", "club:none"}}, revision), "code")) == "unknown_team",
"unknown team");
// reads change nothing
check(modern::save_stream(session.state()) == before, "reads leave the state unchanged");
check(session.state().career_id == career, "career id unchanged");
// identities: never reused, nothing set is replaced
modern::ModernState copy = session.state();
modern::ensure_ui_identities(copy);
check(copy.manager_ids == session.state().manager_ids && copy.career_id == career, "identities stable");
copy.manager_ids.clear();
modern::ensure_ui_identities(copy);
check(copy.manager_ids.size() == 1 && copy.manager_ids[0] == "manager:2", "a freed slot gets a new id");
// money
check(s(at(modern::ui_money(15), "minor")) == "1500" && s(at(modern::ui_money(-3), "minor")) == "-300", "money");
} catch (const std::exception& ex) {
std::fprintf(stderr, "FAIL: %s\n", ex.what());
return 1;
}
if (failures == 0) std::printf("modern ui contract tests passed\n");
return failures == 0 ? 0 : 1;
}