// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/demo_post_match.hpp" #include "modern/ui/demo_live.hpp" #include #include #include #include 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() <= maximum) || (v.is_number_integer() && v.get() >= 0 && uint64_t(v.get()) <= maximum); } bool text(const Json &v, size_t maximum = 240) { if (!v.is_string()) return false; const auto &s = v.get_ref(); 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 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() || (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 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 players; std::map 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()); 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(); 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")}, {"review_id", "ui-post:" + fixture.at("fixture_id").get()}, {"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() > 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"] = match.value("attendance", Json()); out["attendance_reason"] = out["attendance"].is_null() ? "not_recorded" : "recorded"; out["weather"] = match.value("weather", Json()); 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, const Json &records) { 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", int64_t(0)}, {"attendance_reason", "recorded"}, {"scope", "recorded_human_matches"}}; bool crowds_known = true; 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"] = match.value("attendance", Json()); row["attendance_reason"] = row["attendance"].is_null() ? "not_recorded" : "recorded"; row["weather"] = match.value("weather", Json()); if (row["attendance"].is_null()) crowds_known = false; else totals["attendance"] = totals.at("attendance").get() + row.at("attendance").get(); out["matches"].push_back(std::move(row)); for (auto key : {"yellow", "red", "chances"}) totals[key] = totals.at(key).get() + final.at(key)[0].get() + final.at(key)[1].get(); totals["goals"] = totals.at("goals").get() + final.at("score")[0].get() + final.at("score")[1].get(); // 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()}, {"match_ids", Json::array({match.at("id")})}, {"rows", final.at("standings")}, {"notice", "Vorgegebener POC-Tabellenstand der Liveaufnahme"}, {"wins", nullptr}, {"draws", nullptr}, {"losses", nullptr}}); } } if (!crowds_known) { totals["attendance"] = nullptr; totals["attendance_reason"] = "not_recorded"; } out["overview"] = std::move(totals); out["eleven"] = {{"available", false}, {"reason", "not_recorded"}, {"rows", Json::array()}}; out["unknown_fields"] = Json::array({"actor", "assists", "ratings", "playing_minutes", "eleven", "wins", "draws", "losses"}); if (!crowds_known) out["unknown_fields"].push_back("attendance"); 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 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()) { (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::max())) return failure("invalid_post_match_sequence"); auto review = make_demo_post_match(fixture, q.at("completed_sequence").get(), 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"))) 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())) return failure("invalid_post_match_minute"); return report(fixture, *match, minute.get()); } 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