2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
import { Misc } from "./misc.js";
|
2020-12-04 20:52:04 +01:00
|
|
|
import { TMRUtility } from "./tmr-utility.js";
|
2020-11-27 10:21:20 +01:00
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
export class RdDItemSort extends Item {
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
static isDifficulteVariable(sort) {
|
|
|
|
return sort && (sort.data.difficulte.toLowerCase() == "variable");
|
|
|
|
}
|
2020-12-04 20:52:04 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
static isCoutVariable(sort) {
|
|
|
|
return sort && (sort.data.ptreve.toLowerCase() == "variable" || sort.data.ptreve.indexOf("+") >= 0);
|
|
|
|
}
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
static setCoutReveReel(sort){
|
|
|
|
if (sort) {
|
|
|
|
sort.data.ptreve_reel = this.isCoutVariable(sort) ? 1 : sort.data.ptreve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
static getDifficulte(sort, variable) {
|
|
|
|
if (sort && !RdDItemSort.isDifficulteVariable(sort)) {
|
|
|
|
return Misc.toInt(sort.data.difficulte);
|
|
|
|
}
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static buildBonusCaseList( caseBonusString, newCase ) {
|
2020-12-10 02:29:11 +01:00
|
|
|
if (caseBonusString == undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-12-04 20:52:04 +01:00
|
|
|
let bonusCaseList = [];
|
2020-12-10 02:29:11 +01:00
|
|
|
let bonusCaseArray = caseBonusString == undefined ? [] : caseBonusString.split(',');
|
2020-12-04 20:52:04 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Retourne une liste de bonus/case pour un item-sheet
|
|
|
|
* @param {} item
|
|
|
|
*/
|
|
|
|
static getBonusCaseList( data, newCase = false ) {
|
2020-12-10 02:29:11 +01:00
|
|
|
// Gestion spéciale case bonus
|
|
|
|
if ( data.item.type == 'sort') {
|
|
|
|
return this.buildBonusCaseList(data.data.bonuscase, newCase );
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
2020-12-10 02:29:11 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
2020-12-04 20:52:04 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
/** Met à jour les données de formulaire
|
|
|
|
* si static des bonus de cases sont présents
|
|
|
|
* */
|
|
|
|
static buildBonusCaseStringFromFormData( formData ) {
|
|
|
|
if ( formData.bonusValue ) {
|
|
|
|
let list = [];
|
2020-12-08 23:12:43 +01:00
|
|
|
let caseCheck = {};
|
2020-12-04 20:52:04 +01:00
|
|
|
for(let i=0; i<formData.bonusValue.length; i++) {
|
|
|
|
let caseTMR = formData.caseValue[i] || 'A1';
|
|
|
|
caseTMR = caseTMR.toUpperCase();
|
|
|
|
if ( TMRUtility.verifyTMRCoord( caseTMR ) ) { // Sanity check
|
2020-12-08 23:12:43 +01:00
|
|
|
let bonus = formData.bonusValue[i] || 0;
|
|
|
|
if ( bonus > 0 && caseCheck[caseTMR] == undefined ) {
|
|
|
|
caseCheck[caseTMR] = bonus;
|
|
|
|
list.push( caseTMR+":"+bonus );
|
|
|
|
}
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
formData.bonusValue = undefined;
|
|
|
|
formData.caseValue = undefined;
|
|
|
|
formData['data.bonuscase'] = list.toString(); // Reset
|
|
|
|
}
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static incrementBonusCase( actor, sort, coordTMR ) {
|
|
|
|
let bonusCaseList = this.buildBonusCaseList(sort.data.bonuscase, false);
|
|
|
|
//console.log("ITEMSORT", sort, bonusCaseList);
|
|
|
|
|
|
|
|
let found = false;
|
|
|
|
let StringList = [];
|
|
|
|
for( let bc of bonusCaseList) {
|
|
|
|
if (bc.case == coordTMR) { // 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(coordTMR+':1');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sauvegarde/update
|
|
|
|
let bonuscase = StringList.toString();
|
|
|
|
//console.log("Bonus cae :", bonuscase);
|
|
|
|
actor.updateEmbeddedEntity("OwnedItem", { _id: sort._id, 'data.bonuscase': bonuscase } );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static getCaseBonus( sort, coordTMR) {
|
|
|
|
let bonusCaseList = this.buildBonusCaseList(sort.data.bonuscase, false);
|
|
|
|
for( let bc of bonusCaseList) {
|
|
|
|
if (bc.case == coordTMR) { // Case existante
|
2021-01-22 14:35:14 +01:00
|
|
|
return Number(bc.bonus);
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-27 10:21:20 +01:00
|
|
|
}
|