Bind the kickoff of the Modern desk to the engine and show its recording in the ticker
The kickoff button sends session.advance; the engine's recording of the played matchday is shaped for the live ticker (phases, per-match tables, the desk manager's squad). A contract running out on the way asks the manager to extend at the player's demand or to release him. The UI script driver accepts optional clicks, and modern_live_dump writes the engine's recording for inspection. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e92d07b03
commit
7c0e66affe
15 files changed
+447
-30
No files matched your search
@@ -0,0 +1,195 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// The engine's kickoff recording, shaped for the live ticker. Nothing here simulates or
|
||||
// estimates: the recording is copied, its phases and tables are renamed to the ticker's
|
||||
// vocabulary and the desk manager's squad is attached for the team tab.
|
||||
#include "modern/ui/engine_live.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
Json failure(const std::string &error) { return {{"ok", false}, {"error", error}}; }
|
||||
bool text(const Json &v, size_t max = 400) { return v.is_string() && v.get_ref<const std::string &>().size() <= max; }
|
||||
bool count(const Json &v, uint64_t max = 1000000) { return v.is_number_integer() && v.get<int64_t>() >= 0 && uint64_t(v.get<int64_t>()) <= max; }
|
||||
bool team_ok(const Json &v) { return v.is_object() && text(v.at("id")) && !v.at("id").get_ref<const std::string &>().empty() && text(v.at("name")); }
|
||||
Json metric(const Json &player, const char *key) {
|
||||
try {
|
||||
const auto &value = player.at("metrics").at(key).at("value");
|
||||
return value.is_number_integer() ? value : Json();
|
||||
} catch (const std::exception &) { return Json(); }
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json engine_advance_command(const Json &bundle, const std::string &request_id, const Json &payload) {
|
||||
validate_engine_desk(bundle);
|
||||
if (request_id.empty()) throw editor::Error("Anfrage-ID fehlt.");
|
||||
const auto &meta = bundle.at("desktop").at("meta");
|
||||
const auto commands = meta.at("scope").value("allowedCommands", Json::array());
|
||||
if (!commands.is_array() || std::find(commands.begin(), commands.end(), Json("session.advance")) == commands.end())
|
||||
throw editor::Error("Der Anstoß ist in diesem Kontext gesperrt.");
|
||||
return {{"messageType", "command"}, {"contractVersion", meta.at("contractVersion")}, {"requestId", request_id},
|
||||
{"careerId", meta.at("careerId")}, {"expectedRevision", meta.at("stateRevision")},
|
||||
{"owner", {{"managerId", meta.at("scope").at("managerId")}}},
|
||||
{"kind", "session.advance"}, {"payload", payload.is_object() ? payload : Json::object()}};
|
||||
}
|
||||
|
||||
bool valid_engine_live_fixture(const Json &v) {
|
||||
try {
|
||||
if (!v.is_object() || !v.value("ok", false) || v.at("schema") != "openbmh-live-fixture@1" ||
|
||||
v.at("source") != "engine-recording" || !text(v.at("fixture_id")) || !count(v.at("source_revision"), UINT64_MAX >> 1) ||
|
||||
!v.at("owners").is_array() || v.at("owners").empty() || v.at("owners").size() > 4 ||
|
||||
!v.at("matches").is_array() || v.at("matches").empty() || v.at("matches").size() > 16 ||
|
||||
!v.at("frames").is_array() || v.at("frames").size() < 2 || v.at("frames").size() > 2000 ||
|
||||
!count(v.at("duration"), 200) || !count(v.at("field_count"), 11) ||
|
||||
(v.at("role") != "club" && v.at("role") != "national") ||
|
||||
(v.at("surface") != "outdoor" && v.at("surface") != "indoor")) return false;
|
||||
std::set<std::string> match_ids, owner_ids;
|
||||
for (const auto &match : v.at("matches")) {
|
||||
if (!text(match.at("id")) || !match_ids.insert(match.at("id")).second || !team_ok(match.at("home")) ||
|
||||
!team_ok(match.at("away")) || !match.at("owners").is_array()) return false;
|
||||
for (const auto &id : match.at("owners")) if (!id.is_string()) return false;
|
||||
}
|
||||
for (const auto &owner : v.at("owners")) {
|
||||
if (!text(owner.at("id")) || !owner_ids.insert(owner.at("id")).second || !text(owner.at("name")) ||
|
||||
!count(owner.at("portrait"), 11) || !team_ok(owner.at("team")) || !match_ids.contains(owner.at("match_id")) ||
|
||||
!owner.at("view").is_object() || owner.at("view").at("engine") != true || !owner.at("view").at("rows").is_array() ||
|
||||
owner.at("view").at("rows").size() > 64) return false;
|
||||
for (const auto &row : owner.at("view").at("rows"))
|
||||
if (!text(row.at("id")) || !text(row.at("name")) || !row.contains("strength") || !row.contains("condition")) return false;
|
||||
}
|
||||
const int64_t duration = v.at("duration");
|
||||
int64_t minute = -1; size_t sequence = 0;
|
||||
for (const auto &frame : v.at("frames")) {
|
||||
if (frame.at("sequence") != sequence++ || !count(frame.at("minute"), uint64_t(duration)) || frame.at("minute").get<int64_t>() < minute ||
|
||||
!frame.at("events").is_array() || frame.at("events").size() > 8000 || !frame.at("matches").is_array() ||
|
||||
frame.at("matches").size() != match_ids.size()) return false;
|
||||
minute = frame.at("minute");
|
||||
const auto phase = frame.value("phase", "");
|
||||
if (phase != "first_half" && phase != "interval" && phase != "second_half" && phase != "extra_time" &&
|
||||
phase != "shootout" && phase != "finished") return false;
|
||||
if ((phase == "finished") != (sequence == v.at("frames").size())) return false;
|
||||
std::set<std::string> scored;
|
||||
for (const auto &m : frame.at("matches")) {
|
||||
if (!match_ids.contains(m.at("match_id")) || !scored.insert(m.at("match_id")).second) return false;
|
||||
for (auto key : {"score", "chances", "yellow", "red", "players"})
|
||||
if (!m.at(key).is_array() || m.at(key).size() != 2 || !count(m.at(key)[0], 1000) || !count(m.at(key)[1], 1000)) return false;
|
||||
if (!m.at("standings").is_array() || m.at("standings").size() > 128) return false;
|
||||
for (const auto &row : m.at("standings")) {
|
||||
if (!team_ok(row.at("team"))) return false;
|
||||
for (auto key : {"position", "played", "gf", "ga", "points"}) if (!count(row.at(key))) return false;
|
||||
}
|
||||
}
|
||||
for (const auto &event : frame.at("events"))
|
||||
if (!text(event.at("id")) || !match_ids.contains(event.at("match_id")) || !count(event.at("minute"), 200) ||
|
||||
!text(event.at("headline")) || !text(event.at("body"), 2200) || !text(event.at("kind"), 40) ||
|
||||
(event.at("side") != "home" && event.at("side") != "away") || !event.at("score").is_array() ||
|
||||
event.at("score").size() != 2 || !count(event.at("score")[0], 1000) || !count(event.at("score")[1], 1000)) return false;
|
||||
}
|
||||
return true;
|
||||
} catch (const std::exception &) { return false; }
|
||||
}
|
||||
|
||||
Json engine_live_fixture(const Json &recording, const Json &bundle) {
|
||||
try {
|
||||
if (!recording.is_object()) return failure("live_unavailable");
|
||||
if (!recording.value("ok", false)) return failure(recording.value("error", "live_unavailable"));
|
||||
Json out = recording;
|
||||
out["source"] = "engine-recording";
|
||||
const std::string role = out.value("role", "club") == "national" ? "national" : "club";
|
||||
out["role"] = role;
|
||||
if (!out.contains("surface")) out["surface"] = "outdoor";
|
||||
|
||||
// The desk manager's squad in line-up order (the squad after the kickoff).
|
||||
std::string desk_team;
|
||||
Json rows = Json::array();
|
||||
try {
|
||||
desk_team = bundle.at("desktop").at("meta").at("scope").at("teamId").get<std::string>();
|
||||
const auto &players = bundle.at("squad").at("data").at("players");
|
||||
std::map<std::string, const Json *> by_id;
|
||||
std::vector<std::string> listed;
|
||||
for (const auto &p : players) {
|
||||
const std::string id = p.at("player").at("id");
|
||||
by_id[id] = &p; listed.push_back(id);
|
||||
}
|
||||
std::vector<std::string> order;
|
||||
try {
|
||||
for (const auto &id : bundle.at("lineup").at("data").at("order")) if (id.is_string()) order.push_back(id);
|
||||
} catch (const std::exception &) {}
|
||||
std::set<std::string> seen;
|
||||
const auto add = [&](const std::string &id) {
|
||||
const auto at = by_id.find(id);
|
||||
if (at == by_id.end() || !seen.insert(id).second) return;
|
||||
const auto &p = *at->second;
|
||||
rows.push_back({{"id", id}, {"name", p.at("player").at("name")}, {"slot", rows.size()},
|
||||
{"strength", metric(p, "strength")}, {"condition", metric(p, "condition")},
|
||||
{"caps", nullptr}, {"international_goals", nullptr}});
|
||||
};
|
||||
for (const auto &id : order) add(id);
|
||||
for (const auto &id : listed) add(id);
|
||||
} catch (const std::exception &) { rows = Json::array(); }
|
||||
|
||||
if (!out.at("owners").is_array() || out.at("owners").empty()) return failure("live_no_human_match");
|
||||
std::set<int> portraits;
|
||||
for (auto &owner : out.at("owners")) {
|
||||
int portrait = owner.value("portrait", 0);
|
||||
portrait = std::clamp(portrait, 0, 11);
|
||||
for (int tries = 0; tries < 12 && portraits.contains(portrait); ++tries) portrait = (portrait + 1) % 12;
|
||||
portraits.insert(portrait);
|
||||
owner["portrait"] = portrait;
|
||||
if (!owner.at("team").contains("role")) owner["team"]["role"] = role;
|
||||
const bool own = owner.at("team").at("id") == desk_team;
|
||||
owner["view"] = {{"ok", true}, {"engine", true}, {"rows", own ? rows : Json::array()},
|
||||
{"field_count", out.value("field_count", 11)}};
|
||||
}
|
||||
for (auto &match : out.at("matches")) {
|
||||
for (auto side : {"home", "away"}) if (!match.at(side).contains("role")) match[side]["role"] = role;
|
||||
if (!match.contains("owners") || !match.at("owners").is_array()) match["owners"] = Json::array();
|
||||
}
|
||||
|
||||
// Tables arrive on the frame when they change; the ticker wants each match's table.
|
||||
std::map<std::string, Json> tables; // competition id -> ticker rows
|
||||
const int64_t duration = out.value("duration", 90);
|
||||
auto &frames = out.at("frames");
|
||||
for (size_t i = 0; i < frames.size(); ++i) {
|
||||
auto &frame = frames[i];
|
||||
const std::string phase = frame.value("phase", "");
|
||||
const bool last = i + 1 == frames.size();
|
||||
frame["phase"] = last ? "finished"
|
||||
: phase == "kickoff" ? "first_half"
|
||||
: phase == "restart" ? "second_half"
|
||||
: phase == "fulltime" || phase == "shootout_end" || phase == "finished" ? "second_half"
|
||||
: phase;
|
||||
if (frame.at("minute").is_number_integer()) frame["minute"] = std::clamp<int64_t>(frame.at("minute").get<int64_t>(), 0, duration);
|
||||
if (frame.contains("standings") && frame.at("standings").is_array())
|
||||
for (const auto &table : frame.at("standings")) {
|
||||
if (!table.is_object() || !table.contains("rows")) continue;
|
||||
Json converted = Json::array();
|
||||
for (const auto &row : table.at("rows")) {
|
||||
Json team = row.at("team");
|
||||
team["role"] = "club";
|
||||
converted.push_back({{"team", team}, {"position", row.at("rank")}, {"played", row.at("played")},
|
||||
{"gf", row.at("goalsFor")}, {"ga", row.at("goalsAgainst")}, {"points", row.at("points")}});
|
||||
}
|
||||
tables[table.value("competitionId", std::string("table"))] = std::move(converted);
|
||||
}
|
||||
for (auto &score : frame.at("matches")) {
|
||||
score["standings"] = Json::array();
|
||||
for (auto key : {"score", "chances", "yellow", "red", "players"})
|
||||
for (auto &value : score.at(key)) if (value.is_number_integer() && value.get<int64_t>() < 0) value = 0;
|
||||
const Json *home = nullptr;
|
||||
for (const auto &match : out.at("matches")) if (match.at("id") == score.at("match_id")) home = &match.at("home").at("id");
|
||||
if (!home || role == "national") continue;
|
||||
for (const auto &[id, table] : tables) {
|
||||
(void)id;
|
||||
const bool has = std::any_of(table.begin(), table.end(), [&](const Json &row) { return row.at("team").at("id") == *home; });
|
||||
if (has) { score["standings"] = table; break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!valid_engine_live_fixture(out)) return failure("invalid_live_fixture");
|
||||
return out;
|
||||
} catch (const std::exception &) { return failure("invalid_live_fixture"); }
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user