Skip to content

Commit

Permalink
spell validation and import multiple npcs with spells
Browse files Browse the repository at this point in the history
  • Loading branch information
HarmlessHarm committed Jun 2, 2023
1 parent 5c1abba commit 30029b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
56 changes: 32 additions & 24 deletions src/components/ImportContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,10 @@ export default {
this.custom_spells &&
Object.keys(this.custom_spells).length <= this.availableSpellSlots
) {
console.log("spells to add", this.custom_spells);
for (const [key, spell] of Object.entries(this.custom_spells)) {
try {
// TODO: use parse function to filter out spells
delete spell.key;
delete spell.updated;
delete spell.created;
await this.edit_spell({ id: key, spell: spell });
await this.add_spell({ spell: spell, predefined_key: key });
} catch (error) {
this.failed_imports.push(spell);
}
Expand Down Expand Up @@ -631,31 +627,43 @@ export default {
return npc;
},
async parseCustomSpells(npc) {
for (const [old_key, spell] of Object.entries(npc.custom_spells)) {
// Check if there already is a spell with name
let spell_id = await this.get_spell_id_by_name({ name: spell.name });
if (!spell_id) {
// Generate a id for the spell so when we can link the spell in NPC to a future spell
const new_spell_id = await this.reserve_spell_id();
spell_id = new_spell_id;
this.$set(this.custom_spells, spell_id, spell);
}
if (npc.custom_spells) {
for (const [old_key, spell] of Object.entries(npc.custom_spells)) {
// Check if there already is a spell with name
delete spell.key;
delete spell.updated;
delete spell.created;
const valid = ajv.validate(spellSchema, spell);
if (!valid) {
spell.errors = ajv.errors;
}
// Check if spell is already known to importer
if (!Object.keys(this.map_old_to_custom).includes(old_key)) {
let spell_id = await this.get_spell_id_by_name({ name: spell.name });
if (!spell_id) {
// Generate a id for the spell so when we can link the spell in NPC to a future spell
const new_spell_id = await this.reserve_spell_id();
spell_id = new_spell_id;
this.$set(this.custom_spells, spell_id, spell);
}
this.map_old_to_custom[old_key] = spell_id;
}
this.map_old_to_custom[old_key] = spell_id;
}
}
for (const spell_list_type of ["caster_spells", "innate_spells"]) {
if (npc[spell_list_type]) {
const spell_list = Object.assign({}, npc[spell_list_type]);
for (const [spell_key, spell] of Object.entries(spell_list)) {
if (spell.custom && spell_key in this.map_old_to_custom) {
npc[spell_list_type][this.map_old_to_custom[spell_key]] = { ...spell };
delete npc[spell_list_type][spell_key];
for (const spell_list_type of ["caster_spells", "innate_spells"]) {
if (npc[spell_list_type]) {
const spell_list = Object.assign({}, npc[spell_list_type]);
for (const [spell_key, spell] of Object.entries(spell_list)) {
if (spell.custom && spell_key in this.map_old_to_custom) {
npc[spell_list_type][this.map_old_to_custom[spell_key]] = { ...spell };
delete npc[spell_list_type][spell_key];
}
}
}
}
delete npc.custom_spells;
}
delete npc.custom_spells;
return npc;
},
},
Expand Down
15 changes: 11 additions & 4 deletions src/services/spells.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,25 @@ export class SpellServices {
* @param {Object} search_spell Compressed spell
* @returns Key of the newly added spell
*/
async addSpell(uid, spell, search_spell) {
async addSpell(uid, spell, search_spell, predefined_key = undefined) {
try {
spell.name = spell.name.toLowerCase();
const newSpell = await SPELLS_REF.child(uid).push(spell);

spell.created = firebase.database.ServerValue.TIMESTAMP;
spell.updated = firebase.database.ServerValue.TIMESTAMP;

let spell_key = predefined_key;
if (predefined_key) {
await SPELLS_REF.child(uid).child(predefined_key).set(spell);
} else {
newSpell = await SPELLS_REF.child(uid).push(spell);
spell_key = newSpell.key;
}

// Update search_spells
SEARCH_SPELLS_REF.child(`${uid}/results/${newSpell.key}`).set(search_spell);
SEARCH_SPELLS_REF.child(`${uid}/results/${spell_key}`).set(search_spell);

return newSpell.key;
return spell_key;
} catch (error) {
throw error;
}
Expand Down
8 changes: 4 additions & 4 deletions src/store/modules/userContent/spells.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const spell_actions = {
* @param {object} spell
* @returns {string} the id of the newly added spell
*/
async add_spell({ rootGetters, commit, dispatch }, spell) {
async add_spell({ rootGetters, commit, dispatch }, { spell, predefined_key }) {
const uid = rootGetters.user ? rootGetters.user.uid : undefined;
const available_slots = rootGetters.tier.benefits.spells;

Expand All @@ -142,7 +142,7 @@ const spell_actions = {
}
try {
const search_spell = convert_spell(spell);
const id = await services.addSpell(uid, spell, search_spell);
const id = await services.addSpell(uid, spell, search_spell, predefined_key);
commit("SET_SPELL", { id, search_spell });
commit("SET_CACHED_SPELL", { uid, id, spell });

Expand Down Expand Up @@ -212,8 +212,8 @@ const spell_actions = {
const new_count = await services.updateSpellCount(uid, value);
commit("SET_SPELL_COUNT", new_count);
dispatch("checkEncumbrance", "", { root: true });
} catch(error) {
throw(error);
} catch (error) {
throw error;
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/views/UserContent/Npcs/Npcs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export default {
const all_npcs = await this.get_full_npcs();
for (const key in all_npcs) {
all_npcs[key].harmless_key = key;
this.addCustomSpellToExport(all_npcs[key]);
await this.addCustomSpellToExport(all_npcs[key]);
}
const json_export = Object.values(all_npcs);
Expand Down

0 comments on commit 30029b6

Please sign in to comment.