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

+75 -12
View File
@@ -488,6 +488,51 @@ void new_player_identity(ModernState& g, int player) {
g.world_player_id[size_t(player)] = "player:gen-" + std::to_string(++g.almanac.generated_players);
}
bool normalize_legacy_shootout(AlmanacMatch& match) {
if (match.decided != 2 || match.home_shootout || match.away_shootout) return false;
std::vector<const AlmanacEvent*> log, shown;
int normal[2] = {}, penalties[2] = {}, taken[2] = {};
for (const auto& e : match.events) {
if (e.side > 1) return false;
if ((e.kind == AlmanacEventKind::Goal || e.kind == AlmanacEventKind::Miss) && e.minute == 121) {
if (e.other != kAlmanacNone || e.detail != 0) return false;
log.push_back(&e);
} else if (e.kind == AlmanacEventKind::Goal) ++normal[e.side];
if (e.kind == AlmanacEventKind::ShootoutKick) shown.push_back(&e);
}
if (log.empty() || log.size() != shown.size()) return false;
for (size_t i = 0; i < log.size(); ++i) {
const auto& l = *log[i];
const auto& s = *shown[i];
const bool goal = l.kind == AlmanacEventKind::Goal;
if (s.minute != 121 || s.side != l.side || s.player != l.player ||
s.detail != (goal ? 1 : 0) || l.side != (log[0]->side ^ (i & 1))) return false;
++taken[l.side];
if (goal) ++penalties[l.side];
bool ended = false;
if (taken[0] <= 5 && taken[1] <= 5)
ended = penalties[0] > penalties[1] + 5 - taken[1] || penalties[1] > penalties[0] + 5 - taken[0];
else if (taken[0] == taken[1]) ended = penalties[0] != penalties[1];
if (ended != (i + 1 == log.size())) return false;
}
if (normal[0] > 31 || normal[1] > 31 || penalties[0] > 12 || penalties[1] > 12) return false;
// Verify the old CupTie2 packing, including its total/side limits. The
// complete event streams still retain the actual goals before that clamp.
int packed_home = normal[0] + penalties[0], packed_away = normal[1] + penalties[1];
while (packed_home + packed_away > 29) --packed_home, --packed_away;
packed_home = std::clamp(packed_home, 0, 15);
packed_away = std::clamp(packed_away, 0, 15);
if (packed_home != match.home_goals || packed_away != match.away_goals) return false;
match.home_goals = uint8_t(normal[0]);
match.away_goals = uint8_t(normal[1]);
match.home_shootout = uint8_t(penalties[0]);
match.away_shootout = uint8_t(penalties[1]);
std::erase_if(match.events, [](const AlmanacEvent& e) {
return e.minute == 121 && (e.kind == AlmanacEventKind::Goal || e.kind == AlmanacEventKind::Miss);
});
return true;
}
// --- recording -------------------------------------------------------------------------
AlmanacRecorder::AlmanacRecorder(ModernState& g, const std::vector<MatchGroup>& groups) : g_(g), groups_(groups) {
@@ -619,6 +664,7 @@ void AlmanacRecorder::after_minute(int minute) {
void AlmanacRecorder::finish(const std::vector<ShowEvent>& shown) {
std::vector<std::vector<AlmanacEvent>> events(running_.size());
std::vector<bool> logged_shootout(running_.size(), false);
std::map<std::pair<size_t, int>, size_t> by_match;
for (size_t i = 0; i < running_.size(); ++i) by_match[{running_[i].group, running_[i].match}] = i;
auto ref = [&](int player) { return player >= 0 && player < 0x0fff ? player_ref(player) : kAlmanacNone; };
@@ -653,9 +699,20 @@ void AlmanacRecorder::finish(const std::vector<ShowEvent>& shown) {
e.player = ref(player);
switch (code) {
case 0: case 1:
e.kind = code == 0 ? AlmanacEventKind::Goal : AlmanacEventKind::Miss;
e.detail = uint8_t(detail);
e.other = ref(assistant);
// CupTie2 uses the same log codes for its kicks. Minute
// 121 is reserved for the shoot-out; the normal extra-time
// driver ends at that minute before playing another chance.
if (value == 121 && groups_[gi].results.empty() &&
size_t(running_[it->second].match) < groups_[gi].cup_results.size() &&
(groups_[gi].cup_results.at(size_t(running_[it->second].match)) >> 10 & 3) == 2) {
e.kind = AlmanacEventKind::ShootoutKick;
e.detail = code == 0 ? 1 : 0;
logged_shootout[it->second] = true;
} else {
e.kind = code == 0 ? AlmanacEventKind::Goal : AlmanacEventKind::Miss;
e.detail = uint8_t(detail);
e.other = ref(assistant);
}
break;
case 3: e.kind = AlmanacEventKind::YellowCard; break;
case 4: e.kind = AlmanacEventKind::SecondYellow; break;
@@ -666,11 +723,14 @@ void AlmanacRecorder::finish(const std::vector<ShowEvent>& shown) {
events[it->second].push_back(e);
}
}
// The kicks of a shoot-out (the log has none): the show events of the match engine.
// A presentation-only caller may supply just show events. The complete
// match log is authoritative when present, so visible matches never double
// their kicks and background matches do not depend on presentation events.
for (const ShowEvent& s : shown) {
if (s.what != Show::PenaltyKick) continue;
const auto it = by_match.find({size_t(s.group), int(s.match)});
if (it == by_match.end()) continue;
if (logged_shootout[it->second]) continue;
AlmanacEvent e;
e.kind = AlmanacEventKind::ShootoutKick;
e.minute = uint8_t(std::clamp<int>(s.minute, 0, 255));
@@ -698,17 +758,20 @@ void AlmanacRecorder::finish(const std::vector<ShowEvent>& shown) {
AlmanacMatch& rec = r.record;
rec.events = std::move(events[i]);
if (group.results.empty()) rec.decided = uint8_t(group.cup_results[size_t(m)] >> 10 & 3);
// After a shoot-out the stored score includes its goals (CupTie2): the goals of the
// match are those of the log, the rest were scored in the shoot-out.
// CupTie2's packed score adds (and may clamp) shoot-out goals. Archive
// the actual two event streams separately, without changing that score.
if (rec.decided == 2) {
int goals[2] = {0, 0};
for (const AlmanacEvent& e : rec.events)
int goals[2] = {0, 0}, penalties[2] = {0, 0};
for (const AlmanacEvent& e : rec.events) if (e.side < 2) {
if (e.kind == AlmanacEventKind::Goal) ++goals[e.side];
rec.home_shootout = uint8_t(std::max(0, home - goals[0]));
rec.away_shootout = uint8_t(std::max(0, away - goals[1]));
if (e.kind == AlmanacEventKind::ShootoutKick && e.detail == 1) ++penalties[e.side];
}
home = goals[0], away = goals[1];
rec.home_shootout = uint8_t(penalties[0]);
rec.away_shootout = uint8_t(penalties[1]);
}
rec.home_goals = uint8_t(std::max(0, home - rec.home_shootout));
rec.away_goals = uint8_t(std::max(0, away - rec.away_shootout));
rec.home_goals = uint8_t(home);
rec.away_goals = uint8_t(away);
if (size_t(m) < group.spectators.size()) rec.attendance = group.spectators[size_t(m)];
season.matches.push_back(std::move(rec));
}
+5
View File
@@ -179,6 +179,11 @@ std::string encode_almanac_season(const AlmanacSeason& season);
// Throws FormatError when the bytes are not an almanac season of a known version.
AlmanacSeason decode_almanac_season(const std::string& bytes);
// Repair the old double recording of shoot-out kicks in a presentation copy.
// Only a complete, matching log/show pair with consistent totals qualifies.
// Uncertain records and the saved season bytes remain untouched.
bool normalize_legacy_shootout(AlmanacMatch& match);
// A player index now belongs to a new person (retirement or youth player): gives it a new
// stable identity so the almanac does not merge the two careers.
void new_player_identity(ModernState& g, int player);