sf-api 0.4.1

A simple API to send commands to the Shakes & Fidget servers and parse their responses into characters
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
use std::cmp::Ordering;

use chrono::{DateTime, Local};
use enum_map::{Enum, EnumMap};
use log::warn;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use strum::{EnumCount, EnumIter};

use super::{
    CFPGet, Class, EnumMapGet, HabitatType, SFError, ServerTime,
    unlockables::EquipmentIdent,
};
use crate::{
    command::{AttributeType, ShopType},
    gamestate::{CCGet, CGet, ShopPosition},
};

/// The basic inventory, that every player has
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Inventory {
    pub backpack: Vec<Option<Item>>,
}

/// The game keeps track between 5 slot bag and the extended inventory.
#[derive(Debug, Default, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BagPosition(pub(crate) usize);

impl BagPosition {
    /// The 0 based index into the backpack vec, where the item is parsed into
    #[must_use]
    pub fn backpack_pos(&self) -> usize {
        self.0
    }
    /// The inventory type and position within it, where the item is stored
    /// according to previous inventory management logic. This is what you use
    /// for commands
    #[must_use]
    pub fn inventory_pos(&self) -> (InventoryType, usize) {
        let pos = self.0;
        if pos <= 4 {
            (InventoryType::MainInventory, pos)
        } else {
            (InventoryType::ExtendedInventory, pos - 5)
        }
    }
}

impl Inventory {
    // Splits the backpack, as if it was the old bag/fortress chest layout.
    // The first slice will be the bag, the second the fortress chest.
    // If the backback if empty for unknown reasons, or is shorter than 5
    // elements, both slices will be empty
    #[must_use]
    pub fn as_split(&self) -> (&[Option<Item>], &[Option<Item>]) {
        if self.backpack.len() < 5 {
            return (&[], &[]);
        }
        self.backpack.split_at(5)
    }

    // Splits the backpack, as if it was the old bag/fortress chest layout.
    // The first slice will be the bag, the second the fortress chest
    // If the backback if empty for unknown reasons, or is shorter than 5
    // elements, both slices will be emptys
    #[must_use]
    pub fn as_split_mut(
        &mut self,
    ) -> (&mut [Option<Item>], &mut [Option<Item>]) {
        if self.backpack.len() < 5 {
            return (&mut [], &mut []);
        }
        self.backpack.split_at_mut(5)
    }

    /// Returns a place in the inventory, that can store a new item.
    /// This is only useful, when you are dealing with commands, that require
    /// a free slot position. The index will be 0 based per inventory
    #[must_use]
    pub fn free_slot(&self) -> Option<BagPosition> {
        for (pos, item) in self.iter() {
            if item.is_none() {
                return Some(pos);
            }
        }
        None
    }

    #[must_use]
    pub fn count_free_slots(&self) -> usize {
        self.backpack.iter().filter(|slot| slot.is_none()).count()
    }

    /// Creates an iterator over the inventory slots.
    pub fn iter(&self) -> impl Iterator<Item = (BagPosition, Option<&Item>)> {
        self.backpack
            .iter()
            .enumerate()
            .map(|(pos, item)| (BagPosition(pos), item.as_ref()))
    }
}

/// All the parts of `ItemPlace`, that are owned by the player
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum PlayerItemPlace {
    Equipment = 1,
    MainInventory = 2,
    ExtendedInventory = 5,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ItemPosition {
    pub place: ItemPlace,
    pub position: usize,
}

impl std::fmt::Display for ItemPosition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}/{}", self.place as usize, self.position + 1)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PlayerItemPosition {
    pub place: PlayerItemPlace,
    pub position: usize,
}

impl std::fmt::Display for PlayerItemPosition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}/{}", self.place as usize, self.position + 1)
    }
}

impl From<PlayerItemPosition> for ItemPosition {
    fn from(value: PlayerItemPosition) -> Self {
        Self {
            place: value.place.item_position(),
            position: value.position,
        }
    }
}

impl From<BagPosition> for ItemPosition {
    fn from(value: BagPosition) -> Self {
        let player: PlayerItemPosition = value.into();
        player.into()
    }
}

impl From<EquipmentSlot> for ItemPosition {
    fn from(value: EquipmentSlot) -> Self {
        let player: PlayerItemPosition = value.into();
        player.into()
    }
}

impl From<ShopPosition> for ItemPosition {
    fn from(value: ShopPosition) -> Self {
        Self {
            place: value.typ.into(),
            position: value.pos,
        }
    }
}

impl From<ShopType> for ItemPlace {
    fn from(value: ShopType) -> Self {
        match value {
            ShopType::Weapon => ItemPlace::WeaponShop,
            ShopType::Magic => ItemPlace::MageShop,
        }
    }
}

impl From<BagPosition> for PlayerItemPosition {
    fn from(value: BagPosition) -> Self {
        let p = value.inventory_pos();
        Self {
            place: p.0.player_item_position(),
            position: p.1,
        }
    }
}

impl From<EquipmentSlot> for PlayerItemPosition {
    fn from(value: EquipmentSlot) -> Self {
        Self {
            place: PlayerItemPlace::Equipment,
            position: value as usize - 1,
        }
    }
}

