forked from public/foundryvtt-reve-de-dragon
77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
import { DIFF_MODE, DIFF_MODES, ROLL_MODE_MEDITATION, ROLL_MODE_OEUVRE, ROLL_MODE_SORT, ROLL_MODE_TACHE } from "./roll-constants.mjs";
|
|
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs";
|
|
|
|
export const PART_DIFF = "diff"
|
|
|
|
const EXCLUDED_ROLL_MODES = [ROLL_MODE_TACHE, ROLL_MODE_MEDITATION, ROLL_MODE_SORT, ROLL_MODE_OEUVRE]
|
|
|
|
export class RollPartDiff extends RollPart {
|
|
|
|
get code() { return PART_DIFF }
|
|
get section() { return ROLLDIALOG_SECTION.CHOIX }
|
|
|
|
restore(rollData) {
|
|
const current = this.getCurrent(rollData)
|
|
const saved = this.getSaved(rollData)
|
|
current.value = saved?.value ?? current.value ?? 0
|
|
current.mode = saved?.mode ?? current.mode
|
|
}
|
|
|
|
store(rollData, targetData) {
|
|
const current = this.getCurrent(rollData)
|
|
this.setSaved(targetData, {
|
|
value: current.value,
|
|
mode: current.mode
|
|
})
|
|
}
|
|
|
|
visible(rollData) {
|
|
if (EXCLUDED_ROLL_MODES.includes(rollData.mode.current)) {
|
|
return false
|
|
}
|
|
const current = this.getCurrent(rollData)
|
|
/* TODO: affiner les cas où afficher ou non. devrait s'afficher pour les jets basiques (même si pas d'opposant sélectionné)*/
|
|
return Object.values(DIFF_MODE).includes(current.mode)
|
|
}
|
|
|
|
prepareContext(rollData) {
|
|
const current = this.getCurrent(rollData)
|
|
const diffMode = DIFF_MODES[current.mode] ?? DIFF_MODES[DIFF_MODE.AUCUN]
|
|
foundry.utils.mergeObject(current,
|
|
{
|
|
mode: diffMode.key,
|
|
label: diffMode?.label ?? '',
|
|
value: current.value ?? 0,
|
|
disabled: !diffMode.libre,
|
|
min: -10,
|
|
max: diffMode.max
|
|
},
|
|
{ inplace: true }
|
|
)
|
|
}
|
|
|
|
setDiff(rollData, diffDefense) {
|
|
const current = this.getCurrent(rollData)
|
|
current.value = diffDefense.diff
|
|
current.mode = diffDefense.mode
|
|
}
|
|
|
|
getAjustements(rollData) {
|
|
const current = this.getCurrent(rollData)
|
|
return [{
|
|
label: current.label,
|
|
diff: current.value
|
|
}]
|
|
}
|
|
|
|
async _onRender(rollDialog, context, options) {
|
|
const input = rollDialog.element.querySelector(`roll-section[name="${this.code}"] input[name="${this.code}"]`)
|
|
|
|
input?.addEventListener("change", e => {
|
|
this.getCurrent(rollDialog.rollData).value = parseInt(e.currentTarget.value)
|
|
rollDialog.render()
|
|
})
|
|
}
|
|
|
|
|
|
} |