forked from public/foundryvtt-reve-de-dragon
Les compétences de jeu spécifiques (doublon) ne remplacent plus la compétence active si le jet n'est pas un jet de jeu
123 lines
3.5 KiB
JavaScript
123 lines
3.5 KiB
JavaScript
import { ITEM_TYPES } from "../constants.js"
|
|
import { CARACS } from "../rdd-carac.js"
|
|
import { ROLL_TYPE_JEU } from "./roll-constants.mjs"
|
|
import { PART_CARAC } from "./roll-part-carac.mjs"
|
|
import { PART_COMP, RollPartComp } from "./roll-part-comp.mjs"
|
|
import { RollPartSelect } from "./roll-part-select.mjs"
|
|
import { ROLLDIALOG_SECTION } from "./roll-part.mjs"
|
|
import { RdDItem } from "../item.js"
|
|
import { Misc } from "../misc.js"
|
|
|
|
export const PART_JEU = "jeu"
|
|
|
|
export const COMPETENCE_JEU = 'Jeu'
|
|
|
|
export class RollPartJeu extends RollPartSelect {
|
|
|
|
get code() { return PART_JEU }
|
|
get section() { return ROLLDIALOG_SECTION.CHOIX }
|
|
|
|
isValid(rollData) { return rollData.active.actor.isPersonnage() }
|
|
visible(rollData) { return this.isRollType(rollData, ROLL_TYPE_JEU) }
|
|
|
|
loadRefs(rollData) {
|
|
const refs = this.getRefs(rollData)
|
|
const actor = rollData.active.actor
|
|
const compJeu = actor.getCompetence(COMPETENCE_JEU)
|
|
|
|
refs.jeux = actor.itemTypes[ITEM_TYPES.jeu]
|
|
.map(it => RollPartJeu.$extractJeu(it, compJeu))
|
|
|
|
if (refs.jeux.length > 0) {
|
|
this.$selectJeu(rollData)
|
|
}
|
|
}
|
|
|
|
choices(refs) { return refs.jeux }
|
|
|
|
static $extractJeu(jeu, compJeu) {
|
|
const caracs = jeu.system.caraccomp.toLowerCase().split(/[.,:\/-]/).map(it => it.trim())
|
|
const base = RollPartJeu.$getJeuBase(jeu, compJeu, caracs)
|
|
return {
|
|
key: jeu.id,
|
|
label: jeu.name,
|
|
caracs: caracs,
|
|
jeu: jeu,
|
|
value: 0,
|
|
base: base,
|
|
comp: compJeu
|
|
}
|
|
}
|
|
|
|
static $getJeuBase(jeu, comp, caracs) {
|
|
if (jeu.system.base < comp.system.niveau) {
|
|
return undefined
|
|
}
|
|
return new RdDItem({
|
|
id: comp.id,
|
|
name: `Jeu ${Misc.lowerFirst(jeu.name)}`,
|
|
type: ITEM_TYPES.competence,
|
|
img: comp.img,
|
|
system: foundry.utils.mergeObject(
|
|
{
|
|
niveau: jeu.system.base,
|
|
base: jeu.system.base,
|
|
default_carac: caracs.length > 0 ? caracs[0] : CARACS.CHANCE
|
|
},
|
|
comp.system,
|
|
{ overwrite: false }
|
|
)
|
|
})
|
|
}
|
|
|
|
prepareContext(rollData) {
|
|
const current = this.getCurrent(rollData)
|
|
if (rollData.type.current == ROLL_TYPE_JEU && current) {
|
|
rollData.type.opposed = true
|
|
}
|
|
}
|
|
|
|
getAjustements(rollData) { return [] }
|
|
|
|
$selectJeu(rollData, key) {
|
|
this.selectByKey(rollData, key, 0)
|
|
if (rollData.type.current == ROLL_TYPE_JEU) {
|
|
RollPartJeu.forceCompJeu(rollData)
|
|
}
|
|
}
|
|
|
|
static forceCompJeu(rollData) {
|
|
const jeu = rollData.current[PART_JEU].base ?? rollData.current[PART_JEU].comp
|
|
rollData.refs[PART_COMP].comps = [jeu].map(it => RollPartComp.extractComp(it))
|
|
rollData.current[PART_COMP] = rollData.refs[PART_COMP].comps[0]
|
|
}
|
|
|
|
async _onRender(rollDialog, context, options) {
|
|
const selectjeu = rollDialog.element.querySelector(`roll-section[name="${this.code}"] select[name="select-jeu"]`)
|
|
|
|
selectjeu.addEventListener("change", e => {
|
|
const selectOptions = e.currentTarget.options
|
|
const index = selectOptions.selectedIndex
|
|
this.$selectJeu(rollDialog.rollData, selectOptions[index]?.value)
|
|
rollDialog.render()
|
|
})
|
|
}
|
|
|
|
impactOtherPart(part, rollData) {
|
|
if (this.visible(rollData)) {
|
|
const current = this.getCurrent(rollData)
|
|
switch (part.code) {
|
|
case PART_CARAC: return part.filterCaracs(rollData, current.caracs)
|
|
case PART_COMP: return part.filterComps(rollData, [current.comp?.name])
|
|
}
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
getSpecialComp(rollData) {
|
|
const current = this.getCurrent(rollData)
|
|
return current.base ? [current.base] : []
|
|
}
|
|
|
|
}
|