fvtt-vadentis/modules/vadentis-utility.js
2021-04-06 08:03:44 +02:00

154 lines
5.0 KiB
JavaScript

/* -------------------------------------------- */
import { VadentisCombat } from "./vadentis-combat.js";
/* -------------------------------------------- */
export class VadentisUtility extends Entity {
/* -------------------------------------------- */
static async preloadHandlebarsTemplates() {
const templatePaths = [
'systems/foundryvtt-vadentis/templates/actor-sheet.html',
'systems/foundryvtt-vadentis/templates/item-sheet.html',
'systems/foundryvtt-vadentis/templates/score-option-list.html',
'systems/foundryvtt-vadentis/templates/malus-option-list.html',
'systems/foundryvtt-vadentis/templates/bonus-option-list.html',
'systems/foundryvtt-vadentis/templates/editor-notes-gm.html'
]
return loadTemplates(templatePaths);
}
/* -------------------------------------------- */
static createOptionList( min, max) {
let options = ""
for(let i=min; i<=max; i++) {
options+= `<option value="${i}">${i}</option>\n`;
}
return options;
}
/* -------------------------------------------- */
static createDirectOptionList( min, max) {
let options = {};
for(let i=min; i<=max; i++) {
options[i] = i;
}
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 async performAttack( combatData) {
let attacker = game.actors.get(combatData.attackerActorId);
let defender = game.actors.get(combatData.targetActorId);
if( attacker && defender) {
let defense = defender.getDefenseScore();
let attaque = attacker.getAttaqueScore();
console.log("Attaque : ", attaque);
let myRoll = new Roll("1d20"+attaque);
myRoll.evaluate()
if (game.modules.get("dice-so-nice") && game.modules.get("dice-so-nice").active) {
await game.dice3d.showForRoll(myRoll, game.user, true);
}
if (myRoll.total >= defense) { // Success !
ChatMessage.create( { content: `${attacker.name} a réussi son attaque sur ${defender.name} (${myRoll.total} / ${defense}) !<br> Les dégâts sont de : ${combatData.arme.data.damage}`});
} else { //Echec
ChatMessage.create( { content: `${attacker.name} a raté son attaque sur ${defender.name} (${myRoll.total} / ${defense}) !` });
}
} else {
ui.notifications.warn("Impossible de trouver l'attaquant et le défenseur.")
}
}
/* -------------------------------------------- */
static registerChatCallbacks( ) {
}
/* -------------------------------------------- */
static fillRange (start, end) {
return Array(end - start + 1).fill().map((item, index) => start + index);
}
/* -------------------------------------------- */
static onSocketMesssage( msg ) {
if( !game.user.isGM ) return; // Only GM
if (msg.name == 'msg_attack' ) {
this.performAttack( msg.data );
}
}
/* -------------------------------------------- */
static async loadCompendiumNames(compendium) {
const pack = game.packs.get(compendium);
let competences;
await pack.getIndex().then(index => competences = index);
return competences;
}
/* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) {
let compendiumItems = await SoSUtility.loadCompendiumNames(compendium);
const pack = game.packs.get(compendium);
let list = [];
for (let compendiumItem of compendiumItems) {
await pack.getEntity(compendiumItem._id).then(it => {
const item = it.data;
if (filter(item)) {
list.push(item);
}
});
};
return list;
}
/* -------------------------------------------- */
static getDonnees( ) {
return this.loadCompendiumNames('foundryvtt-vadentis.donnees');
}
/* -------------------------------------------- */
static getEglises( ) {
return this.loadCompendiumNames('foundryvtt-vadentis.eglises');
}
/* -------------------------------------------- */
static async confirmDelete(actorSheet, li) {
let itemId = li.data("item-id");
let objet = actorSheet.actor.items.find(item => item._id == itemId);
let msgTxt = "<p>Etes vous certain de souhaiter supprimer cet item ?";
let buttons = {
delete: {
icon: '<i class="fas fa-check"></i>',
label: "Oui, à supprimer",
callback: () => {
actorSheet.actor.deleteOwnedItem(itemId);
li.slideUp(200, () => actorSheet.render(false));
}
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: "Annuler"
}
}
msgTxt += "</p>";
let d = new Dialog({
title: "Confirmer la suppression",
content: msgTxt,
buttons: buttons,
default: "cancel"
});
d.render(true);
}
}