Compare commits
20 Commits
2.0-beta.2
...
2.0-beta.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
574f8373e7 | ||
|
|
a7ecdecec1 | ||
|
|
0aa59a4bfc | ||
|
|
8f0ff3245e | ||
|
|
9a2d10b7ed | ||
|
|
a8aa1923a8 | ||
|
|
57fa162c89 | ||
|
|
4d548c901c | ||
|
|
a97be2f93a | ||
|
|
1276f872a0 | ||
|
|
7daab97297 | ||
|
|
2e3704d096 | ||
|
|
7283a27727 | ||
|
|
3517636b8b | ||
|
|
e617ef9b75 | ||
|
|
cd45ae1442 | ||
|
|
bcedd548c7 | ||
|
|
dc53e38efe | ||
|
|
e381b3b24d | ||
|
|
111d971bc2 |
@@ -13,42 +13,43 @@ export default function applyAdjustment({
|
||||
...creature.variables,
|
||||
...actionContext,
|
||||
};
|
||||
try {
|
||||
var {result, errors} = evaluateString(prop.amount, scope, 'reduce');
|
||||
if (typeof result !== 'number') {
|
||||
log.content.push({
|
||||
name: 'Attribute damage',
|
||||
error: errors.join(', ') || 'Something went wrong',
|
||||
});
|
||||
}
|
||||
} catch (e){
|
||||
var {result, context} = evaluateString({
|
||||
string: prop.amount,
|
||||
scope,
|
||||
fn: 'reduce'
|
||||
});
|
||||
context.errors.forEach(e => {
|
||||
log.content.push({
|
||||
name: 'Attribute damage',
|
||||
error: e.toString(),
|
||||
name: 'Attribute damage error',
|
||||
error: e.message || e.toString(),
|
||||
});
|
||||
}
|
||||
});
|
||||
if (damageTargets) {
|
||||
damageTargets.forEach(target => {
|
||||
if (prop.target === 'each'){
|
||||
result = evaluateString(prop.amount, scope, 'reduce');
|
||||
({result} = evaluateString({
|
||||
string: prop.amount,
|
||||
scope,
|
||||
fn: 'reduce'
|
||||
}));
|
||||
}
|
||||
damagePropertiesByName.call({
|
||||
creatureId: target._id,
|
||||
variableName: prop.stat,
|
||||
operation: prop.operation || 'increment',
|
||||
value: result
|
||||
value: result.value,
|
||||
});
|
||||
log.content.push({
|
||||
name: 'Attribute damage',
|
||||
resultPrefix: `${prop.stat} ${prop.operation === 'set' ? 'set to' : ''}`,
|
||||
result: `${-result}`,
|
||||
result: `${result.isNumber ? -result.value : result.toString()}`,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
log.content.push({
|
||||
name: 'Attribute damage',
|
||||
resultPrefix: `${prop.stat} ${prop.operation === 'set' ? 'set to' : ''}`,
|
||||
result: `${-result}`,
|
||||
result: `${result.isNumber ? -result.value : result.toString()}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
} from '/imports/api/parenting/parenting.js';
|
||||
import {setDocToLastOrder} from '/imports/api/parenting/order.js';
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import { recomputeCreatureByDoc } from '/imports/api/creature/computation/methods/recomputeCreature.js';
|
||||
|
||||
export default function applyBuff({
|
||||
prop,
|
||||
@@ -58,10 +57,5 @@ function copyNodeListToTarget(propList, target, oldParent){
|
||||
collection: CreatureProperties,
|
||||
doc: propList[0],
|
||||
});
|
||||
|
||||
CreatureProperties.batchInsert(propList, () => {
|
||||
// This insert is racing the main recompute, recmpute again after it's
|
||||
// certainly finished
|
||||
recomputeCreatureByDoc(target);
|
||||
});
|
||||
CreatureProperties.batchInsert(propList);
|
||||
}
|
||||
|
||||
@@ -15,49 +15,79 @@ export default function applyDamage({
|
||||
...creature.variables,
|
||||
...actionContext,
|
||||
};
|
||||
// Add the target's variables to the scope
|
||||
if (targets.length === 1){
|
||||
scope.target = targets[0].variables;
|
||||
}
|
||||
// Determine if the hit is critical
|
||||
let criticalHit = !!(
|
||||
actionContext.criticalHit &&
|
||||
actionContext.criticalHit.value &&
|
||||
prop.damageType !== 'healing' // Can't critically heal
|
||||
);
|
||||
// Double the damage rolls if the hit is critical
|
||||
let context = new CompilationContext({
|
||||
doubleRolls: criticalHit,
|
||||
});
|
||||
try {
|
||||
var {result, errors} = evaluateString(prop.amount, scope, 'reduce', context);
|
||||
if (typeof result !== 'number') {
|
||||
log.content.push({
|
||||
error: errors.join(', '),
|
||||
});
|
||||
}
|
||||
} catch (e){
|
||||
|
||||
// Compute the roll the first time, logging any errors
|
||||
var {result} = evaluateString({
|
||||
string: prop.amount,
|
||||
scope,
|
||||
fn: 'reduce',
|
||||
context
|
||||
});
|
||||
|
||||
// If the result is an error bail out now
|
||||
if (result.constructor.name === 'ErrorNode'){
|
||||
log.content.push({
|
||||
error: e.toString(),
|
||||
name: 'Damage error',
|
||||
error: result.toString(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Memoise the damage suffix for the log
|
||||
let suffix = (criticalHit ? ' critical ' : '') +
|
||||
prop.damageType +
|
||||
(prop.damageType !== 'healing' ? ' damage': '');
|
||||
|
||||
if (damageTargets && damageTargets.length) {
|
||||
// Iterate through all the targets
|
||||
damageTargets.forEach(target => {
|
||||
let name = prop.damageType === 'healing' ? 'Healing' : 'Damage';
|
||||
|
||||
// Reroll the damage if needed
|
||||
if (prop.target === 'each'){
|
||||
result = evaluateString(prop.amount, scope, 'reduce');
|
||||
({result, context} = evaluateString({
|
||||
string: prop.amount,
|
||||
scope,
|
||||
fn: 'reduce'
|
||||
}));
|
||||
}
|
||||
// If the result is an error or not a number bail out now
|
||||
if (result.constructor.name === 'ErrorNode' || !result.isNumber){
|
||||
log.content.push({
|
||||
name: 'Damage error',
|
||||
error: result.toString(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Deal the damage to the target
|
||||
let damageDealt = dealDamage.call({
|
||||
creatureId: target._id,
|
||||
damageType: prop.damageType,
|
||||
amount: result,
|
||||
amount: result.value,
|
||||
});
|
||||
|
||||
// Log the damage done
|
||||
if (target._id === creature._id){
|
||||
// Target is same as self, log damage as such
|
||||
log.content.push({
|
||||
name,
|
||||
result: damageDealt,
|
||||
details: suffix + 'to self',
|
||||
details: suffix + ' to self',
|
||||
});
|
||||
} else {
|
||||
log.content.push({
|
||||
@@ -66,6 +96,7 @@ export default function applyDamage({
|
||||
result: damageDealt,
|
||||
details: suffix + `${target.name && ' to '}${target.name}`,
|
||||
});
|
||||
// Log the damage received on that creature's log as well
|
||||
insertCreatureLog.call({
|
||||
log: {
|
||||
content: [{
|
||||
@@ -80,9 +111,10 @@ export default function applyDamage({
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// There are no targets, just log the result
|
||||
log.content.push({
|
||||
name: prop.damageType === 'healing' ? 'Healing' : 'Damage',
|
||||
result,
|
||||
result: result.toString(),
|
||||
details: suffix,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import applySave from '/imports/api/creature/actions/applySave.js';
|
||||
function applyProperty(options){
|
||||
let prop = options.prop;
|
||||
if (prop.type === 'buff'){
|
||||
// ignore only applied buffs
|
||||
// ignore only applied buffs, don't apply them again
|
||||
if (prop.applied === true){
|
||||
return false;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ function applyProperty(options){
|
||||
break;
|
||||
case 'buff':
|
||||
applyBuff(options);
|
||||
break;
|
||||
return false;
|
||||
case 'toggle':
|
||||
return applyToggle(options);
|
||||
case 'roll':
|
||||
|
||||
@@ -10,23 +10,17 @@ export default function applyRoll({
|
||||
...creature.variables,
|
||||
...actionContext,
|
||||
};
|
||||
try {
|
||||
var {result, errors} = evaluateString(prop.roll, scope, 'reduce');
|
||||
actionContext[prop.variableName] = result;
|
||||
log.content.push({
|
||||
name: prop.name,
|
||||
resultPrefix: prop.variableName + ' = ' + prop.roll + ' = ',
|
||||
result,
|
||||
});
|
||||
if (errors.length) {
|
||||
log.content.push({
|
||||
error: errors.join(', '),
|
||||
});
|
||||
}
|
||||
|
||||
} catch (e){
|
||||
log.content.push({
|
||||
error: e.toString(),
|
||||
});
|
||||
var {result} = evaluateString({
|
||||
string: prop.roll,
|
||||
scope,
|
||||
fn: 'reduce'
|
||||
});
|
||||
if (result.isNumber){
|
||||
actionContext[prop.variableName] = result.value;
|
||||
}
|
||||
log.content.push({
|
||||
name: prop.name,
|
||||
resultPrefix: prop.variableName + ' = ' + prop.roll + ' = ',
|
||||
result: result.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,19 +14,17 @@ export default function applySave({
|
||||
};
|
||||
try {
|
||||
// Calculate the DC
|
||||
var {result, errors} = evaluateString(prop.dc, scope, 'reduce');
|
||||
let dc = result;
|
||||
var {result} = evaluateString({
|
||||
string: prop.dc,
|
||||
scope,
|
||||
fn: 'reduce'
|
||||
});
|
||||
let dc = result.value;
|
||||
log.content.push({
|
||||
name: prop.name,
|
||||
resultPrefix: ' DC ',
|
||||
result,
|
||||
result: result.toString(),
|
||||
});
|
||||
if (errors.length) {
|
||||
log.content.push({
|
||||
error: errors.join(', '),
|
||||
});
|
||||
return false;
|
||||
}
|
||||
if (prop.target === 'self'){
|
||||
let save = CreaturesProperties.findOne({
|
||||
'ancestors.id': creature._id,
|
||||
|
||||
@@ -13,23 +13,22 @@ export default function applyToggle({
|
||||
if (Number.isFinite(+prop.condition)){
|
||||
return !!+prop.condition;
|
||||
}
|
||||
try {
|
||||
var {result, errors} = evaluateString(prop.condition, scope, 'reduce');
|
||||
if (typeof result !== 'number' && typeof result !== 'boolean') {
|
||||
log.content.push({
|
||||
error: errors.join(', '),
|
||||
});
|
||||
return false;
|
||||
}
|
||||
var {result} = evaluateString({
|
||||
string: prop.condition,
|
||||
scope,
|
||||
fn: 'reduce'
|
||||
});
|
||||
if (result.constructor.name === 'ErrorNode') {
|
||||
log.content.push({
|
||||
name: prop.name,
|
||||
resultPrefix: prop.condition + ' = ',
|
||||
result,
|
||||
});
|
||||
return !!result;
|
||||
} catch (e){
|
||||
log.content.push({
|
||||
error: e.toString(),
|
||||
name: 'Toggle error',
|
||||
error: result.toString(),
|
||||
});
|
||||
return false;
|
||||
}
|
||||
log.content.push({
|
||||
name: prop.name || 'Toggle',
|
||||
resultPrefix: prop.condition + ' = ',
|
||||
result: result.toString(),
|
||||
});
|
||||
return !!result.value;
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ const castSpellWithSlot = new ValidatedMethod({
|
||||
action: spell,
|
||||
context: {slotLevel},
|
||||
creature,
|
||||
target,
|
||||
targets: [target],
|
||||
method: this,
|
||||
});
|
||||
// Note this only recomputes the top-level creature, not the nearest one
|
||||
|
||||
@@ -10,6 +10,7 @@ import { recomputeCreatureByDoc } from '/imports/api/creature/computation/method
|
||||
import { nodesToTree } from '/imports/api/parenting/parenting.js';
|
||||
import applyProperties from '/imports/api/creature/actions/applyProperties.js';
|
||||
import recomputeInventory from '/imports/api/creature/denormalise/recomputeInventory.js';
|
||||
import recomputeInactiveProperties from '/imports/api/creature/denormalise/recomputeInactiveProperties.js';
|
||||
|
||||
const doAction = new ValidatedMethod({
|
||||
name: 'creatureProperties.doAction',
|
||||
@@ -46,9 +47,14 @@ const doAction = new ValidatedMethod({
|
||||
|
||||
// The acting creature might have used ammo
|
||||
recomputeInventory(creature._id);
|
||||
|
||||
// The action might add properties which need to be activated
|
||||
recomputeInactiveProperties(creature._id);
|
||||
|
||||
// recompute creatures
|
||||
recomputeCreatureByDoc(creature);
|
||||
targets.forEach(target => {
|
||||
recomputeInactiveProperties(target._id);
|
||||
recomputeCreatureByDoc(target);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -51,7 +51,7 @@ export default function spendResources({prop, log}){
|
||||
// Now that we have confirmed that there are no errors, do actual work
|
||||
//Items
|
||||
itemQuantityAdjustments.forEach(adjustQuantityWork);
|
||||
|
||||
|
||||
// Use uses
|
||||
if (prop.usesResult){
|
||||
CreatureProperties.update(prop._id, {
|
||||
@@ -61,7 +61,7 @@ export default function spendResources({prop, log}){
|
||||
});
|
||||
log.content.push({
|
||||
name: 'Uses left',
|
||||
result: prop.usesResult - prop.usesUsed - 1,
|
||||
result: prop.usesResult - (prop.usesUsed || 0) - 1,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,30 +1,67 @@
|
||||
import { parse, CompilationContext } from '/imports/parser/parser.js';
|
||||
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
|
||||
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
|
||||
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
|
||||
|
||||
export default function evaluateString(string, scope, fn = 'compile', context){
|
||||
let errors = [];
|
||||
//TODO replace constants with their parsed node
|
||||
|
||||
export default function evaluateString({string, scope, fn = 'compile', context}){
|
||||
if (!context){
|
||||
context = new CompilationContext({});
|
||||
}
|
||||
if (!string){
|
||||
errors.push('No string provided');
|
||||
return {result: string, errors};
|
||||
context.storeError('No string provided');
|
||||
return {result: {value: string}, context};
|
||||
}
|
||||
|
||||
if (!scope) errors.push('No scope provided');
|
||||
if (!scope) context.storeError('No scope provided');
|
||||
|
||||
// Parse the string using mathjs
|
||||
let node;
|
||||
try {
|
||||
node = parse(string);
|
||||
} catch (e) {
|
||||
errors.push(e);
|
||||
return {result: string, errors};
|
||||
}
|
||||
if (!context){
|
||||
context = new CompilationContext({});
|
||||
context.storeError(e);
|
||||
return {result: {value: string}, context};
|
||||
}
|
||||
node = replaceConstants({calc: node, context, scope});
|
||||
let result = node[fn](scope, context);
|
||||
if (result instanceof ConstantNode){
|
||||
return {result: result.value, errors: context.errors}
|
||||
} else {
|
||||
return {result: result.toString(), errors: context.errors};
|
||||
}
|
||||
return {result, context};
|
||||
}
|
||||
|
||||
// Replace constants in the calc with the right ParseNodes
|
||||
function replaceConstants({calc, context, scope}){
|
||||
let constFailed = [];
|
||||
calc = calc.replaceNodes(node => {
|
||||
if (!(node instanceof SymbolNode)) return;
|
||||
let constant = scope[node.name];
|
||||
// replace constants that aren't overridden by stats or disabled by a toggle
|
||||
if (constant && constant.type === 'constant'){
|
||||
// Fail if the constant has errors
|
||||
if (constant.errors && constant.errors.length){
|
||||
constFailed.push(node.name);
|
||||
return;
|
||||
}
|
||||
let parsedConstantNode;
|
||||
try {
|
||||
parsedConstantNode = parse(constant.calculation);
|
||||
} catch(e){
|
||||
constFailed.push(node.name);
|
||||
return;
|
||||
}
|
||||
if (!parsedConstantNode) constFailed.push(node.name);
|
||||
return parsedConstantNode;
|
||||
}
|
||||
});
|
||||
constFailed.forEach(name => {
|
||||
context.storeError({
|
||||
type: 'error',
|
||||
message: `${name} is a constant property with parsing errors`
|
||||
});
|
||||
});
|
||||
let failed = !!constFailed.length;
|
||||
if (failed){
|
||||
calc = new ErrorNode({error: 'Failed to replace constants'});
|
||||
}
|
||||
return calc;
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js';
|
||||
|
||||
// Strings can have computations in bracers like so: {computation}
|
||||
export default function evalutateStringWithEmbeddedCalculations(string, scope){
|
||||
console.warn('evalutateStringWithEmbeddedCalculations should be replaced with ' +
|
||||
'fetching the result from the compuations on the property doc');
|
||||
if (!string) return string;
|
||||
// Compute everything inside bracers
|
||||
return string.replace(/\{([^{}]*)\}/g, function(match, p1){
|
||||
let {result} = evaluateString(p1, scope);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@@ -256,7 +256,6 @@ const propDetailsByType = {
|
||||
default(){
|
||||
return {
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
};
|
||||
},
|
||||
toggle(){
|
||||
@@ -264,7 +263,6 @@ const propDetailsByType = {
|
||||
computed: false,
|
||||
busyComputing: false,
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
};
|
||||
},
|
||||
attribute(){
|
||||
@@ -273,7 +271,6 @@ const propDetailsByType = {
|
||||
busyComputing: false,
|
||||
effects: [],
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
idsOfSameName: [],
|
||||
};
|
||||
},
|
||||
@@ -284,7 +281,6 @@ const propDetailsByType = {
|
||||
effects: [],
|
||||
proficiencies: [],
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
idsOfSameName: [],
|
||||
};
|
||||
},
|
||||
@@ -293,26 +289,22 @@ const propDetailsByType = {
|
||||
computed: false,
|
||||
busyComputing: false,
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
};
|
||||
},
|
||||
classLevel(){
|
||||
return {
|
||||
computed: true,
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
};
|
||||
},
|
||||
proficiency(){
|
||||
return {
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
};
|
||||
},
|
||||
denormalizedStat(){
|
||||
return {
|
||||
toggleAncestors: [],
|
||||
disabledByToggle: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@ import computeToggle from '/imports/api/creature/computation/engine/computeToggl
|
||||
import { union } from 'lodash';
|
||||
|
||||
export default function applyToggles(prop, memo){
|
||||
// If it used to be inactive delete those fields
|
||||
if (prop.inactive) prop.inactive = undefined;
|
||||
if (prop.deactivatedByAncestor) prop.deactivatedByAncestor = undefined;
|
||||
if (prop.deactivatedByToggle) prop.deactivatedByToggle = undefined;
|
||||
// Iterate through the toggle ancestors from oldest to nearest
|
||||
prop.computationDetails.toggleAncestors.forEach(toggleId => {
|
||||
let toggle = memo.togglesById[toggleId];
|
||||
computeToggle(toggle, memo);
|
||||
@@ -10,8 +15,11 @@ export default function applyToggles(prop, memo){
|
||||
[toggle._id],
|
||||
toggle.dependencies,
|
||||
);
|
||||
// Deactivate if the toggle is false
|
||||
if (!toggle.toggleResult){
|
||||
prop.computationDetails.disabledByToggle = true;
|
||||
prop.inactive = true;
|
||||
prop.deactivatedByAncestor = true;
|
||||
prop.deactivatedByToggle = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ function combineSkill(stat, aggregator, memo){
|
||||
let prof = stat.computationDetails.proficiencies[i];
|
||||
applyToggles(prof, memo);
|
||||
if (
|
||||
!prof.computationDetails.disabledByToggle &&
|
||||
!prof.deactivatedByToggle &&
|
||||
prof.value > stat.proficiency
|
||||
){
|
||||
stat.proficiency = prof.value;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import applyToggles from '/imports/api/creature/computation/engine/applyToggles.js';
|
||||
|
||||
export default function computeConstant(constant, memo){
|
||||
// Apply any toggles
|
||||
applyToggles(constant, memo);
|
||||
}
|
||||
@@ -75,26 +75,33 @@ function computeAction(prop, memo){
|
||||
});
|
||||
// Items consumed
|
||||
prop.resources.itemsConsumed.forEach((itemConsumed, i) => {
|
||||
let item = itemConsumed.itemId && memo.equipmentById[itemConsumed.itemId];
|
||||
prop.resources.itemsConsumed[i].itemId = item && item._id;
|
||||
let available = item && item.quantity || 0;
|
||||
let item = itemConsumed.itemId ?
|
||||
memo.equipmentById[itemConsumed.itemId] :
|
||||
undefined;
|
||||
let available = item ? item.quantity : 0;
|
||||
prop.resources.itemsConsumed[i].available = available;
|
||||
let name = item && item.name;
|
||||
if (item && item.quantity !== 1 && item.plural){
|
||||
name = item.plural;
|
||||
}
|
||||
prop.resources.itemsConsumed[i].itemName = name;
|
||||
prop.resources.itemsConsumed[i].itemIcon = item && item.icon;
|
||||
prop.resources.itemsConsumed[i].itemColor = item && item.color;
|
||||
if (!item || available < itemConsumed.quantity){
|
||||
prop.insufficientResources = true;
|
||||
}
|
||||
if (item){
|
||||
prop.resources.itemsConsumed[i].itemId = item._id;
|
||||
let name = item.name;
|
||||
if (item.quantity !== 1 && item.plural){
|
||||
name = item.plural;
|
||||
}
|
||||
if (name) prop.resources.itemsConsumed[i].itemName = name;
|
||||
if (item.icon) prop.resources.itemsConsumed[i].itemIcon = item.icon;
|
||||
if (item.color) prop.resources.itemsConsumed[i].itemColor = item.color;
|
||||
prop.dependencies = union(
|
||||
prop.dependencies,
|
||||
[item._id],
|
||||
item.dependencies
|
||||
);
|
||||
} else {
|
||||
delete prop.resources.itemsConsumed[i].itemId;
|
||||
delete prop.resources.itemsConsumed[i].itemName;
|
||||
delete prop.resources.itemsConsumed[i].itemIcon;
|
||||
delete prop.resources.itemsConsumed[i].itemColor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,8 +5,13 @@ import computeEffect from '/imports/api/creature/computation/engine/computeEffec
|
||||
import computeToggle from '/imports/api/creature/computation/engine/computeToggle.js';
|
||||
import computeEndStepProperty from '/imports/api/creature/computation/engine/computeEndStepProperty.js';
|
||||
import computeInlineCalculations from '/imports/api/creature/computation/engine/computeInlineCalculations.js';
|
||||
import computeConstant from '/imports/api/creature/computation/engine/computeConstant.js';
|
||||
|
||||
export default function computeMemo(memo){
|
||||
// Compute all constants that could be used
|
||||
forOwn(memo.constantsByVariableName, constant => {
|
||||
computeConstant (constant, memo);
|
||||
});
|
||||
// Compute level
|
||||
computeLevels(memo);
|
||||
// Compute all stats, even if they are overriden
|
||||
|
||||
@@ -22,7 +22,7 @@ export default function computeStat(stat, memo){
|
||||
// Apply any toggles
|
||||
applyToggles(stat, memo);
|
||||
|
||||
if (!stat.computationDetails.disabledByToggle){
|
||||
if (!stat.deactivatedByToggle){
|
||||
// Compute and aggregate all the effects
|
||||
let aggregator = new EffectAggregator(stat, memo)
|
||||
each(stat.computationDetails.effects, (effect) => {
|
||||
@@ -37,7 +37,7 @@ export default function computeStat(stat, memo){
|
||||
stat.dependencies,
|
||||
effect.dependencies
|
||||
)
|
||||
if (!effect.computationDetails.disabledByToggle){
|
||||
if (!effect.deactivatedByToggle){
|
||||
aggregator.addEffect(effect);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import evaluateCalculation from '/imports/api/creature/computation/engine/evaluateCalculation.js';
|
||||
import applyToggles from '/imports/api/creature/computation/engine/applyToggles.js';
|
||||
import { union } from 'lodash';
|
||||
|
||||
export default function computeToggle(toggle, memo){
|
||||
@@ -16,6 +17,9 @@ export default function computeToggle(toggle, memo){
|
||||
// Before doing any work, mark this toggle as busy
|
||||
toggle.computationDetails.busyComputing = true;
|
||||
|
||||
// Apply any parent toggles
|
||||
applyToggles(toggle, memo);
|
||||
|
||||
// Do work
|
||||
delete toggle.errors;
|
||||
if (toggle.enabled){
|
||||
@@ -41,6 +45,11 @@ export default function computeToggle(toggle, memo){
|
||||
toggle.errors = context.errors;
|
||||
}
|
||||
}
|
||||
if (!toggle.toggleResult){
|
||||
toggle.inactive = true;
|
||||
toggle.deactivatedBySelf = true;
|
||||
toggle.deactivatedByToggle = true;
|
||||
}
|
||||
toggle.computationDetails.computed = true;
|
||||
toggle.computationDetails.busyComputing = false;
|
||||
}
|
||||
|
||||
@@ -71,8 +71,8 @@ function replaceConstants({calc, memo, prop, dependencies, context}){
|
||||
} else if (node.name === '#constant'){
|
||||
constant = findAncestorByType({type: 'constant', prop, memo});
|
||||
}
|
||||
// replace constants that aren't overridden by stats
|
||||
if (constant && !stat){
|
||||
// replace constants that aren't overridden by stats or disabled by a toggle
|
||||
if (constant && !constant.deactivatedByToggle && !stat){
|
||||
dependencies = union(dependencies, [
|
||||
constant._id,
|
||||
...constant.dependencies
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
|
||||
export default function getComputationProperties(creatureId){
|
||||
// find ids of all toggles that have conditions, even if they are inactive
|
||||
let toggleIds = CreatureProperties.find({
|
||||
'ancestors.id': creatureId,
|
||||
type: 'toggle',
|
||||
removed: {$ne: true},
|
||||
condition: { $exists: true },
|
||||
}, {
|
||||
fields: {_id: 1},
|
||||
}).map(t => t._id);
|
||||
// Find all the relevant properties
|
||||
return CreatureProperties.find({
|
||||
'ancestors.id': creatureId,
|
||||
@@ -17,17 +8,15 @@ export default function getComputationProperties(creatureId){
|
||||
$or: [
|
||||
// All active properties
|
||||
{inactive: {$ne: true}},
|
||||
// All active and inactive toggles with conditions
|
||||
// Same as {$in: toggleIds}, but should be slightly faster
|
||||
{type: 'toggle', condition: { $exists: true }},
|
||||
// All decendents of the above toggles
|
||||
{'ancestors.id': {$in: toggleIds}},
|
||||
// Unless they were deactivated because of a toggle
|
||||
{deactivatedByToggle: true},
|
||||
]
|
||||
}, {
|
||||
// Filter out fields never used by calculations
|
||||
fields: {
|
||||
icon: 0,
|
||||
},
|
||||
// Obey tree order
|
||||
sort: {
|
||||
order: 1,
|
||||
}
|
||||
|
||||
@@ -19,7 +19,14 @@ export default function writeAlteredProperties(memo){
|
||||
ids.forEach(id => {
|
||||
let op = undefined;
|
||||
let original = memo.originalPropsById[id];
|
||||
let keys = ['dependencies', ...schema.objectKeys()];
|
||||
let keys = [
|
||||
'dependencies',
|
||||
'inactive',
|
||||
'deactivatedBySelf',
|
||||
'deactivatedByAncestor',
|
||||
'deactivatedByToggle',
|
||||
...schema.objectKeys(),
|
||||
];
|
||||
op = addChangedKeysToOp(op, keys, original, changed);
|
||||
if (op){
|
||||
bulkWriteOperations.push(op);
|
||||
|
||||
@@ -4,36 +4,46 @@ import VERSION from '/imports/constants/VERSION.js';
|
||||
|
||||
export default function writeCreatureVariables(memo, creatureId, fullRecompute = true) {
|
||||
const fields = [
|
||||
'name',
|
||||
'attributeType',
|
||||
'baseValue',
|
||||
'spellSlotLevelValue',
|
||||
'damage',
|
||||
'decimal',
|
||||
'reset',
|
||||
'resetMultiplier',
|
||||
'value',
|
||||
'currentValue',
|
||||
'modifier',
|
||||
'ability',
|
||||
'skillType',
|
||||
'baseProficiency',
|
||||
'abilityMod',
|
||||
'advantage',
|
||||
'passiveBonus',
|
||||
'proficiency',
|
||||
'attributeType',
|
||||
'baseProficiency',
|
||||
'baseValue',
|
||||
'calculation',
|
||||
'conditionalBenefits',
|
||||
'rollBonuses',
|
||||
'currentValue',
|
||||
'damage',
|
||||
'decimal',
|
||||
'fail',
|
||||
'level',
|
||||
'modifier',
|
||||
'name',
|
||||
'passiveBonus',
|
||||
'proficiency',
|
||||
'reset',
|
||||
'resetMultiplier',
|
||||
'rollBonuses',
|
||||
'skillType',
|
||||
'spellSlotLevelValue',
|
||||
'type',
|
||||
'value',
|
||||
];
|
||||
|
||||
if (fullRecompute){
|
||||
memo.creatureVariables = {};
|
||||
forOwn(memo.statsByVariableName, (stat, variableName) => {
|
||||
// Don't save context variables
|
||||
if (variableName[0] === '#') return;
|
||||
let condensedStat = pick(stat, fields);
|
||||
memo.creatureVariables[variableName] = condensedStat;
|
||||
});
|
||||
forOwn(memo.constantsByVariableName, (stat, variableName) => {
|
||||
let condensedStat = pick(stat, fields);
|
||||
if (!memo.creatureVariables[variableName]){
|
||||
memo.creatureVariables[variableName] = condensedStat;
|
||||
}
|
||||
});
|
||||
Creatures.update(creatureId, {$set: {
|
||||
variables: memo.creatureVariables,
|
||||
computeVersion: VERSION,
|
||||
|
||||
@@ -89,7 +89,6 @@ export function recomputeCreatureByDoc(creature){
|
||||
writeCreatureVariables(computationMemo, creatureId);
|
||||
recomputeDamageMultipliersById(creatureId);
|
||||
recomputeSlotFullness(creatureId);
|
||||
recomputeInactiveProperties(creatureId);
|
||||
return computationMemo;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,13 @@ let CreaturePropertySchema = new SimpleSchema({
|
||||
optional: true,
|
||||
index: 1,
|
||||
},
|
||||
// Denormalised flag if this property was made inactive because of a toggle
|
||||
// calculation. Either an ancestor toggle calculation or its own.
|
||||
deactivatedByToggle: {
|
||||
type: Boolean,
|
||||
optional: true,
|
||||
index: 1,
|
||||
},
|
||||
// Denormalised list of all properties or creatures this property depends on
|
||||
dependencies: {
|
||||
type: Array,
|
||||
|
||||
@@ -24,6 +24,9 @@ const damageProperty = new ValidatedMethod({
|
||||
run({_id, operation, value}) {
|
||||
// Check permissions
|
||||
let property = CreatureProperties.findOne(_id);
|
||||
if (!property) throw new Meteor.Error(
|
||||
'Damage property failed', 'Property doesn\'t exist'
|
||||
);
|
||||
let rootCreature = getRootCreatureAncestor(property);
|
||||
assertEditPermission(rootCreature, this.userId);
|
||||
// Check if property can take damage
|
||||
@@ -34,9 +37,10 @@ const damageProperty = new ValidatedMethod({
|
||||
`Property of type "${property.type}" can't be damaged`
|
||||
);
|
||||
}
|
||||
damagePropertyWork({property, operation, value});
|
||||
let result = damagePropertyWork({property, operation, value});
|
||||
// Dependencies can't be changed through damage, only recompute deps
|
||||
recomputePropertyDependencies(property);
|
||||
return result;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js
|
||||
import { organizeDoc } from '/imports/api/parenting/organizeMethods.js';
|
||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||
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';
|
||||
|
||||
@@ -62,6 +63,7 @@ const equipItem = new ValidatedMethod({
|
||||
skipRecompute: true,
|
||||
});
|
||||
|
||||
recomputeInactiveProperties(creature._id);
|
||||
recomputeInventory(creature._id);
|
||||
recomputeCreatureByDoc(creature);
|
||||
},
|
||||
|
||||
@@ -3,7 +3,7 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||
import { recomputeCreatureByDoc } from '/imports/api/creature/computation/methods/recomputeCreature.js';
|
||||
import { recomputeCreatureById } 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';
|
||||
|
||||
@@ -59,7 +59,8 @@ const updateCreatureProperty = new ValidatedMethod({
|
||||
recomputeInventory(rootCreature._id);
|
||||
}
|
||||
// Updating a property is likely to change dependencies, do a full recompute
|
||||
recomputeCreatureByDoc(rootCreature);
|
||||
// denormalised stats might change, so fetch the creature again
|
||||
recomputeCreatureById(rootCreature._id);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ export default function recomputeInactiveProperties(ancestorId){
|
||||
{disabled: true}, // Everything can be disabled
|
||||
{type: 'buff', applied: false}, // Buffs can be applied
|
||||
{type: 'item', equipped: {$ne: true}},
|
||||
{type: 'toggle', toggleResult: false},
|
||||
{type: 'spell', prepared: {$ne: true}, alwaysPrepared: {$ne: true}},
|
||||
],
|
||||
};
|
||||
@@ -56,14 +55,18 @@ export default function recomputeInactiveProperties(ancestorId){
|
||||
CreatureProperties.update({
|
||||
'ancestors.id': {$eq: ancestorId, $nin: disabledIds},
|
||||
'_id': {$nin: disabledIds},
|
||||
// if it was a toggle responsible, we leave it alone
|
||||
deactivatedByToggle: {$ne: true},
|
||||
$or: [
|
||||
{inactive: true},
|
||||
{deactivatedByAncestor: true},
|
||||
{deactivatedBySelf: true}
|
||||
],
|
||||
}, {
|
||||
$unset: {
|
||||
inactive: 1,
|
||||
deactivatedByAncestor: 1,
|
||||
deactivatedBySelf: 1,
|
||||
},
|
||||
}, {
|
||||
multi: true,
|
||||
|
||||
@@ -84,13 +84,13 @@ function getInventoryData(tree, containersToWrite){
|
||||
for (let key in data){
|
||||
data[key] += childData[key];
|
||||
}
|
||||
if (node.contentsWeightless){
|
||||
data.weightCarried = node.weight;
|
||||
}
|
||||
if (node.carried === false){
|
||||
data.weightCarried = 0;
|
||||
data.valueCarried = 0;
|
||||
}
|
||||
if (node.contentsWeightless){
|
||||
data.weightCarried = node.weight;
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
|
||||
import ArrayNode from '/imports/parser/parseTree/ArrayNode.js';
|
||||
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
|
||||
|
||||
export default class IndexNode extends ParseNode {
|
||||
constructor({array, index}) {
|
||||
@@ -8,16 +10,42 @@ export default class IndexNode extends ParseNode {
|
||||
}
|
||||
resolve(fn, scope, context){
|
||||
let index = this.index[fn](scope, context);
|
||||
if (index.isInteger){
|
||||
let selection = this.array.values[index.value - 1];
|
||||
let array = this.array[fn](scope, context);
|
||||
|
||||
if (index.isInteger && array instanceof ArrayNode){
|
||||
if (index.value < 1 || index.value > array.values.length){
|
||||
if (context){
|
||||
context.storeError({
|
||||
type: 'warning',
|
||||
message: `Index of ${index.value} is out of range for an array` +
|
||||
` of length ${array.values.length}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
let selection = array.values[index.value - 1];
|
||||
if (selection){
|
||||
let result = selection[fn](scope, context);
|
||||
return result;
|
||||
}
|
||||
} else if (fn === 'reduce'){
|
||||
if (!(array instanceof ArrayNode)){
|
||||
return new ErrorNode({
|
||||
node: this,
|
||||
error: 'Can not get the index of a non-array node: ' +
|
||||
this.array.toString() + ' = ' + array.toString(),
|
||||
context,
|
||||
});
|
||||
} else if (!index.isInteger){
|
||||
return new ErrorNode({
|
||||
node: this,
|
||||
error: array.toString() + ' is not an integer index of the array',
|
||||
context,
|
||||
});
|
||||
}
|
||||
}
|
||||
return new IndexNode({
|
||||
index,
|
||||
array: this.array[fn](scope, context),
|
||||
array,
|
||||
previousNodes: [this],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ export default class ParenthesisNode extends ParseNode {
|
||||
resolve(fn, scope, context){
|
||||
let content = this.content[fn](scope, context);
|
||||
if (
|
||||
fn === 'reduce' ||
|
||||
content.constructor.name === 'ConstantNode' ||
|
||||
content.constructor.name === 'ErrorNode'
|
||||
){
|
||||
|
||||
@@ -32,26 +32,31 @@ Meteor.publish('libraries', function(){
|
||||
});
|
||||
|
||||
let libraryIdSchema = new SimpleSchema({
|
||||
libraryId: {
|
||||
libraryIds: {
|
||||
type: Array,
|
||||
},
|
||||
'libraryIds.$':{
|
||||
type: String,
|
||||
regEx: SimpleSchema.RegEx.Id,
|
||||
},
|
||||
});
|
||||
|
||||
Meteor.publish('library', function(libraryId){
|
||||
libraryIdSchema.validate({libraryId});
|
||||
Meteor.publish('libraryNodes', function(libraryIds){
|
||||
libraryIdSchema.validate({libraryIds});
|
||||
if (!libraryIds.length) return [];
|
||||
this.autorun(function (){
|
||||
let userId = this.userId;
|
||||
let libraryCursor = Libraries.find({
|
||||
_id: libraryId,
|
||||
});
|
||||
let library = libraryCursor.fetch()[0];
|
||||
try { assertViewPermission(library, userId) }
|
||||
catch(e){ return [] }
|
||||
for (let i in libraryIds){
|
||||
let libraryId = libraryIds[i];
|
||||
let library = Libraries.findOne(libraryId);
|
||||
try { assertViewPermission(library, userId) }
|
||||
catch(e){
|
||||
return this.error(e);
|
||||
}
|
||||
}
|
||||
return [
|
||||
libraryCursor,
|
||||
LibraryNodes.find({
|
||||
'ancestors.id': libraryId,
|
||||
'ancestors.id': {$in: libraryIds},
|
||||
}, {
|
||||
sort: {order: 1},
|
||||
}),
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
props: {
|
||||
value: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
},
|
||||
open: Boolean,
|
||||
flat: Boolean,
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
return this.speedDialsByTab[tabs[this.tabNumber]];
|
||||
},
|
||||
speedDialsByTab() { return {
|
||||
'stats': ['attribute', 'skill'],
|
||||
'stats': ['attribute', 'skill', 'action', 'attack'],
|
||||
'features': ['feature'],
|
||||
'inventory': ['item', 'container'],
|
||||
'spells': ['spellList', 'spell'],
|
||||
|
||||
@@ -143,10 +143,10 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
||||
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
|
||||
import DialogBase from '/imports/ui/dialogStack/DialogBase.vue';
|
||||
import { getPropertyName } from '/imports/constants/PROPERTIES.js';
|
||||
import { parse, CompilationContext } from '/imports/parser/parser.js';
|
||||
import PROPERTIES from '/imports/constants/PROPERTIES.js';
|
||||
import TreeNodeView from '/imports/ui/properties/treeNodeViews/TreeNodeView.vue';
|
||||
import PropertyDescription from '/imports/ui/properties/viewers/shared/PropertyDescription.vue'
|
||||
import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js';
|
||||
|
||||
export default {
|
||||
components: {
|
||||
@@ -243,15 +243,12 @@ export default {
|
||||
// the quantity to fill
|
||||
nodes = nodes.filter(node => {
|
||||
if (node.slotFillerCondition){
|
||||
let context = new CompilationContext();
|
||||
let conditionResult;
|
||||
try {
|
||||
conditionResult = parse(node.slotFillerCondition)
|
||||
.reduce(this.creature.variables, context);
|
||||
} catch (e){
|
||||
console.warn(e);
|
||||
}
|
||||
if (conditionResult && !conditionResult.value) return false;
|
||||
let {result} = evaluateString({
|
||||
string: node.slotFillerCondition,
|
||||
scope: this.creature.variables,
|
||||
fn: 'reduce',
|
||||
});
|
||||
if (!result.value) return false;
|
||||
}
|
||||
if (
|
||||
node.type === 'slotFiller' &&
|
||||
|
||||
@@ -112,9 +112,6 @@ export default {
|
||||
getPropertyName,
|
||||
},
|
||||
meteor: {
|
||||
$subscribe: {
|
||||
'libraries': [],
|
||||
},
|
||||
libraries(){
|
||||
return Libraries.find({}, {
|
||||
sort: {name: 1}
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
</v-expansion-panel-content>
|
||||
</v-expansion-panel>
|
||||
<v-btn
|
||||
v-show="expandedLibrary === null"
|
||||
v-show="noLibrariesExpanded"
|
||||
v-if="editMode"
|
||||
flat
|
||||
color="primary"
|
||||
@@ -94,6 +94,17 @@ export default {
|
||||
meteor: {
|
||||
$subscribe: {
|
||||
'libraries': [],
|
||||
'libraryNodes'(){
|
||||
if (!this.expandedLibrary) return [[]];
|
||||
let libraryIds = [];
|
||||
this.expandedLibrary.forEach((expanded, index) => {
|
||||
if (expanded){
|
||||
let library = this.libraries[index];
|
||||
if (library) libraryIds.push(library._id)
|
||||
}
|
||||
});
|
||||
return [libraryIds];
|
||||
}
|
||||
},
|
||||
libraries(){
|
||||
return Libraries.find({}, {
|
||||
@@ -105,6 +116,16 @@ export default {
|
||||
return tier && tier.paidBenefits;
|
||||
},
|
||||
},
|
||||
computed: {
|
||||
noLibrariesExpanded(){
|
||||
if (!this.expandedLibrary) return true;
|
||||
let noneExpanded = true;
|
||||
this.expandedLibrary.forEach(lib => {
|
||||
if(lib) noneExpanded = false;
|
||||
});
|
||||
return noneExpanded;
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
insertLibrary(){
|
||||
if (this.paidBenefits){
|
||||
|
||||
@@ -27,11 +27,6 @@
|
||||
selectedNodeId: String,
|
||||
},
|
||||
meteor: {
|
||||
$subscribe: {
|
||||
'library'(){
|
||||
return [this.libraryId]
|
||||
},
|
||||
},
|
||||
library(){
|
||||
return Libraries.findOne(this.libraryId);
|
||||
},
|
||||
|
||||
@@ -146,7 +146,7 @@ export default {
|
||||
return Math.max(this.model.usesResult, 0);
|
||||
},
|
||||
usesLeft(){
|
||||
return Math.max(this.model.usesResult - this.model.usesUsed, 0);
|
||||
return Math.max(this.model.usesResult - (this.model.usesUsed || 0), 0);
|
||||
},
|
||||
propertyName(){
|
||||
return getPropertyName(this.model.type);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<template lang="html">
|
||||
<div class="resources-form">
|
||||
<div
|
||||
v-if="model.attributesConsumed.length"
|
||||
v-if="model.attributesConsumed && model.attributesConsumed.length"
|
||||
class="subheading"
|
||||
>
|
||||
Attributes
|
||||
|
||||
@@ -168,7 +168,7 @@ export default {
|
||||
return Math.max(this.model.usesResult, 0);
|
||||
},
|
||||
usesLeft(){
|
||||
return Math.max(this.model.usesResult - this.model.usesUsed, 0);
|
||||
return Math.max(this.model.usesResult - (this.model.usesUsed || 0), 0);
|
||||
},
|
||||
actionTypeIcon() {
|
||||
return `$vuetify.icons.${this.model.actionType}`;
|
||||
|
||||
Reference in New Issue
Block a user