mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-07-22 02:19:54 +02:00
Bulk add items during character creation (#1992)
This commit is contained in:
parent
0fb79b98bb
commit
b884130826
2 changed files with 48 additions and 83 deletions
|
|
@ -524,37 +524,52 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
await createEmbeddedItemWithEffects(this.character, ancestry);
|
// Inner function to create the base item data
|
||||||
await createEmbeddedItemWithEffects(this.character, this.setup.community);
|
async function createEmbeddedItemData(baseData) {
|
||||||
await createEmbeddedItemWithEffects(this.character, this.setup.class);
|
const uuid = baseData.uuid ?? baseData._uuid
|
||||||
await createEmbeddedItemWithEffects(this.character, this.setup.subclass);
|
const data = baseData instanceof Item ? baseData : await foundry.utils.fromUuid(baseData.uuid) ?? baseData;
|
||||||
await createEmbeddedItemsWithEffects(this.character, Object.values(this.setup.domainCards));
|
return {
|
||||||
|
...baseData,
|
||||||
|
id: data.id,
|
||||||
|
uuid: uuid,
|
||||||
|
_uuid: uuid,
|
||||||
|
effects: data.effects?.map(effect => effect.toObject()),
|
||||||
|
flags: baseData.flags ?? data.flags,
|
||||||
|
_stats: {
|
||||||
|
...data._stats,
|
||||||
|
compendiumSource: uuid.startsWith('Compendium.') ? uuid : null,
|
||||||
|
duplicateSource: uuid && !uuid.startsWith('Compendium.') ? uuid : null
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the class first. All other items validate it during pre creation
|
||||||
|
await this.character.createEmbeddedDocuments('Item', [await createEmbeddedItemData(this.setup.class)]);
|
||||||
|
|
||||||
|
// Add the remaining items
|
||||||
|
const newItems = [
|
||||||
|
await createEmbeddedItemData(ancestry),
|
||||||
|
await createEmbeddedItemData(this.setup.community),
|
||||||
|
await createEmbeddedItemData(this.setup.subclass),
|
||||||
|
...(await Promise.all(
|
||||||
|
Object.values(this.setup.domainCards).map(d => createEmbeddedItemData(d))
|
||||||
|
))
|
||||||
|
];
|
||||||
if (this.equipment.armor.uuid)
|
if (this.equipment.armor.uuid)
|
||||||
await createEmbeddedItemWithEffects(this.character, this.equipment.armor, {
|
newItems.push(await createEmbeddedItemData(this.equipment.armor));
|
||||||
...this.equipment.armor,
|
|
||||||
system: { ...this.equipment.armor.system, equipped: true }
|
|
||||||
});
|
|
||||||
if (this.equipment.primaryWeapon.uuid)
|
if (this.equipment.primaryWeapon.uuid)
|
||||||
await createEmbeddedItemWithEffects(this.character, this.equipment.primaryWeapon, {
|
newItems.push(await createEmbeddedItemData(this.equipment.primaryWeapon));
|
||||||
...this.equipment.primaryWeapon,
|
|
||||||
system: { ...this.equipment.primaryWeapon.system, equipped: true }
|
|
||||||
});
|
|
||||||
if (this.equipment.secondaryWeapon.uuid)
|
if (this.equipment.secondaryWeapon.uuid)
|
||||||
await createEmbeddedItemWithEffects(this.character, this.equipment.secondaryWeapon, {
|
newItems.push(await createEmbeddedItemData(this.equipment.secondaryWeapon));
|
||||||
...this.equipment.secondaryWeapon,
|
|
||||||
system: { ...this.equipment.secondaryWeapon.system, equipped: true }
|
|
||||||
});
|
|
||||||
if (this.equipment.inventory.choiceA.uuid)
|
if (this.equipment.inventory.choiceA.uuid)
|
||||||
await createEmbeddedItemWithEffects(this.character, this.equipment.inventory.choiceA);
|
newItems.push(await createEmbeddedItemData(this.equipment.inventory.choiceA));
|
||||||
if (this.equipment.inventory.choiceB.uuid)
|
if (this.equipment.inventory.choiceB.uuid)
|
||||||
await createEmbeddedItemWithEffects(this.character, this.equipment.inventory.choiceB);
|
newItems.push(await createEmbeddedItemData(this.equipment.inventory.choiceB));
|
||||||
|
for (const item of this.setup.class.system.inventory.take.filter(x => x)) {
|
||||||
await createEmbeddedItemsWithEffects(
|
newItems.push(await createEmbeddedItemData(item));
|
||||||
this.character,
|
}
|
||||||
this.setup.class.system.inventory.take.filter(x => x)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
await this.character.createEmbeddedDocuments('Item', newItems);
|
||||||
await this.character.update(
|
await this.character.update(
|
||||||
{
|
{
|
||||||
system: {
|
system: {
|
||||||
|
|
@ -587,26 +602,14 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl
|
||||||
const item = await foundry.utils.fromUuid(data.uuid);
|
const item = await foundry.utils.fromUuid(data.uuid);
|
||||||
if (item.type === 'ancestry' && event.target.closest('.primary-ancestry-card')) {
|
if (item.type === 'ancestry' && event.target.closest('.primary-ancestry-card')) {
|
||||||
this.setup.ancestryName.primary = item.name;
|
this.setup.ancestryName.primary = item.name;
|
||||||
this.setup.primaryAncestry = {
|
this.setup.primaryAncestry = item;
|
||||||
...item,
|
|
||||||
effects: Array.from(item.effects).map(x => x.toObject()),
|
|
||||||
uuid: item.uuid
|
|
||||||
};
|
|
||||||
} else if (item.type === 'ancestry' && event.target.closest('.secondary-ancestry-card')) {
|
} else if (item.type === 'ancestry' && event.target.closest('.secondary-ancestry-card')) {
|
||||||
this.setup.ancestryName.secondary = item.name;
|
this.setup.ancestryName.secondary = item.name;
|
||||||
this.setup.secondaryAncestry = {
|
this.setup.secondaryAncestry = item;
|
||||||
...item,
|
|
||||||
effects: Array.from(item.effects).map(x => x.toObject()),
|
|
||||||
uuid: item.uuid
|
|
||||||
};
|
|
||||||
} else if (item.type === 'community' && event.target.closest('.community-card')) {
|
} else if (item.type === 'community' && event.target.closest('.community-card')) {
|
||||||
this.setup.community = {
|
this.setup.community = item;
|
||||||
...item,
|
|
||||||
effects: Array.from(item.effects).map(x => x.toObject()),
|
|
||||||
uuid: item.uuid
|
|
||||||
};
|
|
||||||
} else if (item.type === 'class' && event.target.closest('.class-card')) {
|
} else if (item.type === 'class' && event.target.closest('.class-card')) {
|
||||||
this.setup.class = { ...item, effects: Array.from(item.effects).map(x => x.toObject()), uuid: item.uuid };
|
this.setup.class = item;
|
||||||
this.setup.subclass = {};
|
this.setup.subclass = {};
|
||||||
this.setup.domainCards = {
|
this.setup.domainCards = {
|
||||||
[foundry.utils.randomID()]: {},
|
[foundry.utils.randomID()]: {},
|
||||||
|
|
@ -619,11 +622,7 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setup.subclass = {
|
this.setup.subclass = item;
|
||||||
...item,
|
|
||||||
effects: Array.from(item.effects).map(x => x.toObject()),
|
|
||||||
uuid: item.uuid
|
|
||||||
};
|
|
||||||
} else if (item.type === 'domainCard' && event.target.closest('.domain-card')) {
|
} else if (item.type === 'domainCard' && event.target.closest('.domain-card')) {
|
||||||
if (!this.setup.class.uuid) {
|
if (!this.setup.class.uuid) {
|
||||||
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.missingClass'));
|
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.missingClass'));
|
||||||
|
|
@ -645,14 +644,14 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setup.domainCards[event.target.closest('.domain-card').dataset.card] = { ...item, uuid: item.uuid };
|
this.setup.domainCards[event.target.closest('.domain-card').dataset.card] = item;
|
||||||
} else if (item.type === 'armor' && event.target.closest('.armor-card')) {
|
} else if (item.type === 'armor' && event.target.closest('.armor-card')) {
|
||||||
if (item.system.tier > 1) {
|
if (item.system.tier > 1) {
|
||||||
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.itemTooHighTier'));
|
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.itemTooHighTier'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.equipment.armor = { ...item, uuid: item.uuid };
|
this.equipment.armor = item;
|
||||||
} else if (item.type === 'weapon' && event.target.closest('.primary-weapon-card')) {
|
} else if (item.type === 'weapon' && event.target.closest('.primary-weapon-card')) {
|
||||||
if (item.system.secondary) {
|
if (item.system.secondary) {
|
||||||
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.notPrimary'));
|
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.notPrimary'));
|
||||||
|
|
@ -668,7 +667,7 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl
|
||||||
this.equipment.secondaryWeapon = {};
|
this.equipment.secondaryWeapon = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.equipment.primaryWeapon = { ...item, uuid: item.uuid };
|
this.equipment.primaryWeapon = item;
|
||||||
} else if (item.type === 'weapon' && event.target.closest('.secondary-weapon-card')) {
|
} else if (item.type === 'weapon' && event.target.closest('.secondary-weapon-card')) {
|
||||||
if (this.equipment.primaryWeapon?.system?.burden === burden.twoHanded.value) {
|
if (this.equipment.primaryWeapon?.system?.burden === burden.twoHanded.value) {
|
||||||
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.primaryIsTwoHanded'));
|
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.primaryIsTwoHanded'));
|
||||||
|
|
@ -685,7 +684,7 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.equipment.secondaryWeapon = { ...item, uuid: item.uuid };
|
this.equipment.secondaryWeapon = item;
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -418,40 +418,6 @@ export function createScrollText(actor, data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createEmbeddedItemWithEffects(actor, baseData, update) {
|
|
||||||
const data = baseData.uuid.startsWith('Compendium') ? await foundry.utils.fromUuid(baseData.uuid) : baseData;
|
|
||||||
const [doc] = await actor.createEmbeddedDocuments('Item', [
|
|
||||||
{
|
|
||||||
...(update ?? data),
|
|
||||||
...baseData,
|
|
||||||
id: data.id,
|
|
||||||
uuid: data.uuid,
|
|
||||||
_uuid: data.uuid,
|
|
||||||
effects: data.effects?.map(effect => effect.toObject()),
|
|
||||||
_stats: {
|
|
||||||
...data._stats,
|
|
||||||
compendiumSource: data.pack ? `Compendium.${data.pack}.Item.${data.id}` : null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
|
|
||||||
return doc;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createEmbeddedItemsWithEffects(actor, baseData) {
|
|
||||||
const effectData = [];
|
|
||||||
for (let d of baseData) {
|
|
||||||
const data = d.uuid.startsWith('Compendium') ? await foundry.utils.fromUuid(d.uuid) : d;
|
|
||||||
effectData.push({
|
|
||||||
...data,
|
|
||||||
id: data.id,
|
|
||||||
uuid: data.uuid,
|
|
||||||
effects: data.effects?.map(effect => effect.toObject())
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await actor.createEmbeddedDocuments('Item', effectData);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function shuffleArray(array) {
|
export function shuffleArray(array) {
|
||||||
let currentIndex = array.length;
|
let currentIndex = array.length;
|
||||||
while (currentIndex != 0) {
|
while (currentIndex != 0) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue