/* -------------------------------------------- */ import { PegasusCombat } from "./pegasus-combat.js"; import { PegasusCommands } from "./pegasus-commands.js"; import { PegasusActorCreate } from "./pegasus-create-char.js"; /* -------------------------------------------- */ const __level2Dice = ["d0", "d4", "d6", "d8", "d10", "d12"]; const __name2DiceValue = { "0": 0, "d0": 0, "d4": 4, "d6": 6, "d8": 8, "d10": 10, "d12": 12 } /* -------------------------------------------- */ export class PegasusUtility { /* -------------------------------------------- */ static async init() { Hooks.on('renderChatLog', (log, html, data) => PegasusUtility.chatListeners(html)); Hooks.on("getCombatTrackerEntryContext", (html, options) => { PegasusUtility.pushInitiativeOptions(html, options); }); Hooks.on("dropCanvasData", (canvas, data) => { PegasusUtility.dropItemOnToken(canvas, data) }); this.rollDataStore = {} this.defenderStore = {} this.diceList = []; this.diceFoundryList = []; this.optionsDiceList = ""; this.buildDiceLists(); PegasusCommands.init(); Handlebars.registerHelper('count', function (list) { return list.length; }); Handlebars.registerHelper('includes', function (array, val) { return array.includes(val); }); Handlebars.registerHelper('upper', function (text) { return text.toUpperCase(); }); Handlebars.registerHelper('upperFirst', function (text) { if (typeof text !== 'string') return text return text.charAt(0).toUpperCase() + text.slice(1) }); Handlebars.registerHelper('notEmpty', function (list) { return list.length > 0; }); } /* -------------------------------------------- */ static pushInitiativeOptions(html, options) { console.log('Option pushed....') options.push({ name: "Apply -10", condition: true, icon: '', callback: target => { PegasusCombat.decInitBy10(target.data('combatant-id'), -10); } }) } /* -------------------------------------------- */ static getSpecs() { return this.specs; } /* -------------------------------------------- */ static async ready() { const specs = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.specialisations"); this.specs = specs.map(i => i.toObject()); } /* -------------------------------------------- */ static async dropItemOnToken(canvas, data) { if (data.type != "Item") { return } const position = canvas.grid.getTopLeft(data.x, data.y) let x = position[0] let y = position[1] const tokensList = [...canvas.tokens.placeables] for (let token of tokensList) { if (x >= token.x && x <= (token.x + token.width) && y >= token.y && y <= (token.y + token.height)) { let item = await this.searchItem(data) token.actor.preprocessItem("none", item, false) console.log("Dropped !!!", item, token) return } } } /* -------------------------------------------- */ static async loadCompendiumData(compendium) { const pack = game.packs.get(compendium); return await pack?.getDocuments() ?? []; } /* -------------------------------------------- */ static async loadCompendium(compendium, filter = item => true) { let compendiumData = await PegasusUtility.loadCompendiumData(compendium); return compendiumData.filter(filter); } /* -------------------------------------------- */ static buildDiceLists() { let maxLevel = game.settings.get("fvtt-pegasus-rpg", "dice-max-level"); let diceList = ["0"]; let diceValues = [0]; let diceFoundryList = ["d0"]; let diceLevel = 1; let concat = ""; let concatFoundry = ""; let optionsDiceList = ''; let optionsLevel = ''; for (let i = 1; i <= maxLevel; i++) { let currentDices = concat + __level2Dice[diceLevel]; diceList.push(currentDices); diceFoundryList.push(concatFoundry + __level2Dice[diceLevel] + "x"); if (__level2Dice[diceLevel] == "d12") { concat = concat + "d12 "; concatFoundry = concatFoundry + "d12x, "; diceLevel = 1; } else { diceLevel++; } optionsDiceList += ``; optionsLevel += ``; } this.diceList = diceList; this.diceFoundryList = diceFoundryList; this.optionsDiceList = optionsDiceList; this.optionsLevel = optionsLevel; this.optionsStatusList = ''; } /* -------------------------------------------- */ static getOptionsStatusList() { return this.optionsStatusList; } /* -------------------------------------------- */ static getOptionsDiceList() { return this.optionsDiceList; } /* -------------------------------------------- */ static getOptionsLevel() { return this.optionsLevel; } /* -------------------------------------------- */ static computeAttackDefense(defenseRollId) { let defenseRollData = this.getRollData(defenseRollId); let attackRollData = this.getRollData(defenseRollData.linkedRollId); let defender = game.actors.get(defenseRollData.actorId); defender.processDefenseResult(defenseRollData, attackRollData); } /* -------------------------------------------- */ static applyDamage(defenseRollId) { let defenseRollData = this.getRollData(defenseRollId); let defender = game.actors.get(defenseRollData.actorId); defender.applyDamageLoss(defenseRollData.finalDamage); } /* -------------------------------------------- */ static applyNoDefense(actorId, attackRollId) { let attackRollData = this.getRollData(attackRollId); let defender = game.actors.get(actorId); defender.processNoDefense(attackRollData); } /* -------------------------------------------- */ static async chatListeners(html) { html.on("click", '.chat-create-actor', event => { game.system.pegasus.creator.processChatEvent(event); }); html.on("click", '.view-item-from-chat', event => { game.system.pegasus.creator.openItemView(event) }); } /* -------------------------------------------- */ static async preloadHandlebarsTemplates() { const templatePaths = [ 'systems/fvtt-pegasus-rpg/templates/editor-notes-gm.html', 'systems/fvtt-pegasus-rpg/templates/partial-roll-select-effects.html', 'systems/fvtt-pegasus-rpg/templates/partial-options-statistics.html', 'systems/fvtt-pegasus-rpg/templates/partial-options-level.html', 'systems/fvtt-pegasus-rpg/templates/partial-options-range.html', 'systems/fvtt-pegasus-rpg/templates/partial-options-equipment-types.html', 'systems/fvtt-pegasus-rpg/templates/partial-equipment-effects.html' ] return loadTemplates(templatePaths); } /* -------------------------------------------- */ static async getEffectFromCompendium(effectName) { effectName = effectName.toLowerCase() let effect = game.items.find(item => item.data.type == 'effect' && item.name.toLowerCase() == effectName) if (!effect) { let effects = await this.loadCompendium('fvtt-pegasus.effect', item => item.name.toLowerCase() == effectName) let objs = effects.map(i => i.toObject()) effect = objs[0] } else { effect = duplicate(effect); } console.log("Effect", effect) return effect } /* -------------------------------------------- */ static removeChatMessageId(messageId) { if (messageId) { game.messages.get(messageId)?.delete(); } } static findChatMessageId(current) { return PegasusUtility.getChatMessageId(PegasusUtility.findChatMessage(current)); } static getChatMessageId(node) { return node?.attributes.getNamedItem('data-message-id')?.value; } static findChatMessage(current) { return PegasusUtility.findNodeMatching(current, it => it.classList.contains('chat-message') && it.attributes.getNamedItem('data-message-id')); } static findNodeMatching(current, predicate) { if (current) { if (predicate(current)) { return current; } return PegasusUtility.findNodeMatching(current.parentElement, predicate); } return undefined; } /* -------------------------------------------- */ static templateData(it) { return PegasusUtility.data(it)?.data ?? {} } /* -------------------------------------------- */ static data(it) { if (it instanceof Actor || it instanceof Item || it instanceof Combatant) { return it.data; } return it; } /* -------------------------------------------- */ static getDiceValue(level = 0) { let diceString = this.diceList[level] let diceTab = diceString.split(" ") let diceValue = 0 for (let dice of diceTab) { diceValue += __name2DiceValue[dice] } return diceValue } /* -------------------------------------------- */ static getDiceFromLevel(level = 0) { level = Number(level) return this.diceList[level]; } /* -------------------------------------------- */ static getFoundryDiceFromLevel(level = 0) { level = Number(level) //console.log(this.diceFoundryList); return this.diceFoundryList[level]; } /* -------------------------------------------- */ static createDirectOptionList(min, max) { let options = {}; for (let i = min; i <= max; i++) { options[`${i}`] = `${i}`; } return options; } /* -------------------------------------------- */ static buildListOptions(min, max) { let options = "" for (let i = min; i <= max; i++) { options += `` } return options; } /* -------------------------------------------- */ static getTarget() { if (game.user.targets && game.user.targets.size == 1) { for (let target of game.user.targets) { return target; } } return undefined; } /* -------------------------------------------- */ static getDefenseState(actorId) { return this.defenderStore[actorId]; } /* -------------------------------------------- */ static async updateDefenseState(defenderId, rollId) { this.defenderStore[defenderId] = rollId; if (game.user.character && game.user.character.id == defenderId) { let defender = game.actors.get(defenderId); let chatData = { user: game.user.id, alias: defender.name, rollMode: game.settings.get("core", "rollMode"), whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')), content: `
Are you sure to remove this Item ?"; let buttons = { delete: { icon: '', label: "Yes, remove it", callback: () => { actorSheet.actor.deleteEmbeddedDocuments("Item", [itemId]); li.slideUp(200, () => actorSheet.render(false)); } }, cancel: { icon: '', label: "Cancel" } } msgTxt += "
"; let d = new Dialog({ title: "Confirm removal", content: msgTxt, buttons: buttons, default: "cancel" }); d.render(true); } }