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) {
|
2022-09-07 09:01:23 +02:00
|
|
|
return sort && (sort.system.difficulte.toLowerCase() == "variable");
|
2020-11-27 10:21:20 +01:00
|
|
|
}
|
2020-12-04 20:52:04 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2020-11-27 10:21:20 +01:00
|
|
|
static isCoutVariable(sort) {
|
2022-09-07 09:01:23 +02:00
|
|
|
return sort && (sort.system.ptreve.toLowerCase() == "variable" || sort.system.ptreve.indexOf("+") >= 0);
|
2020-11-27 10:21:20 +01:00
|
|
|
}
|
2023-03-30 01:31:41 +02:00
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2023-03-30 01:31:41 +02:00
|
|
|
static setCoutReveReel(sort) {
|
2020-11-27 10:21:20 +01:00
|
|
|
if (sort) {
|
2022-06-12 08:17:59 +02:00
|
|
|
sort.system.ptreve_reel = this.isCoutVariable(sort) ? 1 : sort.system.ptreve;
|
2020-11-27 10:21:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2023-03-30 01:31:41 +02:00
|
|
|
return Misc.toInt(sort.system.difficulte);
|
2020-11-27 10:21:20 +01:00
|
|
|
}
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2023-03-30 01:31:41 +02:00
|
|
|
static buildBonusCaseList(bonuscase, newCase) {
|
|
|
|
const list = RdDItemSort._bonuscaseStringToList(bonuscase)
|
|
|
|
if (newCase) {
|
|
|
|
return list.concat({ case: "Nouvelle", bonus: 0 });
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
2023-03-30 01:31:41 +02:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/**
|
|
|
|
* Retourne une liste de bonus/case pour un item-sheet
|
|
|
|
* @param {} item
|
|
|
|
*/
|
2023-03-30 01:31:41 +02:00
|
|
|
static getBonusCaseList(item, newCase = false) {
|
2020-12-10 02:29:11 +01:00
|
|
|
// Gestion spéciale case bonus
|
2023-03-30 01:31:41 +02:00
|
|
|
if (item.type == 'sort') {
|
|
|
|
return RdDItemSort.buildBonusCaseList(item.system.bonuscase, newCase);
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
2020-12-10 02:29:11 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
2023-03-30 01:31:41 +02:00
|
|
|
|
2022-09-07 18:47:56 +02:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
/** Met à jour les données de formulaire
|
|
|
|
* si static des bonus de cases sont présents
|
|
|
|
* */
|
2023-03-30 01:31:41 +02:00
|
|
|
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) {
|
2022-10-21 02:10:37 +02:00
|
|
|
caseCheck[coord] = bonus;
|
2023-03-30 01:31:41 +02:00
|
|
|
list.push({ case: coord, bonus: bonus });
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 01:31:41 +02:00
|
|
|
return RdDItemSort._bonuscaseListToString(list);
|
2022-09-07 18:47:56 +02:00
|
|
|
}
|
2022-10-21 02:10:37 +02:00
|
|
|
return undefined;
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
2023-03-30 01:31:41 +02:00
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2023-03-30 01:31:41 +02:00
|
|
|
static incrementBonusCase(actor, sort, coord) {
|
|
|
|
if (TMRUtility.getTMR(coord).type == "fleuve") {
|
|
|
|
coord = 'Fleuve';
|
|
|
|
}
|
|
|
|
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 }]);
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
2023-03-30 01:31:41 +02:00
|
|
|
|
|
|
|
|
2020-12-04 20:52:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2023-03-30 01:31:41 +02:00
|
|
|
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] };
|
|
|
|
});
|
2020-12-04 20:52:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 10:21:20 +01:00
|
|
|
}
|