From 5e44a97686122605a60de6a6e3d0f1aacc9311ad Mon Sep 17 00:00:00 2001 From: sladecraven Date: Sun, 13 Feb 2022 21:58:19 +0100 Subject: [PATCH] Manage stuff --- modules/pegasus-actor-sheet.js | 14 ++++- modules/pegasus-actor.js | 90 +++++++++++++++++++++++---------- system.json | 4 +- template.json | 7 ++- templates/actor-sheet.html | 43 +++++++++++++--- templates/item-perk-sheet.html | 2 +- templates/item-power-sheet.html | 2 +- 7 files changed, 119 insertions(+), 43 deletions(-) diff --git a/modules/pegasus-actor-sheet.js b/modules/pegasus-actor-sheet.js index e83ef5e..27ba189 100644 --- a/modules/pegasus-actor-sheet.js +++ b/modules/pegasus-actor-sheet.js @@ -121,12 +121,24 @@ export class PegasusActorSheet extends ActorSheet { let itemId = li.data("item-id"); this.actor.perkEffectUsed( itemId) }); + html.find('.perk-status').change(ev => { const li = $(ev.currentTarget).parents(".item"); let itemId = li.data("item-id"); this.actor.updatePerkStatus( itemId, ev.currentTarget.value) }); - + html.find('.power-cost-spent').change(ev => { + const li = $(ev.currentTarget).parents(".item"); + let itemId = li.data("item-id"); + this.actor.updatePowerSpentCost( itemId, ev.currentTarget.value) + }); + html.find('.perk-used').change(ev => { + const li = $(ev.currentTarget).parents(".item") + let itemId = li.data("item-id") + let index = Number($(ev.currentTarget).data("use-index") ) + this.actor.updatePerkUsed( itemId, index, ev.currentTarget.checked ) + }); + html.find('.subactor-edit').click(ev => { const li = $(ev.currentTarget).parents(".item"); let actorId = li.data("actor-id"); diff --git a/modules/pegasus-actor.js b/modules/pegasus-actor.js index 7bb36de..cf6b2ef 100644 --- a/modules/pegasus-actor.js +++ b/modules/pegasus-actor.js @@ -181,27 +181,26 @@ export class PegasusActor extends Actor { /* -------------------------------------------- */ async manageWorstFear(flag) { if (flag) { - let effect = await PegasusUtility.getEffectFromCompendium( "Worst Fear" ) - console.log("got effect", effect) + let effect = await PegasusUtility.getEffectFromCompendium("Worst Fear") effect.data.worstfear = true - this.createEmbeddedDocuments( 'Item', [effect]) + this.createEmbeddedDocuments('Item', [effect]) } else { - let effect = this.data.items.find( item => item.type == "effect" && item.data.data.worstfear ) + let effect = this.data.items.find(item => item.type == "effect" && item.data.data.worstfear) if (effect) { - this.deleteEmbeddedDocuments( 'Item', [ effect.id ]) + this.deleteEmbeddedDocuments('Item', [effect.id]) } } } /* -------------------------------------------- */ async manageDesires(flag) { if (flag) { - let effect = await PegasusUtility.getEffectFromCompendium( "Desires" ) + let effect = await PegasusUtility.getEffectFromCompendium("Desires") effect.data.desires = true - this.createEmbeddedDocuments( 'Item', [effect]) + this.createEmbeddedDocuments('Item', [effect]) } else { - let effect = this.data.items.find( item => item.type == "effect" && item.data.data.desires ) + let effect = this.data.items.find(item => item.type == "effect" && item.data.data.desires) if (effect) { - this.deleteEmbeddedDocuments( 'Item', [ effect.id ]) + this.deleteEmbeddedDocuments('Item', [effect.id]) } } } @@ -230,13 +229,14 @@ export class PegasusActor extends Actor { if (item && item.data.data) { let nrg = duplicate(this.data.data.nrg) - if ( !item.data.data.activated) { // Current value - - if ( item.data.data.cost > nrg.value) { - return ui.notifications.warn("Not enough NRG to activate the Power " + item.name ) + if (!item.data.data.activated) { // Current value + + if (item.data.data.costspent > nrg.value || item.data.data.costspent > nrg.max) { + return ui.notifications.warn("Not enough NRG to activate the Power " + item.name) } - nrg.activated += item.data.data.cost - nrg.value -= item.data.data.cost + nrg.activated += item.data.data.costspent + nrg.value -= item.data.data.costspent + nrg.max -= item.data.data.costspent this.update({ 'data.nrg': nrg }) let effects = [] @@ -248,19 +248,20 @@ export class PegasusActor extends Actor { await this.createEmbeddedDocuments('Item', effects) } } else { - nrg.activated -= item.data.data.cost + nrg.activated -= item.data.data.costspent + nrg.max += item.data.data.costspent this.update({ 'data.nrg': nrg }) let toRem = [] - for( let item of this.data.items) { + for (let item of this.data.items) { if (item.type == 'effect' && item.data.data.powerId == itemId) { - toRem.push( item.id) + toRem.push(item.id) } } if (toRem.length) { await this.deleteEmbeddedDocuments('Item', toRem) } - } + } let update = { _id: item.id, "data.activated": !item.data.data.activated } await this.updateEmbeddedDocuments('Item', [update]) // Updates one EmbeddedEntity } @@ -468,6 +469,47 @@ export class PegasusActor extends Actor { } } + /* -------------------------------------------- */ + async cleanPerkEffects(itemId) { + let effects = [] + for (let item of this.data.items) { + if (item.type == "effect" && item.data.data.perkId == itemId) { + effects.push(item.id) + } + } + if (effects.length) { + await this.deleteEmbeddedDocuments('Item', effects) + } + } + + /* -------------------------------------------- */ + async updatePerkUsed(itemId, index, checked) { + let item = this.items.get(itemId) + if (item && index) { + let key = "data.used" + index + await this.updateEmbeddedDocuments('Item', [{ _id: itemId, [`${key}`]: checked }] ) + item = this.items.get(itemId) // Refresh + if (item.data.data.nbuse == "next1action" && item.data.data.used1) { + this.cleanPerkEffects(itemId) + } + if (item.data.data.nbuse == "next2action" && item.data.data.used1 && item.data.data.used2) { + this.cleanPerkEffects(itemId) + } + if (item.data.data.nbuse == "next3action" && item.data.data.used1 && item.data.data.used2 && item.data.data.used3) { + this.cleanPerkEffects(itemId) + } + } + } + + /* -------------------------------------------- */ + async updatePowerSpentCost(itemId, value) { + let item = this.items.get(itemId) + if (item && value) { + value = Number(value) || 0 + await this.updateEmbeddedDocuments('Item', [{ _id: itemId, 'data.costspent': value }]) + } + } + /* -------------------------------------------- */ async updatePerkStatus(itemId, status) { let item = this.items.get(itemId) @@ -477,15 +519,7 @@ export class PegasusActor extends Actor { let updateOK = true if (status == "ready") { - let effects = [] - for (let item of this.data.items) { - if (item.type == "effect" && item.data.data.perkId == itemId) { - effects.push(item.id) - } - } - if (effects.length) { - await this.deleteEmbeddedDocuments('Item', effects) - } + this.cleanPerkEffects(itemId) if (item.data.data.features.nrgcost.flag) { let nrg = duplicate(this.data.data.nrg) nrg.activated -= item.data.data.features.nrgcost.value diff --git a/system.json b/system.json index a147bfe..fa10e4b 100644 --- a/system.json +++ b/system.json @@ -180,9 +180,9 @@ "styles": [ "styles/simple.css" ], - "templateVersion": 73, + "templateVersion": 75, "title": "Pegasus RPG", "url": "https://www.uberwald.me/data/files/fvtt-pegasus-rpg", - "version": "0.4.8", + "version": "0.4.10", "background" : "./images/ui/pegasus_welcome_page.webp" } diff --git a/template.json b/template.json index f1ff90b..8383695 100644 --- a/template.json +++ b/template.json @@ -302,13 +302,16 @@ } }, "status": "", - "nbuse": "" + "nbuse": "", + "used1":false, + "used2":false, + "used3":false }, "power": { "rollneeded": false, "statistic": "", "cost": 0, - "costtype": "", + "costspent": 0, "range": "", "action": "", "type": "", diff --git a/templates/actor-sheet.html b/templates/actor-sheet.html index 7624112..1d7844a 100644 --- a/templates/actor-sheet.html +++ b/templates/actor-sheet.html @@ -83,7 +83,8 @@