-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweapons.js
961 lines (928 loc) · 37.1 KB
/
weapons.js
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
var DoRounds = DoRounds || {};
DoRounds.Weapons = (function () {
const weaponStorageKey = "doRoundsWeapons";
let Weapon = function(props) {
var self = {};
if (!props) {
// Provide a default initialization object
props = {};
props.name = "New weapon";
props.damageType = 0;
props.damage = "";
props.range = "";
props.weight = 0;
props.properties = [];
props.versatileDamage = "";
props.special = "";
props.hitbonus = 0;
}
if (props.name) {
self.name = props.name;
self.id = props.name;
self.label = props.name;
} else {
throw "Weapon must have a name property.";
}
self.damageType = props.damageType || 0;
self.damage = props.damage || null;
self.range = props.range || null;
self.weight = props.weight || 0;
self.properties = props.properties || [];
self.versatileDamage = props.versatileDamage || null;
self.special = props.special || null;
self.hitbonus = props.hitbonus || 0;
self.damageText = function () {
if (Array.isArray(self.damage)) {
return self.damage.join(", ");
} else {
return self.damage;
}
};
return self;
}
let defaultWeaponData = {
Categories: [
"Simple",
"Martial",
"Spell",
"Custom"
],
Properties: [
{
id: 0,
name: "ammunition", // 0
description: "You can use a weapon that has the ammunition property to make a ranged attack only if you have ammunition to fire from the weapon. Each time you attack with the weapon, you expend one piece of ammunition. Drawing the ammunition from a quiver, case, or other container is part of the attack (you need a free hand to load a one-handed weapon). At the end of the battle, you can recover half your expended ammunition by taking a minute to search the battlefield. If you use a weapon that has the ammunition property to make a melee attack, you treat the weapon as an improvised weapon (see 'Improvised Weapons' later in the section). A sling must be loaded to deal any damage when used in this way."
},
{
id: 1,
name: "close", // 1
descritpion: "A close weapon is more effective up close than other ranged weapons. When you make a ranged attack with a close weapon, you do not suffer disadvantage on the attack roll when you are within 5 feet of a hostile creature who can see you and who is not incapacitated."
},
{
id: 2,
name: "deadly", // 2
description: "When you roll a natural 1 on a damage die with a deadly weapon, treat the result as a 2 instead."
},
{
id: 3,
name: "defensive", // 3
description: "A defensive weapon makes you harder to hit effectively while you are wielding two weapons. As long as at least one of your two weapons has the defensive property, you add +1 to your AC against the first attack that targets you in a round, provided you aren't surprised or immobile. You regain the bonus at the start of your next turn, and do not gain this bonus against subsequent attacks against you until then."
},
{
id: 4,
name: "finesse", // 4
description: "When making an attack with a finesse weapon, you use your choice of your Strength or Dexterity modifier for the attack and damage rolls. You must use the same modifier for both rolls."
},
{
id: 5,
name: "heavy", // 5
description: "Small creatures have disadvantage on attack rolls with heavy weapons. A heavy weapon's size and bulk make it too large for a Small creature to use effectively."
},
{
id: 6,
name: "light", // 6
description: "A light weapon is small and easy to handle, making it ideal for use when fighting with two weapons."
},
{
id: 7,
name: "loading", // 7
description: "Because of the time required to load this weapon, you can fire only one piece of ammunition from it when you use an action, bonus action, or reaction to fire it, regardless of the number of attacks you can normally make."
},
{
id: 8,
name: "reach", // 8
description: "This weapon adds 5 feet to your reach when you attack with it, as well as when determining your reach for opportunity attacks with it."
},
{
id: 9,
name: "special", // 9
description: "A weapon with the special property has unusual rules governing its use."
},
{
id: 10,
name: "thrown", // 10
description: "If a weapon has the thrown property, you can throw the weapon to make a ranged attack. If the weapon is a melee weapon, you use the same ability modifier for that attack roll and damage roll that you would use for a melee attack with the weapon. For example, if you throw a handaxe, you use your Strength, but if you throw a dagger, you can use either your Strength or your Dexterity, since the dagger has the finesse property."
},
{
id: 11,
name: "two-handed", // 11
description: "This weapon requires two hands when you attack with it."
},
{
id: 12,
name: "versatile", // 12
description: "This weapon can be used with one or two hands. A damage value in parentheses appears with the property—the damage when the weapon is used with two hands to make a melee attack."
}
],
DamageTypes: [
"bludgeoning", // 0
"piercing", // 1
"slashing", // 2
"radiant", // 3
"fire", // 4
"poison", // 5
"acid", // 6
"cold", // 7
"lightning", // 8
"necrotic", // 9
"force", // 10
],
Items: [
Weapon({
name: "Unarmed attack",
damageType: 0,
damage: "1d4",
range: 0,
weight: 0,
properties: []
}),
{
id: "Simple",
label: "Simple",
children: [
Weapon({
name: "Club",
damageType: 0,
damage: "1d4",
range: null,
weight: 2,
properties: [6]
}),
Weapon({
name: "Dagger",
damageType: 1,
damage: "1d4",
range: "20/60",
weight: 1,
properties: [4, 6, 10]
}),
Weapon({
name: "Greatclub",
damageType: 0,
damage: "1d8",
range: null,
weight: 10,
properties: [11]
}),
Weapon({
name: "Handaxe",
damageType: 2,
damage: "1d6",
range: "20/60",
weight: 2,
properties: [6, 10]
}),
Weapon({
name: "Javelin",
damageType: 1,
damage: "1d6",
range: "30/120",
weight: 2,
properties: [10]
}),
Weapon({
name: "Light hammer",
damageType: 0,
damage: "1d4",
range: "20/60",
weight: 2,
properties: [6, 10]
}),
Weapon({
name: "Mace",
damageType: 0,
damage: "1d6",
range: null,
weight: 4,
properties: []
}),
Weapon({
name: "Quarterstaff",
damageType: 0,
damage: "1d6",
range: null,
weight: 4,
properties: [12],
versatileDamage: "1d8"
}),
Weapon({
name: "Sickle",
damageType: 2,
damage: "1d4",
range: null,
weight: 2,
properties: [6]
}),
Weapon({
name: "Spear",
damageType: 1,
damage: "1d6",
range: "20/60",
weight: 3,
properties: [10, 12],
versatileDamage: "1d8"
}),
Weapon({
name: "Crossbow, light",
damageType: 1,
damage: "1d8",
range: "80/320",
weight: 5,
properties: [0, 7, 11]
}),
Weapon({
name: "Dart",
damageType: 1,
damage: "1d4",
range: "20/60",
weight: .25,
properties: [4, 10]
}),
Weapon({
name: "Shortbow",
damageType: 1,
damage: "1d6",
range: "80/320",
weight: 2,
properties: [0, 11]
}),
Weapon({
name: "Sling",
damageType: 0,
damage: "1d4",
range: "30/120",
weight: 0,
properties: [0]
})
]
},
{
id: "Martial",
label: "Martial",
children: [
Weapon({
name: "Battleaxe",
damageType: 2,
damage: "1d8",
range: null,
weight: 4,
properties: [12],
versatileDamage: "1d10"
}),
Weapon({
name: "Dwarven Urgrosh",
damageType: [1, 2],
damage: ["1d8", "1d10"],
range: "20/60",
weight: 4,
properties: [9, 10],
special: "One head of this weapon ends in a spear and\nthe other end is an axe. Originally used\nexclusively for mining, these weapons were\nadapted to combat creatures in the Underdark.\n\nIf you have the Dual Wielder feat, the Exotic \nWeapon Master feat, or the Two-Weapon \nFighting style, you can wield a dwarven urgrosh\nas a one-handed spear and a one-handed\nbattleaxe. It gains the light property when\nwielded in this way."
}),
Weapon({
name: "Elven Crescent Blade",
damageType: 2,
damage: "2d6",
range: null,
weight: 6,
properties: [5, 9, 11],
special: "This long, almost moon-shaped blade allows a\nproper wielder unsurpassed flexibility in battle.\n\nIf you have the Exotic Weapon Master feat,\nthe elven crescent blade gains the finesse\nproperty."
}),
Weapon({
name: "Flail",
damageType: 0,
damage: "1d8",
range: null,
weight: 2,
properties: [],
versatileDamage: ""
}),
Weapon({
name: "Glaive",
damageType: 2,
damage: "1d10",
range: null,
weight: 6,
properties: [5, 8, 11]
}),
Weapon({
name: "Greataxe",
damageType: 2,
damage: "1d12",
range: null,
weight: 7,
properties: [5, 11]
}),
Weapon({
name: "Greatsword",
damageType: 2,
damage: "2d6",
range: null,
weight: 6,
properties: [5, 11]
}),
Weapon({
name: "Halberd",
damageType: 2,
damage: "1d10",
range: null,
weight: 6,
properties: [5, 8, 11]
}),
Weapon({
name: "Lance",
damageType: 1,
damage: "1d12",
range: null,
weight: 6,
properties: [8, 9],
special: "You have disadvantage when you use a lance\nto attack a target within 5 feet of you.\nAlso, a lance requires two hands to\nwield when you aren't mounted."
}),
Weapon({
name: "Longsword",
damageType: 2,
damage: "1d8",
range: null,
weight: 3,
properties: [12],
versatileDamage: "1d10"
}),
Weapon({
name: "Maul",
damageType: 0,
damage: "2d6",
range: null,
weight: 10,
properties: [5, 11]
}),
Weapon({
name: "Morningstar",
damageType: 1,
damage: "1d8",
range: null,
weight: 4,
properties: []
}),
Weapon({
name: "Pike",
damageType: 1,
damage: "1d10",
range: null,
weight: 18,
properties: [5, 8, 11]
}),
Weapon({
name: "Rapier",
damageType: 1,
damage: "1d8",
range: null,
weight: 2,
properties: [4]
}),
Weapon({
name: "Scimitar",
damageType: 2,
damage: "1d6",
range: null,
weight: 3,
properties: [4, 6]
}),
Weapon({
name: "Shortsword",
damageType: 1,
damage: "1d6",
range: null,
weight: 2,
properties: [4, 6]
}),
Weapon({
name: "Spiked Chain",
damageType: 1,
damage: "1d8",
range: null,
weight: 10,
properties: [4, 5, 8, 9, 11],
special: "A length of spiked chain is between 6 and\n8-feet long with wicked barbs welded onto\none end. If you have the Dual-Wielder\nfeat, the Exotic Weapon Master feat, or\nthe Two-Weapon Fighting style, you can\nwield a spiked chain as two one-handed,\nlight weapons that each deal 1d6 piercing\ndamage. The spiked chain loses the reach\nproperty when wielded in this way."
}),
Weapon({
name: "Trident",
damageType: 1,
damage: "1d6",
range: "20/60",
weight: 5,
properties: [10, 12],
versatileDamage: "1d8"
}),
Weapon({
name: "War pick",
damageType: 1,
damage: "1d8",
range: null,
weight: 2,
properties: []
}),
Weapon({
name: "Warhammer",
damageType: 0,
damage: "1d8",
range: null,
weight: 2,
properties: [12],
versatileDamage: "1d10"
}),
Weapon({
name: "Whip",
damageType: 2,
damage: "1d4",
range: null,
weight: 3,
properties: [4, 8]
}),
Weapon({
name: "War Scythe",
damageType: 2,
damage: "1d10",
range: null,
weight: 2,
properties: [9, 11],
special: "Fashioned to resemble the threshing\nimplement but modified for battle, the war\nscythe can be a deadly weapon in the\nright hands.\nYou can't wield a war scythe in one hand.\nIf you have the Exotic Weapon Master feat,\nyou can wield the war scythe as a war pick.\nIt gains the versatile (d10) property\nwhen wielded in this way.\nWhen you take the Attack action, you can\nattempt the Trip Attack combat manuever\n(DC 8 + your proficiency bonus + your\nStrength modifier) against a creature as\none of your attacks."
}),
Weapon({
name: "Blowgun",
damageType: 1,
damage: "1",
range: "25/100",
weight: 1,
properties: [0, 7]
}),
Weapon({
name: "Crossbow, hand",
damageType: 1,
damage: "1d6",
range: "30/120",
weight: 3,
properties: [0, 6, 7]
}),
Weapon({
name: "Crossbow, heavy",
damageType: 1,
damage: "1d10",
range: "100/400",
weight: 18,
properties: [0, 5, 7, 11]
}),
Weapon({
name: "Great Bow",
damageType: 1,
damage: "1d8",
range: "150/600",
weight: 2,
properties: [0, 5, 9, 11],
special: "This 6-foot tall bow is made of elm\nrather than yew or ash, making it\nastonishingly stiff, large and strong,\nand equally capable of use for long and\nshort shooting.\nYou can use a bonus action to steady\nyourself. While you are steadied, your\nattacks with the great bow deal 2d6\npiercing damage. You are no longer\nsteadied if you move."
}),
Weapon({
name: "Longbow",
damageType: 1,
damage: "1d8",
range: "150/600",
weight: 2,
properties: [0, 5, 11]
}),
Weapon({
name: "Net",
damageType: -1,
damage: null,
range: "5/15",
weight: 3,
properties: [9, 10],
special: "A Large or smaller creature hit by a net\nis restrained until it is freed. A net has\nno effect on creatures that are formless,\nor creatures that are Huge or larger.\nA creature can use its action to make a\nDC 10 Strength check, freeing itself or\nanother creature within its reach on a\nsuccess. Dealing 5 slashing damage to the\nnet (AC 10) also frees the creature\nwithout harming it, ending the effect\nand destroying the net. When you use an\naction, bonus action, or reaction to\nattack regardless of the number of attacks\nyou can normally make."
})
]
},
{
id: "Spell",
label: "Spell",
children: [
Weapon({
name: "Acid Splash 1-4",
damageType: 6,
damage: "1d6",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Acid Splash 5-10",
damageType: 6,
damage: "2d6",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Acid Splash 11-16",
damageType: 6,
damage: "3d6",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Acid Splash 17+",
damageType: 6,
damage: "4d6",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Chill Touch 1-4",
damageType: 9,
damage: "1d8",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Chill Touch 5-10",
damageType: 9,
damage: "2d8",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Chill Touch 11-16",
damageType: 9,
damage: "3d8",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Chill Touch 17+",
damageType: 9,
damage: "4d8",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Eldritch Blast",
damageType: 10,
damage: "1d10",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Fire Bolt 1-4",
damageType: 4,
damage: "1d10",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Fire Bolt 5-10",
damageType: 4,
damage: "2d10",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Fire Bolt 11-16",
damageType: 4,
damage: "3d10",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Fire Bolt 17+",
damageType: 4,
damage: "4d10",
range: 120,
weight: 0,
properties: [],
}),
Weapon({
name: "Poison Spray 1-4",
damageType: 5,
damage: "1d12",
range: 10,
weight: 0,
properties: [],
}),
Weapon({
name: "Poison Spray 5-10",
damageType: 5,
damage: "2d12",
range: 10,
weight: 0,
properties: [],
}),
Weapon({
name: "Poison Spray 11-16",
damageType: 5,
damage: "3d12",
range: 10,
weight: 0,
properties: [],
}),
Weapon({
name: "Poison Spray 17+",
damageType: 5,
damage: "4d12",
range: 10,
weight: 0,
properties: [],
}),
Weapon({
name: "Produce Flame 1-4",
damageType: 4,
damage: "1d8",
range: 30,
weight: 0,
properties: [],
}),
Weapon({
name: "Produce Flame 5-10",
damageType: 4,
damage: "2d8",
range: 30,
weight: 0,
properties: [],
}),
Weapon({
name: "Produce Flame 11-16",
damageType: 4,
damage: "3d8",
range: 30,
weight: 0,
properties: [],
}),
Weapon({
name: "Produce Flame 17+",
damageType: 4,
damage: "4d8",
range: 30,
weight: 0,
properties: [],
}),
Weapon({
name: "Ray of Frost 1-4",
damageType: 7,
damage: "1d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Ray of Frost 5-10",
damageType: 7,
damage: "2d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Ray of Frost 11-16",
damageType: 7,
damage: "3d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Ray of Frost 17+",
damageType: 7,
damage: "4d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Sacred Flame 1-4",
damageType: 3,
damage: "1d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Sacred Flame 5-10",
damageType: 3,
damage: "2d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Sacred Flame 11-16",
damageType: 3,
damage: "3d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Sacred Flame 17+",
damageType: 3,
damage: "4d8",
range: 60,
weight: 0,
properties: [],
}),
Weapon({
name: "Shocking Grasp 1-4",
damageType: 8,
damage: "1d8",
range: 0,
weight: 0,
properties: [],
}),
Weapon({
name: "Shocking Grasp 5-10",
damageType: 8,
damage: "2d8",
range: 0,
weight: 0,
properties: [],
}),
Weapon({
name: "Shocking Grasp 11-16",
damageType: 8,
damage: "3d8",
range: 0,
weight: 0,
properties: [],
}),
Weapon({
name: "Shocking Grasp 17+",
damageType: 8,
damage: "4d8",
range: 0,
weight: 0,
properties: [],
})
]
}
]
};
let savedWeapons = null;
let savedWeaponsString = localStorage.getItem(weaponStorageKey);
if (savedWeaponsString === null) {
savedWeapons = defaultWeaponData;
} else {
savedWeapons = JSON.parse(savedWeaponsString);
}
savedWeapons.save = function () {
let saveString = JSON.stringify(savedWeapons);
localStorage.setItem(weaponStorageKey, saveString);
};
savedWeapons.import = function (importData) {
localStorage.setItem(weaponStorageKey, importData);
let asWeaponsData = JSON.parse(importData);
savedWeapons.Items = asWeaponsData.Items;
};
savedWeapons.export = function () {
return localStorage.getItem(weaponStorageKey);
};
savedWeapons.autoSave = false;
savedWeapons.doAutoSave = function () {
if (savedWeapons.autoSave) {
savedWeapons.save();
}
};
savedWeapons.weaponsWithNew = function () {
let all = [];
all.push(Weapon());
all = all.concat(savedWeapons.Items);
return all;
};
savedWeapons.AddCategory = function (categoryName) {
let foundCategory = savedWeapons.Categories[categoryName];
if (!foundCategory) {
savedWeapons.Categories.push(categoryName);
foundCategory = {
id: categoryName,
label: categoryName,
children: []
};
savedWeapons.Items.push(foundCategory);
savedWeapons.doAutoSave();
}
return foundCategory;
};
savedWeapons.findItemCategory = function (categoryName) {
let foundItemCategory = null;
let normCatName = categoryName.toLowerCase();
savedWeapons.Items.forEach(c => {
if (c.children && (normCatName === c.label.toLowerCase())) {
foundItemCategory = c;
return true;
}
});
return foundItemCategory;
};
savedWeapons.AddWeapon = function (categoryId, weapon) {
let categoryName = savedWeapons.Categories[categoryId];
let category = savedWeapons.findItemCategory(categoryName);
if (!category) {
// This may be the first item in a new category
category = savedWeapons.AddCategory(categoryName);
}
// weapon.properties will contain an array of objects here, but we
// need it to be an array of integers that are indices to the
// savedWeapons.Properties array.
weapon.properties = weapon.properties.map(p => p.id);
let newWeapon = Weapon(weapon);
category.children.push(newWeapon);
savedWeapons.doAutoSave();
};
savedWeapons.propertiesText = function (weapon) {
let text = "";
for (var i = 0; i < weapon.properties.length; i++) {
let curProp = savedWeapons.Properties[weapon.properties[i]];
if (text.length > 0 && curProp.name !== "special") {
text += ", ";
}
if (curProp.name == "versatile") {
text += `versatile (${weapon.versatileDamage})`;
} else if (curProp.name == "special") {
// skip this type; it is rendered by UI code
} else {
text += savedWeapons.Properties[weapon.properties[i]].name;
}
}
return text;
};
savedWeapons.damageText = function (weapon) {
let text = "";
if (Array.isArray(weapon.damageType)) {
for (var i = 0; i < weapon.damageType.length; i++) {
if (text.length > 0) {
text += ", ";
}
text += `${weapon.damage[i]} of ${savedWeapons.DamageTypes[weapon.damageType[i]]}`;
}
} else {
if (weapon.damage) {
text = `${weapon.damage} of ${savedWeapons.DamageTypes[weapon.damageType]}`;
} else {
text = "no";
}
}
return text;
};
function findWeaponByName(weaponName, inList) {
let foundWeapon = null;
if (!inList) {
inList = savedWeapons.Items;
}
inList.every(item => {
if (item.name && item.name === weaponName) {
foundWeapon = item;
return false;
} else if (item.children) {
foundWeapon = findWeaponByName(weaponName, item.children);
if (foundWeapon !== null) {
return false;
}
}
return true;
});
return foundWeapon;
}
savedWeapons.weaponExists = function (weaponName) {
return findWeaponByName(weaponName) !== null;
};
savedWeapons.deleteWeapon = function (weaponName, inList) {
let weaponDeleted = false;
if (!inList) {
inList = inList = savedWeapons.Items;
}
let itemIndex = -1;
inList.every(item => {
itemIndex++;
if (item.children) {
if (savedWeapons.deleteWeapon(weaponName, item.children)) {
weaponDeleted = true;
return false;
} else {
return true;
}
} else if (item.name && item.name === weaponName) {
delete inList[itemIndex];
weaponDeleted = true;
return false;
} else {
return true;
}
});
return weaponDeleted;
};
savedWeapons.Weapon = Weapon;
return savedWeapons;
}());