impl PlayerItemPlace {
    /// `InventoryType` is a subset of `ItemPlace`. This is a convenient
    /// function to convert between them
    #[must_use]
    pub fn item_position(&self) -> ItemPlace {
        match self {
            PlayerItemPlace::Equipment => ItemPlace::Equipment,
            PlayerItemPlace::MainInventory => ItemPlace::MainInventory,
            PlayerItemPlace::ExtendedInventory => ItemPlace::FortressChest,
        }
    }
}

/// All the parts of `ItemPlace`, that are owned by the player
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum InventoryType {
    MainInventory = 2,
    ExtendedInventory = 5,
}

impl InventoryType {
    /// `InventoryType` is a subset of `ItemPlace`. This is a convenient
    /// function to convert between them
    #[must_use]
    pub fn item_position(&self) -> ItemPlace {
        match self {
            InventoryType::MainInventory => ItemPlace::MainInventory,
            InventoryType::ExtendedInventory => ItemPlace::FortressChest,
        }
    }
    /// `InventoryType` is a subset of `ItemPlace`. This is a convenient
    /// function to convert between them
    #[must_use]
    pub fn player_item_position(&self) -> PlayerItemPlace {
        match self {
            InventoryType::MainInventory => PlayerItemPlace::MainInventory,
            InventoryType::ExtendedInventory => {
                PlayerItemPlace::ExtendedInventory
            }
        }
    }
}

/// All places, that items can be dragged to excluding companions
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ItemPlace {
    /// The stuff a player can wear
    Equipment = 1,
    /// All items in the main 5 inventory slots
    MainInventory = 2,
    /// The items in the weapon slot
    WeaponShop = 3,
    /// The items in the mage slot
    MageShop = 4,
    /// The items in the fortress chest slots
    FortressChest = 5,
}

/// All the equipment a player is wearing
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Equipment(pub EnumMap<EquipmentSlot, Option<Item>>);

impl Equipment {
    /// Checks if the character has an item with the enchantment equipped
    #[must_use]
    pub fn has_enchantment(&self, enchantment: Enchantment) -> bool {
        let item = self.0.get(enchantment.equipment_slot());
        if let Some(item) = item {
            return item.enchantment == Some(enchantment);
        }
        false
    }

    /// Expects the input `data` to have items directly at data[0]
    #[allow(clippy::indexing_slicing)]
    pub(crate) fn parse(
        data: &[i64],
        server_time: ServerTime,
    ) -> Result<Equipment, SFError> {
        let mut res = Equipment::default();
        if !data.len().is_multiple_of(ITEM_PARSE_LEN) {
            return Err(SFError::ParsingError(
                "Invalid Equipment",
                format!("{data:?}"),
            ));
        }
        for (chunk, slot) in
            data.chunks_exact(ITEM_PARSE_LEN).zip(res.0.as_mut_slice())
        {
            *slot = Item::parse(chunk, server_time)?;
        }
        Ok(res)
    }
}

pub(crate) const ITEM_PARSE_LEN: usize = 19;

/// Information about a single item. This can be anything, that is either in a
/// inventory, in a reward slot, or similar
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Item {
    /// The type of this item. May contain further type specific values
    pub typ: ItemType,
    /// Either the price to buy, or sell
    pub price: u32,
    /// The price you would have to pay for this item. Note that this value is
    /// junk for other players and potentially in other cases, where you should
    /// not be able to see a price
    pub mushroom_price: u32,
    /// The non-truncated version of the model id. The normal `model_id` is
    /// fine to identify this item visually, but this here is for doing more
    /// specific calculations, apart from that
    pub full_model_id: u32,
    /// The model id of this item
    pub model_id: u16,
    /// The class restriction, that this item has. Will only cover the three
    /// main classes
    pub class: Option<Class>,
    /// Either the armor, weapon dmg, or other. You should be using `armor()`,
    /// or the weapon types damages though, if you want to have a safe
    /// abstraction. This is only public in case I am missing a case here
    pub type_specific_val: u32,
    /// The stats this item gives, when equipped
    pub attributes: EnumMap<AttributeType, u32>,
    /// The gemslot of this item, if any. A gemslot can be filled or empty
    pub gem_slot: Option<GemSlot>,
    /// The rune on this item
    pub rune: Option<Rune>,
    /// The enchantment applied to this item
    pub enchantment: Option<Enchantment>,
    /// This is the color, or other cosmetic variation of an item. There is no
    /// clear 1 => red mapping, so only the raw value here
    pub color: u8,
    /// The amount of times this item has been upgraded at the blacksmith
    pub upgrade_count: u8,
    /// The quality level of this item
    pub item_quality: u32,
    /// Has this item been through the washing cycle?
    pub is_washed: bool,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ItemCommandIdent {
    typ: u8,
    full_model_id: u32,
    price: u32,
    mush_price: u32,
}

impl std::fmt::Display for ItemCommandIdent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}/{}/{}/{}",
            self.typ, self.full_model_id, self.price, self.mush_price
        )
    }
}

#[derive(Debug, Clone, Copy)]
pub struct BlacksmithPayment {
    pub metal: u64,
    pub arcane: u64,
}

