-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
spells.js
299 lines (273 loc) · 11.7 KB
/
spells.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
const fetch = require("node-fetch");
const CONFIG = require("./config.js");
const authentication = require("./auth.js");
const isValidData = data => {
return data.success === true;
};
const removeCantrips = data => {
const filteredSpellList = data.filter(spell => {
return spell.definition.level > 0;
});
return filteredSpellList;
};
const filterByLevel = (data, spellLevelAccess) => {
const filteredSpellList = data.filter(spell => {
return spell.definition.level <= spellLevelAccess;
});
return filteredSpellList;
};
const extractSpells = (classInfo, cobaltId) => {
return new Promise((resolve, reject) => {
const { name, id, spellLevelAccess } = classInfo;
console.log(`Retrieving all spells for ${name} ${cobaltId} (${id}) at spell level ${spellLevelAccess}`);
const url = CONFIG.urls.spellsAPI(id, 20, classInfo.campaignId);
const headers = (authentication.CACHE_AUTH.exists(cobaltId).data !== null) ? {headers: {"Authorization": `Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}`}} : {};
fetch(url, headers)
.then(res => res.json())
.then(json => {
// console.log(json.data.map(sp => sp.definition.name).join(", "));
if (isValidData(json)) {
const filteredSpells = filterByLevel(json.data, spellLevelAccess).filter(item => {
if (item.definition.sources && item.definition.sources.some((source) => source.sourceId === 39)) {
return false;
} else {
return true;
}
});
console.log(
`Adding ${filteredSpells.length} of ${json.data.length} spells available to a lvl${spellLevelAccess} ${classInfo.name} caster...`
);
resolve(filteredSpells);
} else {
console.log("Received no valid spell data, instead:" + json.message);
reject(json.message);
}
})
.catch(error => {
console.log("Error retrieving spells");
console.log(error);
reject(error);
});
});
};
const extractAlwaysPreparedSpells = (classInfo, spellListIds=[]) => {
return new Promise((resolve, reject) => {
const { name, id, spellLevelAccess } = classInfo;
console.log(`Retrieving always prepared spells for ${name} (${id}) at spell level ${spellLevelAccess}`);
const url = CONFIG.urls.alwaysPreparedSpells(id, 20, classInfo.campaignId, spellListIds);
fetch(url)
.then(res => res.json())
.then(json => {
// console.log(json.data.map(sp => sp.definition.name).join(", "));
if (isValidData(json)) {
const filteredSpells = filterByLevel(json.data, spellLevelAccess).filter(item => {
if (item.definition.sources && item.definition.sources.some((source) => source.sourceId === 39)) {
return false;
} else {
return true;
}
});
console.log(
`Adding ${filteredSpells.length} of ${json.data.length} allways prepared spells available to a lvl${spellLevelAccess} ${classInfo.name} caster...`
);
resolve(filteredSpells);
} else {
console.log("Received no valid spell data, instead:" + json.message);
reject(json.message);
}
})
.catch(error => {
console.log("Error retrieving spells");
console.log(error);
reject(error);
});
});
};
const extractAlwaysKnownSpells = (classInfo, cobaltId, cantrips, spellListIds=[]) => {
console.log(`SPELL IDS ${spellListIds}`);
return new Promise((resolve, reject) => {
const { name, id, spellLevelAccess } = classInfo;
console.log(`Retrieving all known spells for ${name} (${id}) at spell level ${spellLevelAccess}`);
const url = CONFIG.urls.alwaysKnownSpells(id, 20, classInfo.campaignId, spellListIds, classInfo.backgroundId);
console.log(url);
// console.log(`Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}`);
const headers = (authentication.CACHE_AUTH.exists(cobaltId).data !== null) ? {headers: {"Authorization": `Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}`}} : {};
fetch(url, headers)
.then(res => res.json())
.then(json => {
// console.log(json.data.map(sp => sp.definition.name).join(", "));
if (isValidData(json)) {
const noCantripSpells = (cantrips) ? json.data : removeCantrips(json.data);
const filteredSpells = filterByLevel(noCantripSpells, spellLevelAccess).filter(item => {
if (item.definition.sources && item.definition.sources.some((source) => source.sourceId === 39)) {
return false;
} else {
return true;
}
});
console.log(
`Adding ${filteredSpells.length} of ${json.data.length} always known spells available to a lvl${spellLevelAccess} ${classInfo.name} caster...`
);
resolve(filteredSpells);
} else {
console.log("Received no valid spell data, instead:" + json.message);
reject(json.message);
}
})
.catch(error => {
console.log("Error retrieving spells");
console.log(error);
reject(error);
});
});
};
const extractCasterLevel = (cls, isMultiClassing) => {
let casterLevel = 0;
if (isMultiClassing) {
// get the casting level if the character is a multiclassed spellcaster
if (cls.definition.spellRules && cls.definition.spellRules.multiClassSpellSlotDivisor) {
casterLevel = Math.floor(cls.level / cls.definition.spellRules.multiClassSpellSlotDivisor);
}
} else {
casterLevel = cls.level;
}
return casterLevel;
};
const extractSpellLevelAccess = (cls, casterLevel) => {
const spellSlots = cls.definition.spellRules.levelSpellSlots[casterLevel];
const spellLevelAccess = spellSlots.reduce((count, numSpellSlots) => (numSpellSlots > 0 ? count + 1 : count), 0);
return spellLevelAccess;
};
const extractClassIds = (data) => {
const isMultiClassing = data.character.classes.length > 1;
return data.character.classes.map(characterClass => {
return {
characterClassId: characterClass.id,
backgroundId: data.character.background.definition && data.character.background.definition.id ? data.character.background.definition.id : null,
name:
characterClass.subclassDefinition && characterClass.subclassDefinition.name
? characterClass.definition.name + ` (${characterClass.subclassDefinition.name})`
: characterClass.definition.name,
id:
characterClass.subclassDefinition && characterClass.subclassDefinition.id
? characterClass.subclassDefinition.id
: characterClass.definition.id,
level: extractCasterLevel(characterClass, isMultiClassing),
spellLevelAccess: extractSpellLevelAccess(characterClass, extractCasterLevel(characterClass)),
spells: [],
classId: characterClass.definition.id,
subclassId: characterClass.subclassDefinition ? characterClass.subclassDefinition.id : null,
characterClass: characterClass.definition.name,
characterSubclass: characterClass.subclassDefinition ? characterClass.subclassDefinition.name : null,
characterId: data.character.id,
campaignId: (data.character.campaign) ? data.character.campaign.id : null,
spellListIds: data.classOptions
? data.classOptions
.filter((option) => option.spellListIds)
.filter((option) =>
option.spellListIds && option.spellListIds.length > 0
&& (option.classId == characterClass.definition.id
|| (characterClass.subclassDefinition && characterClass.subclassDefinition.id == option.classId))
)
.map((option) => {
return option.spellListIds;
})
.flat()
: [],
};
});
};
async function extractExtraSpellsForClass(klassInfo, cobaltId) {
const cobaltToken = authentication.CACHE_AUTH.exists(cobaltId);
const knowSpellsClasses = ["Druid", "Cleric", "Paladin", "Artificer"];
console.log("[ ALWAYS KNOWN SPELLS =========================================== ]");
const alwaysKnownSpells = cobaltToken && knowSpellsClasses.includes(klassInfo.characterClass)
? await extractAlwaysKnownSpells(klassInfo, cobaltId, true, klassInfo.spellListIds)
: [];
console.log("[ ALWAYS PREPARED SPELLS ======================================== ]");
const alwaysPreparedSpells = await extractAlwaysPreparedSpells(klassInfo, klassInfo.spellListIds);
return {
alwaysKnownSpells,
alwaysPreparedSpells,
};
}
// this is used by the character loader to load class spells
const loadSpellAdditions = (classInfo, cobaltId) => {
return new Promise((resolve, reject) => {
Promise.allSettled(classInfo.map(info => {
return extractExtraSpellsForClass(info, cobaltId);
}))
.then(results => {
// combining all resolved results
results.forEach((result, index) => {
classInfo[index].spells = result.value.alwaysKnownSpells.concat(result.value.alwaysPreparedSpells);
});
resolve(classInfo);
})
.catch(error => reject(error));
});
};
// this is used by the spell muncher to munch all class spells
const loadSpells = (classInfo, cobaltToken, cantrips) => {
return new Promise((resolve, reject) => {
Promise.allSettled(classInfo.map(info => {
const knownSpells = extractAlwaysKnownSpells(info, cobaltToken, cantrips);
const otherSpells = extractSpells(info, cobaltToken); // for cantrips etc
return Promise.all([knownSpells, otherSpells]);
}))
.then(results => {
// combining all resolved results
results.forEach((result, index) => {
if (result.status === "fulfilled") {
classInfo[index].spells = result.value.flat();
}
});
resolve(classInfo);
})
.catch(error => reject(error));
});
};
function getSpellAdditions(data, cacheId) {
return new Promise((resolve) => {
const classInfo = extractClassIds(data);
console.log("CLASS INFORMATION FOR SPELL ADDITIONS:");
console.log(classInfo);
loadSpellAdditions(classInfo, cacheId).then(classInfo => {
// add the always prepared spells to the class' spell list
data.character.classSpells = data.character.classSpells.map(classSpells => {
// find always prepared spells in the results
const additionalSpells = classInfo.find(
classInfo => classInfo.characterClassId === classSpells.characterClassId
);
if (additionalSpells) {
additionalSpells.spells.forEach(spell => {
console.log("Adding spells to character...");
console.log(" + Adding spell to character: " + spell.definition.name);
classSpells.spells.push(spell);
});
}
return classSpells;
});
console.log("******** ADDITIONAL SPELL LOAD FINISHED ***********");
resolve(data);
});
});
}
function filterHomebrew(data, includeHomebrew) {
if (includeHomebrew) {
return data;
} else {
data.character.classSpells = data.character.classSpells.map((classSpells) => {
classSpells.spells = classSpells.spells.filter(spell => !spell.definition.isHomebrew);
return classSpells;
});
data.character.spells.class = data.character.spells.class.filter(spell => !spell.definition || !spell.definition.isHomebrew);
data.character.spells.race = data.character.spells.race.filter(spell => !spell.definition || !spell.definition.isHomebrew);
data.character.spells.feat = data.character.spells.feat.filter(spell => !spell.definition || !spell.definition.isHomebrew);
data.character.spells.item = data.character.spells.item.filter(spell => !spell.definition || !spell.definition.isHomebrew);
return data;
}
}
exports.loadSpells = loadSpells;
exports.getSpellAdditions = getSpellAdditions;
exports.filterHomebrew = filterHomebrew;