(self, raw: bytes, players=None)
| 125 | DRAW_JS = DRAW_JS |
| 126 | |
| 127 | def parse(self, raw: bytes, players=None) -> ReplayData: |
| 128 | data = json.loads(raw.decode()) |
| 129 | width = int(data.get("width", 0)) |
| 130 | height = int(data.get("height", 0)) |
| 131 | frames_raw = data.get("frames", []) |
| 132 | player_order = data.get("player_order", []) |
| 133 | |
| 134 | frames = [] |
| 135 | for fr in frames_raw: |
| 136 | units = [] |
| 137 | for u in fr.get("units", []): |
| 138 | coords = u.get("coordinates", [0, 0]) |
| 139 | units.append( |
| 140 | { |
| 141 | "unit_id": u.get("unit_id"), |
| 142 | "agent_id": u.get("agent_id"), |
| 143 | "x": coords[0], |
| 144 | "y": coords[1], |
| 145 | "hp": u.get("hp", 0), |
| 146 | } |
| 147 | ) |
| 148 | entities = [] |
| 149 | for e in fr.get("entities", []): |
| 150 | coords = e.get("coordinates", [0, 0]) |
| 151 | ent = {"type": e.get("type"), "coordinates": [coords[0], coords[1]]} |
| 152 | if "timer" in e: |
| 153 | ent["timer"] = e["timer"] |
| 154 | if "owner" in e: |
| 155 | ent["owner"] = e["owner"] |
| 156 | if "ttl" in e: |
| 157 | ent["ttl"] = e["ttl"] |
| 158 | entities.append(ent) |
| 159 | frames.append({"turn": fr.get("tick"), "tick": fr.get("tick"), "units": units, "entities": entities}) |
| 160 | |
| 161 | # First player_order id -> Blue, second -> Red. Fall back to display names. |
| 162 | blue_id = player_order[0] if len(player_order) > 0 else "Blue" |
| 163 | red_id = player_order[1] if len(player_order) > 1 else "Red" |
| 164 | names = {"Blue": blue_id, "Red": red_id} |
| 165 | |
| 166 | winner_raw = data.get("winner") |
| 167 | if winner_raw == blue_id: |
| 168 | winner, draw = names["Blue"], False |
| 169 | elif winner_raw == red_id: |
| 170 | winner, draw = names["Red"], False |
| 171 | else: # "TIE" or unknown |
| 172 | winner, draw = None, True |
| 173 | |
| 174 | return ReplayData( |
| 175 | w=width, |
| 176 | h=height, |
| 177 | frames=frames, |
| 178 | winner=winner, |
| 179 | draw=draw, |
| 180 | extra={ |
| 181 | "names": names, |
| 182 | "colors": TEAM_COLORS, |
| 183 | "agents": [blue_id, red_id], |
| 184 | "maxhp": START_HP, |
nothing calls this directly
no test coverage detected