import { Grammar } from "../grammar.js" import { ROLL_MODE_ATTAQUE } 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, RollPart } from "./roll-part.mjs" export const PART_ATTAQUE = 'attaque' export class RollPartAttaque extends RollPartSelect { get code() { return PART_ATTAQUE } get section() { return ROLLDIALOG_SECTION.CHOIX } visible(rollData) { return this.isRollMode(rollData, ROLL_MODE_ATTAQUE) } loadRefs(rollData) { const refs = this.getRefs(rollData) const attaques = rollData.active.actor.listAttaques() refs.attaques = attaques.map(it => RollPartAttaque.$extractAttaque(it, rollData.active.actor)) if (refs.attaques.length>0){ this.$selectAttaque(rollData) } } choices(refs) { return refs.attaques } static $extractAttaque(action, actor) { return { key: `${action.action}::${action.arme.id}::${action.comp.id}`, label: action.name, action: action, arme: action.arme, comp: action.comp, } } $selectAttaque(rollData, key) { this.selectByKey(rollData, key, 0) } async _onRender(rollDialog, context, options) { const selectAttaque = rollDialog.element.querySelector(`roll-section[name="${this.code}"] select[name="select-attaque"]`) selectAttaque.addEventListener("change", e => { const selectOptions = e.currentTarget.options const index = selectOptions.selectedIndex this.$selectAttaque(rollDialog.rollData, selectOptions[index]?.value) rollDialog.setModeTitle() rollDialog.render() }) } getExternalPartsFilter(partCode, rollData) { if (this.visible(rollData)) { const current = this.getCurrent(rollData) switch (partCode) { case PART_CARAC: return p => Grammar.equalsInsensitive(current.action.carac.key, p.key) case PART_COMP: return p => p.label == current.comp?.name } } return undefined } }