Improved migration code substantially, wrote migrations for more properties

This commit is contained in:
Stefan Zermatten
2021-09-06 17:40:57 +02:00
parent 235560eb44
commit e79b8fda3b
20 changed files with 525 additions and 303 deletions

View File

@@ -5,9 +5,8 @@ import {
InlineCalculationFieldSchema,
} from '/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js';
import {
FieldToComputeSchema,
ComputedOnlyFieldSchema,
ComputedFieldSchema,
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import {
ResourcesSchema,
@@ -60,7 +59,7 @@ let ActionSchema = new SimpleSchema({
},
// Calculation of how many times this action can be used
uses: {
type: FieldToComputeSchema,
type: Object,
optional: true,
},
// Integer of how many times it has already been used
@@ -74,7 +73,7 @@ let ActionSchema = new SimpleSchema({
allowedValues: ['longRest', 'shortRest'],
optional: true,
},
});
}).extend(fieldToCompute('uses'));
const ComputedOnlyActionSchema = new SimpleSchema({
summary: {
@@ -85,10 +84,6 @@ const ComputedOnlyActionSchema = new SimpleSchema({
type: ComputedOnlyInlineCalculationFieldSchema,
optional: true,
},
uses: {
type: ComputedOnlyFieldSchema,
optional: true,
},
resources: {
type: ResourcesComputedOnlySchema,
defaultValue: {},
@@ -99,16 +94,12 @@ const ComputedOnlyActionSchema = new SimpleSchema({
type: Boolean,
optional: true,
},
});
}).extend(computedOnlyField('uses'));
const ComputedActionSchema = new SimpleSchema()
.extend(ActionSchema)
.extend(ComputedOnlyActionSchema)
.extend({
uses: {
type: ComputedFieldSchema,
optional: true,
},
summary: {
type: InlineCalculationFieldSchema,
optional: true,

View File

@@ -1,15 +1,20 @@
import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
const AdjustmentSchema = new SimpleSchema({
// The roll that determines how much to change the attribute
// This can be simplified, but should only compute when activated
amount: {
type: String,
type: Object,
optional: true,
defaultValue: '1',
max: STORAGE_LIMITS.calculation,
},
'amount.calculation': {
type: String,
defaultValue: 1,
},
// Who this adjustment applies to
target: {
@@ -32,22 +37,9 @@ const AdjustmentSchema = new SimpleSchema({
allowedValues: ['set', 'increment'],
defaultValue: 'increment',
},
});
}).extend(fieldToCompute('amount'));
const ComputedOnlyAdjustmentSchema = new SimpleSchema({
amountResult: {
type: SimpleSchema.oneOf(String, Number),
optional: true,
},
amountErrors: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
'amountErrors.$':{
type: ErrorSchema,
},
});
const ComputedOnlyAdjustmentSchema = computedOnlyField('amount');
const ComputedAdjustmentSchema = new SimpleSchema()
.extend(AdjustmentSchema)

View File

@@ -1,6 +1,9 @@
import SimpleSchema from 'simpl-schema';
import { ActionSchema, ComputedOnlyActionSchema } from '/imports/api/properties/Actions.js';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
// Attacks are special instances of actions
@@ -9,10 +12,12 @@ let AttackSchema = new SimpleSchema()
.extend({
// What gets added to the d20 roll
rollBonus: {
type: Object,
optional: true,
},
'rollBonus.calculation': {
type: String,
defaultValue: 'strength.modifier + proficiencyBonus',
optional: true,
max: STORAGE_LIMITS.calculation,
},
// Set better defaults for the action
actionType: {
@@ -29,24 +34,11 @@ let AttackSchema = new SimpleSchema()
type: String,
max: STORAGE_LIMITS.tagLength,
},
});
}).extend(fieldToCompute('rollBonus'));
const ComputedOnlyAttackSchema = new SimpleSchema()
.extend(ComputedOnlyActionSchema)
.extend({
rollBonusResult: {
type: Number,
optional: true,
},
rollBonusErrors: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
'rollBonusErrors.$':{
type: ErrorSchema,
},
});
.extend(computedOnlyField('rollBonus'));
const ComputedAttackSchema = new SimpleSchema()
.extend(AttackSchema)

View File

@@ -1,9 +1,8 @@
import SimpleSchema from 'simpl-schema';
import { Random } from 'meteor/random';
import {
FieldToComputeSchema,
ComputedOnlyFieldSchema,
ComputedFieldSchema,
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
@@ -21,10 +20,10 @@ const AttributeConsumedSchema = new SimpleSchema({
max: STORAGE_LIMITS.variableName,
},
quantity: {
type: FieldToComputeSchema,
type: Object,
optional: true,
},
});
}).extend(fieldToCompute('quantity'));
const ComputedOnlyAttributeConsumedSchema = new SimpleSchema({
available: {
@@ -41,21 +40,11 @@ const ComputedOnlyAttributeConsumedSchema = new SimpleSchema({
optional: true,
max: STORAGE_LIMITS.name,
},
quantity: {
type: ComputedOnlyFieldSchema,
optional: true,
},
});
}).extend(computedOnlyField('quantity'));
const ComputedAttributeConsumedSchema = new SimpleSchema()
.extend(AttributeConsumedSchema)
.extend(ComputedOnlyAttributeConsumedSchema)
.extend({
quantity: {
type: ComputedFieldSchema,
optional: true,
},
});
.extend(ComputedOnlyAttributeConsumedSchema);
export {
AttributeConsumedSchema,

View File

@@ -2,34 +2,53 @@ import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
const FieldToComputeSchema = new SimpleSchema({
// This is required, if we don't have a calculation delete the whole object
calculation: {
type: String,
},
});
// Get schemas that apply fields directly so they can be gracefully extended
// because {type: Schema} fields can't be extended
function fieldToCompute(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
// This is required, if we don't have a calculation delete the whole object
[`${field}.calculation`]: {
type: String,
max: STORAGE_LIMITS.calculation,
},
});
}
const ComputedOnlyFieldSchema = new SimpleSchema({
value: {
type: SimpleSchema.oneOf(String, Number),
optional: true,
},
errors: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
'errors.$':{
type: ErrorSchema,
},
});
function computedOnlyField(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
[`${field}.value`]: {
type: SimpleSchema.oneOf(String, Number),
optional: true,
},
[`${field}.errors`]: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
[`${field}.errors.$`]:{
type: ErrorSchema,
},
});
}
const ComputedFieldSchema = new SimpleSchema()
.extend(FieldToComputeSchema)
.extend(ComputedOnlyFieldSchema)
// This should rarely be used, since the other two will merge correctly when
// uncomputed and computedOnly schemas are merged
function computedField(field){
return computedField(field).extend(computedOnlyField(field));
}
export {
FieldToComputeSchema,
ComputedOnlyFieldSchema,
ComputedFieldSchema
fieldToCompute,
computedOnlyField,
computedField,
};

