/
equipment.go
326 lines (286 loc) · 9.34 KB
/
equipment.go
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
package popper
import (
"context"
"encoding/json"
"strings"
"github.com/ecshreve/dndgen/ent"
"github.com/ecshreve/dndgen/ent/coin"
"github.com/ecshreve/dndgen/ent/gear"
"github.com/samsarahq/go/oops"
log "github.com/sirupsen/logrus"
)
type IndxWrapper struct {
Indx string `json:"index"`
}
type Range struct {
Normal int `json:"normal,omitempty"`
Long int `json:"long,omitempty"`
}
type WeaponWrapper struct {
WeaponCategory string `json:"weapon_category"`
WeaponRange string `json:"weapon_range"`
MeleeRange Range `json:"range,omitempty"`
ThrowRange Range `json:"throw_range,omitempty"`
Damage struct {
Dice string `json:"damage_dice,omitempty"`
DType IndxWrapper `json:"damage_type,omitempty"`
} `json:"damage,omitempty"`
Properties []IndxWrapper `json:"properties,omitempty"`
}
type ArmorWrapper struct {
ArmorCategory string `json:"armor_category"`
StealthDisadvantage bool `json:"stealth_disadvantage"`
StrMinimum int `json:"str_minimum,omitempty"`
ArmorClass struct {
Base int `json:"base"`
DexBonus bool `json:"dex_bonus"`
MaxBonus int `json:"max_bonus,omitempty"`
} `json:"armor_class"`
}
type GearWrapper struct {
GearCategory IndxWrapper `json:"gear_category"`
Quantity *int `json:"quantity,omitempty"`
}
type ToolWrapper struct {
ToolCategory string `json:"tool_category"`
}
type VehicleWrapper struct {
VehicleCategory string `json:"vehicle_category"`
}
type CostWrapper struct {
Quantity int `json:"quantity"`
Unit string `json:"unit"`
}
type EquipmentSubWrapper struct {
Name string `json:"name"`
Cost CostWrapper `json:"cost"`
Weight float64 `json:"weight"`
EquipmentCategory IndxWrapper `json:"equipment_category"`
}
type EquipmentWrapper struct {
Indx string `json:"index"`
EquipmentSubWrapper
*WeaponWrapper
*ArmorWrapper
*GearWrapper
*ToolWrapper
*VehicleWrapper
}
// // PopulateEquipment populates the Equipment entities from the JSON data files.
func (p *Popper) PopulateEquipment(ctx context.Context) error {
equipmentCategories := map[string][]string{
"weapon": {
"simple",
"martial",
"melee",
"ranged",
},
"armor": {
"light",
"medium",
"heavy",
"shield",
},
"adventuring_gear": {
"equipment_packs",
"standard_gear",
"ammunition",
"arcane_foci",
"druidic_foci",
"holy_symbols",
"kits",
},
"tools": {
"artisans_tools",
"gaming_sets",
"musical_instruments",
"other_tools",
},
"mounts_and_vehicles": {
"land",
"waterborne",
},
"other": {},
}
for cat, subs := range equipmentCategories {
parent := p.Client.EquipmentCategory.Create().SetName(cat).SaveX(ctx)
log.Infof("created parent entity for type EquipmentCategory %s", cat)
creates := []*ent.EquipmentCategoryCreate{}
for _, sub := range subs {
creates = append(creates, p.Client.EquipmentCategory.Create().SetName(sub).SetParentCategoryID(parent.ID))
}
cats := p.Client.EquipmentCategory.CreateBulk(creates...).SaveX(ctx)
log.Infof("created %d entities for type EquipmentCategory %s", len(cats), cat)
p.IdToIndx[parent.ID] = string(cat)
p.IndxToId[cat] = parent.ID
for _, c := range cats {
p.IdToIndx[c.ID] = string(c.Name)
p.IndxToId[string(c.Name)] = c.ID
}
}
fpath := "data/Equipment.json"
var v []EquipmentWrapper
if err := LoadJSONFile(fpath, &v); err != nil {
return oops.Wrapf(err, "unable to load JSON file %s", fpath)
}
for _, ww := range v {
vv := ent.Equipment{
Indx: ww.Indx,
Name: ww.Name,
}
cn := p.Client.Coin.Query().Where(coin.IndxEQ(ww.Cost.Unit)).FirstX(ctx)
// val := cn.GoldConversionRate * float64(ww.Cost.Quantity)
eq, err := p.Client.Equipment.Create().
SetEquipment(&vv).
Save(ctx)
if ent.IsConstraintError(err) {
log.Debugf("constraint failed, skipping %s", vv.Indx)
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %s", vv.Indx)
}
eq.Update().AddEquipmentCategoryIDs(p.IndxToId[strings.ToLower(strings.Replace(ww.EquipmentCategory.Indx, "-", "_", -1))]).SaveX(ctx)
eqc := &ent.EquipmentCost{
EquipmentID: eq.ID,
Quantity: ww.Cost.Quantity,
CoinID: cn.ID,
GpValue: cn.GoldConversionRate * float64(ww.Cost.Quantity),
}
_, err = p.Client.EquipmentCost.Create().SetEquipmentCost(eqc).Save(ctx)
if err != nil {
return oops.Wrapf(err, "unable to create eq cost %s", vv.Indx)
}
if ww.WeaponWrapper != nil {
w := ent.Weapon{
Indx: ww.Indx,
Name: ww.Name,
WeaponRange: strings.ToLower(strings.Replace(ww.WeaponRange, "-", "_", -1)),
WeaponCategory: strings.ToLower(strings.Replace(ww.WeaponCategory, "-", "_", -1)),
}
created, err := p.Client.Weapon.Create().SetWeapon(&w).SetEquipment(eq).Save(ctx)
if ent.IsConstraintError(err) {
log.Debugf("constraint failed, skipping %s", vv.Indx)
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %s", vv.Indx)
}
eq.Update().AddEquipmentCategoryIDs(p.IndxToId[w.WeaponCategory], p.IndxToId[created.WeaponRange]).SaveX(ctx)
jj, _ := json.Marshal(ww.Properties)
propIDs := p.GetIDsFromIndxs(jj)
created.Update().AddWeaponPropertyIDs(propIDs...).SaveX(ctx)
if ww.WeaponWrapper.Damage.Dice != "" {
did := p.IndxToId[ww.WeaponWrapper.Damage.DType.Indx]
_, err = p.Client.WeaponDamage.Create().SetWeaponID(created.ID).SetDamageTypeID(did).SetDice(ww.WeaponWrapper.Damage.Dice).Save(ctx)
if ent.IsConstraintError(err) {
log.Debugf("constraint failed, skipping %s", vv.Indx)
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %s", vv.Indx)
}
}
}
if ww.ArmorWrapper != nil && ww.EquipmentCategory.Indx == "armor" {
a := ent.Armor{
Indx: ww.Indx,
Name: ww.Name,
StealthDisadvantage: ww.ArmorWrapper.StealthDisadvantage,
MinStrength: ww.ArmorWrapper.StrMinimum,
ArmorCategory: strings.ToLower(strings.Split(ww.ArmorWrapper.ArmorCategory, "-")[0]),
}
b := ent.ArmorClass{
Base: ww.ArmorWrapper.ArmorClass.Base,
DexBonus: ww.ArmorWrapper.ArmorClass.DexBonus,
MaxBonus: ww.ArmorWrapper.ArmorClass.MaxBonus,
}
ac, err := p.Client.ArmorClass.Create().SetArmorClass(&b).Save(ctx)
if ent.IsConstraintError(err) {
log.Debug("constraint failed, skipping")
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %v", ac)
}
_, err = p.Client.Armor.Create().SetArmor(&a).AddArmorClass(ac).SetEquipment(eq).Save(ctx)
if ent.IsConstraintError(err) {
log.Debug("constraint failed, skipping")
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %v", ac)
}
eq.Update().AddEquipmentCategoryIDs(p.IndxToId[a.ArmorCategory]).SaveX(ctx)
}
if ww.GearWrapper != nil && ww.EquipmentCategory.Indx == "adventuring-gear" {
a := ent.Gear{
Indx: ww.Indx,
Name: ww.Name,
Quantity: intOrDef(ww.Quantity, 0),
GearCategory: gear.GearCategory(strings.Replace(ww.GearCategory.Indx, "-", "_", -1)),
}
_, err = p.Client.Gear.Create().SetGear(&a).SetEquipment(eq).Save(ctx)
if ent.IsConstraintError(err) {
log.Debug("constraint failed, skipping")
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %v", a)
}
eq.Update().AddEquipmentCategoryIDs(p.IndxToId[string(a.GearCategory)]).SaveX(ctx)
}
if ww.ToolWrapper != nil && ww.EquipmentCategory.Indx == "tools" {
a := ent.Tool{
Indx: ww.Indx,
Name: ww.Name,
ToolCategory: cleanString(ww.ToolCategory),
}
_, err = p.Client.Tool.Create().SetTool(&a).SetEquipment(eq).Save(ctx)
if ent.IsConstraintError(err) {
log.Debug("constraint failed, skipping")
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %v", a)
}
eq.Update().AddEquipmentCategoryIDs(p.IndxToId[a.ToolCategory]).SaveX(ctx)
}
if ww.VehicleWrapper != nil && ww.EquipmentCategory.Indx == "mounts-and-vehicles" {
a := ent.Vehicle{
Indx: ww.Indx,
Name: ww.Name,
VehicleCategory: "land",
}
if catraw := strings.Contains(ww.VehicleCategory, "Waterborne"); catraw {
a.VehicleCategory = "waterborne"
}
_, err = p.Client.Vehicle.Create().SetVehicle(&a).SetEquipment(eq).Save(ctx)
if ent.IsConstraintError(err) {
log.Debug("constraint failed, skipping")
log.Debug(err)
continue
}
if err != nil {
return oops.Wrapf(err, "unable to create entity %v", a)
}
eq.Update().AddEquipmentCategoryIDs(p.IndxToId[a.VehicleCategory]).SaveX(ctx)
}
p.IdToIndx[eq.ID] = vv.Indx
p.IndxToId[vv.Indx] = eq.ID
}
log.Infof("created %d entities for type Equipment", p.Client.Equipment.Query().CountX(ctx))
log.Infof("-> created %d entities for type Weapon", p.Client.Weapon.Query().CountX(ctx))
log.Infof("-> created %d entities for type Armor", p.Client.Armor.Query().CountX(ctx))
log.Infof("-> created %d entities for type Gear", p.Client.Gear.Query().CountX(ctx))
log.Infof("-> created %d entities for type Tool", p.Client.Tool.Query().CountX(ctx))
log.Infof("-> created %d entities for type Vehicle", p.Client.Vehicle.Query().CountX(ctx))
return nil
}