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

134 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

import { Misc } from "./misc.js";
import { RdDResolutionTable } from "./rdd-resolution-table.js";
2020-12-31 02:08:58 +01:00
const titleTableDeResolution = 'Table de résolution';
/**
* Extend the base Dialog entity to select roll parameters
* @extends {Dialog}
*/
/* -------------------------------------------- */
export class RdDRollResolutionTable extends Dialog {
/* -------------------------------------------- */
static async open(rollData = {}) {
RdDRollResolutionTable._setDefaultOptions(rollData);
let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-roll-resolution.html', rollData);
const dialog = new RdDRollResolutionTable(rollData, html);
dialog.render(true);
}
/* -------------------------------------------- */
2020-12-29 00:11:58 +01:00
static _setDefaultOptions(rollData) {
let defRollData = {
show: { title: titleTableDeResolution },
ajustementsConditions: CONFIG.RDD.ajustementsConditions,
2021-01-16 23:45:15 +01:00
difficultesLibres: CONFIG.RDD.ajustementsConditions,
etat: 0,
moral: 0,
carac: {},
finalLevel: 0,
diffConditions: 0,
diffLibre: 0,
}
mergeObject(rollData, defRollData, { overwrite: false });
for (let i = 1; i < 21; i++) {
const key = `${i}`;
rollData.carac[key] = { type: "number", value: i, label: key }
}
let selected = (rollData.selectedCarac && rollData.selectedCarac.label)
? rollData.selectedCarac.label
: (Number.isInteger(rollData.selectedCarac))
? rollData.selectedCarac
: 10;
rollData.selectedCarac = rollData.carac[selected];
}
/* -------------------------------------------- */
constructor(rollData, html) {
let conf = {
2020-12-31 02:08:58 +01:00
title: titleTableDeResolution,
content: html,
buttons: {
'lancer-fermer': { label: 'Lancer les dés et fermer', callback: html => this.onLancerFermer() }
},
default: 'lancer'
};
super(conf, { classes: ["rdddialog"], width: 800, height: 800, 'z-index': 99999 });
this.rollData = rollData;
}
/* -------------------------------------------- */
async onLancer() {
await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRollResolutionTable -=>", this.rollData, this.rollData.rolled);
await RdDResolutionTable.displayRollData(this.rollData);
}
/* -------------------------------------------- */
async onLancerFermer() {
await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRollResolutionTable -=>", this.rollData, this.rollData.rolled);
await RdDResolutionTable.displayRollData(this.rollData);
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
this.bringToTop();
var dialog = this;
// Setup everything onload
2021-01-12 01:08:03 +01:00
function onLoad(){
$("#diffLibre").val(Misc.toInt(dialog.rollData.diffLibre));
$("#diffConditions").val(Misc.toInt(dialog.rollData.diffConditions));
dialog.updateRollResult();
}
$(function () { onLoad();});
html.find('#lancer').click((event) => {
this.onLancer();
});
// Update !
html.find('#diffLibre').change((event) => {
2021-01-12 01:08:03 +01:00
this.rollData.diffLibre = Misc.toInt(event.currentTarget.value);
this.updateRollResult();
});
html.find('#diffConditions').change((event) => {
2021-01-16 23:45:15 +01:00
this.rollData.diffConditions = Misc.toInt(event.currentTarget.value);
2021-01-12 01:08:03 +01:00
this.updateRollResult();
});
html.find('#carac').change((event) => {
let caracKey = event.currentTarget.value;
2021-01-12 01:08:03 +01:00
this.rollData.selectedCarac = this.rollData.carac[caracKey];
this.updateRollResult();
});
}
2021-01-12 01:08:03 +01:00
async updateRollResult() {
let rollData = this.rollData;
rollData.caracValue = parseInt(rollData.selectedCarac.value)
rollData.finalLevel = this._computeFinalLevel(rollData);
// Mise à jour valeurs
$("#carac").val(rollData.caracValue);
$("#roll-param").text(rollData.selectedCarac.value + " / " + Misc.toSignedString(rollData.finalLevel));
$(".table-resolution").remove();
$(".table-proba-reussite").remove();
$("#tableResolution").append(RdDResolutionTable.buildHTMLTable(rollData.caracValue, rollData.finalLevel));
$("#tableProbaReussite").append(RdDResolutionTable.buildHTMLResults(rollData.caracValue, rollData.finalLevel));
}
/* -------------------------------------------- */
_computeFinalLevel(rollData) {
const diffConditions = Misc.toInt(rollData.diffConditions);
const diffLibre = this._computeDiffLibre(rollData);
return diffLibre + diffConditions;
}
_computeDiffLibre(rollData) {
return Misc.toInt(rollData.diffLibre);
}
}