Fix Almanac shoot-out recording and verified legacy views (by Codex)
This commit is contained in:
1 parent
ddd4447563
commit
f9e8719b2d
11 files changed
+309
-28
No files matched your search
@@ -45,6 +45,10 @@ std::vector<AlmanacSeason> seasons_of(const ModernState& g) {
|
||||
std::vector<AlmanacSeason> out;
|
||||
for (const auto& [name, bytes] : g.almanac.finished) out.push_back(decode_almanac_season(bytes));
|
||||
if (g.almanac.current.season >= 0) out.push_back(g.almanac.current);
|
||||
// Work on query copies only: finished season bytes and the loaded career
|
||||
// remain immutable, including older saves affected by double-counted kicks.
|
||||
for (auto& season : out)
|
||||
for (auto& match : season.matches) modern::normalize_legacy_shootout(match);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -309,7 +313,18 @@ J almanac_match(const ModernState& g, const AlmanacSeason& s, size_t index) {
|
||||
int regulation[2] = {0, 0};
|
||||
for (const auto& e : m.events)
|
||||
if (e.kind == AlmanacEventKind::Goal && e.minute <= 90 && e.side < 2) ++regulation[e.side];
|
||||
const bool extra = m.decided >= 1, shootout = m.decided == 2;
|
||||
const bool shootout = m.decided == 2;
|
||||
const bool indoor = m.type == 12 || m.type == 13; // indoor cup / masters: 2 x 20, then penalties
|
||||
const bool extra = m.decided >= 1 && !indoor;
|
||||
int goals[2] = {};
|
||||
bool ambiguous_stoppage = false;
|
||||
for (const auto& e : m.events) if (e.kind == AlmanacEventKind::Goal && e.side < 2) {
|
||||
++goals[e.side];
|
||||
// Both regular stoppage time and the first extra-time minutes use
|
||||
// 91..93. F10 has no phase tag; do not invent a 90-minute score there.
|
||||
if (e.minute > 90 && e.minute <= 93) ambiguous_stoppage = true;
|
||||
}
|
||||
const bool split_known = goals[0] == m.home_goals && goals[1] == m.away_goals && !ambiguous_stoppage;
|
||||
J lineups = O{};
|
||||
for (int side = 0; side < 2; ++side) {
|
||||
const auto& l = m.lineup[size_t(side)];
|
||||
@@ -328,8 +343,8 @@ J almanac_match(const ModernState& g, const AlmanacSeason& s, size_t index) {
|
||||
return O{{"id", match_id(s, index)}, {"date", iso_date(m.date, s.display_start_year)}, {"season", season_label(s.start_year)},
|
||||
{"competition", O{{"id", c.id}, {"name", c.name}, {"type", type}}}, {"round", int(m.round)},
|
||||
{"home", almanac_side(g, s, m.home, m.home_manager)}, {"away", almanac_side(g, s, m.away, m.away_manager)},
|
||||
{"result", O{{"regulation", extra ? J(A{J(regulation[0]), J(regulation[1])}) : J(A{J(int(m.home_goals)), J(int(m.away_goals))})},
|
||||
{"extra", extra ? J(A{J(int(m.home_goals) - regulation[0]), J(int(m.away_goals) - regulation[1])}) : J(nullptr)},
|
||||
{"result", O{{"regulation", extra ? (split_known ? J(A{J(regulation[0]), J(regulation[1])}) : J(nullptr)) : J(A{J(int(m.home_goals)), J(int(m.away_goals))})},
|
||||
{"extra", extra && split_known ? J(A{J(int(m.home_goals) - regulation[0]), J(int(m.away_goals) - regulation[1])}) : J(nullptr)},
|
||||
{"penalties", shootout ? J(A{J(int(m.home_shootout)), J(int(m.away_shootout))}) : J(nullptr)},
|
||||
{"final", A{J(int(m.home_goals)), J(int(m.away_goals))}}, {"extra_played", extra}, {"penalties_played", shootout}}},
|
||||
{"referee", m.referee > 0 && m.referee < s.referees.size() ? J(s.referees[m.referee]) : J(nullptr)},
|
||||
|
||||
+75
-12
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user