impl Item {
    /// Calculates the amount of metal & arcane we are expected to receive from
    /// the blacksmith
    ///
    /// This code is a direct port of the implementation available here:
    /// <https://snfsmithsim.12hp.de>/ . As such, all credit goes to:
    /// `ÐonMuErte`, `Werwolf Legion (F17)` & `Rising Phoenix (F21)`
    #[must_use]
    pub fn dismantle_reward(&self) -> BlacksmithPayment {
        let mut attribute_val =
            f64::from(*self.attributes.values().max().unwrap_or(&0));
        let item_stats = self.attributes.values().filter(|a| **a > 0).count();
        let is_scout_or_mage_weapon = self
            .class
            .is_some_and(|a| a == Class::Scout || a == Class::Mage)
            && self.typ.is_weapon();

        if self.price != 0 {
            for _ in 0..self.upgrade_count {
                attribute_val = (attribute_val / 1.04).round();
            }
        }

        if item_stats >= 4 {
            attribute_val *= 1.2;
        }
        if is_scout_or_mage_weapon {
            attribute_val /= 2.0;
        }
        // // 1-stat items
        if (item_stats == 1) && attribute_val > 66.0 {
            attribute_val = attribute_val.round() * 0.75;
        }

        attribute_val = attribute_val.round().powf(1.2).floor();

        let (min_dmg, max_dmg) = match self.typ {
            ItemType::Weapon { min_dmg, max_dmg } => (min_dmg, max_dmg),
            _ => (0, 0),
        };

        let price = (u32::from(self.typ.raw_id()) * 37)
            + (self.full_model_id * 83)
            + (min_dmg * 1731)
            + (max_dmg * 162);

        let (metal_price, arcane_price) = match item_stats {
            1 => (75 + (price % 26), price % 2),
            2 => (50 + (price % 31), 5 + (price % 6)),
            // Epics
            _ => (25 + (price % 26), 50 + (price % 51)),
        };

        #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
        let calc_result = |rng: u32| {
            ((attribute_val * f64::from(rng)) / 100.0).floor() as u64
        };
        let mut metal_result = calc_result(metal_price);
        let mut arcane_result = calc_result(arcane_price);

        if is_scout_or_mage_weapon {
            metal_result *= 2;
            arcane_result *= 2;
        }
        BlacksmithPayment {
            metal: metal_result * 2,
            arcane: arcane_result * 2,
        }
    }

    /// Calculates the amount of metal & arcane it would cost to upgrade this
    /// item. Each upgrade increases the highest attribute by 3% (all highest
    /// for epics)
    ///
    /// This code is a direct port of the implementation available here:
    /// <https://snfsmithsim.12hp.de>/ . As such, all credit goes to:
    /// `ÐonMuErte`, `Werwolf Legion (F17)` & `Rising Phoenix (F21)`
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
    pub fn upgrade_costs(&self) -> Option<BlacksmithPayment> {
        if self.upgrade_count >= 20 || self.equipment_ident().is_none() {
            return None;
        }

        let item_stats = self.attributes.values().filter(|a| **a > 0).count();
        let is_scout_or_mage_weapon = self
            .class
            .is_some_and(|a| a == Class::Scout || a == Class::Mage)
            && self.typ.is_weapon();

        // Highest attribue is the base price
        let mut price =
            f64::from(*self.attributes.values().max().unwrap_or(&0));

        // 5-stats items
        if item_stats >= 4 {
            price *= 1.2;
        }

        if is_scout_or_mage_weapon {
            price /= 2.0;
        }

        // 1-stat items
        if item_stats == 1 && price > 66.0 {
            price = (price * 0.75).ceil();
        }

        price = price.round().powf(1.2).floor();

        let mut metal_price = 50;
        let mut arcane_price = match item_stats {
            1 => 25,
            2 => 50,
            // Epics
            _ => 75,
        };

        let i = i64::from(self.upgrade_count);
        match i {
            0 => {
                metal_price *= 3;
                arcane_price = 0;
            }
            1 => {
                metal_price *= 4;
                arcane_price = 1;
            }
            2..=7 => {
                metal_price *= i + 3;
                arcane_price *= i - 1;
            }
            8 => {
                metal_price *= 12;
                arcane_price *= 8;
            }
            9 => {
                metal_price *= 15;
                arcane_price *= 10;
            }
            _ => {
                metal_price *= i + 6;
                arcane_price *= 10 + 2 * (i - 9);
            }
        }

        metal_price = ((price * (metal_price as f64)) / 100.0).floor() as i64;
        arcane_price = ((price * (arcane_price as f64)) / 100.0).floor() as i64;

        if is_scout_or_mage_weapon {
            metal_price *= 2;
            arcane_price *= 2;
        }

        Some(BlacksmithPayment {
            metal: metal_price.try_into().unwrap_or(0),
            arcane: arcane_price.try_into().unwrap_or(0),
        })
    }

    /// Maps an item to its ident. This is mainly useful, if you want to see,
    /// if a item is already in your scrapbook
    #[must_use]
    pub fn equipment_ident(&self) -> Option<EquipmentIdent> {
        Some(EquipmentIdent {
            class: self.class,
            typ: self.typ.equipment_slot()?,
            model_id: self.model_id,
            color: self.color,
        })
    }

