wows_replays 0.2.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
use crate::analyzer::{Analyzer, AnalyzerBuilder};
use crate::packet2::{Entity, EntityMethodPacket, Packet, PacketType};
use crate::{ErrorKind, IResult};
use kinded::Kinded;
use modular_bitfield::prelude::*;
use nom::number::complete::{le_f32, le_i32, le_u16, le_u32, le_u64, le_u8};
use pickled::Value;
use serde::Serialize;
use std::collections::HashMap;
use std::convert::TryInto;
use std::iter::FromIterator;
use wowsunpack::data::Version;
use wowsunpack::rpc::typedefs::ArgValue;
use wowsunpack::unpack_rpc_args;

use super::analyzer::{AnalyzerMut, AnalyzerMutBuilder};

pub struct DecoderBuilder {
    silent: bool,
    no_meta: bool,
    path: Option<String>,
}

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()),
        }
    }
}

impl AnalyzerMutBuilder for DecoderBuilder {
    fn build(&self, meta: &crate::ReplayMeta) -> Box<dyn AnalyzerMut> {
        let version = Version::from_client_exe(&meta.clientVersionFromExe);
        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>
            }),
            version: version,
        };
        if !self.no_meta {
            decoder.write(&serde_json::to_string(&meta).unwrap());
        }
        Box::new(decoder)
    }
}

/// Enumerates voicelines which can be said in the game.
#[derive(Debug, Clone, Copy, Serialize)]
pub enum VoiceLine {
    IntelRequired,
    FairWinds,
    Wilco,
    Negative,
    WellDone,
    Curses,
    UsingRadar,
    UsingHydroSearch,
    DefendTheBase, // TODO: ...except when it's "thank you"?
    SetSmokeScreen,
    FollowMe,
    // TODO: definitely has associated data similar to AttentionToSquare
    /// World x and y coordinates corresponding to the map grid
    /// MapPointQuickCommand in game code
    MapPointAttention(f32, f32),
    UsingSubmarineLocator,
    /// "Provide anti-aircraft support"
    ProvideAntiAircraft,
    /// If a player is called out in the message, their avatar ID will be here.
    RequestingSupport(Option<u32>),
    /// If a player is called out in the message, their avatar ID will be here.
    Retreat(Option<i32>),

    /// The position is (letter,number) and zero-indexed. e.g. F2 is (5,1)
    /// `RectangleAttentionCommand`` in game code
    AttentionToSquare(u32, u32),

    /// Field is the avatar ID of the target
    /// Pair of the target type and target ID
    QuickTactic(u16, u64),
}

/// Enumerates the ribbons which appear in the top-right
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize)]
pub enum Ribbon {
    PlaneShotDown,
    Incapacitation,
    SetFire,
    Citadel,
    SecondaryHit,
    OverPenetration,
    Penetration,
    NonPenetration,
    Ricochet,
    TorpedoProtectionHit,
    Captured,
    AssistedInCapture,
    Spotted,
    Destroyed,
    TorpedoHit,
    Defended,
    Flooding,
    DiveBombPenetration,
    RocketPenetration,
    RocketNonPenetration,
    RocketTorpedoProtectionHit,
    DepthChargeHit,
    ShotDownByAircraft,
    BuffSeized,
    SonarOneHit,
    SonarTwoHits,
    SonarNeutralized,
    Unknown(i8),
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize)]
pub enum DeathCause {
    Secondaries,
    Artillery,
    Fire,
    Flooding,
    Torpedo,
    DiveBomber,
    AerialRocket,
    AerialTorpedo,
    Detonation,
    Ramming,
    DepthCharge,
    SkipBombs,
    Unknown(u32),
}

/// Contains the information describing a player
#[derive(Debug, Clone, Serialize)]
pub struct OnArenaStateReceivedPlayer {
    /// The username of this player
    pub username: String,
    /// The player's clan
    pub clan: String,
    /// The player's clan DB id
    pub clan_id: i64,
    /// The color of the player's clan tag as an RGB integer
    pub clan_color: i64,
    /// The player's DB ID (unique player ID)
    pub db_id: i64,
    /// The realm this player belongs to
    pub realm: String,
    /// Their avatar ID in the game
    pub avatar_id: i64,
    /// Their ship ID in the game
    pub meta_ship_id: i64,
    /// This player's entity created by a CreateEntity packet
    pub entity_id: i64,
    //playeravatarid: i64,
    /// Which team they're on.
    pub team_id: i64,
    /// Division ID
    pub prebattle_id: i64,
    /// Their starting health
    pub max_health: i64,
    /// ????
    pub is_abuser: bool,
    /// Has hidden stats
    pub is_hidden: bool,
    /// Has the client loaded into the game
    pub is_client_loaded: bool,
    /// Is the client connected into the game
    pub is_connected: bool,

