/* -------------------------------------------- */ import { PegasusCommands } from "./pegasus-commands.js"; import { PegasusActorCreate } from "./pegasus-create-char.js"; /* -------------------------------------------- */ const __level2Dice = [ "d0", "d4", "d6", "d8", "d10", "d12" ]; const __name2DiceValue = { "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)); this.rollDataStore = {} this.defenderStore = {} this.diceList = []; this.diceFoundryList = []; this.optionsDiceList = ""; this.buildDiceLists(); PegasusCommands.init(); 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) }); } /* -------------------------------------------- */ 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 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 = ''; console.log("Defautl dice List", diceList, diceFoundryList); } /* -------------------------------------------- */ 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 => { console.log("Event !") game.system.pegasus.creator.processChatEvent(event); } ); } /* -------------------------------------------- */ static async preloadHandlebarsTemplates() { const templatePaths = [ 'systems/fvtt-pegasus-rpg/templates/editor-notes-gm.html', 'systems/fvtt-pegasus-rpg/templates/partial-roll-common-dices.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' ] return loadTemplates(templatePaths); } /* -------------------------------------------- */ 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); } }