forked from public/foundryvtt-reve-de-dragon
Avancement messages défense & oeuvres
This commit is contained in:
@@ -38,7 +38,8 @@ import { RollPartDefense } from "./roll-part-defense.mjs";
|
||||
import { RollDialogAdapter } from "./roll-dialog-adapter.mjs";
|
||||
import { ROLLDIALOG_SECTION } from "./roll-part.mjs";
|
||||
import { ROLL_TYPE_COMP } from "./roll-constants.mjs";
|
||||
import { RollChatResult } from "./roll-chat-result.mjs";
|
||||
import { ChatRollResult } from "./chat-roll-result.mjs";
|
||||
|
||||
|
||||
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api
|
||||
const doNothing = (dialog) => { }
|
||||
@@ -48,11 +49,12 @@ const ALL_ROLL_TYPES = [
|
||||
new RollTypeTache(),
|
||||
new RollTypeAttaque(),
|
||||
new RollTypeDefense(),
|
||||
// new RollTypeResistance ??
|
||||
new RollTypeSort(),
|
||||
new RollTypeMeditation(),
|
||||
new RollTypeOeuvre(),
|
||||
new RollTypeJeu(),
|
||||
// new RollTypeResistance ??
|
||||
// new RollTypeFixedCarac ??
|
||||
]
|
||||
|
||||
const BASIC_PARTS = new RollBasicParts()
|
||||
@@ -182,6 +184,8 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
'roll-button': 'systems/foundryvtt-reve-de-dragon/templates/roll/roll-button.hbs',
|
||||
})
|
||||
|
||||
ChatRollResult.onReady()
|
||||
|
||||
foundry.applications.handlebars.loadTemplates(ALL_ROLL_TYPES.map(m => m.template))
|
||||
foundry.applications.handlebars.loadTemplates(ROLL_PARTS.map(p => p.template))
|
||||
ROLL_PARTS.forEach(p => p.onReady())
|
||||
@@ -246,9 +250,49 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
// rien pour l'instant
|
||||
}
|
||||
|
||||
/** pre-configure les paramètres des différentes parties de la fenêtre (par exemple, prépare les listes de caractéristiques/compétences */
|
||||
static $prepareRollData(rollData) {
|
||||
rollData.current = rollData.current ?? {}
|
||||
rollData.selected = rollData.selected ?? {}
|
||||
rollData.type = rollData.type ?? {}
|
||||
rollData.type.retry = rollData.type.retry ?? false
|
||||
BASIC_PARTS.restore(rollData)
|
||||
|
||||
const potential = ALL_ROLL_TYPES.find(m => m.code == rollData.type.current)?.code
|
||||
const allowed = rollData.type.retry && potential
|
||||
? [potential]
|
||||
: (rollData.type.allowed ?? ALL_ROLL_TYPES.filter(m => m.isAllowed(rollData) && m.visible(rollData)).map(m => m.code))
|
||||
const rollType = allowed.find(c => c == rollData.type.current) ?? (allowed.length > 0 ? allowed[0].code : ROLL_TYPE_COMP);
|
||||
|
||||
rollData.type.allowed = allowed
|
||||
rollData.type.current = rollType
|
||||
ALL_ROLL_TYPES.find(m => m.code == rollType).setRollDataType(rollData)
|
||||
|
||||
rollData.refs = foundry.utils.mergeObject(rollData.refs ?? {}, Object.fromEntries(ROLL_PARTS.map(p => [p.code, {}])));
|
||||
rollData.options = rollData.options ?? { rollMode: game.settings.get("core", "rollMode") }
|
||||
|
||||
ROLL_PARTS.forEach(p => p.initialize(rollData))
|
||||
ROLL_PARTS.forEach(p => p.restore(rollData))
|
||||
ROLL_PARTS.filter(p => p.isValid(rollData))
|
||||
.forEach(p => {
|
||||
p.loadRefs(rollData)
|
||||
p.prepareContext(rollData)
|
||||
})
|
||||
return rollData
|
||||
}
|
||||
|
||||
static saveParts(rollData) {
|
||||
const target = BASIC_PARTS.initFrom(rollData)
|
||||
ROLL_PARTS.filter(p => p.isActive(rollData))
|
||||
.forEach(p => p.storeClean(rollData, target))
|
||||
target.attackerRoll = rollData.attackerRoll
|
||||
return target
|
||||
}
|
||||
|
||||
constructor(rollData, rollOptions) {
|
||||
super()
|
||||
this.rollData = rollData
|
||||
|
||||
this.rollData = RollDialog.$prepareRollData(rollData)
|
||||
this.rollOptions = {
|
||||
callbacks: [
|
||||
async r => await r.active.actor.appliquerAjoutExperience(r),
|
||||
@@ -258,37 +302,8 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
customChatMessage: rollOptions.customChatMessage,
|
||||
onRollDone: rollOptions.onRollDone ?? doNothing
|
||||
}
|
||||
this.$loadParts()
|
||||
}
|
||||
|
||||
/** pre-configure les paramètres des différentes parties de la fenêtre (par exemple, prépare les listes de caractéristiques/compétences */
|
||||
$loadParts() {
|
||||
const rollData = this.rollData;
|
||||
rollData.current = rollData.current ?? {}
|
||||
rollData.selected = rollData.selected ?? {}
|
||||
rollData.type = rollData.type ?? {}
|
||||
rollData.type.retry = rollData.type.retry ?? false
|
||||
BASIC_PARTS.restore(rollData)
|
||||
|
||||
const loadedType = ALL_ROLL_TYPES.find(m => m.code == rollData.type?.current)?.code
|
||||
const allowedTypes = ALL_ROLL_TYPES.filter(m => m.isAllowed(rollData) && m.visible(rollData)).map(m => m.code)
|
||||
|
||||
rollData.type.allowed = rollData.type.retry ? [loadedType] : rollData.type.allowed ?? ALL_ROLL_TYPES.map(m => m.code)
|
||||
rollData.type.current = allowedTypes.find(m => m == rollData.type?.current) ?? (allowedTypes.length > 0 ? allowedTypes[0] : ROLL_TYPE_COMP)
|
||||
|
||||
this.getSelectedType().setRollDataType(rollData)
|
||||
|
||||
rollData.refs = this.$prepareRefs(rollData)
|
||||
rollData.options = rollData.options ?? { showDice: true, rollMode: game.settings.get("core", "rollMode") }
|
||||
|
||||
ROLL_PARTS.forEach(p => p.initialize(rollData))
|
||||
ROLL_PARTS.forEach(p => p.restore(rollData))
|
||||
ROLL_PARTS.filter(p => p.isValid(rollData))
|
||||
.forEach(p => {
|
||||
p.loadRefs(rollData)
|
||||
p.prepareContext(rollData)
|
||||
})
|
||||
this.selectType();
|
||||
this.chatRollResult = new ChatRollResult();
|
||||
this.selectType()
|
||||
}
|
||||
|
||||
selectType() {
|
||||
@@ -301,23 +316,21 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
ROLL_PARTS.find(it => it.code == PART_COMP).filterComps(this.rollData)
|
||||
}
|
||||
|
||||
$prepareRefs(rollData) {
|
||||
return foundry.utils.mergeObject(rollData.refs ?? {}, Object.fromEntries(ROLL_PARTS.map(p => [p.code, {}])));
|
||||
}
|
||||
|
||||
$saveParts() {
|
||||
const target = BASIC_PARTS.initFrom(this.rollData)
|
||||
ROLL_PARTS.filter(p => p.isActive(this.rollData))
|
||||
.forEach(p => p.store(this.rollData, target))
|
||||
return target
|
||||
}
|
||||
|
||||
getActiveParts() {
|
||||
return ROLL_PARTS.filter(p => p.isActive(this.rollData))
|
||||
}
|
||||
|
||||
get title() {
|
||||
return this.rollData.title ?? `Jet de dés de ${this.rollData.active.actor.name}`
|
||||
// get title() {
|
||||
// return this.rollData.title ?? `Jet de dés de ${this.rollData.active.actor.name}`
|
||||
// }
|
||||
|
||||
rollTitle(rollData) {
|
||||
return rollData.label ?? ROLL_PARTS
|
||||
.filter(it => it.section == ROLLDIALOG_SECTION.ACTION)
|
||||
.filter(it => it.isActive(rollData))
|
||||
.map(it => it.title(rollData))
|
||||
.reduce(Misc.joining(' '))
|
||||
}
|
||||
|
||||
async _onRender(context, options) {
|
||||
@@ -344,9 +357,9 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
)
|
||||
}
|
||||
|
||||
getAjustements() {
|
||||
getAjustements(rollData = this.rollData) {
|
||||
return this.getActiveParts()
|
||||
.map(p => p.getAjustements(this.rollData))
|
||||
.map(p => p.getAjustements(rollData))
|
||||
.reduce((a, b) => a.concat(b))
|
||||
.sort((a, b) => a.diff == undefined ? 1 : b.diff == undefined ? -1 : 0)
|
||||
}
|
||||
@@ -392,10 +405,10 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
}
|
||||
}
|
||||
|
||||
calculAjustements() {
|
||||
this.rollData.ajustements = this.getAjustements()
|
||||
this.rollData.ajustements.forEach(it => it.isDiff = it.diff != undefined)
|
||||
this.rollData.current.totaldiff = this.rollData.ajustements
|
||||
calculAjustements(rollData = this.rollData) {
|
||||
rollData.ajustements = this.getAjustements(rollData)
|
||||
rollData.ajustements.forEach(it => it.isDiff = it.diff != undefined)
|
||||
rollData.current.totaldiff = rollData.ajustements
|
||||
.map(adj => adj.diff)
|
||||
.filter(d => d != undefined)
|
||||
.reduce(Misc.sum(), 0)
|
||||
@@ -406,36 +419,27 @@ export default class RollDialog extends HandlebarsApplicationMixin(ApplicationV2
|
||||
}
|
||||
|
||||
async roll() {
|
||||
this.calculAjustements()
|
||||
const rollData = this.rollData
|
||||
console.info('Roll parts:', this.$saveParts())
|
||||
const rolled = await this.$rollDice(rollData)
|
||||
rollData.rolled = rolled
|
||||
Promise.all(this.rollOptions.callbacks.map(async callback => await callback(rollData)))
|
||||
if (!this.rollOptions.customChatMessage) {
|
||||
const rollChatResult = new RollChatResult(this.getSelectedType())
|
||||
await rollChatResult.display(this.rollData)
|
||||
rollData.active.actor.$onRollCompetence(this.rollData)
|
||||
}
|
||||
|
||||
const roll = RollDialog.saveParts(this.rollData)
|
||||
RollDialog.$prepareRollData(roll)
|
||||
this.calculAjustements(roll)
|
||||
roll.current.resultat = this.rollData.current.resultat
|
||||
roll.v2 = true
|
||||
roll.rolled = await this.$rollDice(roll)
|
||||
roll.result = this.getSelectedType(roll).getResult(roll)
|
||||
console.info('RollDialog.roll:', roll)
|
||||
await Promise.all(this.rollOptions.callbacks.map(async callback => await callback(roll)))
|
||||
await this.chatRollResult.display(roll)
|
||||
|
||||
this.rollOptions.onRollDone(this)
|
||||
}
|
||||
|
||||
|
||||
async defaultCallback(rollData, rolled) {
|
||||
await rollData.active.actor.appliquerAjoutExperience(rollData)
|
||||
await rollData.active.actor.appliquerAppelMoral(rollData)
|
||||
}
|
||||
|
||||
async $rollDice(rollData) {
|
||||
const adapter = new RollDialogAdapter(ROLL_PARTS);
|
||||
return await adapter.rollDice(rollData, this.rollTitle(rollData));
|
||||
}
|
||||
|
||||
rollTitle(rollData) {
|
||||
return ROLL_PARTS
|
||||
.filter(it => it.section == ROLLDIALOG_SECTION.ACTION)
|
||||
.filter(it => it.isActive(rollData))
|
||||
.map(it => it.title(rollData))
|
||||
.reduce(Misc.joining(' '))
|
||||
return await RollDialogAdapter.rollDice(rollData, this.rollTitle(rollData));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user