    /// Commands require an ident for the source ident now. Most likely to make
    /// sure the item has not changed, which could be the case in the shop.
    /// This function produces the required identification for an item
    #[must_use]
    pub fn command_ident(&self) -> ItemCommandIdent {
        ItemCommandIdent {
            typ: self.typ.raw_id(),
            full_model_id: self.full_model_id,
            price: self.price,
            mush_price: self.mushroom_price,
        }
    }

    /// Checks, if this item is unique. Technically they are not always unique,
    /// as the scrapbook/keys can be sold, but it should be clear what this is
    #[must_use]
    pub fn is_unique(&self) -> bool {
        self.typ.is_unique()
    }

    /// Checks if this item is an epic
    #[must_use]
    pub fn is_epic(&self) -> bool {
        self.model_id >= 50
    }

    /// Checks if this item is a legendary
    #[must_use]
    pub fn is_legendary(&self) -> bool {
        self.model_id >= 90
    }

    /// The armor rating of this item. This is just the `effect_val`, if any
    #[must_use]
    pub fn armor(&self) -> u32 {
        #[allow(clippy::enum_glob_use)]
        use ItemType::*;
        match self.typ {
            Hat | BreastPlate | Gloves | FootWear | Amulet | Belt | Ring
            | Talisman => self.type_specific_val,
            _ => 0,
        }
    }

    /// Checks, if this item can be enchanted
    #[must_use]
    pub fn is_enchantable(&self) -> bool {
        self.typ.is_enchantable()
    }

    /// Checks if a companion of the given class can equip this item.
    ///
    /// Returns `true` if the item itself is equipment and this class has the
    /// ability to wear it
    #[must_use]
    pub fn can_be_equipped_by_companion(
        &self,
        class: impl Into<Class>,
    ) -> bool {
        !self.typ.is_shield() && self.can_be_equipped_by(class.into())
    }

    /// Checks if a character of the given class can equip this item. Note that
    /// this only checks the class, so this will make no sense if you use this
    /// for anything that can not equip items at all (monsters, etc.). For
    /// companions you should use `can_companion_equip`
    ///
    /// Returns `true` if the item itself is equipment and this class has the
    /// ability to wear it
    #[must_use]
    pub fn can_be_equipped_by(&self, class: Class) -> bool {
        self.typ.equipment_slot().is_some() && self.can_be_used_by(class)
    }

    /// Checks if a character of the given class can use this item. If you want
    /// to check equipment, you should use `can_be_equipped_by`
    ///
    /// Returns `true` if the item does not have a class requirement, or if the
    /// class requirement matches the given class.
    #[must_use]
    #[allow(clippy::enum_glob_use, clippy::match_same_arms)]
    pub fn can_be_used_by(&self, class: Class) -> bool {
        use Class::*;

        // Without a class requirement any class can use this
        let Some(class_requirement) = self.class else {
            return true;
        };

        match class {
            Warrior | Paladin => class_requirement == Warrior,
            Berserker => class_requirement == Warrior && !self.typ.is_shield(),
            Scout => class_requirement == Scout,
            Mage | Necromancer => class_requirement == Mage,
            Assassin => match class_requirement {
                Warrior => self.typ.is_weapon(),
                Scout => !self.typ.is_weapon(),
                _ => false,
            },
            Bard | Druid => match class_requirement {
                Mage => self.typ.is_weapon(),
                Scout => !self.typ.is_weapon(),
                _ => false,
            },
            BattleMage | PlagueDoctor => match class_requirement {
                Warrior => self.typ.is_weapon(),
                Mage => !self.typ.is_weapon(),
                _ => false,
            },
            DemonHunter => match class_requirement {
                Scout => self.typ.is_weapon(),
                Warrior => !self.typ.is_weapon() && !self.typ.is_shield(),
                _ => false,
            },
        }
    }

