2020-12-06 19:29:10 +01:00
|
|
|
import { HtmlUtility } from "./html-utility.js";
|
2020-12-20 21:54:09 +01:00
|
|
|
import { RdDItemCompetence } from "./item-competence.js";
|
2020-12-06 19:29:10 +01:00
|
|
|
import { RdDItemSort } from "./item-sort.js";
|
|
|
|
import { Misc } from "./misc.js";
|
2020-12-16 23:02:15 +01:00
|
|
|
import { RdDBonus } from "./rdd-bonus.js";
|
2020-12-06 19:29:10 +01:00
|
|
|
import { RdDResolutionTable } from "./rdd-resolution-table.js";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend the base Dialog entity to select roll parameters
|
|
|
|
* @extends {Dialog}
|
|
|
|
*/
|
2020-12-16 23:02:15 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-06 19:29:10 +01:00
|
|
|
export class RdDRoll extends Dialog {
|
|
|
|
|
2020-12-16 23:02:15 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-06 23:31:23 +01:00
|
|
|
static async create(actor, rollData, dialogConfig, ...actions) {
|
2020-12-06 19:29:10 +01:00
|
|
|
|
2020-12-06 23:31:23 +01:00
|
|
|
RdDRoll._ensureCorrectActions(actions);
|
2020-12-06 19:29:10 +01:00
|
|
|
RdDRoll._setDefaultOptions(actor, rollData);
|
|
|
|
|
2020-12-06 23:31:23 +01:00
|
|
|
const html = await renderTemplate(dialogConfig.html, rollData);
|
2020-12-06 19:29:10 +01:00
|
|
|
|
2020-12-06 23:31:23 +01:00
|
|
|
let options = { classes: ["rdddialog"], width: 600, height: 500, 'z-index': 99999 };
|
|
|
|
if (dialogConfig.options) {
|
|
|
|
mergeObject(options, dialogConfig.options, { overwrite: true })
|
|
|
|
}
|
2020-12-28 10:11:47 +01:00
|
|
|
return new RdDRoll(actor, rollData, html, options, actions, dialogConfig.close );
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 23:02:15 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-06 19:29:10 +01:00
|
|
|
static _setDefaultOptions(actor, rollData) {
|
2020-12-06 23:31:23 +01:00
|
|
|
|
2020-12-15 08:37:52 +01:00
|
|
|
let defaultRollData = {
|
|
|
|
ajustementsConditions: CONFIG.RDD.ajustementsConditions,
|
|
|
|
difficultesLibres: CONFIG.RDD.difficultesLibres,
|
|
|
|
etat: actor.data.data.compteurs.etat.value,
|
2020-12-18 23:57:28 +01:00
|
|
|
moral: actor.isPersonnage() ? actor.data.data.compteurs.moral.value : 0,
|
2020-12-15 08:37:52 +01:00
|
|
|
carac: actor.data.data.carac,
|
|
|
|
finalLevel: 0,
|
2020-12-18 01:23:11 +01:00
|
|
|
diffConditions: rollData.arme ? RdDBonus.bonusAttaque(rollData.surpriseDefenseur) :0,
|
2020-12-15 08:37:52 +01:00
|
|
|
diffLibre: 0,
|
|
|
|
editLibre: true,
|
|
|
|
editConditions: true,
|
2020-12-16 23:02:15 +01:00
|
|
|
forceValue: actor.getForceValue(),
|
2020-12-18 23:57:28 +01:00
|
|
|
malusArmureValue: (actor.isPersonnage() && actor.data.data.attributs && actor.data.data.attributs.malusarmure) ? actor.data.data.attributs.malusarmure.value : 0,
|
|
|
|
surencMalusFlag: actor.isPersonnage() ? (actor.data.data.compteurs.surenc.value < 0) : false,
|
|
|
|
surencMalusValue: actor.isPersonnage() ? actor.data.data.compteurs.surenc.value : 0,
|
2020-12-15 08:37:52 +01:00
|
|
|
surencMalusApply: false,
|
2020-12-20 21:54:09 +01:00
|
|
|
isMalusEncombrementTotal: RdDItemCompetence.isMalusEncombrementTotal(rollData.competence),
|
|
|
|
useMalusEncTotal: false,
|
|
|
|
encTotal: actor.getEncombrementTotal(),
|
2020-12-15 08:37:52 +01:00
|
|
|
ajustementAstrologique: actor.ajustementAstrologique()
|
|
|
|
}
|
2020-12-16 23:02:15 +01:00
|
|
|
mergeObject(rollData, defaultRollData, { overwrite: false });
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 23:02:15 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-06 19:29:10 +01:00
|
|
|
static _ensureCorrectActions(actions) {
|
2020-12-06 23:31:23 +01:00
|
|
|
if (actions.length == 0) {
|
2020-12-06 19:29:10 +01:00
|
|
|
throw 'No action defined';
|
|
|
|
}
|
|
|
|
actions.forEach(action => {
|
|
|
|
if (action.callbacks == undefined) {
|
2020-12-06 23:31:23 +01:00
|
|
|
action.callbacks = [{ action: r => console.log(action.name, r) }];
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-16 23:02:15 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-28 10:11:47 +01:00
|
|
|
constructor(actor, rollData, html, options, actions, close = undefined) {
|
2020-12-06 19:29:10 +01:00
|
|
|
let conf = {
|
|
|
|
title: actions[0].label,
|
|
|
|
content: html,
|
|
|
|
buttons: {},
|
2020-12-28 10:11:47 +01:00
|
|
|
default: actions[0].name,
|
|
|
|
close: close
|
2020-12-06 19:29:10 +01:00
|
|
|
};
|
|
|
|
for (let action of actions) {
|
|
|
|
conf.buttons[action.name] = { label: action.label, callback: html => this.onAction(action, html) };
|
|
|
|
}
|
|
|
|
|
|
|
|
super(conf, options);
|
|
|
|
|
|
|
|
this.actor = actor;
|
|
|
|
this.rollData = rollData;
|
|
|
|
}
|
|
|
|
|
2020-12-16 23:02:15 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-06 19:29:10 +01:00
|
|
|
async onAction(action, html) {
|
|
|
|
await RdDResolutionTable.rollData(this.rollData);
|
2020-12-08 23:07:41 +01:00
|
|
|
console.log("RdDRoll -=>", this.rollData, this.rollData.rolled);
|
|
|
|
|
2020-12-06 19:29:10 +01:00
|
|
|
if (action.callbacks)
|
2020-12-06 23:31:23 +01:00
|
|
|
for (let callback of action.callbacks) {
|
|
|
|
if (callback.condition == undefined || callback.condition(this.rollData)) {
|
|
|
|
callback.action(this.rollData);
|
|
|
|
}
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
|
|
|
|
|
|
|
this.bringToTop();
|
|
|
|
|
|
|
|
var rollData = this.rollData;
|
2020-12-16 23:02:15 +01:00
|
|
|
var actor = this.actor;
|
2020-12-18 23:57:28 +01:00
|
|
|
var dialog = this;
|
2020-12-06 19:29:10 +01:00
|
|
|
|
|
|
|
function updateRollResult(rollData) {
|
|
|
|
let caracValue = parseInt(rollData.selectedCarac.value)
|
2020-12-18 23:57:28 +01:00
|
|
|
let rollLevel = dialog._computeFinalLevel(rollData);
|
2020-12-16 23:02:15 +01:00
|
|
|
rollData.dmg = rollData.attackerRoll ? rollData.attackerRoll.dmg : RdDBonus.dmg(rollData, actor.getBonusDegat());
|
2020-12-06 19:29:10 +01:00
|
|
|
rollData.finalLevel = rollLevel;
|
2020-12-16 23:02:15 +01:00
|
|
|
rollData.caracValue = caracValue;
|
|
|
|
rollData.coupsNonMortels = (rollData.attackerRoll ? rollData.attackerRoll.dmg.mortalite : rollData.dmg.mortalite) == 'non-mortel';
|
|
|
|
let dmgText = Misc.toSignedString(rollData.dmg.total);
|
|
|
|
if (rollData.coupsNonMortels) {
|
|
|
|
dmgText = '(' + dmgText + ')';
|
|
|
|
}
|
2020-12-06 19:29:10 +01:00
|
|
|
|
2020-12-20 02:05:47 +01:00
|
|
|
HtmlUtility._showControlWhen($(".diffMoral"), rollData.selectedCarac == actor.data.data.carac.volonte);
|
|
|
|
HtmlUtility._showControlWhen($(".etat-general"), !dialog._isIgnoreEtatGeneral(rollData));
|
2020-12-06 19:29:10 +01:00
|
|
|
|
|
|
|
// Sort management
|
|
|
|
if (rollData.selectedSort) {
|
2020-12-16 23:02:15 +01:00
|
|
|
rollData.bonus = RdDItemSort.getCaseBonus(rollData.selectedSort, rollData.coord),
|
2020-12-06 19:29:10 +01:00
|
|
|
//console.log("Toggle show/hide", rollData.selectedSort);
|
2020-12-20 02:05:47 +01:00
|
|
|
HtmlUtility._showControlWhen($("#div-sort-difficulte"), RdDItemSort.isDifficulteVariable(rollData.selectedSort))
|
|
|
|
HtmlUtility._showControlWhen($("#div-sort-ptreve"), RdDItemSort.isCoutVariable(rollData.selectedSort))
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mise à jour valeurs
|
|
|
|
$("#roll-param").text(rollData.selectedCarac.value + " / " + Misc.toSignedString(rollData.finalLevel));
|
2020-12-18 23:57:28 +01:00
|
|
|
$("#compdialogTitle").text(dialog._getTitle(rollData));
|
2020-12-16 23:02:15 +01:00
|
|
|
$('#coupsNonMortels').prop('checked', rollData.coupsNonMortels);
|
|
|
|
$("#dmg-arme-actor").text(dmgText);
|
|
|
|
$("#defenseur-surprise").text(RdDBonus.description(rollData.surpriseDefenseur));
|
2020-12-06 19:29:10 +01:00
|
|
|
$(".table-resolution").remove();
|
|
|
|
$("#resolutionTable").append(RdDResolutionTable.buildHTMLTableExtract(caracValue, rollLevel));
|
2020-12-15 23:54:09 +01:00
|
|
|
$(".span-valeur").remove();
|
|
|
|
$("#resolutionValeurs").append(RdDResolutionTable.buildHTMLResults(caracValue, rollLevel));
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
RdDItemSort.setCoutReveReel(rollData.selectedSort);
|
|
|
|
$("#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); // Update the selected bonus/malus
|
|
|
|
//console.log("RdDRollSelectDialog","BM CLICKED !!!", rollData);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#diffConditions').change((event) => {
|
|
|
|
rollData.diffConditions = Misc.toInt(event.currentTarget.value); // Update the selected bonus/malus
|
|
|
|
//console.log("RdDRollSelectDialog","BM CLICKED !!!", rollData);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#carac').change((event) => {
|
|
|
|
let caracKey = event.currentTarget.value;
|
|
|
|
this.rollData.selectedCarac = rollData.carac[caracKey]; // Update the selectedCarac
|
|
|
|
//console.log("RdDRollSelectDialog","CARAC CLICKED !!!", rollData);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#draconic').change((event) => {
|
|
|
|
let draconicKey = Misc.toInt(event.currentTarget.value);
|
|
|
|
this.rollData.selectedDraconic = rollData.draconicList[draconicKey]; // Update the selectedCarac
|
|
|
|
//console.log("RdDRollSelectDialog","CARAC CLICKED !!!", rollData);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#sort').change((event) => {
|
|
|
|
let sortKey = Misc.toInt(event.currentTarget.value);
|
|
|
|
this.rollData.selectedSort = rollData.sortList[sortKey]; // Update the selectedCarac
|
2020-12-16 23:02:15 +01:00
|
|
|
this.rollData.bonus = RdDItemSort.getCaseBonus(rollData.selectedSort, rollData.coord);
|
2020-12-06 19:29:10 +01:00
|
|
|
RdDItemSort.setCoutReveReel(rollData.selectedSort);
|
|
|
|
//console.log("RdDRollSelectDialog - Sort selection", rollData.selectedSort);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#ptreve-variable').change((event) => {
|
|
|
|
let ptreve = Misc.toInt(event.currentTarget.value);
|
|
|
|
this.rollData.selectedSort.data.ptreve_reel = ptreve;
|
|
|
|
console.log("RdDRollSelectDialog - Cout reve", ptreve);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#ptreve-variable').change((event) => {
|
|
|
|
let ptreve = Misc.toInt(event.currentTarget.value);
|
|
|
|
this.rollData.selectedSort.data.ptreve_reel = ptreve; // Update the selectedCarac
|
|
|
|
console.log("RdDRollSelectDialog - Cout reve", ptreve);
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
html.find('#coupsNonMortels').change((event) => {
|
2020-12-16 23:02:15 +01:00
|
|
|
this.rollData.dmg.mortalite = event.currentTarget.checked ? "non-mortel" : "mortel";
|
|
|
|
updateRollResult(rollData);
|
2020-12-12 21:58:44 +01:00
|
|
|
});
|
2020-12-16 23:02:15 +01:00
|
|
|
html.find('#tactique-combat').change((event) => {
|
|
|
|
this.rollData.tactique = event.currentTarget.value;
|
|
|
|
updateRollResult(rollData);
|
2020-12-06 19:29:10 +01:00
|
|
|
});
|
|
|
|
html.find('#surencMalusApply').change((event) => {
|
|
|
|
this.rollData.surencMalusApply = event.currentTarget.checked;
|
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
2020-12-20 21:54:09 +01:00
|
|
|
html.find('#useMalusEncTotal').change((event) => {
|
|
|
|
this.rollData.useMalusEncTotal = event.currentTarget.checked;
|
2020-12-06 19:29:10 +01:00
|
|
|
updateRollResult(rollData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:57:28 +01:00
|
|
|
_isIgnoreEtatGeneral(rollData) {
|
2020-12-06 19:29:10 +01:00
|
|
|
return rollData.selectedCarac.ignoreEtatGeneral;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2020-12-18 23:57:28 +01:00
|
|
|
_computeFinalLevel(rollData) {
|
|
|
|
const etat = this._isIgnoreEtatGeneral(rollData) ? 0 : Misc.toInt(rollData.etat);
|
2020-12-06 19:29:10 +01:00
|
|
|
const diffConditions = Misc.toInt(rollData.diffConditions);
|
2020-12-16 23:02:15 +01:00
|
|
|
const malusEnc = (rollData.surencMalusApply) ? rollData.surencMalusValue : 0;
|
|
|
|
const bonusTactique = RdDBonus.bonusAttaque(rollData.tactique);
|
2020-12-20 21:54:09 +01:00
|
|
|
const malusEncTotal = (rollData.useMalusEncTotal) ? -rollData.encTotal : 0;
|
2020-12-16 23:02:15 +01:00
|
|
|
const ajustementChance = rollData.selectedCarac.label.toLowerCase().includes('chance') ? rollData.ajustementAstrologique : 0;
|
2020-12-06 19:29:10 +01:00
|
|
|
// Gestion malus armure
|
2020-12-18 23:57:28 +01:00
|
|
|
const malusArmureValue = this._computeMalusArmure(rollData);
|
2020-12-16 23:02:15 +01:00
|
|
|
|
2020-12-18 23:57:28 +01:00
|
|
|
const diffLibre = this._computeDiffLibre(rollData);
|
|
|
|
const diffCompetence = this._computeDiffCompetence(rollData);
|
|
|
|
const diffMoral = rollData.selectedCarac == this.actor.data.data.carac.volonte ? rollData.moral : 0;
|
2020-12-16 23:02:15 +01:00
|
|
|
|
2020-12-20 21:54:09 +01:00
|
|
|
return etat + diffCompetence + diffLibre + diffMoral + diffConditions + malusEnc + malusEncTotal + malusArmureValue + ajustementChance + bonusTactique;
|
2020-12-16 23:02:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:57:28 +01:00
|
|
|
_computeDiffCompetence(rollData) {
|
2020-12-16 23:02:15 +01:00
|
|
|
if (rollData.competence) {
|
|
|
|
return Misc.toInt(rollData.competence.data.niveau);
|
|
|
|
}
|
|
|
|
if (rollData.draconicList) {
|
|
|
|
return Misc.toInt(rollData.selectedDraconic.data.niveau);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:57:28 +01:00
|
|
|
_computeDiffLibre(rollData) {
|
2020-12-16 23:02:15 +01:00
|
|
|
let diffLibre = Misc.toInt(rollData.diffLibre);
|
|
|
|
if (rollData.draconicList && rollData.selectedSort) {
|
|
|
|
return RdDItemSort.getDifficulte(rollData.selectedSort, diffLibre);
|
|
|
|
}
|
|
|
|
return diffLibre;
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:57:28 +01:00
|
|
|
_computeMalusArmure(rollData) {
|
2020-12-06 19:29:10 +01:00
|
|
|
let malusArmureValue = 0;
|
|
|
|
if (rollData.malusArmureValue != 0 && (rollData.selectedCarac.label == "Agilité" || rollData.selectedCarac.label == "Dérobée")) {
|
|
|
|
$("#addon-message").text("Malus armure appliqué : " + rollData.malusArmureValue);
|
|
|
|
malusArmureValue = rollData.malusArmureValue;
|
|
|
|
} else {
|
|
|
|
$("#addon-message").text("");
|
|
|
|
}
|
2020-12-16 23:02:15 +01:00
|
|
|
return malusArmureValue;
|
2020-12-06 19:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2020-12-18 23:57:28 +01:00
|
|
|
_getTitle(rollData) {
|
2020-12-06 19:29:10 +01:00
|
|
|
if (rollData.competence) {
|
|
|
|
// If a weapon is there, add it in the title
|
|
|
|
let armeTitle = (rollData.arme) ? " (" + rollData.arme.name + ") " : "";
|
|
|
|
let niveau = Misc.toSignedString(rollData.competence.data.niveau);
|
|
|
|
return rollData.selectedCarac.label + "/" + rollData.competence.name + armeTitle + " " + niveau
|
|
|
|
}
|
|
|
|
if (rollData.draconicList) {
|
|
|
|
return rollData.selectedDraconic.name + " - " + rollData.selectedSort.name;
|
|
|
|
}
|
|
|
|
return rollData.selectedCarac.label;
|
|
|
|
}
|
|
|
|
}
|