diff --git a/module/item-sheet.js b/module/item-sheet.js index 268474cd..2cbbf815 100644 --- a/module/item-sheet.js +++ b/module/item-sheet.js @@ -239,9 +239,8 @@ export class RdDItemSheet extends ItemSheet { _updateObject(event, formData) { if (this.item.type == 'sort') { // Données de bonus de cases ? - formData['system.bonuscase'] = RdDItemSort.buildBonusCaseStringFromFormData(formData.bonusValue, formData.caseValue); + formData['system.bonuscase'] = RdDItemSort.buildBonuscaseFromArrays(formData.bonusValue, formData.caseValue); } - return this.item.update(formData); } diff --git a/module/item-sort.js b/module/item-sort.js index befbb6b0..09f9be37 100644 --- a/module/item-sort.js +++ b/module/item-sort.js @@ -1,4 +1,3 @@ -/* -------------------------------------------- */ import { Misc } from "./misc.js"; import { TMRUtility } from "./tmr-utility.js"; @@ -14,9 +13,9 @@ export class RdDItemSort extends Item { static isCoutVariable(sort) { return sort && (sort.system.ptreve.toLowerCase() == "variable" || sort.system.ptreve.indexOf("+") >= 0); } - + /* -------------------------------------------- */ - static setCoutReveReel(sort){ + static setCoutReveReel(sort) { if (sort) { sort.system.ptreve_reel = this.isCoutVariable(sort) ? 1 : sort.system.ptreve; } @@ -25,94 +24,91 @@ export class RdDItemSort extends Item { /* -------------------------------------------- */ static getDifficulte(sort, variable) { if (sort && !RdDItemSort.isDifficulteVariable(sort)) { - return Misc.toInt(sort.system.difficulte); + return Misc.toInt(sort.system.difficulte); } return variable; } /* -------------------------------------------- */ - static buildBonusCaseList( caseBonusString, newCase ) { - if (caseBonusString == undefined) { - return []; - } - let bonusCaseList = []; - let bonusCaseArray = caseBonusString == undefined ? [] : caseBonusString.split(','); - for( let bonusCase of bonusCaseArray) { - let bonusSplit = bonusCase.split(':'); - bonusCaseList.push( { case: bonusSplit[0], bonus: bonusSplit[1] } ); - } - if ( newCase ) - bonusCaseList.push( {case: "Nouvelle", bonus: 0} ); - return bonusCaseList; + static buildBonusCaseList(bonuscase, newCase) { + const list = RdDItemSort._bonuscaseStringToList(bonuscase) + if (newCase) { + return list.concat({ case: "Nouvelle", bonus: 0 }); } - - /* -------------------------------------------- */ + return list; + } + /** * Retourne une liste de bonus/case pour un item-sheet * @param {} item */ - static getBonusCaseList( item, newCase = false ) { + static getBonusCaseList(item, newCase = false) { // Gestion spéciale case bonus - if ( item.type == 'sort') { - return this.buildBonusCaseList(item.system.bonuscase, newCase ); + if (item.type == 'sort') { + return RdDItemSort.buildBonusCaseList(item.system.bonuscase, newCase); } return undefined; } - + /* -------------------------------------------- */ /** Met à jour les données de formulaire * si static des bonus de cases sont présents * */ - static buildBonusCaseStringFromFormData( bonuses, cases ) { - if ( bonuses ) { - let list = []; - let caseCheck = {}; - for (let i=0; i 0 && caseCheck[coord] == undefined ) { + static buildBonuscaseFromArrays(bonuses, coords) { + if (bonuses) { + const list = []; + const caseCheck = {}; + for (let i = 0; i < bonuses.length && i < coords.length; i++) { + const coord = coords[i] == 'Fleuve' ? 'Fleuve' : (coords[i]?.toUpperCase() ?? 'A1'); + const bonus = bonuses[i] || 0; + if (TMRUtility.verifyTMRCoord(coord) && bonus > 0 && caseCheck[coord] == undefined) { caseCheck[coord] = bonus; - list.push( coord+":"+bonus ); + list.push({ case: coord, bonus: bonus }); } } - return list.toString(); + return RdDItemSort._bonuscaseListToString(list); } return undefined; } - + /* -------------------------------------------- */ - static incrementBonusCase( actor, sort, coord ) { - let bonusCaseList = this.buildBonusCaseList(sort.system.bonuscase, false); - //console.log("ITEMSORT", sort, bonusCaseList); - - let found = false; - let StringList = []; - for( let bc of bonusCaseList) { - if (bc.case == coord) { // Case existante - found = true; - bc.bonus = Number(bc.bonus) + 1; - } - StringList.push( bc.case+':'+bc.bonus ); - } - if ( !found) { //Nouvelle case, bonus de 1 - StringList.push(coord+':1'); - } - - // Sauvegarde/update - let bonuscase = StringList.toString(); - //console.log("Bonus cae :", bonuscase); - actor.updateEmbeddedDocuments('Item', [{ _id: sort._id, 'system.bonuscase': bonuscase }] ); - } - - /* -------------------------------------------- */ - static getCaseBonus( sort, coord) { - let bonusCaseList = this.buildBonusCaseList(sort.system.bonuscase, false); - for( let bc of bonusCaseList) { - if (bc.case == coord) { // Case existante - return Number(bc.bonus); - } + static incrementBonusCase(actor, sort, coord) { + if (TMRUtility.getTMR(coord).type == "fleuve") { + coord = 'Fleuve'; } - return 0; + const list = RdDItemSort.buildBonusCaseList(sort.system.bonuscase, false); + const bonus = Number(list.find(it => it.case == coord)?.bonus ?? 0); + const modified = { case: coord, bonus: bonus + 1 }; + + const bonuscase = RdDItemSort._bonuscaseListToString( + list.filter(it => it.case != coord).concat(modified) + ); + + // Sauvegarde/update + actor.updateEmbeddedDocuments('Item', [{ _id: sort._id, 'system.bonuscase': bonuscase }]); + } + + + /* -------------------------------------------- */ + static getCaseBonus(sort, coord) { + const isFleuve = TMRUtility.getTMR(coord).type == "fleuve"; + + let bc = RdDItemSort.buildBonusCaseList(sort.system.bonuscase, false) + .filter(it => it.case == coord || (isFleuve && it.case == 'Fleuve')) + .find(it => true) + return Number(bc?.bonus ?? 0); + } + + static _bonuscaseListToString(list) { + return list.map(it => `${it.case}:${it.bonus}`) + .sort(Misc.ascending()) + .join(','); + } + static _bonuscaseStringToList(bonuscase) { + return (bonuscase ?? '').split(',').map(it => { + const b = it.split(':'); + return { case: b[0], bonus: b[1] }; + }); } } \ No newline at end of file diff --git a/module/tmr-utility.js b/module/tmr-utility.js index 66ab857b..f74e4194 100644 --- a/module/tmr-utility.js +++ b/module/tmr-utility.js @@ -226,9 +226,6 @@ export const TMRType = { desolation: { name: "désolation", genre: "f" } } -/* -------------------------------------------- */ -const caseSpecificModes = ["attache", "trounoir", "debordement", "reserve_extensible", "maitrisee"]; - /* -------------------------------------------- */ const tmrRandomMovePatten = [{ name: 'top', col: 0, row: -1 }, @@ -239,8 +236,6 @@ const tmrRandomMovePatten = { name: 'topleft', col: -1, row: -1 } ] -/* -------------------------------------------- */ - /* -------------------------------------------- */ export class TMRUtility { static init() { @@ -258,32 +253,25 @@ export class TMRUtility { /* -------------------------------------------- */ static verifyTMRCoord(coord) { - let TMRregexp = new RegExp(/([A-M])(\d+)/g); - let res = TMRregexp.exec(coord); - if (res && res[1] && res[2]) { - if (res[2] > 0 && res[2] < 16) { - return true; - } - } - return false; + return Grammar.equalsInsensitive(coord, 'Fleuve') || TMRUtility.getTMR(coord); } /* -------------------------------------------- */ static getTMR(coord) { - return TMRMapping[coord]; + return coord == 'Fleuve' ? TMRMapping['D1'] : TMRMapping[coord]; } static getTMRLabel(coord) { - return TMRMapping[coord]?.label ?? (coord + ": case inconnue"); + return TMRUtility.getTMR(coord)?.label ?? (coord + ": case inconnue"); } static getTMRType(coord) { - const tmr = TMRMapping[coord]; + const tmr = TMRUtility.getTMR(coord); return Misc.upperFirst(TMRType[tmr.type].name); } static getTMRDescr(coord) { - const tmr = TMRMapping[coord]; + const tmr = TMRUtility.getTMR(coord); return Grammar.articleDetermine(tmr.type) + ' ' + tmr.label; }