Added DomainCard selection

This commit is contained in:
WBHarry 2025-05-30 14:16:15 +02:00
parent 07c533c82c
commit 66defbffce
18 changed files with 823 additions and 132 deletions

View file

@ -11,13 +11,16 @@ export default class DhlevelUp extends HandlebarsApplicationMixin(ApplicationV2)
const playerLevelupData = actor.system.levelData;
this.levelup = new DhLevelup(DhLevelup.initializeData(this.levelTiers, playerLevelupData, actor.system.level));
this._dragDrop = this._createDragDropHandlers();
}
get title() {
return `${this.actor.name} - Level Up`;
return game.i18n.format('DAGGERHEART.Application.LevelUp.Title', { actor: this.actor.name });
}
static DEFAULT_OPTIONS = {
tag: 'form',
classes: ['daggerheart', 'levelup'],
position: { width: 1000, height: 'auto' },
window: {
@ -25,31 +28,188 @@ export default class DhlevelUp extends HandlebarsApplicationMixin(ApplicationV2)
},
actions: {
save: this.save
}
},
form: {
handler: this.updateForm,
submitOnChange: true,
closeOnSubmit: false
},
dragDrop: [{ dragSelector: null, dropSelector: '.levelup-card-selection .card-preview-container' }]
};
static PARTS = {
form: {
id: 'levelup',
template: 'systems/daggerheart/templates/views/levelup.hbs'
tabs: { template: 'systems/daggerheart/templates/sheets/global/tabs/tab-navigation.hbs' },
advancements: { template: 'systems/daggerheart/templates/views/levelup/tabs/advancements.hbs' },
selections: { template: 'systems/daggerheart/templates/views/levelup/tabs/selections.hbs' },
summary: { template: 'systems/daggerheart/templates/views/levelup/tabs/summary.hbs' }
};
static TABS = {
advancements: {
active: true,
cssClass: '',
group: 'primary',
id: 'advancements',
icon: null,
label: 'DAGGERHEART.Application.LevelUp.Tabs.advancement'
},
selections: {
active: false,
cssClass: '',
group: 'primary',
id: 'selections',
icon: null,
label: 'DAGGERHEART.Application.LevelUp.Tabs.selections'
},
summary: {
active: false,
cssClass: '',
group: 'primary',
id: 'summary',
icon: null,
label: 'DAGGERHEART.Application.LevelUp.Tabs.summary'
}
};
async _prepareContext(_options) {
const context = await super._prepareContext(_options);
context.levelup = this.levelup;
context.tabs = this._getTabs(this.constructor.TABS);
return context;
}
async _preparePartContext(partId, context) {
switch (partId) {
case 'selections':
context.advancementChoices = this.levelup.selectionData.reduce((acc, data) => {
const advancementChoice = {
...data,
path: `tiers.${data.tier}.levels.${data.level}.optionSelections.${data.optionKey}.${data.checkboxNr}.data`
};
if (acc[data.type]) acc[data.type].push(advancementChoice);
else acc[data.type] = [advancementChoice];
return acc;
}, {});
context.newExperiences = this.levelup.allInitialAchievements.newExperiences;
const allDomainCards = {
...context.advancementChoices.domainCard,
...this.levelup.domainCards
};
const allDomainCardValues = Object.values(allDomainCards);
context.domainCards = [];
for (var domainCard of allDomainCardValues) {
const uuid = domainCard.data ?? domainCard.uuid;
const card = uuid ? await foundry.utils.fromUuid(uuid) : { path: domainCard.path };
context.domainCards.push({
...(card.toObject?.() ?? card),
emptySubtext: game.i18n.format(
'DAGGERHEART.Application.LevelUp.Selections.emptyDomainCardHint',
{ level: domainCard.level }
),
limit: domainCard.level
});
}
break;
case 'summary':
const actorArmor = this.actor.system.armor;
const { current: currentLevel, changed: changedLevel } = this.actor.system.levelData.level;
context.levelAchievements = {
statisticIncreases: {
proficiency: {
old: this.actor.system.proficiency,
new: this.actor.system.proficiency + this.levelup.allInitialAchievements.proficiency
},
damageThresholds: {
major: {
old: this.actor.system.damageThresholds.major,
new: this.actor.system.damageThresholds.major + changedLevel - currentLevel
},
severe: {
old: this.actor.system.damageThresholds.severe,
new:
this.actor.system.damageThresholds.severe +
(actorArmor ? changedLevel - currentLevel : (changedLevel - currentLevel) * 2)
},
unarmored: !actorArmor
}
}
};
break;
}
return context;
}
_getTabs(tabs) {
for (const v of Object.values(tabs)) {
v.active = this.tabGroups[v.group] ? this.tabGroups[v.group] === v.id : v.active;
v.cssClass = v.active ? 'active' : '';
}
return tabs;
}
_createDragDropHandlers() {
return this.options.dragDrop.map(d => {
d.callbacks = {
drop: this._onDrop.bind(this)
};
return new foundry.applications.ux.DragDrop.implementation(d);
});
}
_attachPartListeners(partId, htmlElement, options) {
super._attachPartListeners(partId, htmlElement, options);
$(htmlElement).find('.selection-checkbox').on('change', this.selectionClick.bind(this));
htmlElement
.querySelectorAll('.selection-checkbox')
.forEach(element => element.addEventListener('change', this.selectionClick.bind(this)));
this._dragDrop.forEach(d => d.bind(htmlElement));
}
static async updateForm(event, _, formData) {
const { levelup } = foundry.utils.expandObject(formData.object);
await this.levelup.updateSource(levelup);
this.render();
}
async _onDrop(event) {
const data = foundry.applications.ux.TextEditor.getDragEventData(event);
const item = await fromUuid(data.uuid);
if (event.currentTarget.parentElement?.classList?.contains('domain-cards')) {
if (item.type === 'domainCard') {
if (!this.actor.system.class.system.domains.includes(item.system.domain)) {
// Also needs to check for multiclass adding a new domain
ui.notifications.error(
game.i18n.localize('DAGGERHEART.Application.LevelUp.notifications.error.domainCardWrongDomain')
);
return;
}
if (item.system.level > Number(event.currentTarget.dataset.limit)) {
ui.notifications.error(
game.i18n.localize('DAGGERHEART.Application.LevelUp.notifications.error.domainCardToHighLevel')
);
return;
}
const achievementCard = event.currentTarget.dataset.path.startsWith('domainCards');
await this.levelup.updateSource({ [event.currentTarget.dataset.path]: item.uuid });
this.render();
}
}
}
async selectionClick(event) {
event.stopPropagation();
const button = event.currentTarget;
// const advancementSelections = this.getAdvancementSelectionUpdates(button);
if (!button.checked) {
await this.levelup.updateSource({
[`tiers.${button.dataset.tier}.levels.${button.dataset.level}.optionSelections.${button.dataset.option}.-=${button.dataset.checkboxNr}`]:
@ -101,12 +261,7 @@ export default class DhlevelUp extends HandlebarsApplicationMixin(ApplicationV2)
}
static async save() {
await this.actor.update({
'system.levelData': {
'level.current': this.actor.system.levelData.level.changed,
'selections': this.levelup.playerData
}
});
await this.actor.levelUp(this.levelup.selectionData);
this.close();
}