    /// 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)
    pub raw: HashMap<i64, String>,
    pub raw_with_names: HashMap<&'static str, serde_json::Value>,
}

/// Indicates that the given attacker has dealt damage
#[derive(Debug, Clone, Serialize)]
pub struct DamageReceived {
    /// Ship ID of the aggressor
    pub aggressor: i32,
    /// 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: i32,
    /// 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,

    /// Zero is the left edge of the map, 1.0 is the right edge
    pub x: f32,

    /// Zero is the bottom edge of the map, 1.0 is the top edge
    pub y: f32,

    /// Unknown, but this appears to be something related to the big hunt
    pub unknown: bool,
}

/// Enumerates usable consumables in-game
#[derive(Debug, Clone, Copy, Serialize)]
pub enum Consumable {
    DamageControl,
    SpottingAircraft,
    DefensiveAntiAircraft,
    SpeedBoost,
    RepairParty,
    CatapultFighter,
    MainBatteryReloadBooster,
    TorpedoReloadBooster,
    Smoke,
    Radar,
    HydroacousticSearch,
    Hydrophone,
    EnhancedRudders,
    ReserveBattery,
    Unknown(i8),
}

#[derive(Debug, Clone, Copy, Serialize)]
pub enum CameraMode {
    OverheadMap,
    FollowingShells,
    FollowingPlanes,
    FollowingShip,
    FollowingSubmarine,
    FreeFlying,
    Unknown(u32),
}

/// 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: i64,
    player_name: String,
}

