From f1fbf3549a45980a94232492efb9d750d47ebb22 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Tue, 8 Dec 2020 23:07:41 +0100 Subject: [PATCH] Lancer un sort avec RdDRoll --- module/actor.js | 228 +++++++++++++++++---------------- module/rdd-resolution-table.js | 4 +- module/rdd-roll.js | 5 + module/rdd-tmr-dialog.js | 2 +- 4 files changed, 128 insertions(+), 111 deletions(-) diff --git a/module/actor.js b/module/actor.js index f2293cd0..48d084b9 100644 --- a/module/actor.js +++ b/module/actor.js @@ -91,7 +91,6 @@ export class RdDActor extends Actor { if (actorData.type === 'personnage') this._prepareCharacterData(actorData); if (actorData.type === 'creature') this.computeEtatGeneral(actorData); if (actorData.type === 'humanoide') this.computeEtatGeneral(actorData); - } /* -------------------------------------------- */ @@ -142,14 +141,8 @@ export class RdDActor extends Actor { /* -------------------------------------------- */ async performRoll(rollData, attacker = undefined) { - - // Cas des bonus de cases pour les sorts - let sortBonus = 0; - if (rollData.selectedSort) { - sortBonus = RdDItemSort.getCaseBonus( rollData.selectedSort, rollData.coord ); - } // garder le résultat - rollData.rolled = await RdDResolutionTable.roll(rollData.caracValue, rollData.finalLevel, sortBonus); + rollData.rolled = await RdDResolutionTable.rollData(rollData); //console.log("performRoll", rollData) if ( !rollData.attackerRoll) {// Store in the registry if not a defense roll @@ -303,12 +296,6 @@ export class RdDActor extends Actor { } } - // Sort management - if (rollData.selectedSort) { // Lancement de sort ! - resumeCompetence = rollData.selectedDraconic.name + "/" + rollData.selectedSort.name; - explications = await this._rollLancementDeSort(rollData, rolled); - } - // Save it for fight in the flags area game.system.rdd.rollDataHandler[this.data._id] = duplicate(rollData); @@ -370,68 +357,6 @@ export class RdDActor extends Actor { return dmgArme + dmgPerso; } - /* -------------------------------------------- */ - async _rollLancementDeSort(rollData, rolled) { - - let sort = duplicate(rollData.selectedSort); - - let closeTMR = true; - let coutReve = sort.data.ptreve_reel; // toujours positionné par cas de sort à ptreve variables - - let explications = "
Lancement du sort " + sort.name + " : " + Misc.upperFirst(sort.data.draconic) - + " pour " + coutReve + " points de Rêve" - + "
Depuis la case " + rollData.coord + " (" + TMRUtility.getTMRDescription(rollData.coord).label + ")"; - - let myReve = duplicate(this.data.data.reve.reve); - if (rolled.isSuccess) { // Réussite du sort ! - sort.ptreve_reel = coutReve; - if (rolled.isPart) { - coutReve = Math.max(Math.ceil(coutReve / 2), 1); - } - // Incrémenter/gére le bonus de case - RdDItemSort.incrementBonusCase(this, sort, rollData.coord); - - if (myReve.value > coutReve){ - explications += "
Réussite du sort: " + coutReve + " points de Rêve sont dépensés (Bonus de case : +" + rolled.bonus + "%)"; - - if (rollData.isSortReserve) { - // Mise en réserve - myReve.value--; - await this.sortMisEnReserve(rollData, sort); - closeTMR = false; - } - } - else { - // Todo 0 pts de reve !!!! - explications += "
Pas assez de rêve"; - mergeObject(rollData, RdDResolutionTable.getResultat("echec")); - } - } else { - if (rolled.isETotal) { // Echec total ! - coutReve *= 2; - myReve.value = myReve.value - coutReve; - explications += "
Echec TOTAL du sort : " + coutReve + " Points de Rêve"; - } else { - coutReve = 0 - explications += "
Echec du sort !"; - } - } - - myReve.value = Math.max(myReve.value - coutReve, 0); - await this.update({ "data.reve.reve": myReve }); - - if (myReve.value == 0) { // 0 points de reve - ChatMessage.create({ content: this.name + " est réduit à 0 Points de Rêve, et tombe endormi !" }); - closeTMR = true; - } - if (closeTMR) { - this.currentTMR.close(); // Close TMR ! - } else { - this.currentTMR.maximize(); // Re-display TMR - } - return explications - } - /* -------------------------------------------- */ async dormirChateauDormant() { let message = { @@ -1324,38 +1249,6 @@ export class RdDActor extends Actor { } } - /* -------------------------------------------- */ - async rollUnSort(coord) { - let draconicList = this.getDraconicList(); - let sortList = duplicate(this.getSortList()); // Duplication car les pts de reve sont modifiés dans le sort - - let rollData = { - selectedCarac: this.data.data.carac.reve, - etat: this.data.data.compteurs.etat.value, - draconicList: draconicList, - sortList: sortList, - selectedDraconic: this.getBestDraconic(), - selectedSort: sortList[0], - coord: coord, - coordLabel: TMRUtility.getTMRDescription( coord).label, - finalLevel: 0, - diffConditions: 0, - diffLibre: sortList[0].data.difficulte, // Per default at startup - coutreve: Array(20).fill().map((item, index) => 1 + index), - ajustementsConditions: CONFIG.RDD.ajustementsConditions, - difficultesLibres: CONFIG.RDD.difficultesLibres, - malusArmureValue: 0, - surencMalusFlag: false, // A ne pas utiliser pour les sorts - surencMalusValue: 0, - surencMalusApply: false, - isNatation: false, - useEncForNatation: false - } - if ( this.currentTMR) this.currentTMR.minimize(); // Hide - let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-roll-sort.html', rollData); - new RdDRollDialog("sort", html, rollData, this ).render(true); - } - /* -------------------------------------------- */ _createCallbackExperience() { return { @@ -1374,6 +1267,125 @@ export class RdDActor extends Actor { ChatMessage.create(message); } + /* -------------------------------------------- */ + async rollUnSort(coord) { + let sortList = duplicate(this.getSortList()); // Duplication car les pts de reve sont modifiés dans le sort + if (!sortList || sortList.length == 0) + { + ui.notifications.info("Aucun sort disponible!"); + return; + } + + let rollData = { + selectedCarac: this.data.data.carac.reve, + draconicList: this.getDraconicList(), + sortList: sortList, + selectedDraconic: this.getBestDraconic(), + selectedSort: sortList[0], + coord: coord, + coordLabel: TMRUtility.getTMRDescription( coord).label, + diffLibre: sortList[0].data.difficulte, // Per default at startup + coutreve: Array(20).fill().map((item, index) => 1 + index) + } + + if ( this.currentTMR) this.currentTMR.minimize(); // Hide + const dialog = await RdDRoll.create(this, rollData, + {html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-sort.html'}, + { + name: 'lancer-un-sort', + label: 'Lancer un sort', + callbacks: [ + this._createCallbackExperience(), + { action: r => this._rollUnSortResult(r, false) } + ] + }, + { + name: 'mettre-en-reserve', + label: 'Mettre un sort en réserve', + callbacks: [ + this._createCallbackExperience(), + { action: r => this._rollUnSortResult(r, true) } + ] + } + ); + dialog.render(true); + } + + async _rollUnSortResult(rollData, isSortReserve = false) { + rollData.isSortReserve = isSortReserve; + let rolled = rollData.rolled; + let sort = rollData.selectedSort; + let closeTMR = !rollData.isSortReserve; + + let explications = rollData.isSortReserve + ? ("
Mise en réserve du sort en " + rollData.coordLabel + "(" + rollData.coord + ")") + : "
Lancement du sort "; + explications += sort.name + " : " + Misc.upperFirst(sort.data.draconic) + + " pour " + sort.data.ptreve_reel + " points de Rêve" + + "
Depuis la case " + rollData.coord + " (" + TMRUtility.getTMRDescription(rollData.coord).label + ")"; + + let depenseReve = sort.data.ptreve_reel; + let myReve = duplicate(this.data.data.reve.reve); + if (rolled.isSuccess) { // Réussite du sort ! + //sort.ptreve_reel = coutReve; + if (rolled.isPart) { + depenseReve = Math.max(Math.floor(depenseReve / 2), 1); + } + if (rollData.isSortReserve) { + depenseReve++; + } + + if (myReve.value > depenseReve) { + explications += "
Réussite du sort: " + depenseReve + " points de Rêve sont dépensés (Bonus de case en " + rollData.coord + ": +" + rolled.bonus + "%)"; + + // Incrémenter/gére le bonus de case + RdDItemSort.incrementBonusCase(this, sort, rollData.coord); + + if (rollData.isSortReserve) { + await this.sortMisEnReserve(rollData, sort); + closeTMR = false; + } + } + else { + // Todo 0 pts de reve !!!! + depenseReve = 0; + explications += "
Pas assez de rêve"; + mergeObject(rollData, RdDResolutionTable.getResultat("echec")); + } + } else { + if (rolled.isETotal) { // Echec total ! + depenseReve = Math.max(myReve.value, Math.floor(depenseReve * 1.5)) + explications += "
Echec TOTAL du sort : " + depenseReve + " Points de Rêve"; + // TODO: mise en réserve d'un échec total... + } else { + depenseReve = 0 + explications += "
Echec du sort !"; + } + } + + myReve.value = Math.max(myReve.value - depenseReve, 0); + await this.update({ "data.reve.reve": myReve }); + + if (myReve.value == 0) { // 0 points de reve + ChatMessage.create({ content: this.name + " est réduit à 0 Points de Rêve, et tombe endormi !" }); + closeTMR = true; + } + if (closeTMR) { + this.currentTMR.close(); // Close TMR ! + } else { + this.currentTMR.maximize(); // Re-display TMR + } + // Final chat message + let chatOptions = { + content: "Test : " + rollData.selectedCarac.label + " / " + rollData.selectedDraconic.name + " / " + rollData.selectedSort.name + "" + + "
Difficultés libre : " + rollData.diffLibre + " / conditions : " + Misc.toSignedString(rollData.diffConditions) +" / état : " + rollData.etat + + RdDResolutionTable.explain(rolled) + + explications + } + + ChatUtility.chatWithRollMode(chatOptions, this.name) + } + /* -------------------------------------------- */ async rollCarac( caracName ) { let rollData = { diff --git a/module/rdd-resolution-table.js b/module/rdd-resolution-table.js index f72142e2..534667f3 100644 --- a/module/rdd-resolution-table.js +++ b/module/rdd-resolution-table.js @@ -102,9 +102,9 @@ export class RdDResolutionTable { rollData.rolled = await this.roll(rollData.caracValue, rollData.finalLevel, rollData.bonus); return rollData; } - + /* -------------------------------------------- */ - static async roll(caracValue, finalLevel, bonus = 0 ) { + static async roll(caracValue, finalLevel, bonus = undefined ) { let chances = this.computeChances(caracValue, finalLevel); chances.showDice = true; this.updateChancesWithBonus( chances, bonus); diff --git a/module/rdd-roll.js b/module/rdd-roll.js index adca65a0..3cc84e04 100644 --- a/module/rdd-roll.js +++ b/module/rdd-roll.js @@ -75,6 +75,9 @@ export class RdDRoll extends Dialog { async onAction(action, html) { await RdDResolutionTable.rollData(this.rollData); + + console.log("RdDRoll -=>", this.rollData, this.rollData.rolled); + if (action.callbacks) for (let callback of action.callbacks) { if (callback.condition == undefined || callback.condition(this.rollData)) { @@ -102,6 +105,7 @@ export class RdDRoll extends Dialog { // Sort management if (rollData.selectedSort) { + rollData.bonus = RdDItemSort.getCaseBonus( rollData.selectedSort, rollData.coord ), //console.log("Toggle show/hide", rollData.selectedSort); HtmlUtility._showControlWhen("#div-sort-difficulte", RdDItemSort.isDifficulteVariable(rollData.selectedSort)) HtmlUtility._showControlWhen("#div-sort-ptreve", RdDItemSort.isCoutVariable(rollData.selectedSort)) @@ -154,6 +158,7 @@ export class RdDRoll extends Dialog { html.find('#sort').change((event) => { let sortKey = Misc.toInt(event.currentTarget.value); this.rollData.selectedSort = rollData.sortList[sortKey]; // Update the selectedCarac + this.rollData.bonus = RdDItemSort.getCaseBonus( rollData.selectedSort, rollData.coord ); RdDItemSort.setCoutReveReel(rollData.selectedSort); //console.log("RdDRollSelectDialog - Sort selection", rollData.selectedSort); updateRollResult(rollData); diff --git a/module/rdd-tmr-dialog.js b/module/rdd-tmr-dialog.js index 8eaca821..a00fea30 100644 --- a/module/rdd-tmr-dialog.js +++ b/module/rdd-tmr-dialog.js @@ -388,7 +388,7 @@ export class RdDTMRDialog extends Dialog { console.log("declencheSortEnReserve", sortReserve) const declenchementSort = "Vous avez déclenché le sort " + sortReserve.sort.name + " en réserve en " + sortReserve.coord + " (" + TMRUtility.getTMRDescription(sortReserve.coord).label - + ") avec " + sortReserve.sort.ptreve_reel + " points de Rêve"; + + ") avec " + sortReserve.sort.data.ptreve_reel + " points de Rêve"; this._tellToGM(declenchementSort); this.close(); }