File size: 7,443 Bytes
04ff6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
League of Legends Replay Packet Definitions
"""

from dataclasses import dataclass
from typing import Dict, List, Optional, Union
from enum import Enum

@dataclass
class Position:
    """2D position on the game map"""
    x: float
    z: float

class AIType(Enum):
    """Type of AI entity"""
    UNKNOWN = "Unknown"
    HERO = "Hero" 
    MINION = "Minion"
    TURRET = "Turret"
    NEUTRAL = "Neutral"

class ReplicationInternalData:
    """Internal replication data that can be int or float"""
    def __init__(self, value: Union[int, float]):
        self.value = value
        self.is_int = isinstance(value, int)
        self.is_float = isinstance(value, float)

@dataclass
class CreateHero:
    """Packet for hero/champion creation"""
    time: float
    net_id: int
    name: str
    champion: str

@dataclass 
class WaypointGroup:
    """Packet containing movement waypoints for entities"""
    time: float
    waypoints: Dict[int, List[Position]]

@dataclass
class WaypointGroupWithSpeed:
    """Packet containing movement waypoints with speed data"""
    time: float
    waypoints: Dict[int, List[Position]]

@dataclass
class EnterFog:
    """Packet for entity entering fog of war"""
    time: float
    net_id: int

@dataclass
class LeaveFog:
    """Packet for entity leaving fog of war"""
    time: float
    net_id: int

@dataclass
class UnitApplyDamage:
    """Packet for damage application between units"""
    time: float
    source_net_id: int
    target_net_id: int
    damage: float

@dataclass
class DoSetCooldown:
    """Packet for ability cooldown updates"""
    time: float
    net_id: int
    slot: int
    cooldown: float
    display_cooldown: float

@dataclass
class BasicAttackPos:
    """Packet for basic attack with positional data"""
    time: float
    source_net_id: int
    target_net_id: int
    source_position: Position
    target_position: Position
    slot: int
    caster_net_id: int
    spell_chain_owner_net_id: int
    spell_hash: int
    spell_name: str
    level: int
    target_end_position: Position
    target_net_ids: List[int]
    windup_time: float
    cooldown: float
    mana_cost: float

@dataclass
class CastSpellAns:
    """Packet for spell casting"""
    time: float
    caster_net_id: int
    spell_chain_owner_net_id: int
    spell_hash: int
    spell_name: str
    level: int
    source_position: Position
    target_position: Position
    target_end_position: Position
    target_net_ids: List[int]
    windup_time: float
    cooldown: float
    mana_cost: float
    slot: int

@dataclass
class BarrackSpawnUnit:
    """Packet for minion spawning from barracks"""
    time: float
    barrack_net_id: int
    minion_net_id: int
    wave_count: int
    minion_type: int  # 0xc0 = cannon, 0x60 = ranged, 0 = melee
    minion_level: int

@dataclass
class ReplicationData:
    """Replication data for game state synchronization"""
    primary_index: int
    secondary_index: int
    name: str
    data: ReplicationInternalData

@dataclass
class Replication:
    """Packet containing replication data for multiple entities"""
    time: float
    net_id_to_replication_datas: Dict[int, ReplicationData]

@dataclass
class SpawnMinion:
    """Packet for minion spawning"""
    time: float
    net_id: int
    position1: Position
    position2: Position
    name: str
    skin_name: str
    level: int
    targetable_on_client: int
    targetable_to_team_flags_on_client: int
    bot: bool

@dataclass
class CreateNeutral:
    """Packet for neutral monster creation"""
    time: float
    net_id: int
    position1: Position
    position2: Position
    name: str
    skin_name: str
    level: int
    direction: Position
    camp_id: int
    neutral_type: int

@dataclass
class CreateTurret:
    """Packet for turret creation"""
    time: float
    net_id: int
    owner_net_id: int
    name: str

@dataclass
class NPCDieMapView:
    """Packet for NPC death (map view)"""
    time: float
    killer_net_id: int
    killed_net_id: int

@dataclass
class NPCDieMapViewBroadcast:
    """Packet for NPC death broadcast"""
    time: float
    killer_net_id: int
    killed_net_id: int

@dataclass
class HeroDie:
    """Packet for hero/champion death"""
    time: float
    net_id: int

@dataclass
class BuyItem:
    """Packet for item purchase"""
    time: float
    net_id: int
    slot: int
    item_id: int
    item_name: str
    items_in_slot: int
    spell_charges: int
    item_gold: float
    entity_gold_after_change: float

@dataclass
class RemoveItem:
    """Packet for item removal"""
    time: float
    net_id: int
    slot: int
    items_in_slot: int
    entity_gold_after_change: float

@dataclass
class SwapItem:
    """Packet for item slot swapping"""
    time: float
    net_id: int
    source_slot: int
    target_slot: int

@dataclass
class UseItem:
    """Packet for item usage"""
    time: float
    net_id: int
    slot: int
    items_in_slot: int
    spell_charges: int

# Union type for all possible packets
PacketType = Union[
    CreateHero,
    WaypointGroup, 
    WaypointGroupWithSpeed,
    EnterFog,
    LeaveFog,
    UnitApplyDamage,
    DoSetCooldown,
    BasicAttackPos,
    CastSpellAns,
    BarrackSpawnUnit,
    Replication,
    SpawnMinion,
    CreateNeutral,
    CreateTurret,
    NPCDieMapView,
    NPCDieMapViewBroadcast,
    HeroDie,
    BuyItem,
    RemoveItem,
    SwapItem,
    UseItem
]

def parse_packet_from_dict(packet_dict: dict) -> PacketType:
    """
    Parse a packet dictionary into the appropriate packet dataclass.
    
    Args:
        packet_dict: Dictionary containing packet data with 'packet_type' field
        
    Returns:
        Appropriate packet dataclass instance
        
    Raises:
        ValueError: If packet_type is unknown
    """
    packet_type = packet_dict.get('packet_type')
    
    # Remove packet_type from dict for dataclass creation
    data = {k: v for k, v in packet_dict.items() if k != 'packet_type'}
    
    # Convert position dictionaries to Position objects
    def convert_positions(obj):
        if isinstance(obj, dict):
            if 'x' in obj and 'z' in obj and len(obj) == 2:
                return Position(x=obj['x'], z=obj['z'])
            return {k: convert_positions(v) for k, v in obj.items()}
        elif isinstance(obj, list):
            return [convert_positions(item) for item in obj]
        return obj
    
    data = convert_positions(data)
    
    # Map packet types to dataclasses
    packet_classes = {
        'CreateHero': CreateHero,
        'WaypointGroup': WaypointGroup,
        'WaypointGroupWithSpeed': WaypointGroupWithSpeed,
        'EnterFog': EnterFog,
        'LeaveFog': LeaveFog,
        'UnitApplyDamage': UnitApplyDamage,
        'DoSetCooldown': DoSetCooldown,
        'BasicAttackPos': BasicAttackPos,
        'CastSpellAns': CastSpellAns,
        'BarrackSpawnUnit': BarrackSpawnUnit,
        'Replication': Replication,
        'SpawnMinion': SpawnMinion,
        'CreateNeutral': CreateNeutral,
        'CreateTurret': CreateTurret,
        'NPCDieMapView': NPCDieMapView,
        'NPCDieMapViewBroadcast': NPCDieMapViewBroadcast,
        'HeroDie': HeroDie,
        'BuyItem': BuyItem,
        'RemoveItem': RemoveItem,
        'SwapItem': SwapItem,
        'UseItem': UseItem
    }
    
    if packet_type not in packet_classes:
        raise ValueError(f"Unknown packet type: {packet_type}")
    
    return packet_classes[packet_type](**data)