From e18b5ad1927b61f86f8e20b5438afff90d0b2d20 Mon Sep 17 00:00:00 2001 From: sladecraven Date: Sun, 3 Jan 2021 18:19:18 +0100 Subject: [PATCH] #103 : Gerer les augmentations de niveaux en comp et carac --- module/actor-sheet.js | 13 +++- module/actor.js | 83 ++++++++++++++++++++++--- module/rdd-utility.js | 15 ++++- templates/actor-sheet.html | 6 +- templates/chat-actor-carac-xp.html | 3 + templates/chat-actor-competence-xp.html | 6 ++ 6 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 templates/chat-actor-carac-xp.html create mode 100644 templates/chat-actor-competence-xp.html diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 7ec9930c..fbad5d0b 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -44,6 +44,7 @@ export class RdDActorSheet extends ActorSheet { let competenceXPTotal = 0; if (data.itemsByType.competence) { for (const item of data.itemsByType.competence) { + this.actor.checkCompetenceXP( item.name ); // Petite vérification experience //console.log("Push...", item, item.data.categorie); let list = data.competenceByCategory[item.data.categorie]; if (!list) { @@ -66,7 +67,12 @@ export class RdDActorSheet extends ActorSheet { // Compute current carac sum let sum = 0; - Object.values(data.data.carac).forEach(carac => { if (!carac.derivee) { sum += parseInt(carac.value) } } ); + for (let caracName in data.data.carac) { + let carac = data.data.carac[caracName]; + if (!carac.derivee) { + sum += parseInt(carac.value); + } + } data.data.caracSum = sum; // Force empty arme, at least for Esquive @@ -397,6 +403,11 @@ export class RdDActorSheet extends ActorSheet { //console.log("Value changed :", event, caracName); this.actor.updateCarac( caracName, parseInt(event.target.value) ); } ); + html.find('.carac-xp').change((event) => { + let caracName = event.currentTarget.name.replace(".xp", "").replace("data.carac.", ""); + //console.log("Value changed :", event, caracName); + this.actor.updateCaracXP( caracName, parseInt(event.target.value) ); + } ); // On competence change html.find('.competence-value').change((event) => { let compName = event.currentTarget.attributes.compname.value; diff --git a/module/actor.js b/module/actor.js index fea389a0..6b2ec179 100644 --- a/module/actor.js +++ b/module/actor.js @@ -540,7 +540,7 @@ export class RdDActor extends Actor { } /* -------------------------------------------- */ - updateCarac( caracName, caracValue ) + async updateCarac( caracName, caracValue ) { let caracpath = "data.carac." + caracName + ".value" if (caracName == "reve") { @@ -548,9 +548,17 @@ export class RdDActor extends Actor { this.setPointsDeSeuil(caracValue); } } - this.update( { caracpath: caracValue } ); + await this.update( { [caracpath]: caracValue } ); } - + + /* -------------------------------------------- */ + async updateCaracXP( caracName, caracXP ) + { + let caracpath = "data.carac."+caracName+".xp"; + await this.update( { [caracpath]: caracXP } ); + this.checkCaracXP( caracName ); + } + /* -------------------------------------------- */ async updateCreatureCompetence( compName, fieldName, compValue ) { @@ -1160,6 +1168,7 @@ export class RdDActor extends Actor { return ajustementMoral; } + /* -------------------------------------------- */ async moralIncDec(ajustementMoral) { let compteurs = duplicate(this.data.data.compteurs); compteurs.moral.value = Misc.toInt(compteurs.moral.value);; @@ -1190,8 +1199,9 @@ export class RdDActor extends Actor { if (!succes && moral > 0) return -1; } return 0; - } + + /* -------------------------------------------- */ async setEthylisme(degre) { let ethylisme = duplicate(this.data.data.compteurs.ethylisme); ethylisme.value = degre; @@ -1347,6 +1357,56 @@ export class RdDActor extends Actor { }; } + /* -------------------------------------------- */ + async checkCaracXP( caracName ) { + let carac = this.data.data.carac[caracName]; + console.log("XP chek", carac, caracName); + if (carac && carac.xp > 0) { + let xpNeeded = RdDUtility.getCaracNextXp( carac.value ); + if ( carac.xp >= xpNeeded ) { + carac = duplicate(carac); + carac.value = Number(carac.value) + 1; + await this.updateCarac( caracName, carac.value ); + carac.xp -= xpNeeded; + await this.updateCaracXP( caracName, carac.xp ); + + let xpData = { + alias: this.name, + carac: caracName, + value: carac.value, + xp: carac.xp + } + let content = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-actor-carac-xp.html`, xpData); + ChatUtility.createChatMessage({ content: content }, "default", this.name); + } + } + } + + /* -------------------------------------------- */ + async checkCompetenceXP( compName ) { + let competence = RdDUtility.findCompetence( this.data.items, compName); + if ( competence && competence.data.xp > 0) { + let xpNeeded = RdDUtility.getCompetenceNextXp( competence.data.niveau ); + if ( competence.data.xp >= xpNeeded ) { + competence.data.xp -= xpNeeded; + competence.data.niveau += 1; + let update = {_id: competence._id, "data.xp": competence.data.xp, "data.niveau": competence.data.niveau}; + await this.updateEmbeddedEntity( "OwnedItem", update ); + + let xpData = { + alias: this.name, + competence: competence.name, + niveau: competence.data.niveau, + xp: competence.data.xp, + archetype: competence.data.niveau_archetype, + archetypeWarning: competence.data.niveau > competence.data.niveau_archetype + } + let content = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-actor-competence-xp.html`, xpData); + ChatUtility.createChatMessage({ content: content }, "default", this.name); + } + } + } + /* -------------------------------------------- */ async _appliquerAjoutExperience(rollData, display=true) { let xpResult = this.appliquerExperience( rollData.rolled, rollData.selectedCarac.label, rollData.competence); @@ -1357,6 +1417,12 @@ export class RdDActor extends Actor { + xpmsg; ChatMessage.create(message); } + if ( xpResult && xpResult.xpComp > 0 && rollData.competence) { + this.checkCompetenceXP( rollData.competence.name ); + } + if ( xpResult && xpResult.xpCarac > 0 && rollData.selectedCarac) { + this.checkCaracXP( rollData.selectedCarac.name ); + } } /* -------------------------------------------- */ @@ -1765,16 +1831,17 @@ export class RdDActor extends Actor { whisper: ChatMessage.getWhisperRecipients(game.user.name) } ); } } - return { result:true, xpcarac:xpCarac, xpCompetence: xpComp }; //XP + return { result:true, xpCarac:xpCarac, xpCompetence: xpComp }; //XP } - return { result:false, xpcarac:0, xpCompetence: 0 }; // Pas d'XP + return { result:false, xpCarac:0, xpCompetence: 0 }; // Pas d'XP } /* -------------------------------------------- */ async ajouteNombreAstral( data ) { // Gestion expérience (si existante) - let astrologie = RdDUtility.findCompetence( this.data.items, "astrologie"); - this.appliquerExperience( data.rolled, "vue", astrologie); + data.competence = RdDUtility.findCompetence( this.data.items, "astrologie"); + data.selectedCarac = this.data.data.carac["vue"]; + this._appliquerAjoutExperience( data ); // Ajout du nombre astral const item = {name: "Nombre Astral", type: "nombreastral", data: diff --git a/module/rdd-utility.js b/module/rdd-utility.js index 83cb2347..8c939dc3 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -45,6 +45,8 @@ const carac_array = [ "taille", "apparence", "constitution", "force", "agilite", const difficultesLibres = [0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]; const ajustementsConditions = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10]; const ajustementsEncaissement = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12, +13, +14, +15, +16, +17, +18, +19, +20, +21, +22, +23, +24, +25]; +const carac_xp_par_valeur = [6, 6, 7, 7, 8, 8, 9, 9, 10, 20, 30, 40, 50, 60, 70]; +const XP_CARAC_OFFSET = 7; /* -------------------------------------------- */ function _buildAllSegmentsFatigue(max) { @@ -208,7 +210,8 @@ export class RdDUtility { 'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-tache.html', 'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-sort.html', 'systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-summary.html', - + 'systems/foundryvtt-reve-de-dragon/templates/chat-actor-competence-xp.html', + 'systems/foundryvtt-reve-de-dragon/templates/chat-actor-carac-xp.html' ]; return loadTemplates(templatePaths); @@ -406,6 +409,16 @@ export class RdDUtility { return false; } + /* -------------------------------------------- */ + static getCaracNextXp( value ) { + return carac_xp_par_valeur[value - XP_CARAC_OFFSET]; + } + + /* -------------------------------------------- */ + static getCompetenceNextXp( niveau ) { + return competence_xp_par_niveau[niveau+10]; + } + /* -------------------------------------------- */ static computeCompetenceXPCost( competence ) { diff --git a/templates/actor-sheet.html b/templates/actor-sheet.html index d6be90b5..8eb79477 100644 --- a/templates/actor-sheet.html +++ b/templates/actor-sheet.html @@ -105,16 +105,16 @@ {{#if carac.isTaille}} {{carac.label}} -