Iterated on migration :(

This commit is contained in:
Stefan Zermatten
2021-09-06 23:59:52 +02:00
parent e79b8fda3b
commit b0980d26ac
8 changed files with 137 additions and 88 deletions

View File

@@ -1,18 +1,10 @@
import SimpleSchema from 'simpl-schema';
import {
InlineCalculationFieldToComputeSchema,
ComputedOnlyInlineCalculationFieldSchema,
InlineCalculationFieldSchema,
} from '/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js';
import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import {
ResourcesSchema,
ResourcesComputedOnlySchema,
ResourcesComputedSchema,
} from '/imports/api/properties/subSchemas/ResourcesSchema.js';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
/*
@@ -21,18 +13,18 @@ import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
* Any actions that are children of this action will be considered alternatives
* to this action
*/
let ActionSchema = new SimpleSchema({
let ActionSchema = createPropertySchema({
name: {
type: String,
optional: true,
max: STORAGE_LIMITS.name,
},
summary: {
type: InlineCalculationFieldToComputeSchema,
type: 'inlineCalculationFieldToCompute',
optional: true,
},
description: {
type: InlineCalculationFieldToComputeSchema,
type: 'inlineCalculationFieldToCompute',
optional: true,
},
// What time-resource is used to take the action in combat
@@ -59,7 +51,7 @@ let ActionSchema = new SimpleSchema({
},
// Calculation of how many times this action can be used
uses: {
type: Object,
type: 'fieldToCompute',
optional: true,
},
// Integer of how many times it has already been used
@@ -73,15 +65,15 @@ let ActionSchema = new SimpleSchema({
allowedValues: ['longRest', 'shortRest'],
optional: true,
},
}).extend(fieldToCompute('uses'));
});
const ComputedOnlyActionSchema = new SimpleSchema({
const ComputedOnlyActionSchema = createPropertySchema({
summary: {
type: ComputedOnlyInlineCalculationFieldSchema,
type: 'computedOnlyInlineCalculationField',
optional: true,
},
description: {
type: ComputedOnlyInlineCalculationFieldSchema,
type: 'computedOnlyInlineCalculationField',
optional: true,
},
resources: {
@@ -94,20 +86,12 @@ const ComputedOnlyActionSchema = new SimpleSchema({
type: Boolean,
optional: true,
},
}).extend(computedOnlyField('uses'));
});
const ComputedActionSchema = new SimpleSchema()
.extend(ActionSchema)
.extend(ComputedOnlyActionSchema)
.extend({
summary: {
type: InlineCalculationFieldSchema,
optional: true,
},
description: {
type: InlineCalculationFieldSchema,
optional: true,
},
resources: {
type: ResourcesComputedSchema,
defaultValue: {},

View File

@@ -1,15 +1,12 @@
import SimpleSchema from 'simpl-schema';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
const AdjustmentSchema = new SimpleSchema({
const AdjustmentSchema = createPropertySchema({
// The roll that determines how much to change the attribute
// This can be simplified, but should only compute when activated
amount: {
type: Object,
type: 'fieldToCompute',
optional: true,
},
'amount.calculation': {
@@ -37,9 +34,14 @@ const AdjustmentSchema = new SimpleSchema({
allowedValues: ['set', 'increment'],
defaultValue: 'increment',
},
}).extend(fieldToCompute('amount'));
});
const ComputedOnlyAdjustmentSchema = computedOnlyField('amount');
const ComputedOnlyAdjustmentSchema = createPropertySchema({
amount: {
type: 'computedOnlyField',
optional: true,
},
});
const ComputedAdjustmentSchema = new SimpleSchema()
.extend(AdjustmentSchema)

View File

@@ -1,12 +1,9 @@
import SimpleSchema from 'simpl-schema';
import { Random } from 'meteor/random';
import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
const AttributeConsumedSchema = new SimpleSchema({
const AttributeConsumedSchema = createPropertySchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
@@ -20,12 +17,12 @@ const AttributeConsumedSchema = new SimpleSchema({
max: STORAGE_LIMITS.variableName,
},
quantity: {
type: Object,
type: 'fieldToCompute',
optional: true,
},
}).extend(fieldToCompute('quantity'));
});
const ComputedOnlyAttributeConsumedSchema = new SimpleSchema({
const ComputedOnlyAttributeConsumedSchema = createPropertySchema({
available: {
type: Number,
optional: true,
@@ -40,7 +37,11 @@ const ComputedOnlyAttributeConsumedSchema = new SimpleSchema({
optional: true,
max: STORAGE_LIMITS.name,
},
}).extend(computedOnlyField('quantity'));
quantity: {
type: 'computedOnlyField',
optional: true,
},
});
const ComputedAttributeConsumedSchema = new SimpleSchema()
.extend(AttributeConsumedSchema)

View File

@@ -1,37 +0,0 @@
import SimpleSchema from 'simpl-schema';
import InlineComputationSchema from '/imports/api/properties/subSchemas/InlineComputationSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
const InlineCalculationFieldToComputeSchema = new SimpleSchema({
text: {
type: String,
optional: true,
max: STORAGE_LIMITS.inlineCalculationField,
},
});
const ComputedOnlyInlineCalculationFieldSchema = new SimpleSchema({
'inlineCalculations': {
type: Array,
defaultValue: [],
maxCount: STORAGE_LIMITS.inlineCalculationCount,
},
'inlineCalculations.$': {
type: InlineComputationSchema,
},
value: {
type: String,
optional: true,
max: STORAGE_LIMITS.inlineCalculationField,
},
});
const InlineCalculationFieldSchema = new SimpleSchema()
.extend(InlineCalculationFieldToComputeSchema)
.extend(ComputedOnlyInlineCalculationFieldSchema)
export {
InlineCalculationFieldToComputeSchema,
ComputedOnlyInlineCalculationFieldSchema,
InlineCalculationFieldSchema,
};

View File

@@ -1,13 +1,10 @@
import SimpleSchema from 'simpl-schema';
import { Random } from 'meteor/random';
import {
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';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
const ItemConsumedSchema = new SimpleSchema({
const ItemConsumedSchema = createPropertySchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
@@ -20,7 +17,7 @@ const ItemConsumedSchema = new SimpleSchema({
optional: true,
},
quantity: {
type: Object,
type: 'fieldToCompute',
optional: true,
},
itemId: {
@@ -28,13 +25,17 @@ const ItemConsumedSchema = new SimpleSchema({
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
}).extend(fieldToCompute('quantity'));
});
const ComputedOnlyItemConsumedSchema = new SimpleSchema({
available: {
type: Number,
optional: true,
},
quantity: {
type: 'computedOnlyField',
optional: true,
},
// This appears both in the computed and uncomputed schema because it can be
// set by both a computation or a form
itemId: {
@@ -57,7 +58,7 @@ const ComputedOnlyItemConsumedSchema = new SimpleSchema({
optional: true,
max: STORAGE_LIMITS.color,
},
}).extend(computedOnlyField('quantity'));
});
const ComputedItemConsumedSchema = new SimpleSchema()
.extend(ItemConsumedSchema)

View File

@@ -0,0 +1,43 @@
import {
inlineCalculationFieldToCompute,
computedOnlyInlineCalculationField,
} from '/imports/api/properties/subSchemas/inlineCalculationField.js';
import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/computedField.js';
import SimpleSchema from 'simpl-schema';
export default function createPropertySchema(definition){
const computationFields = {
inlineCalculationFieldToCompute: [],
computedOnlyInlineCalculationField: [],
fieldToCompute: [],
computedOnlyField: [],
};
const computedKeys = Object.keys(computationFields);
for (let key in definition){
const def = definition[key];
if (computedKeys.includes(def.type)){
computationFields[def.type].push(key);
def.type = Object;
}
}
const schema = new SimpleSchema(definition);
computationFields.inlineCalculationFieldToCompute.forEach(key => {
schema.extend(inlineCalculationFieldToCompute(key))
});
computationFields.computedOnlyInlineCalculationField.forEach(key => {
schema.extend(computedOnlyInlineCalculationField(key))
});
computationFields.fieldToCompute.forEach(key => {
schema.extend(fieldToCompute(key))
});
computationFields.computedOnlyField.forEach(key => {
schema.extend(computedOnlyField(key))
});
return schema
}

View File

@@ -0,0 +1,55 @@
import SimpleSchema from 'simpl-schema';
import InlineComputationSchema from '/imports/api/properties/subSchemas/InlineComputationSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
// Get schemas that apply fields directly so they can be gracefully extended
// because {type: Schema} fields can't be extended
function inlineCalculationFieldToCompute(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
[`${field}.text`]: {
type: String,
optional: true,
max: STORAGE_LIMITS.inlineCalculationField,
},
});
}
function computedOnlyInlineCalculationField(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
[`${field}.inlineCalculations`]: {
type: Array,
defaultValue: [],
maxCount: STORAGE_LIMITS.inlineCalculationCount,
},
[`${field}.inlineCalculations.$`]: {
type: InlineComputationSchema,
},
[`${field}.value`]: {
type: String,
optional: true,
max: STORAGE_LIMITS.inlineCalculationField,
},
});
}
function computedInlineCalculationField(field){
return inlineCalculationFieldToCompute(field).extend(
computedOnlyInlineCalculationField(field)
)
}
export {
inlineCalculationFieldToCompute,
computedOnlyInlineCalculationField,
computedInlineCalculationField,
};