This commit is contained in:
latypov committed 2026-11-23 23:55:37 +07:00
commit 2ca7a264b3
53 files changed
+7113

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
"""Verify and inspect a finished JSON export without a server or bot."""
import argparse
import gzip
import json
from collections import Counter
from pathlib import Path
def verify(doc):
if doc.get("schema") != 1:
raise ValueError("Неизвестная версия экспорта")
events = doc["events"]
expected = Counter(doc["catalog"]["cards"])
if not events or events[0]["kind"] != "start" or events[-1]["kind"] != "winner":
raise ValueError("Нет начала или завершения партии")
previous_turn = 1
initial = events[0]["state"]
for seq, event in enumerate(events, 1):
s = event["state"]
if (
event["seq"] != seq
or event["turn"] != s["turn"]
or not previous_turn <= s["turn"] <= previous_turn + 1
):
raise ValueError(f"Нарушена последовательность на шаге {seq}")
previous_turn = s["turn"]
cards = Counter(s["deck"]) + Counter(s["discard"])
for hand in s["hands"]:
cards.update(hand)
cards.update(c for c in (s["weather"], s.get("transit")) if c)
if cards != expected:
raise ValueError(f"Состав колоды нарушен на шаге {seq}")
positions = [
p["pos"] for p in s["planes"].values() if p["pos"] not in ("H", "F")
]
if (
len(positions) != len(set(positions))
or s["planes"].keys() != initial["planes"].keys()
or s["catalog"] != doc["rules"]
or s["colors"] != initial["colors"]
):
raise ValueError(f"Некорректное поле или каталог на шаге {seq}")
winner = events[-1]["data"]["seat"]
if s["winner"] != winner or any(
p["pos"] != "F" for p in s["planes"].values() if p["owner"] == winner
):
raise ValueError("Результат не совпадает с состоянием флота")
return len(events), s["turn"], winner
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("file", type=Path)
parser.add_argument(
"--step", type=int, help="Вывести полный снимок указанного события"
)
args = parser.parse_args()
opener = gzip.open if args.file.suffix == ".gz" else open
try:
with opener(args.file, "rt", encoding="utf8") as stream:
doc = json.load(stream)
n, t, w = verify(doc)
print(
f"Проверено событий: {n}. Ходов: {t}. Победитель: {doc['events'][-1]['state']['colors'][w]}."
)
if args.step is not None:
if not 1 <= args.step <= n:
raise ValueError(f"Номер события должен быть от 1 до {n}")
print(
json.dumps(doc["events"][args.step - 1], ensure_ascii=False, indent=2)
)
except (ValueError, KeyError, IndexError, TypeError, OSError) as exc:
parser.exit(1, f"Ошибка проверки: {exc}\n")
if __name__ == "__main__":
main()