foundryvtt-reve-de-dragon/module/rdd-roll-dialog.js
2020-06-12 22:46:04 +02:00

110 lines
4.1 KiB
JavaScript

/**
* 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) {
// Common conf
let dialogConf = {
content: html,
buttons:
{
rollButton:
{
label: "Lancer",
callback: html => this.performRoll(html)
}
},
default: "rollButton"
}
let dialogOptions = { classes: [ "rdddialog"] }
// Select proper roll dialog template and stuff
if (mode == "competence" ) {
dialogConf.title = "Test de compétence",
dialogOptions.width = 600;
dialogOptions.height = 360;
} else if (mode == "arme") {
dialogConf.title = "Test de combat/arme",
dialogOptions.width = 600;
dialogOptions.height = 360;
} else if (mode == "carac") {
dialogConf.title = "Test de caractéristique",
dialogOptions.width = 600;
dialogOptions.height = 320;
}
super(dialogConf, dialogOptions);
this.mode = mode;
this.rollData = rollData;
this.actor = actor;
}
/* -------------------------------------------- */
performRoll (html) {
this.actor.performRoll( this.rollData );
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
// Get the rollData stuff
var rollData = this.rollData;
function updateRollResult( rollData ) {
if ( rollData.competence )
rollData.finalLevel = parseInt(rollData.competence.data.niveau) + parseInt(rollData.bmValue) + parseInt(rollData.etat);
else
rollData.finalLevel = parseInt(rollData.bmValue) + parseInt(rollData.etat);
rollData.finalLevelStr = (rollData.finalLevel >= 0 ) ? "+" + rollData.finalLevel : rollData.finalLevel;
$("#roll-param").text( rollData.selectedCarac.value + " / " + rollData.finalLevelStr );
rollData.rollTarget = game.data.RdDUtility.getResolutionField( rollData.selectedCarac.value, rollData.finalLevel);
let armeTitle = ( rollData.arme ) ? " ("+rollData.arme.name+") " : ""; // If a weapon is there, add it in the title
let niveauStr = "";
if ( rollData.competence ) {
niveauStr = (rollData.competence.data.niveau >= 0) ? "+" + rollData.competence.data.niveau : rollData.competence.data.niveau;
$("#compdialogTitle").text( rollData.selectedCarac.label + "/" + rollData.competence.name + armeTitle + " " + niveauStr );
} else {
$("#compdialogTitle").text( rollData.selectedCarac.label );
}
$(".table-resolution").remove();
game.data.RdDUtility.makeHTMLResolutionTable( $("#resolutionTable"), rollData.selectedCarac.value-2, parseInt(rollData.selectedCarac.value) + 2, -10, 11,
rollData.selectedCarac.value, rollData.finalLevel );
}
// Setup everything onload
$(function() {
// Update html, according to data
if (rollData.competence) {
// Set the default carac from the competence item
rollData.selectedCarac = rollData.carac[rollData.competence.data.defaut_carac];
$("#carac").val( rollData.competence.data.defaut_carac );
}
$("#bonusmalus").val( rollData.bmValue );
updateRollResult(rollData);
});
// Update !
$('#bonusmalus').click((event) => {
rollData.bmValue = event.currentTarget.value; // Update the selected bonus/malus
//console.log("BM CLICKED !!!", rollData.bmValue, rollData.competence.data.niveau, parseInt(rollData.competence.data.niveau) + parseInt(rollData.bmValue) );
updateRollResult(rollData);
});
$('#carac').click((event) => {
let caracKey = event.currentTarget.value;
rollData.selectedCarac = rollData.carac[caracKey]; // Update the selectedCarac
//console.log("CARAC CLICKED !!!", rollData.selectedCarac, rollData.competence.data.niveau, rollData.bmValue);
updateRollResult(rollData);
});
}
}