UNPKG

@evilmonkeyinc/srd-api

Version:

Tools and helpers to query SRD content

251 lines 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class SpellAPI { constructor(spells) { this.spellByName = new Map(); this.spellsByClass = new Map(); this.spellsByLevel = new Map(); this.spellsBySchool = new Map(); this.spellsByAttackType = new Map(); this.spellsBySaveType = new Map(); this.concentrationSpells = new Array(); this.ritualSpells = new Array(); this.spellsByCastingTime = new Map(); this.materialSpells = new Array(); this.somaticSpells = new Array(); this.verbalSpells = new Array(); this.spellsByDamageType = new Map(); this.spellsByConditionType = new Map(); this.spellsByDuration = new Map(); spells.forEach((spell) => { var _a, _b; const key = spell.name.toLowerCase(); this.spellByName.set(key, spell); spell.classes.forEach((classType) => { let classArray = this.spellsByClass.get(classType.toLowerCase()) || []; classArray.push(key); this.spellsByClass.set(classType.toLowerCase(), classArray); }); let levelArray = this.spellsByLevel.get(spell.level) || []; levelArray.push(key); this.spellsByLevel.set(spell.level, levelArray); let schoolArray = this.spellsBySchool.get(spell.school) || []; schoolArray.push(key); this.spellsBySchool.set(spell.school, schoolArray); if (spell.attack !== undefined) { let array = this.spellsByAttackType.get(spell.attack) || []; array.push(key); this.spellsByAttackType.set(spell.attack, array); } if (spell.save !== undefined) { let array = this.spellsBySaveType.get(spell.save) || []; array.push(key); this.spellsBySaveType.set(spell.save, array); } if (spell.concentration) { this.concentrationSpells.push(key); } if (spell.ritual) { this.ritualSpells.push(key); } if (spell.components.material !== undefined) { this.materialSpells.push(key); } if (spell.components.somatic) { this.somaticSpells.push(key); } if (spell.components.verbal) { this.verbalSpells.push(key); } (_a = spell.damageType) === null || _a === void 0 ? void 0 : _a.forEach((damageType) => { let array = this.spellsByDamageType.get(damageType) || []; array.push(key); this.spellsByDamageType.set(damageType, array); }); (_b = spell.conditions) === null || _b === void 0 ? void 0 : _b.forEach((condition) => { let array = this.spellsByConditionType.get(condition.toLowerCase()) || []; array.push(key); this.spellsByConditionType.set(condition.toLowerCase(), array); }); const durationArray = this.spellsByDuration.get(spell.duration) || []; durationArray.push(key); this.spellsByDuration.set(spell.duration, durationArray); let castingTimeArray = this.spellsByCastingTime.get(spell.castingTime) || []; castingTimeArray.push(key); this.spellsByCastingTime.set(spell.castingTime, castingTimeArray); }); } get(name) { return this.spellByName.get(name.toLowerCase()); } list() { return Array.from(this.spellByName.values()); } query(query) { let spellList = new Array(); let spellNameList = Array.from(this.spellByName.keys()); if (query.classes !== undefined && query.classes.length > 0) { let array = new Array(); query.classes.forEach((classType) => { let subArray = this.spellsByClass.get(classType.toLowerCase()) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.levels !== undefined && query.levels.length > 0) { let array = new Array(); query.levels.forEach((level) => { let subArray = this.spellsByLevel.get(level) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.schools !== undefined) { let array = new Array(); query.schools.forEach((school) => { let subArray = this.spellsBySchool.get(school) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.attackTypes !== undefined && query.attackTypes.length > 0) { let array = new Array(); query.attackTypes.forEach((attackType) => { let subArray = this.spellsByAttackType.get(attackType) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.saveTypes !== undefined && query.saveTypes.length > 0) { let array = new Array(); query.saveTypes.forEach((saveType) => { let subArray = this.spellsBySaveType.get(saveType) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.concentration !== undefined) { if (query.concentration) { spellNameList = spellNameList.filter((value) => this.concentrationSpells.includes(value)); } else { spellNameList = spellNameList.filter((value) => !this.concentrationSpells.includes(value)); } } if (query.ritual !== undefined) { if (query.ritual) { spellNameList = spellNameList.filter((value) => this.ritualSpells.includes(value)); } else { spellNameList = spellNameList.filter((value) => !this.ritualSpells.includes(value)); } } if (query.castingTime !== undefined && query.castingTime.length > 0) { let array = new Array(); query.castingTime.forEach((castingTime) => { let subArray = this.spellsByCastingTime.get(castingTime) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.name !== undefined && query.name !== '') { const regex = new RegExp(`.*${query.name.split(' ').join('.*')}.*`, 'gmi'); spellNameList = spellNameList.filter((value) => value.match(regex)); } if (query.components !== undefined) { if (query.components.material !== undefined) { if (query.components.material) { spellNameList = spellNameList.filter((value) => this.materialSpells.includes(value)); } else { spellNameList = spellNameList.filter((value) => !this.materialSpells.includes(value)); } } if (query.components.somatic !== undefined) { if (query.components.somatic) { spellNameList = spellNameList.filter((value) => this.somaticSpells.includes(value)); } else { spellNameList = spellNameList.filter((value) => !this.somaticSpells.includes(value)); } } if (query.components.verbal !== undefined) { if (query.components.verbal) { spellNameList = spellNameList.filter((value) => this.verbalSpells.includes(value)); } else { spellNameList = spellNameList.filter((value) => !this.verbalSpells.includes(value)); } } } if (query.damageTypes !== undefined && query.damageTypes.length > 0) { let array = new Array(); query.damageTypes.forEach((damageType) => { let subArray = this.spellsByDamageType.get(damageType) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.conditions !== undefined && query.conditions.length > 0) { let array = new Array(); query.conditions.forEach((condition) => { let subArray = this.spellsByConditionType.get(condition.toLowerCase()) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } if (query.durations !== undefined && query.durations.length > 0) { let array = new Array(); query.durations.forEach((duration) => { let subArray = this.spellsByDuration.get(duration) || []; if (subArray.length > 0) { array.push(...subArray); } }); if (array.length > 0) { spellNameList = spellNameList.filter((value) => array.includes(value)); } } spellNameList.forEach((spellName) => { const spell = this.spellByName.get(spellName); if (spell !== undefined) { spellList.push(spell); } }); return spellList; } } exports.default = SpellAPI; //# sourceMappingURL=SpellAPI.js.map