Play the Modern kickoff live: minute steps, substitutions and tactics from the ticker
A rehearsal (session.advance with dryRun) settles questions of the run before the kickoff; an extension answered there is agreed at the player's demand of that moment. With an own match the kickoff runs live: one live.step per beat of the speed control, substitutions and the tactics board through live.substitute and live.tactics, and leaving the ticker plays the match to its end. After the final whistle the engine's recording replaces the frames composed on the way. The paused live read names clubs in the world's spelling, and the review table shows wins, draws and losses. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d66609ca8
commit
a5810b15ae
14 files changed
+456
-31
No files matched your search
@@ -69,7 +69,8 @@ bool valid_engine_live_fixture(const Json &v) {
|
||||
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;
|
||||
// a kickoff still under way has no final frame yet
|
||||
if (v.value("live_running", false) ? phase == "finished" : (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;
|
||||
@@ -172,7 +173,8 @@ Json engine_live_fixture(const Json &recording, const Json &bundle) {
|
||||
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")}});
|
||||
{"gf", row.at("goalsFor")}, {"ga", row.at("goalsAgainst")}, {"points", row.at("points")},
|
||||
{"won", row.value("won", Json())}, {"drawn", row.value("drawn", Json())}, {"lost", row.value("lost", Json())}});
|
||||
}
|
||||
tables[table.value("competitionId", std::string("table"))] = std::move(converted);
|
||||
}
|
||||
@@ -194,4 +196,162 @@ Json engine_live_fixture(const Json &recording, const Json &bundle) {
|
||||
return out;
|
||||
} catch (const std::exception &) { return failure("invalid_live_fixture"); }
|
||||
}
|
||||
|
||||
Json engine_live_command(const Json &bundle, const std::string &revision, const std::string &request_id,
|
||||
const std::string &kind, const Json &payload) {
|
||||
const auto &meta = bundle.at("desktop").at("meta");
|
||||
return {{"messageType", "command"}, {"contractVersion", meta.at("contractVersion")}, {"requestId", request_id},
|
||||
{"careerId", meta.at("careerId")}, {"expectedRevision", revision},
|
||||
{"owner", {{"managerId", meta.at("scope").at("managerId")}}},
|
||||
{"kind", kind}, {"payload", payload.is_object() ? payload : Json::object()}};
|
||||
}
|
||||
|
||||
Json engine_live_roster(const Json &bundle) {
|
||||
Json rows = Json::array();
|
||||
try {
|
||||
const auto &players = bundle.at("squad").at("data").at("players");
|
||||
std::map<std::string, const Json *> by_id;
|
||||
std::vector<std::string> listed, order;
|
||||
for (const auto &p : players) {
|
||||
const std::string id = p.at("player").at("id");
|
||||
by_id[id] = &p; listed.push_back(id);
|
||||
}
|
||||
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(); }
|
||||
return rows;
|
||||
}
|
||||
|
||||
// The standing kickoff as the ticker's fixture: the frames so far plus one for this minute.
|
||||
// Event texts are composed here from the engine's facts (kind, player, minute); the full
|
||||
// recording with the engine's own wording replaces this fixture after the final whistle.
|
||||
Json engine_live_progress(const Json &previous, const Json &state, const Json &bundle, const Json &roster) {
|
||||
try {
|
||||
if (!state.is_object() || !state.value("ok", false) || !state.value("live", false)) return failure("live_unavailable");
|
||||
const auto &meta = bundle.at("desktop").at("meta");
|
||||
const std::string desk_team = meta.at("scope").at("teamId"), desk_manager = meta.at("scope").at("managerId");
|
||||
const std::string role = desk_team.starts_with("nt:") || meta.at("scope").value("role", "club") == "national" ? "national" : "club";
|
||||
const int64_t minute = std::clamp<int64_t>(state.at("minute").get<int64_t>(), 0, 120);
|
||||
Json out = previous.is_object() && previous.value("live_running", false) ? previous : Json();
|
||||
if (!out.is_object()) {
|
||||
out = {{"ok", true}, {"schema", "openbmh-live-fixture@1"}, {"source", "engine-recording"}, {"live_running", true},
|
||||
{"fixture_id", "live:running:" + meta.at("date").get<std::string>()}, {"source_revision", 0},
|
||||
{"date", meta.at("date")}, {"world", meta.value("worldId", std::string())}, {"role", role}, {"surface", "outdoor"},
|
||||
{"duration", 90}, {"field_count", 11}, {"owners", Json::array()}, {"matches", Json::array()},
|
||||
{"frames", Json::array()}, {"notice", "Der Spieltag läuft"}};
|
||||
std::set<int> portraits;
|
||||
for (const auto &m : state.at("matches")) {
|
||||
const std::string id = "live:" + m.at("home").at("id").get<std::string>();
|
||||
Json owners = Json::array();
|
||||
for (auto side : {"home", "away"}) {
|
||||
const auto &manager = m.at(std::string(side) + "ManagerId");
|
||||
if (!manager.is_string() || out.at("owners").size() >= 4) continue;
|
||||
const bool own = manager == desk_manager;
|
||||
int portrait = int(out.at("owners").size());
|
||||
Json team = m.at(side); team["role"] = role;
|
||||
std::string name = own ? bundle.at("desktop").at("data").at("manager").value("name", std::string("Manager")) : std::string("Manager");
|
||||
out["owners"].push_back({{"id", manager}, {"portrait", portrait}, {"name", name}, {"team", team}, {"match_id", id},
|
||||
{"view", {{"ok", true}, {"engine", true}, {"rows", own ? roster : Json::array()}, {"field_count", 11}}}});
|
||||
owners.push_back(manager);
|
||||
}
|
||||
Json home = m.at("home"), away = m.at("away");
|
||||
home["role"] = role; away["role"] = role;
|
||||
out["matches"].push_back({{"id", id}, {"home", home}, {"away", away}, {"owners", owners}, {"competition", "Spieltag"},
|
||||
{"attendance", m.value("attendance", Json())}, {"weather", m.value("weather", Json())}});
|
||||
}
|
||||
if (out.at("owners").empty() || out.at("matches").empty()) return failure("live_no_human_match");
|
||||
}
|
||||
// the desk manager's roster follows substitutions made during the match
|
||||
for (auto &owner : out.at("owners")) if (owner.at("id") == desk_manager) owner["view"]["rows"] = roster;
|
||||
if (minute > 90) out["duration"] = 120;
|
||||
|
||||
Json events = Json::array(), scores = Json::array();
|
||||
const size_t sequence = out.at("frames").size() + (out.at("frames").empty() ? 1 : 0);
|
||||
// earlier events keep the frame they first appeared in
|
||||
std::map<std::string, Json> known;
|
||||
if (!out.at("frames").empty())
|
||||
for (const auto &e : out.at("frames").back().at("events")) known[e.at("id")] = e;
|
||||
for (const auto &m : state.at("matches")) {
|
||||
const std::string id = "live:" + m.at("home").at("id").get<std::string>();
|
||||
int goals[2] = {0, 0}, chances[2] = {0, 0}, yellow[2] = {0, 0}, red[2] = {0, 0};
|
||||
size_t index = 0;
|
||||
for (const auto &e : m.at("events")) {
|
||||
const std::string kind = e.value("kind", std::string());
|
||||
const size_t side = e.value("side", std::string("home")) == "away" ? 1 : 0;
|
||||
const std::string team = m.at(side ? "away" : "home").at("name");
|
||||
const std::string player = e.at("player").is_object() ? e.at("player").value("name", std::string()) : std::string();
|
||||
const std::string other = e.contains("other") && e.at("other").is_object() ? e.at("other").value("name", std::string()) : std::string();
|
||||
std::string headline, body, shown = kind;
|
||||
if (kind == "goal") {
|
||||
++goals[side]; ++chances[side];
|
||||
headline = "Tor für " + team;
|
||||
body = (player.empty() ? std::string("Treffer") : player + " trifft") + " zum " + std::to_string(goals[0]) + ":" + std::to_string(goals[1]) + "." +
|
||||
(other.empty() ? "" : " Vorlage: " + other + ".");
|
||||
} else if (kind == "miss") {
|
||||
++chances[side]; shown = "chance";
|
||||
headline = "Chance für " + team; body = player.empty() ? "Die Chance bleibt ungenutzt." : player + " vergibt.";
|
||||
} else if (kind == "yellow") {
|
||||
++yellow[side]; headline = "Gelbe Karte"; body = (player.empty() ? "Ein Spieler" : player) + " (" + team + ") sieht Gelb.";
|
||||
} else if (kind == "second_yellow" || kind == "red") {
|
||||
++red[side]; shown = "red";
|
||||
headline = kind == "red" ? "Rote Karte" : "Gelb-Rot"; body = (player.empty() ? "Ein Spieler" : player) + " (" + team + ") muss vom Platz.";
|
||||
} else if (kind == "injury") {
|
||||
headline = "Verletzung"; body = (player.empty() ? "Ein Spieler" : player) + " (" + team + ") muss behandelt werden.";
|
||||
} else continue;
|
||||
const std::string event_id = id + ":event:" + std::to_string(index++);
|
||||
const auto at = known.find(event_id);
|
||||
events.push_back(at != known.end() ? at->second
|
||||
: Json{{"id", event_id}, {"sequence", sequence}, {"match_id", id},
|
||||
{"minute", std::clamp<int64_t>(e.value("minute", int64_t(minute)), 0, minute)}, {"kind", shown},
|
||||
{"headline", headline}, {"body", body}, {"score", {goals[0], goals[1]}},
|
||||
{"side", side ? "away" : "home"}, {"player", e.at("player")}, {"other", e.value("other", Json())}});
|
||||
}
|
||||
Json score = m.value("score", Json::array({goals[0], goals[1]}));
|
||||
Json standings = Json::array();
|
||||
if (role == "club" && state.contains("standings"))
|
||||
for (const auto &table : state.at("standings")) {
|
||||
if (!table.is_object() || !table.contains("rows")) continue;
|
||||
Json rows = Json::array(); bool has = false;
|
||||
for (const auto &row : table.at("rows")) {
|
||||
Json team = row.at("team"); team["role"] = "club";
|
||||
has = has || team.at("id") == m.at("home").at("id");
|
||||
rows.push_back({{"team", team}, {"position", row.at("rank")}, {"played", row.at("played")}, {"gf", row.at("goalsFor")},
|
||||
{"ga", row.at("goalsAgainst")}, {"points", row.at("points")}, {"won", row.value("won", Json())},
|
||||
{"drawn", row.value("drawn", Json())}, {"lost", row.value("lost", Json())}});
|
||||
}
|
||||
if (has) { standings = std::move(rows); break; }
|
||||
}
|
||||
scores.push_back({{"match_id", id}, {"score", score}, {"chances", {chances[0], chances[1]}}, {"yellow", {yellow[0], yellow[1]}},
|
||||
{"red", {red[0], red[1]}}, {"players", {std::max(0, 11 - red[0]), std::max(0, 11 - red[1])}},
|
||||
{"standings", standings}});
|
||||
}
|
||||
// events are shown newest last, in minute order across the matches
|
||||
std::stable_sort(events.begin(), events.end(), [](const Json &a, const Json &b) { return a.at("minute").get<int64_t>() < b.at("minute").get<int64_t>(); });
|
||||
if (out.at("frames").empty()) {
|
||||
Json zero = Json::array();
|
||||
for (const auto &s : scores) zero.push_back({{"match_id", s.at("match_id")}, {"score", {0, 0}}, {"chances", {0, 0}}, {"yellow", {0, 0}},
|
||||
{"red", {0, 0}}, {"players", {11, 11}}, {"standings", s.at("standings")}});
|
||||
out["frames"].push_back({{"sequence", 0}, {"minute", 0}, {"phase", "first_half"}, {"matches", zero}, {"events", Json::array()}});
|
||||
}
|
||||
const int64_t last_minute = out.at("frames").back().at("minute");
|
||||
out["frames"].push_back({{"sequence", out.at("frames").size()}, {"minute", std::max(minute, last_minute)},
|
||||
{"phase", minute <= 45 ? "first_half" : minute <= 90 ? "second_half" : "extra_time"},
|
||||
{"matches", scores}, {"events", events}});
|
||||
if (out.at("frames").size() > 1900) return failure("invalid_live_fixture");
|
||||
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