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

170 lines
6.6 KiB
JavaScript

import { RdDResolutionTable } from "./rdd-resolution-table.js";
/**
* Extend the base Dialog entity by defining a custom window to perform roll.
* @extends {Dialog}
*/
export class RdDRollDialog extends Dialog {
/* -------------------------------------------- */
constructor(mode, html, rollData, actor) {
let myButtons
if (mode == "sort") {
myButtons = {
rollButton: { label: "Lancer le sort", callback: html => this.performRollSort(html, false) },
reserveButton: { label: "Mettre en reserve", callback: html => this.performRollSort(html, true) }
}
}
else {
myButtons = {
rollButton: { label: "Lancer", callback: html => this.actor.performRoll(this.rollData) }
};
}
// Common conf
let dialogConf = { content: html, title: "Test", buttons: myButtons, default: "rollButton" }
let dialogOptions = { classes: ["rdddialog"], width: 600, height: 400 }
// Select proper roll dialog template and stuff
if (mode == "competence") {
dialogConf.title = "Test de compétence"
dialogConf.height = 400
} else if (mode == "arme") {
dialogConf.title = "Test de combat/arme"
dialogConf.height = 430
} else if (mode == "carac") {
dialogConf.title = "Test de caractéristique"
dialogOptions.height = 350
} else if (mode == "sort") {
dialogConf.title = "Lancer un sort"
dialogConf.height = 450
}
super(dialogConf, dialogOptions)
this.mode = mode
this.rollData = rollData
this.actor = actor
}
/* -------------------------------------------- */
performRollSort(html, isSortReserve = false) {
this.rollData.isSortReserve = isSortReserve;
this.actor.performRoll(this.rollData);
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
// Get the rollData stuff
var rollData = this.rollData;
function updateRollResult(rollData) {
let caracValue = parseInt(rollData.selectedCarac.value)
let rollLevel = RdDRollDialog._computeFinalLevel(rollData);
rollData.finalLevel = rollLevel;
rollData.finalLevelStr = (rollLevel > 0 ? "+" : "") + rollLevel;
rollData.rollTarget = RdDResolutionTable.computeChances(rollData.selectedCarac.value, rollData.finalLevel);
// Sort management
if ( rollData.selectedSort ) {
//console.log("Toggle show/hide", rollData.selectedSort);
if (rollData.selectedSort.data.difficulte.toLowerCase() == "variable") {
$("#div-sort-difficulte").show();
} else {
$("#div-sort-difficulte").hide();
}
if (rollData.selectedSort.data.ptreve.toLowerCase() == "variable" || rollData.selectedSort.data.ptreve.indexOf("+")>=0) {
$("#div-sort-ptreve").show();
} else {
$("#div-sort-ptreve").hide();
}
}
$("#roll-param").text(rollData.selectedCarac.value + " / " + rollData.finalLevelStr);
$("#compdialogTitle").text(RdDRollDialog._getTitle(rollData));
$(".table-resolution").remove();
$("#resolutionTable").append(RdDResolutionTable.buildHTMLTableExtract(caracValue, rollLevel));
}
// Setup everything onload
$(function () {
// Update html, according to data
if (rollData.competence) {
// Set the default carac from the competence item
//console.log("RdDDialogRoll", rollData.competence.data.defaut_carac, rollData.carac);
rollData.selectedCarac = rollData.carac[rollData.competence.data.defaut_carac];
$("#carac").val(rollData.competence.data.defaut_carac);
}
// Si sort, for les points de reve à 1
if (rollData.selectedSort && !rollData.selectedSort.data.ptreve_reel)
rollData.selectedSort.data.ptreve_reel = 1;
$("#bonusmalus").val(rollData.bmValue);
updateRollResult(rollData);
});
// Update !
html.find('#bonusmalus').click((event) => {
rollData.bmValue = event.currentTarget.value; // Update the selected bonus/malus
//console.log("RdDRollDialog","BM CLICKED !!!", rollData);
updateRollResult(rollData);
});
html.find('#carac').click((event) => {
let caracKey = event.currentTarget.value;
this.rollData.selectedCarac = rollData.carac[caracKey]; // Update the selectedCarac
//console.log("RdDRollDialog","CARAC CLICKED !!!", rollData);
updateRollResult(rollData);
});
html.find('#draconic').click((event) => {
let draconicKey = Number(event.currentTarget.value);
this.rollData.selectedDraconic = rollData.draconicList[draconicKey]; // Update the selectedCarac
//console.log("RdDRollDialog","CARAC CLICKED !!!", rollData);
updateRollResult(rollData);
});
html.find('#sort').click((event) => {
let sortKey = Number(event.currentTarget.value);
this.rollData.selectedSort = rollData.sortList[sortKey]; // Update the selectedCarac
//console.log("RdDRollDialog - Sort selection", rollData.selectedSort);
updateRollResult(rollData);
});
html.find('#ptreve-variable').click((event) => {
let ptreve = Number(event.currentTarget.value);
this.rollData.selectedSort.data.ptreve_reel = ptreve; // Update the selectedCarac
console.log("RdDRollDialog - Cout reve", ptreve);
updateRollResult(rollData);
});
}
/* -------------------------------------------- */
static _computeFinalLevel(rollData) {
let etat = rollData.etat === undefined ? 0 : parseInt(rollData.etat);
if (rollData.competence) {
return etat + parseInt(rollData.competence.data.niveau) + parseInt(rollData.bmValue);
}
if (rollData.draconicList) {
let difficulte = rollData.selectedSort.data.difficulte; // Sort de difficulté variable
if (difficulte.toLowerCase() == "variable") {
difficulte = parseInt(rollData.bmValue); // Récupérer la valeur de la listbox dans ce cas
}
return etat + parseInt(rollData.selectedDraconic.data.niveau) + parseInt(difficulte);
}
return etat + parseInt(rollData.bmValue);
}
/* -------------------------------------------- */
static _getTitle(rollData) {
if (rollData.competence) {
// If a weapon is there, add it in the title
let armeTitle = (rollData.arme) ? " (" + rollData.arme.name + ") " : "";
let niveauStr = (rollData.competence.data.niveau > 0 ? "+" : "") + rollData.competence.data.niveau;
return rollData.selectedCarac.label + "/" + rollData.competence.name + armeTitle + " " + niveauStr
}
if (rollData.draconicList) {
return rollData.selectedDraconic.name + " - " + rollData.selectedSort.name;
}
return rollData.selectedCarac.label;
}
}