diff --git a/docs/new_engine/ui-anbindung.md b/docs/new_engine/ui-anbindung.md index aec3b9f..301b041 100644 --- a/docs/new_engine/ui-anbindung.md +++ b/docs/new_engine/ui-anbindung.md @@ -28,6 +28,9 @@ Revision kommt vom Aufrufer (Konsole bzw. AppController), der Read ändert nicht | `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`) | | `camps` | Karten und Lager (Qualität 0–6, Ort, Nation, Exklusiv-Sponsor-Chance), je Lager ein bindendes Angebot für den eigenen Kader (Grundpreis + Kaderzuschlag, ohne Zufall; `allowed`, `reasonCode` `booked_this_week`/`visit_limit`/`no_club`, `exceedsCash`), Schwerpunkte, Aufenthalte der Saison, Buchung dieser Woche | +| `lineup` | die Kaderzeilen in Reihenfolge (die ersten elf spielen) mit Rolle, die Plätze der Startelf auf der Taktiktafel (`depth` 0–19, `lane` 0–8; das JSON hat keine Kommazahlen), `manual` (Aufstellung von Hand) | +| `tactics` | die acht Einstellungen (`tactic:0` Grundausrichtung bis `tactic:7` Einsatz) mit Wert und Maximum aus der EXE; die Bedeutung der Diagramm-Einstellungen 1–3, 5, 6 ist nicht belegt, daher neutrale Namen | +| `training` | Bälle je Spielertyp und Trainingsbereich (1–3) mit den Grenzen, je Spieler Intensität und Schwerpunkt, ob das Training eingerichtet ist, Automatik-Bits | Fehler: `unknown_read`, `manager_not_active`, `no_active_manager`, `unknown_team`. @@ -42,8 +45,11 @@ Fehler: `unknown_read`, `manager_not_active`, `no_active_manager`, `unknown_team | Command | payload | Wirkung | |---|---|---| | `camp.book` | `campId`, optional `focus {condition, technique, play}` (0–15; Erholung folgt) | setzt die Schwerpunkte und bucht das Lager wie der Schreibtisch; Fehler `unknown_camp`, `invalid_focus`, `booked_this_week`, `visit_limit`, `insufficient_funds` | +| `lineup.commit` | `order`: Spieler-IDs in der gewünschten Reihenfolge | öffnet die Aufstellung wie am Schreibtisch und tauscht die Zeilen; Fehler `unknown_player`, `lineup_refused` (z. B. Verletzte, Ausländerregel) | +| `tactics.commit` | `values`: die acht Werte | jede geänderte Einstellung über die Prüfung der Taktiktafel (eine gesetzte Grundausrichtung beendet die Steuerung durch den Assistenten); Fehler `invalid_tactic` | +| `training.commit` | optional `types` (4), `areas` (9), `players [{playerId, intensity}]` | richtet das Training bei Bedarf ein, setzt erst die Senkungen, dann die Erhöhungen (jeder Zwischenstand bleibt in den Grenzen); Fehler `training_unavailable`, `unknown_player` und die Gründe der Engine | -Test: `modern_ui_contract_tests` (Umschlag, IDs, Geld, Fehler, Reads ändern den Spielstand nicht; Commands: veraltete Revision, fremder Manager, Wiederholung, Konflikt, Ablehnung ohne Änderung; braucht Spieldaten). +Test: `modern_ui_contract_tests` (Umschlag, IDs, Geld, Fehler, Reads ändern den Spielstand nicht; Commands: veraltete Revision, fremder Manager, Wiederholung, Konflikt, Ablehnung ohne Änderung, je ein angenommener und ein abgelehnter Befehl für Lager, Aufstellung, Taktik und Training; braucht Spieldaten). Noch nicht gefüllt: `nationId`, `crestAssetId` der Teams, `employments` (Amtszeiten), `strengthChoice`, `allowedCommands`. diff --git a/src/modern/app/modern_session.cpp b/src/modern/app/modern_session.cpp index 28ac526..3c14fa2 100644 --- a/src/modern/app/modern_session.cpp +++ b/src/modern/app/modern_session.cpp @@ -8752,6 +8752,85 @@ std::string ui_text(const cli::Json& j, const char* key) { } // namespace +cli::Json ModernSession::ui_read(const cli::Json& request, uint64_t revision) const { + using O = cli::Json::Object; + using A = cli::Json::Array; + UiContext context; + context.state_revision = revision; + context.manager = int(g_.memory.has(::bmh::layout::ActiveManager) ? g_.memory.u8(::bmh::layout::ActiveManager) : 0); + const ui::DeskState desk = desktop_projection(revision); + context.next_opponent = desk.next_opponent; + context.next_competition = desk.next_competition_label; + context.next_away = desk.next_match_away; + const std::string kind = ui_text(request, "kind"), request_id = ui_text(request, "requestId"); + const int manager = context.manager; + if (manager < 0 || size_t(manager) >= g_.manager_teams.size() || + (kind != "lineup" && kind != "tactics" && kind != "training")) + 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); + const int team = g_.manager_teams[size_t(manager)]; + if (team < 0 || team >= league_clubs()) + return modern::ui_error(request_id, "no_club", "Am Schreibtisch des Bundestrainers gibt es das nicht.", revision, false); + if (kind == "lineup") { + // the squad rows in order: the first eleven start (+0x8000), the bench (+0x4000) follows; + // the starters' places on the manager's tactics board: depth 0-19, lane 0-8 + const SquadRange range = squad_range(team); + A order, rows, positions; + for (size_t row = 0; row < range.count; ++row) { + const uint16_t e = g_.squad_slots[range.first + row]; + const int p = e & 0x0fff; + if (p == 0x0fff) continue; + const std::string id = modern::ui_player_id(g_, p); + order.push_back(id); + rows.push_back(O{{"row", int(row)}, {"playerId", id}, + {"role", (e & 0x8000) ? "starter" : (e & 0x4000) ? "bench" : "reserve"}}); + const size_t at = size_t(manager) * 0x840 + row * 0x58; + if (row < 11 && at + 0x43 <= g_.tactics.size()) + positions.push_back(O{{"playerId", id}, {"depth", int(int8_t(g_.tactics[at + 0x41]))}, + {"lane", int(int8_t(g_.tactics[at + 0x42]))}}); + } + return modern::ui_snapshot(g_, context, request_id, + O{{"kind", "lineup"}, {"teamId", modern::ui_team_id(g_, team)}, {"formationId", nullptr}, {"order", std::move(order)}, + {"rows", std::move(rows)}, {"positions", std::move(positions)}, {"depthMax", 19}, {"laneMax", 8}, + {"manual", g_.manager_options.size() > size_t(manager) * 5 && g_.manager_options[size_t(manager) * 5] == 0}}); + } + if (kind == "tactics") { + // the eight settings (record +0xd2..+0xd9) with the executable's maxima; the meaning of + // settings 1-3, 5 and 6 (diagrams on the original board) is not proven, hence neutral labels + static constexpr const char* kLabels[8] = {"Grundausrichtung", "Spielzug 1", "Spielzug 2", "Spielzug 3", + "Flache oder hohe Bälle", "Aufbau links", "Aufbau rechts", "Einsatz"}; + const ui::TacticsState t = ui::tactics_from_game(g_, exe_, revision); + A controls; + for (int i = 0; i < 8; ++i) + controls.push_back(O{{"id", "tactic:" + std::to_string(i)}, + {"label", O{{"key", nullptr}, {"text", kLabels[i]}, {"locale", "de"}, {"args", O{}}}}, + {"value", int(t.values[size_t(i)])}, {"choices", A{}}, + {"range", O{{"min", 0}, {"max", int(exe_.u8(::bmh::layout::CareerSession_DispatchLiveTactic_RuntimeField + uint32_t(i)))}, {"step", 1}}}}); + return modern::ui_snapshot(g_, context, request_id, + O{{"kind", "tactics"}, {"teamId", modern::ui_team_id(g_, team)}, {"controls", std::move(controls)}, + {"assistantAttitude", int(t.assistant_attitude)}, {"liveScopeToken", nullptr}}); + } + // training: ball counts of the player types and areas (1-3; types <= 8, fitness + skill <= 4, + // the rest <= 18), per player the intensity 1-3 and the focus symbol, the automation bits + static constexpr const char* kTypes[4] = {"goalkeepers", "defence", "midfield", "attack"}; + static constexpr const char* kAreas[9] = {"fitness", "skill", "corner", "free_kick", "tackling", "pass", "shot", "speed", "game"}; + ui::TrainingState t; + try { t = ui::training_from_game(g_, exe_, revision); } catch (const std::exception&) { + return modern::ui_error(request_id, "training_unavailable", "Training gibt es in diesem Spielmodus nicht.", revision, false); + } + A types, areas, players; + for (size_t i = 0; i < 4; ++i) types.push_back(O{{"id", std::string("training:type/") + kTypes[i]}, {"allocation", int(t.types[i])}, {"min", 1}, {"max", 3}, {"unit", "balls"}}); + for (size_t i = 0; i < 9; ++i) areas.push_back(O{{"id", std::string("training:area/") + kAreas[i]}, {"allocation", int(t.areas[i])}, {"min", 1}, {"max", 3}, {"unit", "balls"}}); + for (const auto& p : t.players) + players.push_back(O{{"playerId", modern::ui_player_id(g_, p.player_id)}, {"row", p.row}, {"intensity", int(p.intensity)}, + {"focus", int(p.focus)}}); + return modern::ui_snapshot(g_, context, request_id, + O{{"kind", "training"}, {"teamId", modern::ui_team_id(g_, team)}, {"types", std::move(types)}, {"areas", std::move(areas)}, + {"limits", O{{"types", 8}, {"fitnessAndSkill", 4}, {"otherAreas", 18}}}, {"players", std::move(players)}, + {"initialized", !t.needs_initialization()}, {"automation", int(t.automation)}}); +} + 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"); @@ -8830,6 +8909,141 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision 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 if (kind == "tactics.commit") { + // payload {values: [8 numbers]}: every changed setting through the tactics board's own check + const cli::Json* values = ui_field(*payload, "values"); + const auto* list = values ? values->array() : nullptr; + if (!list || list->size() != 8) return refuse("invalid_payload", "values braucht acht Taktikwerte."); + const ui::TacticsState current = ui::tactics_from_game(g_, exe_, revision); + bool ok = true; + for (int i = 0; i < 8 && ok; ++i) { + const cli::Json& v = (*list)[size_t(i)]; + if (!v.integer()) { restore(); return refuse("invalid_payload", "Taktikwerte sind ganze Zahlen."); } + if (*v.integer() == current.values[size_t(i)]) continue; + ui::TacticsRequest r; + r.kind = ui::TacticsRequestKind::set_tactic; + r.manager_slot = manager; + r.runtime_club = g_.manager_teams[size_t(manager)]; + r.revision = revision; + r.index = i; + r.desired_value = int(*v.integer()); + if (i == 0) r.desired_assistant_attitude = 0; // a set attitude ends the assistant's control + ok = dispatch_tactics(r, revision, error); + } + if (!ok) { restore(); return refuse("invalid_tactic", error); } + if (revision != previous) revision = previous + 1; + answer = modern::ui_command_result(request_id, previous, revision, {"tactics"}, "Taktik übernommen."); + } else if (kind == "training.commit") { + // payload {types?: [4 x 1-3], areas?: [9 x 1-3], players?: [{playerId, intensity 1-3}]} + ui::TrainingState current; + try { current = ui::training_from_game(g_, exe_, revision); } catch (const std::exception&) { + return refuse("training_unavailable", "Training gibt es in diesem Spielmodus nicht."); + } + auto send = [&](ui::TrainingRequest r) { + r.manager_slot = manager; + r.runtime_club = g_.manager_teams[size_t(manager)]; + r.revision = revision; + if (!dispatch_training(r, revision, error)) return false; + current = ui::training_from_game(g_, exe_, revision); + return true; + }; + bool ok = true; + if (current.needs_initialization()) { // the screen initializes itself on first use + ui::TrainingRequest r; + r.kind = ui::TrainingRequestKind::initialize; + r.desired_types = current.types, r.desired_areas = current.areas, r.desired_automation = 0x9f; + ok = send(r); + } + // targets: lower values first, then higher, so every step keeps the limits + // (types sum <= 8, fitness + skill <= 4, the other areas <= 18) + const auto targets = [&](const char* key, size_t n, std::vector& out) { + out.clear(); + const cli::Json* v = ui_field(*payload, key); + if (!v) return true; + const auto* a = v->array(); + if (!a || a->size() != n) return false; + for (const auto& x : *a) { + if (!x.integer() || *x.integer() < 1 || *x.integer() > 3) return false; + out.push_back(int(*x.integer())); + } + return true; + }; + std::vector types, areas; + if (!targets("types", 4, types) || !targets("areas", 9, areas)) { + restore(); + return refuse("invalid_payload", "types (4) und areas (9) mit Werten 1 bis 3."); + } + for (int pass = 0; pass < 2 && ok; ++pass) + for (int group = 0; group < 2 && ok; ++group) { + const std::vector& want = group == 0 ? types : areas; + for (size_t i = 0; i < want.size() && ok; ++i) { + const int now = group == 0 ? current.types[i] : current.areas[i]; + if (want[i] == now || (pass == 0) != (want[i] < now)) continue; + ui::TrainingRequest r; + r.kind = group == 0 ? ui::TrainingRequestKind::set_type : ui::TrainingRequestKind::set_area; + r.index = int(i), r.value = want[i]; + r.desired_types = current.types, r.desired_areas = current.areas; + (group == 0 ? r.desired_types[i] : r.desired_areas[i]) = uint8_t(want[i]); + r.desired_automation = uint8_t(current.automation & ~uint8_t(1u << (group == 0 ? 1 : 2))); + ok = send(r); + } + } + if (const cli::Json* players = ui_field(*payload, "players"); ok && players) { + const auto* list = players->array(); + if (!list) { restore(); return refuse("invalid_payload", "players ist eine Liste."); } + for (const auto& p : *list) { + const std::string id = ui_text(p, "playerId"); + const cli::Json* intensity = ui_field(p, "intensity"); + if (!intensity || !intensity->integer()) { restore(); return refuse("invalid_payload", "intensity fehlt."); } + const auto row = std::find_if(current.players.begin(), current.players.end(), [&](const auto& t) { + return modern::ui_player_id(g_, t.player_id) == id; + }); + if (row == current.players.end()) { restore(); return refuse("unknown_player", "Spieler nicht im Kader: " + id); } + if (row->intensity == *intensity->integer()) continue; + ui::TrainingRequest r; + r.kind = ui::TrainingRequestKind::set_player_intensity; + r.index = row->row, r.player_id = row->player_id, r.value = int(*intensity->integer()); + r.desired_types = current.types, r.desired_areas = current.areas; + r.desired_automation = uint8_t(current.automation & ~uint8_t(1u << 3)); + if (!(ok = send(r))) break; + } + } + if (!ok) { restore(); return refuse(error.empty() ? "invalid_training" : error, "Training nicht übernommen: " + error); } + if (revision != previous) revision = previous + 1; + answer = modern::ui_command_result(request_id, previous, revision, {"training"}, "Training übernommen."); + } else if (kind == "lineup.commit") { + // payload {order: [player ids]}: the squad in this order (the first eleven start); the + // board opens as on the desk (the human line-up path), then rows are swapped into place + const cli::Json* order = ui_field(*payload, "order"); + const auto* ids = order ? order->array() : nullptr; + if (!ids || ids->empty()) return refuse("invalid_payload", "order braucht die Spieler-IDs."); + const int team = g_.manager_teams[size_t(manager)]; + const SquadRange range = squad_range(team); + ui::LineupRequest prepare; + prepare.kind = ui::LineupRequestKind::prepare_lineup; + prepare.manager_slot = manager, prepare.runtime_club = team, prepare.revision = revision; + bool ok = dispatch_lineup(prepare, revision, error); + for (size_t want = 0; ok && want < ids->size(); ++want) { + const std::string id = (*ids)[want].string() ? *(*ids)[want].string() : std::string{}; + size_t at = range.count; + for (size_t row = 0; row < range.count; ++row) { + const int p = g_.squad_slots[range.first + row] & 0x0fff; + if (p != 0x0fff && modern::ui_player_id(g_, p) == id) at = row; + } + if (at == range.count || want >= range.count) { error = "unknown_player"; ok = false; break; } + if (at == want) continue; + ui::LineupRequest swap; + swap.kind = ui::LineupRequestKind::swap_players; + swap.manager_slot = manager, swap.runtime_club = team, swap.revision = revision; + swap.first_row = int(want), swap.second_row = int(at); + swap.first_slot = g_.squad_slots[range.first + want], swap.second_slot = g_.squad_slots[range.first + at]; + swap.first_player = (swap.first_slot & 0x0fff) == 0x0fff ? -1 : swap.first_slot & 0x0fff; + swap.second_player = swap.second_slot & 0x0fff; + ok = dispatch_lineup(swap, revision, error); + } + if (!ok) { restore(); return refuse(error == "unknown_player" ? "unknown_player" : "lineup_refused", error); } + revision = previous + 1; + answer = modern::ui_command_result(request_id, previous, revision, {"lineup", "squad"}, "Aufstellung übernommen."); } else { return refuse("unknown_command", "Unbekannter Befehl: " + kind); } diff --git a/src/modern/app/modern_session.hpp b/src/modern/app/modern_session.hpp index 084ed8e..be0095d 100644 --- a/src/modern/app/modern_session.hpp +++ b/src/modern/app/modern_session.hpp @@ -838,16 +838,8 @@ public: } // The interface contract (docs/new_engine/ui-anbindung.md): a read in the contract's envelope // for the manager at the desk, at the revision the caller holds. Read-only. - cli::Json ui_read(const cli::Json& request, uint64_t revision) const { - UiContext context; - context.state_revision = revision; - context.manager = int(g_.memory.has(::bmh::layout::ActiveManager) ? g_.memory.u8(::bmh::layout::ActiveManager) : 0); - const ui::DeskState desk = desktop_projection(revision); - context.next_opponent = desk.next_opponent; - context.next_competition = desk.next_competition_label; - context.next_away = desk.next_match_away; - return modern::ui_read(g_, context, request); - } + // Kinds of the session itself: lineup, tactics, training; the others in ui_contract.cpp. + cli::Json ui_read(const cli::Json& request, uint64_t revision) const; // A command of the interface contract: checks career, expectedRevision and owner, runs the // engine action and answers command_result or error. A refused command leaves the state and // both generators untouched; a repeated requestId with the same command gets the stored diff --git a/src/modern/content/ui_contract.cpp b/src/modern/content/ui_contract.cpp index 1afcc40..1ecefd5 100644 --- a/src/modern/content/ui_contract.cpp +++ b/src/modern/content/ui_contract.cpp @@ -342,6 +342,17 @@ cli::Json ui_money(int64_t euros) { return O{{"minor", std::to_string(euros * 100)}, {"currency", "EUR"}, {"minorUnits", 2}}; } +cli::Json ui_snapshot(const ModernState& g, const UiContext& c, const std::string& request_id, cli::Json data) { + return snapshot(request_id, meta(g, c), std::move(data)); +} + +std::string ui_team_id(const ModernState& g, int team) { return team_id(g, team); } + +std::string ui_player_id(const ModernState& g, int player) { + std::string id = entry(g.world_player_id, size_t(player)); + return id.empty() ? "player:legacy-" + std::to_string(player) : id; +} + 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"); diff --git a/src/modern/content/ui_contract.hpp b/src/modern/content/ui_contract.hpp index ed78d7d..5937f02 100644 --- a/src/modern/content/ui_contract.hpp +++ b/src/modern/content/ui_contract.hpp @@ -54,5 +54,9 @@ cli::Json ui_error(const std::string& request_id, const std::string& code, const uint64_t revision, bool retryable); // The contract id of a manager slot ("manager:"). std::string ui_manager_id(const ModernState& g, int manager); +// Reads the session answers itself (they need the executable's tables): data in the envelope. +cli::Json ui_snapshot(const ModernState& g, const UiContext& context, const std::string& request_id, cli::Json data); +std::string ui_team_id(const ModernState& g, int team); +std::string ui_player_id(const ModernState& g, int player); } // namespace bmh::modern diff --git a/tests/modern_ui_contract_tests.cpp b/tests/modern_ui_contract_tests.cpp index f1c1e12..21a3d3d 100644 --- a/tests/modern_ui_contract_tests.cpp +++ b/tests/modern_ui_contract_tests.cpp @@ -146,6 +146,73 @@ int main(int argc, char** argv) { const J after = session.ui_read(O{{"kind", "camps"}}, rev); check(at(at(after, "data"), "staysThisSeason").integer() && *at(at(after, "data"), "staysThisSeason").integer() == 1 && truth(at(at(after, "data"), "bookedThisWeek")), "camp read shows the stay"); + // lineup, tactics, training: read, one accepted and one refused command each + const auto cmd = [&](const std::string& id, const char* kind, J payload) { + return J(O{{"messageType", "command"}, {"requestId", id}, {"expectedRevision", std::to_string(rev)}, + {"owner", O{{"managerId", "manager:1"}}}, {"kind", kind}, {"payload", std::move(payload)}}); + }; + const J lineup = session.ui_read(O{{"kind", "lineup"}}, rev); + const auto* order = at(at(lineup, "data"), "order").array(); + check(order && order->size() >= 12 && at(at(lineup, "data"), "positions").array() && + at(at(lineup, "data"), "positions").array()->size() == 11, "lineup read"); + if (order && order->size() >= 12) { + J::Array wanted = *order; + std::swap(wanted[0], wanted[1]); + std::swap(wanted[10], wanted[11]); + const uint64_t before_lineup = rev; + const J done = session.ui_command(cmd("request:l1", "lineup.commit", O{{"order", wanted}}), rev); + if (s(at(done, "messageType")) != "command_result") std::fprintf(stderr, "lineup: %s\n", cli::dump_json(done).c_str()); + const J reread = session.ui_read(O{{"kind", "lineup"}}, rev); + const auto* now = at(at(reread, "data"), "order").array(); + if (now && (s((*now)[0]) != s(wanted[0]) || s((*now)[10]) != s(wanted[10]) || rev != before_lineup + 1)) + std::fprintf(stderr, "lineup: rev %llu/%llu, now %s %s %s, wanted %s %s %s\n", (unsigned long long)rev, + (unsigned long long)before_lineup, s((*now)[0]).c_str(), s((*now)[1]).c_str(), s((*now)[10]).c_str(), + s(wanted[0]).c_str(), s(wanted[1]).c_str(), s(wanted[10]).c_str()); + check(s(at(done, "messageType")) == "command_result" && rev == before_lineup + 1 && now && + s((*now)[0]) == s(wanted[0]) && s((*now)[10]) == s(wanted[10]), + "lineup committed"); + const auto kept = modern::save_stream(session.state()); + check(s(at(session.ui_command(cmd("request:l2", "lineup.commit", O{{"order", J::Array{J("player:none")}}}), rev), + "code")) == "unknown_player" && modern::save_stream(session.state()) == kept && rev == before_lineup + 1, + "unknown player refused without change"); + } + const J tactics = session.ui_read(O{{"kind", "tactics"}}, rev); + const auto* controls = at(at(tactics, "data"), "controls").array(); + check(controls && controls->size() == 8, "tactics read"); + if (controls && controls->size() == 8) { + J::Array values; + for (const auto& c : *controls) values.push_back(at(c, "value")); + const int64_t max7 = *at(at((*controls)[7], "range"), "max").integer(); + const int64_t v7 = *values[7].integer(); + values[7] = J(v7 == max7 ? v7 - 1 : v7 + 1); + const uint64_t before_tactics = rev; + const J done = session.ui_command(cmd("request:t1", "tactics.commit", O{{"values", values}}), rev); + const J reread = session.ui_read(O{{"kind", "tactics"}}, rev); + const auto* now = at(at(reread, "data"), "controls").array(); + check(s(at(done, "messageType")) == "command_result" && rev == before_tactics + 1 && now && + *at((*now)[7], "value").integer() == *values[7].integer(), "tactics committed"); + values[7] = J(max7 + 1); + const auto kept = modern::save_stream(session.state()); + check(s(at(session.ui_command(cmd("request:t2", "tactics.commit", O{{"values", values}}), rev), "code")) == + "invalid_tactic" && modern::save_stream(session.state()) == kept, "tactic above its maximum refused"); + } + const J training = session.ui_read(O{{"kind", "training"}}, rev); + check(s(at(training, "messageType")) == "snapshot" && at(at(training, "data"), "areas").array() && + at(at(training, "data"), "areas").array()->size() == 9, "training read"); + { + const uint64_t before_training = rev; + const J done = session.ui_command(cmd("request:p1", "training.commit", + O{{"types", J::Array{J(2), J(2), J(2), J(2)}}, + {"areas", J::Array{J(2), J(2), J(2), J(2), J(2), J(2), J(2), J(2), J(2)}}}), rev); + const J now = session.ui_read(O{{"kind", "training"}}, rev); + const auto* areas = at(at(now, "data"), "areas").array(); + check(s(at(done, "messageType")) == "command_result" && rev == before_training + 1 && areas && + *at((*areas)[8], "allocation").integer() == 2, "training committed"); + const auto kept = modern::save_stream(session.state()); + check(s(at(session.ui_command(cmd("request:p2", "training.commit", O{{"types", J::Array{J(3), J(3), J(3), J(3)}}}), rev), + "messageType")) == "error" && modern::save_stream(session.state()) == kept && rev == before_training + 1, + "training over the limit refused without change"); + } } catch (const std::exception& ex) { std::fprintf(stderr, "FAIL: %s\n", ex.what()); return 1;