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
+
from dataclasses import dataclass
|
| 6 |
+
from typing import Dict, List, Optional, Union
|
| 7 |
+
from enum import Enum
|
| 8 |
+
|
| 9 |
+
@dataclass
|
| 10 |
+
class Position:
|
| 11 |
+
"""2D position on the game map"""
|
| 12 |
+
x: float
|
| 13 |
+
z: float
|
| 14 |
+
|
| 15 |
+
class AIType(Enum):
|
| 16 |
+
"""Type of AI entity"""
|
| 17 |
+
UNKNOWN = "Unknown"
|
| 18 |
+
HERO = "Hero"
|
| 19 |
+
MINION = "Minion"
|
| 20 |
+
TURRET = "Turret"
|
| 21 |
+
NEUTRAL = "Neutral"
|
| 22 |
+
|
| 23 |
+
class ReplicationInternalData:
|
| 24 |
+
"""Internal replication data that can be int or float"""
|
| 25 |
+
def __init__(self, value: Union[int, float]):
|
| 26 |
+
self.value = value
|
| 27 |
+
self.is_int = isinstance(value, int)
|
| 28 |
+
self.is_float = isinstance(value, float)
|
| 29 |
+
|
| 30 |
+
@dataclass
|
| 31 |
+
class CreateHero:
|
| 32 |
+
"""Packet for hero/champion creation"""
|
| 33 |
+
time: float
|
| 34 |
+
net_id: int
|
| 35 |
+
name: str
|
| 36 |
+
champion: str
|
| 37 |
+
|
| 38 |
+
@dataclass
|
| 39 |
+
class WaypointGroup:
|
| 40 |
+
"""Packet containing movement waypoints for entities"""
|
| 41 |
+
time: float
|
| 42 |
+
waypoints: Dict[int, List[Position]]
|
| 43 |
+
|
| 44 |
+
@dataclass
|
| 45 |
+
class WaypointGroupWithSpeed:
|
| 46 |
+
"""Packet containing movement waypoints with speed data"""
|
| 47 |
+
time: float
|
| 48 |
+
waypoints: Dict[int, List[Position]]
|
| 49 |
+
|
| 50 |
+
@dataclass
|
| 51 |
+
class EnterFog:
|
| 52 |
+
"""Packet for entity entering fog of war"""
|
| 53 |
+
time: float
|
| 54 |
+
net_id: int
|
| 55 |
+
|
| 56 |
+
@dataclass
|
| 57 |
+
class LeaveFog:
|
| 58 |
+
"""Packet for entity leaving fog of war"""
|
| 59 |
+
time: float
|
| 60 |
+
net_id: int
|
| 61 |
+
|
| 62 |
+
@dataclass
|
| 63 |
+
class UnitApplyDamage:
|
| 64 |
+
"""Packet for damage application between units"""
|
| 65 |
+
time: float
|
| 66 |
+
source_net_id: int
|
| 67 |
+
target_net_id: int
|
| 68 |
+
damage: float
|
| 69 |
+
|
| 70 |
+
@dataclass
|
| 71 |
+
class DoSetCooldown:
|
| 72 |
+
"""Packet for ability cooldown updates"""
|
| 73 |
+
time: float
|
| 74 |
+
net_id: int
|
| 75 |
+
slot: int
|
| 76 |
+
cooldown: float
|
| 77 |
+
display_cooldown: float
|
| 78 |
+
|
| 79 |
+
@dataclass
|
| 80 |
+
class BasicAttackPos:
|
| 81 |
+
"""Packet for basic attack with positional data"""
|
| 82 |
+
time: float
|
| 83 |
+
source_net_id: int
|
| 84 |
+
target_net_id: int
|
| 85 |
+
source_position: Position
|
| 86 |
+
target_position: Position
|
| 87 |
+
slot: int
|
| 88 |
+
caster_net_id: int
|
| 89 |
+
spell_chain_owner_net_id: int
|
| 90 |
+
spell_hash: int
|
| 91 |
+
spell_name: str
|
| 92 |
+
level: int
|
| 93 |
+
target_end_position: Position
|
| 94 |
+
target_net_ids: List[int]
|
| 95 |
+
windup_time: float
|
| 96 |
+
cooldown: float
|
| 97 |
+
mana_cost: float
|
| 98 |
+
|
| 99 |
+
@dataclass
|
| 100 |
+
class CastSpellAns:
|
| 101 |
+
"""Packet for spell casting"""
|
| 102 |
+
time: float
|
| 103 |
+
caster_net_id: int
|
| 104 |
+
spell_chain_owner_net_id: int
|
| 105 |
+
spell_hash: int
|
| 106 |
+
spell_name: str
|
| 107 |
+
level: int
|
| 108 |
+
source_position: Position
|
| 109 |
+
target_position: Position
|
| 110 |
+
target_end_position: Position
|
| 111 |
+
target_net_ids: List[int]
|
| 112 |
+
windup_time: float
|
| 113 |
+
cooldown: float
|
| 114 |
+
mana_cost: float
|
| 115 |
+
slot: int
|
| 116 |
+
|
| 117 |
+
@dataclass
|
| 118 |
+
class BarrackSpawnUnit:
|
| 119 |
+
"""Packet for minion spawning from barracks"""
|
| 120 |
+
time: float
|
| 121 |
+
barrack_net_id: int
|
| 122 |
+
minion_net_id: int
|
| 123 |
+
wave_count: int
|
| 124 |
+
minion_type: int # 0xc0 = cannon, 0x60 = ranged, 0 = melee
|
| 125 |
+
minion_level: int
|
| 126 |
+
|
| 127 |
+
@dataclass
|
| 128 |
+
class ReplicationData:
|
| 129 |
+
"""Replication data for game state synchronization"""
|
| 130 |
+
primary_index: int
|
| 131 |
+
secondary_index: int
|
| 132 |
+
name: str
|
| 133 |
+
data: ReplicationInternalData
|
| 134 |
+
|
| 135 |
+
@dataclass
|
| 136 |
+
class Replication:
|
| 137 |
+
"""Packet containing replication data for multiple entities"""
|
| 138 |
+
time: float
|
| 139 |
+
net_id_to_replication_datas: Dict[int, ReplicationData]
|
| 140 |
+
|
| 141 |
+
@dataclass
|
| 142 |
+
class SpawnMinion:
|
| 143 |
+
"""Packet for minion spawning"""
|
| 144 |
+
time: float
|
| 145 |
+
net_id: int
|
| 146 |
+
position1: Position
|
| 147 |
+
position2: Position
|
| 148 |
+
name: str
|
| 149 |
+
skin_name: str
|
| 150 |
+
level: int
|
| 151 |
+
targetable_on_client: int
|
| 152 |
+
targetable_to_team_flags_on_client: int
|
| 153 |
+
bot: bool
|
| 154 |
+
|
| 155 |
+
@dataclass
|
| 156 |
+
class CreateNeutral:
|
| 157 |
+
"""Packet for neutral monster creation"""
|
| 158 |
+
time: float
|
| 159 |
+
net_id: int
|
| 160 |
+
position1: Position
|
| 161 |
+
position2: Position
|
| 162 |
+
name: str
|
| 163 |
+
skin_name: str
|
| 164 |
+
level: int
|
| 165 |
+
direction: Position
|
| 166 |
+
camp_id: int
|
| 167 |
+
neutral_type: int
|
| 168 |
+
|
| 169 |
+
@dataclass
|
| 170 |
+
class CreateTurret:
|
| 171 |
+
"""Packet for turret creation"""
|
| 172 |
+
time: float
|
| 173 |
+
net_id: int
|
| 174 |
+
owner_net_id: int
|
| 175 |
+
name: str
|
| 176 |
+
|
| 177 |
+
@dataclass
|
| 178 |
+
class NPCDieMapView:
|
| 179 |
+
"""Packet for NPC death (map view)"""
|
| 180 |
+
time: float
|
| 181 |
+
killer_net_id: int
|
| 182 |
+
killed_net_id: int
|
| 183 |
+
|
| 184 |
+
@dataclass
|
| 185 |
+
class NPCDieMapViewBroadcast:
|
| 186 |
+
"""Packet for NPC death broadcast"""
|
| 187 |
+
time: float
|
| 188 |
+
killer_net_id: int
|
| 189 |
+
killed_net_id: int
|
| 190 |
+
|
| 191 |
+
@dataclass
|
| 192 |
+
class HeroDie:
|
| 193 |
+
"""Packet for hero/champion death"""
|
| 194 |
+
time: float
|
| 195 |
+
net_id: int
|
| 196 |
+
|
| 197 |
+
@dataclass
|
| 198 |
+
class BuyItem:
|
| 199 |
+
"""Packet for item purchase"""
|
| 200 |
+
time: float
|
| 201 |
+
net_id: int
|
| 202 |
+
slot: int
|
| 203 |
+
item_id: int
|
| 204 |
+
item_name: str
|
| 205 |
+
items_in_slot: int
|
| 206 |
+
spell_charges: int
|
| 207 |
+
item_gold: float
|
| 208 |
+
entity_gold_after_change: float
|
| 209 |
+
|
| 210 |
+
@dataclass
|
| 211 |
+
class RemoveItem:
|
| 212 |
+
"""Packet for item removal"""
|
| 213 |
+
time: float
|
| 214 |
+
net_id: int
|
| 215 |
+
slot: int
|
| 216 |
+
items_in_slot: int
|
| 217 |
+
entity_gold_after_change: float
|
| 218 |
+
|
| 219 |
+
@dataclass
|
| 220 |
+
class SwapItem:
|
| 221 |
+
"""Packet for item slot swapping"""
|
| 222 |
+
time: float
|
| 223 |
+
net_id: int
|
| 224 |
+
source_slot: int
|
| 225 |
+
target_slot: int
|
| 226 |
+
|
| 227 |
+
@dataclass
|
| 228 |
+
class UseItem:
|
| 229 |
+
"""Packet for item usage"""
|
| 230 |
+
time: float
|
| 231 |
+
net_id: int
|
| 232 |
+
slot: int
|
| 233 |
+
items_in_slot: int
|
| 234 |
+
spell_charges: int
|
| 235 |
+
|
| 236 |
+
# Union type for all possible packets
|
| 237 |
+
PacketType = Union[
|
| 238 |
+
CreateHero,
|
| 239 |
+
WaypointGroup,
|
| 240 |
+
WaypointGroupWithSpeed,
|
| 241 |
+
EnterFog,
|
| 242 |
+
LeaveFog,
|
| 243 |
+
UnitApplyDamage,
|
| 244 |
+
DoSetCooldown,
|
| 245 |
+
BasicAttackPos,
|
| 246 |
+
CastSpellAns,
|
| 247 |
+
BarrackSpawnUnit,
|
| 248 |
+
Replication,
|
| 249 |
+
SpawnMinion,
|
| 250 |
+
CreateNeutral,
|
| 251 |
+
CreateTurret,
|
| 252 |
+
NPCDieMapView,
|
| 253 |
+
NPCDieMapViewBroadcast,
|
| 254 |
+
HeroDie,
|
| 255 |
+
BuyItem,
|
| 256 |
+
RemoveItem,
|
| 257 |
+
SwapItem,
|
| 258 |
+
UseItem
|
| 259 |
+
]
|
| 260 |
+
|
| 261 |
+
def parse_packet_from_dict(packet_dict: dict) -> PacketType:
|
| 262 |
+
"""
|
| 263 |
+
Parse a packet dictionary into the appropriate packet dataclass.
|
| 264 |
+
|
| 265 |
+
Args:
|
| 266 |
+
packet_dict: Dictionary containing packet data with 'packet_type' field
|
| 267 |
+
|
| 268 |
+
Returns:
|
| 269 |
+
Appropriate packet dataclass instance
|
| 270 |
+
|
| 271 |
+
Raises:
|
| 272 |
+
ValueError: If packet_type is unknown
|
| 273 |
+
"""
|
| 274 |
+
packet_type = packet_dict.get('packet_type')
|
| 275 |
+
|
| 276 |
+
# Remove packet_type from dict for dataclass creation
|
| 277 |
+
data = {k: v for k, v in packet_dict.items() if k != 'packet_type'}
|
| 278 |
+
|
| 279 |
+
# Convert position dictionaries to Position objects
|
| 280 |
+
def convert_positions(obj):
|
| 281 |
+
if isinstance(obj, dict):
|
| 282 |
+
if 'x' in obj and 'z' in obj and len(obj) == 2:
|
| 283 |
+
return Position(x=obj['x'], z=obj['z'])
|
| 284 |
+
return {k: convert_positions(v) for k, v in obj.items()}
|
| 285 |
+
elif isinstance(obj, list):
|
| 286 |
+
return [convert_positions(item) for item in obj]
|
| 287 |
+
return obj
|
| 288 |
+
|
| 289 |
+
data = convert_positions(data)
|
| 290 |
+
|
| 291 |
+
# Map packet types to dataclasses
|
| 292 |
+
packet_classes = {
|
| 293 |
+
'CreateHero': CreateHero,
|
| 294 |
+
'WaypointGroup': WaypointGroup,
|
| 295 |
+
'WaypointGroupWithSpeed': WaypointGroupWithSpeed,
|
| 296 |
+
'EnterFog': EnterFog,
|
| 297 |
+
'LeaveFog': LeaveFog,
|
| 298 |
+
'UnitApplyDamage': UnitApplyDamage,
|
| 299 |
+
'DoSetCooldown': DoSetCooldown,
|
| 300 |
+
'BasicAttackPos': BasicAttackPos,
|
| 301 |
+
'CastSpellAns': CastSpellAns,
|
| 302 |
+
'BarrackSpawnUnit': BarrackSpawnUnit,
|
| 303 |
+
'Replication': Replication,
|
| 304 |
+
'SpawnMinion': SpawnMinion,
|
| 305 |
+
'CreateNeutral': CreateNeutral,
|
| 306 |
+
'CreateTurret': CreateTurret,
|
| 307 |
+
'NPCDieMapView': NPCDieMapView,
|
| 308 |
+
'NPCDieMapViewBroadcast': NPCDieMapViewBroadcast,
|
| 309 |
+
'HeroDie': HeroDie,
|
| 310 |
+
'BuyItem': BuyItem,
|
| 311 |
+
'RemoveItem': RemoveItem,
|
| 312 |
+
'SwapItem': SwapItem,
|
| 313 |
+
'UseItem': UseItem
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
if packet_type not in packet_classes:
|
| 317 |
+
raise ValueError(f"Unknown packet type: {packet_type}")
|
| 318 |
+
|
| 319 |
+
return packet_classes[packet_type](**data)
|