    /// Parses an item, that starts at the start of the given data
    pub(crate) fn parse(
        data: &[i64],
        server_time: ServerTime,
    ) -> Result<Option<Self>, SFError> {
        let Some(typ) = ItemType::parse(data, server_time)? else {
            return Ok(None);
        };

        let enchantment = data.cfpget(2, "item enchantment", |a| a)?;
        let gem_slot_val = data.cimget(1, "gem slot val", |a| a)?;
        let gem_pwr = data.cimget(16, "gem pwr", |a| a)?;

        let gem_slot = GemSlot::parse(gem_slot_val, gem_pwr);

        let class = if typ.is_class_item() {
            data.cfpget(3, "item class", |x| (x & 0xFFFF) / 1000)?
        } else {
            None
        };
        let mut rune = None;
        let mut attributes: EnumMap<AttributeType, u32> = EnumMap::default();
        let price = data.csiget(13, "item price", u32::MAX)?;

        if typ.equipment_slot().is_some() {
            for i in 0..3 {
                let atr_typ = data.cget(i + 7, "item atr typ")?;
                let Ok(atr_typ) = atr_typ.try_into() else {
                    warn!("Invalid attribute typ: {atr_typ}, {typ:?}");
                    continue;
                };
                let atr_val = data.cget(i + 10, "item atr val")?;
                let Ok(atr_val): Result<u32, _> = atr_val.try_into() else {
                    warn!("Invalid attribute value: {atr_val}, {typ:?}");
                    continue;
                };
                match atr_typ {
                    0 => {}
                    1..=5 => {
                        let Some(atr_typ) = FromPrimitive::from_usize(atr_typ)
                        else {
                            continue;
                        };
                        *attributes.get_mut(atr_typ) += atr_val;
                    }
                    6 => {
                        for atr in attributes.values_mut() {
                            *atr += atr_val;
                        }
                    }
                    21 => {
                        for atr in [
                            AttributeType::Strength,
                            AttributeType::Constitution,
                            AttributeType::Luck,
                        ] {
                            *attributes.get_mut(atr) += atr_val;
                        }
                    }
                    22 => {
                        for atr in [
                            AttributeType::Dexterity,
                            AttributeType::Constitution,
                            AttributeType::Luck,
                        ] {
                            *attributes.get_mut(atr) += atr_val;
                        }
                    }
                    23 => {
                        for atr in [
                            AttributeType::Intelligence,
                            AttributeType::Constitution,
                            AttributeType::Luck,
                        ] {
                            *attributes.get_mut(atr) += atr_val;
                        }
                    }
                    rune_typ => {
                        let Some(typ) = FromPrimitive::from_usize(rune_typ)
                        else {
                            warn!(
                                "Unhandled item val: {atr_typ} -> {atr_val} \
                                 for {class:?} {typ:?}",
                            );
                            continue;
                        };
                        let Ok(value) = atr_val.try_into() else {
                            warn!("Rune value too big for a u8: {atr_val}");
                            continue;
                        };
                        rune = Some(Rune { typ, value });
                    }
                }
            }
        }
        let model_id: u16 =
            data.cimget(3, "item model id", |x| (x & 0xFFFF) % 1000)?;

        let color = match model_id {
            ..=49 if typ != ItemType::Talisman => data
                .get(5..=12)
                .map(|a| a.iter().sum::<i64>())
                .map(|a| (a % 5) + 1)
                .and_then(|a| a.try_into().ok())
                .unwrap_or(1),
            _ => 1,
        };

        let item = Item {
            typ,
            model_id,
            rune,
            type_specific_val: data.csiget(5, "effect value", 0)?,
            gem_slot,
            enchantment,
            class,
            attributes,
            color,
            price,
            mushroom_price: data.csiget(14, "mushroom price", u32::MAX)?,
            upgrade_count: data.csiget(15, "upgrade count", u8::MAX)?,
            item_quality: data.csiget(17, "item quality", 0)?,
            is_washed: data.csiget(18, "is washed", 0)? != 0,
            full_model_id: data.csiget(3, "raw model id", 0)?,
        };
        Ok(Some(item))
    }
}

/// A enchantment, that gives a bonus to an aspect, if the item
#[derive(
    Debug, Clone, Copy, FromPrimitive, PartialEq, Eq, EnumIter, Hash, Enum,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Enchantment {
    /// Increased crit damage
    SwordOfVengeance = 11,
    /// Finds more mushrooms
    MariosBeard = 31,
    /// Shortens travel time
    ManyFeetBoots = 41,
    /// Increased reaction score in combat
    ShadowOfTheCowboy = 51,
    /// Extra XP on expeditions
    AdventurersArchaeologicalAura = 61,
    /// Allows an extra beer
    ThirstyWanderer = 71,
    /// Find items at paths edge (expeditions) more often
    UnholyAcquisitiveness = 81,
    /// Find extra gold on expeditions
    TheGraveRobbersPrayer = 91,
    /// Increase the chance of loot against other players
    RobberBaronRitual = 101,
}

impl Enchantment {
    #[must_use]
    pub fn equipment_slot(&self) -> EquipmentSlot {
        match self {
            Enchantment::SwordOfVengeance => EquipmentSlot::Weapon,
            Enchantment::MariosBeard => EquipmentSlot::BreastPlate,
            Enchantment::ManyFeetBoots => EquipmentSlot::FootWear,
            Enchantment::ShadowOfTheCowboy => EquipmentSlot::Gloves,
            Enchantment::AdventurersArchaeologicalAura => EquipmentSlot::Hat,
            Enchantment::ThirstyWanderer => EquipmentSlot::Belt,
            Enchantment::UnholyAcquisitiveness => EquipmentSlot::Amulet,
            Enchantment::TheGraveRobbersPrayer => EquipmentSlot::Ring,
            Enchantment::RobberBaronRitual => EquipmentSlot::Talisman,
        }
    }
}

/// A rune, which has both a type and a strength
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rune {
    /// The type of tune this is
    pub typ: RuneType,
    /// The "strength" of this rune. So a value like 50 here and a typ of
    /// `FireResistance` would mean 50% fire resistance
    pub value: u8,
}

#[derive(Debug, Clone, Copy, FromPrimitive, PartialEq, Eq, EnumIter, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
/// The effect of a rune
pub enum RuneType {
    QuestGold = 31,
    EpicChance,
    ItemQuality,
    QuestXP,
    ExtraHitPoints,
    FireResistance,
    ColdResistence,
    LightningResistance,
    TotalResistence,
    FireDamage,
    ColdDamage,
    LightningDamage,
}

