forked from public/foundryvtt-reve-de-dragon
Fix: saisie des bonus de cases
This commit is contained in:
@ -2,7 +2,7 @@ import { Grammar } from "./grammar.js";
|
||||
import { RdDItemCompetence } from "./item-competence.js";
|
||||
import { ITEM_TYPES } from "./item.js";
|
||||
import { Misc } from "./misc.js";
|
||||
import { TMRUtility } from "./tmr-utility.js";
|
||||
import { FLEUVE_COORD, TMRType, TMRUtility } from "./tmr-utility.js";
|
||||
|
||||
const VOIES_DRACONIC = [
|
||||
{ code: 'O', label: "Voie d'Oniros", short: 'Oniros', ordre: 'a' },
|
||||
@ -80,89 +80,60 @@ export class RdDItemSort extends Item {
|
||||
return variable;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static buildBonusCaseList(bonuscase, newCase) {
|
||||
const list = RdDItemSort.bonuscaseStringToList(bonuscase)
|
||||
if (newCase) {
|
||||
list.push({ 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) {
|
||||
// Gestion spéciale case bonus
|
||||
if (item.type == ITEM_TYPES.sort) {
|
||||
return RdDItemSort.buildBonusCaseList(item.system.bonuscase, newCase);
|
||||
return RdDItemSort.stringToBonuscases(item.system.bonuscase)
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/** Met à jour les données de formulaire
|
||||
* si static des bonus de cases sont présents
|
||||
* */
|
||||
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({ case: coord, bonus: bonus });
|
||||
}
|
||||
}
|
||||
return RdDItemSort._bonuscaseListToString(list);
|
||||
}
|
||||
return undefined;
|
||||
return [];
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static incrementBonusCase(actor, sort, coord) {
|
||||
if (TMRUtility.getTMR(coord).type == "fleuve") {
|
||||
coord = 'Fleuve';
|
||||
if (TMRUtility.isFleuve(coord)) {
|
||||
coord = FLEUVE_COORD;
|
||||
}
|
||||
let list = RdDItemSort.stringToBonuscases(sort.system.bonuscase);
|
||||
const existing = list.find(it => it.case == coord)
|
||||
const bonus = Number(existing?.bonus ?? 0) + 1
|
||||
if (existing) {
|
||||
existing.bonus = bonus
|
||||
}
|
||||
else {
|
||||
list.push({ case: coord, bonus: 1 })
|
||||
}
|
||||
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 }]);
|
||||
actor.updateEmbeddedDocuments('Item', [{ _id: sort._id, 'system.bonuscase': RdDItemSort.bonuscasesToString(list) }]);
|
||||
}
|
||||
|
||||
|
||||
/* -------------------------------------------- */
|
||||
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)
|
||||
const search = TMRUtility.isFleuve(coord)
|
||||
? it => it.case == 'Fleuve'
|
||||
: it => it.case == coord;
|
||||
const bc = RdDItemSort.stringToBonuscases(sort.system.bonuscase)
|
||||
.find(search)
|
||||
return Number(bc?.bonus ?? 0);
|
||||
}
|
||||
|
||||
static _bonuscaseListToString(list) {
|
||||
static bonuscasesToString(list) {
|
||||
return list.map(it => `${it.case}:${it.bonus}`)
|
||||
.sort(Misc.ascending())
|
||||
.join(',');
|
||||
}
|
||||
static bonuscaseStringToList(bonuscase) {
|
||||
|
||||
static stringToBonuscases(bonuscase) {
|
||||
if (bonuscase == undefined || bonuscase == '') {
|
||||
return []
|
||||
}
|
||||
return bonuscase.split(',').map(it => {
|
||||
const b = it.split(':');
|
||||
return { case: b[0], bonus: b[1] };
|
||||
});
|
||||
return bonuscase.split(',')
|
||||
.map(it => it.split(':'))
|
||||
.map(it => { return { case: it[0], bonus: it[1] } });
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user