-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpathbuilder-import.js
More file actions
1267 lines (1043 loc) · 40.6 KB
/
pathbuilder-import.js
File metadata and controls
1267 lines (1043 loc) · 40.6 KB
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
Hooks.on('renderActorSheet', function(obj, html){
// Only inject the link if the actor is of type "character" and the user has permission to update it
const actor = obj.actor;
// if (!(actor.data.type === "character" && actor.can(game.user, "update"))) return;
let element = html.find(".window-header .window-title");
if (element.length != 1) return;
let button = $(`<a class="popout" style><i class="fas fa-book"></i>Import from Pathbuilder 2e</a>`);
button.on('click', () => beginPathbuilderImport(obj.object));
element.after(button);
}
);
var applyChanges = false;
var finishedFeats = false;
var finishedActions = false;
var finishedClassFeatures = false;
var finishedAncestryFeatures=false;
var finishedEquipment=false;
var finishedSpells = false;
var addFeats=false;
var addEquipment=false;
var addMoney=false;
var addSpellcasters=false;
var deleteAll=false;
var allItems=[];
var jsonBuild=[];
var addedItems=[];
function beginPathbuilderImport(targetActor){
applyChanges = false;
finishedFeats = false;
finishedActions = false;
finishedClassFeatures = false;
finishedAncestryFeatures=false;
finishedEquipment=false;
finishedSpells = false;
allItems=[];
// <input type="checkbox" id="checkBoxSpellcasters" name="checkBoxSpellcasters" checked>
// <label for="checkBoxSpellcasters"> Import Spellcasters? (Deletes existing)</label><br><br>
new Dialog({
title: `Pathbuilder Import`,
content: `
<div>
<p>Step 1: Refresh this browser page!</p>
<p>Step 2: Export your character from Pathbuilder 2e via the app menu</p>
<p>Step 3: Enter the 6 digit user ID number from the pathbuilder export dialog below</p>
<br>
<p>Please note - items which cannot be matched to the Foundry database will not be imported!<p>
<p>Spells are not currently being imported.</p>
<div>
<hr/>
<form>
<input type="checkbox" id="checkBoxFeats" name="checkBoxFeats" checked>
<label for="checkBoxFeats"> Import Feats and Specials?</label><br><br>
<input type="checkbox" id="checkBoxEquipment" name="checkBoxEquipment">
<label for="checkBoxEquipment"> Import Equipment?</label><br>
<input type="checkbox" id="checkBoxMoney" name="checkBoxMoney">
<label for="checkBoxMoney"> Import Money?</label><br><br>
<input type="checkbox" id="checkBoxDeleteAll" name="checkBoxDeleteAll">
<label for="checkBoxDeleteAll"> Delete all existing items before import (excluding spells)?</label><br><br>
</form>
<div id="divCode">
Enter your pathbuilder user ID number<br>
<div id="divOuter">
<div id="divInner">
<input id="textBoxBuildID" type="number" maxlength="6" />
</div>
</div>
</div>
<br><br>
<style>
#textBoxBuildID {
border: 0px;
padding-left: 15px;
letter-spacing: 42px;
background-image: linear-gradient(to left, black 70%, rgba(255, 255, 255, 0) 0%);
background-position: bottom;
background-size: 50px 1px;
background-repeat: repeat-x;
background-position-x: 35px;
width: 330px;
min-width: 330px;
}
#divInner{
left: 0;
position: sticky;
}
#divOuter{
width: 285px;
overflow: hidden;
}
#divCode{
border: 1px solid black;
width: 300px;
margin: 0 auto;
padding: 5px;
}
#checkBoxMoney{
margin-left: 35px;
}
</style>
`,
buttons: {
yes: {
icon: "<i class='fas fa-check'></i>",
label: `Import`,
callback: () => applyChanges = true
},
no: {
icon: "<i class='fas fa-times'></i>",
label: `Cancel`
},
},
default: "yes",
close: html => {
if (applyChanges) {
let buildID= html.find('[id="textBoxBuildID"]')[0].value;
if (!isNormalInteger(buildID)){
ui.notifications.warn("Build ID must be a positive integer!");
return;
}
addFeats = html.find('[name="checkBoxFeats"]')[0].checked;
addEquipment = html.find('[name="checkBoxEquipment"]')[0].checked;
addMoney = html.find('[name="checkBoxMoney"]')[0].checked;
// addSpellcasters = html.find('[name="checkBoxSpellcasters"]')[0].checked;
deleteAll = html.find('[name="checkBoxDeleteAll"]')[0].checked;
fetchPathbuilderBuild(targetActor, buildID);
}
}
}).render(true);
}
function isNormalInteger(str) {
var n = Math.floor(Number(str));
return n !== Infinity && String(n) === str && n >= 0;
}
function fetchPathbuilderBuild(targetActor, buildID){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let responseJSON = JSON.parse(this.responseText);
console.log(responseJSON);
if (responseJSON.success){
jsonBuild = responseJSON.build;
checkCharacterIsCorrect(targetActor, responseJSON.build);
} else {
ui.notifications.warn("Unable to find a character with this build id!");
return;
}
}
};
xmlhttp.open("GET", "https://www.pathbuilder2e.com/json.php?id="+buildID, true);
xmlhttp.send();
}
function checkCharacterIsCorrect(targetActor,jsonBuild){
let correctCharacter = false;
new Dialog({
title: jsonBuild.name,
content: `
<div>Importing `+jsonBuild.name+`, level `+jsonBuild.level+`<div><br><br>
`,
buttons: {
yes: {
icon: "<i class='fas fa-check'></i>",
label: `Proceed`,
callback: () => correctCharacter = true
},
no: {
icon: "<i class='fas fa-times'></i>",
label: `Cancel`
},
},
default: "yes",
close: html => {
if (correctCharacter) {
importCharacter(targetActor, jsonBuild);
}
}
}).render(true);
}
function shouldBeManuallyDeleted(i){
// if (i.data.type=="ancestry"){
// return false;
// }
if (i.data.type=="feat"){
if (i.data.data.featType.value=="ancestryfeature"){
return false;
}
}
if (i.data.type=="spell"){
return false;
}
if (i.data.type=="spellcastingEntry"){
return false;
}
return true;
}
async function importCharacter(targetActor, jsonBuild){
if (deleteAll){
const items = targetActor.data.items.filter(i => shouldBeManuallyDeleted(i));
const deletions = items.map(i => i.id);
const updated = await targetActor.deleteEmbeddedDocuments("Item", deletions);
// let deletions = targetActor.data.items.map(i => i.id);
// let updated = await targetActor.deleteEmbeddedDocuments("Item", deletions);
} else if (addMoney){
let items = targetActor.data.items.filter(i => i.name === "Platinum Pieces" || i.name === "Gold Pieces" || i.name === "Silver Pieces" || i.name === "Copper Pieces");
let deletions = items.map(i => i.id);
let updated = await targetActor.deleteEmbeddedDocuments("Item", deletions);
}
let arrayFeats = jsonBuild.feats;
let arrayEquipment = jsonBuild.equipment;
let arrayWeapons = jsonBuild.weapons;
let arrayArmor = jsonBuild.armor;
let arraySpecials = jsonBuild.specials;
let arrayLores = jsonBuild.lores;
// lower case languages fix
for (var ref in jsonBuild.languages) {
if (jsonBuild.languages.hasOwnProperty(ref)) {
jsonBuild.languages[ref]=jsonBuild.languages[ref].toLowerCase();
}
}
// senses
var senses = [];
for (var ref in arraySpecials){
if (arraySpecials.hasOwnProperty(ref)){
if (arraySpecials[ref]=="Low-Light Vision"){
senses[0]={
exceptions: '',
label: 'Low-Light Vision',
type: "lowLightVision",
value: ''
}
} else if (arraySpecials[ref]=="Darkvision"){
senses[1]={
exceptions: '',
label: 'Darkvision',
type: "darkvision",
value: ''
}
}
}
}
// 'data.details.class.value': jsonBuild.class,
// 'data.details.ancestry.value': jsonBuild.ancestry,
targetActor.update({
'name': jsonBuild.name,
'data.details.level.value': jsonBuild.level,
'data.details.background.value': jsonBuild.background,
'data.details.heritage.value': jsonBuild.heritage,
'data.details.age.value': jsonBuild.age,
'data.details.gender.value': jsonBuild.gender,
'data.details.keyability.value': jsonBuild.keyability,
'data.traits.size.value': getSizeValue(jsonBuild.size),
'data.traits.languages.value': jsonBuild.languages,
'data.traits.senses': senses,
'data.abilities.str.value': jsonBuild.abilities.str,
'data.abilities.dex.value': jsonBuild.abilities.dex,
'data.abilities.con.value': jsonBuild.abilities.con,
'data.abilities.int.value': jsonBuild.abilities.int,
'data.abilities.wis.value': jsonBuild.abilities.wis,
'data.abilities.cha.value': jsonBuild.abilities.cha,
'data.attributes.ancestryhp': jsonBuild.attributes.ancestryhp,
'data.attributes.classhp': jsonBuild.attributes.classhp,
'data.attributes.speed.value': jsonBuild.attributes.speed,
'data.attributes.flatbonushp': jsonBuild.attributes.bonushp,
'data.saves.fortitude.rank': jsonBuild.proficiencies.fortitude/2,
'data.saves.reflex.rank': jsonBuild.proficiencies.reflex/2,
'data.saves.will.rank': jsonBuild.proficiencies.will/2,
'data.martial.advanced.rank': jsonBuild.proficiencies.advanced/2,
'data.martial.heavy.rank': jsonBuild.proficiencies.heavy/2,
'data.martial.light.rank': jsonBuild.proficiencies.light/2,
'data.martial.medium.rank': jsonBuild.proficiencies.medium/2,
'data.martial.unarmored.rank': jsonBuild.proficiencies.unarmored/2,
'data.martial.martial.rank': jsonBuild.proficiencies.martial/2,
'data.martial.simple.rank': jsonBuild.proficiencies.simple/2,
'data.martial.unarmed.rank': jsonBuild.proficiencies.unarmed/2,
'data.skills.acr.rank' : jsonBuild.proficiencies.acrobatics/2,
'data.skills.arc.rank' : jsonBuild.proficiencies.arcana/2,
'data.skills.ath.rank' : jsonBuild.proficiencies.athletics/2,
'data.skills.cra.rank' : jsonBuild.proficiencies.crafting/2,
'data.skills.dec.rank' : jsonBuild.proficiencies.deception/2,
'data.skills.dip.rank' : jsonBuild.proficiencies.diplomacy/2,
'data.skills.itm.rank' : jsonBuild.proficiencies.intimidation/2,
'data.skills.med.rank' : jsonBuild.proficiencies.medicine/2,
'data.skills.nat.rank' : jsonBuild.proficiencies.nature/2,
'data.skills.occ.rank' : jsonBuild.proficiencies.occultism/2,
'data.skills.prf.rank' : jsonBuild.proficiencies.performance/2,
'data.skills.rel.rank' : jsonBuild.proficiencies.religion/2,
'data.skills.soc.rank' : jsonBuild.proficiencies.society/2,
'data.skills.ste.rank' : jsonBuild.proficiencies.stealth/2,
'data.skills.sur.rank' : jsonBuild.proficiencies.survival/2,
'data.skills.thi.rank' : jsonBuild.proficiencies.thievery/2,
'data.attributes.perception.rank': jsonBuild.proficiencies.perception/2,
'data.attributes.classDC.rank': jsonBuild.proficiencies.classDC/2
});
// // //ancestry
if (targetActor.data.data.details.ancestry!=jsonBuild.ancestry){
if (!deleteAll){
const items = targetActor.data.items.filter(i => i.type === "ancestry");
const deletions = items.map(i => i.id);
const updated = await targetActor.deleteEmbeddedDocuments("Item", deletions); // Deletes multiple EmbeddedEntity objects
}
let packAncestry = await game.packs.get('pf2e.ancestries').getDocuments();
for (const item of packAncestry) {
if (item.data.data.slug == getSlug(jsonBuild.ancestry)){
allItems.push(item.data);
}
}
}
// //class
if (targetActor.data.data.details.class!=jsonBuild.class){
if (!deleteAll){
const items = targetActor.data.items.filter(i => i.type === "class");
const deletions = items.map(i => i.id);
const updated = await targetActor.deleteEmbeddedDocuments("Item", deletions); // Deletes multiple EmbeddedEntity objects
}
let packClasses = await game.packs.get('pf2e.classes').getDocuments();
for (const item of packClasses) {
if (item.data.data.slug == getSlug(jsonBuild.class)){
allItems.push(item.data);
}
}
}
if (addFeats){
finishedAncestryFeatures=true;
finishedClassFeatures=true;
addFeatItems(targetActor, arrayFeats);
addActionItems(targetActor, arraySpecials);
// addAncestryFeatureItems(targetActor, arraySpecials);
// addClassFeatureItems(targetActor, arraySpecials);
}else {
finishedFeats=true;
finishedAncestryFeatures=true;
finishedActions=true;
finishedClassFeatures=true;
checkAllFinishedAndCreate(targetActor);
}
if (addEquipment){
let pack = game.packs.get('pf2e.equipment-srd');
let content = await game.packs.get('pf2e.equipment-srd').getDocuments();
let backpackData = await pack.getDocuments('3lgwjrFEsQVKzhh7');
let backpackInstance = [];
let arrayKit=[];
if (hasAdventurersPack(arrayEquipment)){
// adventurers kit hack since pathbuilder allows unexploded kits and foundry doesn't
backpackInstance = await targetActor.createEmbeddedDocuments("Item", backpackData);
arrayKit.push(["bedroll", 1]);
arrayKit.push(["chalk", 10]);
arrayKit.push(["flint-and-steel", 1]);
arrayKit.push(["rope", 1]);
arrayKit.push(["rations", 14]);
arrayKit.push(["torch", 5]);
arrayKit.push(["waterskin", 1]);
}
// CURRENTLY DISABLED AS THE PATHFINDER MODULE NOW AUTOMATICALLY ADDS SPECIFIC WEAPON PROFICIENCIES
// specific proficiencies
// let specificTrained = jsonBuild.specificProficiencies.trained;
// let specificExpert = jsonBuild.specificProficiencies.expert;
// let specificMaster = jsonBuild.specificProficiencies.master;
// let specificLegendary = jsonBuild.specificProficiencies.legendary;
// let specificTrainedInstance =[];
// let specificExpertInstance =[];
// let specificMasterInstance =[];
// let specificLegendaryInstance =[];
// if (specificTrained.length>0 && needsNewInstanceofItem(targetActor, 'Specific Trained' )){
// specificTrainedInstance = await targetActor.createEmbeddedDocuments('Item', {
// name: 'Specific Trained',
// type: 'martial',
// data: { proficient: { value: 1 }}
// });
// }
// if (specificExpert.length>0 && needsNewInstanceofItem(targetActor, 'Specific Expert' )){
// specificExpertInstance = await targetActor.createEmbeddedDocuments('Item', {
// name: 'Specific Expert',
// type: 'martial',
// data: { proficient: { value: 2 }}
// });
// }
// if (specificMaster.length>0 && needsNewInstanceofItem(targetActor, 'Specific Master' )){
// specificMasterInstance = await targetActor.createEmbeddedDocuments('Item', {
// name: 'Specific Master',
// type: 'martial',
// data: { proficient: { value: 3 }}
// });
// }
// if (specificLegendary.length>0 && needsNewInstanceofItem(targetActor, 'Specific Legendary' )){
// specificLegendaryInstance = await targetActor.createEmbeddedDocuments('Item', {
// name: 'Specific Legendary',
// type: 'martial',
// data: { proficient: { value: 4 }}
// });
// }
for (const action of content.filter(item => equipmentIsRequired(item, arrayEquipment, arrayWeapons, arrayArmor, arrayKit, addMoney))) {
for (var ref in arrayEquipment) {
if (arrayEquipment.hasOwnProperty(ref)) {
var itemName= arrayEquipment[ref][0];
if (isNameMatch(itemName, action.data.data.slug) && needsNewInstanceofItem(targetActor, itemName)){
var itemAmount= arrayEquipment[ref][1];
arrayEquipment[ref].added=true;
const clonedData = JSON.parse(JSON.stringify(action.data));
if (clonedData.type!="kit"){
clonedData.data.quantity.value = itemAmount;
allItems.push(clonedData);
}
}
}
}
for (var ref in arrayKit) {
if (arrayKit.hasOwnProperty(ref)) {
var itemSlug= arrayKit[ref][0];
if (itemSlug=== action.data.data.slug && needsNewInstanceofItem(targetActor, itemName)){
var itemAmount= arrayKit[ref][1];
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.data.quantity.value = itemAmount;
clonedData.data.containerId.value = backpackInstance.id;
allItems.push(clonedData);
}
}
}
for (var ref in arrayWeapons) {
if (arrayWeapons.hasOwnProperty(ref)) {
var weaponDetails = arrayWeapons[ref];
if (isNameMatch(weaponDetails.name, action.data.data.slug) && needsNewInstanceofItem(targetActor, weaponDetails.name)){
weaponDetails.added=true;
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.data.quantity.value = weaponDetails.qty;
// if (specificTrained.includes(weaponDetails.name)){
// clonedData.data.weaponType.value = specificTrainedInstance.id;
// } else if (specificExpert.includes(weaponDetails.name)){
// clonedData.data.weaponType.value = specificExpertInstance.id;
// } else if (specificMaster.includes(weaponDetails.name)){
// clonedData.data.weaponType.value = specificMasterInstance.id;
// } else if (specificLegendary.includes(weaponDetails.name)){
// clonedData.data.weaponType.value = specificLegendaryInstance.id;
// } else {
// clonedData.data.weaponType.value = weaponDetails.prof;
// }
clonedData.data.damage.die = weaponDetails.die;
clonedData.data.potencyRune.value = weaponDetails.pot.toString();
clonedData.data.strikingRune.value = weaponDetails.str;
if (weaponDetails.runes[0]){
clonedData.data.propertyRune1.value=camelCase(weaponDetails.runes[0]);
}
if (weaponDetails.runes[1]){
clonedData.data.propertyRune2.value==camelCase(weaponDetails.runes[1]);
}
if (weaponDetails.runes[2]){
clonedData.data.propertyRune3.value==camelCase(weaponDetails.runes[2]);
}
if (weaponDetails.runes[3]){
clonedData.data.propertyRune4.value=camelCase(weaponDetails.runes[3]);
}
if (weaponDetails.mat){
let material = weaponDetails.mat.split(" (")[0];
clonedData.data.preciousMaterial.value = camelCase(material);
clonedData.data.preciousMaterialGrade.value = getMaterialGrade(weaponDetails.mat);
}
if (weaponDetails.display){
clonedData.name = weaponDetails.display;
}
allItems.push(clonedData);
}
}
}
for (var ref in arrayArmor) {
if (arrayArmor.hasOwnProperty(ref)) {
var armorDetails = arrayArmor[ref];
if (isNameMatch(armorDetails.name, action.data.data.slug) && needsNewInstanceofItem(targetActor, armorDetails.name)){
armorDetails.added=true;
const clonedData = JSON.parse(JSON.stringify(action.data));
if (notBracersOfArmor(armorDetails.name)){
clonedData.data.quantity.value = armorDetails.qty;
clonedData.data.armorType.value = armorDetails.prof;
clonedData.data.potencyRune.value = armorDetails.pot.toString();
clonedData.data.resiliencyRune.value = armorDetails.res;
// this will also catch the nulls from early json data which did not have this value
if (armorDetails.worn){
clonedData.data.equipped.value = true;
} else {
clonedData.data.equipped.value = false;
}
if (armorDetails.runes[0]){
clonedData.data.propertyRune1.value=camelCase(armorDetails.runes[0]);
}
if (armorDetails.runes[1]){
clonedData.data.propertyRune2.value=camelCase(armorDetails.runes[1]);
}
if (armorDetails.runes[2]){
clonedData.data.propertyRune3.value=camelCase(armorDetails.runes[2]);
}
if (armorDetails.runes[3]){
clonedData.data.propertyRune4.value=camelCase(armorDetails.runes[3]);
}
if (armorDetails.mat){
let material = armorDetails.mat.split(" (")[0];
clonedData.data.preciousMaterial.value = camelCase(material);
clonedData.data.preciousMaterialGrade.value = getMaterialGrade(armorDetails.mat);
}
if (armorDetails.display){
clonedData.name = armorDetails.display;
}
}
allItems.push(clonedData);
}
}
}
if (addMoney){
if (action.data.data.slug==='platinum-pieces'){
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.data.quantity.value = jsonBuild.money.pp;
allItems.push(clonedData);
} else if (action.data.data.slug==='gold-pieces'){
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.data.quantity.value = jsonBuild.money.gp;
allItems.push(clonedData);
} else if (action.data.data.slug==='silver-pieces'){
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.data.quantity.value = jsonBuild.money.sp;
allItems.push(clonedData);
} else if (action.data.data.slug==='copper-pieces'){
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.data.quantity.value = jsonBuild.money.cp;
allItems.push(clonedData);
}
}
}
finishedEquipment=true;
checkAllFinishedAndCreate(targetActor);
} else {
finishedEquipment=true;
checkAllFinishedAndCreate(targetActor);
}
if (addSpellcasters){
setSpellcasters(targetActor, jsonBuild.spellCasters, deleteAll);
} else {
finishedSpells=true;
checkAllFinishedAndCreate(targetActor);
}
addLores(targetActor, arrayLores);
}
// function getExistingClassSlug(targetActor){
// for (const item of targetActor.data.items) {
// console.log(item.data.type);
// if (item.data.type =="class"){
// return item.data.slug;
// }
// }
// // for (var ref in targetActor.data.items) {
// // if (targetActor.data.items.hasOwnProperty(ref)) {
// // let item =targetActor.data.items[ref];
// // if (item.type =="class"){
// // return item.data.slug;
// // }
// // }
// // }
// return null;
// }
function getExistingAncestrySlug(targetActor){
for (var ref in targetActor.data.items) {
if (targetActor.data.items.hasOwnProperty(ref)) {
let item =targetActor.data.items[ref];
if (item.type =="ancestry"){
return item.data.slug;
}
}
}
return null;
}
function notBracersOfArmor(name){
return !name.toLowerCase().includes("bracers of armor");
}
function camelCase(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index)
{
return index == 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
function getMaterialGrade(material){
if (material.toLowerCase().includes("high-grade")){
return "high";
} else if (material.toLowerCase().includes("standard-grade")){
return "standard";
}
return "low";
}
async function addFeatItems(targetActor, arrayFeats){
var usedLocations =[];
let content = await game.packs.get('pf2e.feats-srd').getDocuments();
for (const action of content.filter(item => featIsRequired(item, arrayFeats))) {
for (var ref in arrayFeats) {
if (arrayFeats.hasOwnProperty(ref)) {
let pathbuilderFeatItem = arrayFeats[ref];
var itemName= pathbuilderFeatItem[0];
var itemExtra= pathbuilderFeatItem[1];
if (isNameMatch(itemName, action.data.data.slug) && needsNewInstanceofFeat(targetActor, itemName, itemExtra)){
var displayName = itemName;
addedItems.push(itemName);
if (itemExtra!=null){
displayName +=" ("+itemExtra+")";
}
const clonedData = JSON.parse(JSON.stringify(action.data));
clonedData.name = displayName;
try{
if (pathbuilderFeatItem[2] && pathbuilderFeatItem[3]){
let location = getFoundryFeatLocation(pathbuilderFeatItem[2], pathbuilderFeatItem[3]);
if (!usedLocations.includes(location)){
clonedData.data.location = location;
usedLocations.push(location);
}
}
} catch(err){
console.log(err);
}
allItems.push(clonedData);
}
}
}
}
finishedFeats=true;
checkAllFinishedAndCreate(targetActor);
}
function isNameMatch(pathbuilderItemName, foundryItemSlug){
if (getSlug(pathbuilderItemName)==foundryItemSlug)return true;
if (getSlug(getClassAdjustedSpecialNameLowerCase(pathbuilderItemName)) == foundryItemSlug) return true;
return false;
}
async function addActionItems(targetActor, arraySpecials){
let content = await game.packs.get('pf2e.actionspf2e').getDocuments();
for (const action of content.filter(item => specialIsRequired(item, arraySpecials))) {
for (var ref in arraySpecials) {
if (arraySpecials.hasOwnProperty(ref)) {
var itemName= arraySpecials[ref];
if (isNameMatch(itemName, action.data.data.slug) && needsNewInstanceofItem(targetActor, itemName)){
addedItems.push(itemName);
allItems.push(action.data);
}
}
}
}
finishedActions = true;
checkAllFinishedAndCreate(targetActor);
}
async function addAncestryFeatureItems(targetActor, arraySpecials){
let content = await game.packs.get('pf2e.ancestryfeatures').getDocuments();
for (const action of content.filter(item => specialIsRequired(item, arraySpecials))) {
for (var ref in arraySpecials) {
if (arraySpecials.hasOwnProperty(ref)) {
var itemName= arraySpecials[ref];
if (isNameMatch(itemName, action.data.data.slug) && needsNewInstanceofItem(targetActor, itemName)){
addedItems.push(itemName);
allItems.push(action.data);
}
}
}
}
finishedAncestryFeatures = true;
checkAllFinishedAndCreate(targetActor);
}
async function addClassFeatureItems(targetActor, arraySpecials){
let content = await game.packs.get('pf2e.classfeatures').getDocuments();
for (const action of content.filter(item => specialIsRequired(item, arraySpecials))) {
for (var ref in arraySpecials) {
if (arraySpecials.hasOwnProperty(ref)) {
var itemName= arraySpecials[ref];
if (isNameMatch(itemName, action.data.data.slug) && needsNewInstanceofItem(targetActor, itemName)){
addedItems.push(itemName);
allItems.push(action.data);
}
}
}
}
finishedClassFeatures = true;
checkAllFinishedAndCreate(targetActor);
}
function hasAdventurersPack(arrayEquipment){
for (var ref in arrayEquipment) {
if (arrayEquipment.hasOwnProperty(ref)) {
var itemName= arrayEquipment[ref][0];
if (itemName==="Adventurer's Pack"){
arrayEquipment[ref].added=true;
return true;
}
}
}
return false;
}
function isSpecialsPack(packName){
return packName==='actionspf2e' || packName==='ancestryfeatures' || packName==='classfeatures';
}
function featIsRequired(item, arrayFeats){
for (var featDetails in arrayFeats) {
if (arrayFeats.hasOwnProperty(featDetails)) {
if (getSlug(arrayFeats[featDetails][0]) == item.data.data.slug){
return true;
}
if (getSlug(getClassAdjustedSpecialNameLowerCase(arrayFeats[featDetails][0]))== item.data.data.slug) return true;
}
}
return false;
}
function specialIsRequired(item, arraySpecials){
for (var ref in arraySpecials) {
if (arraySpecials.hasOwnProperty(ref)) {
if (getSlug(arraySpecials[ref]) == item.data.data.slug) return true;
if (getSlug(getClassAdjustedSpecialNameLowerCase(arraySpecials[ref]))== item.data.data.slug) return true;
}
}
return false;
}
function equipmentIsRequired(item, arrayEquipment, arrayWeapons, arrayArmor, arrayKit, addMoney){
for (var ref in arrayEquipment) {
if (arrayEquipment.hasOwnProperty(ref)) {
if (getSlug(arrayEquipment[ref][0]) === item.data.data.slug) return true;
}
}
for (var ref in arrayWeapons) {
if (arrayWeapons.hasOwnProperty(ref)) {
if (getSlug(arrayWeapons[ref].name) === item.data.data.slug) return true;
}
}
for (var ref in arrayArmor) {
if (arrayArmor.hasOwnProperty(ref)) {
if (getSlug(arrayArmor[ref].name) === item.data.data.slug) return true;
}
}
for (var ref in arrayKit) {
if (arrayKit.hasOwnProperty(ref)) {
if (arrayKit[ref][0] === item.data.data.slug) return true;
}
}
if (addMoney && (item.data.data.slug==="platinum-pieces" || item.data.data.slug==="gold-pieces" || item.data.data.slug==="silver-pieces" || item.data.data.slug==="copper-pieces")){
return true;
}
return false;
}
function getClassAdjustedSpecialNameLowerCase(specialName){
var name = specialName+" ("+jsonBuild.class+")";
return name.toLowerCase();
}
function needsNewInstanceofFeat(targetActor, itemName, itemExtra){
for (const existingItem of targetActor.data.items) {
var displayName = itemName;
if (itemExtra!=null)displayName +=" ("+itemExtra+")";
if (existingItem.data.name===displayName)return false;
}
return true;
}
function needsNewInstanceofItem(targetActor, itemName){
for (var ref in targetActor.data.items) {
if (targetActor.data.items.hasOwnProperty(ref)) {
if (targetActor.data.items[ref].name===itemName)return false;
}
}
return true;
}
function getSizeValue(size){
switch(size) {
case 0:
return "tiny";
case 1:
return "sm";
case 3:
return "lg";
}
return "med";
}
/// spells
async function setSpellcasters(targetActor, arraySpellcasters, deleteAll){
// // delete existing spellcasters and spells if not already deleted || i.type === "spell"
// if (!deleteAll){
// let items = targetActor.data.items.filter(i => i.type === "spellcastingEntry");
// let deletions = items.map(i => i.id);
// let updated = await targetActor.deleteEmbeddedDocuments("Item", deletions);
// }
// make array of spellcaster instances. put
let requiredSpells=[];
for (var ref in arraySpellcasters) {
if (arraySpellcasters.hasOwnProperty(ref)) {
let spellCaster = arraySpellcasters[ref];
spellCaster.instance = await addSpecificCasterAndSpells(targetActor, spellCaster, spellCaster.magicTradition, spellCaster.spellcastingType);
// for (var ref in spellCaster.spells) {
// if (spellCaster.spells.hasOwnProperty(ref)) {
// let spellListObject = spellCaster.spells[ref];
// requiredSpells = requiredSpells.concat(spellListObject.list);
// }
// }
}
}
finishedSpells=true;
checkAllFinishedAndCreate(targetActor);
// game.packs.filter(pack => pack.metadata.name === 'spells-srd').forEach(async (pack) => {
// const content = await pack.getDocuments();
// for (const action of content.filter(item => spellIsRequired(item, requiredSpells))) {
// arraySpellcasters.forEach(spellCaster => {
// for (var ref in spellCaster.spells) {
// if (spellCaster.spells.hasOwnProperty(ref)) {
// let spellListObject = spellCaster.spells[ref];
// for (var ref in spellListObject.list) {
// if (spellListObject.list.hasOwnProperty(ref)) {
// if (getSlug(spellListObject.list[ref])==action.data.data.slug){
// const clonedData = JSON.parse(JSON.stringify(action.data));
// clonedData.data.location.value = spellCaster.instance.id;
// clonedData.data.level.value = spellListObject.spellLevel;
// allItems.push(clonedData);
// }
// }
// }
// }
// }
// });
// }
// finishedSpells=true;
// checkAllFinishedAndCreate(targetActor);
// });
}