/// A gem slot for an item
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum GemSlot {
    /// This gemslot has been filled and can only be emptied by the blacksmith
    Filled(Gem),
    /// A gem can be inserted into this item
    Empty,
}

impl GemSlot {
    pub(crate) fn parse(slot_val: i64, gem_pwr: i64) -> Option<GemSlot> {
        match slot_val {
            0 => return None,
            1 => return Some(GemSlot::Empty),
            _ => {}
        }

        let Ok(value) = gem_pwr.try_into() else {
            warn!("Invalid gem power {gem_pwr}");
            return None;
        };

        match GemType::parse(slot_val, value) {
            Some(typ) => Some(GemSlot::Filled(Gem { typ, value })),
            None => Some(GemSlot::Empty),
        }
    }
}

/// A potion. This is not just itemtype to make active potions easier
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Potion {
    /// The rtype of potion
    pub typ: PotionType,
    /// The size of potion
    pub size: PotionSize,
    /// The time at which this potion expires. If this is none, the time is not
    /// known. This can happen for other players
    pub expires: Option<DateTime<Local>>,
}

/// Identifies a specific item and contains all values related to the specific
/// type. The only thing missing is armor, which can be found as a method on
/// `Item`
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum ItemType {
    Hat,
    BreastPlate,
    Gloves,
    FootWear,
    Weapon {
        min_dmg: u32,
        max_dmg: u32,
    },
    Amulet,
    Belt,
    Ring,
    Talisman,
    Shield {
        block_chance: u32,
    },
    Shard {
        piece: u32,
    },
    Potion(Potion),
    Scrapbook,
    DungeonKey {
        id: u32,
        shadow_key: bool,
    },
    Gem(Gem),
    PetItem {
        typ: PetItem,
    },
    QuickSandGlass,
    HeartOfDarkness,
    WheelOfFortune,
    Mannequin,
    Resource {
        amount: u32,
        typ: ResourceType,
    },
    ToiletKey,
    Gral,
    EpicItemBag,
    /// If there is a new item added to the game, this will be the placeholder
    /// to make sure you never think a place is empty somewhere, if it is not
    Unknown(u8),
}

impl ItemType {
    /// Checks if this item type is a weapon.
    #[must_use]
    pub const fn is_weapon(self) -> bool {
        matches!(self, ItemType::Weapon { .. })
    }

    /// Checks if this item type is a shield.
    #[must_use]
    pub const fn is_shield(self) -> bool {
        matches!(self, ItemType::Shield { .. })
    }

    /// Checks if this type can only be worn by only a particular class
    #[must_use]
    pub fn is_class_item(&self) -> bool {
        matches!(
            self,
            ItemType::Hat
                | ItemType::Belt
                | ItemType::Gloves
                | ItemType::FootWear
                | ItemType::Shield { .. }
                | ItemType::Weapon { .. }
                | ItemType::BreastPlate
        )
    }

    /// Checks, if this item type is unique. Technically they are not always
    /// unique, as the scrapbook/keys can be sold, but it should be clear
    /// what this is
    #[must_use]
    pub fn is_unique(&self) -> bool {
        matches!(
            self,
            ItemType::Scrapbook
                | ItemType::HeartOfDarkness
                | ItemType::WheelOfFortune
                | ItemType::Mannequin
                | ItemType::ToiletKey
                | ItemType::Gral
                | ItemType::EpicItemBag
                | ItemType::DungeonKey { .. }
        )
    }

    /// The equipment slot, that this item type can be equipped to
    #[must_use]
    pub fn equipment_slot(&self) -> Option<EquipmentSlot> {
        Some(match self {
            ItemType::Hat => EquipmentSlot::Hat,
            ItemType::BreastPlate => EquipmentSlot::BreastPlate,
            ItemType::Gloves => EquipmentSlot::Gloves,
            ItemType::FootWear => EquipmentSlot::FootWear,
            ItemType::Weapon { .. } => EquipmentSlot::Weapon,
            ItemType::Amulet => EquipmentSlot::Amulet,
            ItemType::Belt => EquipmentSlot::Belt,
            ItemType::Ring => EquipmentSlot::Ring,
            ItemType::Talisman => EquipmentSlot::Talisman,
            ItemType::Shield { .. } => EquipmentSlot::Shield,
            _ => return None,
        })
    }

    /// Checks, if this item type can be enchanted
    #[must_use]
    pub fn is_enchantable(&self) -> bool {
        self.equipment_slot()
            .is_some_and(|e| e.enchantment().is_some())
    }

    pub(crate) fn parse(
        data: &[i64],
        _server_time: ServerTime,
    ) -> Result<Option<Self>, SFError> {
        let raw_typ: u8 = data.csimget(0, "item type", 255, |a| a & 0xFF)?;
        let unknown_item = |name: &'static str| {
            warn!("Could no parse item of type: {raw_typ}. {name} is faulty");
            Ok(Some(ItemType::Unknown(raw_typ)))
        };

        let sub_ident = data.cget(3, "item sub type")?;

