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

142 lines
5.5 KiB
JavaScript
Raw Normal View History

import { RdDResolutionTable } from "./rdd-resolution-table.js";
import { RdDUtility } from "./rdd-utility.js";
2020-06-03 21:35:18 +02:00
/**
* Extend the base Dialog entity by defining a custom window to perform roll.
* @extends {Dialog}
*/
export class RdDRollDialog extends Dialog {
/* -------------------------------------------- */
2020-06-03 21:35:18 +02:00
constructor(mode, html, rollData, actor) {
let myButtons
2020-07-26 17:26:17 +02:00
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) }
}
2020-07-26 17:26:17 +02:00
}
else {
myButtons = {
rollButton: { label: "Lancer", callback: html => this.actor.performRoll(this.rollData) }
};
2020-07-26 17:26:17 +02:00
}
// Common conf
let dialogConf = { content: html, title: "Test", buttons: myButtons, default: "rollButton" }
let dialogOptions = { classes: ["rdddialog"], width: 600, height: 400 }
2020-06-03 21:35:18 +02:00
// Select proper roll dialog template and stuff
if (mode == "competence") {
dialogConf.title = "Test de compétence"
dialogConf.height = 400
2020-06-07 23:16:29 +02:00
} else if (mode == "arme") {
dialogConf.title = "Test de combat/arme"
dialogConf.height = 430
2020-06-12 22:46:04 +02:00
} else if (mode == "carac") {
dialogConf.title = "Test de caractéristique"
dialogOptions.height = 350
2020-07-23 22:09:40 +02:00
} else if (mode == "sort") {
dialogConf.title = "Lancer un sort"
dialogConf.height = 450
2020-06-07 23:16:29 +02:00
}
super(dialogConf, dialogOptions)
2020-06-03 21:35:18 +02:00
this.mode = mode
this.rollData = rollData
this.actor = actor
}
2020-06-03 21:35:18 +02:00
/* -------------------------------------------- */
performRollSort(html, isReserve = false) {
2020-07-26 17:26:17 +02:00
this.rollData.isSortReserve = isReserve;
this.actor.performRoll(this.rollData);
2020-06-03 21:35:18 +02:00
}
/* -------------------------------------------- */
activateListeners(html) {
2020-06-03 21:35:18 +02:00
super.activateListeners(html);
2020-06-03 21:35:18 +02:00
// 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 = RdDUtility.getResolutionField(rollData.selectedCarac.value, rollData.finalLevel);
$("#roll-param").text(rollData.selectedCarac.value + " / " + rollData.finalLevelStr);
$("#compdialogTitle").text(RdDRollDialog._getTitle(rollData));
2020-06-03 21:35:18 +02:00
$(".table-resolution").remove();
$("#resolutionTable").append(RdDResolutionTable.buildHTMLTable(caracValue, rollLevel, caracValue - 2, caracValue + 2));
2020-06-03 21:35:18 +02:00
}
2020-06-03 21:35:18 +02:00
// Setup everything onload
$(function () {
2020-06-03 21:35:18 +02:00
// Update html, according to data
2020-06-12 22:46:04 +02:00
if (rollData.competence) {
// Set the default carac from the competence item
2020-09-20 19:55:10 +02:00
console.log(rollData.competence.data.defaut_carac, rollData.carac);
2020-06-12 22:46:04 +02:00
rollData.selectedCarac = rollData.carac[rollData.competence.data.defaut_carac];
$("#carac").val(rollData.competence.data.defaut_carac);
2020-06-12 22:46:04 +02:00
}
$("#bonusmalus").val(rollData.bmValue);
2020-06-03 21:35:18 +02:00
updateRollResult(rollData);
});
2020-06-03 21:35:18 +02:00
// Update !
html.find('#bonusmalus').click((event) => {
2020-06-03 21:35:18 +02:00
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);
});
html.find('#carac').click((event) => {
let caracKey = event.currentTarget.value;
2020-06-03 21:35:18 +02:00
rollData.selectedCarac = rollData.carac[caracKey]; // Update the selectedCarac
//console.log("CARAC CLICKED !!!", rollData.selectedCarac, rollData.competence.data.niveau, rollData.bmValue);
updateRollResult(rollData);
});
html.find('#draconic').click((event) => {
let draconicKey = event.currentTarget.value;
2020-07-23 22:09:40 +02:00
rollData.selectedDraconic = rollData.draconicList[draconicKey]; // Update the selectedCarac
//console.log("CARAC CLICKED !!!", rollData.selectedCarac, rollData.competence.data.niveau, rollData.bmValue);
updateRollResult(rollData);
});
html.find('#sort').click((event) => {
let sortKey = event.currentTarget.value;
2020-07-23 22:09:40 +02:00
rollData.selectedSort = rollData.sortList[sortKey]; // Update the selectedCarac
//console.log("CARAC CLICKED !!!", rollData.selectedCarac, rollData.competence.data.niveau, rollData.bmValue);
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) {
return etat + parseInt(rollData.selectedDraconic.data.niveau) + parseInt(rollData.selectedSort.data.difficulte);
}
return etat + parseInt(rollData.bmValue);
2020-06-03 21:35:18 +02:00
}
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;
}
2020-06-03 21:35:18 +02:00
}