#[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: u32, // TODO: Is entity ID different than sender ID?
        /// Avatar ID of the sender
        sender_id: i32,
        /// 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: i32,
        /// 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),
    /// Indicates updating a damage statistic. The first tuple, `(i64,i64)`, is a two-part
    /// label indicating what type of damage this refers to. The second tuple, `(i64,f64)`,
    /// indicates the actual damage counter increment.
    ///
    /// Some known keys include:
    /// - (1, 0) key is (# AP hits that dealt damage, total AP damage dealt)
    /// - (1, 3) is (# artillery fired, total possible damage) ?
    /// - (2, 0) is (# HE penetrations, total HE damage)
    /// - (17, 0) is (# fire tick marks, total fire damage)
    DamageStat(Vec<((i64, i64), (i64, f64))>),
    /// Sent when a ship is destroyed.
    ShipDestroyed {
        /// The ship ID (note: Not the avatar ID) of the killer
        killer: i32,
        /// The ship ID (note: Not the avatar ID) of the victim
        victim: i32,
        /// Cause of death
        cause: 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
        arg0: i64,
        /// Unknown
        arg1: i8,
        /// Unknown
        arg2: HashMap<i64, Vec<Option<HashMap<String, String>>>>,
        /// A list of the players in this game
        players: Vec<OnArenaStateReceivedPlayer>,
    },
    CheckPing(u64),
    /// Indicates that the given victim has received damage from one or more attackers.
    DamageReceived {
        /// Ship ID of the ship being damaged
        victim: u32,
        /// 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>,
        /// Unknown
        // TODO: Probably how the game was won? (time expired, score, or ships destroyed)
        state: Option<u8>,
    },
    /// Sent when a consumable is activated
    Consumable {
        /// The ship ID of the ship using the consumable
        entity: u32,
        /// The consumable
        consumable: Consumable,
        /// How long the consumable will be active for
        duration: f32,
    },
    /// 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(CameraMode),
    /// If true, indicates that the player has enabled the "free look" camera (by holding right click)
    CameraFreeLook(bool),
    /// 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) {
                pickled::value::HashableValue::String(s.to_owned())
            } else {
                pickled::value::HashableValue::Bytes(b)
            }
        }
        pickled::value::HashableValue::Tuple(t) => pickled::value::HashableValue::Tuple(
            t.into_iter()
                .map(|item| try_convert_hashable_pickle_to_string(item))
                .collect(),
        ),
        pickled::value::HashableValue::FrozenSet(s) => pickled::value::HashableValue::FrozenSet(
            s.into_iter()
                .map(|item| try_convert_hashable_pickle_to_string(item))
                .collect(),
        ),
        value => value,
    }
}

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) {
                pickled::value::Value::String(s.to_owned())
            } else {
                pickled::value::Value::Bytes(b)
            }
        }
        pickled::value::Value::List(l) => pickled::value::Value::List(
            l.into_iter()
                .map(|item| try_convert_pickle_to_string(item))
                .collect(),
        ),
        pickled::value::Value::Tuple(t) => pickled::value::Value::Tuple(
            t.into_iter()
                .map(|item| try_convert_pickle_to_string(item))
                .collect(),
        ),
        pickled::value::Value::Set(s) => pickled::value::Value::Set(
            s.into_iter()
                .map(|item| try_convert_hashable_pickle_to_string(item))
                .collect(),
        ),
        pickled::value::Value::FrozenSet(s) => pickled::value::Value::FrozenSet(
            s.into_iter()
                .map(|item| try_convert_hashable_pickle_to_string(item))
                .collect(),
        ),
        pickled::value::Value::Dict(d) => pickled::value::Value::Dict(
            d.into_iter()
                .map(|(k, v)| {
                    (
                        try_convert_hashable_pickle_to_string(k),
                        try_convert_pickle_to_string(v),
                    )
                })
                .collect(),
        ),
        value => value,
    }
}

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

    // if !matches!(line, 2 | 13 | 16 | 15 | 19) {
    //     panic!("{:#X?}", blob);
    // }

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

    Ok((i, (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,
    ) -> Self {
        match payload {
            PacketType::EntityMethod(ref em) => {
                DecodedPacketPayload::from_entity_method(version, audit, em)
            }
            PacketType::Camera(camera) => DecodedPacketPayload::Camera(camera),
            PacketType::CameraMode(mode) => match mode {
                3 => DecodedPacketPayload::CameraMode(CameraMode::OverheadMap),
                5 => DecodedPacketPayload::CameraMode(CameraMode::FollowingShells),
                6 => DecodedPacketPayload::CameraMode(CameraMode::FollowingPlanes),
                8 => DecodedPacketPayload::CameraMode(CameraMode::FollowingShip),
                9 => DecodedPacketPayload::CameraMode(CameraMode::FreeFlying),
                11 => DecodedPacketPayload::CameraMode(CameraMode::FollowingSubmarine),
                _ => {
                    if audit {
                        DecodedPacketPayload::Audit(format!("CameraMode({})", mode))
                    } else {
                        DecodedPacketPayload::CameraMode(CameraMode::Unknown(*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) => {
                if packet_type == 0x18 {
                    if audit
                        && u != &[
                            00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
                            00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
                            00, 00, 00, 00, 00, 00, 0x80, 0xbf, 00, 00, 0x80, 0xbf, 00, 00, 0x80,
                            0xbf,
                        ]
                    {
                        DecodedPacketPayload::Audit(format!("Camera18 unexpected value!"))
                    } else {
                        DecodedPacketPayload::Unknown(&u)
                    }
                } else {
                    DecodedPacketPayload::Unknown(&u)
                }
            }
            PacketType::Invalid(u) => DecodedPacketPayload::Invalid(&u),
            PacketType::BattleResults(results) => DecodedPacketPayload::BattleResults(results),
        }
    }

    fn from_entity_method(
        version: &Version,
        audit: bool,
        packet: &'rawpacket EntityMethodPacket<'argtype>,
    ) -> 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")
                        .into_iter()
                        .map(|(key, value)| {
                            let key = match key {
                                pickled::HashableValue::Bytes(bytes) => String::from_utf8(bytes)
                                    .expect("key is not a valid utf-8 sequence"),
                                pickled::HashableValue::String(string) => string,
                                other => {
                                    panic!("unexpected key type {:?}", other)
                                }
                            };

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

                            (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"),
                    typ: extra_dict
                        .remove("type")
                        .unwrap()
                        .i64()
                        .expect("type is not an i64"),
                    player_avatar_id: 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"),
                };

                assert!(extra_dict.is_empty());

                extra_data = Some(extra);
            }
            DecodedPacketPayload::Chat {
                entity_id: *entity_id,
                sender_id: *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 (_reminader, (message_type, is_global)) =
                        parse_receive_common_cmd_blob(blob.as_ref())
                            .expect("receive_CommonCMD: failed to parse blob");

                    (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 as u64),
                        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,
                        _ => {
                            panic!("Unknown voice line {} a={:x} b={:x}!", line, a, b);
                        }
                    };

                    (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,
                is_global,
                message,
            }
        } 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.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
                    .iter()
                    .map(|elem| match elem {
                        pickled::value::Value::Dict(d) => Some(
                            d.iter()
                                .map(|(k, v)| {
                                    let k = match k {
                                        pickled::value::HashableValue::Bytes(b) => {
                                            std::str::from_utf8(b).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.iter() {
                    let mut values = HashMap::new();
                    if let pickled::value::Value::List(elements) = player {
                        for elem in elements.iter() {
                            if let pickled::value::Value::Tuple(kv) = elem {
                                let key = match kv[0] {
                                    pickled::value::Value::I64(key) => key,
                                    _ => panic!(),
                                };
                                values.insert(key, kv[1].clone());
                            }
                        }
                    }

                    let keys: HashMap<&'static str, i64> =
                        if version.is_at_least(&Version::from_client_exe("0,12,8,0")) {
                            let mut h = HashMap::new();
                            h.insert("accountDBID", 0);
                            h.insert("antiAbuseEnabled", 1);
                            h.insert("avatarId", 2);
                            h.insert("camouflageInfo", 3);
                            h.insert("clanColor", 4);
                            h.insert("clanID", 5);
                            h.insert("clanTag", 6);
                            h.insert("crewParams", 7);
                            h.insert("dogTag", 8);
                            h.insert("fragsCount", 9);
                            h.insert("friendlyFireEnabled", 10);
                            h.insert("id", 11);
                            h.insert("invitationsEnabled", 12);
                            h.insert("isAbuser", 13);
                            h.insert("isAlive", 14);
                            h.insert("isBot", 15);
                            h.insert("isClientLoaded", 16);
                            h.insert("isConnected", 17);
                            h.insert("isHidden", 18);
                            h.insert("isLeaver", 19);
                            h.insert("isPreBattleOwner", 20);
                            h.insert("isTShooter", 21);
                            h.insert("keyTargetMarkers", 22);
                            h.insert("killedBuildingsCount", 23);
                            h.insert("maxHealth", 24);
                            h.insert("name", 25);
                            h.insert("playerMode", 26);
                            h.insert("preBattleIdOnStart", 27);
                            h.insert("preBattleSign", 28);
                            h.insert("prebattleId", 29);
                            h.insert("realm", 30);
                            h.insert("shipComponents", 31);
                            h.insert("shipConfigDump", 32);
                            h.insert("shipId", 33);
                            h.insert("shipParamsId", 34);
                            h.insert("skinId", 35);
                            h.insert("teamId", 36);
                            h.insert("ttkStatus", 37);
                            h
                        } else if version.is_at_least(&Version::from_client_exe("0,10,9,0")) {
                            // 0.10.9 inserted things at 0x1 and 0x1F
                            let mut h = HashMap::new();
                            h.insert("avatarId", 0x2);
                            h.insert("clanTag", 0x6);
                            h.insert("maxHealth", 0x17);
                            h.insert("name", 0x18);
                            h.insert("shipId", 0x20);
                            h.insert("shipParamsId", 0x21);
                            h.insert("skinId", 0x22);
                            h.insert("teamId", 0x23);
                            h
                        } else if version.is_at_least(&Version::from_client_exe("0,10,7,0")) {
                            // 0.10.7
                            let mut h = HashMap::new();
                            h.insert("avatarId", 0x1);
                            h.insert("clanTag", 0x5);
                            h.insert("maxHealth", 0x16);
                            h.insert("name", 0x17);
                            h.insert("shipId", 0x1e);
                            h.insert("shipParamsId", 0x1f);
                            h.insert("skinId", 0x20);
                            h.insert("teamId", 0x21);
                            h
                        } else {
                            // 0.10.6 and earlier
                            let mut h = HashMap::new();
                            h.insert("avatarId", 0x1);
                            h.insert("clanTag", 0x5);
                            h.insert("maxHealth", 0x15);
                            h.insert("name", 0x16);
                            h.insert("shipId", 0x1d);
                            h.insert("shipParamsId", 0x1e);
                            h.insert("skinId", 0x1f);
                            h.insert("teamId", 0x20);
                            h
                        };

                    /*
                    1: Player ID
                    5: Clan name
                    16: Username
                    1c: Equipped equipment (?)
                    1d: Ship/hull ID? (1 more than player ID)
                    1e: Player ship ID
                    1f: Player ship ID (why does this appear twice?)
                    */
                    let avatar = *values
                        .get(keys.get("avatarId").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("avatarId is not an i64");

                    let username = values
                        .get(keys.get("name").unwrap())
                        .unwrap()
                        .string_ref()
                        .expect("name is not a string")
                        .clone();

                    let clan = values
                        .get(keys.get("clanTag").unwrap())
                        .unwrap()
                        .string_ref()
                        .expect("clanTag is not a string")
                        .clone();

                    let clan_id = values
                        .get(keys.get("clanID").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("clanID is not an i64")
                        .clone();

                    let shipid = *values
                        .get(keys.get("shipId").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("shipId is not an i64");
                    let meta_ship_id = *values
                        .get(keys.get("id").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("shipId is not an i64");
                    let playerid = *values
                        .get(keys.get("shipParamsId").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("shipParamsId is not an i64");
                    let _playeravatarid = *values
                        .get(keys.get("skinId").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("skinId is not an i64");
                    let team = *values
                        .get(keys.get("teamId").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("teamId is not an i64");
                    let health = *values
                        .get(keys.get("maxHealth").unwrap())
                        .unwrap()
                        .i64_ref()
                        .expect("maxHealth is not an i64");

                    let realm = values
                        .get(keys.get("realm").unwrap())
                        .unwrap()
                        .string_ref()
                        .cloned()
                        .expect("maxHealth is not an i64");

                    let db_id = values
                        .get(keys.get("accountDBID").unwrap())
                        .unwrap()
                        .i64_ref()
                        .cloned()
                        .expect("accountDBID is not an i64");

                    let prebattle_id = values
                        .get(keys.get("prebattleId").unwrap())
                        .unwrap()
                        .i64_ref()
                        .cloned()
                        .expect("accountDBID is not an i64");

                    let anti_abuse_enabled = values
                        .get(keys.get("antiAbuseEnabled").unwrap())
                        .unwrap()
                        .bool_ref()
                        .cloned()
                        .expect("antiAbuseEnabled is not a bool");

                    let is_abuser = values
                        .get(keys.get("isAbuser").unwrap())
                        .unwrap()
                        .bool_ref()
                        .cloned()
                        .expect("isAbuser is not a bool");

                    let is_hidden = values
                        .get(keys.get("isHidden").unwrap())
                        .unwrap()
                        .bool_ref()
                        .cloned()
                        .expect("isHidden is not a bool");

                    let is_connected = values
                        .get(keys.get("isConnected").unwrap())
                        .unwrap()
                        .bool_ref()
                        .cloned()
                        .expect("isConnected is not a bool");

                    let is_client_loaded = values
                        .get(keys.get("isClientLoaded").unwrap())
                        .unwrap()
                        .bool_ref()
                        .cloned()
                        .expect("isClientLoaded is not a bool");

                    let clan_color = values
                        .get(keys.get("clanColor").unwrap())
                        .unwrap()
                        .i64_ref()
                        .cloned()
                        .expect("clanColor is not an integer");

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

                    let mut raw_with_names = HashMap::new();
                    for (k, v) in values.iter() {
                        for (name, idx) in &keys {
                            if *k == *idx {
                                raw_with_names.insert(
                                    *name,
                                    wowsunpack::game_params::convert::pickle_to_json(v.clone()),
                                );
                            }
                        }
                    }

                    players_out.push(OnArenaStateReceivedPlayer {
                        username,
                        clan,
                        clan_id,
                        clan_color,
                        realm,
                        db_id,
                        avatar_id: avatar,
                        meta_ship_id,
                        entity_id: shipid,
                        team_id: team,
                        max_health: health,
                        is_abuser,
                        is_hidden,
                        raw,
                        is_connected,
                        is_client_loaded,
                        raw_with_names,
                        prebattle_id,
                    });
                }
            }
            DecodedPacketPayload::OnArenaStateReceived {
                arg0,
                arg1,
                arg2,
                players: players_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.iter() {
                        let k = match k {
                            pickled::value::HashableValue::Tuple(t) => {
                                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 v = match v {
                            pickled::value::Value::List(t) => {
                                assert!(t.len() == 2);
                                (
                                    match t[0] {
                                        pickled::value::Value::I64(i) => i,
                                        _ => panic!("foo"),
                                    },
                                    match t[1] {
                                        pickled::value::Value::F64(i) => i,
                                        // TODO: This appears in the (17,2) key,
                                        // it is unknown what it means
                                        pickled::value::Value::I64(i) => i as f64,
                                        _ => panic!("foo"),
                                    },
                                )
                            }
                            _ => panic!("foo"),
                        };
                        //println!("{:?}: {:?}", k, v);

                        stats.push((k, v));
                    }
                }
                _ => panic!("foo"),
            }
            DecodedPacketPayload::DamageStat(stats)
        } else if *method == "receiveVehicleDeath" {
            let (victim, killer, cause) = unpack_rpc_args!(args, i32, i32, u32);
            let cause = match cause {
                2 => DeathCause::Secondaries,
                3 => DeathCause::Torpedo,
                4 => DeathCause::DiveBomber,
                5 => DeathCause::AerialTorpedo,
                6 => DeathCause::Fire,
                7 => DeathCause::Ramming,
                9 => DeathCause::Flooding,
                13 => DeathCause::DepthCharge,
                14 => DeathCause::AerialRocket,
                15 => DeathCause::Detonation,
                17 => DeathCause::Artillery,
                18 => DeathCause::Artillery,
                19 => DeathCause::Artillery,
                22 => DeathCause::SkipBombs,
                28 => DeathCause::DepthCharge, // TODO: Why is this different from the above depth charge?
                cause => {
                    if audit {
                        return DecodedPacketPayload::Audit(format!(
                            "receiveVehicleDeath(victim={}, killer={}, unknown cause {})",
                            victim, killer, cause
                        ));
                    } else {
                        DeathCause::Unknown(cause)
                    }
                }
            };
            DecodedPacketPayload::ShipDestroyed {
                victim,
                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!(),
                };
                v.push(DamageReceived {
                    aggressor: map.get("vehicleID").unwrap().try_into().unwrap(),
                    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.;

                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) => *u as i32,
                        _ => panic!(),
                    },
                    x,
                    y,
                    heading,
                    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, state) =
                if version.is_at_least(&Version::from_client_exe("0,12,8,0")) {
                    (None, None)
                } else {
                    let (winning_team, unknown) = unpack_rpc_args!(args, i8, u8);
                    (Some(winning_team), Some(unknown))
                };
            DecodedPacketPayload::BattleEnd {
                winning_team,
                state,
            }
        } else if *method == "consumableUsed" {
            let (consumable, duration) = unpack_rpc_args!(args, i8, f32);
            let raw_consumable = consumable;
            let consumable = match consumable {
                0 => Consumable::DamageControl,
                1 => Consumable::SpottingAircraft,
                2 => Consumable::DefensiveAntiAircraft,
                3 => Consumable::SpeedBoost,
                5 => Consumable::MainBatteryReloadBooster,
                7 => Consumable::Smoke,
                9 => Consumable::RepairParty,
                10 => Consumable::CatapultFighter,
                11 => Consumable::HydroacousticSearch,
                12 => Consumable::TorpedoReloadBooster,
                13 => Consumable::Radar,
                35 => Consumable::Hydrophone,
                36 => Consumable::EnhancedRudders,
                37 => Consumable::ReserveBattery,
                _ => {
                    if audit {
                        return DecodedPacketPayload::Audit(format!(
                            "consumableUsed({},{},{})",
                            entity_id, raw_consumable, duration
                        ));
                    } else {
                        Consumable::Unknown(consumable)
                    }
                }
            };
            DecodedPacketPayload::Consumable {
                entity: *entity_id,
                consumable: consumable,
                duration: duration,
            }
        } else {
            DecodedPacketPayload::EntityMethod(packet)
        }
    }
}

#[derive(Debug, Serialize)]
pub struct DecodedPacket<'replay, 'argtype, 'rawpacket> {
    pub packet_type: u32,
    pub clock: f32,
    pub payload: DecodedPacketPayload<'replay, 'argtype, 'rawpacket>,
}

impl<'replay, 'argtype, 'rawpacket> DecodedPacket<'replay, 'argtype, 'rawpacket>
where
    'rawpacket: 'replay,
    'rawpacket: 'argtype,
{
    pub fn from(version: &Version, audit: bool, packet: &'rawpacket Packet<'_, '_>) -> Self {
        let decoded = Self {
            clock: packet.clock,
            packet_type: packet.packet_type,
            payload: DecodedPacketPayload::from(
                version,
                audit,
                &packet.payload,
                packet.packet_type,
            ),
        };
        decoded
    }
}

struct Decoder {
    silent: bool,
    output: Option<Box<dyn std::io::Write>>,
    version: Version,
}

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);
                }
            }
        }
    }
}

#[bitfield]
struct RawMinimapUpdate {
    x: B11,
    y: B11,
    heading: B8,
    unknown: bool,
    is_disappearing: bool,
}

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

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