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

123 lines
4.1 KiB
JavaScript
Raw Normal View History

import { ChatUtility } from "./chat-utility.js";
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 RdDRollResolution extends Dialog {
/* -------------------------------------------- */
2020-12-29 00:11:58 +01:00
static async open(rollData = {selectedCarac:10}) {
RdDRollResolution._setDefaultOptions(rollData);
let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-roll-resolution.html', rollData);
const dialog = new RdDRollResolution(rollData, html);
dialog.render(true);
}
/* -------------------------------------------- */
2020-12-29 00:11:58 +01:00
static _setDefaultOptions(rollData) {
let defRollData = {
2020-12-31 02:08:58 +01:00
show: {title: titleTableDeResolution, points:true},
ajustementsConditions: CONFIG.RDD.ajustementsConditions,
difficultesLibres: CONFIG.RDD.difficultesLibres,
etat: 0,
moral: 0,
carac: {},
finalLevel: 0,
diffConditions: 0,
diffLibre: 0
}
2020-12-29 00:11:58 +01:00
mergeObject(rollData, defRollData, {overwrite: false});
for (let i = 1; i < 21; i++) {
rollData.carac[i] = { type: "number", value: i, label: i }
2020-12-29 00:11:58 +01:00
if (rollData.selectedCarac == i) {
rollData.selectedCarac = rollData.carac[i];
}
}
}
/* -------------------------------------------- */
constructor(rollData, html) {
let conf = {
2020-12-31 02:08:58 +01:00
title: titleTableDeResolution,
content: html,
buttons: {
'lancer': { label: 'Lancer les dés', callback: html => this.onAction(html) }
}
};
super(conf, { classes: ["rdddialog"], width: 800, height: 800, 'z-index': 99999 });
this.rollData = rollData;
}
/* -------------------------------------------- */
async onAction(html) {
await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRollResolution -=>", this.rollData, this.rollData.rolled);
2020-12-31 02:08:58 +01:00
const message = { content: await RdDResolutionTable.explainRollDataV2(this.rollData)};
ChatUtility.chatWithRollMode(message, game.user.name)
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
this.bringToTop();
var rollData = this.rollData;
var dialog = this;
function updateRollResult(rollData) {
rollData.caracValue = parseInt(rollData.selectedCarac.value)
rollData.finalLevel = dialog._computeFinalLevel(rollData);
// Mise à jour valeurs
$("#carac").val(rollData.caracValue);
$("#roll-param").text(rollData.selectedCarac.value + " / " + Misc.toSignedString(rollData.finalLevel));
$(".table-resolution").remove();
$("#resolutionTable").append(RdDResolutionTable.buildHTMLTable(rollData.caracValue, rollData.finalLevel, 1, 20, -10, 10));
$(".span-valeur").remove();
$("#resolutionValeurs").append(RdDResolutionTable.buildHTMLResults(rollData.caracValue, rollData.finalLevel));
}
// Setup everything onload
$(function () {
$("#diffLibre").val(Misc.toInt(rollData.diffLibre));
$("#diffConditions").val(Misc.toInt(rollData.diffConditions));
updateRollResult(rollData);
});
// Update !
html.find('#diffLibre').change((event) => {
rollData.diffLibre = Misc.toInt(event.currentTarget.value);
updateRollResult(rollData);
});
html.find('#diffConditions').change((event) => {
rollData.diffConditions = Misc.toInt(event.currentTarget.value);
updateRollResult(rollData);
});
html.find('#carac').change((event) => {
let caracKey = event.currentTarget.value;
this.rollData.selectedCarac = rollData.carac[caracKey];
updateRollResult(rollData);
});
}
/* -------------------------------------------- */
_computeFinalLevel(rollData) {
const diffConditions = Misc.toInt(rollData.diffConditions);
const diffLibre = this._computeDiffLibre(rollData);
return diffLibre + diffConditions;
}
_computeDiffLibre(rollData) {
return Misc.toInt(rollData.diffLibre);
}
}