wows_replays 0.32.0

A parser for World of Warships replay files
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
use crate::PResult;
use crate::packet2::EntityMethodPacket;
use crate::packet2::Packet;
use crate::packet2::PacketType;
use crate::types::AccountId;
use crate::types::AvatarId;
use crate::types::EntityId;
use crate::types::GameParamId;
use crate::types::NormalizedPos;
use crate::types::PlaneId;
use crate::types::ShotId;
use crate::types::WorldPos;
use crate::types::WorldPos2D;
use kinded::Kinded;
use pickled::Value;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::convert::TryInto;
use std::iter::FromIterator;
use tracing::error;
use winnow::Parser;
use winnow::binary::le_f32;
use winnow::binary::le_u8;
use winnow::binary::le_u16;
use winnow::binary::le_u64;
use wowsunpack::data::Version;
use wowsunpack::game_constants::DEFAULT_BATTLE_CONSTANTS;
use wowsunpack::game_constants::DEFAULT_COMMON_CONSTANTS;
use wowsunpack::game_constants::DEFAULT_SHIPS_CONSTANTS;
use wowsunpack::game_params::convert::pickle_to_json;
use wowsunpack::game_params::types::BigWorldDistance;
use wowsunpack::game_types::DamageStatCategory;
use wowsunpack::game_types::DamageStatWeapon;
use wowsunpack::rpc::typedefs::ArgValue;
use wowsunpack::unpack_rpc_args;

use super::super::analyzer::Analyzer;

pub struct DecoderBuilder {
    silent: bool,
    no_meta: bool,
    path: Option<String>,
    game_constants: Option<&'static crate::game_constants::GameConstants>,
}

impl DecoderBuilder {
    pub fn new(silent: bool, no_meta: bool, output: Option<&str>) -> Self {
        Self { silent, no_meta, path: output.map(|s| s.to_string()), game_constants: None }
    }

    /// Override the game constants used for decoding (consumable IDs, battle stages, etc.).
    pub fn game_constants(mut self, gc: &'static crate::game_constants::GameConstants) -> Self {
        self.game_constants = Some(gc);
        self
    }

    pub fn build(self, meta: &crate::ReplayMeta) -> Box<dyn Analyzer> {
        let version = Version::from_client_exe(&meta.clientVersionFromExe);
        let gc = self.game_constants.unwrap_or(&*crate::game_constants::DEFAULT_GAME_CONSTANTS);
        let mut decoder = Decoder {
            silent: self.silent,
            output: self
                .path
                .as_ref()
                .map(|path| Box::new(std::fs::File::create(path).unwrap()) as Box<dyn std::io::Write>),
            packet_decoder: PacketDecoder::builder()
                .version(version)
                .battle_constants(gc.battle())
                .common_constants(gc.common())
                .ships_constants(gc.ships())
                .build(),
        };
        if !self.no_meta {
            decoder.write(&serde_json::to_string(&meta).unwrap());
        }
        Box::new(decoder)
    }
}
// Types that are also re-exported from the parent module (decoder/mod.rs).
// Imported here (non-pub) for internal use within this file.
use wowsunpack::game_types::CameraMode;
use wowsunpack::game_types::CollisionType;
use wowsunpack::game_types::Consumable;
use wowsunpack::game_types::ConsumableUsageParams;
use wowsunpack::game_types::DeathCause;
use wowsunpack::game_types::FinishType;
use wowsunpack::game_types::Ribbon;
use wowsunpack::game_types::ShellHitType;
use wowsunpack::game_types::VoiceLine;
use wowsunpack::recognized::Recognized;

/// Properties only present for human players (not bots)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HumanPlayerProperties {
    /// Their avatar entity ID in the game
    pub(crate) avatar_id: AvatarId,
    /// Division ID
    pub(crate) prebattle_id: i64,
    /// Has the client loaded into the game
    pub(crate) is_client_loaded: bool,
    /// Is the client connected into the game
    pub(crate) is_connected: bool,
}

/// Contains the information describing a player
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerStateData {
    /// The username of this player
    pub(crate) username: String,
    /// The player's clan
    pub(crate) clan: String,
    /// The player's clan DB id
    pub(crate) clan_id: i64,
    /// The color of the player's clan tag as an RGB integer
    pub(crate) clan_color: i64,
    /// The player's DB ID (unique player ID)
    pub(crate) db_id: AccountId,
    /// The realm this player belongs to
    pub(crate) realm: Option<String>,
    /// Their meta ID in the game (account-level identifier)
    pub(crate) meta_ship_id: AccountId,
    /// This player's entity created by a CreateEntity packet
    pub(crate) entity_id: EntityId,
    /// Which team they're on.
    pub(crate) team_id: i64,
    /// Their starting health
    pub(crate) max_health: i64,
    /// ????
    pub(crate) is_abuser: bool,
    /// Has hidden stats
    pub(crate) is_hidden: bool,
    /// Is this player a bot (AI-controlled)
    pub(crate) is_bot: bool,
    /// Properties only present for human players
    pub(crate) human_properties: Option<HumanPlayerProperties>,

    /// This is a raw dump (with the values converted to strings) of every key for the player.
    // TODO: Replace String with the actual pickle value (which is cleanly serializable)
    #[serde(skip_deserializing)]
    pub(crate) raw: HashMap<i64, String>,
    #[serde(skip_deserializing)]
    pub(crate) raw_with_names: HashMap<&'static str, serde_json::Value>,
}

impl PlayerStateData {
    // Key string constants for player data fields
    pub(crate) const KEY_ACCOUNT_DBID: &'static str = "accountDBID";
    pub(crate) const KEY_ANTI_ABUSE_ENABLED: &'static str = "antiAbuseEnabled";
    pub(crate) const KEY_AVATAR_ID: &'static str = "avatarId";
    pub(crate) const KEY_CAMOUFLAGE_INFO: &'static str = "camouflageInfo";
    pub(crate) const KEY_CLAN_COLOR: &'static str = "clanColor";
    pub(crate) const KEY_CLAN_ID: &'static str = "clanID";
    pub(crate) const KEY_CLAN_TAG: &'static str = "clanTag";
    pub(crate) const KEY_CREW_PARAMS: &'static str = "crewParams";
    pub(crate) const KEY_DOG_TAG: &'static str = "dogTag";
    pub(crate) const KEY_FRAGS_COUNT: &'static str = "fragsCount";
    pub(crate) const KEY_FRIENDLY_FIRE_ENABLED: &'static str = "friendlyFireEnabled";
    pub(crate) const KEY_ID: &'static str = "id";
    pub(crate) const KEY_INVITATIONS_ENABLED: &'static str = "invitationsEnabled";
    pub(crate) const KEY_IS_ABUSER: &'static str = "isAbuser";
    pub(crate) const KEY_IS_ALIVE: &'static str = "isAlive";
    pub(crate) const KEY_IS_BOT: &'static str = "isBot";
    pub(crate) const KEY_IS_CLIENT_LOADED: &'static str = "isClientLoaded";
    pub(crate) const KEY_IS_CONNECTED: &'static str = "isConnected";
    pub(crate) const KEY_IS_HIDDEN: &'static str = "isHidden";
    pub(crate) const KEY_IS_LEAVER: &'static str = "isLeaver";
    pub(crate) const KEY_IS_PRE_BATTLE_OWNER: &'static str = "isPreBattleOwner";
    pub(crate) const KEY_IS_T_SHOOTER: &'static str = "isTShooter";
    pub(crate) const KEY_KEY_TARGET_MARKERS: &'static str = "keyTargetMarkers";
    pub(crate) const KEY_KILLED_BUILDINGS_COUNT: &'static str = "killedBuildingsCount";
    pub(crate) const KEY_MAX_HEALTH: &'static str = "maxHealth";
    pub(crate) const KEY_NAME: &'static str = "name";
    pub(crate) const KEY_PLAYER_MODE: &'static str = "playerMode";
    pub(crate) const KEY_PRE_BATTLE_ID_ON_START: &'static str = "preBattleIdOnStart";
    pub(crate) const KEY_PRE_BATTLE_SIGN: &'static str = "preBattleSign";
    pub(crate) const KEY_PREBATTLE_ID: &'static str = "prebattleId";
    pub(crate) const KEY_REALM: &'static str = "realm";
    pub(crate) const KEY_SHIP_COMPONENTS: &'static str = "shipComponents";
    pub(crate) const KEY_SHIP_CONFIG_DUMP: &'static str = "shipConfigDump";
    pub(crate) const KEY_SHIP_ID: &'static str = "shipId";
    pub(crate) const KEY_SHIP_PARAMS_ID: &'static str = "shipParamsId";
    pub(crate) const KEY_SKIN_ID: &'static str = "skinId";
    pub(crate) const KEY_TEAM_ID: &'static str = "teamId";
    pub(crate) const KEY_TTK_STATUS: &'static str = "ttkStatus";

    fn convert_raw_dict(values: &HashMap<i64, Value>, version: &Version, is_bot: bool) -> HashMap<&'static str, Value> {
        let keys: HashMap<&'static str, i64> =
            if is_bot { Self::bot_key_map(version) } else { Self::player_key_map(version) };

        let mut raw_with_names = HashMap::new();
        for (k, v) in values.iter() {
            if let Some(name) = keys.iter().find_map(|(name, idx)| if *idx == *k { Some(*name) } else { None }) {
                raw_with_names.insert(name, v.clone());
            }
        }

        raw_with_names
    }

    fn player_key_map(version: &Version) -> HashMap<&'static str, i64> {
        if version.is_at_least(&Version::from_client_exe("0,12,8,0")) {
            let mut h = HashMap::new();
            h.insert(Self::KEY_ACCOUNT_DBID, 0);
            h.insert(Self::KEY_ANTI_ABUSE_ENABLED, 1);
            h.insert(Self::KEY_AVATAR_ID, 2);
            h.insert(Self::KEY_CAMOUFLAGE_INFO, 3);
            h.insert(Self::KEY_CLAN_COLOR, 4);
            h.insert(Self::KEY_CLAN_ID, 5);
            h.insert(Self::KEY_CLAN_TAG, 6);
            h.insert(Self::KEY_CREW_PARAMS, 7);
            h.insert(Self::KEY_DOG_TAG, 8);
            h.insert(Self::KEY_FRAGS_COUNT, 9);
            h.insert(Self::KEY_FRIENDLY_FIRE_ENABLED, 10);
            h.insert(Self::KEY_ID, 11);
            h.insert(Self::KEY_INVITATIONS_ENABLED, 12);
            h.insert(Self::KEY_IS_ABUSER, 13);
            h.insert(Self::KEY_IS_ALIVE, 14);
            h.insert(Self::KEY_IS_BOT, 15);
            h.insert(Self::KEY_IS_CLIENT_LOADED, 16);
            h.insert(Self::KEY_IS_CONNECTED, 17);
            h.insert(Self::KEY_IS_HIDDEN, 18);
            h.insert(Self::KEY_IS_LEAVER, 19);
            h.insert(Self::KEY_IS_PRE_BATTLE_OWNER, 20);
            h.insert(Self::KEY_IS_T_SHOOTER, 21);
            h.insert(Self::KEY_KEY_TARGET_MARKERS, 22);
            h.insert(Self::KEY_KILLED_BUILDINGS_COUNT, 23);
            h.insert(Self::KEY_MAX_HEALTH, 24);
            h.insert(Self::KEY_NAME, 25);
            h.insert(Self::KEY_PLAYER_MODE, 26);
            h.insert(Self::KEY_PRE_BATTLE_ID_ON_START, 27);
            h.insert(Self::KEY_PRE_BATTLE_SIGN, 28);
            h.insert(Self::KEY_PREBATTLE_ID, 29);
            h.insert(Self::KEY_REALM, 30);
            h.insert(Self::KEY_SHIP_COMPONENTS, 31);
            h.insert(Self::KEY_SHIP_CONFIG_DUMP, 32);
            h.insert(Self::KEY_SHIP_ID, 33);
            h.insert(Self::KEY_SHIP_PARAMS_ID, 34);
            h.insert(Self::KEY_SKIN_ID, 35);
            h.insert(Self::KEY_TEAM_ID, 36);
            h.insert(Self::KEY_TTK_STATUS, 37);
            h
        } else if version.is_at_least(&Version::from_client_exe("0,10,9,0")) {
            let mut h = HashMap::new();
            h.insert(Self::KEY_AVATAR_ID, 0x2);
            h.insert(Self::KEY_CLAN_TAG, 0x6);
            h.insert(Self::KEY_MAX_HEALTH, 0x17);
            h.insert(Self::KEY_NAME, 0x18);
            h.insert(Self::KEY_SHIP_ID, 0x20);
            h.insert(Self::KEY_SHIP_PARAMS_ID, 0x21);
            h.insert(Self::KEY_SKIN_ID, 0x22);
            h.insert(Self::KEY_TEAM_ID, 0x23);
            h
        } else if version.is_at_least(&Version::from_client_exe("0,10,7,0")) {
            let mut h = HashMap::new();
            h.insert(Self::KEY_AVATAR_ID, 0x1);
            h.insert(Self::KEY_CLAN_TAG, 0x5);
            h.insert(Self::KEY_MAX_HEALTH, 0x16);
            h.insert(Self::KEY_NAME, 0x17);
            h.insert(Self::KEY_SHIP_ID, 0x1e);
            h.insert(Self::KEY_SHIP_PARAMS_ID, 0x1f);
            h.insert(Self::KEY_SKIN_ID, 0x20);
            h.insert(Self::KEY_TEAM_ID, 0x21);
            h
        } else {
            let mut h = HashMap::new();
            h.insert(Self::KEY_AVATAR_ID, 0x1);
            h.insert(Self::KEY_CLAN_TAG, 0x5);
            h.insert(Self::KEY_MAX_HEALTH, 0x15);
            h.insert(Self::KEY_NAME, 0x16);
            h.insert(Self::KEY_SHIP_ID, 0x1d);
            h.insert(Self::KEY_SHIP_PARAMS_ID, 0x1e);
            h.insert(Self::KEY_SKIN_ID, 0x1f);
            h.insert(Self::KEY_TEAM_ID, 0x20);
            h
        }
    }

    /// Bot key mapping — bots have a different (smaller) set of fields with different indices.
    fn bot_key_map(version: &Version) -> HashMap<&'static str, i64> {
        if version.is_at_least(&Version::from_client_exe("0,12,8,0")) {
            let mut h = HashMap::new();
            h.insert(Self::KEY_ACCOUNT_DBID, 0);
            h.insert(Self::KEY_ANTI_ABUSE_ENABLED, 1);
            h.insert(Self::KEY_CAMOUFLAGE_INFO, 2);
            h.insert(Self::KEY_CLAN_COLOR, 3);
            h.insert(Self::KEY_CLAN_ID, 4);
            h.insert(Self::KEY_CLAN_TAG, 5);
            h.insert(Self::KEY_CREW_PARAMS, 6);
            h.insert(Self::KEY_DOG_TAG, 7);
            h.insert(Self::KEY_FRAGS_COUNT, 8);
            h.insert(Self::KEY_FRIENDLY_FIRE_ENABLED, 9);
            h.insert(Self::KEY_ID, 10);
            h.insert(Self::KEY_IS_ABUSER, 11);
            h.insert(Self::KEY_IS_ALIVE, 12);
            h.insert(Self::KEY_IS_BOT, 13);
            h.insert(Self::KEY_IS_HIDDEN, 14);
            h.insert(Self::KEY_IS_T_SHOOTER, 15);
            h.insert(Self::KEY_KILLED_BUILDINGS_COUNT, 16);
            h.insert(Self::KEY_KEY_TARGET_MARKERS, 17);
            h.insert(Self::KEY_MAX_HEALTH, 18);
            h.insert(Self::KEY_NAME, 19);
            h.insert(Self::KEY_REALM, 20);
            h.insert(Self::KEY_SHIP_COMPONENTS, 21);
            h.insert(Self::KEY_SHIP_CONFIG_DUMP, 22);
            h.insert(Self::KEY_SHIP_ID, 23);
            h.insert(Self::KEY_SHIP_PARAMS_ID, 24);
            h.insert(Self::KEY_SKIN_ID, 25);
            h.insert(Self::KEY_TEAM_ID, 26);
            h.insert(Self::KEY_TTK_STATUS, 27);
            h
        } else {
            // For older versions, bots weren't separately tracked or had
            // the same layout as players. Fall back to player key map.
            Self::player_key_map(version)
        }
    }

    fn from_pickle(value: &pickled::Value, version: &Version, is_bot: bool) -> Self {
        let raw_values = convert_flat_dict_to_real_dict(value);

        let mapped_values = Self::convert_raw_dict(&raw_values, version, is_bot);
        Self::from_values(raw_values, mapped_values, version)
    }

    fn from_values(
        raw_values: HashMap<i64, pickled::Value>,
        mut mapped_values: HashMap<&'static str, pickled::Value>,
        _version: &Version,
    ) -> Self {
        let username =
            mapped_values.get(Self::KEY_NAME).unwrap().string_ref().expect("name is not a string").inner().clone();

        let clan = mapped_values
            .get(Self::KEY_CLAN_TAG)
            .unwrap()
            .string_ref()
            .expect("clanTag is not a string")
            .inner()
            .clone();

        let clan_id = *mapped_values.get(Self::KEY_CLAN_ID).unwrap().i64_ref().expect("clanID is not an i64");

        let shipid = *mapped_values.get(Self::KEY_SHIP_ID).unwrap().i64_ref().expect("shipId is not an i64");
        let meta_ship_id = *mapped_values.get(Self::KEY_ID).unwrap().i64_ref().expect("id is not an i64");
        let team = *mapped_values.get(Self::KEY_TEAM_ID).unwrap().i64_ref().expect("teamId is not an i64");
        let health = *mapped_values.get(Self::KEY_MAX_HEALTH).unwrap().i64_ref().expect("maxHealth is not an i64");

        let realm = mapped_values.get(Self::KEY_REALM).unwrap().string_ref().map(|realm| realm.inner().clone());

        let db_id =
            mapped_values.get(Self::KEY_ACCOUNT_DBID).unwrap().i64_ref().cloned().expect("accountDBID is not an i64");

        let is_abuser = mapped_values
            .get(Self::KEY_IS_ABUSER)
            .and_then(|v| v.bool_ref().cloned().or_else(|| v.i64_ref().map(|i| *i != 0)))
            .unwrap_or(false);

        let is_hidden = mapped_values
            .get(Self::KEY_IS_HIDDEN)
            .and_then(|v| v.bool_ref().cloned().or_else(|| v.i64_ref().map(|i| *i != 0)))
            .unwrap_or(false);

        let is_bot = mapped_values.get(Self::KEY_IS_BOT).and_then(|v| v.bool_ref().cloned()).unwrap_or(false);

        let clan_color =
            mapped_values.get(Self::KEY_CLAN_COLOR).unwrap().i64_ref().cloned().expect("clanColor is not an integer");

        // Human-only properties (not present for bots)
        let human_properties =
            mapped_values.get(Self::KEY_AVATAR_ID).and_then(|v| v.i64_ref().copied()).map(|avatar_id| {
                let prebattle_id =
                    mapped_values.get(Self::KEY_PREBATTLE_ID).and_then(|v| v.i64_ref().copied()).unwrap_or(0);
                let is_connected =
                    mapped_values.get(Self::KEY_IS_CONNECTED).and_then(|v| v.bool_ref().copied()).unwrap_or(false);
                let is_client_loaded =
                    mapped_values.get(Self::KEY_IS_CLIENT_LOADED).and_then(|v| v.bool_ref().copied()).unwrap_or(false);
                HumanPlayerProperties {
                    avatar_id: AvatarId::from(avatar_id as u32),
                    prebattle_id,
                    is_connected,
                    is_client_loaded,
                }
            });

        let mut raw = HashMap::new();
        for (k, v) in raw_values.iter() {
            raw.insert(*k, format!("{:?}", v));
        }

        PlayerStateData {
            username,
            clan,
            clan_id,
            clan_color,
            realm,
            db_id: AccountId::from(db_id),
            meta_ship_id: AccountId::from(meta_ship_id),
            entity_id: EntityId::from(shipid),
            team_id: team,
            max_health: health,
            is_abuser,
            is_hidden,
            is_bot,
            human_properties,
            raw,
            raw_with_names: HashMap::from_iter(mapped_values.drain().map(|(k, v)| (k, pickle_to_json(v)))),
        }
    }

    /// Updates the PlayerStateData from a dictionary of values.
    /// Only fields present in the dictionary will be updated.
    pub fn update_from_dict(&mut self, values: &HashMap<&'static str, pickled::Value>) {
        if let Some(v) = values.get(Self::KEY_AVATAR_ID)
            && let Some(id) = v.i64_ref()
            && let Some(ref mut hp) = self.human_properties
        {
            hp.avatar_id = AvatarId::from(*id);
        }
        if let Some(v) = values.get(Self::KEY_NAME)
            && let Some(s) = v.string_ref()
        {
            self.username = s.inner().clone();
        }
        if let Some(v) = values.get(Self::KEY_CLAN_TAG)
            && let Some(s) = v.string_ref()
        {
            self.clan = s.inner().clone();
        }
        if let Some(v) = values.get(Self::KEY_CLAN_ID)
            && let Some(id) = v.i64_ref()
        {
            self.clan_id = *id;
        }
        if let Some(v) = values.get(Self::KEY_CLAN_COLOR)
            && let Some(id) = v.i64_ref()
        {
            self.clan_color = *id;
        }
        if let Some(v) = values.get(Self::KEY_SHIP_ID)
            && let Some(id) = v.i64_ref()
        {
            self.entity_id = EntityId::from(*id);
        }
        if let Some(v) = values.get(Self::KEY_ID)
            && let Some(id) = v.i64_ref()
        {
            self.meta_ship_id = AccountId::from(*id);
        }
        if let Some(v) = values.get(Self::KEY_TEAM_ID)
            && let Some(id) = v.i64_ref()
        {
            self.team_id = *id;
        }
        if let Some(v) = values.get(Self::KEY_MAX_HEALTH)
            && let Some(id) = v.i64_ref()
        {
            self.max_health = *id;
        }
        if let Some(v) = values.get(Self::KEY_REALM)
            && let Some(s) = v.string_ref()
        {
            self.realm = Some(s.inner().clone());
        }
        if let Some(v) = values.get(Self::KEY_ACCOUNT_DBID)
            && let Some(id) = v.i64_ref()
        {
            self.db_id = AccountId::from(*id);
        }
        if let Some(v) = values.get(Self::KEY_PREBATTLE_ID)
            && let Some(id) = v.i64_ref()
            && let Some(ref mut hp) = self.human_properties
        {
            hp.prebattle_id = *id;
        }
        if let Some(v) = values.get(Self::KEY_IS_ABUSER)
            && let Some(b) = v.bool_ref()
        {
            self.is_abuser = *b;
        }
        if let Some(v) = values.get(Self::KEY_IS_HIDDEN)
            && let Some(b) = v.bool_ref()
        {
            self.is_hidden = *b;
        }
        if let Some(v) = values.get(Self::KEY_IS_CONNECTED)
            && let Some(b) = v.bool_ref()
            && let Some(ref mut hp) = self.human_properties
        {
            hp.is_connected = *b;
        }
        if let Some(v) = values.get(Self::KEY_IS_CLIENT_LOADED)
            && let Some(b) = v.bool_ref()
            && let Some(ref mut hp) = self.human_properties
        {
            hp.is_client_loaded = *b;
        }
        if let Some(v) = values.get(Self::KEY_IS_BOT)
            && let Some(b) = v.bool_ref()
        {
            self.is_bot = *b;
        }

        // Update raw_with_names with any new values
        for (k, v) in values.iter() {
            self.raw_with_names.insert(k, pickle_to_json(v.clone()));
        }
    }

    pub fn username(&self) -> &str {
        &self.username
    }

    pub fn clan(&self) -> &str {
        &self.clan
    }

    pub fn clan_id(&self) -> i64 {
        self.clan_id
    }

    pub fn clan_color(&self) -> i64 {
        self.clan_color
    }

    pub fn db_id(&self) -> AccountId {
        self.db_id
    }

    pub fn realm(&self) -> Option<&str> {
        self.realm.as_deref()
    }

    pub fn avatar_id(&self) -> Option<AvatarId> {
        self.human_properties.as_ref().map(|hp| hp.avatar_id)
    }

    pub fn meta_ship_id(&self) -> AccountId {
        self.meta_ship_id
    }

    pub fn entity_id(&self) -> EntityId {
        self.entity_id
    }

    pub fn team_id(&self) -> i64 {
        self.team_id
    }

    pub fn division_id(&self) -> i64 {
        self.human_properties.as_ref().map(|hp| hp.prebattle_id).unwrap_or(0)
    }

    /// Returns true if `other` is in the same division as `self` (and is not `self`).
    pub fn is_division_mate(&self, other: &PlayerStateData) -> bool {
        self.db_id() != other.db_id() && self.division_id() > 0 && other.division_id() == self.division_id()
    }

    pub fn max_health(&self) -> i64 {
        self.max_health
    }

    pub fn is_abuser(&self) -> bool {
        self.is_abuser
    }

    pub fn is_hidden(&self) -> bool {
        self.is_hidden
    }

    pub fn is_client_loaded(&self) -> bool {
        self.human_properties.as_ref().map(|hp| hp.is_client_loaded).unwrap_or_else(|| self.is_bot())
    }

    pub fn is_connected(&self) -> bool {
        self.human_properties.as_ref().map(|hp| hp.is_connected).unwrap_or_else(|| self.is_bot())
    }

    pub fn human_properties(&self) -> Option<&HumanPlayerProperties> {
        self.human_properties.as_ref()
    }

    pub fn is_bot(&self) -> bool {
        self.is_bot
    }

    /// The ship's GameParamId, if present in the decoded data.
    pub fn ship_params_id(&self) -> Option<GameParamId> {
        self.raw_with_names
            .get(Self::KEY_SHIP_PARAMS_ID)
            .and_then(|v| v.as_u64())
            .map(|id| GameParamId::from(id as u32))
    }

    pub fn raw(&self) -> &HashMap<i64, String> {
        &self.raw
    }

    pub fn raw_with_names(&self) -> &HashMap<&'static str, serde_json::Value> {
        &self.raw_with_names
    }
}

