Added denormalized computation fields
This commit is contained in:
@@ -4,7 +4,7 @@ import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
|
|||||||
|
|
||||||
// Get schemas that apply fields directly so they can be gracefully extended
|
// Get schemas that apply fields directly so they can be gracefully extended
|
||||||
// because {type: Schema} fields can't be extended
|
// because {type: Schema} fields can't be extended
|
||||||
function fieldToCompute(field){
|
function fieldToCompute(field) {
|
||||||
const schemaObj = {
|
const schemaObj = {
|
||||||
[`${field}.calculation`]: {
|
[`${field}.calculation`]: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -17,22 +17,52 @@ function fieldToCompute(field){
|
|||||||
return new SimpleSchema(schemaObj);
|
return new SimpleSchema(schemaObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
function computedOnlyField(field){
|
function computedOnlyField(field) {
|
||||||
const schemaObj = {
|
const schemaObj = {
|
||||||
[`${field}.value`]: {
|
// The parseNode of the compiled value before any effects are applied or rolls made
|
||||||
|
[`${field}.unaffected`]: {
|
||||||
|
type: Object,
|
||||||
|
optional: true,
|
||||||
|
blackbox: true,
|
||||||
|
},
|
||||||
|
/*
|
||||||
|
// toString(.baseValue)
|
||||||
|
[`${field}.baseValueString`]: {
|
||||||
type: SimpleSchema.oneOf(String, Number),
|
type: SimpleSchema.oneOf(String, Number),
|
||||||
optional: true,
|
optional: true,
|
||||||
removeBeforeCompute: true,
|
removeBeforeCompute: true,
|
||||||
},
|
},
|
||||||
// A list of effects targeting this calculation
|
*/
|
||||||
[`${field}.effects`]: {
|
// The compiled parseNode after applying all effects
|
||||||
|
[`${field}.value`]: {
|
||||||
|
type: Object,
|
||||||
|
optional: true,
|
||||||
|
blackbox: true,
|
||||||
|
},
|
||||||
|
/*
|
||||||
|
// toString(.value)
|
||||||
|
[`${field}.valueString`]: {
|
||||||
|
type: SimpleSchema.oneOf(String, Number),
|
||||||
|
optional: true,
|
||||||
|
removeBeforeCompute: true,
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
// A list of effect Ids targeting this calculation
|
||||||
|
[`${field}.effectIds`]: {
|
||||||
type: Array,
|
type: Array,
|
||||||
optional: true,
|
optional: true,
|
||||||
removeBeforeCompute: true,
|
removeBeforeCompute: true,
|
||||||
},
|
},
|
||||||
[`${field}.effects.$`]: {
|
[`${field}.effectIds.$`]: {
|
||||||
type: Object,
|
type: String,
|
||||||
blackbox: true,
|
},
|
||||||
|
[`${field}.proficiencyIds`]: {
|
||||||
|
type: Array,
|
||||||
|
optional: true,
|
||||||
|
removeBeforeCompute: true,
|
||||||
|
},
|
||||||
|
[`${field}.proficiencyIds.$`]: {
|
||||||
|
type: String,
|
||||||
},
|
},
|
||||||
// A cache of the parse result of the calculation
|
// A cache of the parse result of the calculation
|
||||||
[`${field}.parseNode`]: {
|
[`${field}.parseNode`]: {
|
||||||
@@ -56,7 +86,7 @@ function computedOnlyField(field){
|
|||||||
maxCount: STORAGE_LIMITS.errorCount,
|
maxCount: STORAGE_LIMITS.errorCount,
|
||||||
removeBeforeCompute: true,
|
removeBeforeCompute: true,
|
||||||
},
|
},
|
||||||
[`${field}.errors.$`]:{
|
[`${field}.errors.$`]: {
|
||||||
type: ErrorSchema,
|
type: ErrorSchema,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -65,9 +95,9 @@ function computedOnlyField(field){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We must include parent array and object fields for the schema to be valid
|
// We must include parent array and object fields for the schema to be valid
|
||||||
function includeParentFields(field, schemaObj){
|
function includeParentFields(field, schemaObj) {
|
||||||
const splitField = field.split('.');
|
const splitField = field.split('.');
|
||||||
if (splitField.length === 1){
|
if (splitField.length === 1) {
|
||||||
schemaObj[field] = {
|
schemaObj[field] = {
|
||||||
type: Object,
|
type: Object,
|
||||||
optional: true,
|
optional: true,
|
||||||
@@ -78,8 +108,8 @@ function includeParentFields(field, schemaObj){
|
|||||||
let key = '';
|
let key = '';
|
||||||
splitField.push('');
|
splitField.push('');
|
||||||
splitField.forEach((value, index) => {
|
splitField.forEach((value, index) => {
|
||||||
if (key){
|
if (key) {
|
||||||
if (value === '$'){
|
if (value === '$') {
|
||||||
schemaObj[key] = {
|
schemaObj[key] = {
|
||||||
type: Array,
|
type: Array,
|
||||||
optional: true
|
optional: true
|
||||||
@@ -90,7 +120,7 @@ function includeParentFields(field, schemaObj){
|
|||||||
optional: true,
|
optional: true,
|
||||||
};
|
};
|
||||||
// the last object is the computed field
|
// the last object is the computed field
|
||||||
if (index === splitField.length - 1){
|
if (index === splitField.length - 1) {
|
||||||
schemaObj[key].computedField = true;
|
schemaObj[key].computedField = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,7 +132,7 @@ function includeParentFields(field, schemaObj){
|
|||||||
|
|
||||||
// This should rarely be used, since the other two will merge correctly when
|
// This should rarely be used, since the other two will merge correctly when
|
||||||
// uncomputed and computedOnly schemas are merged
|
// uncomputed and computedOnly schemas are merged
|
||||||
function computedField(field){
|
function computedField(field) {
|
||||||
return computedField(field).extend(computedOnlyField(field));
|
return computedField(field).extend(computedOnlyField(field));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user