import { ITEM_TYPES } from "../constants.js" import { Grammar } from "../grammar.js" import { CARACS, RdDCarac } from "../rdd-carac.js" import { RdDTimestamp } from "../time/rdd-timestamp.js" import { TMRUtility } from "../tmr-utility.js" import { ROLL_MODE_MEDITATION } from "./roll-constants.mjs" import { PART_CARAC } from "./roll-part-carac.mjs" import { PART_COMP } from "./roll-part-comp.mjs" import { RollPartSelect } from "./roll-part-select.mjs" import { ROLLDIALOG_SECTION } from "./roll-part.mjs" export const PART_MEDITATION = "meditation" export class RollPartMeditation extends RollPartSelect { get code() { return PART_MEDITATION } get section() { return ROLLDIALOG_SECTION.CHOIX } isValid(rollData) { return rollData.active.actor.isPersonnage() && rollData.active.actor.isHautRevant() } visible(rollData) { return this.isRollMode(rollData, ROLL_MODE_MEDITATION) } loadRefs(rollData) { const refs = this.getRefs(rollData) foundry.utils.mergeObject(refs, { meditations: rollData.active.actor.itemTypes[ITEM_TYPES.meditation] .map(it => RollPartMeditation.$extractMeditation(it, rollData.active.actor)) } ) if (refs.meditations.length > 0) { this.$selectMeditation(rollData) } } choices(refs) { return refs.meditations } static $extractMeditation(meditation, actor) { return { key: meditation.id, label: meditation.name, meditation: meditation, comp: actor.getCompetence(meditation.system.competence) } } prepareContext(rollData) { this.getCurrent(rollData).value = this.getMalusConditions(rollData) } getMalusConditions(rollData) { const current = this.getCurrent(rollData) const conditionsManquantes = [ current.isComportement, current.isHeure, current.isPurification, current.isVeture ].filter(it => !it).length return -2 * conditionsManquantes } getMalusEchecs(rollData) { return this.getCurrent(rollData).meditation.system.malus } getAjustements(rollData) { const malusEchecs = { label: "Méditation", diff: this.getMalusEchecs(rollData) } const malusConditions = { label: "Conditions", diff: this.getMalusConditions(rollData) } return [malusConditions, ...(malusEchecs.diff == 0 ? [] : [malusEchecs])] } $selectMeditation(rollData, key) { const previous = this.getCurrent(rollData) const current = this.selectByKey(rollData, key, 0) if (current.key != previous.key) { const heureMonde = RdDTimestamp.getWorldTime().heure const heureMeditation = RdDTimestamp.findHeure(current.meditation.system.heure)?.heure current.isHeure = heureMeditation == heureMonde current.isTMR = Grammar.equalsInsensitive(current.meditation.system.tmr, TMRUtility.getTMRType(rollData.active.actor.system.reve.tmrpos.coord)) } } async _onRender(rollDialog, context, options) { const selectMeditation = rollDialog.element.querySelector(`roll-section[name="${this.code}"] select[name="select-meditation"]`) selectMeditation.addEventListener("change", e => { const selectOptions = e.currentTarget.options const index = selectOptions.selectedIndex this.$selectMeditation(rollDialog.rollData, selectOptions[index]?.value) rollDialog.render() rollDialog.setModeTitle() }) this.setupListenerCondition(rollDialog, 'isComportement') this.setupListenerCondition(rollDialog, 'isHeure') this.setupListenerCondition(rollDialog, 'isPurification') this.setupListenerCondition(rollDialog, 'isVeture') } setupListenerCondition(rollDialog, inputName) { const checkbox = rollDialog.element.querySelector(`roll-section[name="${this.code}"] input[name="${inputName}"]`) checkbox.addEventListener("change", e => { const current = this.getCurrent(rollDialog.rollData) current[inputName] = e.currentTarget.checked rollDialog.render() }) } impactOtherPart(part, rollData) { if (this.visible(rollData)) { const current = this.getCurrent(rollData) switch (part.code) { case PART_CARAC: return part.filterCaracs(rollData, [CARACS.INTELLECT]) case PART_COMP: return part.filterComps(rollData,[current.comp?.name]) } } return undefined } }