79 lines
2.3 KiB
JavaScript
79 lines
2.3 KiB
JavaScript
import { DIFF, DIFFS, ROLL_TYPE_MEDITATION, ROLL_TYPE_OEUVRE, ROLL_TYPE_SORT, ROLL_TYPE_TACHE } from "./roll-constants.mjs";
|
|
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs";
|
|
import { Misc } from "../misc.js";
|
|
|
|
export const PART_DIFF = "diff"
|
|
|
|
const EXCLUDED_ROLL_TYPES = [ROLL_TYPE_TACHE, ROLL_TYPE_MEDITATION, ROLL_TYPE_SORT, ROLL_TYPE_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.type = saved?.type ?? current.type
|
|
}
|
|
|
|
store(rollData, targetData) {
|
|
const current = this.getCurrent(rollData)
|
|
this.setSaved(targetData, {
|
|
value: current.value,
|
|
type: current.type
|
|
})
|
|
}
|
|
|
|
visible(rollData) {
|
|
if (EXCLUDED_ROLL_TYPES.includes(rollData.type.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).includes(current.type)
|
|
}
|
|
|
|
prepareContext(rollData) {
|
|
const current = this.getCurrent(rollData)
|
|
const diffType = DIFFS[current.type] ?? DIFFS[DIFF.AUCUN]
|
|
foundry.utils.mergeObject(current,
|
|
{
|
|
type: diffType.key,
|
|
label: diffType?.label ?? '',
|
|
disabled: !diffType.libre,
|
|
value: Misc.inRange(current.value ?? 0, -10, diffType.max),
|
|
min: -10,
|
|
max: diffType.max
|
|
},
|
|
{ inplace: true }
|
|
)
|
|
}
|
|
|
|
setDiff(rollData, diff) {
|
|
const current = this.getCurrent(rollData)
|
|
current.value = diff.diff
|
|
current.type = diff.type
|
|
}
|
|
|
|
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("input", e => this.onInputChange(e, rollDialog))
|
|
}
|
|
|
|
onInputChange(event, rollDialog) {
|
|
this.getCurrent(rollDialog.rollData).value = parseInt(event.currentTarget.value)
|
|
rollDialog.render()
|
|
}
|
|
|
|
} |