2020-12-21 23:35:29 +01:00
|
|
|
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';
|
2020-12-21 23:35:29 +01:00
|
|
|
/**
|
|
|
|
* Extend the base Dialog entity to select roll parameters
|
|
|
|
* @extends {Dialog}
|
|
|
|
*/
|
|
|
|
/* -------------------------------------------- */
|
2021-01-02 14:10:43 +01:00
|
|
|
export class RdDRollResolutionTable extends Dialog {
|
2020-12-21 23:35:29 +01:00
|
|
|
|
2022-12-06 01:30:12 +01:00
|
|
|
static resolutionTable = undefined;
|
2020-12-21 23:35:29 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-12-06 01:30:12 +01:00
|
|
|
static async open() {
|
|
|
|
if (RdDRollResolutionTable.resolutionTable == undefined) {
|
|
|
|
const rollData = {}
|
|
|
|
RdDRollResolutionTable._setDefaultOptions(rollData);
|
|
|
|
let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-roll-resolution.html', rollData);
|
|
|
|
RdDRollResolutionTable.resolutionTable = new RdDRollResolutionTable(rollData, html);
|
|
|
|
RdDRollResolutionTable.resolutionTable.render(true);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
RdDRollResolutionTable.resolutionTable.bringToTop();
|
|
|
|
}
|
2020-12-21 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2020-12-29 00:11:58 +01:00
|
|
|
static _setDefaultOptions(rollData) {
|
|
|
|
let defRollData = {
|
2021-01-02 04:28:43 +01:00
|
|
|
show: { title: titleTableDeResolution },
|
2020-12-21 23:35:29 +01:00
|
|
|
ajustementsConditions: CONFIG.RDD.ajustementsConditions,
|
2021-01-16 23:45:15 +01:00
|
|
|
difficultesLibres: CONFIG.RDD.ajustementsConditions,
|
2020-12-21 23:35:29 +01:00
|
|
|
etat: 0,
|
|
|
|
moral: 0,
|
|
|
|
carac: {},
|
|
|
|
finalLevel: 0,
|
|
|
|
diffConditions: 0,
|
2021-01-01 03:25:48 +01:00
|
|
|
diffLibre: 0,
|
2021-05-29 20:20:30 +02:00
|
|
|
use: { conditions:true, libre:true }
|
2020-12-21 23:35:29 +01:00
|
|
|
}
|
2021-01-01 03:25:48 +01:00
|
|
|
mergeObject(rollData, defRollData, { overwrite: false });
|
2020-12-21 23:35:29 +01:00
|
|
|
for (let i = 1; i < 21; i++) {
|
2021-01-01 03:25:48 +01:00
|
|
|
const key = `${i}`;
|
|
|
|
rollData.carac[key] = { type: "number", value: i, label: key }
|
2020-12-21 23:35:29 +01:00
|
|
|
}
|
2021-01-01 03:25:48 +01:00
|
|
|
let selected = (rollData.selectedCarac && rollData.selectedCarac.label)
|
|
|
|
? rollData.selectedCarac.label
|
|
|
|
: (Number.isInteger(rollData.selectedCarac))
|
|
|
|
? rollData.selectedCarac
|
|
|
|
: 10;
|
|
|
|
rollData.selectedCarac = rollData.carac[selected];
|
2020-12-21 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
constructor(rollData, html) {
|
|
|
|
let conf = {
|
2020-12-31 02:08:58 +01:00
|
|
|
title: titleTableDeResolution,
|
2020-12-21 23:35:29 +01:00
|
|
|
content: html,
|
|
|
|
buttons: {
|
2021-01-18 11:24:16 +01:00
|
|
|
'lancer-fermer': { label: 'Lancer les dés et fermer', callback: html => this.onLancerFermer() }
|
2021-10-09 22:02:13 +02:00
|
|
|
}
|
2020-12-21 23:35:29 +01:00
|
|
|
};
|
2022-12-06 01:30:12 +01:00
|
|
|
super(conf, { classes: ["rdd-roll-dialog"], top: 50, width: 'fit-content', height: 'fit-content', 'z-index': 99999 });
|
2020-12-21 23:35:29 +01:00
|
|
|
|
|
|
|
this.rollData = rollData;
|
|
|
|
}
|
|
|
|
|
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html = html;
|
2020-12-21 23:35:29 +01:00
|
|
|
this.bringToTop();
|
|
|
|
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find("[name='diffLibre']").val(Misc.toInt(this.rollData.diffLibre));
|
|
|
|
this.html.find("[name='diffConditions']").val(Misc.toInt(this.rollData.diffConditions));
|
|
|
|
this.updateRollResult();
|
|
|
|
|
|
|
|
this.html.find('.lancer-table-resolution').click((event) => {
|
2021-01-18 11:24:16 +01:00
|
|
|
this.onLancer();
|
|
|
|
});
|
2020-12-21 23:35:29 +01:00
|
|
|
// Update !
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find("[name='diffLibre']").change((event) => {
|
2021-01-12 01:08:03 +01:00
|
|
|
this.rollData.diffLibre = Misc.toInt(event.currentTarget.value);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.updateRollResult();
|
2020-12-21 23:35:29 +01:00
|
|
|
});
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find("[name='diffConditions']").change((event) => {
|
2021-01-16 23:45:15 +01:00
|
|
|
this.rollData.diffConditions = Misc.toInt(event.currentTarget.value);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.updateRollResult();
|
2020-12-21 23:35:29 +01:00
|
|
|
});
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find("[name='carac']").change((event) => {
|
2020-12-21 23:35:29 +01:00
|
|
|
let caracKey = event.currentTarget.value;
|
2021-01-12 01:08:03 +01:00
|
|
|
this.rollData.selectedCarac = this.rollData.carac[caracKey];
|
2022-12-09 02:00:31 +01:00
|
|
|
this.updateRollResult();
|
2020-12-21 23:35:29 +01:00
|
|
|
});
|
|
|
|
}
|
2021-05-28 09:12:18 +02:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-12-09 02:00:31 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
async updateRollResult() {
|
2021-01-12 01:08:03 +01:00
|
|
|
let rollData = this.rollData;
|
|
|
|
rollData.caracValue = parseInt(rollData.selectedCarac.value)
|
|
|
|
rollData.finalLevel = this._computeFinalLevel(rollData);
|
|
|
|
|
2022-12-06 01:30:12 +01:00
|
|
|
const htmlTable = await RdDResolutionTable.buildHTMLTable({
|
|
|
|
carac:rollData.caracValue,
|
|
|
|
level: rollData.finalLevel
|
|
|
|
});
|
|
|
|
|
2021-01-12 01:08:03 +01:00
|
|
|
// Mise à jour valeurs
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find("[name='carac']").val(rollData.caracValue);
|
|
|
|
this.html.find(".roll-param-resolution").text(rollData.selectedCarac.value + " / " + Misc.toSignedString(rollData.finalLevel));
|
|
|
|
this.html.find(".table-resolution").remove();
|
|
|
|
this.html.find(".table-proba-reussite").remove();
|
2022-12-06 01:30:12 +01:00
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find("div.placeholder-resolution").append(htmlTable)
|
2022-12-06 01:30:12 +01:00
|
|
|
|
2021-01-12 01:08:03 +01:00
|
|
|
}
|
2020-12-21 23:35:29 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
_computeFinalLevel(rollData) {
|
|
|
|
const diffConditions = Misc.toInt(rollData.diffConditions);
|
2022-12-06 01:30:12 +01:00
|
|
|
const diffLibre = Misc.toInt(rollData.diffLibre);
|
2020-12-21 23:35:29 +01:00
|
|
|
|
|
|
|
return diffLibre + diffConditions;
|
|
|
|
}
|
|
|
|
|
2022-12-06 01:30:12 +01:00
|
|
|
async close() {
|
|
|
|
await super.close();
|
|
|
|
RdDRollResolutionTable.resolutionTable = undefined;
|
2020-12-21 23:35:29 +01:00
|
|
|
}
|
|
|
|
}
|