From 255ac529b317954c0b20427977f53cea899c8141 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 12 Apr 2021 15:35:12 +0200 Subject: [PATCH] Added more default properties to creatures --- app/imports/api/creature/Creatures.js | 21 ++---- .../creatureProperties/methods/equipItem.js | 22 +----- .../methods/getParentRefByTag.js | 13 ++++ .../creature/defaultCharacterProperties.js | 73 +++++++++++++++++++ .../{INVENTORY_TAGS.js => BUILT_IN_TAGS.js} | 4 +- .../characterSheetTabs/InventoryTab.vue | 8 +- 6 files changed, 103 insertions(+), 38 deletions(-) create mode 100644 app/imports/api/creature/creatureProperties/methods/getParentRefByTag.js create mode 100644 app/imports/api/creature/defaultCharacterProperties.js rename app/imports/constants/{INVENTORY_TAGS.js => BUILT_IN_TAGS.js} (53%) diff --git a/app/imports/api/creature/Creatures.js b/app/imports/api/creature/Creatures.js index 7f2de833..bc240371 100644 --- a/app/imports/api/creature/Creatures.js +++ b/app/imports/api/creature/Creatures.js @@ -7,6 +7,7 @@ import SharingSchema from '/imports/api/sharing/SharingSchema.js'; import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js'; import {assertEditPermission} from '/imports/api/sharing/sharingPermissions.js'; import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js'; +import defaultCharacterProperties from '/imports/api/creature/defaultCharacterProperties.js'; import '/imports/api/creature/removeCreature.js'; import '/imports/api/creature/restCreature.js'; @@ -198,24 +199,16 @@ const insertCreature = new ValidatedMethod({ let creatureId = Creatures.insert({ owner: this.userId, }); - CreatureProperties.insert({ - slotTags: ['base'], - quantityExpected: 1, - type: 'propertySlot', - name: 'Base', - description: 'Choose a starting point for your character, this will define the basic setup of your character sheet. Without a base, your sheet will be empty.', - hideWhenFull: true, - parent: {collection: 'creatures', id: creatureId}, - ancestors: [{collection: 'creatures', id: creatureId}], - order: 0, - tags: [], - spaceLeft: 1, - totalFilled: 0, + + // Insert the default properties + // Not batchInsert because we want the properties cleaned by the schema + defaultCharacterProperties(creatureId).forEach(prop => { + CreatureProperties.insert(prop); }); + this.unblock(); return creatureId; }, - }); const updateCreature = new ValidatedMethod({ diff --git a/app/imports/api/creature/creatureProperties/methods/equipItem.js b/app/imports/api/creature/creatureProperties/methods/equipItem.js index ab396c9f..ba1a0444 100644 --- a/app/imports/api/creature/creatureProperties/methods/equipItem.js +++ b/app/imports/api/creature/creatureProperties/methods/equipItem.js @@ -7,23 +7,8 @@ import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/ge import { recomputeCreatureByDoc } from '/imports/api/creature/computation/methods/recomputeCreature.js'; import recomputeInactiveProperties from '/imports/api/creature/denormalise/recomputeInactiveProperties.js'; import recomputeInventory from '/imports/api/creature/denormalise/recomputeInventory.js'; -import INVENTORY_TAGS from '/imports/constants/INVENTORY_TAGS.js'; - -export function getParentRefByTag(creatureId, tag){ - let prop = CreatureProperties.findOne({ - 'ancestors.id': creatureId, - removed: {$ne: true}, - inactive: {$ne: true}, - tags: tag, - }, { - sort: {order: 1}, - }); - if (prop){ - return {id: prop._id, collection: 'creatureProperties'}; - } else { - return {id: creatureId, collection: 'creatures'}; - } -} +import BUILT_IN_TAGS from '/imports/constants/BUILT_IN_TAGS.js'; +import getParentRefByTag from '/imports/api/creature/creatureProperties/methods/getParentRefByTag.js'; // Equipping or unequipping an item will also change its parent const equipItem = new ValidatedMethod({ @@ -50,8 +35,9 @@ const equipItem = new ValidatedMethod({ }, { selector: {type: 'item'}, }); - let tag = equipped ? INVENTORY_TAGS.equipment : INVENTORY_TAGS.carried; + let tag = equipped ? BUILT_IN_TAGS.equipment : BUILT_IN_TAGS.carried; let parentRef = getParentRefByTag(creature._id, tag); + if (!parentRef) parentRef = {id: creature._id, collection: 'creatures'}; organizeDoc.call({ docRef: { diff --git a/app/imports/api/creature/creatureProperties/methods/getParentRefByTag.js b/app/imports/api/creature/creatureProperties/methods/getParentRefByTag.js new file mode 100644 index 00000000..f5236c9f --- /dev/null +++ b/app/imports/api/creature/creatureProperties/methods/getParentRefByTag.js @@ -0,0 +1,13 @@ +import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js'; + +export default function getParentRefByTag(creatureId, tag){ + let prop = CreatureProperties.findOne({ + 'ancestors.id': creatureId, + removed: {$ne: true}, + inactive: {$ne: true}, + tags: tag, + }, { + sort: {order: 1}, + }); + return prop && {id: prop._id, collection: 'creatureProperties'}; +} diff --git a/app/imports/api/creature/defaultCharacterProperties.js b/app/imports/api/creature/defaultCharacterProperties.js new file mode 100644 index 00000000..864555b6 --- /dev/null +++ b/app/imports/api/creature/defaultCharacterProperties.js @@ -0,0 +1,73 @@ +import BUILT_IN_TAGS from '/imports/constants/BUILT_IN_TAGS.js'; + +const properties = [ + { + type: 'propertySlot', + name: 'Base', + description: 'Choose a starting point for your character, this will define the basic setup of your character sheet. Without a base, your sheet will be empty.', + slotTags: ['base'], + tags: [], + quantityExpected: 1, + hideWhenFull: true, + spaceLeft: 1, + totalFilled: 0, + }, { + type: 'folder', + name: 'Inventory', + tags: [BUILT_IN_TAGS.inventory], + }, { + type: 'folder', + name: 'Equipment', + tags: [BUILT_IN_TAGS.equipment], + }, { + type: 'folder', + name: 'Carried', + tags: [BUILT_IN_TAGS.carried], + }, +]; + +export default function defaultCharacterProperties(creatureId){ + if (!creatureId) throw 'creatureId is required'; + const creatureRef = {collection: 'creatures', id: creatureId}; + let randomSrc = DDP.randomStream('defaultProperties'); + const inventoryId = randomSrc.id(); + const inventoryRef = {collection: 'creatureProperties', id: inventoryId}; + return [ + { + type: 'propertySlot', + name: 'Base', + description: 'Choose a starting point for your character, this will define the basic setup of your character sheet. Without a base, your sheet will be empty.', + slotTags: ['base'], + tags: [], + quantityExpected: 1, + hideWhenFull: true, + spaceLeft: 1, + totalFilled: 0, + order: 0, + parent: creatureRef, + ancestors: [creatureRef], + }, { + _id: inventoryId, + type: 'folder', + name: 'Inventory', + tags: [BUILT_IN_TAGS.inventory], + order: 1, + parent: creatureRef, + ancestors: [creatureRef], + }, { + type: 'folder', + name: 'Equipment', + tags: [BUILT_IN_TAGS.equipment], + order: 2, + parent: inventoryRef, + ancestors: [creatureRef, inventoryRef], + }, { + type: 'folder', + name: 'Carried', + tags: [BUILT_IN_TAGS.carried], + order: 3, + parent: inventoryRef, + ancestors: [creatureRef, inventoryRef], + }, + ]; +} diff --git a/app/imports/constants/INVENTORY_TAGS.js b/app/imports/constants/BUILT_IN_TAGS.js similarity index 53% rename from app/imports/constants/INVENTORY_TAGS.js rename to app/imports/constants/BUILT_IN_TAGS.js index 25a00cc9..bdfee9ed 100644 --- a/app/imports/constants/INVENTORY_TAGS.js +++ b/app/imports/constants/BUILT_IN_TAGS.js @@ -1,7 +1,7 @@ -const INVENTORY_TAGS = Object.freeze({ +const BUILT_IN_TAGS = Object.freeze({ inventory: 'inventory', equipment: 'equipment', carried: 'carried', }); -export default INVENTORY_TAGS; +export default BUILT_IN_TAGS; diff --git a/app/imports/ui/creature/character/characterSheetTabs/InventoryTab.vue b/app/imports/ui/creature/character/characterSheetTabs/InventoryTab.vue index 5e8104a0..7833cd37 100644 --- a/app/imports/ui/creature/character/characterSheetTabs/InventoryTab.vue +++ b/app/imports/ui/creature/character/characterSheetTabs/InventoryTab.vue @@ -104,8 +104,8 @@ import ColumnLayout from '/imports/ui/components/ColumnLayout.vue'; import ContainerCard from '/imports/ui/properties/components/inventory/ContainerCard.vue'; import ToolbarCard from '/imports/ui/components/ToolbarCard.vue'; import ItemList from '/imports/ui/properties/components/inventory/ItemList.vue'; -import { getParentRefByTag } from '/imports/api/creature/creatureProperties/methods/equipItem.js'; -import INVENTORY_TAGS from '/imports/constants/INVENTORY_TAGS.js'; +import getParentRefByTag from '/imports/api/creature/creatureProperties/methods/getParentRefByTag.js'; +import BUILT_IN_TAGS from '/imports/constants/BUILT_IN_TAGS.js'; import CoinValue from '/imports/ui/components/CoinValue.vue'; export default { @@ -183,10 +183,10 @@ export default { }); }, equipmentParentRef(){ - return getParentRefByTag(this.creatureId, INVENTORY_TAGS.equipment); + return getParentRefByTag(this.creatureId, BUILT_IN_TAGS.equipment); }, carriedParentRef(){ - return getParentRefByTag(this.creatureId, INVENTORY_TAGS.carried); + return getParentRefByTag(this.creatureId, BUILT_IN_TAGS.carried); }, }, computed: {