        Ok(Some(match raw_typ {
            0 => return Ok(None),
            1 => ItemType::Weapon {
                min_dmg: data.csiget(5, "weapon min dmg", 0)?,
                max_dmg: data.csiget(6, "weapon min dmg", 0)?,
            },
            2 => ItemType::Shield {
                block_chance: data.csiget(5, "shield block chance", 0)?,
            },
            3 => ItemType::BreastPlate,
            4 => ItemType::FootWear,
            5 => ItemType::Gloves,
            6 => ItemType::Hat,
            7 => ItemType::Belt,
            8 => ItemType::Amulet,
            9 => ItemType::Ring,
            10 => ItemType::Talisman,
            11 => {
                let id = sub_ident & 0xFFFF;
                let Ok(id) = id.try_into() else {
                    return unknown_item("unique sub ident");
                };
                match id {
                    1..=11 | 17 | 19 | 22 | 69 | 70 => ItemType::DungeonKey {
                        id,
                        shadow_key: false,
                    },
                    20 => ItemType::ToiletKey,
                    51..=64 | 67..=68 => ItemType::DungeonKey {
                        id,
                        shadow_key: true,
                    },
                    10000 => ItemType::EpicItemBag,
                    piece => ItemType::Shard { piece },
                }
            }
            12 => {
                let id = sub_ident & 0xFF;
                if id > 16 {
                    let Some(typ) = FromPrimitive::from_i64(id) else {
                        return unknown_item("resource type");
                    };
                    ItemType::Resource {
                        // TODO:
                        // data.csiget(7, "resource amount", 0)?,
                        amount: 0,
                        typ,
                    }
                } else {
                    let Some(typ) = PotionType::parse(id) else {
                        return unknown_item("potion type");
                    };
                    let Some(size) = PotionSize::parse(id) else {
                        return unknown_item("potion size");
                    };
                    ItemType::Potion(Potion {
                        typ,
                        size,
                        // TODO:
                        expires: None,
                        // expires: data.cstget(
                        //     4,
                        //     "potion expires",
                        //     server_time,
                        // )?,
                    })
                }
            }
            13 => ItemType::Scrapbook,
            15 => {
                let gem_value = data.csiget(16, "gem pwr", 0)?;
                let Some(typ) = GemType::parse(sub_ident, gem_value) else {
                    return unknown_item("gem type");
                };
                let gem = Gem {
                    typ,
                    value: gem_value,
                };
                ItemType::Gem(gem)
            }
            16 => {
                let Some(typ) = PetItem::parse(sub_ident & 0xFFFF) else {
                    return unknown_item("pet item");
                };
                ItemType::PetItem { typ }
            }
            17 if (sub_ident & 0xFFFF) == 4 => ItemType::Gral,
            17 => ItemType::QuickSandGlass,
            18 => ItemType::HeartOfDarkness,
            19 => ItemType::WheelOfFortune,
            20 => ItemType::Mannequin,
            _ => {
                return unknown_item("main ident");
            }
        }))
    }

    /// The id, that the server has associated with this item. I honestly forgot
    /// why I have this function public
    #[must_use]
    pub fn raw_id(&self) -> u8 {
        match self {
            ItemType::Weapon { .. } => 1,
            ItemType::Shield { .. } => 2,
            ItemType::BreastPlate => 3,
            ItemType::FootWear => 4,
            ItemType::Gloves => 5,
            ItemType::Hat => 6,
            ItemType::Belt => 7,
            ItemType::Amulet => 8,
            ItemType::Ring => 9,
            ItemType::Talisman => 10,
            ItemType::Shard { .. }
            | ItemType::DungeonKey { .. }
            | ItemType::ToiletKey
            | ItemType::EpicItemBag => 11,
            ItemType::Potion { .. } | ItemType::Resource { .. } => 12,
            ItemType::Scrapbook => 13,
            ItemType::Gem(_) => 15,
            ItemType::PetItem { .. } => 16,
            ItemType::QuickSandGlass | ItemType::Gral => 17,
            ItemType::HeartOfDarkness => 18,
            ItemType::WheelOfFortune => 19,
            ItemType::Mannequin => 20,
            ItemType::Unknown(u) => *u,
        }
    }
}

/// The effect, that the potion is going to have
#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum PotionType {
    Strength,
    Dexterity,
    Intelligence,
    Constitution,
    Luck,
    EternalLife,
}

impl From<AttributeType> for PotionType {
    fn from(value: AttributeType) -> Self {
        match value {
            AttributeType::Strength => PotionType::Strength,
            AttributeType::Dexterity => PotionType::Dexterity,
            AttributeType::Intelligence => PotionType::Intelligence,
            AttributeType::Constitution => PotionType::Constitution,
            AttributeType::Luck => PotionType::Luck,
        }
    }
}

impl PotionType {
    pub(crate) fn parse(id: i64) -> Option<PotionType> {
        if id == 0 {
            return None;
        }
        if id == 16 {
            return Some(PotionType::EternalLife);
        }
        Some(match id % 5 {
            0 => PotionType::Luck,
            1 => PotionType::Strength,
            2 => PotionType::Dexterity,
            3 => PotionType::Intelligence,
            _ => PotionType::Constitution,
        })
    }
}

/// The size and with that, the strength, that this potion has
#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum PotionSize {
    Small,
    Medium,
    Large,
}

