Skip to content

Commit

Permalink
closes #968
Browse files Browse the repository at this point in the history
  • Loading branch information
Muttley committed Dec 4, 2024
1 parent 2682cb2 commit 094326e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 38 deletions.
7 changes: 7 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v3.2.2

## Chores
* [#968] Update Shadowdarklings import process to support Wands and Scrolls

---

# v3.2.1

## Bugs
Expand Down
87 changes: 71 additions & 16 deletions system/src/apps/ShadowdarklingImporterSD.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ export default class ShadowdarklingImporterSD extends FormApplication {
html.on("paste", ".SDImporterJson", this._onPaste.bind(this));
}

/** @override */
async getData(options) {
const data = {
actor: this.importedActor,
errors: this.errors,
gear: this.gear,
};

return data;
}

/** @inheritdoc */
_onSubmit(event) {
event.preventDefault();
Expand All @@ -56,17 +67,6 @@ export default class ShadowdarklingImporterSD extends FormApplication {
// required method
}

/** @override */
async getData(options) {
const data = {
actor: this.importedActor,
errors: this.errors,
gear: this.gear,
};

return data;
}

/**
* Handles pasting of json data
*/
Expand All @@ -88,6 +88,22 @@ export default class ShadowdarklingImporterSD extends FormApplication {
}
}

async _createSpellScroll(item) {
const spell = await this._getSpellFromItem(item);

if (spell) {
return await shadowdark.utils.createItemFromSpell("Scroll", spell);
}
}

async _createSpellWand(item) {
const spell = await this._getSpellFromItem(item);

if (spell) {
return await shadowdark.utils.createItemFromSpell("Wand", spell);
}
}

/**
* finds matching items by category
*/
Expand Down Expand Up @@ -132,7 +148,7 @@ export default class ShadowdarklingImporterSD extends FormApplication {
c.name.toLowerCase() === spell.sourceName.toLowerCase()
);

const itemIndex = pack.index.find( s => (
const itemIndex = pack.index.find(s => (
(s.name.toLowerCase() === spell.bonusName.toLowerCase())
&& (s.type === "Spell")
&& (s.system.class.includes(classObj?.uuid))
Expand Down Expand Up @@ -224,6 +240,32 @@ export default class ShadowdarklingImporterSD extends FormApplication {
}
}

async _getSpellFromItem(item) {
const [spellName, spellClass] = this._getSpellNameAndClassFromItem(item);

if (!spellName || !spellClass) return;

const classUuid = (await shadowdark.compendiums.classes()).find(
i => i.name === spellClass
).uuid;

const spell = (await shadowdark.compendiums.spells()).find(spell => {
return spellName.slugify() === spell.name.slugify()
&& spell.system.class.includes(classUuid);
});

return spell;
}

_getSpellNameAndClassFromItem(item) {
const {spellName, spellClass} =
/^(?<spellName>[\w\s]+)\s+\(Tier\s+\d+,\s+(?<spellClass>\w+)\)/
.exec(item.spellDesc ?? "")
?.groups ?? [undefined, undefined];

return [spellName, spellClass];
}

/**
* Parses JSON data from shadowdarklings and tries to create an Player actor from it.
* @param {JSON} json - JSON Data from the Shadowdarklings.net site
Expand Down Expand Up @@ -400,10 +442,23 @@ export default class ShadowdarklingImporterSD extends FormApplication {

// magic items (not implemented)
for (const item of json.magicItems) {
this.errors.push({
type: "Magic Item",
name: item.name,
});
let newItem = undefined;
if (item.magicItemType === "magicSpellScroll") {
newItem = await this._createSpellScroll(item);
}
else if (item.magicItemType === "magicSpellWand") {
newItem = await this._createSpellWand(item);
}

if (newItem) {
this.gear.push(newItem);
}
else {
this.errors.push({
type: "Magic Item",
name: item.name,
});
}
}

// Load Bonuses / talents & Spells
Expand Down
23 changes: 1 addition & 22 deletions system/src/sheets/PlayerSheetSD.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,7 @@ export default class PlayerSheetSD extends ActorSheetSD {
}

async _createItemFromSpell(spell, type) {
const name = (type !== "Spell")
? game.i18n.format(
`SHADOWDARK.item.name_from_spell.${type}`,
{spellName: spell.name}
)
: spell.name;

const itemData = {
type,
name,
system: spell.system,
};

if (type === "Spell") {
itemData.img = spell.img;
}
else {
delete itemData.system.lost;
itemData.system.magicItem = true;
itemData.system.spellImg = spell.img;
itemData.system.spellName = spell.name;
}
const itemData = await shadowdark.utils.createItemFromSpell(type, spell);

super._onDropItemCreate(itemData);
}
Expand Down
27 changes: 27 additions & 0 deletions system/src/utils/UtilitySD.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ export default class UtilitySD {
}


static async createItemFromSpell(type, spell) {
const name = (type !== "Spell")
? game.i18n.format(
`SHADOWDARK.item.name_from_spell.${type}`,
{ spellName: spell.name }
)
: spell.name;

const itemData = {
type,
name,
system: spell.system,
};

if (type === "Spell") {
itemData.img = spell.img;
}
else {
delete itemData.system.lost;
itemData.system.magicItem = true;
itemData.system.spellImg = spell.img;
itemData.system.spellName = spell.name;
}
return itemData;
}


static diceSound() {
const sounds = [CONFIG.sounds.dice];
const src = sounds[0];
Expand Down

0 comments on commit 094326e

Please sign in to comment.