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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2021-01-18 11:24:16 +01:00
|
|
|
async onLancer() {
|
|
|
|
await RdDResolutionTable.rollData(this.rollData);
|
|
|
|
console.log("RdDRollResolutionTable -=>", this.rollData, this.rollData.rolled);
|
|
|
|
await RdDResolutionTable.displayRollData(this.rollData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
async onLancerFermer() {
|
2020-12-21 23:35:29 +01:00
|
|
|
await RdDResolutionTable.rollData(this.rollData);
|
2021-01-02 14:10:43 +01:00
|
|
|
console.log("RdDRollResolutionTable -=>", this.rollData, this.rollData.rolled);
|
2021-01-05 18:43:13 +01:00
|
|
|
await RdDResolutionTable.displayRollData(this.rollData);
|
2020-12-21 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
|
|
|
|
|
|
|
this.bringToTop();
|
|
|
|
|
|
|
|
var dialog = this;
|
|
|
|
|
|
|
|
// Setup everything onload
|
2021-01-12 01:08:03 +01:00
|
|
|
function onLoad(){
|
2022-12-06 01:30:12 +01:00
|
|
|
$("[name='diffLibre']").val(Misc.toInt(dialog.rollData.diffLibre));
|
|
|
|
$("[name='diffConditions']").val(Misc.toInt(dialog.rollData.diffConditions));
|
|
|
|
dialog.updateRollResult(html);
|
2021-01-12 01:08:03 +01:00
|
|
|
}
|
|
|
|
$(function () { onLoad();});
|
2022-12-06 01:30:12 +01:00
|
|
|
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-06 01:30:12 +01:00
|
|
|
html.find("[name='diffLibre']").change((event) => {
|
2021-01-12 01:08:03 +01:00
|
|
|
this.rollData.diffLibre = Misc.toInt(event.currentTarget.value);
|
2022-12-06 01:30:12 +01:00
|
|
|
this.updateRollResult(html);
|
2020-12-21 23:35:29 +01:00
|
|
|
});
|
2022-12-06 01:30:12 +01:00
|
|
|
html.find("[name='diffConditions']").change((event) => {
|
2021-01-16 23:45:15 +01:00
|
|
|
this.rollData.diffConditions = Misc.toInt(event.currentTarget.value);
|
2022-12-06 01:30:12 +01:00
|
|
|
this.updateRollResult(html);
|
2020-12-21 23:35:29 +01:00
|
|
|
});
|
2022-12-06 01:30:12 +01:00
|
|
|
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-06 01:30:12 +01:00
|
|
|
this.updateRollResult(html);
|
2020-12-21 23:35:29 +01:00
|
|
|
});
|
|
|
|
}
|
2021-05-28 09:12:18 +02:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-12-06 01:30:12 +01:00
|
|
|
async updateRollResult(html) {
|
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-06 01:30:12 +01:00
|
|
|
html.find("[name='carac']").val(rollData.caracValue);
|
|
|
|
$(".roll-param-resolution").text(rollData.selectedCarac.value + " / " + Misc.toSignedString(rollData.finalLevel));
|
2021-01-12 01:08:03 +01:00
|
|
|
$(".table-resolution").remove();
|
|
|
|
$(".table-proba-reussite").remove();
|
2022-12-06 01:30:12 +01:00
|
|
|
|
|
|
|
html.find("div.placeholder-resolution").append(htmlTable)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|