Skip to content

Commit

Permalink
Merge pull request #181 from VTTAssets/code_clean_5
Browse files Browse the repository at this point in the history
Linting promises: clean up npc add spell
  • Loading branch information
MrPrimate committed May 30, 2020
2 parents 6b35d3b + ddbd451 commit 1ad59f2
Showing 1 changed file with 77 additions and 64 deletions.
141 changes: 77 additions & 64 deletions src/messaging/type/add/spell.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,94 @@
import utils from "../../../utils.js";

const SAVE_ALL = 0;
const SAVE_NEW = 1;
const SAVE_NONE = 2;

const CLEAN_NONE = 0;
const CLEAN_MONSTERS = 1;
const CLEAN_SPELLS = 2;
const CLEAN_ALL = 3;

let addSpell = (body) => {
return new Promise(async (resolve, reject) => {
// cleaning up after imports
const cleanupAfterImport =
game.settings.get("vtta-dndbeyond", "entity-cleanup-policy") ===
CLEAN_ALL ||
game.settings.get("vtta-dndbeyond", "entity-cleanup-policy") ===
CLEAN_SPELLS;
const createSpell = async (data) => {
// get the folder to add this spell into
const folder = await utils.getFolder("spell");
data.folder = folder._id;

// get the folder to add this spell into
let folder = await utils.getFolder(body.type);
body.data.folder = folder.id;
// check if there is a Spell with that name in that folder already
let spell = folder.content ? folder.content.find((spell) => spell.name === data.name) : undefined;
if (spell) {
data._id = spell._id;
// update the current spell
await spell.update(data);
} else {
// create the new spell
spell = await Item.create(data, {
temporary: false,
displaySheet: true,
});
}

// check if there is an NPC with that name in that folder already
let spell = folder.content
? folder.content.find((spell) => spell.name === body.data.name)
: undefined;
if (spell) {
body.data._id = spell.id;
// update the current npc
await spell.update(body.data);
} else {
// create the new spell
spell = await Item.create(body.data, {
temporary: false,
displaySheet: true,
});
return spell;
};


const getCompendium = async () => {
const compendiumName = await game.settings.get("vtta-dndbeyond", "entity-spell-compendium");
if (compendiumName && compendiumName !== "") {
const compendium = await game.packs.find((pack) => pack.collection === compendiumName);
if (compendium) {
return compendium;
}
}
return undefined;
};

// decide wether to save it into the compendium
if (
game.settings.get("vtta-dndbeyond", "entity-import-policy") !== SAVE_NONE
) {
// update existing (1) or overwrite (0)
let compendiumName = game.settings.get(
"vtta-dndbeyond",
"entity-spell-compendium"
);
if (compendiumName && compendiumName !== "") {
let compendium = game.packs.find(
(pack) => pack.collection === compendiumName
);
if (compendium) {
let index = await compendium.getIndex();
let entity = index.find(
(entity) =>
entity.name.toLowerCase() === body.data.name.toLowerCase()
);
if (entity) {
if (SAVE_ALL) {
const id = spell.data._id;
spell.data._id = entity._id;
await compendium.updateEntity(spell.data);
spell.data._id = id;
}
} else {
await compendium.createEntity(spell.data);
}
} else {
reject("Error opening compendium, check your settings");
const addSpellToCompendium = async (spell, name) => {
// decide wether to save it into the compendium
if (game.settings.get("vtta-dndbeyond", "entity-import-policy") !== SAVE_NONE) {
// update existing (1) or overwrite (0)
const compendium = await getCompendium();
if (compendium) {
let index = await compendium.getIndex();
let entity = index.find((entity) => entity.name.toLowerCase() === name.toLowerCase());
if (entity) {
if (SAVE_ALL) {
const compendiumSpell = JSON.parse(JSON.stringify(spell));
compendiumSpell.data._id = entity._id;
await compendium.updateEntity(compendiumSpell.data);
}
} else {
await compendium.createEntity(spell.data);
}
} else {
console.error("Error opening compendium, check your settings");
}
if (cleanupAfterImport) {
await spell.delete();
}
resolve(spell.data);
}
};

const cleanUp = async (spell) => {
// cleaning up after imports
const cleanupAfterImport =
game.settings.get("vtta-dndbeyond", "entity-cleanup-policy") === CLEAN_ALL ||
game.settings.get("vtta-dndbeyond", "entity-cleanup-policy") === CLEAN_SPELLS;
if (cleanupAfterImport) {
await spell.delete();
}
};

const parseSpell = async (body) => {
let spell = await createSpell(body.data);
await addSpellToCompendium(spell, body.data.name);
await cleanUp(spell);
return spell;
};

let addSpell = (body) => {
return new Promise((resolve, reject) => {
parseSpell(body)
.then((spell) => {
resolve(spell.data);
})
.catch((error) => {
console.error(`error parsing spell: ${error}`);
reject(error);
});
});
};

Expand Down

0 comments on commit 1ad59f2

Please sign in to comment.