Fix Almanac shoot-out recording and verified legacy views (by Codex)

This commit is contained in:
Codex committed 2026-10-28 20:42:40 +01:00
1 parent ddd4447563
commit f9e8719b2d
11 files changed
+309 -28

No files matched your search

+126 -1
View File
@@ -1,10 +1,15 @@
// SPDX-License-Identifier: GPL-3.0-only
// The football almanac (docs/new_engine/almanach.md, M8) on synthetic data: the binary
// season round trip, damaged input, the calendar dates and new player identities.
// season round trip, damaged input, dates/identities, shoot-out recording and read-only
// legacy normalization through the actual statistics projection.
#include <cstdio>
#include <algorithm>
#include <string>
#include "formats/binary.hpp"
#include "game/match_engine.hpp"
#include "game/world_layout.hpp"
#include "modern/content/statistics.hpp"
#include "modern/game/almanac.hpp"
#include "modern/game/modern_state.hpp"
@@ -20,6 +25,85 @@ void check(bool ok, const char* what) {
}
}
AlmanacMatch recorded_shootout(bool with_log, bool with_show, bool capped = false) {
ModernState g;
g.season = 0; g.display_start_year = 2026;
const int home = first_foreign_cup(), away = home + 1;
g.world_club_id.resize(size_t(away + 1)); g.world_club_name.resize(size_t(away + 1));
g.world_club_id[size_t(home)] = "club:a"; g.world_club_name[size_t(home)] = "A";
g.world_club_id[size_t(away)] = "club:b"; g.world_club_name[size_t(away)] = "B";
std::vector<MatchGroup> groups(1);
auto& group = groups[0];
group.desc.resize(0xd0); group.desc[0] = 2;
group.fixtures = {home, away}; group.spectators = {12345};
const int normal = capped ? 5 : 1;
const int combined_home = capped ? 15 : 5, combined_away = capped ? 14 : 4;
group.cup_results = {uint16_t(0x800 | combined_home << 5 | combined_away)};
AlmanacRecorder recorder(g, groups);
const auto log = [&](int side, bool goal, int minute) {
const int team = side ? away : home;
group.log.insert(group.log.end(), {uint16_t(0xfff + (goal ? 0 : 0x1000)),
uint16_t(team * 0x80 + minute), uint16_t(0x2fff), uint16_t(team * 0x80 + 1)});
};
for (int n = 0; n < normal; ++n) { log(0, true, 10+n); log(1, true, 70+n); }
std::vector<ShowEvent> shown;
const auto kick = [&](int side, bool goal) {
if (with_log) log(side, goal, 121);
if (with_show) {
ShowEvent e{Show::PenaltyKick}; e.group = 0; e.match = 0; e.minute = 121;
e.side = uint8_t(side); e.flag = goal; e.player = 0xfff; shown.push_back(e);
}
};
if (capped) {
for (int i = 0; i < 12; ++i) { kick(0, true); kick(1, i != 11); }
} else {
const bool h[5] = {true,true,false,true,true}, a[5] = {true,false,true,true,false};
for (int i=0;i<5;++i) { kick(0,h[i]); kick(1,a[i]); }
}
const auto packed = group.cup_results;
recorder.finish(shown);
check(group.cup_results == packed, "recorder never rewrites packed match scores");
return g.almanac.current.matches.at(0);
}
void check_shootout_recording() {
const auto both=recorded_shootout(true,true), background=recorded_shootout(true,false),
shown_only=recorded_shootout(false,true), capped=recorded_shootout(true,true,true);
check(both==background && both==shown_only, "visible/background/show-only shoot-outs agree");
check(both.home_goals==1 && both.away_goals==1 && both.home_shootout==4 && both.away_shootout==3,
"normal and shoot-out goals stay separate");
check(both.events.size()==12 && std::count_if(both.events.begin(),both.events.end(),[](const auto& e){return e.kind==AlmanacEventKind::ShootoutKick;})==10,
"each logged/shown kick is recorded exactly once");
check(std::none_of(both.events.begin(),both.events.end(),[](const auto& e){return e.minute==121 && e.kind!=AlmanacEventKind::ShootoutKick;}),
"shoot-out misses and goals do not become normal match events");
check(capped.home_goals==5 && capped.away_goals==5 && capped.home_shootout==12 && capped.away_shootout==11,
"packed-score clamping cannot erase actual shoot-out goals");
auto legacy=both;
legacy.home_goals=5; legacy.away_goals=4; legacy.home_shootout=legacy.away_shootout=0;
std::vector<AlmanacEvent> duplicates;
for(const auto& e:both.events)if(e.kind==AlmanacEventKind::ShootoutKick) {
auto copy=e;copy.kind=e.detail?AlmanacEventKind::Goal:AlmanacEventKind::Miss;copy.detail=0;duplicates.push_back(copy);
}
legacy.events.insert(legacy.events.begin()+2,duplicates.begin(),duplicates.end());
const auto original=legacy;
check(normalize_legacy_shootout(legacy) && legacy==both, "exact legacy duplicate signature is recoverable");
check(!normalize_legacy_shootout(legacy), "legacy normalization is idempotent");
auto legacy_capped=capped;
legacy_capped.home_goals=15;legacy_capped.away_goals=14;legacy_capped.home_shootout=legacy_capped.away_shootout=0;
std::vector<AlmanacEvent> capped_duplicates;
for(const auto& e:capped.events)if(e.kind==AlmanacEventKind::ShootoutKick){auto copy=e;copy.kind=e.detail?AlmanacEventKind::Goal:AlmanacEventKind::Miss;copy.detail=0;capped_duplicates.push_back(copy);}
legacy_capped.events.insert(legacy_capped.events.begin()+10,capped_duplicates.begin(),capped_duplicates.end());
check(normalize_legacy_shootout(legacy_capped)&&legacy_capped==capped,"complete legacy sources also recover clamped shoot-out scores");
auto uncertain=original;uncertain.events.back().player=7;
const auto uncertain_before=uncertain;
check(!normalize_legacy_shootout(uncertain) && uncertain==uncertain_before, "disagreeing legacy sources remain unchanged");
uncertain=original;uncertain.events.pop_back();
const auto partial=uncertain;
check(!normalize_legacy_shootout(uncertain) && uncertain==partial, "partial legacy series is not guessed");
}
AlmanacSeason sample() {
AlmanacSeason s;
s.season = 3, s.start_year = 2029, s.display_start_year = 2026;
@@ -102,7 +186,48 @@ std::string encode_version_2(const AlmanacSeason& s) {
}
} // namespace
const cli::Json& field(const cli::Json& j,const char* key) { return j.object()->at(key); }
void check_shootout_projection() {
auto match=recorded_shootout(true,true);
auto season=sample();season.season=0;season.start_year=2026;season.matches={match};
// Valid historical player references let the same read prove that penalty
// takers do not acquire normal goals in the statistics projection.
for(auto& e:season.matches[0].events)e.player=e.side;
auto& legacy=season.matches[0];
legacy.home_goals=5;legacy.away_goals=4;legacy.home_shootout=legacy.away_shootout=0;
std::vector<AlmanacEvent> duplicate;
for(const auto& e:legacy.events)if(e.kind==AlmanacEventKind::ShootoutKick){auto copy=e;copy.kind=e.detail?AlmanacEventKind::Goal:AlmanacEventKind::Miss;copy.detail=0;duplicate.push_back(copy);}
legacy.events.insert(legacy.events.begin()+2,duplicate.begin(),duplicate.end());
ModernState g;g.season=0;g.display_start_year=2026;g.almanac.current=season;
const auto bytes=encode_almanac_season(season);
const auto read=[&]{return query_almanac(g,cli::Json::Object{{"kind","match"},{"id","match:2026-0"}});};
const auto result=read();const auto& scores=field(field(result,"match"),"result");
check(*field(scores,"final").array()->at(0).integer()==1 && *field(scores,"penalties").array()->at(0).integer()==4,
"legacy running-season query separates scores");
const auto stats=query_statistics(g,cli::Json::Object{{"kind","player_stats"},{"competition","cup:1"}});
const auto* players=field(stats,"players").array();
check(players&&players->size()==2&&*field(players->at(0),"goals").integer()==1&&*field(players->at(1),"goals").integer()==1,
"shoot-out hits are excluded from old-season scorer statistics");
check(encode_almanac_season(g.almanac.current)==bytes,"query normalization leaves running-season bytes unchanged");
g.almanac.current=AlmanacSeason{};g.almanac.finished={{"history/season-2026.bin",bytes}};
const auto finished=read();
check(cli::dump_json(finished)==cli::dump_json(result)&&g.almanac.finished[0].second==bytes,
"finished-season query repairs view without rewriting history bytes");
g.almanac.finished.clear();g.almanac.current=season;
auto& m=g.almanac.current.matches[0];m=match;m.type=12;m.events[1].minute=30;
const auto indoor=read();const auto& ir=field(field(indoor,"match"),"result");
check(!std::get<bool>(field(ir,"extra_played").value)&&std::holds_alternative<std::nullptr_t>(field(ir,"extra").value)&&std::get<bool>(field(ir,"penalties_played").value),
"indoor shoot-out does not invent extra time");
m=match;m.events[0].minute=91;
const auto ambiguous=read();const auto& ar=field(field(ambiguous,"match"),"result");
check(std::holds_alternative<std::nullptr_t>(field(ar,"regulation").value)&&std::holds_alternative<std::nullptr_t>(field(ar,"extra").value),
"overlapping stoppage/extra-time goal minute has unknown partial score");
}
int main() {
check_shootout_recording();
check_shootout_projection();
const AlmanacSeason s = sample();
const std::string bytes = encode_almanac_season(s);
const AlmanacSeason back = decode_almanac_season(bytes);