Modern UI contract: lineup, tactics and training reads and commands
lineup.commit takes the squad order and swaps rows into place after opening the board as the desk does; tactics.commit sets the eight values through the board's checks; training.commit initializes the plan when needed and applies decreases before increases so every step stays within the limits. Each is one revision and is rolled back as a whole when the engine refuses a step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
99a48a463d
commit
78b827a0e6
6 files changed
+305
-11
No files matched your search
@@ -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<int>& 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<int> 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<int>& 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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user