Cleanup roll windows

- permettre plusieurs fenêtres de jets en même temps en éliminant les
  id dans le html et les jquery sur id pour éviter les interactions
- génération de la table par handlebars
This commit is contained in:
Vincent Vandemeulebrouck
2022-12-06 01:30:12 +01:00
parent f027e3318b
commit 2122a54db7
43 changed files with 319 additions and 343 deletions

View File

@ -17,23 +17,16 @@ export class RdDRoll extends Dialog {
/* -------------------------------------------- */
static async create(actor, rollData, dialogConfig, ...actions) {
if (actor.isRollWindowsOpened()) {
ui.notifications.warn("Vous avez déja une fenêtre de Test ouverte, il faut la fermer avant d'en ouvrir une autre.")
return;
}
actor.setRollWindowsOpened(true);
RdDRoll._ensureCorrectActions(actions);
RdDRoll._setDefaultOptions(actor, rollData);
const html = await renderTemplate(dialogConfig.html, rollData);
let options = { classes: ["rdddialog"], width: 600, height: 'fit-content', 'z-index': 99999 };
let options = { classes: [ "rdd-roll-dialog"], width: 600, height: 'fit-content', 'z-index': 99999 };
if (dialogConfig.options) {
mergeObject(options, dialogConfig.options, { overwrite: true })
}
return new RdDRoll(actor, rollData, html, options, actions, dialogConfig.close);
return new RdDRoll(actor, rollData, html, options, actions);
}
/* -------------------------------------------- */
@ -97,7 +90,6 @@ export class RdDRoll extends Dialog {
return facteurSign;
}
/* -------------------------------------------- */
static _ensureCorrectActions(actions) {
if (actions.length == 0) {
@ -111,13 +103,13 @@ export class RdDRoll extends Dialog {
}
/* -------------------------------------------- */
constructor(actor, rollData, html, options, actions, close = undefined) {
constructor(actor, rollData, html, options, actions) {
let conf = {
title: actions[0].label,
content: html,
buttons: {},
default: actions[0].name,
close: close
close: options.close
};
for (let action of actions) {
conf.buttons[action.name] = {
@ -136,7 +128,6 @@ export class RdDRoll extends Dialog {
close() {
if (this.rollData.canClose) {
this.actor.setRollWindowsOpened(false);
return super.close();
}
ui.notifications.info("Vous devez faire ce jet de dés!");
@ -145,10 +136,9 @@ export class RdDRoll extends Dialog {
/* -------------------------------------------- */
async onAction(action, html) {
this.rollData.forceDiceResult = Number.parseInt($('#force-dice-result').val()) ?? -1;
this.rollData.forceDiceResult = Number.parseInt(html.find("[name='force-dice-result']").val()) ?? -1;
await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRoll -=>", this.rollData, this.rollData.rolled);
this.actor.setRollWindowsOpened(false);
if (action.callbacks)
for (let callback of action.callbacks) {
if (callback.condition == undefined || callback.condition(this.rollData)) {
@ -173,90 +163,90 @@ export class RdDRoll extends Dialog {
const defaut_carac = rollData.competence.system.defaut_carac
// Set the default carac from the competence item
rollData.selectedCarac = rollData.carac[defaut_carac];
$("#carac").val(defaut_carac);
html.find("[name='carac']").val(defaut_carac);
}
if (rollData.selectedSort) {
dialog.setSelectedSort(rollData.selectedSort);
$(".draconic").val(rollData.selectedSort.system.listIndex); // Uniquement a la selection du sort, pour permettre de changer
dialog.setSelectedSort(rollData.selectedSort, html);
html.find(".draconic").val(rollData.selectedSort.system.listIndex); // Uniquement a la selection du sort, pour permettre de changer
}
RdDItemSort.setCoutReveReel(rollData.selectedSort);
$("#diffLibre").val(Misc.toInt(rollData.diffLibre));
$("#diffConditions").val(Misc.toInt(rollData.diffConditions));
dialog.updateRollResult();
html.find("[name='diffLibre']").val(Misc.toInt(rollData.diffLibre));
html.find("[name='diffConditions']").val(Misc.toInt(rollData.diffConditions));
dialog.updateRollResult(html);
}
// Setup everything onload
$(function () { onLoad(); });
// Update !
html.find('#diffLibre').change((event) => {
html.find("[name='diffLibre']").change((event) => {
this.rollData.diffLibre = Misc.toInt(event.currentTarget.value); // Update the selected bonus/malus
this.updateRollResult();
this.updateRollResult(html);
});
html.find('#diffConditions').change((event) => {
html.find("[name='diffConditions']").change((event) => {
this.rollData.diffConditions = Misc.toInt(event.currentTarget.value); // Update the selected bonus/malus
this.updateRollResult();
this.updateRollResult(html);
});
html.find('#force-dice-result').change((event) => {
html.find("[name='force-dice-result']").change((event) => {
this.rollData.forceDiceResult = Misc.toInt(event.currentTarget.value);
});
html.find('#carac').change((event) => {
html.find("[name='carac']").change((event) => {
let caracKey = event.currentTarget.value;
this.rollData.selectedCarac = this.rollData.carac[caracKey]; // Update the selectedCarac
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.roll-draconic').change((event) => {
let draconicKey = Misc.toInt(event.currentTarget.value);
this.rollData.competence = this.rollData.draconicList[draconicKey]; // Update the selectedCarac
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.roll-sort').change((event) => {
let sortKey = Misc.toInt(event.currentTarget.value);
this.setSelectedSort(this.rollData.sortList[sortKey]);
this.updateRollResult();
$("#diffLibre").val(this.rollData.diffLibre);
this.setSelectedSort(this.rollData.sortList[sortKey], html);
this.updateRollResult(html);
html.find("[name='diffLibre']").val(this.rollData.diffLibre);
});
html.find('.roll-carac-competence').change((event) => {
const competence = event.currentTarget.value;
this.rollData.competence = this.rollData.competences.find(it => it.name == competence);
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.roll-signedraconique').change((event) => {
let sortKey = Misc.toInt(event.currentTarget.value);
this.setSelectedSigneDraconique(this.rollData.signes[sortKey]);
this.updateRollResult();
this.updateRollResult(html);
});
html.find('#ptreve-variable').change((event) => {
html.find("[name='ptreve-variable']").change((event) => {
let ptreve = Misc.toInt(event.currentTarget.value);
this.rollData.selectedSort.system.ptreve_reel = ptreve;
console.log("RdDRollSelectDialog - Cout reve", ptreve);
this.updateRollResult();
this.updateRollResult(html);
});
html.find("[name='coupsNonMortels']").change((event) => {
this.rollData.dmg.mortalite = event.currentTarget.checked ? "non-mortel" : "mortel";
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.cuisine-proportions').change((event) => {
this.rollData.proportions = Number(event.currentTarget.value);
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.select-by-name').change((event) => {
const attribute = event.currentTarget.attributes['name'].value;
this.rollData[attribute] = event.currentTarget.value;
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.checkbox-by-name').change((event) => {
const attribute = event.currentTarget.attributes['name'].value;
this.rollData[attribute] = event.currentTarget.checked;
this.updateRollResult();
this.updateRollResult(html);
});
html.find('input.use-encTotal').change((event) => {
this.rollData.use.encTotal = event.currentTarget.checked;
this.updateRollResult();
this.updateRollResult(html);
});
html.find('input.use-surenc').change((event) => {
this.rollData.use.surenc = event.currentTarget.checked;
this.updateRollResult();
this.updateRollResult(html);
});
html.find('.appel-moral').click((event) => { /* l'appel au moral, qui donne un bonus de +1 */
this.rollData.use.moral = !this.rollData.use.moral;
@ -274,38 +264,38 @@ export class RdDRoll extends Dialog {
tooltip.innerHTML = "Sans appel au moral";
appelMoral.src = "/systems/foundryvtt-reve-de-dragon/icons/moral-neutre.svg";
}
this.updateRollResult();
this.updateRollResult(html);
});
// Section Méditation
html.find('.conditionMeditation').change((event) => {
let condition = event.currentTarget.attributes['id'].value;
let condition = event.currentTarget.attributes['name'].value;
this.rollData.conditionMeditation[condition] = event.currentTarget.checked;
this.updateRollResult();
this.updateRollResult(html);
});
}
async setSelectedSort(sort) {
async setSelectedSort(sort, html) {
this.rollData.selectedSort = sort; // Update the selectedCarac
this.rollData.competence = RdDItemCompetence.getVoieDraconic(this.rollData.draconicList, sort.system.draconic);
this.rollData.bonus = RdDItemSort.getCaseBonus(sort, this.rollData.tmr.coord);
this.rollData.diffLibre = RdDItemSort.getDifficulte(sort, -7);
RdDItemSort.setCoutReveReel(sort);
const htmlSortDescription = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/partial-description-sort.html", { sort: sort });
$(".sort-ou-rituel").text(sort.system.isrituel ? "rituel" : "sort");
$(".bonus-case").text(`${this.rollData.bonus}%`);
$(".details-sort").remove();
$(".description-sort").append(htmlSortDescription);
$(".roll-draconic").val(sort.system.listIndex);
$(".div-sort-difficulte-fixe").text(Misc.toSignedString(sort.system.difficulte));
$(".div-sort-ptreve-fixe").text(sort.system.ptreve);
html.find(".sort-ou-rituel").text(sort.system.isrituel ? "rituel" : "sort");
html.find(".bonus-case").text(`${this.rollData.bonus}%`);
html.find(".details-sort").remove();
html.find(".description-sort").append(htmlSortDescription);
html.find(".roll-draconic").val(sort.system.listIndex);
html.find(".div-sort-difficulte-fixe").text(Misc.toSignedString(sort.system.difficulte));
html.find(".div-sort-ptreve-fixe").text(sort.system.ptreve);
const diffVariable = RdDItemSort.isDifficulteVariable(sort);
const coutVariable = RdDItemSort.isCoutVariable(sort);
HtmlUtility._showControlWhen($(".div-sort-non-rituel"), !sort.system.isrituel);
HtmlUtility._showControlWhen($(".div-sort-difficulte-var"), diffVariable);
HtmlUtility._showControlWhen($(".div-sort-difficulte-fixe"), !diffVariable);
HtmlUtility._showControlWhen($(".div-sort-ptreve-var"), coutVariable);
HtmlUtility._showControlWhen($(".div-sort-ptreve-fixe"), !coutVariable);
HtmlUtility._showControlWhen(html.find(".div-sort-non-rituel"), !sort.system.isrituel);
HtmlUtility._showControlWhen(html.find(".div-sort-difficulte-var"), diffVariable);
HtmlUtility._showControlWhen(html.find(".div-sort-difficulte-fixe"), !diffVariable);
HtmlUtility._showControlWhen(html.find(".div-sort-ptreve-var"), coutVariable);
HtmlUtility._showControlWhen(html.find(".div-sort-ptreve-fixe"), !coutVariable);
}
async setSelectedSigneDraconique(signe){
@ -315,7 +305,7 @@ export class RdDRoll extends Dialog {
}
/* -------------------------------------------- */
async updateRollResult() {
async updateRollResult(html) {
let rollData = this.rollData;
rollData.dmg = rollData.attackerRoll?.dmg ?? RdDBonus.dmg(rollData, this.actor.getBonusDegat())
@ -333,31 +323,27 @@ export class RdDRoll extends Dialog {
RollDataAjustements.calcul(rollData, this.actor);
rollData.finalLevel = this._computeFinalLevel(rollData);
const resolutionTable = await RdDResolutionTable.buildHTMLTable(RdDResolutionTable.subTable(rollData.caracValue, rollData.finalLevel))
const adjustements = await this.buildAjustements(rollData);
HtmlUtility._showControlWhen($(".use-encTotal"), rollData.ajustements.encTotal.visible && RdDCarac.isAgiliteOuDerivee(rollData.selectedCarac));
HtmlUtility._showControlWhen($(".use-surenc"), rollData.ajustements.surenc.visible && RdDCarac.isActionPhysique(rollData.selectedCarac));
HtmlUtility._showControlWhen($(".utilisation-moral"), rollData.use.appelAuMoral);
HtmlUtility._showControlWhen($(".diffMoral"), rollData.ajustements.moralTotal.used);
HtmlUtility._showControlWhen($(".divAppelAuMoral"), rollData.use.appelAuMoral);
HtmlUtility._showControlWhen($("#etat-general"), !RdDCarac.isIgnoreEtatGeneral(rollData));
HtmlUtility._showControlWhen($("#ajust-astrologique"), RdDResolutionTable.isAjustementAstrologique(rollData));
// Mise à jour valeurs
$(".dialog-roll-title").text(this._getTitle(rollData));
$("[name='coupsNonMortels']").prop('checked', rollData.mortalite == 'non-mortel');
$(".dmg-arme-actor").text(dmgText);
$('.table-ajustement').remove();
$(".table-resolution").remove();
$(".table-proba-reussite").remove();
$("#tableAjustements").append(await this.buildAjustements(rollData));
$("#tableResolution").append(RdDResolutionTable.buildHTMLTableExtract(rollData.caracValue, rollData.finalLevel));
$("#tableProbaReussite").append(RdDResolutionTable.buildHTMLResults(rollData.caracValue, rollData.finalLevel));
html.find(".dialog-roll-title").text(this._getTitle(rollData));
html.find("[name='coupsNonMortels']").prop('checked', rollData.mortalite == 'non-mortel');
html.find(".dmg-arme-actor").text(dmgText);
html.find("div.placeholder-ajustements").empty().append(adjustements);
html.find("div.placeholder-resolution").empty().append(resolutionTable)
}
/* -------------------------------------------- */
async buildAjustements(rollData) {
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/partial-roll-ajustements.html`, rollData);
return html;
return await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/partial-roll-ajustements.html`, rollData);
}
/* -------------------------------------------- */
@ -375,23 +361,11 @@ export class RdDRoll extends Dialog {
return 0;
}
/* -------------------------------------------- */
_computeDiffLibre(rollData) {
let diffLibre = Misc.toInt(rollData.diffLibre);
if (rollData.draconicList && rollData.selectedSort) {
return RdDItemSort.getDifficulte(rollData.selectedSort, diffLibre);
}
return diffLibre;
}
/* -------------------------------------------- */
_computeMalusArmure(rollData) {
let malusArmureValue = 0;
if (rollData.malusArmureValue && (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("");
}
return malusArmureValue;
}