From d419943e86fc08084562304d869cfeb69931e838 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 25 Oct 2026 12:48:00 +0100 Subject: [PATCH] Almanac format 3: matches column by column, line-ups against the previous one Each match field of a season is stored as its own column and every player of a line-up as his slot in the club's previous line-up. Deflate then finds the repeats: a half season shrinks from 294 to 218 KB in the save. Versions 1 and 2 are still read; tests cover a hand-built version 2 season and a reordered line-up with a new player. Co-Authored-By: Claude Opus 5.5 --- docs/new_engine/almanach.md | 4 +- src/modern/game/almanac.cpp | 187 +++++++++++++++++++++++++-------- tests/modern_almanac_tests.cpp | 52 +++++++++ 3 files changed, 199 insertions(+), 44 deletions(-) diff --git a/docs/new_engine/almanach.md b/docs/new_engine/almanach.md index 76294b3..435704c 100644 --- a/docs/new_engine/almanach.md +++ b/docs/new_engine/almanach.md @@ -47,7 +47,7 @@ Pro Saison zusätzlich: - Jede Saison hat ihre eigenen Tabellen (Ligen, Vereine, Spieler, Manager, Schiedsrichter) und ist damit für sich lesbar. - Abgeschlossene Saisons liegen im Spielstand als `history/season-.bin` und werden nie wieder geschrieben; die laufende als `history/current.bin`. Beide stehen in `state/integrity.json`. -- Binärformat Version 2 (Little Endian): `OBAL`, u16 Version, Saisonkopf, Tabellen (Ligen, Vereine, Spieler, Manager, Schiedsrichter), Spiele, Abschlusstabellen, Sieger. Version 1 (ohne Abschlusstabellen und Sieger) wird weiter gelesen. Text: u16 Länge + UTF-8. Einzelheiten in `encode_almanac_season`. +- Binärformat Version 3 (Little Endian): `OBAL`, u16 Version, Saisonkopf, Tabellen (Ligen, Vereine, Spieler, Manager, Schiedsrichter), Spiele, Abschlusstabellen, Sieger. Die Spiele stehen spaltenweise (jedes Feld aller Spiele als eigener Block, Daten als Abstände, Spielerverweise in Low- und High-Bytes); eine Aufstellung nennt jeden Spieler über seinen Platz in der vorigen Aufstellung desselben Vereins. Versionen 1 (ohne Abschlusstabellen und Sieger) und 2 (eine Zeile je Spiel) werden weiter gelesen; abgeschlossene Saisons bleiben in der Version, in der sie geschrieben wurden. Text: u16 Länge + UTF-8. Einzelheiten in `encode_almanac_season`. - Größe: etwa 0,5 MB pro Saison im (komprimierten) Spielstand, mit allen Ereignissen und Aufstellungen. Ein Spielstand nach einer Saison ist ~700 KB statt ~210 KB. ## Für die UI (Codex) @@ -70,4 +70,4 @@ keine F10-/OBS-Dateien und ändert keine Engine- oder Spielstanddaten. - Kader für die übrigen Länder- und Amateurteams (die Engine lässt sie ohne Spieler antreten), damit auch deren Aufstellungen und Torschützen aufgezeichnet werden. - Behoben (25.10.2026): Die Spiele der deutschen Nationalmannschaft fehlten ganz, weil ihre Team-Nummer (357) zugleich „kein Team“ ist und als Platzhalter aussortiert wurde. - Projektionen für die Almanach-Oberfläche. -- Bessere Kompression der abgeschlossenen Saisons (heute schnellste ZIP-Stufe). +- Kompression: Version 3 spart gegenüber Version 2 rund ein Viertel (eine halbe Saison: Almanach 294 → 218 KB, Spielstand 501 → 432 KB). Den Rest belegen vor allem die Ereignisse; eine stärkere ZIP-Stufe brächte nur etwa 10 %. diff --git a/src/modern/game/almanac.cpp b/src/modern/game/almanac.cpp index 60c0a72..462539e 100644 --- a/src/modern/game/almanac.cpp +++ b/src/modern/game/almanac.cpp @@ -2,6 +2,7 @@ #include "modern/game/almanac.hpp" #include +#include #include #include #include @@ -22,7 +23,7 @@ namespace { constexpr size_t kManagerRecord = 0xB0C; // Version 2 adds the final tables and the winners; version 1 seasons are still read. -constexpr uint32_t kVersion = 2; +constexpr uint32_t kVersion = 3; std::string utf8(std::string_view original) { return almanac_utf8(original); } @@ -130,6 +131,141 @@ struct Reader { } }; +// Version 3 stores the matches column by column: every field of all matches stands together +// (dates as differences, clubs, scores, line-up sizes, the low and the high bytes of the player +// references, event kinds, minutes ...), so deflate finds the repeats a row per match hides. +// A line-up names each player by his slot in the same club's previous line-up (0xff: a player +// not in it, followed by his reference); a club fielding the same squad writes 0, 1, 2 ... +enum MatchColumn { + kDateStep, kFixture, kClubs, kManagers, kScore, kAttendance, kWeather, kLineupSize, kLineupSlot, kLineupLow, + kLineupHigh, kEventCount, kEventKind, kEventMinute, kEventSide, kEventPlayerLow, kEventPlayerHigh, + kEventOtherLow, kEventOtherHigh, kMatchColumns +}; + +void encode_matches(Writer& w, const std::vector& matches) { + std::array c; + std::map> previous; // the last line-up of every club + uint32_t date = 0; + for (const AlmanacMatch& m : matches) { + c[kDateStep].u32(m.date - date), date = m.date; + c[kFixture].u8(m.type), c[kFixture].u8(m.league), c[kFixture].u8(m.stage), c[kFixture].u8(m.round); + c[kClubs].u16(m.home), c[kClubs].u16(m.away); + c[kManagers].u8(uint8_t(m.home_manager)), c[kManagers].u8(uint8_t(m.away_manager)); + c[kScore].u8(m.home_goals), c[kScore].u8(m.away_goals), c[kScore].u8(m.home_shootout), c[kScore].u8(m.away_shootout); + c[kScore].u8(m.decided), c[kScore].u8(m.referee); + c[kAttendance].u32(m.attendance); + c[kWeather].u8(uint8_t(m.temperature)), c[kWeather].u8(m.rain); + for (int s = 0; s < 2; ++s) { + const auto& side = m.lineup[size_t(s)]; + if (side.size() > 0xff) throw FormatError("almanac: line-up too long"); + c[kLineupSize].u8(uint32_t(side.size())); + // each player as his slot in the club's previous line-up, or 0xff and the reference + auto& before = previous[s ? m.away : m.home]; + for (uint16_t p : side) { + const auto at = std::find(before.begin(), before.end(), p); + const size_t slot = size_t(at - before.begin()); + if (at != before.end() && slot < 0xff) { + c[kLineupSlot].u8(uint32_t(slot)); + } else { + c[kLineupSlot].u8(0xff), c[kLineupLow].u8(p), c[kLineupHigh].u8(p >> 8); + } + } + if (!side.empty()) before = side; + } + if (m.events.size() > 0xffff) throw FormatError("almanac: too many events in a match"); + c[kEventCount].u16(uint32_t(m.events.size())); + for (const AlmanacEvent& e : m.events) { + c[kEventKind].u8(uint32_t(e.kind)), c[kEventMinute].u8(e.minute); + c[kEventSide].u8(e.side), c[kEventSide].u8(e.detail); + c[kEventPlayerLow].u8(e.player), c[kEventPlayerHigh].u8(e.player >> 8); + c[kEventOtherLow].u8(e.other), c[kEventOtherHigh].u8(e.other >> 8); + } + } + w.u32(uint32_t(matches.size())); + for (const Writer& col : c) w.u32(uint32_t(col.out.size())), w.out += col.out; +} + +AlmanacMatch read_match_row(Reader& r) { // versions 1 and 2: one row per match + AlmanacMatch m; + m.date = r.u32(); + m.type = uint8_t(r.u8()), m.league = uint8_t(r.u8()), m.stage = uint8_t(r.u8()), m.round = uint8_t(r.u8()); + m.home = uint16_t(r.u16()), m.away = uint16_t(r.u16()); + m.home_manager = int8_t(r.u8()), m.away_manager = int8_t(r.u8()); + m.home_goals = uint8_t(r.u8()), m.away_goals = uint8_t(r.u8()); + m.home_shootout = uint8_t(r.u8()), m.away_shootout = uint8_t(r.u8()); + m.decided = uint8_t(r.u8()), m.referee = uint8_t(r.u8()); + m.attendance = r.u32(); + m.temperature = int8_t(r.u8()), m.rain = uint8_t(r.u8()); + for (auto& side : m.lineup) + for (uint32_t k = r.u8(); k > 0; --k) side.push_back(uint16_t(r.u16())); + for (uint32_t k = r.u16(); k > 0; --k) { + AlmanacEvent e; + e.kind = AlmanacEventKind(r.u8()); + e.minute = uint8_t(r.u8()), e.side = uint8_t(r.u8()), e.detail = uint8_t(r.u8()); + e.player = uint16_t(r.u16()), e.other = uint16_t(r.u16()); + m.events.push_back(e); + } + return m; +} + +std::vector decode_match_columns(Reader& r) { + const uint32_t count = r.u32(); + std::array data; + for (auto& col : data) { + const size_t n = r.u32(); + if (r.at + n > r.in.size()) throw FormatError("almanac: truncated season"); + col = r.in.substr(r.at, n), r.at += n; + } + std::vector c; + for (const auto& col : data) c.push_back(Reader{col}); + std::vector out; + std::map> previous; // the last line-up of every club + uint32_t date = 0; + for (uint32_t n = 0; n < count; ++n) { + AlmanacMatch m; + m.date = date += c[kDateStep].u32(); + m.type = uint8_t(c[kFixture].u8()), m.league = uint8_t(c[kFixture].u8()); + m.stage = uint8_t(c[kFixture].u8()), m.round = uint8_t(c[kFixture].u8()); + m.home = uint16_t(c[kClubs].u16()), m.away = uint16_t(c[kClubs].u16()); + m.home_manager = int8_t(c[kManagers].u8()), m.away_manager = int8_t(c[kManagers].u8()); + m.home_goals = uint8_t(c[kScore].u8()), m.away_goals = uint8_t(c[kScore].u8()); + m.home_shootout = uint8_t(c[kScore].u8()), m.away_shootout = uint8_t(c[kScore].u8()); + m.decided = uint8_t(c[kScore].u8()), m.referee = uint8_t(c[kScore].u8()); + m.attendance = c[kAttendance].u32(); + m.temperature = int8_t(c[kWeather].u8()), m.rain = uint8_t(c[kWeather].u8()); + for (int s = 0; s < 2; ++s) { + auto& side = m.lineup[size_t(s)]; + auto& before = previous[s ? m.away : m.home]; + for (uint32_t k = c[kLineupSize].u8(); k > 0; --k) { + const uint32_t slot = c[kLineupSlot].u8(); + if (slot != 0xff) { + if (slot >= before.size()) throw FormatError("almanac: line-up slot outside the previous line-up"); + side.push_back(before[slot]); + } else { + const uint32_t low = c[kLineupLow].u8(); + side.push_back(uint16_t(low | c[kLineupHigh].u8() << 8)); + } + } + if (!side.empty()) before = side; + } + for (uint32_t k = c[kEventCount].u16(); k > 0; --k) { + AlmanacEvent e; + e.kind = AlmanacEventKind(c[kEventKind].u8()); + e.minute = uint8_t(c[kEventMinute].u8()); + e.side = uint8_t(c[kEventSide].u8()), e.detail = uint8_t(c[kEventSide].u8()); + const uint32_t player = c[kEventPlayerLow].u8(); + e.player = uint16_t(player | c[kEventPlayerHigh].u8() << 8); + const uint32_t other = c[kEventOtherLow].u8(); + e.other = uint16_t(other | c[kEventOtherHigh].u8() << 8); + m.events.push_back(e); + } + out.push_back(std::move(m)); + } + for (const Reader& col : c) + if (col.at != col.in.size()) throw FormatError("almanac: trailing bytes in a match column"); + return out; +} + } // namespace uint16_t AlmanacSeason::club(const AlmanacClub& c) { @@ -211,26 +347,7 @@ std::string encode_almanac_season(const AlmanacSeason& s) { for (const auto& m : s.managers) w.u8(uint32_t(uint8_t(int8_t(m.index)))), w.str(m.name); w.u16(uint32_t(s.referees.size())); for (const auto& r : s.referees) w.str(r); - w.u32(uint32_t(s.matches.size())); - for (const AlmanacMatch& m : s.matches) { - w.u32(m.date); - w.u8(m.type), w.u8(m.league), w.u8(m.stage), w.u8(m.round); - w.u16(m.home), w.u16(m.away); - w.u8(uint8_t(m.home_manager)), w.u8(uint8_t(m.away_manager)); - w.u8(m.home_goals), w.u8(m.away_goals), w.u8(m.home_shootout), w.u8(m.away_shootout); - w.u8(m.decided), w.u8(m.referee); - w.u32(m.attendance); - w.u8(uint8_t(m.temperature)), w.u8(m.rain); - for (const auto& side : m.lineup) { - if (side.size() > 0xff) throw FormatError("almanac: line-up too long"); - w.u8(uint32_t(side.size())); - for (uint16_t p : side) w.u16(p); - } - if (m.events.size() > 0xffff) throw FormatError("almanac: too many events in a match"); - w.u16(uint32_t(m.events.size())); - for (const AlmanacEvent& e : m.events) - w.u8(uint32_t(e.kind)), w.u8(e.minute), w.u8(e.side), w.u8(e.detail), w.u16(e.player), w.u16(e.other); - } + encode_matches(w, s.matches); w.u8(uint32_t(s.tables.size())); for (const AlmanacTable& t : s.tables) { w.u8(t.league), w.u8(uint32_t(t.rows.size())); @@ -273,29 +390,15 @@ AlmanacSeason decode_almanac_season(const std::string& bytes) { s.managers.push_back(std::move(m)); } for (uint32_t n = r.u16(); n > 0; --n) s.referees.push_back(r.str()); - for (uint32_t n = r.u32(); n > 0; --n) { - AlmanacMatch m; - m.date = r.u32(); - m.type = uint8_t(r.u8()), m.league = uint8_t(r.u8()), m.stage = uint8_t(r.u8()), m.round = uint8_t(r.u8()); - m.home = uint16_t(r.u16()), m.away = uint16_t(r.u16()); - m.home_manager = int8_t(r.u8()), m.away_manager = int8_t(r.u8()); - m.home_goals = uint8_t(r.u8()), m.away_goals = uint8_t(r.u8()); - m.home_shootout = uint8_t(r.u8()), m.away_shootout = uint8_t(r.u8()); - m.decided = uint8_t(r.u8()), m.referee = uint8_t(r.u8()); - m.attendance = r.u32(); - m.temperature = int8_t(r.u8()), m.rain = uint8_t(r.u8()); - for (auto& side : m.lineup) - for (uint32_t k = r.u8(); k > 0; --k) side.push_back(uint16_t(r.u16())); - for (uint32_t k = r.u16(); k > 0; --k) { - AlmanacEvent e; - e.kind = AlmanacEventKind(r.u8()); - e.minute = uint8_t(r.u8()), e.side = uint8_t(r.u8()), e.detail = uint8_t(r.u8()); - e.player = uint16_t(r.u16()), e.other = uint16_t(r.u16()); + if (version >= 3) { + s.matches = decode_match_columns(r); + } else { + for (uint32_t n = r.u32(); n > 0; --n) s.matches.push_back(read_match_row(r)); + } + for (const AlmanacMatch& m : s.matches) { + for (const AlmanacEvent& e : m.events) if (uint8_t(e.kind) > uint8_t(AlmanacEventKind::Miss)) throw FormatError("almanac: unknown event kind"); - m.events.push_back(e); - } if (m.home >= s.clubs.size() || m.away >= s.clubs.size()) throw FormatError("almanac: unknown club in a match"); - s.matches.push_back(std::move(m)); } if (version >= 2) { for (uint32_t n = r.u8(); n > 0; --n) { diff --git a/tests/modern_almanac_tests.cpp b/tests/modern_almanac_tests.cpp index 60089b3..2c33b16 100644 --- a/tests/modern_almanac_tests.cpp +++ b/tests/modern_almanac_tests.cpp @@ -41,6 +41,13 @@ AlmanacSeason sample() { m.events.push_back({AlmanacEventKind::Substitution, 80, 0, 0, b, a}); m.events.push_back({AlmanacEventKind::ShootoutKick, 121, 1, 1, a, kAlmanacNone}); s.matches.push_back(m); + // the same club again: two players from its previous line-up in a new order and a new one + // (version 3 names them by their slot in the previous line-up) + const uint16_t c = s.player({"player:3", "Carl", "Neu", "nation:at"}); + AlmanacMatch next; + next.date = 407, next.type = 0, next.league = 0, next.round = 3, next.home = away, next.away = home; + next.lineup[1] = {b, c, a}; + s.matches.push_back(next); AlmanacTable t; t.league = 0; t.rows.push_back({home, 1, 34, 25, 5, 4, 80, 30, 77}); @@ -50,6 +57,49 @@ AlmanacSeason sample() { s.winners.push_back({12, 0xff, 4, 1, 310, away, 0xffff}); return s; } + +// A season in the row format of version 2 (one row per match), as older saves hold it. +std::string encode_version_2(const AlmanacSeason& s) { + std::string o = "OBAL"; + auto u8 = [&](uint32_t v) { o.push_back(char(v & 0xff)); }; + auto u16 = [&](uint32_t v) { u8(v), u8(v >> 8); }; + auto u32 = [&](uint32_t v) { u16(v), u16(v >> 16); }; + auto str = [&](const std::string& t) { u16(uint32_t(t.size())), o += t; }; + u16(2), u16(uint32_t(int16_t(s.season))), u16(uint32_t(s.start_year)), u16(uint32_t(s.display_start_year)); + u16(uint32_t(s.leagues.size())); + for (const auto& l : s.leagues) u8(uint32_t(l.index)), str(l.id), str(l.name); + u16(uint32_t(s.clubs.size())); + for (const auto& c : s.clubs) str(c.id), str(c.name); + u32(uint32_t(s.players.size())); + for (const auto& p : s.players) str(p.id), str(p.first), str(p.last), str(p.nation); + u8(uint32_t(s.managers.size())); + for (const auto& m : s.managers) u8(uint32_t(uint8_t(int8_t(m.index)))), str(m.name); + u16(uint32_t(s.referees.size())); + for (const auto& r : s.referees) str(r); + u32(uint32_t(s.matches.size())); + for (const AlmanacMatch& m : s.matches) { + u32(m.date), u8(m.type), u8(m.league), u8(m.stage), u8(m.round), u16(m.home), u16(m.away); + u8(uint8_t(m.home_manager)), u8(uint8_t(m.away_manager)); + u8(m.home_goals), u8(m.away_goals), u8(m.home_shootout), u8(m.away_shootout), u8(m.decided), u8(m.referee); + u32(m.attendance), u8(uint8_t(m.temperature)), u8(m.rain); + for (const auto& side : m.lineup) { + u8(uint32_t(side.size())); + for (uint16_t p : side) u16(p); + } + u16(uint32_t(m.events.size())); + for (const AlmanacEvent& e : m.events) u8(uint32_t(e.kind)), u8(e.minute), u8(e.side), u8(e.detail), u16(e.player), u16(e.other); + } + u8(uint32_t(s.tables.size())); + for (const AlmanacTable& t : s.tables) { + u8(t.league), u8(uint32_t(t.rows.size())); + for (const AlmanacStanding& r : t.rows) + u16(r.club), u8(r.rank), u8(r.played), u8(r.won), u8(r.drawn), u8(r.lost), u8(r.goals_for), u8(r.goals_against), + u16(uint32_t(uint16_t(r.points))); + } + u16(uint32_t(s.winners.size())); + for (const AlmanacWinner& x : s.winners) u8(x.type), u8(x.league), u8(x.stage), u8(x.detail), u32(x.date), u16(x.winner), u16(x.runner_up); + return o; +} } // namespace int main() { @@ -58,6 +108,8 @@ int main() { const AlmanacSeason back = decode_almanac_season(bytes); check(back == s, "season round trip"); check(encode_almanac_season(back) == bytes, "stable encoding"); + check(decode_almanac_season(encode_version_2(s)) == s, "version 2 saves still read"); + check(bytes.size() > 6 && bytes[4] == 3 && bytes[5] == 0, "written as version 3"); AlmanacSeason grown = back; // the lookups survive decoding: no duplicate entries check(grown.club({"club:sued", "FC Südstadt"}) == 0 && grown.clubs.size() == 2, "club lookup after decode");