Project completed demo live recordings into read-only match reviews (by Codex)
This commit is contained in:
1 parent
fec4c6c7e7
commit
2c4b9d2595
7 files changed
+464
-2
No files matched your search
@@ -0,0 +1,151 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_post_match.hpp"
|
||||
#include "modern/ui/demo_live.hpp"
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
Json failure(const char *error) { return {{"ok", false}, {"error", error}}; }
|
||||
bool unsigned_value(const Json &v, uint64_t maximum) {
|
||||
return (v.is_number_unsigned() && v.get<uint64_t>() <= maximum) ||
|
||||
(v.is_number_integer() && v.get<int64_t>() >= 0 &&
|
||||
uint64_t(v.get<int64_t>()) <= maximum);
|
||||
}
|
||||
Json provenance(const Json &fixture) {
|
||||
return {{"ok", true}, {"schema", "openbmh-post-match@1"},
|
||||
{"source", fixture.at("source")},
|
||||
{"review_id", "ui-post:" + fixture.at("fixture_id").get<std::string>()},
|
||||
{"fixture_id", fixture.at("fixture_id")},
|
||||
{"source_revision", fixture.at("source_revision")},
|
||||
{"completed_sequence", fixture.at("frames").size() - 1},
|
||||
{"world", fixture.at("world")}, {"date", fixture.at("date")},
|
||||
{"role", fixture.at("role")}, {"surface", fixture.at("surface")},
|
||||
{"duration", fixture.at("duration")}, {"field_count", fixture.at("field_count")},
|
||||
{"notice", "UI-Demo · Rückblick auf vorgegebene Ergebnisse, keine Spielsimulation"}};
|
||||
}
|
||||
Json match_events(const Json &events, const Json &match_id) {
|
||||
Json rows = Json::array();
|
||||
for (const auto &event : events)
|
||||
if (event.at("match_id") == match_id) rows.push_back(event);
|
||||
return rows;
|
||||
}
|
||||
const Json &match_snapshot(const Json &frame, const Json &match_id) {
|
||||
return *std::find_if(frame.at("matches").begin(), frame.at("matches").end(),
|
||||
[&](const auto &m) { return m.at("match_id") == match_id; });
|
||||
}
|
||||
Json report(const Json &fixture, const Json &match, int minute) {
|
||||
const auto &frames = fixture.at("frames");
|
||||
const Json *frame = &frames.front();
|
||||
for (const auto &candidate : frames) {
|
||||
if (candidate.at("minute").get<int>() > minute) break;
|
||||
frame = &candidate;
|
||||
}
|
||||
const auto ¤t = match_snapshot(*frame, match.at("id"));
|
||||
const auto &final = match_snapshot(frames.back(), match.at("id"));
|
||||
auto out = provenance(fixture);
|
||||
out["kind"] = "report";
|
||||
out["match_id"] = match.at("id");
|
||||
out["match"] = match;
|
||||
out["home"] = match.at("home"); out["away"] = match.at("away");
|
||||
out["minute"] = minute; out["recorded_minute"] = frame->at("minute");
|
||||
out["sequence"] = frame->at("sequence"); out["phase"] = frame->at("phase");
|
||||
out["final"] = final; out["final_score"] = final.at("score");
|
||||
for (auto key : {"score", "chances", "yellow", "red", "players", "standings"})
|
||||
out[key] = current.at(key);
|
||||
// Raw events retain their original IDs/text/side/score. The recording names
|
||||
// no player actor, assist or exact playing-time/ratings, so none is inferred.
|
||||
out["events"] = match_events(frame->at("events"), match.at("id"));
|
||||
out["timeline"] = match_events(frames.back().at("events"), match.at("id"));
|
||||
out["attendance"] = nullptr;
|
||||
out["attendance_reason"] = "not_recorded";
|
||||
out["actor"] = nullptr; out["assists"] = nullptr; out["ratings"] = nullptr;
|
||||
out["playing_minutes"] = nullptr;
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json make_demo_post_match(const Json &fixture, size_t completed_sequence) {
|
||||
try {
|
||||
if (!valid_live_fixture(fixture)) return failure("invalid_post_match_fixture");
|
||||
const auto &last = fixture.at("frames").back();
|
||||
if (completed_sequence != fixture.at("frames").size() - 1 ||
|
||||
last.at("phase") != "finished" || last.at("minute") != fixture.at("duration"))
|
||||
return failure("post_match_not_finished");
|
||||
auto out = provenance(fixture);
|
||||
out["kind"] = "review";
|
||||
out["owners"] = fixture.at("owners");
|
||||
out["tabs"] = fixture.at("role") == "club" && fixture.at("surface") == "outdoor"
|
||||
? Json::array({"results", "report", "eleven", "overview", "table"})
|
||||
: Json::array({"results", "report", "overview"});
|
||||
out["matches"] = Json::array(); out["tables"] = Json::array();
|
||||
Json totals = {{"games", fixture.at("matches").size()}, {"goals", 0},
|
||||
{"yellow", 0}, {"red", 0}, {"chances", 0},
|
||||
{"attendance", nullptr}, {"attendance_reason", "not_recorded"},
|
||||
{"scope", "recorded_human_matches"}};
|
||||
for (const auto &match : fixture.at("matches")) {
|
||||
const auto &final = match_snapshot(last, match.at("id"));
|
||||
auto row = match;
|
||||
row["final"] = final;
|
||||
row["events"] = match_events(last.at("events"), match.at("id"));
|
||||
row["attendance"] = nullptr; row["attendance_reason"] = "not_recorded";
|
||||
out["matches"].push_back(std::move(row));
|
||||
for (auto key : {"yellow", "red", "chances"})
|
||||
totals[key] = totals.at(key).get<int>() + final.at(key)[0].get<int>() + final.at(key)[1].get<int>();
|
||||
totals["goals"] = totals.at("goals").get<int>() + final.at("score")[0].get<int>() + final.at("score")[1].get<int>();
|
||||
// Preserve declared final table snapshots exactly. Several human matches
|
||||
// can refer to one recorded table; do not recalculate a season standings.
|
||||
if (out.at("tabs").back() == "table" && !final.at("standings").empty()) {
|
||||
auto table = std::find_if(out["tables"].begin(), out["tables"].end(),
|
||||
[&](const auto &t) { return t.at("rows") == final.at("standings"); });
|
||||
if (table != out["tables"].end()) table->at("match_ids").push_back(match.at("id"));
|
||||
else out["tables"].push_back({{"id", "ui-table:" + match.at("id").get<std::string>()},
|
||||
{"match_ids", Json::array({match.at("id")})},
|
||||
{"rows", final.at("standings")},
|
||||
{"notice", "Vorgegebener POC-Tabellenstand der Liveaufnahme"},
|
||||
{"wins", nullptr}, {"draws", nullptr}, {"losses", nullptr}});
|
||||
}
|
||||
}
|
||||
out["overview"] = std::move(totals);
|
||||
out["eleven"] = {{"available", false}, {"reason", "not_recorded"}, {"rows", Json::array()}};
|
||||
out["unknown_fields"] = Json::array({"attendance", "actor", "assists", "ratings", "playing_minutes", "eleven", "wins", "draws", "losses"});
|
||||
return out;
|
||||
} catch (const std::exception &) { return failure("invalid_post_match_fixture"); }
|
||||
}
|
||||
|
||||
Json query_demo_post_match(const Json &fixture, const Json &q) {
|
||||
try {
|
||||
if (!q.is_object()) return failure("invalid_post_match_query");
|
||||
for (const auto &[key, value] : q.items()) {
|
||||
(void)value;
|
||||
if (key != "kind" && key != "completed_sequence" && key != "fixture_id" &&
|
||||
key != "revision" && key != "match_id" && key != "minute")
|
||||
return failure("invalid_post_match_query");
|
||||
}
|
||||
if (!q.contains("completed_sequence") ||
|
||||
!unsigned_value(q.at("completed_sequence"), std::numeric_limits<size_t>::max()))
|
||||
return failure("invalid_post_match_sequence");
|
||||
auto review = make_demo_post_match(fixture, q.at("completed_sequence").get<size_t>());
|
||||
if (!review.value("ok", false)) return review;
|
||||
if (q.contains("revision") && (!unsigned_value(q.at("revision"), UINT64_MAX) ||
|
||||
q.at("revision") != fixture.at("source_revision")))
|
||||
return failure("stale_post_match_revision");
|
||||
if (q.contains("fixture_id") && (!q.at("fixture_id").is_string() ||
|
||||
q.at("fixture_id") != fixture.at("fixture_id")))
|
||||
return failure("stale_post_match_fixture");
|
||||
const auto kind = q.value("kind", "review");
|
||||
if (kind == "review")
|
||||
return q.contains("match_id") || q.contains("minute") ? failure("invalid_post_match_query") : review;
|
||||
if (kind != "report") return failure("invalid_post_match_query");
|
||||
const auto id = q.contains("match_id") ? q.at("match_id") : fixture.at("matches").front().at("id");
|
||||
if (!id.is_string()) return failure("invalid_post_match_id");
|
||||
const auto match = std::find_if(fixture.at("matches").begin(), fixture.at("matches").end(),
|
||||
[&](const auto &m) { return m.at("id") == id; });
|
||||
if (match == fixture.at("matches").end()) return failure("invalid_post_match_id");
|
||||
const auto minute = q.contains("minute") ? q.at("minute") : fixture.at("duration");
|
||||
if (!unsigned_value(minute, fixture.at("duration").get<uint64_t>()))
|
||||
return failure("invalid_post_match_minute");
|
||||
return report(fixture, *match, minute.get<int>());
|
||||
} catch (const std::exception &) { return failure("invalid_post_match_query"); }
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user