fvtt-vadentis/modules/vadentis-utility.js

250 lines
8.6 KiB
JavaScript
Raw Normal View History

2024-11-19 23:54:30 +01:00
/* -------------------------------------------- */
2021-04-01 21:18:36 +02:00
import { VadentisCombat } from "./vadentis-combat.js";
2024-11-19 23:54:30 +01:00
/* -------------------------------------------- */
export class VadentisUtility {
/* -------------------------------------------- */
2021-04-01 21:18:36 +02:00
static async preloadHandlebarsTemplates() {
2024-11-19 23:54:30 +01:00
// Handle v12 removal of this helper
Handlebars.registerHelper('select', function (selected, options) {
const escapedValue = RegExp.escape(Handlebars.escapeExpression(selected));
const rgx = new RegExp(' value=[\"\']' + escapedValue + '[\"\']');
const html = options.fn(this);
return html.replace(rgx, "$& selected");
});
2021-04-01 21:18:36 +02:00
const templatePaths = [
'systems/foundryvtt-vadentis/templates/actor-sheet.html',
2021-04-01 22:35:03 +02:00
'systems/foundryvtt-vadentis/templates/item-sheet.html',
2021-04-24 21:30:17 +02:00
'systems/foundryvtt-vadentis/templates/editor-notes-gm.html',
'systems/foundryvtt-vadentis/templates/hud-actor-attaque.html',
'systems/foundryvtt-vadentis/templates/hud-actor-sort.html'
2021-04-01 21:18:36 +02:00
]
2024-11-19 23:54:30 +01:00
return loadTemplates(templatePaths);
2021-04-01 21:18:36 +02:00
}
2021-04-08 13:58:51 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static updateCombat(combat, round, diff, id) {
if (game.user.isGM && combat.round != 0 && combat.turns && combat.active) {
2021-04-08 13:58:51 +02:00
let turn = combat.turns.find(t => t.tokenId == combat.current.tokenId);
2024-11-19 23:54:30 +01:00
ChatMessage.create({ content: `Round ${combat.round} : C'est au tour de ${turn.actor.name}<br>` });
2021-04-08 13:58:51 +02:00
canvas.tokens.get(turn.token._id).control();
canvas.tokens.cycleTokens(1, true);
2024-11-19 23:54:30 +01:00
}
2021-04-08 13:58:51 +02:00
}
2021-04-03 22:48:02 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static createOptionList(min, max) {
2021-04-03 22:48:02 +02:00
let options = ""
2024-11-19 23:54:30 +01:00
for (let i = min; i <= max; i++) {
2021-04-08 13:58:51 +02:00
options += `<option value="${i}">${i}</option>\n`;
2021-04-03 22:48:02 +02:00
}
return options;
}
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static createDirectOptionList(min, max) {
2021-04-06 08:03:44 +02:00
let options = {};
2024-11-19 23:54:30 +01:00
for (let i = min; i <= max; i++) {
2021-04-08 13:58:51 +02:00
options[`${i}`] = `${i}`;
2021-04-03 22:48:02 +02:00
}
return options;
2021-04-08 13:58:51 +02:00
}
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static createDirectIntegerOptionList(min, max) {
let options = {};
2024-11-19 23:54:30 +01:00
for (let i = min; i <= max; i++) {
options[i] = `${i}`;
}
return options;
}
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static createDirectReverseOptionList(min, max) {
2021-04-08 13:58:51 +02:00
let options = {};
2024-11-19 23:54:30 +01:00
for (let i = max; i >= min; i--) {
2021-04-08 13:58:51 +02:00
options[`${i}`] = `${i}`;
2021-04-03 22:48:02 +02:00
}
2021-04-08 13:58:51 +02:00
return options;
}
2024-11-19 23:54:30 +01:00
2021-04-03 22:48:02 +02:00
/* -------------------------------------------- */
static getTarget() {
if (game.user.targets && game.user.targets.size == 1) {
2024-11-19 23:54:30 +01:00
for (let target of game.user.targets) {
return target;
2021-04-03 22:48:02 +02:00
}
}
return undefined;
}
2024-11-19 23:54:30 +01:00
2021-04-08 13:58:51 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static processDamageString(formula, actor) {
2021-04-08 13:58:51 +02:00
let workFormula = formula.toLowerCase();
2024-11-19 23:54:30 +01:00
if (workFormula.includes('bonus de force')) {
2021-04-08 13:58:51 +02:00
workFormula = workFormula.replace('bonus de force', actor.getForceScore());
}
return workFormula;
2024-11-19 23:54:30 +01:00
}
2021-04-08 13:58:51 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static async processRoll(formula, rollMode) {
2021-04-08 13:58:51 +02:00
let myRoll = new Roll(formula);
2024-11-19 23:54:30 +01:00
await myRoll.roll();
if (game.modules.get("dice-so-nice")?.active) {
2021-04-08 13:58:51 +02:00
await game.dice3d.showForRoll(myRoll, game.user, true);
}
return myRoll;
}
2021-04-03 22:48:02 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static async performAttack(combatData) {
2021-04-03 22:48:02 +02:00
let attacker = game.actors.get(combatData.attackerActorId);
let defender = game.actors.get(combatData.targetActorId);
2021-04-18 18:14:31 +02:00
console.log("ATTACK !!", attacker, defender);
2024-11-19 23:54:30 +01:00
if (attacker && defender) {
let defense = defender.getDefenseScore();
2021-04-03 22:48:02 +02:00
let attaque = attacker.getAttaqueScore();
2021-04-08 13:58:51 +02:00
2021-04-13 14:01:22 +02:00
let msgData = {
2024-11-19 23:54:30 +01:00
alias: this.name,
2021-04-13 14:01:22 +02:00
title: `${attacker.name} attaque ${defender.name}`
}
2024-11-19 23:54:30 +01:00
2021-04-23 20:40:59 +02:00
let tirMsg = "";
2024-11-19 23:54:30 +01:00
if (combatData.arme.type == 'tir') {
2021-04-23 20:40:59 +02:00
attacker.decrementeMunition(combatData.arme);
tirMsg += `<br>C'est un tir, les munitions de l'attaquant ont été décrémentées`;
}
2024-11-19 23:54:30 +01:00
let formulaTouche = "1d20+" + attaque;
let formulaFull = attacker.buildTexteFormula(attacker.system.combat.attaque);
2021-04-13 14:01:22 +02:00
let myRoll = await this.processRoll(formulaTouche);
2022-01-21 10:57:09 +01:00
if (myRoll.dice[0].results[0].result > 1 && myRoll.total >= defense) { // Success !
let degats = `normaux : ${combatData.arme.system.damage}`;
let formula = combatData.arme.system.damage.toLowerCase();
2021-04-13 14:01:22 +02:00
msgData.msg = `${attacker.name} a réussi son attaque sur ${defender.name} (${formulaFull} => ${myRoll.total} / ${defense}) !<br> Les dégâts sont ${degats}.`;
2024-11-19 23:54:30 +01:00
2021-04-23 20:40:59 +02:00
msgData.msg += tirMsg;
2024-11-19 23:54:30 +01:00
if (myRoll.dice[0].results[0].result >= combatData.arme.system.valuecritical) {
degats = `critiques : ${combatData.arme.system.criticaldamage}`;
formula = combatData.arme.system.criticaldamage.toLowerCase();
2021-04-13 14:01:22 +02:00
msgData.msg += `<br>C'est une <strong>réussite critique</strong> !`;
2024-11-19 23:54:30 +01:00
}
2021-04-13 14:01:22 +02:00
msgData.img = 'systems/foundryvtt-vadentis/images/icons/tchat_attaque_réussie.webp'
2024-11-19 23:54:30 +01:00
formula = this.processDamageString(formula, attacker);
2021-04-08 13:58:51 +02:00
let degatsRoll = await this.processRoll(formula);
2024-11-19 23:54:30 +01:00
msgData.msg += `<br>Les dégats infligés sont de <strong>${degatsRoll.total}</strong> (${formula}).`;
defender.applyDamage(degatsRoll.total);
2021-04-03 22:48:02 +02:00
} else { //Echec
2021-04-13 14:01:22 +02:00
msgData.img = 'systems/foundryvtt-vadentis/images/icons/tchat_attaque_échouée.webp';
2024-11-19 23:54:30 +01:00
if (myRoll.dice[0].results[0].result == 1) {
2021-04-13 14:01:22 +02:00
msgData.msg = `${attacker.name} a fait un <strong>échec critique</strong> et a raté son attaque sur ${defender.name} (${myRoll.total} / ${defense}) !`;
2021-04-08 13:58:51 +02:00
} else {
2021-04-13 14:01:22 +02:00
msgData.msg = `${attacker.name} a raté son attaque sur ${defender.name} (${myRoll.total} / ${defense}) !`;
2021-04-08 13:58:51 +02:00
}
2021-04-23 20:40:59 +02:00
msgData.msg += tirMsg;
2021-04-03 22:48:02 +02:00
}
2021-04-13 14:01:22 +02:00
ChatMessage.create({
//whisper: ChatUtility.getWhisperRecipientsAndGMs(game.user.name),
content: await renderTemplate(`systems/foundryvtt-vadentis/templates/chat-generic-result.html`, msgData)
2024-11-19 23:54:30 +01:00
});
2021-04-03 22:48:02 +02:00
} else {
ui.notifications.warn("Impossible de trouver l'attaquant et le défenseur.")
}
}
2021-04-13 20:19:32 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static buildJetText(stat) {
2021-04-13 20:19:32 +02:00
let name = stat.label;
let title = `Jet de ${name}`;
2024-11-19 23:54:30 +01:00
if (name.toLowerCase().substr(0, 1).match(/[aeoiou]/g)) {
2021-04-13 20:19:32 +02:00
title = `Jet d'${name}`;
2024-11-19 23:54:30 +01:00
}
2021-04-13 20:19:32 +02:00
return title;
}
2024-11-19 23:54:30 +01:00
2021-04-01 22:35:03 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static registerChatCallbacks() {
2021-04-01 22:35:03 +02:00
}
2024-11-19 23:54:30 +01:00
2021-04-01 21:18:36 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static fillRange(start, end) {
2021-04-01 21:18:36 +02:00
return Array(end - start + 1).fill().map((item, index) => start + index);
}
2024-11-19 23:54:30 +01:00
2021-04-01 21:18:36 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static onSocketMesssage(msg) {
if (!game.user.isGM) return; // Only GM
2021-04-01 21:18:36 +02:00
2024-11-19 23:54:30 +01:00
if (msg.name == 'msg_attack') {
this.performAttack(msg.data);
2021-04-01 21:18:36 +02:00
}
}
/* -------------------------------------------- */
static async loadCompendiumNames(compendium) {
const pack = game.packs.get(compendium);
let competences;
await pack.getIndex().then(index => competences = index);
return competences;
}
2024-11-19 23:54:30 +01:00
2021-04-01 21:18:36 +02:00
/* -------------------------------------------- */
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;
}
2021-04-02 16:47:38 +02:00
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static getDonnees() {
2021-04-02 16:47:38 +02:00
return this.loadCompendiumNames('foundryvtt-vadentis.donnees');
}
/* -------------------------------------------- */
2024-11-19 23:54:30 +01:00
static getEglises() {
2024-11-19 23:38:04 +01:00
return this.loadCompendiumNames('foundryvtt-vadentis.croyances');
2021-04-02 16:47:38 +02:00
}
2021-04-02 14:59:58 +02:00
/* -------------------------------------------- */
static async confirmDelete(actorSheet, li) {
let itemId = li.data("item-id");
let msgTxt = "<p>Etes vous certain de souhaiter supprimer cet item ?";
let buttons = {
delete: {
2024-11-19 23:54:30 +01:00
icon: '<i class="fas fa-check"></i>',
label: "Oui, à supprimer",
callback: () => {
actorSheet.actor.deleteEmbeddedDocuments('Item', [itemId]);
li.slideUp(200, () => actorSheet.render(false));
2021-04-02 14:59:58 +02:00
}
2024-11-19 23:54:30 +01:00
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: "Annuler"
2021-04-02 14:59:58 +02:00
}
2024-11-19 23:54:30 +01:00
}
msgTxt += "</p>";
let d = new Dialog({
title: "Confirmer la suppression",
content: msgTxt,
buttons: buttons,
default: "cancel"
});
d.render(true);
2021-04-02 14:59:58 +02:00
}
2021-04-01 21:18:36 +02:00
}