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 <noreply@anthropic.com>
This commit is contained in:
1 parent
ddaebec183
commit
d419943e86
3 files changed
+199
-44
No files matched your search
+145
-42
@@ -2,6 +2,7 @@
|
||||
#include "modern/game/almanac.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <iterator>
|
||||
#include <string_view>
|
||||
@@ -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<AlmanacMatch>& matches) {
|
||||
std::array<Writer, kMatchColumns> c;
|
||||
std::map<uint16_t, std::vector<uint16_t>> 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<AlmanacMatch> decode_match_columns(Reader& r) {
|
||||
const uint32_t count = r.u32();
|
||||
std::array<std::string, kMatchColumns> 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<Reader> c;
|
||||
for (const auto& col : data) c.push_back(Reader{col});
|
||||
std::vector<AlmanacMatch> out;
|
||||
std::map<uint16_t, std::vector<uint16_t>> 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) {
|
||||
|
||||
Reference in new issue
Block a user