From 043d7442e9a408dcb838ed70d558e34720a473e5 Mon Sep 17 00:00:00 2001 From: Vlyan Date: Fri, 2 Jul 2021 09:56:16 +0200 Subject: [PATCH] Factoring --- system/scripts/actors/base-sheet.js | 26 +++- system/scripts/actors/character-sheet.js | 148 ++++++++--------------- system/scripts/helpers.js | 70 ++++++----- 3 files changed, 115 insertions(+), 129 deletions(-) diff --git a/system/scripts/actors/base-sheet.js b/system/scripts/actors/base-sheet.js index cd40e32..54a3d69 100644 --- a/system/scripts/actors/base-sheet.js +++ b/system/scripts/actors/base-sheet.js @@ -171,7 +171,28 @@ export class BaseSheetL5r5e extends ActorSheet { // Check item type and subtype const item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event); - if (!item || item.documentName !== "Item" || item.data.type === "property") { + if (!item || !["Item", "JournalEntry"].includes(item.documentName) || item.data.type === "property") { + return; + } + + // Specific curriculum journal drop + if (item.documentName === "JournalEntry") { + // npc does not have this + if (!this.actor.data.data.identity?.school_curriculum_journal) { + return; + } + this.actor.data.data.identity.school_curriculum_journal = { + id: item.data._id, + name: item.data.name, + pack: item.pack || null, + }; + await this.actor.update({ + data: { + identity: { + school_curriculum_journal: this.actor.data.data.identity.school_curriculum_journal, + }, + }, + }); return; } @@ -513,6 +534,9 @@ export class BaseSheetL5r5e extends ActorSheet { * @private */ _switchSubItemCurriculum(event) { + event.preventDefault(); + event.stopPropagation(); + const itemId = $(event.currentTarget).data("item-id"); const item = this.actor.items.get(itemId); if (item.type !== "item") { diff --git a/system/scripts/actors/character-sheet.js b/system/scripts/actors/character-sheet.js index 4cd2de9..b2ac79c 100644 --- a/system/scripts/actors/character-sheet.js +++ b/system/scripts/actors/character-sheet.js @@ -40,8 +40,8 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e { /** * Commons datas */ - getData() { - const sheetData = super.getData(); + getData(options = {}) { + const sheetData = super.getData(options); // Min rank = 1 this.actor.data.data.identity.school_rank = Math.max(1, this.actor.data.data.identity.school_rank); @@ -75,25 +75,12 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e { return; } - // *** Items : curriculum management *** - html.find(`.item-curriculum`).on("click", (event) => { - event.preventDefault(); - event.stopPropagation(); - this._switchSubItemCurriculum(event); - }); - html.find(`button[name=validate-curriculum]`).on("click", (event) => { - event.preventDefault(); - event.stopPropagation(); - this.actor.data.data.identity.school_rank = this.actor.data.data.identity.school_rank + 1; - this.actor.update({ - data: { - identity: { - school_rank: this.actor.data.data.identity.school_rank, - }, - }, - }); - this.render(false); - }); + // Open linked school curriculum journal + html.find(".school-journal-link").on("click", this._openLinkedJournal.bind(this)); + + // Curriculum management + html.find(".item-curriculum").on("click", this._switchSubItemCurriculum.bind(this)); + html.find("button[name=validate-curriculum]").on("click", this._actorAddOneToRank.bind(this)); // Money +/- html.find(".money-control").on("click", this._modifyMoney.bind(this)); @@ -103,81 +90,6 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e { this._tabs .find((e) => e._navSelector === ".advancements-tabs") .activate("advancement_rank_" + (this.actor.data.data.identity.school_rank || 0)); - - // Open linked school curriculum journal - html.find(`.school-journal-link`).on("click", async (event) => { - event.preventDefault(); - event.stopPropagation(); - - const actorJournal = this.actor.data.data.identity.school_curriculum_journal; - const journal = await this._getJournal(actorJournal.id, actorJournal.pack); - if (journal) { - journal.sheet.render(true); - } - }); - } - - /** - * Handle dropped data on the Actor sheet - * @param {DragEvent} event - */ - async _onDrop(event) { - // *** Everything below here is only needed if the sheet is editable *** - if (!this.options.editable) { - return; - } - - // Curriculum Journal - const json = event.dataTransfer.getData("text/plain"); - if (!json) { - return; - } - - const data = JSON.parse(json); - if (data.type !== "JournalEntry") { - return super._onDrop(event); - } - - const journal = await this._getJournal(data.id, data.pack); - if (!journal) { - return; - } - - this.actor.data.data.identity.school_curriculum_journal = { - id: journal.data._id, - name: journal.data.name, - pack: data.pack || null, - }; - - await this.actor.update({ - data: { - identity: { - school_curriculum_journal: this.actor.data.data.identity.school_curriculum_journal, - }, - }, - }); - } - - /** - * Return the Journal in Pack or in World - * @param id - * @param pack - * @return {Promise} - * @private - */ - async _getJournal(id, pack = null) { - let journal; - if (pack) { - // Compendium - journal = await game.packs.get(pack)?.getDocument(id); - } else { - // World - journal = game.journal.get(id); - } - if (!journal || !(journal instanceof game.l5r5e.JournalL5r5e) || !journal.data?._id) { - return; - } - return journal; } /** @@ -319,4 +231,48 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e { }); this.render(false); } + + /** + * Add +1 to actor school rank + * @param {Event} event + * @private + */ + async _actorAddOneToRank(event) { + event.preventDefault(); + event.stopPropagation(); + + this.actor.data.data.identity.school_rank = this.actor.data.data.identity.school_rank + 1; + await this.actor.update({ + data: { + identity: { + school_rank: this.actor.data.data.identity.school_rank, + }, + }, + }); + this.render(false); + } + + /** + * Open the linked school curriculum journal + * @param {Event} event + * @private + */ + async _openLinkedJournal(event) { + event.preventDefault(); + event.stopPropagation(); + + const actorJournal = this.actor.data.data.identity.school_curriculum_journal; + if (!actorJournal.id) { + return; + } + + const journal = await game.l5r5e.HelpersL5r5e.getObjectGameOrPack({ + id: actorJournal.id, + pack: actorJournal.pack, + type: "JournalEntry", + }); + if (journal) { + journal.sheet.render(true); + } + } } diff --git a/system/scripts/helpers.js b/system/scripts/helpers.js index 9d204a0..9aad911 100644 --- a/system/scripts/helpers.js +++ b/system/scripts/helpers.js @@ -1,5 +1,3 @@ -import { ItemL5r5e } from "./item.js"; - /** * Extends the actor to process special things from L5R. */ @@ -94,7 +92,7 @@ export class HelpersL5r5e { try { // Direct Object if (data?._id) { - document = HelpersL5r5e.createItemFromCompendium(data); + document = HelpersL5r5e.createDocumentFromCompendium({ type, data }); } else if (!id || !type) { return null; } @@ -109,7 +107,7 @@ export class HelpersL5r5e { if (pack) { const data = await game.packs.get(pack).getDocument(id); if (data) { - document = HelpersL5r5e.createItemFromCompendium(data); + document = HelpersL5r5e.createDocumentFromCompendium({ type, data }); } } } @@ -130,7 +128,7 @@ export class HelpersL5r5e { const data = await comp.getDocument(id); if (data) { - document = HelpersL5r5e.createItemFromCompendium(data); + document = HelpersL5r5e.createDocumentFromCompendium({ type, data }); } } } @@ -153,35 +151,35 @@ export class HelpersL5r5e { /** * Make a temporary item for compendium drag n drop - * @param {ItemL5r5e|any[]} data + * @param {string} type + * @param {ItemL5r5e|JournalL5r5e|any[]} data * @return {ItemL5r5e} */ - static createItemFromCompendium(data) { - if ( - ![ - "item", - "armor", - "weapon", - "technique", - "property", - "peculiarity", - "advancement", - "title", - "bond", - "signature_scroll", - "item_pattern", - ].includes(data.type) - ) { - return data; - } + static createDocumentFromCompendium({ type, data }) { + let document = null; + + switch (type) { + case "Item": + if (data instanceof game.l5r5e.ItemL5r5e) { + document = data; + } else { + document = new game.l5r5e.ItemL5r5e(data); + } + break; + + case "JournalEntry": + if (data instanceof game.l5r5e.JournalL5r5e) { + document = data; + } else { + document = new game.l5r5e.JournalL5r5e(data); + } + break; + + default: + console.log(`L5R5E | createObjectFromCompendium - Unmanaged type ${type}`); + break; + } // swi - let document; - if (data instanceof ItemL5r5e) { - document = data; - } else { - // Quick object - document = new ItemL5r5e(data); - } return document; } @@ -191,7 +189,7 @@ export class HelpersL5r5e { * @return {Promise} */ static async refreshItemProperties(document) { - if (document.data.data.properties && typeof Babele !== "undefined") { + if (document.data?.data?.properties && typeof Babele !== "undefined") { document.data.data.properties = await Promise.all( document.data.data.properties.map(async (property) => { const gameProp = await HelpersL5r5e.getObjectGameOrPack({ id: property.id, type: "Item" }); @@ -243,6 +241,8 @@ export class HelpersL5r5e { */ static getPackNameForCoreItem(documentId) { const core = new Map(); + + // Items core.set("Pro", "l5r5e.core-properties"); core.set("Kat", "l5r5e.core-techniques-kata"); core.set("Kih", "l5r5e.core-techniques-kiho"); @@ -265,6 +265,12 @@ export class HelpersL5r5e { core.set("Pas", "l5r5e.core-peculiarities-passions"); core.set("Adv", "l5r5e.core-peculiarities-adversities"); core.set("Anx", "l5r5e.core-peculiarities-anxieties"); + + // Journal + core.set("Csc", "l5r5e.core-journal-school-curriculum"); + core.set("Con", "l5r5e.core-journal-conditions"); + core.set("Ter", "l5r5e.core-journal-terrain-qualities"); + return core.get(documentId.replace(/L5RCore(\w{3})\d+/gi, "$1")); }