Finalised levelup selections and propagating to PC

This commit is contained in:
WBHarry 2025-05-29 16:25:08 +02:00
parent 7a12783a8c
commit d7ebeb3b2a
12 changed files with 282 additions and 234 deletions

View file

@ -19,7 +19,7 @@ export default class DhlevelUp extends HandlebarsApplicationMixin(ApplicationV2)
static DEFAULT_OPTIONS = {
classes: ['daggerheart', 'levelup'],
position: { width: 1200, height: 'auto' },
position: { width: 1000, height: 'auto' },
window: {
resizable: true
},
@ -57,31 +57,43 @@ export default class DhlevelUp extends HandlebarsApplicationMixin(ApplicationV2)
});
} else {
const levelSelections = this.levelup.levelSelections;
if (levelSelections.total >= this.levelup.maxSelections) {
// Notification?
if (levelSelections.total + Number(button.dataset.cost) > this.levelup.maxSelections) {
ui.notifications.info(
game.i18n.localize('DAGGERHEART.Application.LevelUp.notifications.info.insufficentAdvancements')
);
this.render();
return;
}
const tier = this.levelup.tiers[button.dataset.tier];
const tierLevels = Object.keys(tier.levels).map(level => Number(level));
const lowestLevelChoice = Object.keys(levelSelections.selections).reduce((currentLowest, key) => {
const level = Number(key);
if (tierLevels.includes(level)) {
if (!currentLowest || level < currentLowest) return level;
}
const nrTiers = Object.keys(this.levelup.tiers).length;
let lowestLevelChoice = null;
for (var tierKey = Number(button.dataset.tier); tierKey <= nrTiers + 1; tierKey++) {
const tier = this.levelup.tiers[tierKey];
lowestLevelChoice = Object.keys(levelSelections.available).reduce((currentLowest, key) => {
const level = Number(key);
if (levelSelections.available[key] >= button.dataset.cost && tier.belongingLevels.includes(level)) {
if (!currentLowest || level < currentLowest) return level;
}
return currentLowest;
}, null);
return currentLowest;
}, null);
if (lowestLevelChoice) break;
}
if (!lowestLevelChoice) {
// Notification?
ui.notifications.info(
game.i18n.localize(
'DAGGERHEART.Application.LevelUp.notifications.info.insufficientTierAdvancements'
)
);
this.render();
return;
}
await this.levelup.updateSource({
[`tiers.${button.dataset.tier}.levels.${lowestLevelChoice}.optionSelections.${button.dataset.option}.${button.dataset.checkboxNr}`]: true
[`tiers.${button.dataset.tier}.levels.${lowestLevelChoice}.optionSelections.${button.dataset.option}.${button.dataset.checkboxNr}`]:
{ selected: true, minCost: button.dataset.cost }
});
}

View file

@ -166,13 +166,26 @@ export default class PCSheet extends DaggerheartSheet(ActorSheetV2) {
_attachPartListeners(partId, htmlElement, options) {
super._attachPartListeners(partId, htmlElement, options);
$(htmlElement).find('.attribute-value').on('change', this.attributeChange.bind(this));
$(htmlElement).find('.tab-selector').on('click', this.tabSwitch.bind(this));
$(htmlElement).find('.level-title.levelup').on('click', this.openLevelUp.bind(this));
$(htmlElement).find('.feature-input').on('change', this.onFeatureInputBlur.bind(this));
$(htmlElement).find('.experience-description').on('change', this.experienceDescriptionChange.bind(this));
$(htmlElement).find('.experience-value').on('change', this.experienceValueChange.bind(this));
$(htmlElement).find('[data-item]').on('change', this.itemUpdate.bind(this));
htmlElement
.querySelectorAll('.attribute-value')
.forEach(element => element.addEventListener('change', this.attributeChange.bind(this)));
htmlElement
.querySelectorAll('.tab-selector')
.forEach(element => element.addEventListener('click', this.tabSwitch.bind(this)));
htmlElement.querySelector('.level-title.levelup')?.addEventListener('click', this.openLevelUp.bind(this));
htmlElement
.querySelectorAll('.feature-input')
.forEach(element => element.addEventListener('change', this.onFeatureInputBlur.bind(this)));
htmlElement
.querySelectorAll('.experience-description')
.forEach(element => element.addEventListener('change', this.experienceDescriptionChange.bind(this)));
htmlElement
.querySelectorAll('.experience-value')
.forEach(element => element.addEventListener('change', this.experienceValueChange.bind(this)));
htmlElement
.querySelectorAll('[data-item]')
.forEach(element => element.addEventListener.on('change', this.itemUpdate.bind(this)));
htmlElement.querySelector('.level-value').addEventListener('change', this.onLevelChange.bind(this));
}
async _prepareContext(_options) {
@ -838,6 +851,11 @@ export default class PCSheet extends DaggerheartSheet(ActorSheetV2) {
await item.update({ [name]: event.currentTarget.value });
}
async onLevelChange(event) {
await this.document.updateLevel(Number(event.currentTarget.value));
this.render();
}
static async deleteItem(_, button) {
const item = await fromUuid($(button).closest('[data-item-id]')[0].dataset.itemId);
await item.delete();