/// Converts a list of key-value pairs to a real dictionary
fn convert_flat_dict_to_real_dict(value: &Value) -> HashMap<i64, Value> {
    let mut raw_values = HashMap::new();
    if let pickled::value::Value::List(elements) = value {
        for elem in elements.inner().iter() {
            if let pickled::value::Value::Tuple(kv) = elem {
                let key = kv.inner()[0].i64_ref().expect("tuple first value was not an integer");

                raw_values.insert(*key, kv.inner()[1].clone());
            }
        }
    }

    raw_values
}

/// Indicates that the given attacker has dealt damage
#[derive(Debug, Clone, Serialize)]
pub struct DamageReceived {
    /// Ship ID of the aggressor
    pub aggressor: EntityId,
    /// Amount of damage dealt
    pub damage: f32,
}

/// Sent to update the minimap display
#[derive(Debug, Clone, Serialize)]
pub struct MinimapUpdate {
    /// The ship ID of the ship to update
    pub entity_id: EntityId,
    /// True when the raw packed position is (0, 0), indicating the ship is not
    /// visible on the minimap. Checked on raw 11-bit integer values before float
    /// conversion to avoid floating-point precision issues.
    pub is_sentinel: bool,
    /// Set to true if the ship should disappear from the minimap (false otherwise)
    pub disappearing: bool,
    /// The heading of the ship. Unit is degrees, 0 is up, positive is clockwise
    /// (so 90.0 is East)
    pub heading: f32,
    /// Normalized position on the minimap
    pub position: NormalizedPos,
    /// Unknown, but this appears to be something related to the big hunt
    pub unknown: bool,
}

impl MinimapUpdate {
    /// Returns true if this is a hydrophone-style minimap ping: a one-shot
    /// position flash from minimap-only detection (e.g. submarine hydrophone).
    ///
    /// These updates have `disappearing=true` with a valid (non-sentinel)
    /// position. They are always isolated — never preceded by active tracking
    /// and never followed by a sentinel. The position is valid at the instant
    /// of the ping but should not be treated as sustained detection.
    pub fn is_minimap_ping(&self) -> bool {
        self.disappearing && !self.is_sentinel
    }
}

/// A single shell in an artillery salvo (from SHOT in alias.xml)
#[derive(Debug, Clone, Serialize)]
pub struct ArtilleryShotData {
    pub origin: WorldPos,
    /// Gun barrel pitch angle at fire time (radians).
    pub pitch: f32,
    pub speed: f32,
    pub target: WorldPos,
    pub shot_id: ShotId,
    /// Which barrel within the turret fired this shell.
    pub gun_barrel_id: u16,
    /// Server-side time remaining for the shell to reach the target (seconds).
    pub server_time_left: f32,
    /// Height of the shooter above sea level at fire time.
    pub shooter_height: f32,
    /// Distance from the gun to the aimed target point.
    pub hit_distance: f32,
}

/// A salvo of artillery shells from one ship
#[derive(Debug, Clone, Serialize)]
pub struct ArtillerySalvo {
    pub owner_id: EntityId,
    pub params_id: GameParamId,
    pub salvo_id: u32,
    pub shots: Vec<ArtilleryShotData>,
}

/// Homing torpedo maneuver state (from TORPEDO_MANEUVER_DUMP in alias.xml).
#[derive(Debug, Clone, Serialize)]
pub struct TorpedoManeuverDump {
    pub target_yaw: f32,
    pub change_time: f32,
    pub stop_time: f32,
    pub current_time: f32,
    pub yaw_speed: f32,
    pub arm_pos: WorldPos,
    pub final_pos: WorldPos,
}

/// Acoustic torpedo guidance state (from TORPEDO_ACOUSTIC_DUMP in alias.xml).
#[derive(Debug, Clone, Serialize)]
pub struct TorpedoAcousticDump {
    pub is_chasing_target: bool,
    pub prediction_lost: bool,
    pub modificators_level: u8,
    pub activation_time: f32,
    pub degradation_time: f32,
    pub speed_coef: f32,
    pub rotation_yaw: f32,
    pub vertical_speed: f32,
    pub target_yaw: f32,
    pub target_depth: f32,
}

/// A single torpedo launch (from TORPEDO in alias.xml)
#[derive(Debug, Clone, Serialize)]
pub struct TorpedoData {
    pub owner_id: EntityId,
    pub params_id: GameParamId,
    pub salvo_id: u32,
    /// Torpedo skin (cosmetic variant).
    pub skin_id: GameParamId,
    pub shot_id: ShotId,
    pub origin: WorldPos,
    /// Direction vector whose magnitude is the torpedo speed in m/s.
    pub direction: WorldPos,
    /// Whether the torpedo warhead is armed (can detonate on contact).
    pub armed: bool,
    /// Homing torpedo maneuver state. None for straight-running torpedoes.
    pub maneuver_dump: Option<TorpedoManeuverDump>,
    /// Acoustic torpedo guidance state. None for non-acoustic torpedoes.
    pub acoustic_dump: Option<TorpedoAcousticDump>,
}

/// Physics body state for a ship hull fragment after cracking apart on death.
/// Serialized as a 72-byte (0x48) raw binary blob by the engine's `dumpState()`.
/// Used by `syncShipCracks` to synchronize sinking animation between server and client.
#[derive(Debug, Clone, Serialize)]
pub struct PhysicsBodyState {
    /// Elasticity/friction coefficient (body struct offset +0x88, next to mass at +0x84)
    pub elasticity: f32,
    /// World position (x, y, z)
    pub position: WorldPos,
    /// Orientation as a quaternion (x, y, z, w)
    pub orientation: [f32; 4],
    /// Linear velocity in m/s (x, y, z)
    pub linear_velocity: WorldPos,
    /// Angular velocity in rad/s (x, y, z)
    pub angular_velocity: WorldPos,
    /// Unknown physics parameters (likely buoyancy/damping state)
    pub unknown1: [f32; 2],
    /// Unknown physics parameter (likely water damping coefficient)
    pub unknown2: f32,
}

impl PhysicsBodyState {
    /// Parse a 72-byte physics body state blob.
    /// Returns None if the blob is empty or not exactly 72 bytes.
    pub fn parse(data: &[u8]) -> Option<Self> {
        if data.len() != 72 {
            return None;
        }
        let f = |offset: usize| -> f32 { f32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) };
        Some(PhysicsBodyState {
            elasticity: f(0x00),
            position: WorldPos { x: f(0x04), y: f(0x08), z: f(0x0C) },
            orientation: [f(0x10), f(0x14), f(0x18), f(0x1C)],
            linear_velocity: WorldPos { x: f(0x20), y: f(0x24), z: f(0x28) },
            angular_velocity: WorldPos { x: f(0x2C), y: f(0x30), z: f(0x34) },
            unknown1: [f(0x38), f(0x3C)],
            unknown2: f(0x40),
        })
    }
}

/// Packed hit type from SHOTKILL, encoding both collision type and shell hit type.
/// Packed as `collision_type << 5 | shell_hit_type` by `IntPackerUnpacker`.
#[derive(Debug, Clone, Serialize)]
pub struct HitType {
    pub collision: Recognized<CollisionType>,
    pub shell_hit: Recognized<ShellHitType>,
    /// The raw packed byte, preserved in case of unknown values.
    pub raw: u8,
}

impl HitType {
    pub fn from_raw(raw: u8, ships_constants: &wowsunpack::game_constants::ShipsConstants, version: &Version) -> Self {
        let collision_id = ((raw >> 5) & 0x07) as i32;
        let shell_hit_id = (raw & 0x1F) as i32;
        let collision = CollisionType::from_id(collision_id, ships_constants, *version)
            .unwrap_or(Recognized::Unknown(format!("{collision_id}")));
        let shell_hit = ShellHitType::from_id(shell_hit_id, ships_constants, *version)
            .unwrap_or(Recognized::Unknown(format!("{shell_hit_id}")));
        Self { collision, shell_hit, raw }
    }
}

/// Terminal ballistics state at the moment of shell impact (from TERMINAL_BALLISTICS_INFO).
/// Contains the shell's position, velocity vector, detonator state, and the angle
/// against the impacted armor material. Available in game version 14.8+.
#[derive(Debug, Clone, Serialize)]
pub struct TerminalBallisticsInfo {
    /// Shell position at impact in world coordinates.
    pub position: WorldPos,
    /// Shell velocity vector after server-side impact processing (post-impact, not incoming).
    /// Used by the game client to spawn a visual FakeShot showing the shell exiting/bouncing.
    /// For ricochets this is the bounce direction; for overpens the exit direction.
    pub velocity: WorldPos,
    /// Whether the AP detonator has been activated (fuse armed).
    pub detonator_activated: bool,
    /// Angle between the shell trajectory and the armor plate normal (radians).
    pub material_angle: f32,
}

/// A single projectile hit (from receiveShotKills)
#[derive(Debug, Clone, Serialize)]
pub struct ShotHit {
    pub owner_id: EntityId,
    pub hit_type: HitType,
    pub shot_id: ShotId,
    /// World-space position where the projectile impacted.
    pub position: WorldPos,
    /// Terminal ballistics info at impact (shell velocity, detonator state, armor angle).
    /// Only present in game versions that include TERMINAL_BALLISTICS_INFO in SHOTKILL.
    pub terminal_ballistics: Option<TerminalBallisticsInfo>,
}

/// Enumerates the "cruise states". See <https://github.com/lkolbly/wows-replays/issues/14#issuecomment-976784004>
/// for more information.
#[derive(Debug, Clone, Copy, Serialize)]
pub enum CruiseState {
    /// Possible values for the throttle range from -1 for reverse to 4 for full power ahead.
    Throttle,
    /// Note that not all rudder changes are indicated via cruise states, only ones
    /// set via the Q & E keys. Temporarily setting the rudder will not trigger this
    /// packet.
    ///
    /// Possible associated values are:
    /// - -2: Full rudder to port,
    /// - -1: Half rudder to port,
    /// - 0: Neutral
    /// - 1: Half rudder to starboard,
    /// - 2: Full rudder to starboard.
    Rudder,
    /// Sets the dive depth. Known values are:
    /// - 0: 0m
    /// - 1: -6m (periscope depth)
    /// - 2: -18m
    /// - 3: -30m
    /// - 4: -42m
    /// - 5: -54m
    /// - 6: -66m
    /// - 7: -80m
    DiveDepth,
    /// Indicates an unknown cruise state. Send me your replay!
    Unknown(u32),
}

#[derive(Debug, Serialize)]
pub struct ChatMessageExtra {
    pre_battle_sign: i64,
    pre_battle_id: i64,
    player_clan_tag: String,
    typ: i64,
    player_avatar_id: EntityId,
    player_name: String,
}

/// A single entry from a `receiveDamageStat` update.
///
/// The game server sends cumulative damage statistics as a pickled dict keyed by
/// `(weapon_type: i64, category: i64)` with values `[count: i64, total: f64]`.
/// Each entry represents cumulative totals (not incremental deltas) — later updates
/// for the same key replace earlier values.
#[derive(Debug, Clone, Serialize)]
pub struct DamageStatEntry {
    /// Which weapon/damage source this stat tracks.
    pub weapon: Recognized<DamageStatWeapon>,
    /// Which category of damage (enemy, ally, spotting, potential).
    pub category: Recognized<DamageStatCategory>,
    /// Cumulative hit count for this weapon+category combination.
    pub count: i64,
    /// Cumulative damage total for this weapon+category combination.
    pub total: f64,
}

#[derive(Debug, Serialize, Kinded)]
#[kinded(derive(Serialize))]
pub enum DecodedPacketPayload<'replay, 'argtype, 'rawpacket> {
    /// Represents a chat message. Note that this only includes text chats, voicelines
    /// are represented by the VoiceLine variant.
    Chat {
        entity_id: EntityId,
        /// Avatar ID of the sender
        sender_id: AccountId,
        /// Represents the audience for the chat: Division, team, or all.
        audience: &'replay str,
        /// The actual chat message.
        message: &'replay str,
        /// Extra data that may be present if sender_id is 0
        extra_data: Option<ChatMessageExtra>,
    },
    /// Sent when a voice line is played (for example, "Wilco!")
    VoiceLine {
        /// Avatar ID of the player sending the voiceline
        sender_id: AccountId,
        /// True if the voiceline is visible in all chat, false if only in team chat
        is_global: bool,
        /// Which voiceline it is.
        message: VoiceLine,
    },
    /// Sent when the player earns a ribbon
    Ribbon(Ribbon),
    /// Indicates the position of the given object.
    Position(crate::packet2::PositionPacket),
    /// Indicates the position of the player's object or camera.
    PlayerOrientation(crate::packet2::PlayerOrientationPacket),
    /// Server-authoritative cumulative damage statistics from `receiveDamageStat`.
    ///
    /// Each entry represents cumulative totals for a (weapon, category) pair.
    /// Only `DamageStatCategory::Enemy` entries represent actual damage dealt.
    DamageStat(Vec<DamageStatEntry>),
    /// Sent when a ship is destroyed.
    ShipDestroyed {
        /// The ship ID (note: Not the avatar ID) of the killer
        killer: EntityId,
        /// The ship ID (note: Not the avatar ID) of the victim
        victim: EntityId,
        /// Cause of death
        cause: Recognized<DeathCause>,
    },
    EntityMethod(&'rawpacket EntityMethodPacket<'argtype>),
    EntityProperty(&'rawpacket crate::packet2::EntityPropertyPacket<'argtype>),
    BasePlayerCreate(&'rawpacket crate::packet2::BasePlayerCreatePacket<'argtype>),
    CellPlayerCreate(&'rawpacket crate::packet2::CellPlayerCreatePacket<'argtype>),
    EntityEnter(&'rawpacket crate::packet2::EntityEnterPacket),
    EntityLeave(&'rawpacket crate::packet2::EntityLeavePacket),
    EntityCreate(&'rawpacket crate::packet2::EntityCreatePacket<'argtype>),
    /// Contains all of the info required to setup the arena state and show the initial loading screen.
    OnArenaStateReceived {
        /// Unknown
        arena_id: i64,
        /// Unknown
        team_build_type_id: i8,
        /// Unknown
        pre_battles_info: HashMap<i64, Vec<Option<HashMap<String, String>>>>,
        /// A list of the human players in this game
        player_states: Vec<PlayerStateData>,
        /// A list of the bot players in this game
        bot_states: Vec<PlayerStateData>,
    },
    /// Contains info when the arena state changes
    OnGameRoomStateChanged {
        /// Updated player states
        player_states: Vec<HashMap<&'static str, pickled::Value>>,
    },
    /// Sent when new players/bots spawn mid-battle (e.g. reinforcement waves in Operations).
    /// Same format as OnArenaStateReceived player/bot lists.
    NewPlayerSpawnedInBattle {
        /// Human players that spawned
        player_states: Vec<PlayerStateData>,
        /// Bot players that spawned
        bot_states: Vec<PlayerStateData>,
    },
    CheckPing(u64),
    /// Indicates that the given victim has received damage from one or more attackers.
    DamageReceived {
        /// Ship ID of the ship being damaged
        victim: EntityId,
        /// List of damages happening to this ship
        aggressors: Vec<DamageReceived>,
    },
    /// Contains data for a minimap update
    MinimapUpdate {
        /// A list of the updates to make to the minimap
        updates: Vec<MinimapUpdate>,
        /// Unknown
        arg1: &'rawpacket Vec<ArgValue<'argtype>>,
    },
    /// Indicates a property update. Note that many properties contain a hierarchy of properties,
    /// for example the "state" property on the battle manager contains nested dictionaries and
    /// arrays. The top-level entity and property are specified by the `entity_id` and `property`
    /// fields. The nesting structure and how to modify the leaves are indicated by the
    /// `update_cmd` field.
    ///
    /// Within the `update_cmd` field is two fields, `levels` and `action`. `levels` indicates how
    /// to traverse to the leaf property, for example by following a dictionary key or array index.
    /// `action` indicates what action to perform once there, such as setting a subproperty to
    /// a specific value.
    ///
    /// For example, to set the `state[controlPoints][0][hasInvaders]` property, you will see a
    /// packet payload that looks like:
    /// ```ignore
    /// {
    ///     "entity_id": 576258,
    ///     "property": "state",
    ///     "update_cmd": {
    ///         "levels": [
    ///             {"DictKey": "controlPoints"},
    ///             {"ArrayIndex": 0}
    ///         ],
    ///         "action": {
    ///             "SetKey":{"key":"hasInvaders","value":1}
    ///         }
    ///     }
    /// }
    /// ```
    /// This says to take the "state" property on entity 576258, navigate to `state["controlPoints"][0]`,
    /// and set the sub-key `hasInvaders` there to 1.
    ///
    /// The following properties and values are known:
    /// - `state["controlPoints"][N]["invaderTeam"]`: Indicates the team ID of the team currently
    ///   contesting the control point. -1 if nobody is invading point.
    /// - `state["controlPoints"][N]["hasInvaders"]`: 1 if the point is being contested, 0 otherwise.
    /// - `state["controlPoints"][N]["progress"]`: A tuple of two elements. The first is the fraction
    ///   captured, ranging from 0 to 1 as the point is captured, and the second is the amount of
    ///   time remaining until the point is captured.
    /// - `state["controlPoints"][N]["bothInside"]`: 1 if both teams are currently in point, 0 otherwise.
    /// - `state["missions"]["teamsScore"][N]["score"]`: The value of team N's score.
    PropertyUpdate(&'rawpacket crate::packet2::PropertyUpdatePacket<'argtype>),
    /// Indicates that the battle has ended
    BattleEnd {
        /// The team ID of the winning team (corresponds to the teamid in [OnArenaStateReceivedPlayer])
        winning_team: Option<i8>,
        /// How the battle ended (from `FINISH_TYPE` in battle.xml)
        finish_type: Option<Recognized<FinishType>>,
    },
    /// Sent when a consumable is activated
    Consumable {
        /// The ship ID of the ship using the consumable
        entity: EntityId,
        /// The consumable
        consumable: Recognized<Consumable>,
        /// How long the consumable will be active for
        duration: f32,
        /// Usage parameters (15.2+): how the consumable was targeted.
        /// `None` for pre-15.2 replays.
        usage_params: Option<ConsumableUsageParams>,
    },
    /// Indicates a change to the "cruise state," which is the fixed settings for various controls
    /// such as steering (using the Q & E keys), throttle, and dive planes.
    CruiseState {
        /// Which cruise state is being affected
        state: CruiseState,
        /// See [CruiseState] for what the values mean.
        value: i32,
    },
    Map(&'rawpacket crate::packet2::MapPacket<'replay>),
    /// A string representation of the game version this replay is from.
    Version(String),
    Camera(&'rawpacket crate::packet2::CameraPacket),
    /// Indicates a change in the current camera mode
    CameraMode(Recognized<CameraMode>),
    /// If true, indicates that the player has enabled the "free look" camera (by holding right click)
    CameraFreeLook(bool),
    /// Artillery shells fired
    ArtilleryShots {
        avatar_id: AvatarId,
        salvos: Vec<ArtillerySalvo>,
    },
    /// Torpedoes launched
    TorpedoesReceived {
        avatar_id: AvatarId,
        torpedoes: Vec<TorpedoData>,
    },
    /// Homing torpedo direction/position update
    TorpedoDirection {
        owner_id: EntityId,
        shot_id: ShotId,
        position: WorldPos,
        target_yaw: f32,
        speed_coef: f32,
    },
    /// Projectile hits (shells or torpedoes hitting targets)
    ShotKills {
        avatar_id: AvatarId,
        hits: Vec<ShotHit>,
    },
    /// Turret rotation sync for a ship
    GunSync {
        entity_id: EntityId,
        /// Gun group (0 = main battery)
        group: u32,
        /// Turret index within the group
        turret: u32,
        /// Turret yaw in radians relative to ship heading (0 = forward, PI = aft)
        yaw: f32,
        /// Barrel elevation in radians
        pitch: f32,
    },
    /// A new squadron appears on the minimap
    PlaneAdded {
        entity_id: EntityId,
        plane_id: PlaneId,
        /// Team index: 0 = recording player's team, 1 = enemy team
        team_id: u32,
        params_id: GameParamId,
        position: WorldPos2D,
    },
    /// A fighter patrol ward is placed (from receive_wardAdded).
    /// This is the game's mechanism for marking patrol circle areas.
    WardAdded {
        entity_id: EntityId,
        plane_id: PlaneId,
        /// Patrol center position (world coordinates)
        position: WorldPos,
        /// Patrol radius in BigWorld units
        radius: BigWorldDistance,
        /// Owner ship entity ID
        owner_id: EntityId,
    },
    /// A fighter patrol ward is removed (from receive_wardRemoved).
    WardRemoved {
        entity_id: EntityId,
        plane_id: PlaneId,
    },
    /// A squadron is removed from the minimap
    PlaneRemoved {
        entity_id: EntityId,
        plane_id: PlaneId,
    },
    /// Plane/squadron position update on the minimap
    PlanePosition {
        entity_id: EntityId,
        plane_id: PlaneId,
        position: WorldPos2D,
    },
    /// Ammo type selected for a weapon group
    SetAmmoForWeapon {
        entity_id: EntityId,
        /// 0 = artillery, 2 = torpedo
        weapon_type: u32,
        /// GameParamId of the projectile (look up ammoType in GameParams)
        ammo_param_id: GameParamId,
        /// True if the player just switched ammo and is reloading
        is_reload: bool,
    },
    /// EntityControl — transfers entity ownership to the client.
    EntityControl(&'rawpacket crate::packet2::EntityControlPacket),
    /// Non-volatile entity position update (no direction/dead-reckoning).
    NonVolatilePosition(&'rawpacket crate::packet2::NonVolatilePositionPacket),
    /// Player network stats: fps (u8), ping in ms (u16), isLaggingNow (bool).
    PlayerNetStats(&'rawpacket crate::packet2::PlayerNetStatsPacket),
    /// Server timestamp at session start.
    ServerTimestamp(f64),
    /// Links the Avatar to its owned ship entity.
    OwnShip(&'rawpacket crate::packet2::OwnShipPacket),
    /// `onSetWeaponLock` — weapon lock state change.
    SetWeaponLock(&'rawpacket crate::packet2::SetWeaponLockPacket),
    /// Server tick rate constant (observed as 1/7).
    ServerTick(f64),
    /// Submarine controller mode change (0/1 toggle, likely surface/dive).
    SubController(&'rawpacket crate::packet2::SubControllerPacket),
    /// Shot tracking change (entity_id + i64 value, fire control related).
    ShotTracking(&'rawpacket crate::packet2::ShotTrackingPacket),
    /// Gun marker / aiming state (target point, marker position/direction/diameter, etc.).
    GunMarker(&'rawpacket crate::packet2::GunMarkerPacket),
    /// Synchronizes physics state for the two hull fragments after a ship is destroyed
    /// and cracks apart. The engine uses `correctBodyFromServer()` to smoothly interpolate
    /// toward the server state rather than snapping. Purely visual — controls the sinking
    /// animation of the two ship halves.
    SyncShipCracks {
        entity_id: EntityId,
        /// Physics body state for crack part 1 (bow or stern half). None if blob is empty.
        state1: Option<PhysicsBodyState>,
        /// Physics body state for crack part 2 (the other half). None if blob is empty.
        state2: Option<PhysicsBodyState>,
    },
    /// Packet 0x10: Init flag at clock=0.
    InitFlag(u8),
    /// Packet 0x13: Empty init marker at clock=0.
    InitMarker,
    /// This is a packet of unknown type
    Unknown(&'replay [u8]),
    /// This is a packet of known type, but which we were unable to parse
    Invalid(&'rawpacket crate::packet2::InvalidPacket<'replay>),
    /// If parsing with audits enabled, this indicates a packet that may be of special interest
    /// for whoever is reading the audits.
    Audit(String),
    /// End of battle results (free xp, damage details, etc.)
    BattleResults(&'replay str),
    /*
    ArtilleryHit(ArtilleryHitPacket<'a>),
    */
}

fn try_convert_hashable_pickle_to_string(value: pickled::value::HashableValue) -> pickled::value::HashableValue {
    match value {
        pickled::value::HashableValue::Bytes(b) => {
            if let Ok(s) = std::str::from_utf8(b.inner()) {
                pickled::value::HashableValue::String(s.to_owned().into())
            } else {
                pickled::value::HashableValue::Bytes(b)
            }
        }
        pickled::value::HashableValue::Tuple(t) => pickled::value::HashableValue::Tuple(
            t.inner().iter().cloned().map(try_convert_hashable_pickle_to_string).collect::<Vec<_>>().into(),
        ),
        pickled::value::HashableValue::FrozenSet(s) => pickled::value::HashableValue::FrozenSet(
            s.inner().iter().cloned().map(try_convert_hashable_pickle_to_string).collect::<BTreeSet<_>>().into(),
        ),
        value => value,
    }
}

/// Helper function to recursively convert byte values to strings where possible.
fn try_convert_pickle_to_string(value: pickled::value::Value) -> pickled::value::Value {
    match value {
        pickled::value::Value::Bytes(b) => {
            if let Ok(s) = std::str::from_utf8(b.inner()) {
                pickled::value::Value::String(s.to_owned().into())
            } else {
                pickled::value::Value::Bytes(b)
            }
        }
        pickled::value::Value::List(l) => pickled::value::Value::List(
            l.inner().iter().cloned().map(try_convert_pickle_to_string).collect::<Vec<_>>().into(),
        ),
        pickled::value::Value::Tuple(t) => pickled::value::Value::Tuple(
            t.inner().iter().cloned().map(try_convert_pickle_to_string).collect::<Vec<_>>().into(),
        ),
        pickled::value::Value::Set(s) => pickled::value::Value::Set(
            s.inner().iter().cloned().map(try_convert_hashable_pickle_to_string).collect::<BTreeSet<_>>().into(),
        ),
        pickled::value::Value::FrozenSet(s) => pickled::value::Value::FrozenSet(
            s.inner().iter().cloned().map(try_convert_hashable_pickle_to_string).collect::<BTreeSet<_>>().into(),
        ),
        pickled::value::Value::Dict(d) => pickled::value::Value::Dict(
            d.inner()
                .iter()
                .map(|(k, v)| {
                    (try_convert_hashable_pickle_to_string(k.clone()), try_convert_pickle_to_string(v.clone()))
                })
                .collect::<std::collections::BTreeMap<_, _>>()
                .into(),
        ),
        value => value,
    }
}

fn parse_receive_common_cmd_blob(blob: &[u8]) -> PResult<(VoiceLine, bool)> {
    let i = &mut &*blob;
    let line = le_u16.parse_next(i)?;
    let audience = le_u8.parse_next(i)?;

    let is_global = match audience {
        0 => false,
        1 => true,
        _ => {
            panic!("Got unknown audience {}", audience);
        }
    };
    let message = match line {
        1 => {
            let x = le_u16.parse_next(i)?;
            let y = le_u16.parse_next(i)?;
            VoiceLine::AttentionToSquare(x as u32, y as u32)
        }
        2 => {
            let target_type = le_u16.parse_next(i)?;
            let target_id = le_u64.parse_next(i)?;
            VoiceLine::QuickTactic(target_type, target_id)
        }
        3 => VoiceLine::RequestingSupport(None),
        // 4 is "QUICK_SOS"
        // 5 is AYE_AYE
        5 => VoiceLine::Wilco,
        // 6 is NO_WAY
        6 => VoiceLine::Negative,
        // GOOD_GAME
        7 => VoiceLine::WellDone, // TODO: Find the corresponding field
        // GOOD_LUCK
        8 => VoiceLine::FairWinds,
        // CARAMBA
        9 => VoiceLine::Curses,
        // 10 -> THANK_YOU
        10 => VoiceLine::DefendTheBase,
        // 11 -> NEED_AIR_DEFENSE
        11 => VoiceLine::ProvideAntiAircraft,
        // BACK
        12 => {
            let _target_type = le_u16.parse_next(i)?;
            let target_id = le_u64.parse_next(i)?;
            VoiceLine::Retreat(if target_id != 0 { Some(target_id as i32) } else { None })
        }
        // NEED_VISION
        13 => VoiceLine::IntelRequired,
        // NEED_SMOKE
        14 => VoiceLine::SetSmokeScreen,
        // RLS
        15 => VoiceLine::UsingRadar,
        // SONAR
        16 => VoiceLine::UsingHydroSearch,
        // FOLLOW_ME
        17 => VoiceLine::FollowMe,
        // MAP_POINT_ATTENTION
        18 => {
            let x = le_f32.parse_next(i)?;
            let y = le_f32.parse_next(i)?;
            VoiceLine::MapPointAttention(x, y)
        }
        //  SUBMARINE_LOCATOR
        19 => VoiceLine::UsingSubmarineLocator,
        line => {
            eprintln!("Warning: Unknown voice line {}, {:#X?}", line, *i);
            VoiceLine::Unknown(line as i64)
        }
    };

    Ok((message, is_global))
}

impl<'replay, 'argtype, 'rawpacket> DecodedPacketPayload<'replay, 'argtype, 'rawpacket>
where
    'rawpacket: 'replay,
    'rawpacket: 'argtype,
{
    fn from(
        version: &Version,
        audit: bool,
        payload: &'rawpacket crate::packet2::PacketType<'replay, 'argtype>,
        _packet_type: u32,
        battle_constants: &wowsunpack::game_constants::BattleConstants,
        common_constants: &wowsunpack::game_constants::CommonConstants,
        ships_constants: &wowsunpack::game_constants::ShipsConstants,
    ) -> Self {
        match payload {
            PacketType::EntityMethod(em) => DecodedPacketPayload::from_entity_method(
                version,
                audit,
                em,
                battle_constants,
                common_constants,
                ships_constants,
            ),
            PacketType::Camera(camera) => DecodedPacketPayload::Camera(camera),
            PacketType::CameraMode(mode) => {
                if let Some(cm) = CameraMode::from_id(*mode as i32, battle_constants, *version) {
                    DecodedPacketPayload::CameraMode(cm)
                } else if audit {
                    DecodedPacketPayload::Audit(format!("CameraMode({})", mode))
                } else {
                    DecodedPacketPayload::CameraMode(Recognized::Unknown(format!("{}", mode)))
                }
            }
            PacketType::CameraFreeLook(freelook) => match freelook {
                0 => DecodedPacketPayload::CameraFreeLook(false),
                1 => DecodedPacketPayload::CameraFreeLook(true),
                _ => {
                    if audit {
                        DecodedPacketPayload::Audit(format!("CameraFreeLook({})", freelook))
                    } else {
                        DecodedPacketPayload::CameraFreeLook(true)
                    }
                }
            },
            PacketType::CruiseState(cs) => match cs.key {
                0 => DecodedPacketPayload::CruiseState { state: CruiseState::Throttle, value: cs.value },
                1 => DecodedPacketPayload::CruiseState { state: CruiseState::Rudder, value: cs.value },
                2 => DecodedPacketPayload::CruiseState { state: CruiseState::DiveDepth, value: cs.value },
                _ => {
                    if audit {
                        DecodedPacketPayload::Audit(format!("CruiseState(unknown={}, {})", cs.key, cs.value))
                    } else {
                        DecodedPacketPayload::CruiseState { state: CruiseState::Unknown(cs.key), value: cs.value }
                    }
                }
            },
            PacketType::Map(map) => {
                if audit && map.unknown != 0 && map.unknown != 1 {
                    DecodedPacketPayload::Audit(format!("Map: Unknown bool is not a bool (is {})", map.unknown))
                } else if audit
                    && map.matrix
                        != [
                            0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 128, 63,
                        ]
                {
                    DecodedPacketPayload::Audit(format!("Map: Unit matrix is not a unit matrix (is {:?})", map.matrix))
                } else {
                    DecodedPacketPayload::Map(map)
                }
            }
            PacketType::EntityProperty(p) => DecodedPacketPayload::EntityProperty(p),
            PacketType::Position(pos) => DecodedPacketPayload::Position((*pos).clone()),
            PacketType::PlayerOrientation(pos) => DecodedPacketPayload::PlayerOrientation((*pos).clone()),
            PacketType::BasePlayerCreate(b) => DecodedPacketPayload::BasePlayerCreate(b),
            PacketType::CellPlayerCreate(c) => DecodedPacketPayload::CellPlayerCreate(c),
            PacketType::EntityEnter(e) => DecodedPacketPayload::EntityEnter(e),
            PacketType::EntityLeave(e) => DecodedPacketPayload::EntityLeave(e),
            PacketType::EntityCreate(e) => DecodedPacketPayload::EntityCreate(e),
            PacketType::PropertyUpdate(update) => DecodedPacketPayload::PropertyUpdate(update),
            PacketType::Version(version) => DecodedPacketPayload::Version(version.clone()),
            PacketType::Unknown(u) => DecodedPacketPayload::Unknown(u),
            PacketType::Invalid(u) => DecodedPacketPayload::Invalid(u),
            PacketType::BattleResults(results) => DecodedPacketPayload::BattleResults(results),
            PacketType::EntityControl(ec) => DecodedPacketPayload::EntityControl(ec),
            PacketType::NonVolatilePosition(sd) => DecodedPacketPayload::NonVolatilePosition(sd),
            PacketType::PlayerNetStats(ns) => DecodedPacketPayload::PlayerNetStats(ns),
            PacketType::ServerTimestamp(st) => DecodedPacketPayload::ServerTimestamp(st.timestamp),
            PacketType::OwnShip(os) => DecodedPacketPayload::OwnShip(os),
            PacketType::SetWeaponLock(wl) => DecodedPacketPayload::SetWeaponLock(wl),
            PacketType::ServerTick(tick) => DecodedPacketPayload::ServerTick(*tick),
            PacketType::SubController(sc) => DecodedPacketPayload::SubController(sc),
            PacketType::ShotTracking(st) => DecodedPacketPayload::ShotTracking(st),
            PacketType::GunMarker(gm) => DecodedPacketPayload::GunMarker(gm),
            PacketType::InitFlag(flag) => DecodedPacketPayload::InitFlag(*flag),
            PacketType::InitMarker => DecodedPacketPayload::InitMarker,
        }
    }

    fn extract_vec3(val: Option<&ArgValue>) -> WorldPos {
        match val {
            Some(ArgValue::Vector3((x, y, z))) => WorldPos { x: *x, y: *y, z: *z },
            Some(ArgValue::Array(a)) if a.len() >= 3 => {
                let x: f32 = (&a[0]).try_into().unwrap_or(0.0);
                let y: f32 = (&a[1]).try_into().unwrap_or(0.0);
                let z: f32 = (&a[2]).try_into().unwrap_or(0.0);
                WorldPos { x, y, z }
            }
            _ => WorldPos::default(),
        }
    }

    fn from_entity_method(
        version: &Version,
        audit: bool,
        packet: &'rawpacket EntityMethodPacket<'argtype>,
        battle_constants: &wowsunpack::game_constants::BattleConstants,
        common_constants: &wowsunpack::game_constants::CommonConstants,
        ships_constants: &wowsunpack::game_constants::ShipsConstants,
    ) -> Self {
        let entity_id = &packet.entity_id;
        let method = &packet.method;
        let args = &packet.args;
        if *method == "onChatMessage" {
            let target = match &args[1] {
                ArgValue::String(s) => s,
                _ => panic!("foo"),
            };
            let message = match &args[2] {
                ArgValue::String(s) => s,
                _ => panic!("foo"),
            };
            let sender_id = match &args[0] {
                ArgValue::Int32(i) => i,
                _ => panic!("foo"),
            };
            let mut extra_data = None;
            if *sender_id == 0 && args.len() >= 4 {
                let extra =
                    pickled::de::value_from_slice(args[3].string_ref().expect("failed"), pickled::de::DeOptions::new())
                        .expect("value is not pickled");
                let mut extra_dict: HashMap<String, Value> = HashMap::from_iter(
                    extra.dict().expect("value is not a dictionary").inner().iter().map(|(key, value)| {
                        let key = match key {
                            pickled::HashableValue::Bytes(bytes) => {
                                String::from_utf8(bytes.inner().clone()).expect("key is not a valid utf-8 sequence")
                            }
                            pickled::HashableValue::String(string) => string.inner().clone(),
                            other => {
                                panic!("unexpected key type {:?}", other)
                            }
                        };

                        let value = match value {
                            Value::Bytes(bytes) => {
                                if let Ok(result) = String::from_utf8(bytes.inner().clone()) {
                                    Value::String(result.into())
                                } else {
                                    Value::Bytes(bytes.clone())
                                }
                            }
                            other => other.clone(),
                        };

                        (key, value)
                    }),
                );

                let extra = ChatMessageExtra {
                    pre_battle_sign: extra_dict
                        .remove("preBattleSign")
                        .unwrap()
                        .i64()
                        .expect("preBattleSign is not an i64"),
                    pre_battle_id: extra_dict.remove("prebattleId").unwrap().i64().expect("preBattleId is not an i64"),
                    player_clan_tag: extra_dict
                        .remove("playerClanTag")
                        .unwrap()
                        .string()
                        .expect("playerClanTag is not a string")
                        .inner()
                        .clone(),
                    typ: extra_dict.remove("type").unwrap().i64().expect("type is not an i64"),
                    player_avatar_id: EntityId::from(
                        extra_dict.remove("playerAvatarId").unwrap().i64().expect("playerAvatarId is not an i64"),
                    ),
                    player_name: extra_dict
                        .remove("playerName")
                        .unwrap()
                        .string()
                        .expect("playerName is not a string")
                        .inner()
                        .clone(),
                };

                assert!(extra_dict.is_empty());

                extra_data = Some(extra);
            }
            DecodedPacketPayload::Chat {
                entity_id: *entity_id,
                sender_id: AccountId::from(*sender_id),
                audience: std::str::from_utf8(target).unwrap(),
                message: std::str::from_utf8(message).unwrap(),
                extra_data,
            }
        } else if *method == "receive_CommonCMD" {
            let (sender_id, message, is_global) = if version.is_at_least(&Version::from_client_exe("0,12,8,0")) {
                let sender = *args[0].int_32_ref().expect("receive_CommonCMD: sender is not an i32");

                let blob = args[1].blob_ref().expect("receive_CommonCMD: second argument is not a blob");

                let (message_type, is_global) = match parse_receive_common_cmd_blob(blob.as_ref()) {
                    Ok(result) => result,
                    Err(e) => {
                        eprintln!("Warning: receive_CommonCMD: failed to parse blob: {:?}", e);
                        (VoiceLine::Unknown(0), false)
                    }
                };

                (sender, message_type, is_global)
            } else {
                let (audience, sender_id, line, a, b) = unpack_rpc_args!(args, u8, i32, u8, u32, u64);
                let is_global = match audience {
                    0 => false,
                    1 => true,
                    _ => {
                        panic!(
                            "Got unknown audience {} sender=0x{:x} line={} a={:x} b={:x}",
                            audience, sender_id, line, a, b
                        );
                    }
                };
                let message = match line {
                    1 => VoiceLine::AttentionToSquare(a, b as u32),
                    2 => VoiceLine::QuickTactic(a as u16, b),
                    3 => VoiceLine::RequestingSupport(None),
                    5 => VoiceLine::Wilco,
                    6 => VoiceLine::Negative,
                    7 => VoiceLine::WellDone, // TODO: Find the corresponding field
                    8 => VoiceLine::FairWinds,
                    9 => VoiceLine::Curses,
                    10 => VoiceLine::DefendTheBase,
                    11 => VoiceLine::ProvideAntiAircraft,
                    12 => VoiceLine::Retreat(if b != 0 { Some(b as i32) } else { None }),
                    13 => VoiceLine::IntelRequired,
                    14 => VoiceLine::SetSmokeScreen,
                    15 => VoiceLine::UsingRadar,
                    16 => VoiceLine::UsingHydroSearch,
                    17 => VoiceLine::FollowMe,
                    18 => VoiceLine::MapPointAttention(a as f32, b as f32),
                    19 => VoiceLine::UsingSubmarineLocator,
                    _ => {
                        eprintln!("Warning: Unknown voice line {} a={:x} b={:x}!", line, a, b);
                        VoiceLine::Unknown(line as i64)
                    }
                };

                (sender_id, message, is_global)
            };

            // let (audience, sender_id, line, a, b) = unpack_rpc_args!(args, u8, i32, u8, u32, u64);

            DecodedPacketPayload::VoiceLine { sender_id: AccountId::from(sender_id), is_global, message }
        } else if *method == "onGameRoomStateChanged" {
            let player_states = pickled::de::value_from_slice(
                args[0].blob_ref().expect("player_states arg is not a blob"),
                pickled::de::DeOptions::new(),
            )
            .expect("failed to deserialize player_states");

            let player_states = try_convert_pickle_to_string(player_states);

            let mut players_out = vec![];
            if let pickled::value::Value::List(players) = &player_states {
                for player in players.inner().iter() {
                    let raw_values = convert_flat_dict_to_real_dict(player);

                    let mapped_values = PlayerStateData::convert_raw_dict(&raw_values, version, false);
                    players_out.push(mapped_values);
                }
            }
            DecodedPacketPayload::OnGameRoomStateChanged { player_states: players_out }
        } else if *method == "onNewPlayerSpawnedInBattle" {
            // Args: playersData (BLOB), botsData (BLOB), observersData (BLOB)
            // Same pickle format as onArenaStateReceived player/bot lists.
            let mut players_out = vec![];
            if let Some(ArgValue::Blob(blob)) = args.first()
                && let Ok(value) = pickled::de::value_from_slice(blob, pickled::de::DeOptions::new())
            {
                let value = try_convert_pickle_to_string(value);
                if let pickled::value::Value::List(players) = &value {
                    for player in players.inner().iter() {
                        players_out.push(PlayerStateData::from_pickle(player, version, false));
                    }
                }
            }

            let mut bots_out = vec![];
            if let Some(ArgValue::Blob(blob)) = args.get(1)
                && let Ok(value) = pickled::de::value_from_slice(blob, pickled::de::DeOptions::new())
            {
                let value = try_convert_pickle_to_string(value);
                if let pickled::value::Value::List(bots) = &value {
                    for bot in bots.inner().iter() {
                        bots_out.push(PlayerStateData::from_pickle(bot, version, true));
                    }
                }
            }

            DecodedPacketPayload::NewPlayerSpawnedInBattle { player_states: players_out, bot_states: bots_out }
        } else if *method == "onArenaStateReceived" {
            let (arg0, arg1) = unpack_rpc_args!(args, i64, i8);

            let value = pickled::de::value_from_slice(
                match &args[2] {
                    ArgValue::Blob(x) => x,
                    _ => panic!("foo"),
                },
                pickled::de::DeOptions::new(),
            )
            .unwrap();

            let value = match value {
                pickled::value::Value::Dict(d) => d,
                _ => panic!(),
            };
            let mut arg2 = HashMap::new();
            for (k, v) in value.inner().iter() {
                let k = match k {
                    pickled::value::HashableValue::I64(i) => *i,
                    _ => panic!(),
                };
                let v = match v {
                    pickled::value::Value::List(l) => l,
                    _ => panic!(),
                };
                let v: Vec<_> = v
                    .inner()
                    .iter()
                    .map(|elem| match elem {
                        pickled::value::Value::Dict(d) => Some(
                            d.inner()
                                .iter()
                                .map(|(k, v)| {
                                    let k = match k {
                                        pickled::value::HashableValue::Bytes(b) => {
                                            std::str::from_utf8(b.inner()).unwrap().to_string()
                                        }
                                        _ => panic!(),
                                    };
                                    let v = format!("{:?}", v);
                                    (k, v)
                                })
                                .collect(),
                        ),
                        pickled::value::Value::None => None,
                        _ => panic!(),
                    })
                    .collect();
                arg2.insert(k, v);
            }

            let value = pickled::de::value_from_slice(
                match &args[3] {
                    ArgValue::Blob(x) => x,
                    _ => panic!("foo"),
                },
                pickled::de::DeOptions::new(),
            )
            .unwrap();
            let value = try_convert_pickle_to_string(value);

            let mut players_out = vec![];
            if let pickled::value::Value::List(players) = &value {
                for player in players.inner().iter() {
                    players_out.push(PlayerStateData::from_pickle(player, version, false));
                }
            }

            let mut bots_out = vec![];
            if let Some(ArgValue::Blob(blob)) = args.get(4)
                && let Ok(value) = pickled::de::value_from_slice(blob, pickled::de::DeOptions::new())
            {
                let value = try_convert_pickle_to_string(value);
                if let pickled::value::Value::List(bots) = &value {
                    for bot in bots.inner().iter() {
                        bots_out.push(PlayerStateData::from_pickle(bot, version, true));
                    }
                }
            }

            DecodedPacketPayload::OnArenaStateReceived {
                arena_id: arg0,
                team_build_type_id: arg1,
                pre_battles_info: arg2,
                player_states: players_out,
                bot_states: bots_out,
            }
        } else if *method == "receiveDamageStat" {
            let value = pickled::de::value_from_slice(
                match &args[0] {
                    ArgValue::Blob(x) => x,
                    _ => panic!("foo"),
                },
                pickled::de::DeOptions::new(),
            )
            .unwrap();

            let mut stats = vec![];
            match value {
                pickled::value::Value::Dict(d) => {
                    for (k, v) in d.inner().iter() {
                        let (weapon_raw, category_raw) = match k {
                            pickled::value::HashableValue::Tuple(t) => {
                                let t = t.inner();
                                assert!(t.len() == 2);
                                (
                                    match &t[0] {
                                        pickled::value::HashableValue::I64(i) => *i,
                                        _ => panic!("foo"),
                                    },
                                    match &t[1] {
                                        pickled::value::HashableValue::I64(i) => *i,
                                        _ => panic!("foo"),
                                    },
                                )
                            }
                            _ => panic!("foo"),
                        };
                        let (count, total) = match v {
                            pickled::value::Value::List(t) => {
                                let t = t.inner();
                                assert!(t.len() == 2);
                                (
                                    match &t[0] {
                                        pickled::value::Value::I64(i) => *i,
                                        _ => panic!("foo"),
                                    },
                                    match &t[1] {
                                        pickled::value::Value::F64(i) => *i,
                                        // Spotting damage can be sent as integer 0
                                        pickled::value::Value::I64(i) => *i as f64,
                                        _ => panic!("foo"),
                                    },
                                )
                            }
                            _ => panic!("foo"),
                        };

                        let weapon = DamageStatWeapon::from_id(weapon_raw as i32, battle_constants, *version)
                            .unwrap_or(Recognized::Unknown(format!("{weapon_raw}")));
                        let category = DamageStatCategory::from_id(category_raw as i32, battle_constants, *version)
                            .unwrap_or(Recognized::Unknown(format!("{category_raw}")));
                        stats.push(DamageStatEntry { weapon, category, count, total });
                    }
                }
                _ => panic!("foo"),
            }
            DecodedPacketPayload::DamageStat(stats)
        } else if *method == "receiveVehicleDeath" {
            let (victim, killer, cause) = unpack_rpc_args!(args, i32, i32, u32);
            let cause = if let Some(dc) = DeathCause::from_id(cause as i32, battle_constants, *version) {
                dc
            } else if audit {
                return DecodedPacketPayload::Audit(format!(
                    "receiveVehicleDeath(victim={}, killer={}, unknown cause {})",
                    victim, killer, cause
                ));
            } else {
                Recognized::Unknown(format!("{}", cause))
            };
            DecodedPacketPayload::ShipDestroyed {
                victim: EntityId::from(victim),
                killer: EntityId::from(killer),
                cause,
            }
        } else if *method == "onRibbon" {
            let (ribbon,) = unpack_rpc_args!(args, i8);
            let ribbon = match ribbon {
                1 => Ribbon::TorpedoHit,
                3 => Ribbon::PlaneShotDown,
                4 => Ribbon::Incapacitation,
                5 => Ribbon::Destroyed,
                6 => Ribbon::SetFire,
                7 => Ribbon::Flooding,
                8 => Ribbon::Citadel,
                9 => Ribbon::Defended,
                10 => Ribbon::Captured,
                11 => Ribbon::AssistedInCapture,
                13 => Ribbon::SecondaryHit,
                14 => Ribbon::OverPenetration,
                15 => Ribbon::Penetration,
                16 => Ribbon::NonPenetration,
                17 => Ribbon::Ricochet,
                19 => Ribbon::Spotted,
                21 => Ribbon::DiveBombPenetration,
                25 => Ribbon::RocketPenetration,
                26 => Ribbon::RocketNonPenetration,
                27 => Ribbon::ShotDownByAircraft,
                28 => Ribbon::TorpedoProtectionHit,
                30 => Ribbon::RocketTorpedoProtectionHit,
                31 => Ribbon::DepthChargeHit,
                33 => Ribbon::BuffSeized,
                39 => Ribbon::SonarOneHit,
                40 => Ribbon::SonarTwoHits,
                41 => Ribbon::SonarNeutralized,
                ribbon => {
                    if audit {
                        return DecodedPacketPayload::Audit(format!("onRibbon(unknown ribbon {})", ribbon));
                    } else {
                        Ribbon::Unknown(ribbon)
                    }
                }
            };
            DecodedPacketPayload::Ribbon(ribbon)
        } else if *method == "receiveDamagesOnShip" {
            let mut v = vec![];
            for elem in match &args[0] {
                ArgValue::Array(a) => a,
                _ => panic!(),
            } {
                let map = match elem {
                    ArgValue::FixedDict(m) => m,
                    _ => panic!(),
                };
                let aggressor_raw: i32 = map.get("vehicleID").unwrap().try_into().unwrap();
                v.push(DamageReceived {
                    aggressor: EntityId::from(aggressor_raw),
                    damage: map.get("damage").unwrap().try_into().unwrap(),
                });
            }
            DecodedPacketPayload::DamageReceived { victim: *entity_id, aggressors: v }
        } else if *method == "onCheckGamePing" {
            let (ping,) = unpack_rpc_args!(args, u64);
            DecodedPacketPayload::CheckPing(ping)
        } else if *method == "updateMinimapVisionInfo" {
            let v = match &args[0] {
                ArgValue::Array(a) => a,
                _ => panic!(),
            };
            let mut updates = vec![];
            for minimap_update in v.iter() {
                let minimap_update = match minimap_update {
                    ArgValue::FixedDict(m) => m,
                    _ => panic!(),
                };
                let vehicle_id = minimap_update.get("vehicleID").unwrap();

                let packed_data: u32 = minimap_update.get("packedData").unwrap().try_into().unwrap();
                let update = RawMinimapUpdate::from_bytes(packed_data.to_le_bytes());
                let heading = update.heading() as f32 / 256. * 360. - 180.;

                // Check raw 11-bit values for the sentinel (0, 0) before float
                // conversion to avoid any floating-point precision issues.
                // Raw 0 maps to -2500 in world coords (the Python renderer
                // checks `x != -2500 or y != -2500`).
                let is_sentinel = update.x() == 0 && update.y() == 0;

                let x = update.x() as f32 / 512. - 1.5;
                let y = update.y() as f32 / 512. - 1.5;

                updates.push(MinimapUpdate {
                    entity_id: match vehicle_id {
                        ArgValue::Uint32(u) => EntityId::from(*u),
                        _ => panic!(),
                    },
                    position: NormalizedPos { x, y },
                    heading,
                    is_sentinel,
                    disappearing: update.is_disappearing(),
                    unknown: update.unknown(),
                })
            }

            let args1 = match &args[1] {
                ArgValue::Array(a) => a,
                _ => panic!(),
            };

            DecodedPacketPayload::MinimapUpdate { updates, arg1: args1 }
        } else if *method == "onBattleEnd" {
            let (winning_team, finish_type) = if args.len() >= 2 {
                let (winning_team, raw_finish) = unpack_rpc_args!(args, i8, u8);
                let ft = if let Some(ft) = FinishType::from_id(raw_finish as i32, battle_constants, *version) {
                    ft
                } else {
                    Recognized::Unknown(format!("{}", raw_finish))
                };
                (Some(winning_team), Some(ft))
            } else {
                (None, None)
            };
            DecodedPacketPayload::BattleEnd { winning_team, finish_type }
        } else if *method == "consumableUsed" || *method == "onConsumableUsed" {
            // In 15.2+ the first arg changed from CONSUMABLE_ID (a plain integer) to
            // CONSUMABLE_USAGE_PARAMS (a packed struct serialized as a Blob).
            // b[0] = ConsumableUsageType: 0=None, 1=Default (<BB>), 2=Position (<BBff>), 3=Entity (<BBbQ>).
            // b[1] = consumable type ID in all variants (except None).
            let is_new_format = version.is_at_least(&Version { major: 15, minor: 2, patch: 0, build: 0 });
            let (raw_consumable, usage_params): (i32, Option<ConsumableUsageParams>) = if is_new_format {
                match &args[0] {
                    ArgValue::Blob(b) => {
                        // The blob is serialized by UsageConverter via struct.pack with the
                        // format determined by ConsumableUsageType. Length checks guard against
                        // truncated replay data (the game always writes the full struct).
                        match b.first().copied() {
                            Some(0) => {
                                // NONE — no consumable ID or extra data
                                (0, Some(ConsumableUsageParams::None))
                            }
                            Some(1) if b.len() >= 2 => {
                                // DEFAULT: struct.pack('<BB', usage_type, consumable_id) = 2 bytes
                                (b[1] as i32, Some(ConsumableUsageParams::Default))
                            }
                            Some(2) if b.len() >= 10 => {
                                // POSITION: struct.pack('<BBff', usage_type, consumable_id, x, z) = 10 bytes
                                let x = f32::from_le_bytes([b[2], b[3], b[4], b[5]]);
                                let z = f32::from_le_bytes([b[6], b[7], b[8], b[9]]);
                                (b[1] as i32, Some(ConsumableUsageParams::Position(WorldPos2D { x, z })))
                            }
                            Some(3) if b.len() >= 11 => {
                                // ENTITY: struct.pack('<BBbQ', usage_type, consumable_id, target_type, target_id) = 11 bytes
                                let target_type = b[2] as i8;
                                let target_id = u64::from_le_bytes([b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10]]);
                                (b[1] as i32, Some(ConsumableUsageParams::Entity { target_type, target_id }))
                            }
                            other => {
                                error!("onConsumableUsed (15.2+): unexpected blob: {:?}", other);
                                return DecodedPacketPayload::EntityMethod(packet);
                            }
                        }
                    }
                    other => {
                        error!("onConsumableUsed (15.2+): expected Blob arg, got: {:?}", other);
                        return DecodedPacketPayload::EntityMethod(packet);
                    }
                }
            } else {
                let id = match &args[0] {
                    ArgValue::Int8(v) => *v as i32,
                    ArgValue::Uint8(v) => *v as i32,
                    ArgValue::Int16(v) => *v as i32,
                    ArgValue::Uint16(v) => *v as i32,
                    ArgValue::Int32(v) => *v,
                    other => {
                        error!("onConsumableUsed (pre-15.2): unexpected arg type: {:?}", other);
                        return DecodedPacketPayload::EntityMethod(packet);
                    }
                };
                (id, None)
            };
            let duration: f32 = match &args[1] {
                ArgValue::Float32(v) => *v,
                ArgValue::Float64(v) => *v as f32,
                other => {
                    error!("onConsumableUsed: unexpected duration arg type: {:?}", other);
                    return DecodedPacketPayload::EntityMethod(packet);
                }
            };
            // Try runtime-loaded consumable types from game data first
            let consumable = if let Some(c) = Consumable::from_id(raw_consumable, common_constants, *version) {
                c
            } else if audit {
                return DecodedPacketPayload::Audit(format!(
                    "consumableUsed({},{},{})",
                    entity_id, raw_consumable, duration
                ));
            } else {
                Recognized::Unknown(format!("{}", raw_consumable))
            };

            DecodedPacketPayload::Consumable { entity: *entity_id, consumable, duration, usage_params }
        } else if *method == "receiveArtilleryShots" {
            let salvos_array = match &args[0] {
                ArgValue::Array(a) => a,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let mut salvos = Vec::new();
            for salvo_val in salvos_array.iter() {
                let salvo_dict = match salvo_val {
                    ArgValue::FixedDict(m) => m,
                    _ => continue,
                };
                let owner_id: i32 = salvo_dict.get("ownerID").and_then(ArgValue::as_i32).unwrap_or(0);
                let params_id: u32 = salvo_dict.get("paramsID").and_then(ArgValue::as_u32).unwrap_or(0);
                let salvo_id: u32 = salvo_dict.get("salvoID").and_then(ArgValue::as_u32).unwrap_or(0);
                let shots_array = match salvo_dict.get("shots") {
                    Some(ArgValue::Array(a)) => a,
                    _ => continue,
                };
                let mut shots = Vec::new();
                for shot_val in shots_array.iter() {
                    let shot_dict = match shot_val {
                        ArgValue::FixedDict(m) => m,
                        _ => continue,
                    };
                    let pos = Self::extract_vec3(shot_dict.get("pos"));
                    let pitch: f32 = shot_dict.get("pitch").and_then(ArgValue::as_f32).unwrap_or(0.0);
                    let speed: f32 = shot_dict.get("speed").and_then(ArgValue::as_f32).unwrap_or(0.0);
                    let tar_pos = Self::extract_vec3(shot_dict.get("tarPos"));
                    let shot_id: u32 = shot_dict.get("shotID").and_then(ArgValue::as_u32).unwrap_or(0);
                    let gun_barrel_id: u16 = match shot_dict.get("gunBarrelID") {
                        Some(ArgValue::Uint16(v)) => *v,
                        Some(ArgValue::Int16(v)) => *v as u16,
                        Some(ArgValue::Uint8(v)) => *v as u16,
                        _ => 0,
                    };
                    let server_time_left: f32 =
                        shot_dict.get("serverTimeLeft").and_then(ArgValue::as_f32).unwrap_or(0.0);
                    let shooter_height: f32 = shot_dict.get("shooterHeight").and_then(ArgValue::as_f32).unwrap_or(0.0);
                    let hit_distance: f32 = shot_dict.get("hitDistance").and_then(ArgValue::as_f32).unwrap_or(0.0);
                    shots.push(ArtilleryShotData {
                        origin: pos,
                        pitch,
                        speed,
                        target: tar_pos,
                        shot_id: ShotId::from(shot_id),
                        gun_barrel_id,
                        server_time_left,
                        shooter_height,
                        hit_distance,
                    });
                }
                salvos.push(ArtillerySalvo {
                    owner_id: EntityId::from(owner_id),
                    params_id: GameParamId::from(params_id),
                    salvo_id,
                    shots,
                });
            }
            DecodedPacketPayload::ArtilleryShots { avatar_id: AvatarId::from(*entity_id), salvos }
        } else if *method == "receiveTorpedoes" {
            let salvos_array = match &args[0] {
                ArgValue::Array(a) => a,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let mut torpedoes = Vec::new();
            for salvo_val in salvos_array.iter() {
                let salvo_dict = match salvo_val {
                    ArgValue::FixedDict(m) => m,
                    _ => continue,
                };
                let owner_id: i32 = salvo_dict.get("ownerID").and_then(ArgValue::as_i32).unwrap_or(0);
                let params_id: u32 = salvo_dict.get("paramsID").and_then(ArgValue::as_u32).unwrap_or(0);
                let salvo_id: u32 = salvo_dict.get("salvoID").and_then(ArgValue::as_u32).unwrap_or(0);
                let skin_id: u32 = salvo_dict.get("skinID").and_then(ArgValue::as_u32).unwrap_or(0);
                let torps_array = match salvo_dict.get("torpedoes") {
                    Some(ArgValue::Array(a)) => a,
                    _ => continue,
                };
                for torp_val in torps_array.iter() {
                    let torp_dict = match torp_val {
                        ArgValue::FixedDict(m) => m,
                        _ => continue,
                    };
                    let pos = Self::extract_vec3(torp_dict.get("pos"));
                    let dir = Self::extract_vec3(torp_dict.get("dir"));
                    let shot_id: u32 = torp_dict.get("shotID").and_then(ArgValue::as_u32).unwrap_or(0);
                    let armed = match torp_dict.get("armed") {
                        Some(ArgValue::Uint8(v)) => *v != 0,
                        Some(ArgValue::Int8(v)) => *v != 0,
                        _ => false,
                    };
                    let maneuver_dump = torp_dict.get("maneuverDump").and_then(|v| {
                        let d = match v {
                            ArgValue::FixedDict(d) => d,
                            ArgValue::NullableFixedDict(Some(d)) => d,
                            _ => return None,
                        };
                        Some(TorpedoManeuverDump {
                            target_yaw: d.get("targetYaw").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            change_time: d.get("changeTime").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            stop_time: d.get("stopTime").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            current_time: d.get("currentTime").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            yaw_speed: d.get("yawSpeed").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            arm_pos: Self::extract_vec3(d.get("armPos")),
                            final_pos: Self::extract_vec3(d.get("finalPos")),
                        })
                    });
                    let acoustic_dump = torp_dict.get("acousticDump").and_then(|v| {
                        let d = match v {
                            ArgValue::FixedDict(d) => d,
                            ArgValue::NullableFixedDict(Some(d)) => d,
                            _ => return None,
                        };
                        Some(TorpedoAcousticDump {
                            is_chasing_target: match d.get("isChasingTarget") {
                                Some(ArgValue::Uint8(v)) => *v != 0,
                                Some(ArgValue::Int8(v)) => *v != 0,
                                _ => false,
                            },
                            prediction_lost: match d.get("predictionLost") {
                                Some(ArgValue::Uint8(v)) => *v != 0,
                                Some(ArgValue::Int8(v)) => *v != 0,
                                _ => false,
                            },
                            modificators_level: match d.get("modificatorsLevel") {
                                Some(ArgValue::Uint8(v)) => *v,
                                Some(ArgValue::Int8(v)) => *v as u8,
                                _ => 0,
                            },
                            activation_time: d.get("activationTime").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            degradation_time: d.get("degradationTime").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            speed_coef: d.get("speedCoef").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            rotation_yaw: d.get("rotationYaw").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            vertical_speed: d.get("verticalSpeed").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            target_yaw: d.get("targetYaw").and_then(ArgValue::as_f32).unwrap_or(0.0),
                            target_depth: d.get("targetDepth").and_then(ArgValue::as_f32).unwrap_or(0.0),
                        })
                    });
                    torpedoes.push(TorpedoData {
                        owner_id: EntityId::from(owner_id),
                        params_id: GameParamId::from(params_id),
                        salvo_id,
                        skin_id: GameParamId::from(skin_id),
                        shot_id: ShotId::from(shot_id),
                        origin: pos,
                        direction: dir,
                        armed,
                        maneuver_dump,
                        acoustic_dump,
                    });
                }
            }
            DecodedPacketPayload::TorpedoesReceived { avatar_id: AvatarId::from(*entity_id), torpedoes }
        } else if *method == "receiveShotKills" {
            // SHOTKILLS_PACK: Array of { ownerID: PLAYER_ID, hitType: UINT8, kills: Array<SHOTKILL> }
            // SHOTKILL: { pos: VECTOR3, shotID: SHOT_ID }
            let packs = match &args[0] {
                ArgValue::Array(a) => a,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let mut hits = Vec::new();
            for pack in packs {
                let pack_dict = match pack {
                    ArgValue::FixedDict(d) => d,
                    _ => continue,
                };
                let owner_id: i32 = pack_dict.get("ownerID").and_then(ArgValue::as_i32).unwrap_or(0);
                let hit_type: u8 = match pack_dict.get("hitType") {
                    Some(ArgValue::Uint8(v)) => *v,
                    Some(ArgValue::Int8(v)) => *v as u8,
                    _ => 0,
                };
                let kills_array = match pack_dict.get("kills") {
                    Some(ArgValue::Array(a)) => a,
                    _ => continue,
                };
                for kill in kills_array {
                    let kill_dict = match kill {
                        ArgValue::FixedDict(d) => d,
                        _ => continue,
                    };
                    let shot_id: u32 = kill_dict.get("shotID").and_then(ArgValue::as_u32).unwrap_or(0);
                    let pos = Self::extract_vec3(kill_dict.get("pos"));
                    let terminal_ballistics = kill_dict.get("terminalBallisticsInfo").and_then(|v| {
                        let d = match v {
                            ArgValue::FixedDict(d) => d,
                            ArgValue::NullableFixedDict(Some(d)) => d,
                            _ => return None,
                        };
                        let position = Self::extract_vec3(d.get("position"));
                        let velocity = Self::extract_vec3(d.get("velocity"));
                        let detonator_activated = match d.get("detonatorActivated") {
                            Some(ArgValue::Uint8(v)) => *v != 0,
                            Some(ArgValue::Int8(v)) => *v != 0,
                            _ => false,
                        };
                        let material_angle = d.get("materialAngle").and_then(ArgValue::as_f32).unwrap_or(0.0);
                        Some(TerminalBallisticsInfo { position, velocity, detonator_activated, material_angle })
                    });
                    hits.push(ShotHit {
                        owner_id: EntityId::from(owner_id),
                        hit_type: HitType::from_raw(hit_type, ships_constants, version),
                        shot_id: ShotId::from(shot_id),
                        position: pos,
                        terminal_ballistics,
                    });
                }
            }
            DecodedPacketPayload::ShotKills { avatar_id: AvatarId::from(*entity_id), hits }
        } else if *method == "receiveTorpedoDirection" {
            // args: [owner_id, shot_id, position, target_yaw, ?, speed_coef, rotation_yaw, ?, is_chasing]
            let owner_id: EntityId = match &args[0] {
                ArgValue::Int32(v) => EntityId::from(*v),
                ArgValue::Uint32(v) => EntityId::from(*v),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let shot_id: ShotId = match &args[1] {
                ArgValue::Int32(v) => ShotId::from(*v as u32),
                ArgValue::Uint32(v) => ShotId::from(*v),
                ArgValue::Uint8(v) => ShotId::from(*v as u32),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let position = Self::extract_vec3(Some(&args[2]));
            let target_yaw = match &args[3] {
                ArgValue::Float32(v) => *v,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let speed_coef = match &args[5] {
                ArgValue::Float32(v) => *v,
                _ => 1.0,
            };
            DecodedPacketPayload::TorpedoDirection { owner_id, shot_id, position, target_yaw, speed_coef }
        } else if *method == "receive_addMinimapSquadron" {
            // args: [plane_id, team_id, params_id, position, unknown]
            let plane_id: PlaneId = match &args[0] {
                ArgValue::Uint64(v) => PlaneId::from(*v),
                ArgValue::Int64(v) => PlaneId::from(*v),
                ArgValue::Uint32(v) => PlaneId::from(*v as u64),
                ArgValue::Int32(v) => PlaneId::from(*v as i64),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let team_id: u32 = match &args[1] {
                ArgValue::Uint32(v) => *v,
                ArgValue::Int32(v) => *v as u32,
                ArgValue::Uint64(v) => *v as u32,
                ArgValue::Int64(v) => *v as u32,
                ArgValue::Uint8(v) => *v as u32,
                ArgValue::Int8(v) => *v as u32,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let params_id: u64 = match &args[2] {
                ArgValue::Uint64(v) => *v,
                ArgValue::Int64(v) => *v as u64,
                ArgValue::Uint32(v) => *v as u64,
                ArgValue::Int32(v) => *v as u64,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let position = match &args[3] {
                ArgValue::Array(a) if a.len() >= 2 => {
                    let x: f32 = (&a[0]).try_into().unwrap_or(0.0);
                    let y: f32 = (&a[1]).try_into().unwrap_or(0.0);
                    (x, y)
                }
                ArgValue::Vector2((x, y)) => (*x, *y),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::PlaneAdded {
                entity_id: *entity_id,
                plane_id,
                team_id,
                params_id: GameParamId::from(params_id),
                position: WorldPos2D { x: position.0, z: position.1 },
            }
        } else if *method == "receive_removeMinimapSquadron" {
            let plane_id: PlaneId = match &args[0] {
                ArgValue::Uint64(v) => PlaneId::from(*v),
                ArgValue::Int64(v) => PlaneId::from(*v),
                ArgValue::Uint32(v) => PlaneId::from(*v as u64),
                ArgValue::Int32(v) => PlaneId::from(*v as i64),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::PlaneRemoved { entity_id: *entity_id, plane_id }
        } else if *method == "receive_updateMinimapSquadron" {
            let plane_id: PlaneId = match &args[0] {
                ArgValue::Uint64(v) => PlaneId::from(*v),
                ArgValue::Int64(v) => PlaneId::from(*v),
                ArgValue::Uint32(v) => PlaneId::from(*v as u64),
                ArgValue::Int32(v) => PlaneId::from(*v as i64),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let position = match &args[1] {
                ArgValue::Array(a) if a.len() >= 2 => {
                    let x: f32 = (&a[0]).try_into().unwrap_or(0.0);
                    let y: f32 = (&a[1]).try_into().unwrap_or(0.0);
                    (x, y)
                }
                ArgValue::Vector2((x, y)) => (*x, *y),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::PlanePosition {
                entity_id: *entity_id,
                plane_id,
                position: WorldPos2D { x: position.0, z: position.1 },
            }
        } else if *method == "receive_wardAdded" {
            // args: [squadronId, position, unknown, radius, relation, ownerId, unknown2]
            let plane_id: PlaneId = match &args[0] {
                ArgValue::Uint64(v) => PlaneId::from(*v),
                ArgValue::Int64(v) => PlaneId::from(*v),
                ArgValue::Uint32(v) => PlaneId::from(*v as u64),
                ArgValue::Int32(v) => PlaneId::from(*v as i64),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let position = match &args[1] {
                ArgValue::Vector3((x, y, z)) => WorldPos { x: *x, y: *y, z: *z },
                ArgValue::Array(a) if a.len() >= 3 => {
                    let x: f32 = (&a[0]).try_into().unwrap_or(0.0);
                    let y: f32 = (&a[1]).try_into().unwrap_or(0.0);
                    let z: f32 = (&a[2]).try_into().unwrap_or(0.0);
                    WorldPos { x, y, z }
                }
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let radius: f32 = match &args[3] {
                ArgValue::Float32(v) => *v,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let owner_id: EntityId = match &args[5] {
                ArgValue::Uint32(v) => EntityId::from(*v),
                ArgValue::Int32(v) => EntityId::from(*v),
                ArgValue::Uint64(v) => EntityId::from(*v as u32),
                ArgValue::Int64(v) => EntityId::from(*v),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::WardAdded {
                entity_id: *entity_id,
                plane_id,
                position,
                radius: BigWorldDistance::from(radius),
                owner_id,
            }
        } else if *method == "receive_wardRemoved" {
            // args: [squadronId]
            let plane_id: PlaneId = match &args[0] {
                ArgValue::Uint64(v) => PlaneId::from(*v),
                ArgValue::Int64(v) => PlaneId::from(*v),
                ArgValue::Uint32(v) => PlaneId::from(*v as u64),
                ArgValue::Int32(v) => PlaneId::from(*v as i64),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::WardRemoved { entity_id: *entity_id, plane_id }
        } else if *method == "syncGun" {
            // args: [group: int, turret: int, yaw: f32, pitch: f32, state: int, f32, array]
            let group = match &args[0] {
                ArgValue::Uint8(v) => *v as u32,
                ArgValue::Int8(v) => *v as u32,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let turret = match &args[1] {
                ArgValue::Uint8(v) => *v as u32,
                ArgValue::Int8(v) => *v as u32,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let yaw = match &args[2] {
                ArgValue::Float32(v) => *v,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let pitch = match &args[3] {
                ArgValue::Float32(v) => *v,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::GunSync { entity_id: *entity_id, group, turret, yaw, pitch }
        } else if *method == "setAmmoForWeapon" {
            // args: [weaponType: u8, ammoParamsId: u32, isReload: bool (optional in older replays)]
            let weapon_type = match &args[0] {
                ArgValue::Uint8(v) => *v as u32,
                ArgValue::Int8(v) => *v as u32,
                ArgValue::Uint32(v) => *v,
                ArgValue::Int32(v) => *v as u32,
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let ammo_param_id = match &args[1] {
                ArgValue::Uint32(v) => GameParamId::from(*v),
                ArgValue::Int32(v) => GameParamId::from(*v as u32),
                ArgValue::Uint64(v) => GameParamId::from(*v),
                ArgValue::Int64(v) => GameParamId::from(*v),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let is_reload = if args.len() > 2 {
                match &args[2] {
                    ArgValue::Uint8(v) => *v != 0,
                    ArgValue::Int8(v) => *v != 0,
                    _ => false,
                }
            } else {
                false
            };
            DecodedPacketPayload::SetAmmoForWeapon { entity_id: *entity_id, weapon_type, ammo_param_id, is_reload }
        } else if *method == "syncShipCracks" {
            // args: [state1: BLOB, state2: BLOB] — 72-byte physics body states for each hull half
            let raw1 = match &args[0] {
                ArgValue::String(s) => s.as_slice(),
                ArgValue::Blob(b) => b.as_slice(),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            let raw2 = match &args[1] {
                ArgValue::String(s) => s.as_slice(),
                ArgValue::Blob(b) => b.as_slice(),
                _ => return DecodedPacketPayload::EntityMethod(packet),
            };
            DecodedPacketPayload::SyncShipCracks {
                entity_id: *entity_id,
                state1: PhysicsBodyState::parse(raw1),
                state2: PhysicsBodyState::parse(raw2),
            }
        } else {
            DecodedPacketPayload::EntityMethod(packet)
        }
    }
}

#[derive(Debug, Serialize)]
pub struct DecodedPacket<'replay, 'argtype, 'rawpacket> {
    pub packet_type: u32,
    pub clock: crate::types::GameClock,
    pub payload: DecodedPacketPayload<'replay, 'argtype, 'rawpacket>,
    /// Bytes remaining after parsing. Non-empty means the parser didn't consume
    /// the full packet payload.
    #[serde(skip_serializing_if = "<[u8]>::is_empty")]
    pub leftover: &'replay [u8],
}

/// Reusable packet decoder that holds version and game constants.
///
/// Create once per replay, then call `decode()` for each packet.
#[derive(bon::Builder)]
pub struct PacketDecoder<'a> {
    version: Version,
    #[builder(default)]
    audit: bool,
    #[builder(default = &DEFAULT_BATTLE_CONSTANTS)]
    battle_constants: &'a wowsunpack::game_constants::BattleConstants,
    #[builder(default = &DEFAULT_COMMON_CONSTANTS)]
    common_constants: &'a wowsunpack::game_constants::CommonConstants,
    #[builder(default = &DEFAULT_SHIPS_CONSTANTS)]
    ships_constants: &'a wowsunpack::game_constants::ShipsConstants,
}

impl<'a> PacketDecoder<'a> {
    pub fn decode<'replay, 'argtype, 'rawpacket>(
        &self,
        packet: &'rawpacket Packet<'_, '_>,
    ) -> DecodedPacket<'replay, 'argtype, 'rawpacket>
    where
        'rawpacket: 'replay,
        'rawpacket: 'argtype,
    {
        DecodedPacket {
            clock: packet.clock,
            packet_type: packet.packet_type,
            payload: DecodedPacketPayload::from(
                &self.version,
                self.audit,
                &packet.payload,
                packet.packet_type,
                self.battle_constants,
                self.common_constants,
                self.ships_constants,
            ),
            leftover: packet.leftover,
        }
    }
}

struct Decoder {
    silent: bool,
    output: Option<Box<dyn std::io::Write>>,
    packet_decoder: PacketDecoder<'static>,
}

impl Decoder {
    fn write(&mut self, line: &str) {
        if !self.silent {
            match self.output.as_mut() {
                Some(f) => {
                    writeln!(f, "{}", line).unwrap();
                }
                None => {
                    println!("{}", line);
                }
            }
        }
    }
}

mod raw_minimap_update {
    #![allow(dead_code)]
    use modular_bitfield::prelude::*;

    #[bitfield]
    #[derive(Debug)]
    pub(crate) struct RawMinimapUpdate {
        pub x: B11,
        pub y: B11,
        pub heading: B8,
        pub unknown: bool,
        pub is_disappearing: bool,
    }
}
use raw_minimap_update::RawMinimapUpdate;

impl Analyzer for Decoder {
    fn finish(&mut self) {}

    fn process(&mut self, packet: &Packet<'_, '_>) {
        let decoded = self.packet_decoder.decode(packet);
        //println!("{:#?}", decoded);
        //println!("{}", serde_json::to_string_pretty(&decoded).unwrap());
        let encoded = serde_json::to_string(&decoded).unwrap();
        self.write(&encoded);
    }
}