/* -------------------------------------------- */ import { Imperium5Utility } from "./imperium5-utility.js"; import { Imperium5RollDialog } from "./imperium5-roll-dialog.js"; /* -------------------------------------------- */ /** * Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system. * @extends {Actor} */ export class Imperium5Actor extends Actor { /* -------------------------------------------- */ /** * Override the create() function to provide additional SoS functionality. * * This overrided create() function adds initial items * Namely: Basic skills, money, * * @param {Object} data Barebones actor data which this function adds onto. * @param {Object} options (Unused) Additional options which customize the creation workflow. * */ static async create(data, options) { // Case of compendium global import if (data instanceof Array) { return super.create(data, options); } // If the created actor has items (only applicable to duplicated actors) bypass the new actor creation logic if (data.items) { let actor = super.create(data, options); return actor; } if (data.type == 'character') { } return super.create(data, options); } /* -------------------------------------------- */ prepareBaseData() { } /* -------------------------------------------- */ async prepareData() { super.prepareData(); } /* -------------------------------------------- */ prepareDerivedData() { if (this.type == 'character') { //this.data.data.encCapacity = this.getEncumbranceCapacity() //this.buildContainerTree() } console.log("Acteur : ", this) super.prepareDerivedData(); } /* -------------------------------------------- */ _preUpdate(changed, options, user) { super._preUpdate(changed, options, user); } /* -------------------------------------------- */ getArchetype() { let item = duplicate( this.data.items.find( it => it.type == "archetype") || []) return item } getSpecialites() { let item = duplicate(this.data.items.filter( it => it.type == "specialite") || [] ) return item } getFamiliarites() { let item = duplicate(this.data.items.filter( it => it.type == "familiarite") || [] ) return item } /* -------------------------------------------- */ getItemById(id) { let item = this.data.items.find(item => item.id == id) if (item) { item = duplicate(item) } return item; } /* -------------------------------------------- */ compareName(a, b) { if (a.name < b.name) { return -1; } if (a.name > b.name) { return 1; } return 0; } /* -------------------------------------------- */ getActiveEffects(matching = it => true) { let array = Array.from(this.getEmbeddedCollection("ActiveEffect").values()); return Array.from(this.getEmbeddedCollection("ActiveEffect").values()).filter(it => matching(it)); } /* -------------------------------------------- */ getEffectByLabel(label) { return this.getActiveEffects().find(it => it.data.label == label); } /* -------------------------------------------- */ getEffectById(id) { return this.getActiveEffects().find(it => it.id == id); } /* -------------------------------------------- */ getInitiativeScore(combatId, combatantId) { if (this.type == 'character') { this.rollMR(true, combatId, combatantId) } console.log("Init required !!!!") return -1; } /* -------------------------------------------- */ getSubActors() { let subActors = []; for (let id of this.data.data.subactors) { subActors.push(duplicate(game.actors.get(id))) } return subActors; } /* -------------------------------------------- */ async addSubActor(subActorId) { let subActors = duplicate(this.data.data.subactors); subActors.push(subActorId); await this.update({ 'data.subactors': subActors }); } /* -------------------------------------------- */ async delSubActor(subActorId) { let newArray = []; for (let id of this.data.data.subactors) { if (id != subActorId) { newArray.push(id); } } await this.update({ 'data.subactors': newArray }); } /* -------------------------------------------- */ syncRoll(rollData) { let linkedRollId = Imperium5Utility.getDefenseState(this.id); if (linkedRollId) { rollData.linkedRollId = linkedRollId; } this.lastRollId = rollData.rollId; Imperium5Utility.saveRollData(rollData); } /* -------------------------------------------- */ async deleteAllItemsByType(itemType) { let items = this.data.items.filter(item => item.type == itemType); await this.deleteEmbeddedDocuments('Item', items); } /* -------------------------------------------- */ async addItemWithoutDuplicate(newItem) { let item = this.data.items.find(item => item.type == newItem.type && item.name.toLowerCase() == newItem.name.toLowerCase()) if (!item) { await this.createEmbeddedDocuments('Item', [newItem]); } } /* -------------------------------------------- */ async incDecQuantity(objetId, incDec = 0) { let objetQ = this.data.items.get(objetId) if (objetQ) { let newQ = objetQ.data.data.quantity + incDec if (newQ >= 0) { const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'data.quantity': newQ }]) // pdates one EmbeddedEntity } } } /* -------------------------------------------- */ /* ROLL SECTION /* -------------------------------------------- */ /* -------------------------------------------- */ addEffects(rollData) { let effects = this.data.items.filter(item => item.type == 'effect') for (let effect of effects) { effect = duplicate(effect) if (!effect.data.hindrance && (effect.data.stataffected != "notapplicable" || effect.data.specaffected.length > 0) && effect.data.stataffected != "special") { if (effect.data.effectstatlevel) { effect.data.effectlevel = this.data.data.statistics[effect.data.effectstat].value } rollData.effectsList.push({ label: effect.name, type: "effect", applied: false, effect: effect, value: effect.data.effectlevel }) } } } /* -------------------------------------------- */ getCommonRollData(statKey = undefined, useShield = false) { let rollData = Imperium5Utility.getBasicRollData() rollData.alias = this.name rollData.actorImg = this.img rollData.actorId = this.id rollData.img = this.img return rollData } /* -------------------------------------------- */ async startRoll(rollData) { this.syncRoll(rollData) let rollDialog = await Imperium5RollDialog.create(this, rollData) console.log(rollDialog) rollDialog.render(true) } }