foundryvtt-reve-de-dragon/module/rdd-roll.js

305 lines
12 KiB
JavaScript
Raw Normal View History

2021-01-07 00:40:56 +01:00
import { RollDataAjustements } from "./rolldata-ajustements.js";
import { HtmlUtility } from "./html-utility.js";
2020-12-20 21:54:09 +01:00
import { RdDItemCompetence } from "./item-competence.js";
import { RdDItemMeditation } from "./item-meditation.js";
import { RdDItemSort } from "./item-sort.js";
import { Misc } from "./misc.js";
import { RdDBonus } from "./rdd-bonus.js";
import { RdDCarac } from "./rdd-carac.js";
import { RdDResolutionTable } from "./rdd-resolution-table.js";
/**
* Extend the base Dialog entity to select roll parameters
* @extends {Dialog}
*/
/* -------------------------------------------- */
export class RdDRoll extends Dialog {
/* -------------------------------------------- */
static async create(actor, rollData, dialogConfig, ...actions) {
RdDRoll._ensureCorrectActions(actions);
RdDRoll._setDefaultOptions(actor, rollData);
const html = await renderTemplate(dialogConfig.html, rollData);
let options = { classes: ["rdddialog"], width: 600, height: 500, 'z-index': 99999 };
if (dialogConfig.options) {
mergeObject(options, dialogConfig.options, { overwrite: true })
}
return new RdDRoll(actor, rollData, html, options, actions, dialogConfig.close);
}
/* -------------------------------------------- */
static _setDefaultOptions(actor, rollData) {
let defaultRollData = {
alias: actor.name,
ajustementsConditions: CONFIG.RDD.ajustementsConditions,
difficultesLibres: CONFIG.RDD.difficultesLibres,
etat: actor.getEtatGeneral(),
moral: actor.getMoralTotal(),
carac: actor.data.data.carac,
finalLevel: 0,
diffConditions: 0,
diffLibre: rollData.competence?.data.default_diffLibre ?? 0,
malusArmureValue: actor.getMalusArmure(),
2020-12-18 23:57:28 +01:00
surencMalusFlag: actor.isPersonnage() ? (actor.data.data.compteurs.surenc.value < 0) : false,
surencMalusValue: actor.getSurenc(),
2021-01-05 18:39:12 +01:00
useMalusSurenc: false,
2021-01-13 03:11:03 +01:00
use: { libre:true, conditions: true, surenc: false, encTotal: false, },
2020-12-20 21:54:09 +01:00
isMalusEncombrementTotal: RdDItemCompetence.isMalusEncombrementTotal(rollData.competence),
useMalusEncTotal: false,
2021-01-05 18:39:12 +01:00
encTotal: actor.getEncTotal(),
ajustementAstrologique: actor.ajustementAstrologique(),
surprise: actor.getSurprise(false),
}
mergeObject(rollData, defaultRollData, { recursive: true, overwrite: false });
2021-01-27 23:35:45 +01:00
if ( rollData.forceCarac) {
rollData.carac = rollData.forceCarac;
}
RollDataAjustements.calcul(rollData, actor);
}
/* -------------------------------------------- */
static _ensureCorrectActions(actions) {
if (actions.length == 0) {
throw 'No action defined';
}
actions.forEach(action => {
if (action.callbacks == undefined) {
action.callbacks = [{ action: r => console.log(action.name, r) }];
}
});
}
/* -------------------------------------------- */
2020-12-28 10:17:40 +01:00
constructor(actor, rollData, html, options, actions, close = undefined) {
let conf = {
title: actions[0].label,
content: html,
buttons: {},
2020-12-28 10:17:40 +01:00
default: actions[0].name,
close: close
};
for (let action of actions) {
conf.buttons[action.name] = { label: action.label, callback: html => this.onAction(action, html) };
}
super(conf, options);
this.actor = actor;
this.rollData = rollData;
}
/* -------------------------------------------- */
async onAction(action, html) {
await RdDResolutionTable.rollData(this.rollData);
2020-12-08 23:07:41 +01:00
console.log("RdDRoll -=>", this.rollData, this.rollData.rolled);
if (action.callbacks)
for (let callback of action.callbacks) {
if (callback.condition == undefined || callback.condition(this.rollData)) {
callback.action(this.rollData);
}
}
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
this.bringToTop();
2020-12-18 23:57:28 +01:00
var dialog = this;
function onLoad() {
let rollData = dialog.rollData;
// Update html, according to data
if (rollData.competence) {
// Set the default carac from the competence item
rollData.selectedCarac = rollData.carac[rollData.competence.data.defaut_carac];
$("#carac").val(rollData.competence.data.defaut_carac);
}
2021-02-02 07:56:45 +01:00
if (rollData.selectedSort) {
$("#draconic").val( rollData.selectedSort.data.listIndex ); // Uniquement a la selection du sort, pour permettre de changer
}
RdDItemSort.setCoutReveReel(rollData.selectedSort);
$("#diffLibre").val(Misc.toInt(rollData.diffLibre));
$("#diffConditions").val(Misc.toInt(rollData.diffConditions));
dialog.updateRollResult();
}
// Setup everything onload
$(function () { onLoad(); });
// Update !
html.find('#diffLibre').change((event) => {
this.rollData.diffLibre = Misc.toInt(event.currentTarget.value); // Update the selected bonus/malus
this.updateRollResult();
});
html.find('#diffConditions').change((event) => {
this.rollData.diffConditions = Misc.toInt(event.currentTarget.value); // Update the selected bonus/malus
this.updateRollResult();
});
html.find('#carac').change((event) => {
let caracKey = event.currentTarget.value;
this.rollData.selectedCarac = this.rollData.carac[caracKey]; // Update the selectedCarac
this.updateRollResult();
});
html.find('#draconic').change((event) => {
let draconicKey = Misc.toInt(event.currentTarget.value);
this.rollData.competence = this.rollData.draconicList[draconicKey]; // Update the selectedCarac
this.updateRollResult();
});
html.find('#sort').change((event) => {
let sortKey = Misc.toInt(event.currentTarget.value);
this.rollData.selectedSort = this.rollData.sortList[sortKey]; // Update the selectedCarac
this.rollData.bonus = RdDItemSort.getCaseBonus(this.rollData.selectedSort, this.rollData.coord);
RdDItemSort.setCoutReveReel(this.rollData.selectedSort);
2021-02-02 07:56:45 +01:00
$("#draconic").val( this.rollData.selectedSort.data.listIndex ); // Uniquement a la selection du sort, pour permettre de changer
this.updateRollResult();
});
html.find('#ptreve-variable').change((event) => {
let ptreve = Misc.toInt(event.currentTarget.value);
this.rollData.selectedSort.data.ptreve_reel = ptreve;
console.log("RdDRollSelectDialog - Cout reve", ptreve);
this.updateRollResult();
});
html.find('#ptreve-variable').change((event) => {
let ptreve = Misc.toInt(event.currentTarget.value);
this.rollData.selectedSort.data.ptreve_reel = ptreve; // Update the selectedCarac
console.log("RdDRollSelectDialog - Cout reve", ptreve);
this.updateRollResult();
});
html.find('#coupsNonMortels').change((event) => {
this.rollData.dmg.mortalite = event.currentTarget.checked ? "non-mortel" : "mortel";
this.updateRollResult();
});
html.find('#tactique-combat').change((event) => {
this.rollData.tactique = event.currentTarget.value;
this.updateRollResult();
});
2021-01-05 18:39:12 +01:00
html.find('#useMalusSurenc').change((event) => {
this.rollData.useMalusSurenc = event.currentTarget.checked;
this.updateRollResult();
});
2020-12-20 21:54:09 +01:00
html.find('#useMalusEncTotal').change((event) => {
this.rollData.useMalusEncTotal = event.currentTarget.checked;
this.updateRollResult();
});
// Section Méditation
html.find('.conditionMeditation').change((event) => {
let condition = event.currentTarget.attributes['id'].value;
this.rollData.conditionMeditation[condition] = event.currentTarget.checked;
this.updateRollResult();
});
}
2021-01-29 20:10:43 +01:00
/* -------------------------------------------- */
async updateRollResult() {
let rollData = this.rollData;
rollData.dmg = rollData.attackerRoll?.dmg ?? RdDBonus.dmg(rollData, this.actor.getBonusDegat());
rollData.caracValue = parseInt(rollData.selectedCarac.value);
rollData.coupsNonMortels = (rollData.attackerRoll?.dmg.mortalite ?? rollData.dmg.mortalite) == 'non-mortel';
let dmgText = Misc.toSignedString(rollData.dmg.total);
if (rollData.coupsNonMortels) {
dmgText = '(' + dmgText + ')';
}
if (rollData.selectedSort) {
rollData.bonus = RdDItemSort.getCaseBonus(rollData.selectedSort, rollData.coord);
}
RollDataAjustements.calcul(rollData, this.actor);
rollData.finalLevel = this._computeFinalLevel(rollData);
HtmlUtility._showControlWhen($(".diffMoral"), rollData.ajustements.moralTotal.used);
2021-01-13 17:30:44 +01:00
HtmlUtility._showControlWhen($("#etat-general"), !RdDCarac.isIgnoreEtatGeneral(rollData.selectedCarac, rollData.competence));
HtmlUtility._showControlWhen($("#ajust-astrologique"), RdDResolutionTable.isAjustementAstrologique(rollData));
// Sort management
if (rollData.selectedSort) {
rollData.bonus = RdDItemSort.getCaseBonus(rollData.selectedSort, rollData.coord);
//console.log("Toggle show/hide", rollData.selectedSort);
HtmlUtility._showControlWhen($("#div-sort-difficulte"), RdDItemSort.isDifficulteVariable(rollData.selectedSort))
HtmlUtility._showControlWhen($("#div-sort-ptreve"), RdDItemSort.isCoutVariable(rollData.selectedSort))
}
// Mise à jour valeurs
$("#compdialogTitle").text(this._getTitle(rollData));
$('#coupsNonMortels').prop('checked', rollData.coupsNonMortels);
$("#dmg-arme-actor").text(dmgText);
// $("#defenseur-surprise").text(RdDBonus.description(rollData.ajustements.attaqueDefenseurSurpris.descr));
$('.table-ajustement').remove();
$(".table-resolution").remove();
$(".table-proba-reussite").remove();
$("#tableAjustements").append(await this.buildAjustements(rollData));
$("#tableResolution").append(RdDResolutionTable.buildHTMLTableExtract(rollData.caracValue, rollData.finalLevel));
$("#tableProbaReussite").append(RdDResolutionTable.buildHTMLResults(rollData.caracValue, rollData.finalLevel));
}
2021-01-29 20:10:43 +01:00
/* -------------------------------------------- */
async buildAjustements(rollData){
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-roll-ajustements.html`, rollData);
return html;
}
/* -------------------------------------------- */
2020-12-18 23:57:28 +01:00
_computeFinalLevel(rollData) {
2021-01-13 03:11:03 +01:00
return RollDataAjustements.sum(rollData.ajustements);
}
/* -------------------------------------------- */
2020-12-18 23:57:28 +01:00
_computeDiffCompetence(rollData) {
if (rollData.competence) {
return Misc.toInt(rollData.competence.data.niveau);
}
if (rollData.draconicList) {
2020-12-31 02:20:52 +01:00
return Misc.toInt(rollData.competence.data.niveau);
}
return 0;
}
/* -------------------------------------------- */
2020-12-18 23:57:28 +01:00
_computeDiffLibre(rollData) {
let diffLibre = Misc.toInt(rollData.diffLibre);
if (rollData.draconicList && rollData.selectedSort) {
return RdDItemSort.getDifficulte(rollData.selectedSort, diffLibre);
}
return diffLibre;
}
2021-01-29 20:10:43 +01:00
/* -------------------------------------------- */
2020-12-18 23:57:28 +01:00
_computeMalusArmure(rollData) {
let malusArmureValue = 0;
if (rollData.malusArmureValue && (rollData.selectedCarac.label == "Agilité" || rollData.selectedCarac.label == "Dérobée")) {
$("#addon-message").text("Malus armure appliqué : " + rollData.malusArmureValue);
malusArmureValue = rollData.malusArmureValue;
} else {
$("#addon-message").text("");
}
return malusArmureValue;
}
/* -------------------------------------------- */
2020-12-18 23:57:28 +01:00
_getTitle(rollData) {
const carac = rollData.selectedCarac.label;
if (!rollData.competence) {
return carac;
}
const compName = rollData.competence.name;
if (rollData.draconicList && rollData.selectedSort) {
return compName + " - " + rollData.selectedSort.name;
}
// If a weapon is there, add it in the title
const niveau = Misc.toSignedString(rollData.competence.data.niveau);
if (compName == carac) {
// cas des créatures
return carac + " " + niveau
}
const armeTitle = (rollData.arme) ? " (" + rollData.arme.name + ") " : "";
return carac + "/" + compName + armeTitle + " " + niveau
}
}