mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-12 03:31:07 +01:00
* Action Roll DiceSet type * Fix * Remove console log * Tmp fix for non consistent resource * fix
119 lines
4.3 KiB
JavaScript
119 lines
4.3 KiB
JavaScript
const { ApplicationV2, HandlebarsApplicationMixin } = foundry.applications.api;
|
|
|
|
export default class D20RollDialog extends HandlebarsApplicationMixin(ApplicationV2) {
|
|
constructor(roll, config = {}, options = {}) {
|
|
super(options);
|
|
|
|
this.roll = roll;
|
|
this.config = config;
|
|
this.config.experiences = [];
|
|
|
|
if (config.source?.action) {
|
|
this.item = config.data.parent.items.get(config.source.item);
|
|
this.action =
|
|
config.data.attack?._id == config.source.action
|
|
? config.data.attack
|
|
: this.item.system.actions.find(a => a._id === config.source.action);
|
|
}
|
|
}
|
|
|
|
static DEFAULT_OPTIONS = {
|
|
tag: 'form',
|
|
id: 'roll-selection',
|
|
classes: ['daggerheart', 'views', 'roll-selection'],
|
|
position: {
|
|
width: 400,
|
|
height: 'auto'
|
|
},
|
|
actions: {
|
|
updateIsAdvantage: this.updateIsAdvantage,
|
|
selectExperience: this.selectExperience,
|
|
submitRoll: this.submitRoll
|
|
},
|
|
form: {
|
|
handler: this.updateRollConfiguration,
|
|
submitOnChange: true,
|
|
submitOnClose: false
|
|
}
|
|
};
|
|
|
|
/** @override */
|
|
static PARTS = {
|
|
costSelection: {
|
|
id: 'costSelection',
|
|
template: 'systems/daggerheart/templates/views/costSelection.hbs'
|
|
},
|
|
rollSelection: {
|
|
id: 'rollSelection',
|
|
template: 'systems/daggerheart/templates/views/rollSelection.hbs'
|
|
}
|
|
};
|
|
|
|
async _prepareContext(_options) {
|
|
const context = await super._prepareContext(_options);
|
|
context.hasRoll = !!this.config.roll;
|
|
context.experiences = Object.keys(this.config.data.experiences).map(id => ({
|
|
id,
|
|
...this.config.data.experiences[id]
|
|
}));
|
|
context.selectedExperiences = this.config.experiences;
|
|
context.advantage = this.config.advantage;
|
|
/* context.diceOptions = this.diceOptions; */
|
|
context.canRoll = true;
|
|
context.isLite = this.config.roll?.lite;
|
|
if (this.config.costs?.length) {
|
|
const updatedCosts = this.action.calcCosts(this.config.costs);
|
|
context.costs = updatedCosts;
|
|
context.canRoll = this.action.hasCost(updatedCosts);
|
|
this.config.data.scale = this.config.costs[0].total;
|
|
}
|
|
if (this.config.uses?.max) {
|
|
context.uses = this.action.calcUses(this.config.uses);
|
|
context.canRoll = context.canRoll && this.action.hasUses(context.uses);
|
|
}
|
|
context.formula = this.roll.constructFormula(this.config);
|
|
return context;
|
|
}
|
|
|
|
static updateRollConfiguration(event, _, formData) {
|
|
const { ...rest } = foundry.utils.expandObject(formData.object);
|
|
if (this.config.costs) {
|
|
this.config.costs = foundry.utils.mergeObject(this.config.costs, rest.costs);
|
|
}
|
|
if (this.config.uses) this.config.uses = foundry.utils.mergeObject(this.config.uses, rest.uses);
|
|
this.render();
|
|
}
|
|
|
|
static updateIsAdvantage(_, button) {
|
|
const advantage = Number(button.dataset.advantage);
|
|
this.config.advantage = this.config.advantage === advantage ? 0 : advantage;
|
|
this.render();
|
|
}
|
|
|
|
static selectExperience(_, button) {
|
|
/* if (this.config.experiences.find(x => x === button.dataset.key)) {
|
|
this.config.experiences = this.config.experiences.filter(x => x !== button.dataset.key);
|
|
} else {
|
|
this.config.experiences = [...this.config.experiences, button.dataset.key];
|
|
} */
|
|
this.config.experiences = this.config.experiences.indexOf(button.dataset.key) > -1 ? this.config.experiences.filter(x => x !== button.dataset.key) : [...this.config.experiences, button.dataset.key];
|
|
this.render();
|
|
}
|
|
|
|
static async submitRoll() {
|
|
await this.close({ submitted: true });
|
|
}
|
|
|
|
/** @override */
|
|
_onClose(options = {}) {
|
|
if (!options.submitted) this.config = false;
|
|
}
|
|
|
|
static async configure(roll, config = {}, options={}) {
|
|
return new Promise(resolve => {
|
|
const app = new this(roll, config, options);
|
|
app.addEventListener('close', () => resolve(app.config), { once: true });
|
|
app.render({ force: true });
|
|
});
|
|
}
|
|
}
|