Updated Armor SRD

This commit is contained in:
WBHarry 2026-02-10 21:44:22 +01:00
parent 47b16392eb
commit b9e3eec34c
42 changed files with 1520 additions and 23 deletions

View file

@ -7,3 +7,4 @@ export { default as DhFearTracker } from './fearTracker.mjs';
export { default as DhHotbar } from './hotbar.mjs';
export { default as DhSceneNavigation } from './sceneNavigation.mjs';
export { ItemBrowser } from './itemBrowser.mjs';
export { default as DhProgress } from './progress.mjs';

View file

@ -0,0 +1,27 @@
export default class DhProgress {
#notification;
constructor({ max, label = '' }) {
this.max = max;
this.label = label;
this.#notification = ui.notifications.info(this.label, { progress: true });
}
updateMax(newMax) {
this.max = newMax;
}
advance({ by = 1, label = this.label } = {}) {
if (this.value === this.max) return;
this.value += Math.abs(by);
this.#notification.update({ message: label, pct: this.value / this.max });
}
close({ label = '' } = {}) {
this.#notification.update({ message: label, pct: 1 });
}
static createMigrationProgress(max = 0) {
return new DhProgress({ max, label: game.i18n.localize('DAGGERHEART.UI.Progress.migrationLabel') });
}
}

View file

@ -107,9 +107,10 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
}
async updateArmorMax(newMax) {
const { effect, ...baseChange } = this.armorChange;
const newChanges = [
{
...this.armorChange,
...baseChange,
max: newMax,
value: Math.min(this.armorChange.value, newMax)
}
@ -152,7 +153,7 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
armorChange.key = 'system.armorScore';
}
static getDefaultEffectData() {
static getDefaultObject() {
return {
type: 'armor',
name: game.i18n.localize('DAGGERHEART.EFFECTS.Armor.newArmorEffect'),
@ -160,13 +161,6 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
};
}
async _preCreate(data, options, user) {
const allowed = await super._preCreate(data, options, user);
if (allowed === false) return;
await this.updateSource({ ...ArmorEffect.getDefaultEffectData(), data });
}
async _preUpdate(changes, options, user) {
const allowed = await super._preUpdate(changes, options, user);
if (allowed === false) return false;

View file

@ -85,7 +85,7 @@ export default class DHArmor extends AttachableItem {
if (!this.parent.effects.some(x => x.type === 'armor')) {
this.parent.createEmbeddedDocuments('ActiveEffect', [
game.system.api.data.activeEffects.ArmorEffect.getDefaultEffectData()
game.system.api.data.activeEffects.ArmorEffect.getDefaultObject()
]);
}
}
@ -175,6 +175,25 @@ export default class DHArmor extends AttachableItem {
}
}
/** @inheritDoc */
static migrateDocumentData(source) {
if (source.system.baseScore !== undefined && !source.effects.some(x => x.type === 'armor')) {
// source.effects.push({
// ...game.system.api.data.activeEffects.ArmorEffect.getDefaultObject(),
// changes: [{
// type: CONFIG.DH.GENERAL.activeEffectModes.armor.id,
// phase: 'initial',
// priority: 20,
// value: 0,
// max: source.system.baseScore
// }],
// });
if (!source.flags) source.flags = {};
if (!source.flags.daggerheart) source.flags.daggerheart = {};
source.flags.daggerheart.baseScoreMigrationValue = source.system.baseScore;
}
}
/**
* Generates a list of localized tags based on this item's type-specific properties.
* @returns {string[]} An array of localized tag strings.

View file

@ -230,4 +230,14 @@ export default class DHItem extends foundry.documents.Item {
async _preDelete() {
this.deleteTriggers();
}
/** @inheritDoc */
static migrateData(source) {
const documentClass = game.system.api.data.items[`DH${source.type.capitalize()}`];
if (documentClass?.migrateDocumentData) {
documentClass.migrateDocumentData(source);
}
return super.migrateData(source);
}
}

View file

@ -246,6 +246,106 @@ export async function runMigrations() {
lastMigrationVersion = '1.6.0';
}
if (foundry.utils.isNewerVersion('2.0.0', lastMigrationVersion)) {
/* Migrate existing armors to the new Armor Effects */
const progress = game.system.api.applications.ui.DhProgress.createMigrationProgress(0);
const lockedPacks = [];
const itemPacks = game.packs.filter(x => x.metadata.type === 'Item');
const actorPacks = game.packs.filter(x => x.metadata.type === 'Actor');
const getIndexes = async (packs, type) => {
const indexes = [];
for (const pack of packs) {
const indexValues = pack.index.values().reduce((acc, index) => {
if (index.type === type) acc.push(index.uuid);
return acc;
}, []);
if (indexValues.length && pack.locked) {
lockedPacks.push(pack.collection);
await pack.configure({ locked: false });
}
indexes.push(...indexValues);
}
return indexes;
};
const armorEntries = await getIndexes(itemPacks, 'armor');
const actorEntries = await getIndexes(actorPacks, 'actor');
const worldArmors = game.items.filter(x => x instanceof game.system.api.documents.DHItem && x.type === 'armor');
for (const character of game.actors.filter(x => x.type === 'character')) {
worldArmors.push(...character.items.filter(x => x.type === 'armor'));
}
/* The async fetches are the mainstay of time. Leaving 1 progress for the sync logic */
const newMax = armorEntries.length + actorEntries.length + 1;
progress.updateMax(newMax);
const compendiumArmors = [];
for (const entry of armorEntries) {
const armor = await foundry.utils.fromUuid(entry);
compendiumArmors.push(armor);
progress.advance();
}
for (const entry of actorEntries) {
const actor = await foundry.utils.fromUuid(entry);
compendiumArmors.push(...actor.items.filter(x => x.type === 'armor'));
progress.advance();
}
// const lockedPacks = [];
// const compendiumArmors = [];
// const compendiumCharacters = [];
// for (let pack of game.packs.filter(x => x.metadata.type === 'Item')) {
// if (pack.locked) {
// lockedPacks.push(pack.collection);
// await pack.configure({ locked: false });
// }
// const documents = await pack.getDocuments({ type: 'armor' });
// compendiumArmors.push(...documents.filter(x => x instanceof game.system.api.documents.DHItem && x.type === 'armor'));
// compendiumCharacters.push(...documents.filter(x => x.type === 'character'));
// }
for (const armor of [...compendiumArmors, ...worldArmors]) {
const hasArmorEffect = armor.effects.some(x => x.type === 'armor');
const migrationArmorScore = armor.flags.daggerheart?.baseScoreMigrationValue;
if (migrationArmorScore !== undefined && !hasArmorEffect) {
await armor.createEmbeddedDocuments('ActiveEffect', [
{
...game.system.api.data.activeEffects.ArmorEffect.getDefaultObject(),
changes: [
{
type: CONFIG.DH.GENERAL.activeEffectModes.armor.id,
phase: 'initial',
priority: 20,
value: 0,
max: migrationArmorScore
}
]
}
]);
}
}
progress.advance();
for (let packId of lockedPacks) {
const pack = game.packs.get(packId);
await pack.configure({ locked: true });
}
progress.close();
// lastMigrationVersion = '2.0.0';
}
//#endregion
await game.settings.set(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LastMigrationVersion, lastMigrationVersion);