(self, raw: bytes, players=None)
| 67 | DRAW_JS = DRAW_JS |
| 68 | |
| 69 | def parse(self, raw: bytes, players=None) -> ReplayData: |
| 70 | rows = [json.loads(line) for line in raw.decode().splitlines() if line.strip()] |
| 71 | # per-turn board states (dedupe: keep the last frame seen for each turn) |
| 72 | by_turn = {} |
| 73 | for r in rows: |
| 74 | if isinstance(r, dict) and "board" in r and "turn" in r: |
| 75 | by_turn[r["turn"]] = r["board"] |
| 76 | turns = sorted(by_turn) |
| 77 | result = next((r for r in reversed(rows) if isinstance(r, dict) and "winnerName" in r), {}) |
| 78 | if not turns: |
| 79 | return ReplayData(w=0, h=0, frames=[], winner=result.get("winnerName"), draw=result.get("isDraw", False)) |
| 80 | |
| 81 | # stable color per snake name (prefer the snake's own color from the log if present) |
| 82 | names, colors = [], {} |
| 83 | for t in turns: |
| 84 | for s in by_turn[t]["snakes"]: |
| 85 | if s["name"] not in names: |
| 86 | names.append(s["name"]) |
| 87 | for i, nm in enumerate(names): |
| 88 | colors[nm] = PALETTE[i % len(PALETTE)] |
| 89 | for t in turns: |
| 90 | for s in by_turn[t]["snakes"]: |
| 91 | if s.get("color"): |
| 92 | colors[s["name"]] = s["color"] |
| 93 | |
| 94 | b0 = by_turn[turns[0]] |
| 95 | frames = [] |
| 96 | for t in turns: |
| 97 | b = by_turn[t] |
| 98 | frames.append( |
| 99 | { |
| 100 | "turn": t, |
| 101 | "food": [[c["x"], c["y"]] for c in b.get("food", [])], |
| 102 | "hazards": [[c["x"], c["y"]] for c in b.get("hazards", [])], |
| 103 | "snakes": [ |
| 104 | { |
| 105 | "name": s["name"], |
| 106 | "health": s.get("health", 0), |
| 107 | "body": [[c["x"], c["y"]] for c in s["body"]], |
| 108 | } |
| 109 | for s in b["snakes"] |
| 110 | ], |
| 111 | } |
| 112 | ) |
| 113 | return ReplayData( |
| 114 | w=b0["width"], |
| 115 | h=b0["height"], |
| 116 | frames=frames, |
| 117 | winner=result.get("winnerName"), |
| 118 | draw=result.get("isDraw", False), |
| 119 | extra={"colors": colors}, |
| 120 | ) |
nothing calls this directly
no test coverage detected