impl PartialOrd for PotionSize {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.effect().partial_cmp(&other.effect())
    }
}

impl PotionSize {
    #[must_use]
    pub fn effect(&self) -> f64 {
        match self {
            PotionSize::Small => 0.1,
            PotionSize::Medium => 0.15,
            PotionSize::Large => 0.25,
        }
    }

    pub(crate) fn parse(id: i64) -> Option<Self> {
        Some(match id {
            1..=5 => PotionSize::Small,
            6..=10 => PotionSize::Medium,
            11..=16 => PotionSize::Large,
            _ => return None,
        })
    }
}

/// Differentiates resource items
#[derive(Debug, Clone, PartialEq, Eq, Copy, FromPrimitive)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum ResourceType {
    Wood = 17,
    Stone,
    Souls,
    Arcane,
    Metal,
}

/// A gem, that is either socketed in an item, or in the inventory
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Gem {
    /// The type of gem
    pub typ: GemType,
    /// The strength of this gem
    pub value: u32,
}

/// The type the gam has
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum GemType {
    Strength,
    Dexterity,
    Intelligence,
    Constitution,
    Luck,
    All,
    Legendary,
}

impl GemType {
    pub(crate) fn parse(id: i64, debug_value: u32) -> Option<GemType> {
        Some(match id {
            0 | 1 => return None,
            10..=40 => match id % 10 {
                0 => GemType::Strength,
                1 => GemType::Dexterity,
                2 => GemType::Intelligence,
                3 => GemType::Constitution,
                4 => GemType::Luck,
                5 => GemType::All,
                // Just put this here because it makes sense. I only ever
                // see 4 for these
                6 => GemType::Legendary,
                _ => {
                    return None;
                }
            },
            _ => {
                warn!("Unknown gem: {id} - {debug_value}");
                return None;
            }
        })
    }
}

/// Denotes the place, where an item is equipped
#[derive(
    Debug, Copy, Clone, PartialEq, Eq, Hash, Enum, EnumIter, EnumCount,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum EquipmentSlot {
    Hat = 1,
    BreastPlate,
    Gloves,
    FootWear,
    Amulet,
    Belt,
    Ring,
    Talisman,
    Weapon,
    Shield,
}

impl EquipmentSlot {
    /// The value the game internally uses for these slots. No idea, why this is
    /// pub
    #[must_use]
    pub fn raw_id(&self) -> u8 {
        match self {
            EquipmentSlot::Weapon => 1,
            EquipmentSlot::Shield => 2,
            EquipmentSlot::BreastPlate => 3,
            EquipmentSlot::FootWear => 4,
            EquipmentSlot::Gloves => 5,
            EquipmentSlot::Hat => 6,
            EquipmentSlot::Belt => 7,
            EquipmentSlot::Amulet => 8,
            EquipmentSlot::Ring => 9,
            EquipmentSlot::Talisman => 10,
        }
    }

    /// Returns the corresponding enchantment for this equipment slot, if it
    /// can be enchanted
    #[must_use]
    pub const fn enchantment(&self) -> Option<Enchantment> {
        match self {
            EquipmentSlot::Hat => {
                Some(Enchantment::AdventurersArchaeologicalAura)
            }
            EquipmentSlot::BreastPlate => Some(Enchantment::MariosBeard),
            EquipmentSlot::Gloves => Some(Enchantment::ShadowOfTheCowboy),
            EquipmentSlot::FootWear => Some(Enchantment::ManyFeetBoots),
            EquipmentSlot::Amulet => Some(Enchantment::UnholyAcquisitiveness),
            EquipmentSlot::Belt => Some(Enchantment::ThirstyWanderer),
            EquipmentSlot::Ring => Some(Enchantment::TheGraveRobbersPrayer),
            EquipmentSlot::Talisman => Some(Enchantment::RobberBaronRitual),
            EquipmentSlot::Weapon => Some(Enchantment::SwordOfVengeance),
            EquipmentSlot::Shield => None,
        }
    }
}

/// An item usable for pets
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum PetItem {
    Egg(HabitatType),
    SpecialEgg(HabitatType),
    GoldenEgg,
    Nest,
    Fruit(HabitatType),
}

impl PetItem {
    pub(crate) fn parse(val: i64) -> Option<Self> {
        Some(match val {
            1..=5 => PetItem::Egg(HabitatType::from_typ_id(val)?),
            11..=15 => PetItem::SpecialEgg(HabitatType::from_typ_id(val - 10)?),
            21 => PetItem::GoldenEgg,
            22 => PetItem::Nest,
            31..=35 => PetItem::Fruit(HabitatType::from_typ_id(val - 30)?),
            _ => return None,
        })
    }
}

pub(crate) fn parse_active_potions(
    data: &[i64],
    server_time: ServerTime,
) -> [Option<Potion>; 3] {
    if data.len() < 10 {
        return Default::default();
    }
    #[allow(clippy::indexing_slicing)]
    core::array::from_fn(move |i| {
        Some(Potion {
            typ: PotionType::parse(data[i + 1])?,
            size: PotionSize::parse(data[i + 1])?,
            expires: server_time.convert_to_local(data[4 + i], "potion exp"),
            // 6 => effect, but no idea why we would want to use that
        })
    })
}