1 module sde.records.itemtype;
2 
3 import std.typecons;
4 import std.algorithm.iteration;
5 import std.stdio;
6 import std.range;
7 import std.array;
8 import dyaml;
9 import sde.yamlutils;
10 
11 immutable struct ItemType {
12     uint typeID;
13     uint groupID;
14     // optional members:
15     string name;
16     double mass;
17     float volume;
18     double basePrice;
19     uint iconID;
20     uint marketGroupID;
21 
22     static ItemType fromYamlPair(Node.Pair yamlPair) {
23         auto value = yamlPair.value;
24 
25         return ItemType(
26             yamlPair.key.get!uint,
27             value["groupID"].as!uint,
28             value["name"].getWithDefaultFallback!string("en"),
29             value.getWithDefaultFallback!double("mass"),
30             value.getWithDefaultFallback!float("volume"),
31             value.getWithDefaultFallback!double("basePrice"),
32             value.getWithDefaultFallback!uint("iconID"),
33             value.getWithDefaultFallback!uint("marketGroupID"),
34         );
35     }
36 
37     static ItemType[] fromRootYaml(Node yamlRootNode) {
38         return yamlRootNode
39             .mapping()
40             .map!(ItemType.fromYamlPair)
41             .array;
42     }
43 }
44 
45 unittest {
46     import std.string : strip;
47 
48     auto yamlString = strip("
49 222:
50     groupID: 2
51     name:
52         de: Corporation
53         en: Corporation
54         fr: Corporation
55         ja: コーポレーション
56         ru: Corporation
57         zh: 军团
58     portionSize: 1
59     published: false
60 333:
61     groupID: 2
62     name:
63         de: Corporation
64         en: Corporation
65         fr: Corporation
66         ja: コーポレーション
67         ru: Corporation
68         zh: 军团
69     portionSize: 1
70     published: false
71 ");
72 
73     auto yamlRoot = Loader.fromString(yamlString).load();
74     auto itemTypes = ItemType.fromRootYaml(yamlRoot);
75 
76     assert(itemTypes.length == 2);
77 }
78 
79 unittest {
80     import std.string : strip;
81     import std.math : isNaN;
82 
83     auto yamlString = strip("
84 222:
85     groupID: 2
86     name:
87         de: Corporation
88         en: Corporation
89         fr: Corporation
90         ja: コーポレーション
91         ru: Corporation
92         zh: 军团
93     portionSize: 1
94     published: false
95 ");
96 
97     auto yamlRoot = Loader.fromString(yamlString).load();
98     auto itemType = ItemType.fromYamlPair(yamlRoot.mapping.front);
99 
100     assert(itemType.typeID == 222);
101     assert(itemType.groupID == 2);
102     assert(itemType.name == "Corporation");
103     assert(isNaN(itemType.mass));
104 }