Accept fixture-bound supplied review nominations without deriving match facts (by Codex)
This commit is contained in:
1 parent
2c4b9d2595
commit
925042b559
6 files changed
+344
-5
No files matched your search
@@ -130,7 +130,11 @@ foreach(component statistics news settings desktop files entry opening career li
|
||||
if(MSVC)
|
||||
target_compile_options(modern_${component}_ui_tests PRIVATE /utf-8)
|
||||
endif()
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
|
||||
if(component STREQUAL "post_match")
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests "${CMAKE_SOURCE_DIR}/tests/fixtures/modern_post_match_eleven.json")
|
||||
else()
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
add_executable(modern_news_catalog_tests ../../../tests/modern_news_catalog_tests.cpp)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include "modern/ui/demo_live.hpp"
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
@@ -12,6 +14,68 @@ bool unsigned_value(const Json &v, uint64_t maximum) {
|
||||
(v.is_number_integer() && v.get<int64_t>() >= 0 &&
|
||||
uint64_t(v.get<int64_t>()) <= maximum);
|
||||
}
|
||||
bool text(const Json &v, size_t maximum = 240) {
|
||||
if (!v.is_string()) return false;
|
||||
const auto &s = v.get_ref<const std::string &>();
|
||||
if (s.empty() || s.size() > maximum ||
|
||||
std::any_of(s.begin(), s.end(), [](unsigned char c) { return c < 32 || c == 127; })) return false;
|
||||
(void)v.dump(); // The existing JSON serializer rejects malformed UTF-8.
|
||||
return true;
|
||||
}
|
||||
bool keys(const Json &v, std::initializer_list<const char *> names) {
|
||||
if (!v.is_object() || v.size() != names.size()) return false;
|
||||
for (const auto *name : names) if (!v.contains(name)) return false;
|
||||
return true;
|
||||
}
|
||||
const char *attach_records(Json &review, const Json &fixture, const Json &records) {
|
||||
try {
|
||||
if (records.is_object() && records.empty()) return nullptr;
|
||||
if (!keys(records, {"schema", "fixture_id", "source_revision", "eleven"}) ||
|
||||
records.at("schema") != "openbmh-post-match-records@1") return "invalid_post_match_records";
|
||||
if (!text(records.at("fixture_id")) || !unsigned_value(records.at("source_revision"), UINT64_MAX) ||
|
||||
records.at("fixture_id") != fixture.at("fixture_id") ||
|
||||
records.at("source_revision") != fixture.at("source_revision")) return "stale_post_match_records";
|
||||
if (fixture.at("role") != "club" || fixture.at("surface") != "outdoor")
|
||||
return "post_match_records_scope_unavailable";
|
||||
const auto &eleven = records.at("eleven");
|
||||
if (!keys(eleven, {"available", "source", "notice", "formation", "rows"}) ||
|
||||
!eleven.at("available").is_boolean() || !eleven.at("available").get<bool>() ||
|
||||
(eleven.at("source") != "supplied-records" && eleven.at("source") != "synthetic-layout-fixture") ||
|
||||
!text(eleven.at("notice"), 1000) || eleven.at("formation") != "3-4-3" ||
|
||||
!eleven.at("rows").is_array() || eleven.at("rows").size() != 11)
|
||||
return "invalid_post_match_records";
|
||||
std::map<std::string, Json> teams;
|
||||
for (const auto &match : fixture.at("matches"))
|
||||
for (const auto *side : {"home", "away"}) teams.emplace(match.at(side).at("id"), match.at(side));
|
||||
for (const auto &snapshot : fixture.at("frames").back().at("matches"))
|
||||
for (const auto &row : snapshot.at("standings")) teams.emplace(row.at("team").at("id"), row.at("team"));
|
||||
std::set<std::string> players;
|
||||
std::map<std::string, size_t> positions;
|
||||
for (const auto &row : eleven.at("rows")) {
|
||||
if (!keys(row, {"player", "team", "position", "rating"}) ||
|
||||
!keys(row.at("player"), {"id", "name"}) ||
|
||||
!keys(row.at("team"), {"id", "name", "role"}) ||
|
||||
!text(row.at("player").at("id")) || !text(row.at("player").at("name")) ||
|
||||
!players.insert(row.at("player").at("id")).second ||
|
||||
!text(row.at("team").at("id")) || !text(row.at("team").at("name")) ||
|
||||
row.at("team").at("role") != "club" || !row.at("rating").is_null() ||
|
||||
!row.at("position").is_string()) return "invalid_post_match_records";
|
||||
const auto team = teams.find(row.at("team").at("id").get<std::string>());
|
||||
if (team == teams.end() || team->second.at("name") != row.at("team").at("name"))
|
||||
return "invalid_post_match_records";
|
||||
const auto position = row.at("position").get<std::string>();
|
||||
if (position != "gk" && position != "def" && position != "mid" && position != "fwd")
|
||||
return "invalid_post_match_records";
|
||||
++positions[position];
|
||||
}
|
||||
if (positions["gk"] != 1 || positions["def"] != 3 || positions["mid"] != 4 || positions["fwd"] != 3)
|
||||
return "invalid_post_match_records";
|
||||
review["eleven"] = eleven; // Preserve supplied names/identities; never nominate here.
|
||||
auto &unknown = review["unknown_fields"];
|
||||
unknown.erase(std::remove(unknown.begin(), unknown.end(), Json("eleven")), unknown.end());
|
||||
return nullptr;
|
||||
} catch (const std::exception &) { return "invalid_post_match_records"; }
|
||||
}
|
||||
Json provenance(const Json &fixture) {
|
||||
return {{"ok", true}, {"schema", "openbmh-post-match@1"},
|
||||
{"source", fixture.at("source")},
|
||||
@@ -65,7 +129,7 @@ Json report(const Json &fixture, const Json &match, int minute) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json make_demo_post_match(const Json &fixture, size_t completed_sequence) {
|
||||
Json make_demo_post_match(const Json &fixture, size_t completed_sequence, const Json &records) {
|
||||
try {
|
||||
if (!valid_live_fixture(fixture)) return failure("invalid_post_match_fixture");
|
||||
const auto &last = fixture.at("frames").back();
|
||||
@@ -109,11 +173,16 @@ Json make_demo_post_match(const Json &fixture, size_t completed_sequence) {
|
||||
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"});
|
||||
if (const auto *error = attach_records(out, fixture, records)) return failure(error);
|
||||
return out;
|
||||
} catch (const std::exception &) { return failure("invalid_post_match_fixture"); }
|
||||
}
|
||||
|
||||
Json query_demo_post_match(const Json &fixture, const Json &q) {
|
||||
Json make_demo_post_match(const Json &fixture, size_t completed_sequence) {
|
||||
return make_demo_post_match(fixture, completed_sequence, Json::object());
|
||||
}
|
||||
|
||||
Json query_demo_post_match(const Json &fixture, const Json &q, const Json &records) {
|
||||
try {
|
||||
if (!q.is_object()) return failure("invalid_post_match_query");
|
||||
for (const auto &[key, value] : q.items()) {
|
||||
@@ -125,7 +194,7 @@ Json query_demo_post_match(const Json &fixture, const Json &q) {
|
||||
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>());
|
||||
auto review = make_demo_post_match(fixture, q.at("completed_sequence").get<size_t>(), records);
|
||||
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")))
|
||||
@@ -148,4 +217,7 @@ Json query_demo_post_match(const Json &fixture, const Json &q) {
|
||||
return report(fixture, *match, minute.get<int>());
|
||||
} catch (const std::exception &) { return failure("invalid_post_match_query"); }
|
||||
}
|
||||
Json query_demo_post_match(const Json &fixture, const Json &q) {
|
||||
return query_demo_post_match(fixture, q, Json::object());
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -8,9 +8,15 @@ namespace bmh::modern_ui {
|
||||
// sequence acknowledges UI playback completion, never engine match completion.
|
||||
// The recording, its identities and every original outcome remain unchanged.
|
||||
Json make_demo_post_match(const Json &live_fixture, size_t completed_sequence);
|
||||
// Optional externally supplied nominations, explicitly bound to this recording.
|
||||
// No nominations/ratings are derived from catalogue strength or live events.
|
||||
Json make_demo_post_match(const Json &live_fixture, size_t completed_sequence,
|
||||
const Json &records);
|
||||
// {kind:"review"|"report", completed_sequence:uint,
|
||||
// fixture_id?:string, revision?:uint, match_id?:string, minute?:uint}
|
||||
// match_id/minute are report-only. Minute seeking selects an existing recorded
|
||||
// frame; it does not interpolate scores, calculate ratings or execute actions.
|
||||
Json query_demo_post_match(const Json &live_fixture, const Json &request);
|
||||
Json query_demo_post_match(const Json &live_fixture, const Json &request,
|
||||
const Json &records);
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user