View File

@@ -1,9 +1,8 @@
import SimpleSchema from 'simpl-schema';
import { Random } from 'meteor/random';
import {
FieldToComputeSchema,
ComputedOnlyFieldSchema,
ComputedFieldSchema,
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import { storedIconsSchema } from '/imports/api/icons/Icons.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
@@ -21,7 +20,7 @@ const ItemConsumedSchema = new SimpleSchema({
optional: true,
},
quantity: {
type: FieldToComputeSchema,
type: Object,
optional: true,
},
itemId: {
@@ -29,17 +28,13 @@ const ItemConsumedSchema = new SimpleSchema({
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
});
}).extend(fieldToCompute('quantity'));
const ComputedOnlyItemConsumedSchema = new SimpleSchema({
available: {
type: Number,
optional: true,
},
quantity: {
type: ComputedOnlyFieldSchema,
optional: true,
},
// This appears both in the computed and uncomputed schema because it can be
// set by both a computation or a form
itemId: {
@@ -62,17 +57,11 @@ const ComputedOnlyItemConsumedSchema = new SimpleSchema({
optional: true,
max: STORAGE_LIMITS.color,
},
})
}).extend(computedOnlyField('quantity'));
const ComputedItemConsumedSchema = new SimpleSchema()
.extend(ItemConsumedSchema)
.extend(ComputedOnlyItemConsumedSchema)
.extend({
quantity: {
type: ComputedFieldSchema,
optional: true,
},
});
.extend(ComputedOnlyItemConsumedSchema);
export {
ItemConsumedSchema,