Files
foundryvtt-reve-de-dragon/module/roll/roll-part-sort.mjs

132 lines
4.5 KiB
JavaScript

import { ITEM_TYPES } from "../constants.js"
import { ROLL_TYPE_SORT } from "./roll-constants.mjs"
import { PART_CARAC } from "./roll-part-carac.mjs"
import { PART_COMP } from "./roll-part-comp.mjs"
import { ROLLDIALOG_SECTION } from "./roll-part.mjs"
import { TMRUtility } from "../tmr-utility.js"
import { RdDItemSort } from "../item-sort.js"
import { RollPartSelect } from "./roll-part-select.mjs"
import { CARACS } from "../rdd-carac.js"
export const PART_SORT = "sort"
export class RollPartSort extends RollPartSelect {
onReady() {
// TODO: utiliser un hook pour écouter les déplacements dans les TMRs?
}
get code() { return PART_SORT }
get section() { return ROLLDIALOG_SECTION.CHOIX }
isValid(rollData) { return rollData.active.actor.isPersonnage() && rollData.active.actor.isHautRevant() }
visible(rollData) { return this.isRollType(rollData, ROLL_TYPE_SORT) }
loadRefs(rollData) {
const refs = this.getRefs(rollData)
const coord = rollData.active.actor.system.reve.tmrpos.coord
const draconics = rollData.active.actor.getDraconics()
const sorts = rollData.active.actor.itemTypes[ITEM_TYPES.sort]
.map(s => RollPartSort.$extractSort(s, coord, draconics))
foundry.utils.mergeObject(refs,
{
coord: coord,
tmr: TMRUtility.getTMR(coord),
reve: rollData.active.actor.system.reve.reve.value,
draconics: draconics,
all: sorts,
sorts: sorts.filter(it => RdDItemSort.isSortOnCoord(it.sort, coord))
},
{ inplace: true }
)
if (refs.sorts.length > 0) {
this.$selectSort(rollData)
}
}
choices(refs) { return refs.sorts }
static $extractSort(sort, coord, draconics) {
const isDiffVariable = RdDItemSort.isDifficulteVariable(sort)
const isReveVariable = RdDItemSort.isCoutVariable(sort)
return {
key: sort.id,
label: sort.name,
value: isDiffVariable ? -7 : parseInt(sort.system.difficulte),
ptreve: isReveVariable ? 1 : sort.system.ptreve,
caseTMR: RdDItemSort.getCaseTMR(sort),
isDiffVariable: isDiffVariable,
isReveVariable: isReveVariable,
isReserve: false,
sort: sort,
draconics: RdDItemSort.getDraconicsSort(draconics, sort).map(it => it.name)
}
}
getAjustements(rollData) {
const current = this.getCurrent(rollData)
if (current) {
const reserve = current.isReserve ?
[{ label: `Mise en réserve en ${current.coord}` }] : []
const bonusCase = current.bonusCase ?
[{ label: `Bonus case +${current.bonusCase}%` }] : []
return [
{ label: current.label, diff: current.value },
{ label: `r${current.ptreve}` },
...bonusCase,
...reserve
]
}
return []
}
$selectSort(rollData, key) {
const previous = this.getCurrent(rollData)
const current = this.selectByKey(rollData, key, -7)
if (current.key != previous.key) { }
current.bonusCase = RdDItemSort.getCaseBonus(current.sort,
rollData.active.actor.system.reve.tmrpos.coord)
}
async _onRender(rollDialog, context, options) {
const current = this.getCurrent(rollDialog.rollData)
const selectSort = rollDialog.element.querySelector(`roll-section[name="${this.code}"] select[name="select-sort"]`)
const inputDiff = rollDialog.element.querySelector(`roll-section[name="${this.code}"] input[name="diff-var"]`)
const inputPtReve = rollDialog.element.querySelector(`roll-section[name="${this.code}"] input[name="ptreve-var"]`)
const checkboxReserve = rollDialog.element.querySelector(`roll-section[name="${this.code}"] input[name="reserve"]`)
selectSort.addEventListener("change", e => {
const selectOptions = e.currentTarget.options
const index = selectOptions.selectedIndex
this.$selectSort(rollDialog.rollData, selectOptions[index]?.value)
rollDialog.render()
})
inputDiff?.addEventListener("change", e => {
current.value = parseInt(e.currentTarget.value)
rollDialog.render()
})
inputPtReve?.addEventListener("change", e => {
current.ptreve = parseInt(e.currentTarget.value)
rollDialog.render()
})
checkboxReserve?.addEventListener("change", e => {
current.isReserve = 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.REVE])
case PART_COMP: return part.filterComps(rollData,current.draconics ?? [])
}
}
return undefined
}
}