Compare commits
151 Commits
2.0-beta.3
...
2.0-beta.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43f8df09f0 | ||
|
|
b6ed9ffb74 | ||
|
|
a84da7d8a5 | ||
|
|
249aebea0f | ||
|
|
11a527481e | ||
|
|
8d729216b5 | ||
|
|
1677e8c424 | ||
|
|
987aacbb67 | ||
|
|
2714d0b9d5 | ||
|
|
1d98c41168 | ||
|
|
e42ec4b862 | ||
|
|
59fc5ab851 | ||
|
|
5d14c392e8 | ||
|
|
c6ca8c1fa4 | ||
|
|
28307e26c3 | ||
|
|
6d42eb62f0 | ||
|
|
877c9ca099 | ||
|
|
9b652fc133 | ||
|
|
7d66c06107 | ||
|
|
21629138f0 | ||
|
|
59a488256b | ||
|
|
766519b4a3 | ||
|
|
e7f73d0e54 | ||
|
|
193d5eec50 | ||
|
|
9284c9ad76 | ||
|
|
f86152675f | ||
|
|
cbac5264cd | ||
|
|
34e3325464 | ||
|
|
79c9e67ce2 | ||
|
|
4c2aabf90d | ||
|
|
48331d3806 | ||
|
|
45f05d0d34 | ||
|
|
58629c92f4 | ||
|
|
719af548f0 | ||
|
|
f2a1861279 | ||
|
|
38c3b6ff1f | ||
|
|
23e848fe40 | ||
|
|
4d6cdf50bd | ||
|
|
1cf9f3b5fd | ||
|
|
8164b79667 | ||
|
|
360df79004 | ||
|
|
a8f163ff33 | ||
|
|
36b3b80850 | ||
|
|
1d22f4c054 | ||
|
|
99e4e8d6bb | ||
|
|
2bb3265356 | ||
|
|
263f2d8424 | ||
|
|
ee0e764294 | ||
|
|
13fc0c0b12 | ||
|
|
ecfeeaccd9 | ||
|
|
b324fb1f03 | ||
|
|
8d34cc1369 | ||
|
|
839c2488b2 | ||
|
|
fd79bc2bb3 | ||
|
|
1050442606 | ||
|
|
53ed271ea2 | ||
|
|
6ccbf204eb | ||
|
|
d44d4e0315 | ||
|
|
2b8f7e4927 | ||
|
|
65e7ce6dce | ||
|
|
24cc87d6f7 | ||
|
|
03578b2066 | ||
|
|
6ea882a053 | ||
|
|
bec65be170 | ||
|
|
0483a7effc | ||
|
|
4c5c537f29 | ||
|
|
a0c2822dac | ||
|
|
27a21aed59 | ||
|
|
1da2d319fb | ||
|
|
82879aaa60 | ||
|
|
be654d5d45 | ||
|
|
f88ffcf0c3 | ||
|
|
8b62bac83f | ||
|
|
e698b4b838 | ||
|
|
566d6a4fef | ||
|
|
6f7e742eb9 | ||
|
|
0c06f60b7e | ||
|
|
f8e9131bdd | ||
|
|
bf9639ae59 | ||
|
|
ee89a052bc | ||
|
|
59ef7527b7 | ||
|
|
b8a03245ea | ||
|
|
1a71c2cfa7 | ||
|
|
292388dead | ||
|
|
00272e7b55 | ||
|
|
f07f05ca2c | ||
|
|
9dd84a83d2 | ||
|
|
b2f89eceee | ||
|
|
b484a27637 | ||
|
|
da5143693f | ||
|
|
9cc4186171 | ||
|
|
9f59a6cf86 | ||
|
|
fdaa035bfb | ||
|
|
b31760af0c | ||
|
|
a8ffa2f786 | ||
|
|
9b1ec46064 | ||
|
|
11f373ddd8 | ||
|
|
e7e8f938ed | ||
|
|
28934baac9 | ||
|
|
385ac17812 | ||
|
|
a04935c5b4 | ||
|
|
ccc861b6fa | ||
|
|
6702f431d0 | ||
|
|
1b3efae81a | ||
|
|
7a35c66904 | ||
|
|
78cd8ffc8d | ||
|
|
23fa6fe634 | ||
|
|
caf50d1578 | ||
|
|
df7889edd9 | ||
|
|
ddc7f87a4a | ||
|
|
33fa22c187 | ||
|
|
2e3f0320f3 | ||
|
|
0b7c20e616 | ||
|
|
abb8890070 | ||
|
|
8dbcae1060 | ||
|
|
3a18bce7e6 | ||
|
|
3e97baaaaa | ||
|
|
ea32c54f57 | ||
|
|
6b724cf365 | ||
|
|
8b44c83741 | ||
|
|
2ca9ac5342 | ||
|
|
7609e916c6 | ||
|
|
f440e030cf | ||
|
|
13b6689ba4 | ||
|
|
b28bcbe079 | ||
|
|
ffa6353a3f | ||
|
|
33f60c2c6d | ||
|
|
dcc95486b3 | ||
|
|
88bc223daa | ||
|
|
cbc42f8500 | ||
|
|
cc24690a67 | ||
|
|
4d5cb3ed50 | ||
|
|
c3d9ee7589 | ||
|
|
d9f572504d | ||
|
|
cb80f3a6da | ||
|
|
e89d415e7e | ||
|
|
ac8f19bcfb | ||
|
|
788cbb182d | ||
|
|
c68667be9c | ||
|
|
fada07e048 | ||
|
|
12fc9b1be3 | ||
|
|
e7f718c785 | ||
|
|
9732db8d67 | ||
|
|
73ca6dc364 | ||
|
|
10242b596f | ||
|
|
782f2cdc73 | ||
|
|
a8ebf6a1de | ||
|
|
7dcd0aeff2 | ||
|
|
a19e7d0514 | ||
|
|
2442ae4fa0 | ||
|
|
545050cfa3 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
build
|
||||||
@@ -3,26 +3,25 @@
|
|||||||
# 'meteor add' and 'meteor remove' will edit this file for you,
|
# 'meteor add' and 'meteor remove' will edit this file for you,
|
||||||
# but you can also edit it by hand.
|
# but you can also edit it by hand.
|
||||||
|
|
||||||
accounts-password@2.2.0
|
accounts-password@2.3.1
|
||||||
random@1.2.0
|
random@1.2.0
|
||||||
underscore@1.0.10
|
underscore@1.0.10
|
||||||
dburles:mongo-collection-instances
|
dburles:mongo-collection-instances
|
||||||
accounts-google@1.4.0
|
accounts-google@1.4.0
|
||||||
email@2.2.0
|
email@2.2.1
|
||||||
meteor-base@1.5.1
|
meteor-base@1.5.1
|
||||||
mobile-experience@1.1.0
|
mobile-experience@1.1.0
|
||||||
mongo@1.14.6
|
mongo@1.15.0
|
||||||
session@1.2.0
|
session@1.2.0
|
||||||
tracker@1.2.0
|
tracker@1.2.0
|
||||||
logging@1.3.1
|
logging@1.3.1
|
||||||
reload@1.3.1
|
reload@1.3.1
|
||||||
ejson@1.1.1
|
ejson@1.1.2
|
||||||
check@1.3.1
|
check@1.3.1
|
||||||
standard-minifier-js@2.8.0
|
standard-minifier-js@2.8.0
|
||||||
shell-server@0.5.0
|
shell-server@0.5.0
|
||||||
ecmascript@0.16.1
|
ecmascript@0.16.2
|
||||||
es5-shim@4.8.0
|
es5-shim@4.8.0
|
||||||
percolate:synced-cron
|
|
||||||
service-configuration@1.3.0
|
service-configuration@1.3.0
|
||||||
dynamic-import@0.7.2
|
dynamic-import@0.7.2
|
||||||
ddp-rate-limiter@1.1.0
|
ddp-rate-limiter@1.1.0
|
||||||
@@ -47,3 +46,5 @@ meteortesting:mocha
|
|||||||
ostrio:files
|
ostrio:files
|
||||||
simple:rest-bearer-token-parser
|
simple:rest-bearer-token-parser
|
||||||
simple:rest-json-error-handler
|
simple:rest-json-error-handler
|
||||||
|
littledata:synced-cron
|
||||||
|
mdg:meteor-apm-agent
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
METEOR@2.6.1
|
METEOR@2.7.3
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
accounts-base@2.2.1
|
accounts-base@2.2.3
|
||||||
accounts-google@1.4.0
|
accounts-google@1.4.0
|
||||||
accounts-oauth@1.4.0
|
accounts-oauth@1.4.1
|
||||||
accounts-password@2.2.0
|
accounts-password@2.3.1
|
||||||
accounts-patreon@0.1.0
|
accounts-patreon@0.1.0
|
||||||
akryum:npm-check@0.1.2
|
akryum:npm-check@0.1.2
|
||||||
akryum:vue-component@0.15.2
|
akryum:vue-component@0.15.2
|
||||||
@@ -12,13 +12,13 @@ aldeed:collection2@3.5.0
|
|||||||
aldeed:schema-index@3.0.0
|
aldeed:schema-index@3.0.0
|
||||||
allow-deny@1.1.1
|
allow-deny@1.1.1
|
||||||
autoupdate@1.8.0
|
autoupdate@1.8.0
|
||||||
babel-compiler@7.8.1
|
babel-compiler@7.9.0
|
||||||
babel-runtime@1.5.0
|
babel-runtime@1.5.1
|
||||||
base64@1.0.12
|
base64@1.0.12
|
||||||
binary-heap@1.0.11
|
binary-heap@1.0.11
|
||||||
blaze-tools@1.1.2
|
blaze-tools@1.1.3
|
||||||
boilerplate-generator@1.7.1
|
boilerplate-generator@1.7.1
|
||||||
bozhao:link-accounts@2.4.0
|
bozhao:link-accounts@2.6.1
|
||||||
caching-compiler@1.2.2
|
caching-compiler@1.2.2
|
||||||
caching-html-compiler@1.2.1
|
caching-html-compiler@1.2.1
|
||||||
callback-hook@1.4.0
|
callback-hook@1.4.0
|
||||||
@@ -33,26 +33,29 @@ ddp-rate-limiter@1.1.0
|
|||||||
ddp-server@2.5.0
|
ddp-server@2.5.0
|
||||||
diff-sequence@1.1.1
|
diff-sequence@1.1.1
|
||||||
dynamic-import@0.7.2
|
dynamic-import@0.7.2
|
||||||
ecmascript@0.16.1
|
ecmascript@0.16.2
|
||||||
ecmascript-runtime@0.8.0
|
ecmascript-runtime@0.8.0
|
||||||
ecmascript-runtime-client@0.12.1
|
ecmascript-runtime-client@0.12.1
|
||||||
ecmascript-runtime-server@0.11.0
|
ecmascript-runtime-server@0.11.0
|
||||||
ejson@1.1.1
|
ejson@1.1.2
|
||||||
email@2.2.0
|
email@2.2.1
|
||||||
es5-shim@4.8.0
|
es5-shim@4.8.0
|
||||||
fetch@0.1.1
|
fetch@0.1.1
|
||||||
geojson-utils@1.0.10
|
geojson-utils@1.0.10
|
||||||
google-oauth@1.4.1
|
google-oauth@1.4.2
|
||||||
hot-code-push@1.0.4
|
hot-code-push@1.0.4
|
||||||
html-tools@1.1.2
|
html-tools@1.1.3
|
||||||
htmljs@1.1.1
|
htmljs@1.1.1
|
||||||
http@2.0.0
|
http@2.0.0
|
||||||
id-map@1.1.1
|
id-map@1.1.1
|
||||||
inter-process-messaging@0.1.1
|
inter-process-messaging@0.1.1
|
||||||
lai:collection-extensions@0.3.0
|
lai:collection-extensions@0.3.0
|
||||||
launch-screen@1.3.0
|
launch-screen@1.3.0
|
||||||
|
littledata:synced-cron@1.5.1
|
||||||
|
livedata@1.0.18
|
||||||
localstorage@1.2.0
|
localstorage@1.2.0
|
||||||
logging@1.3.1
|
logging@1.3.1
|
||||||
|
mdg:meteor-apm-agent@3.5.0
|
||||||
mdg:validated-method@1.2.0
|
mdg:validated-method@1.2.0
|
||||||
meteor@1.10.0
|
meteor@1.10.0
|
||||||
meteor-base@1.5.1
|
meteor-base@1.5.1
|
||||||
@@ -61,22 +64,23 @@ meteortesting:mocha@2.0.3
|
|||||||
meteortesting:mocha-core@8.1.2
|
meteortesting:mocha-core@8.1.2
|
||||||
mikowals:batch-insert@1.3.0
|
mikowals:batch-insert@1.3.0
|
||||||
minifier-css@1.6.0
|
minifier-css@1.6.0
|
||||||
minifier-js@2.7.3
|
minifier-js@2.7.4
|
||||||
minimongo@1.8.0
|
minimongo@1.8.0
|
||||||
mobile-experience@1.1.0
|
mobile-experience@1.1.0
|
||||||
mobile-status-bar@1.1.0
|
mobile-status-bar@1.1.0
|
||||||
modern-browsers@0.1.7
|
modern-browsers@0.1.8
|
||||||
modules@0.18.0
|
modules@0.18.0
|
||||||
modules-runtime@0.12.0
|
modules-runtime@0.13.0
|
||||||
mongo@1.14.6
|
mongo@1.15.0
|
||||||
mongo-decimal@0.1.2
|
mongo-decimal@0.1.3
|
||||||
mongo-dev-server@1.1.0
|
mongo-dev-server@1.1.0
|
||||||
mongo-id@1.0.8
|
mongo-id@1.0.8
|
||||||
|
mongo-livedata@1.0.12
|
||||||
npm-mongo@4.3.1
|
npm-mongo@4.3.1
|
||||||
oauth@2.1.1
|
oauth@2.1.2
|
||||||
oauth2@1.3.1
|
oauth2@1.3.1
|
||||||
ordered-dict@1.1.0
|
ordered-dict@1.1.0
|
||||||
ostrio:cookies@2.7.0
|
ostrio:cookies@2.7.2
|
||||||
ostrio:files@2.0.1
|
ostrio:files@2.0.1
|
||||||
patreon-oauth@0.1.0
|
patreon-oauth@0.1.0
|
||||||
peerlibrary:assert@0.3.0
|
peerlibrary:assert@0.3.0
|
||||||
@@ -90,12 +94,11 @@ peerlibrary:reactive-publish@0.10.0
|
|||||||
peerlibrary:server-autorun@0.8.0
|
peerlibrary:server-autorun@0.8.0
|
||||||
peerlibrary:subscription-data@0.8.0
|
peerlibrary:subscription-data@0.8.0
|
||||||
percolate:migrations@1.0.3
|
percolate:migrations@1.0.3
|
||||||
percolate:synced-cron@1.3.2
|
|
||||||
promise@0.12.0
|
promise@0.12.0
|
||||||
raix:eventemitter@1.0.0
|
raix:eventemitter@1.0.0
|
||||||
random@1.2.0
|
random@1.2.0
|
||||||
rate-limit@1.0.9
|
rate-limit@1.0.9
|
||||||
react-fast-refresh@0.2.2
|
react-fast-refresh@0.2.3
|
||||||
reactive-dict@1.3.0
|
reactive-dict@1.3.0
|
||||||
reactive-var@1.0.11
|
reactive-var@1.0.11
|
||||||
reload@1.3.1
|
reload@1.3.1
|
||||||
@@ -111,14 +114,14 @@ simple:rest@1.2.1
|
|||||||
simple:rest-bearer-token-parser@1.1.1
|
simple:rest-bearer-token-parser@1.1.1
|
||||||
simple:rest-json-error-handler@1.1.1
|
simple:rest-json-error-handler@1.1.1
|
||||||
simple:rest-method-mixin@1.1.0
|
simple:rest-method-mixin@1.1.0
|
||||||
socket-stream-client@0.4.0
|
socket-stream-client@0.5.0
|
||||||
spacebars-compiler@1.3.0
|
spacebars-compiler@1.3.1
|
||||||
standard-minifier-js@2.8.0
|
standard-minifier-js@2.8.0
|
||||||
static-html@1.3.2
|
static-html@1.3.2
|
||||||
templating-tools@1.2.1
|
templating-tools@1.2.2
|
||||||
tmeasday:check-npm-versions@1.0.2
|
tmeasday:check-npm-versions@1.0.2
|
||||||
tracker@1.2.0
|
tracker@1.2.0
|
||||||
typescript@4.4.1
|
typescript@4.5.4
|
||||||
underscore@1.0.10
|
underscore@1.0.10
|
||||||
url@1.3.2
|
url@1.3.2
|
||||||
webapp@1.13.1
|
webapp@1.13.1
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import { createS3FilesCollection } from '/imports/api/files/s3FileStorage.js';
|
import { createS3FilesCollection } from '/imports/api/files/s3FileStorage.js';
|
||||||
|
import SimpleSchema from 'simpl-schema';
|
||||||
|
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed.js';
|
||||||
|
import { CreaturePropertySchema } from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
|
import { CreatureSchema } from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
|
||||||
const ArchiveCreatureFiles = createS3FilesCollection({
|
const ArchiveCreatureFiles = createS3FilesCollection({
|
||||||
collectionName: 'archiveCreatureFiles',
|
collectionName: 'archiveCreatureFiles',
|
||||||
@@ -11,7 +15,38 @@ const ArchiveCreatureFiles = createS3FilesCollection({
|
|||||||
if (!/json/i.test(file.extension)){
|
if (!/json/i.test(file.extension)){
|
||||||
return 'Please upload only a JSON file';
|
return 'Please upload only a JSON file';
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
onAfterUpload(file) {
|
||||||
|
if (Meteor.isServer) incrementFileStorageUsed(file.userId, file.size);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let archiveSchema = new SimpleSchema({
|
||||||
|
meta: {
|
||||||
|
type: Object,
|
||||||
|
blackbox: true,
|
||||||
|
},
|
||||||
|
creature: CreatureSchema,
|
||||||
|
properties: {
|
||||||
|
type: Array,
|
||||||
|
},
|
||||||
|
'properties.$': CreaturePropertySchema,
|
||||||
|
experiences: {
|
||||||
|
type: Array,
|
||||||
|
},
|
||||||
|
'experiences.$': {
|
||||||
|
type: Object,
|
||||||
|
blackbox: true,
|
||||||
|
},
|
||||||
|
logs: {
|
||||||
|
type: Array,
|
||||||
|
},
|
||||||
|
'logs.$': {
|
||||||
|
type: Object,
|
||||||
|
blackbox: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export default ArchiveCreatureFiles;
|
export default ArchiveCreatureFiles;
|
||||||
|
export { archiveSchema };
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
|
||||||
|
|
||||||
// Archived creatures is an immutable collection of creatures that are no longer
|
|
||||||
// in use and can be safely archived by the mongoDB hosting service.
|
|
||||||
// It keeps the working datasets like creatureProperties much smaller
|
|
||||||
// than they would otherwise be.
|
|
||||||
let ArchivedCreatures = new Mongo.Collection('archivedCreatures');
|
|
||||||
|
|
||||||
// We use blackbox objects for everything:
|
|
||||||
// - saves time checking every object against a schema
|
|
||||||
// - doesn't accidentaly create indices defined in subschemas
|
|
||||||
// - The objects we are archiving have already been checked against their
|
|
||||||
// own schemas
|
|
||||||
let ArchivedCreatureSchema = new SimpleSchema({
|
|
||||||
owner: {
|
|
||||||
type: String,
|
|
||||||
regEx: SimpleSchema.RegEx.Id,
|
|
||||||
// The primary index on this collection
|
|
||||||
index: 1,
|
|
||||||
},
|
|
||||||
archiveDate: {
|
|
||||||
type: Date,
|
|
||||||
// Indexed so the archiving system can archive documents when they
|
|
||||||
// get to a certain age
|
|
||||||
index: 1,
|
|
||||||
},
|
|
||||||
creature: {
|
|
||||||
type: Object,
|
|
||||||
blackbox: true,
|
|
||||||
},
|
|
||||||
properties: {
|
|
||||||
type: Array,
|
|
||||||
},
|
|
||||||
'properties.$': {
|
|
||||||
type: Object,
|
|
||||||
blackbox: true,
|
|
||||||
},
|
|
||||||
experiences: {
|
|
||||||
type: Array,
|
|
||||||
},
|
|
||||||
'experiences.$': {
|
|
||||||
type: Object,
|
|
||||||
blackbox: true,
|
|
||||||
},
|
|
||||||
logs: {
|
|
||||||
type: Array,
|
|
||||||
},
|
|
||||||
'logs.$': {
|
|
||||||
type: Object,
|
|
||||||
blackbox: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
ArchivedCreatures.attachSchema(ArchivedCreatureSchema);
|
|
||||||
|
|
||||||
import '/imports/api/creature/archive/methods/index.js';
|
|
||||||
export default ArchivedCreatures;
|
|
||||||
@@ -68,7 +68,7 @@ const archiveCreatureToFile = new ValidatedMethod({
|
|||||||
async run({creatureId}) {
|
async run({creatureId}) {
|
||||||
assertOwnership(creatureId, this.userId);
|
assertOwnership(creatureId, this.userId);
|
||||||
if (Meteor.isServer){
|
if (Meteor.isServer){
|
||||||
archiveCreature(creatureId);
|
archiveCreature(creatureId, this.userId);
|
||||||
} else {
|
} else {
|
||||||
removeCreatureWork(creatureId);
|
removeCreatureWork(creatureId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// import '/imports/api/creature/archive/methods/archiveCreatures.js';
|
|
||||||
import '/imports/api/creature/archive/methods/archiveCreatureToFile.js';
|
import '/imports/api/creature/archive/methods/archiveCreatureToFile.js';
|
||||||
import '/imports/api/creature/archive/methods/restoreCreatures.js';
|
|
||||||
import '/imports/api/creature/archive/methods/restoreCreatureFromFile.js';
|
import '/imports/api/creature/archive/methods/restoreCreatureFromFile.js';
|
||||||
|
import '/imports/api/creature/archive/methods/removeArchiveCreature.js';
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import SimpleSchema from 'simpl-schema';
|
||||||
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
|
import ArchiveCreatureFiles from '/imports/api/creature/archive/ArchiveCreatureFiles.js';
|
||||||
|
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed.js';
|
||||||
|
|
||||||
|
const removeArchiveCreature = new ValidatedMethod({
|
||||||
|
name: 'ArchiveCreatureFiles.methods.removeArchiveCreature',
|
||||||
|
validate: new SimpleSchema({
|
||||||
|
'fileId': {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
}).validator(),
|
||||||
|
mixins: [RateLimiterMixin],
|
||||||
|
rateLimit: {
|
||||||
|
numRequests: 5,
|
||||||
|
timeInterval: 5000,
|
||||||
|
},
|
||||||
|
async run({ fileId }) {
|
||||||
|
// fetch the file
|
||||||
|
const file = ArchiveCreatureFiles.findOne({ _id: fileId }).get();
|
||||||
|
if (!file) {
|
||||||
|
throw new Meteor.Error('File not found',
|
||||||
|
'The requested creature archive does not exist');
|
||||||
|
}
|
||||||
|
// Assert ownership
|
||||||
|
const userId = file?.userId;
|
||||||
|
if (!userId || userId !== this.userId) {
|
||||||
|
throw new Meteor.Error('Permission denied',
|
||||||
|
'You can only restore creatures you own');
|
||||||
|
}
|
||||||
|
//Remove the archive once the restore succeeded
|
||||||
|
ArchiveCreatureFiles.remove({ _id: fileId });
|
||||||
|
// Update the user's file storage limits
|
||||||
|
incrementFileStorageUsed(userId, -file.size);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default removeArchiveCreature;
|
||||||
@@ -8,12 +8,16 @@ import CreatureLogs from '/imports/api/creature/log/CreatureLogs.js';
|
|||||||
import Experiences from '/imports/api/creature/experience/Experiences.js';
|
import Experiences from '/imports/api/creature/experience/Experiences.js';
|
||||||
import { removeCreatureWork } from '/imports/api/creature/creatures/methods/removeCreature.js';
|
import { removeCreatureWork } from '/imports/api/creature/creatures/methods/removeCreature.js';
|
||||||
import ArchiveCreatureFiles from '/imports/api/creature/archive/ArchiveCreatureFiles.js';
|
import ArchiveCreatureFiles from '/imports/api/creature/archive/ArchiveCreatureFiles.js';
|
||||||
|
import assertHasCharactersSlots from '/imports/api/creature/creatures/methods/assertHasCharacterSlots.js';
|
||||||
|
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed.js';
|
||||||
|
import verifyArchiveSafety from '/imports/api/creature/archive/methods/verifyArchiveSafety.js';
|
||||||
|
|
||||||
let migrateArchive;
|
let migrateArchive;
|
||||||
if (Meteor.isServer){
|
if (Meteor.isServer){
|
||||||
migrateArchive = require('/imports/migrations/server/migrateArchive.js').default;
|
migrateArchive = require('/imports/migrations/server/migrateArchive.js').default;
|
||||||
}
|
}
|
||||||
|
|
||||||
function restoreCreature(archive){
|
function restoreCreature(archive, userId){
|
||||||
if (SCHEMA_VERSION < archive.meta.schemaVersion){
|
if (SCHEMA_VERSION < archive.meta.schemaVersion){
|
||||||
throw new Meteor.Error('Incompatible',
|
throw new Meteor.Error('Incompatible',
|
||||||
'The archive file is from a newer version. Update required to read.')
|
'The archive file is from a newer version. Update required to read.')
|
||||||
@@ -22,6 +26,19 @@ function restoreCreature(archive){
|
|||||||
// Migrate and verify the archive meets the current schema
|
// Migrate and verify the archive meets the current schema
|
||||||
migrateArchive(archive);
|
migrateArchive(archive);
|
||||||
|
|
||||||
|
// Asset that the archive is safe
|
||||||
|
verifyArchiveSafety(archive);
|
||||||
|
|
||||||
|
// Don't upload creatures twice
|
||||||
|
const existingCreature = Creatures.findOne(archive.creature._id, {
|
||||||
|
fields: { _id: 1 }
|
||||||
|
});
|
||||||
|
if (existingCreature) throw new Meteor.Error('Already exists',
|
||||||
|
'The creature you are trying to restore already exists.')
|
||||||
|
|
||||||
|
// Ensure the user owns the restored creature
|
||||||
|
archive.creature.owner = userId;
|
||||||
|
|
||||||
// Insert the creature sub documents
|
// Insert the creature sub documents
|
||||||
// They still have their original _id's
|
// They still have their original _id's
|
||||||
Creatures.insert(archive.creature);
|
Creatures.insert(archive.creature);
|
||||||
@@ -69,13 +86,18 @@ const restoreCreaturefromFile = new ValidatedMethod({
|
|||||||
throw new Meteor.Error('Permission denied',
|
throw new Meteor.Error('Permission denied',
|
||||||
'You can only restore creatures you own');
|
'You can only restore creatures you own');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assertHasCharactersSlots(this.userId);
|
||||||
|
|
||||||
if (Meteor.isServer){
|
if (Meteor.isServer){
|
||||||
// Read the file data
|
// Read the file data
|
||||||
const archive = await ArchiveCreatureFiles.readJSONFile(file);
|
const archive = await ArchiveCreatureFiles.readJSONFile(file);
|
||||||
restoreCreature(archive);
|
restoreCreature(archive, this.userId);
|
||||||
}
|
}
|
||||||
//Remove the archive once the restore succeeded
|
//Remove the archive once the restore succeeded
|
||||||
ArchiveCreatureFiles.remove({_id: fileId});
|
ArchiveCreatureFiles.remove({ _id: fileId });
|
||||||
|
// Update the user's file storage limits
|
||||||
|
incrementFileStorageUsed(userId, -file.size);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
|
||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|
||||||
import { assertOwnership } from '/imports/api/sharing/sharingPermissions.js';
|
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
|
||||||
import CreatureLogs from '/imports/api/creature/log/CreatureLogs.js';
|
|
||||||
import Experiences from '/imports/api/creature/experience/Experiences.js';
|
|
||||||
import ArchivedCreatures from '/imports/api/creature/archive/ArchivedCreatures.js';
|
|
||||||
import { removeCreatureWork } from '/imports/api/creature/creatures/methods/removeCreature.js';
|
|
||||||
|
|
||||||
export function restoreCreature(archiveId){
|
|
||||||
// Get the archive
|
|
||||||
const archivedCreature = ArchivedCreatures.findOne(archiveId);
|
|
||||||
|
|
||||||
// Insert the creature sub documents
|
|
||||||
// They still have their original _id's
|
|
||||||
Creatures.insert(archivedCreature.creature);
|
|
||||||
try {
|
|
||||||
// Add all the properties
|
|
||||||
if (archivedCreature.properties && archivedCreature.properties.length){
|
|
||||||
CreatureProperties.batchInsert(archivedCreature.properties);
|
|
||||||
}
|
|
||||||
if (archivedCreature.experiences && archivedCreature.experiences.length){
|
|
||||||
Experiences.batchInsert(archivedCreature.experiences);
|
|
||||||
}
|
|
||||||
if (archivedCreature.logs && archivedCreature.logs.length){
|
|
||||||
CreatureLogs.batchInsert(archivedCreature.logs);
|
|
||||||
}
|
|
||||||
// Remove the archived creature
|
|
||||||
ArchivedCreatures.remove(archiveId);
|
|
||||||
} catch (e) {
|
|
||||||
// If the above fails, delete the inserted creature
|
|
||||||
removeCreatureWork(archivedCreature.creature._id);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not recompute. The creature was in a computed and ordered state when
|
|
||||||
// we archived it, just restore everything as-is
|
|
||||||
|
|
||||||
return archivedCreature.creature._id;
|
|
||||||
}
|
|
||||||
|
|
||||||
const restoreCreatures = new ValidatedMethod({
|
|
||||||
name: 'Creatures.methods.restoreCreatures',
|
|
||||||
validate: new SimpleSchema({
|
|
||||||
archiveIds: {
|
|
||||||
type: Array,
|
|
||||||
max: 10,
|
|
||||||
},
|
|
||||||
'archiveIds.$': {
|
|
||||||
type: String,
|
|
||||||
regEx: SimpleSchema.RegEx.Id,
|
|
||||||
},
|
|
||||||
}).validator(),
|
|
||||||
mixins: [RateLimiterMixin],
|
|
||||||
rateLimit: {
|
|
||||||
numRequests: 1,
|
|
||||||
timeInterval: 5000,
|
|
||||||
},
|
|
||||||
run({archiveIds}) {
|
|
||||||
for (let id of archiveIds){
|
|
||||||
let archivedCreature = ArchivedCreatures.findOne(id, {
|
|
||||||
fields: {owner: 1}
|
|
||||||
});
|
|
||||||
assertOwnership(archivedCreature, this.userId)
|
|
||||||
}
|
|
||||||
let creatureIds = [];
|
|
||||||
for (let id of archiveIds){
|
|
||||||
let creatureId = restoreCreature(id);
|
|
||||||
creatureIds.push(creatureId);
|
|
||||||
}
|
|
||||||
return creatureIds;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default restoreCreatures;
|
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { slice } from 'lodash';
|
||||||
|
import PER_CREATURE_LOG_LIMIT from '/imports/api/creature/log/CreatureLogs.js';
|
||||||
|
|
||||||
|
export default function verifyArchiveSafety({ meta, creature, properties, experiences, logs }){
|
||||||
|
const creatureId = creature._id;
|
||||||
|
|
||||||
|
// Check lengths of arrays
|
||||||
|
if (logs.length > PER_CREATURE_LOG_LIMIT) {
|
||||||
|
logs = slice(logs, 0, PER_CREATURE_LOG_LIMIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that everything belongs to the right creature
|
||||||
|
logs.forEach(log => {
|
||||||
|
if (log.creatureId !== creatureId) {
|
||||||
|
throw new Meteor.Error('Malicious log', 'Log contains an entry for the wrong creature');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
experiences.forEach(experience => {
|
||||||
|
if (experience.creatureId !== creatureId) {
|
||||||
|
throw new Meteor.Error('Malicious experience', 'Experiences contains an entry for the wrong creature');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
properties.forEach(prop => {
|
||||||
|
if (prop.ancestors[0].id !== creatureId) {
|
||||||
|
throw new Meteor.Error('Malicious prop', 'Properties contains an entry for the wrong creature');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -82,6 +82,13 @@ const DenormalisedOnlyCreaturePropertySchema = new SimpleSchema({
|
|||||||
index: 1,
|
index: 1,
|
||||||
removeBeforeCompute: true,
|
removeBeforeCompute: true,
|
||||||
},
|
},
|
||||||
|
// When this is true on any property, the creature needs to be recomputed
|
||||||
|
dirty: {
|
||||||
|
type: Boolean,
|
||||||
|
// Default to true because new properties cause a recomputation
|
||||||
|
defaultValue: true,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
CreaturePropertySchema.extend(DenormalisedOnlyCreaturePropertySchema);
|
CreaturePropertySchema.extend(DenormalisedOnlyCreaturePropertySchema);
|
||||||
@@ -98,10 +105,6 @@ for (let key in propertySchemasIndex){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
import '/imports/api/creature/creatureProperties/methods/index.js';
|
|
||||||
//import '/imports/api/creature/actions/doAction.js';
|
|
||||||
//import '/imports/api/creature/actions/castSpellWithSlot.js';
|
|
||||||
|
|
||||||
export default CreatureProperties;
|
export default CreatureProperties;
|
||||||
export {
|
export {
|
||||||
DenormalisedOnlyCreaturePropertySchema,
|
DenormalisedOnlyCreaturePropertySchema,
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import SimpleSchema from 'simpl-schema';
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const adjustQuantity = new ValidatedMethod({
|
const adjustQuantity = new ValidatedMethod({
|
||||||
name: 'creatureProperties.adjustQuantity',
|
name: 'creatureProperties.adjustQuantity',
|
||||||
@@ -29,10 +28,6 @@ const adjustQuantity = new ValidatedMethod({
|
|||||||
|
|
||||||
// Do work
|
// Do work
|
||||||
adjustQuantityWork({property, operation, value});
|
adjustQuantityWork({property, operation, value});
|
||||||
|
|
||||||
// Changing quantity does not change dependencies, but recomputing the
|
|
||||||
// inventory changes many deps at once, so recompute fully
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -47,7 +42,7 @@ export function adjustQuantityWork({property, operation, value}){
|
|||||||
}
|
}
|
||||||
if (operation === 'set'){
|
if (operation === 'set'){
|
||||||
CreatureProperties.update(property._id, {
|
CreatureProperties.update(property._id, {
|
||||||
$set: {quantity: value}
|
$set: {quantity: value, dirty: true}
|
||||||
}, {
|
}, {
|
||||||
selector: property
|
selector: property
|
||||||
});
|
});
|
||||||
@@ -57,7 +52,8 @@ export function adjustQuantityWork({property, operation, value}){
|
|||||||
let currentQuantity = property.quantity;
|
let currentQuantity = property.quantity;
|
||||||
if (currentQuantity + value < 0) value = -currentQuantity;
|
if (currentQuantity + value < 0) value = -currentQuantity;
|
||||||
CreatureProperties.update(property._id, {
|
CreatureProperties.update(property._id, {
|
||||||
$inc: {quantity: value}
|
$inc: { quantity: value },
|
||||||
|
$set: { dirty: true }
|
||||||
}, {
|
}, {
|
||||||
selector: property
|
selector: property
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|
||||||
import SimpleSchema from 'simpl-schema';
|
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
|
||||||
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
|
||||||
|
|
||||||
const damagePropertiesByName = new ValidatedMethod({
|
|
||||||
name: 'CreatureProperties.damagePropertiesByName',
|
|
||||||
validate: new SimpleSchema({
|
|
||||||
creatureId: SimpleSchema.RegEx.Id,
|
|
||||||
variableName: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
operation: {
|
|
||||||
type: String,
|
|
||||||
allowedValues: ['set', 'increment']
|
|
||||||
},
|
|
||||||
value: Number,
|
|
||||||
}).validator(),
|
|
||||||
mixins: [RateLimiterMixin],
|
|
||||||
rateLimit: {
|
|
||||||
numRequests: 20,
|
|
||||||
timeInterval: 5000,
|
|
||||||
},
|
|
||||||
run({creatureId, variableName, operation, value}) {
|
|
||||||
// Check permissions
|
|
||||||
let creature = Creatures.findOne(creatureId, {
|
|
||||||
fields: {
|
|
||||||
variables: 1,
|
|
||||||
owner: 1,
|
|
||||||
readers: 1,
|
|
||||||
writers: 1,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
assertEditPermission(creature, this.userId);
|
|
||||||
CreatureProperties.find({
|
|
||||||
'ancestors.id': creatureId,
|
|
||||||
variableName,
|
|
||||||
removed: {$ne: false},
|
|
||||||
inactive: {$ne: true},
|
|
||||||
}).forEach(property => {
|
|
||||||
// Check if property can take damage
|
|
||||||
let schema = CreatureProperties.simpleSchema(property);
|
|
||||||
if (!schema.allowsKey('damage')) return;
|
|
||||||
// Damage the property
|
|
||||||
damagePropertyWork({property, operation, value});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export default damagePropertiesByName;
|
|
||||||
@@ -2,9 +2,9 @@ import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import { computeCreatureDependencyGroup } from '/imports/api/engine/computeCreature.js';
|
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
|
||||||
|
|
||||||
const damageProperty = new ValidatedMethod({
|
const damageProperty = new ValidatedMethod({
|
||||||
name: 'creatureProperties.damage',
|
name: 'creatureProperties.damage',
|
||||||
@@ -21,60 +21,119 @@ const damageProperty = new ValidatedMethod({
|
|||||||
numRequests: 20,
|
numRequests: 20,
|
||||||
timeInterval: 5000,
|
timeInterval: 5000,
|
||||||
},
|
},
|
||||||
run({_id, operation, value}) {
|
run({ _id, operation, value }) {
|
||||||
// Check permissions
|
|
||||||
let property = CreatureProperties.findOne(_id);
|
// Get action context
|
||||||
if (!property) throw new Meteor.Error(
|
let prop = CreatureProperties.findOne(_id);
|
||||||
|
if (!prop) throw new Meteor.Error(
|
||||||
'Damage property failed', 'Property doesn\'t exist'
|
'Damage property failed', 'Property doesn\'t exist'
|
||||||
);
|
);
|
||||||
let rootCreature = getRootCreatureAncestor(property);
|
const creatureId = prop.ancestors[0].id;
|
||||||
assertEditPermission(rootCreature, this.userId);
|
const actionContext = new ActionContext(creatureId, [creatureId], this);
|
||||||
|
|
||||||
|
// Check permissions
|
||||||
|
assertEditPermission(actionContext.creature, this.userId);
|
||||||
|
|
||||||
// Check if property can take damage
|
// Check if property can take damage
|
||||||
let schema = CreatureProperties.simpleSchema(property);
|
let schema = CreatureProperties.simpleSchema(prop);
|
||||||
if (!schema.allowsKey('damage')){
|
if (!schema.allowsKey('damage')){
|
||||||
throw new Meteor.Error(
|
throw new Meteor.Error(
|
||||||
'Damage property failed',
|
'Damage property failed',
|
||||||
`Property of type "${property.type}" can't be damaged`
|
`Property of type "${prop.type}" can't be damaged`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let result = damagePropertyWork({property, operation, value});
|
|
||||||
// Dependencies can't be changed through damage, only recompute deps
|
// Replace the prop by its actionContext counterpart if possible
|
||||||
computeCreatureDependencyGroup(property);
|
if (prop.variableName) {
|
||||||
|
const actionContextProp = actionContext.scope[prop.variableName];
|
||||||
|
if (actionContextProp?._id === prop._id) {
|
||||||
|
prop = actionContextProp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = damagePropertyWork({ prop, operation, value, actionContext });
|
||||||
|
|
||||||
|
// Insert the log
|
||||||
|
actionContext.writeLog();
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export function damagePropertyWork({property, operation, value}){
|
export function damagePropertyWork({ prop, operation, value, actionContext }) {
|
||||||
let damage, newValue;
|
|
||||||
|
// Save the value to the scope before applying the before triggers
|
||||||
|
if (operation === 'increment') {
|
||||||
|
if (value >= 0) {
|
||||||
|
actionContext.scope['$damage'] = value;
|
||||||
|
} else {
|
||||||
|
actionContext.scope['$healing'] = -value;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
actionContext.scope['$set'] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
applyTriggers(actionContext.triggers?.damageProperty?.before, prop, actionContext);
|
||||||
|
|
||||||
|
// fetch the value from the scope after the before triggers, in case they changed them
|
||||||
|
if (operation === 'increment') {
|
||||||
|
if (value >= 0) {
|
||||||
|
value = actionContext.scope['$damage'];
|
||||||
|
} else {
|
||||||
|
value = -actionContext.scope['$healing'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
value = actionContext.scope['$set'];
|
||||||
|
}
|
||||||
|
|
||||||
|
let damage, newValue, increment;
|
||||||
if (operation === 'set'){
|
if (operation === 'set'){
|
||||||
const total = property.total || 0;
|
const total = prop.total || 0;
|
||||||
// Set represents what we want the value to be after damage
|
// Set represents what we want the value to be after damage
|
||||||
// So we need the actual damage to get to that value
|
// So we need the actual damage to get to that value
|
||||||
damage = total - value;
|
damage = total - value;
|
||||||
// Damage can't exceed total value
|
// Damage can't exceed total value
|
||||||
if (damage > total) damage = total;
|
if (damage > total && !prop.ignoreLowerLimit) damage = total;
|
||||||
// Damage must be positive
|
// Damage must be positive
|
||||||
if (damage < 0) damage = 0;
|
if (damage < 0 && !prop.ignoreUpperLimit) damage = 0;
|
||||||
newValue = property.total - damage;
|
newValue = prop.total - damage;
|
||||||
|
// Write the results
|
||||||
|
CreatureProperties.update(prop._id, {
|
||||||
|
$set: { damage, value: newValue, dirty: true }
|
||||||
|
}, {
|
||||||
|
selector: prop
|
||||||
|
});
|
||||||
|
// Also write it straight to the prop so that it is updated in the actionContext
|
||||||
|
prop.damage = damage;
|
||||||
|
prop.value = newValue;
|
||||||
} else if (operation === 'increment'){
|
} else if (operation === 'increment'){
|
||||||
let currentValue = property.value || 0;
|
let currentValue = prop.value || 0;
|
||||||
let currentDamage = property.damage || 0;
|
let currentDamage = prop.damage || 0;
|
||||||
let increment = value;
|
increment = value;
|
||||||
// Can't increase damage above the remaining value
|
// Can't increase damage above the remaining value
|
||||||
if (increment > currentValue) increment = currentValue;
|
if (increment > currentValue && !prop.ignoreLowerLimit) increment = currentValue;
|
||||||
// Can't decrease damage below zero
|
// Can't decrease damage below zero
|
||||||
if (-increment > currentDamage) increment = -currentDamage;
|
if (-increment > currentDamage && !prop.ignoreUpperLimit) increment = -currentDamage;
|
||||||
damage = currentDamage + increment;
|
damage = currentDamage + increment;
|
||||||
newValue = property.total - damage;
|
newValue = prop.total - damage;
|
||||||
|
// Write the results
|
||||||
|
CreatureProperties.update(prop._id, {
|
||||||
|
$inc: { damage: increment, value: -increment },
|
||||||
|
$set: { dirty: true },
|
||||||
|
}, {
|
||||||
|
selector: prop
|
||||||
|
});
|
||||||
|
// Also write it straight to the prop so that it is updated in the actionContext
|
||||||
|
prop.damage += increment;
|
||||||
|
prop.value -= increment;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the results
|
applyTriggers(actionContext.triggers?.damageProperty?.after, prop, actionContext);
|
||||||
CreatureProperties.update(property._id, {
|
|
||||||
$set: {damage, value: newValue}
|
if (operation === 'set') {
|
||||||
}, {
|
return damage;
|
||||||
selector: property
|
} else if (operation === 'increment') {
|
||||||
});
|
return increment;
|
||||||
return damage;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default damageProperty;
|
export default damageProperty;
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|
||||||
import SimpleSchema from 'simpl-schema';
|
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
|
||||||
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const dealDamage = new ValidatedMethod({
|
|
||||||
name: 'creatureProperties.dealDamage',
|
|
||||||
validate: new SimpleSchema({
|
|
||||||
creatureId: SimpleSchema.RegEx.Id,
|
|
||||||
damageType: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
amount: Number,
|
|
||||||
}).validator(),
|
|
||||||
mixins: [RateLimiterMixin],
|
|
||||||
rateLimit: {
|
|
||||||
numRequests: 20,
|
|
||||||
timeInterval: 5000,
|
|
||||||
},
|
|
||||||
run({creatureId, damageType, amount}) {
|
|
||||||
// permissions
|
|
||||||
let creature = Creatures.findOne(creatureId, {
|
|
||||||
fields: {
|
|
||||||
owner: 1,
|
|
||||||
readers: 1,
|
|
||||||
writers: 1,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
assertEditPermission(creature, this.userId);
|
|
||||||
|
|
||||||
const totalDamage = dealDamageWork({creature, damageType, amount})
|
|
||||||
computeCreature(creatureId);
|
|
||||||
return totalDamage;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export function dealDamageWork({creature, damageType, amount}){
|
|
||||||
// Get all the health bars and do damage to them
|
|
||||||
let healthBars = CreatureProperties.find({
|
|
||||||
'ancestors.id': creature._id,
|
|
||||||
type: 'attribute',
|
|
||||||
attributeType:'healthBar',
|
|
||||||
removed: {$ne: true},
|
|
||||||
inactive: {$ne: true},
|
|
||||||
}, {
|
|
||||||
sort: {order: -1},
|
|
||||||
});
|
|
||||||
//let multiplier = creature.damageMultipliers[damageType];
|
|
||||||
//if (multiplier === undefined) multiplier = 1;
|
|
||||||
//let totalDamage = Math.floor(amount * multiplier);
|
|
||||||
const totalDamage = amount;
|
|
||||||
let damageLeft = totalDamage;
|
|
||||||
if (damageType === 'healing') damageLeft = -totalDamage;
|
|
||||||
let propertyIds = [];
|
|
||||||
healthBars.forEach(healthBar => {
|
|
||||||
if (damageLeft === 0) return;
|
|
||||||
let damageAdded = damagePropertyWork({
|
|
||||||
property: healthBar,
|
|
||||||
operation: 'increment',
|
|
||||||
value: damageLeft,
|
|
||||||
});
|
|
||||||
damageLeft -= damageAdded;
|
|
||||||
propertyIds.push(healthBar._id);
|
|
||||||
});
|
|
||||||
return totalDamage;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default dealDamage;
|
|
||||||
@@ -9,7 +9,6 @@ import {
|
|||||||
renewDocIds
|
renewDocIds
|
||||||
} from '/imports/api/parenting/parenting.js';
|
} from '/imports/api/parenting/parenting.js';
|
||||||
import { reorderDocs } from '/imports/api/parenting/order.js';
|
import { reorderDocs } from '/imports/api/parenting/order.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
var snackbar;
|
var snackbar;
|
||||||
if (Meteor.isClient){
|
if (Meteor.isClient){
|
||||||
snackbar = require(
|
snackbar = require(
|
||||||
@@ -77,6 +76,9 @@ const duplicateProperty = new ValidatedMethod({
|
|||||||
|
|
||||||
// Order the root node
|
// Order the root node
|
||||||
property.order += 0.5;
|
property.order += 0.5;
|
||||||
|
|
||||||
|
// Mark the sheet as needing recompute
|
||||||
|
property.dirty = true;
|
||||||
|
|
||||||
// Insert the properties
|
// Insert the properties
|
||||||
CreatureProperties.batchInsert([property, ...nodes]);
|
CreatureProperties.batchInsert([property, ...nodes]);
|
||||||
@@ -87,9 +89,6 @@ const duplicateProperty = new ValidatedMethod({
|
|||||||
ancestorId: property.ancestors[0].id,
|
ancestorId: property.ancestors[0].id,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Inserting a creature property invalidates dependencies: full recompute
|
|
||||||
computeCreature(creature._id);
|
|
||||||
|
|
||||||
return propertyId;
|
return propertyId;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import { organizeDoc } from '/imports/api/parenting/organizeMethods.js';
|
import { organizeDoc } from '/imports/api/parenting/organizeMethods.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
import BUILT_IN_TAGS from '/imports/constants/BUILT_IN_TAGS.js';
|
import BUILT_IN_TAGS from '/imports/constants/BUILT_IN_TAGS.js';
|
||||||
import getParentRefByTag from '/imports/api/creature/creatureProperties/methods/getParentRefByTag.js';
|
import getParentRefByTag from '/imports/api/creature/creatureProperties/methods/getParentRefByTag.js';
|
||||||
|
|
||||||
@@ -29,7 +28,7 @@ const equipItem = new ValidatedMethod({
|
|||||||
let creature = getRootCreatureAncestor(item);
|
let creature = getRootCreatureAncestor(item);
|
||||||
assertEditPermission(creature, this.userId);
|
assertEditPermission(creature, this.userId);
|
||||||
CreatureProperties.update(_id, {
|
CreatureProperties.update(_id, {
|
||||||
$set: {equipped},
|
$set: { equipped, dirty: true },
|
||||||
}, {
|
}, {
|
||||||
selector: {type: 'item'},
|
selector: {type: 'item'},
|
||||||
});
|
});
|
||||||
@@ -46,8 +45,6 @@ const equipItem = new ValidatedMethod({
|
|||||||
order: Number.MAX_SAFE_INTEGER,
|
order: Number.MAX_SAFE_INTEGER,
|
||||||
skipRecompute: true,
|
skipRecompute: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
computeCreature(creature._id);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const flipToggle = new ValidatedMethod({
|
const flipToggle = new ValidatedMethod({
|
||||||
name: 'creatureProperties.flipToggle',
|
name: 'creatureProperties.flipToggle',
|
||||||
@@ -36,12 +35,10 @@ const flipToggle = new ValidatedMethod({
|
|||||||
CreatureProperties.update(_id, {$set: {
|
CreatureProperties.update(_id, {$set: {
|
||||||
enabled: !currentValue,
|
enabled: !currentValue,
|
||||||
disabled: currentValue,
|
disabled: currentValue,
|
||||||
|
dirty: true,
|
||||||
}}, {
|
}}, {
|
||||||
selector: {type: 'toggle'},
|
selector: {type: 'toggle'},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Updating a toggle is likely to change the whole tree, do a full recompute
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,28 @@ export default function getSlotFillFilter({slot, libraryIds}){
|
|||||||
slotFillerType: slot.slotType,
|
slotFillerType: slot.slotType,
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
|
} else if (slot.type === 'class') {
|
||||||
|
filter.$and.push({
|
||||||
|
$or: [{
|
||||||
|
type: 'classLevel',
|
||||||
|
},{
|
||||||
|
type: 'slotFiller',
|
||||||
|
slotFillerType: 'classLevel',
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
if (slot.variableName) {
|
||||||
|
filter.variableName = slot.variableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only search for levels the class needs
|
||||||
|
if (slot.missingLevels && slot.missingLevels.length) {
|
||||||
|
filter.level = {$in: slot.missingLevels};
|
||||||
|
} else {
|
||||||
|
filter.level = (slot.level || 0) + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let tagsOr = [];
|
let tagsOr = [];
|
||||||
let tagsNor = [];
|
let tagsNin = [];
|
||||||
if (slot.slotTags && slot.slotTags.length){
|
if (slot.slotTags && slot.slotTags.length){
|
||||||
tagsOr.push({tags: {$all: slot.slotTags}});
|
tagsOr.push({tags: {$all: slot.slotTags}});
|
||||||
}
|
}
|
||||||
@@ -27,15 +46,15 @@ export default function getSlotFillFilter({slot, libraryIds}){
|
|||||||
if (extra.operation === 'OR'){
|
if (extra.operation === 'OR'){
|
||||||
tagsOr.push({tags: {$all: extra.tags}});
|
tagsOr.push({tags: {$all: extra.tags}});
|
||||||
} else if (extra.operation === 'NOT'){
|
} else if (extra.operation === 'NOT'){
|
||||||
tagsNor.push({tags: {$all: extra.tags}});
|
tagsNin.push(...extra.tags);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (tagsOr.length){
|
if (tagsOr.length){
|
||||||
filter.$and.push({$or: tagsOr});
|
filter.$or = tagsOr;
|
||||||
}
|
}
|
||||||
if (tagsNor.length){
|
if (tagsNin.length){
|
||||||
filter.$and.push({$nor: tagsNor});
|
filter.$and.push({tags: {$nin: tagsNin}});
|
||||||
}
|
}
|
||||||
if (!filter.$and.length){
|
if (!filter.$and.length){
|
||||||
delete filter.$and;
|
delete filter.$and;
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
|
import '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
|
||||||
import '/imports/api/creature/creatureProperties/methods/damagePropertiesByName.js';
|
|
||||||
import '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
import '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||||
import '/imports/api/creature/creatureProperties/methods/dealDamage.js';
|
|
||||||
import '/imports/api/creature/creatureProperties/methods/duplicateProperty.js';
|
import '/imports/api/creature/creatureProperties/methods/duplicateProperty.js';
|
||||||
import '/imports/api/creature/creatureProperties/methods/equipItem.js';
|
import '/imports/api/creature/creatureProperties/methods/equipItem.js';
|
||||||
import '/imports/api/creature/creatureProperties/methods/insertProperty.js';
|
import '/imports/api/creature/creatureProperties/methods/insertProperty.js';
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/ge
|
|||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import { reorderDocs } from '/imports/api/parenting/order.js';
|
import { reorderDocs } from '/imports/api/parenting/order.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
import { getAncestry } from '/imports/api/parenting/parenting.js';
|
import { getAncestry } from '/imports/api/parenting/parenting.js';
|
||||||
import getParentRefByTag from '/imports/api/creature/creatureProperties/methods/getParentRefByTag.js';
|
import getParentRefByTag from '/imports/api/creature/creatureProperties/methods/getParentRefByTag.js';
|
||||||
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
|
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
|
||||||
@@ -132,14 +131,13 @@ const insertPropertyAsChildOfTag = new ValidatedMethod({
|
|||||||
|
|
||||||
export function insertPropertyWork({property, creature}){
|
export function insertPropertyWork({property, creature}){
|
||||||
delete property._id;
|
delete property._id;
|
||||||
|
property.dirty = true;
|
||||||
let _id = CreatureProperties.insert(property);
|
let _id = CreatureProperties.insert(property);
|
||||||
// Tree structure changed by insert, reorder the tree
|
// Tree structure changed by insert, reorder the tree
|
||||||
reorderDocs({
|
reorderDocs({
|
||||||
collection: CreatureProperties,
|
collection: CreatureProperties,
|
||||||
ancestorId: creature._id,
|
ancestorId: creature._id,
|
||||||
});
|
});
|
||||||
// Inserting a creature property invalidates dependencies: full recompute
|
|
||||||
computeCreature(creature._id);
|
|
||||||
return _id;
|
return _id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
|||||||
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
|
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
|
||||||
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
|
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import {
|
import {
|
||||||
setLineageOfDocs,
|
setLineageOfDocs,
|
||||||
@@ -71,9 +70,6 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
|
|||||||
collection: CreatureProperties,
|
collection: CreatureProperties,
|
||||||
ancestorId: rootCreature._id,
|
ancestorId: rootCreature._id,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Inserting a creature property invalidates dependencies: full recompute
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
// Return the docId of the last property, the inserted root property
|
// Return the docId of the last property, the inserted root property
|
||||||
return rootId;
|
return rootId;
|
||||||
},
|
},
|
||||||
@@ -135,12 +131,14 @@ function insertPropertyFromNode(nodeId, ancestors, order){
|
|||||||
node.order = order;
|
node.order = order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark all nodes as dirty
|
||||||
|
dirtyNodes(nodes);
|
||||||
|
|
||||||
// Insert the creature properties
|
// Insert the creature properties
|
||||||
CreatureProperties.batchInsert(nodes);
|
CreatureProperties.batchInsert(nodes);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function storeLibraryNodeReferences(nodes){
|
function storeLibraryNodeReferences(nodes){
|
||||||
nodes.forEach(node => {
|
nodes.forEach(node => {
|
||||||
if (node.libraryNodeId) return;
|
if (node.libraryNodeId) return;
|
||||||
@@ -148,6 +146,12 @@ function storeLibraryNodeReferences(nodes){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dirtyNodes(nodes) {
|
||||||
|
nodes.forEach(node => {
|
||||||
|
node.dirty = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Covert node references into actual nodes
|
// Covert node references into actual nodes
|
||||||
// TODO: check permissions for each library a reference node references
|
// TODO: check permissions for each library a reference node references
|
||||||
function reifyNodeReferences(nodes, visitedRefs = new Set(), depth = 0){
|
function reifyNodeReferences(nodes, visitedRefs = new Set(), depth = 0){
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const pullFromProperty = new ValidatedMethod({
|
const pullFromProperty = new ValidatedMethod({
|
||||||
name: 'creatureProperties.pull',
|
name: 'creatureProperties.pull',
|
||||||
@@ -21,15 +20,12 @@ const pullFromProperty = new ValidatedMethod({
|
|||||||
|
|
||||||
// Do work
|
// Do work
|
||||||
CreatureProperties.update(_id, {
|
CreatureProperties.update(_id, {
|
||||||
$pull: {[path.join('.')]: {_id: itemId}},
|
$pull: { [path.join('.')]: { _id: itemId } },
|
||||||
|
$set: { dirty: true }
|
||||||
}, {
|
}, {
|
||||||
selector: {type: property.type},
|
selector: {type: property.type},
|
||||||
getAutoValues: false,
|
getAutoValues: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO figure out if this method can change deps or not
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
// recomputePropertyDependencies(property);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
|
|
||||||
const pushToProperty = new ValidatedMethod({
|
const pushToProperty = new ValidatedMethod({
|
||||||
@@ -39,13 +38,11 @@ const pushToProperty = new ValidatedMethod({
|
|||||||
|
|
||||||
// Do work
|
// Do work
|
||||||
CreatureProperties.update(_id, {
|
CreatureProperties.update(_id, {
|
||||||
$push: {[joinedPath]: value},
|
$push: { [joinedPath]: value },
|
||||||
|
$set: { dirty: true },
|
||||||
}, {
|
}, {
|
||||||
selector: {type: property.type},
|
selector: {type: property.type},
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO figure out if this method can change deps or not
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
|||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import { restore } from '/imports/api/parenting/softRemove.js';
|
import { restore } from '/imports/api/parenting/softRemove.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const restoreProperty = new ValidatedMethod({
|
const restoreProperty = new ValidatedMethod({
|
||||||
name: 'creatureProperties.restore',
|
name: 'creatureProperties.restore',
|
||||||
@@ -24,10 +23,13 @@ const restoreProperty = new ValidatedMethod({
|
|||||||
assertEditPermission(rootCreature, this.userId);
|
assertEditPermission(rootCreature, this.userId);
|
||||||
|
|
||||||
// Do work
|
// Do work
|
||||||
restore({_id, collection: CreatureProperties});
|
restore({
|
||||||
|
_id,
|
||||||
// Changes dependency tree by restoring children
|
collection: CreatureProperties,
|
||||||
computeCreature(rootCreature._id);
|
extraUpdates: {
|
||||||
|
$set: { dirty: true }
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import SimpleSchema from 'simpl-schema';
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const selectAmmoItem = new ValidatedMethod({
|
const selectAmmoItem = new ValidatedMethod({
|
||||||
name: 'creatureProperties.selectAmmoItem',
|
name: 'creatureProperties.selectAmmoItem',
|
||||||
@@ -37,15 +36,10 @@ const selectAmmoItem = new ValidatedMethod({
|
|||||||
}
|
}
|
||||||
let path = `resources.itemsConsumed.${itemConsumedIndex}.itemId`;
|
let path = `resources.itemsConsumed.${itemConsumedIndex}.itemId`;
|
||||||
CreatureProperties.update(actionId, {
|
CreatureProperties.update(actionId, {
|
||||||
$set: {[path]: itemId}
|
$set: { [path]: itemId, dirty: true }
|
||||||
}, {
|
}, {
|
||||||
selector: action,
|
selector: action,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Changing the linked item does change the dependency tree
|
|
||||||
// TODO: We can predict exactly which deps will be affected instead of
|
|
||||||
// recomputing the entire creature
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
|||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import { softRemove } from '/imports/api/parenting/softRemove.js';
|
import { softRemove } from '/imports/api/parenting/softRemove.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const softRemoveProperty = new ValidatedMethod({
|
const softRemoveProperty = new ValidatedMethod({
|
||||||
name: 'creatureProperties.softRemove',
|
name: 'creatureProperties.softRemove',
|
||||||
@@ -25,9 +24,6 @@ const softRemoveProperty = new ValidatedMethod({
|
|||||||
|
|
||||||
// Do work
|
// Do work
|
||||||
softRemove({_id, collection: CreatureProperties});
|
softRemove({_id, collection: CreatureProperties});
|
||||||
|
|
||||||
// Changes dependency tree by removing children
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
|
|
||||||
const updateCreatureProperty = new ValidatedMethod({
|
const updateCreatureProperty = new ValidatedMethod({
|
||||||
name: 'creatureProperties.update',
|
name: 'creatureProperties.update',
|
||||||
@@ -37,17 +36,13 @@ const updateCreatureProperty = new ValidatedMethod({
|
|||||||
let modifier;
|
let modifier;
|
||||||
// unset empty values
|
// unset empty values
|
||||||
if (value === null || value === undefined){
|
if (value === null || value === undefined){
|
||||||
modifier = {$unset: {[pathString]: 1}};
|
modifier = { $unset: {[pathString]: 1}, $set: { dirty: true } };
|
||||||
} else {
|
} else {
|
||||||
modifier = {$set: {[pathString]: value}};
|
modifier = { $set: {[pathString]: value, dirty: true } };
|
||||||
}
|
}
|
||||||
CreatureProperties.update(_id, modifier, {
|
CreatureProperties.update(_id, modifier, {
|
||||||
selector: {type: property.type},
|
selector: {type: property.type},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Updating a property is likely to change dependencies, do a full recompute
|
|
||||||
// denormalised stats might change, so fetch the creature again
|
|
||||||
computeCreature(rootCreature._id);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
21
app/imports/api/creature/creatures/CreatureVariables.js
Normal file
21
app/imports/api/creature/creatures/CreatureVariables.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
//set up the collection for creature variables
|
||||||
|
let CreatureVariables = new Mongo.Collection('creatureVariables');
|
||||||
|
|
||||||
|
// Unique index on _creatureId
|
||||||
|
if (Meteor.isServer) {
|
||||||
|
CreatureVariables._ensureIndex({ _creatureId: 1 }, { unique: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
/** No schema because the structure isn't known until compute time
|
||||||
|
* Expect documents to looke like:
|
||||||
|
* {
|
||||||
|
* _id: "nE8Ngd6K4L4jSxLY2",
|
||||||
|
* _creatureId: "nE8Ngd6K4L4jSxLY2", // indexed reference to the creature
|
||||||
|
* explicitlyDefinedVariableName: {...some creatureProperty}
|
||||||
|
* implicitVariableName: {value: 10},
|
||||||
|
* undefinedVariableName: {},
|
||||||
|
* }
|
||||||
|
* Where top level fields that don't start with `_` are variables on the sheet
|
||||||
|
**/
|
||||||
|
|
||||||
|
export default CreatureVariables;
|
||||||
@@ -38,6 +38,11 @@ let CreatureSettingsSchema = new SimpleSchema({
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
optional: true,
|
optional: true,
|
||||||
},
|
},
|
||||||
|
// Hide calculation errors
|
||||||
|
hideCalculationErrors: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
// How much each hitDice resets on a long rest
|
// How much each hitDice resets on a long rest
|
||||||
hitDiceResetMultiplier: {
|
hitDiceResetMultiplier: {
|
||||||
type: Number,
|
type: Number,
|
||||||
@@ -80,6 +85,27 @@ let CreatureSchema = new SimpleSchema({
|
|||||||
optional: true,
|
optional: true,
|
||||||
max: STORAGE_LIMITS.url,
|
max: STORAGE_LIMITS.url,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Libraries
|
||||||
|
allowedLibraries: {
|
||||||
|
type: Array,
|
||||||
|
optional: true,
|
||||||
|
maxCount: 100,
|
||||||
|
},
|
||||||
|
'allowedLibraries.$': {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
allowedLibraryCollections: {
|
||||||
|
type: Array,
|
||||||
|
optional: true,
|
||||||
|
maxCount: 100,
|
||||||
|
},
|
||||||
|
'allowedLibraryCollections.$': {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
|
||||||
// Mechanics
|
// Mechanics
|
||||||
deathSave: {
|
deathSave: {
|
||||||
type: deathSaveSchema,
|
type: deathSaveSchema,
|
||||||
@@ -100,6 +126,11 @@ let CreatureSchema = new SimpleSchema({
|
|||||||
type: SimpleSchema.Integer,
|
type: SimpleSchema.Integer,
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
},
|
},
|
||||||
|
// Does the character need a recompute?
|
||||||
|
dirty: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
// Version of computation engine that was last used to compute this creature
|
// Version of computation engine that was last used to compute this creature
|
||||||
computeVersion: {
|
computeVersion: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -133,6 +164,7 @@ let CreatureSchema = new SimpleSchema({
|
|||||||
'computeErrors.$.details' : {
|
'computeErrors.$.details' : {
|
||||||
type: Object,
|
type: Object,
|
||||||
blackbox: true,
|
blackbox: true,
|
||||||
|
optional: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Tabletop
|
// Tabletop
|
||||||
@@ -159,8 +191,8 @@ CreatureSchema.extend(SharingSchema);
|
|||||||
Creatures.attachSchema(CreatureSchema);
|
Creatures.attachSchema(CreatureSchema);
|
||||||
|
|
||||||
|
|
||||||
import '/imports/api/creature/creatures/methods/index.js';
|
|
||||||
import '/imports/api/engine/actions/doAction.js';
|
|
||||||
|
|
||||||
export default Creatures;
|
export default Creatures;
|
||||||
export { CreatureSchema };
|
export { CreatureSchema };
|
||||||
|
|
||||||
|
import '/imports/api/engine/actions/doAction.js';
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export default function defaultCharacterProperties(creatureId){
|
|||||||
{
|
{
|
||||||
type: 'propertySlot',
|
type: 'propertySlot',
|
||||||
name: 'Ruleset',
|
name: 'Ruleset',
|
||||||
description: {text: '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.'},
|
description: {text: 'Choose a starting point for your character, this will define the basic setup of your character sheet. Without a base ruleset, your sheet will be empty.'},
|
||||||
slotTags: ['base'],
|
slotTags: ['base'],
|
||||||
tags: [],
|
tags: [],
|
||||||
quantityExpected: {calculation: '1'},
|
quantityExpected: {calculation: '1'},
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
|
||||||
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
|
||||||
|
export default function assertHasCharactersSlots(userId) {
|
||||||
|
if (characterSlotsRemaining(userId) <= 0) {
|
||||||
|
throw new Meteor.Error('characterSlotLimit',
|
||||||
|
'No character slots left')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function characterSlotsRemaining(userId) {
|
||||||
|
let tier = getUserTier(userId);
|
||||||
|
const currentCharacterCount = Creatures.find({
|
||||||
|
owner: userId,
|
||||||
|
}, {
|
||||||
|
fields: { _id: 1 },
|
||||||
|
}).count();
|
||||||
|
if (tier.characterSlots === -1) {
|
||||||
|
return Number.POSITIVE_INFINITY;
|
||||||
|
}
|
||||||
|
return tier.characterSlots - currentCharacterCount;
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import {assertEditPermission} from '/imports/api/sharing/sharingPermissions.js';
|
||||||
|
import SimpleSchema from 'simpl-schema';
|
||||||
|
import simpleSchemaMixin from '/imports/api/creature/mixins/simpleSchemaMixin.js';
|
||||||
|
|
||||||
|
const changeAllowedLibraries = new ValidatedMethod({
|
||||||
|
name: 'creatures.changeAllowedLibraries',
|
||||||
|
mixins: [RateLimiterMixin, simpleSchemaMixin],
|
||||||
|
schema: new SimpleSchema({
|
||||||
|
_id: {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
allowedLibraries: {
|
||||||
|
type: Array,
|
||||||
|
optional: true,
|
||||||
|
maxCount: 100,
|
||||||
|
},
|
||||||
|
'allowedLibraries.$': {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
allowedLibraryCollections: {
|
||||||
|
type: Array,
|
||||||
|
optional: true,
|
||||||
|
maxCount: 100,
|
||||||
|
},
|
||||||
|
'allowedLibraryCollections.$': {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
rateLimit: {
|
||||||
|
numRequests: 10,
|
||||||
|
timeInterval: 5000,
|
||||||
|
},
|
||||||
|
run({_id, allowedLibraries, allowedLibraryCollections}) {
|
||||||
|
let creature = Creatures.findOne(_id);
|
||||||
|
assertEditPermission(creature, this.userId);
|
||||||
|
let $set;
|
||||||
|
if (allowedLibraries) {
|
||||||
|
$set = { allowedLibraries }
|
||||||
|
}
|
||||||
|
if (allowedLibraryCollections) {
|
||||||
|
if (!$set) $set = {};
|
||||||
|
$set.allowedLibraryCollections = allowedLibraryCollections;
|
||||||
|
}
|
||||||
|
if (!$set) return;
|
||||||
|
Creatures.update(_id, {$set});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const toggleAllUserLibraries = new ValidatedMethod({
|
||||||
|
name: 'creatures.removeLibraryLimits',
|
||||||
|
mixins: [RateLimiterMixin, simpleSchemaMixin],
|
||||||
|
schema: new SimpleSchema({
|
||||||
|
_id: {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
value: {
|
||||||
|
type: Boolean,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
rateLimit: {
|
||||||
|
numRequests: 10,
|
||||||
|
timeInterval: 5000,
|
||||||
|
},
|
||||||
|
run({_id, value}) {
|
||||||
|
if (value) {
|
||||||
|
Creatures.update(_id, {
|
||||||
|
$unset: {
|
||||||
|
allowedLibraryCollections: 1,
|
||||||
|
allowedLibraries: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Creatures.update(_id, {
|
||||||
|
$set: {
|
||||||
|
allowedLibraryCollections: [],
|
||||||
|
allowedLibraries: [],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export {changeAllowedLibraries, toggleAllUserLibraries};
|
||||||
@@ -2,3 +2,4 @@ import '/imports/api/creature/creatures/methods/insertCreature.js';
|
|||||||
import '/imports/api/creature/creatures/methods/removeCreature.js';
|
import '/imports/api/creature/creatures/methods/removeCreature.js';
|
||||||
import '/imports/api/creature/creatures/methods/restCreature.js';
|
import '/imports/api/creature/creatures/methods/restCreature.js';
|
||||||
import '/imports/api/creature/creatures/methods/updateCreature.js';
|
import '/imports/api/creature/creatures/methods/updateCreature.js';
|
||||||
|
import '/imports/api/creature/creatures/methods/changeAllowedLibraries.js';
|
||||||
|
|||||||
@@ -1,70 +1,104 @@
|
|||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import simpleSchemaMixin from '/imports/api/creature/mixins/simpleSchemaMixin.js';
|
||||||
|
import Creatures, { CreatureSchema } from '/imports/api/creature/creatures/Creatures.js';
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
|
|
||||||
import defaultCharacterProperties from '/imports/api/creature/creatures/defaultCharacterProperties.js';
|
import defaultCharacterProperties from '/imports/api/creature/creatures/defaultCharacterProperties.js';
|
||||||
import insertPropertyFromLibraryNode from '/imports/api/creature/creatureProperties/methods/insertPropertyFromLibraryNode.js';
|
import insertPropertyFromLibraryNode from '/imports/api/creature/creatureProperties/methods/insertPropertyFromLibraryNode.js';
|
||||||
|
import assertHasCharactersSlots from '/imports/api/creature/creatures/methods/assertHasCharacterSlots.js';
|
||||||
|
import getSlotFillFilter from '/imports/api/creature/creatureProperties/methods/getSlotFillFilter.js';
|
||||||
|
import getCreatureLibraryIds from '/imports/api/library/getCreatureLibraryIds.js';
|
||||||
|
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
|
||||||
|
import { insertExperienceForCreature } from '/imports/api/creature/experience/Experiences.js';
|
||||||
|
import SimpleSchema from 'simpl-schema';
|
||||||
|
|
||||||
const insertCreature = new ValidatedMethod({
|
const insertCreature = new ValidatedMethod({
|
||||||
|
|
||||||
name: 'creatures.insertCreature',
|
name: 'creatures.insertCreature',
|
||||||
|
mixins: [RateLimiterMixin, simpleSchemaMixin],
|
||||||
validate: null,
|
schema: CreatureSchema.pick(
|
||||||
|
'name',
|
||||||
mixins: [RateLimiterMixin],
|
'gender',
|
||||||
|
'alignment',
|
||||||
|
'allowedLibraries',
|
||||||
|
'allowedLibraryCollections',
|
||||||
|
).extend({
|
||||||
|
'startingLevel': {
|
||||||
|
type: SimpleSchema.Integer,
|
||||||
|
min: 0,
|
||||||
|
},
|
||||||
|
}),
|
||||||
rateLimit: {
|
rateLimit: {
|
||||||
numRequests: 5,
|
numRequests: 5,
|
||||||
timeInterval: 5000,
|
timeInterval: 5000,
|
||||||
},
|
},
|
||||||
|
|
||||||
run() {
|
run({ name, gender, alignment, startingLevel,
|
||||||
if (!this.userId) {
|
allowedLibraries, allowedLibraryCollections }) {
|
||||||
|
const userId = this.userId
|
||||||
|
if (!userId) {
|
||||||
throw new Meteor.Error('Creatures.methods.insert.denied',
|
throw new Meteor.Error('Creatures.methods.insert.denied',
|
||||||
'You need to be logged in to insert a creature');
|
'You need to be logged in to insert a creature');
|
||||||
}
|
|
||||||
let tier = getUserTier(this.userId);
|
|
||||||
|
|
||||||
let currentCharacterCount = Creatures.find({
|
|
||||||
owner: this.userId,
|
|
||||||
}, {
|
|
||||||
fields: {_id: 1},
|
|
||||||
}).count();
|
|
||||||
|
|
||||||
if (
|
|
||||||
tier.characterSlots !== -1 &&
|
|
||||||
currentCharacterCount >= tier.characterSlots
|
|
||||||
){
|
|
||||||
throw new Meteor.Error('Creatures.methods.insert.denied',
|
|
||||||
`You are already at your limit of ${tier.characterSlots} characters`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the creature document
|
assertHasCharactersSlots(userId);
|
||||||
|
|
||||||
|
// Create the creature document
|
||||||
let creatureId = Creatures.insert({
|
let creatureId = Creatures.insert({
|
||||||
owner: this.userId,
|
owner: userId,
|
||||||
});
|
name,
|
||||||
|
gender,
|
||||||
|
alignment,
|
||||||
|
allowedLibraries,
|
||||||
|
allowedLibraryCollections,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Insert experience to get character to starting level
|
||||||
|
if (startingLevel) {
|
||||||
|
insertExperienceForCreature({
|
||||||
|
experience: {
|
||||||
|
name: 'Starting level',
|
||||||
|
levels: startingLevel,
|
||||||
|
creatureId
|
||||||
|
},
|
||||||
|
creatureId,
|
||||||
|
userId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Insert the default properties
|
// Insert the default properties
|
||||||
// Not batchInsert because we want the properties cleaned by the schema
|
// Not batchInsert because we want the properties cleaned by the schema
|
||||||
let baseId;
|
let baseId, rulesetSlot;
|
||||||
defaultCharacterProperties(creatureId).forEach(prop => {
|
defaultCharacterProperties(creatureId).forEach(prop => {
|
||||||
let id = CreatureProperties.insert(prop);
|
let id = CreatureProperties.insert(prop);
|
||||||
if (prop.name === 'Ruleset'){
|
if (prop.name === 'Ruleset'){
|
||||||
baseId = id;
|
baseId = id;
|
||||||
|
rulesetSlot = prop;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Meteor.isServer){
|
// If the user only has a single ruleset subscribed, use it by default
|
||||||
// Insert the 5e ruleset as the default base
|
if (Meteor.isServer) {
|
||||||
insertPropertyFromLibraryNode.call({
|
insertDefaultRuleset(creatureId, baseId, userId, rulesetSlot);
|
||||||
nodeIds: ['iHbhfcg3AL5isSWbw'],
|
|
||||||
parentRef: {id: baseId, collection: 'creatureProperties'},
|
|
||||||
order: 0.5,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return creatureId;
|
return creatureId;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If the user only has a single ruleset subscribed, insert it by default
|
||||||
|
function insertDefaultRuleset(creatureId, baseId, userId, slot) {
|
||||||
|
const libraryIds = getCreatureLibraryIds(creatureId, userId);
|
||||||
|
const filter = getSlotFillFilter({ slot, libraryIds });
|
||||||
|
const fillCursor = LibraryNodes.find(filter, { fields: { _id: 1 } });
|
||||||
|
const numRulesets = fillCursor.count();
|
||||||
|
if (numRulesets === 1) {
|
||||||
|
const ruleset = fillCursor.fetch()[0]
|
||||||
|
insertPropertyFromLibraryNode.call({
|
||||||
|
nodeIds: [ruleset._id],
|
||||||
|
parentRef: {id: baseId, collection: 'creatureProperties'},
|
||||||
|
order: 0.5,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default insertCreature;
|
export default insertCreature;
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import { assertOwnership } from '/imports/api/creature/creatures/creaturePermissions.js';
|
import { assertOwnership } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import CreatureLogs from '/imports/api/creature/log/CreatureLogs.js';
|
import CreatureLogs from '/imports/api/creature/log/CreatureLogs.js';
|
||||||
import Experiences from '/imports/api/creature/experience/Experiences.js';
|
import Experiences from '/imports/api/creature/experience/Experiences.js';
|
||||||
|
|
||||||
function removeRelatedDocuments(creatureId){
|
function removeRelatedDocuments(creatureId){
|
||||||
|
CreatureVariables.remove({_creatureId: creatureId});
|
||||||
CreatureProperties.remove({'ancestors.id': creatureId});
|
CreatureProperties.remove({'ancestors.id': creatureId});
|
||||||
CreatureLogs.remove({creatureId});
|
CreatureLogs.remove({creatureId});
|
||||||
Experiences.remove({creatureId});
|
Experiences.remove({creatureId});
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
import { union } from 'lodash';
|
||||||
|
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
|
||||||
|
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
const restCreature = new ValidatedMethod({
|
const restCreature = new ValidatedMethod({
|
||||||
name: 'creature.methods.longRest',
|
name: 'creature.methods.rest',
|
||||||
validate: new SimpleSchema({
|
validate: new SimpleSchema({
|
||||||
creatureId: {
|
creatureId: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -23,94 +24,121 @@ const restCreature = new ValidatedMethod({
|
|||||||
numRequests: 5,
|
numRequests: 5,
|
||||||
timeInterval: 5000,
|
timeInterval: 5000,
|
||||||
},
|
},
|
||||||
run({creatureId, restType}) {
|
run({ creatureId, restType }) {
|
||||||
let creature = Creatures.findOne(creatureId, {
|
// Get action context
|
||||||
fields: {
|
const actionContext = new ActionContext(creatureId, [creatureId], this);
|
||||||
owner: 1,
|
// Check permissions
|
||||||
writers: 1,
|
assertEditPermission(actionContext.creature, this.userId);
|
||||||
settings: 1,
|
|
||||||
}
|
|
||||||
}) ;
|
|
||||||
// Need edit permissions
|
|
||||||
assertEditPermission(creature, this.userId);
|
|
||||||
|
|
||||||
// Long rests reset short rest properties as well
|
// Join, sort, and apply before triggers
|
||||||
let resetFilter;
|
const beforeTriggers = union(
|
||||||
if (restType === 'shortRest'){
|
actionContext.triggers.anyRest?.before, actionContext.triggers[restType]?.before
|
||||||
resetFilter = 'shortRest'
|
).sort((a, b) => a.order - b.order);
|
||||||
} else {
|
applyTriggers(beforeTriggers, null, actionContext);
|
||||||
resetFilter = {$in: ['shortRest', 'longRest']}
|
|
||||||
}
|
// Rest
|
||||||
// Only apply to active properties
|
actionContext.addLog({
|
||||||
let filter = {
|
name: restType === 'shortRest' ? 'Short rest' : 'Long rest',
|
||||||
'ancestors.id': creatureId,
|
|
||||||
reset: resetFilter,
|
|
||||||
removed: {$ne: true},
|
|
||||||
inactive: {$ne: true},
|
|
||||||
};
|
|
||||||
// update all attribute's damage
|
|
||||||
filter.type = 'attribute';
|
|
||||||
CreatureProperties.update(filter, {
|
|
||||||
$set: {damage: 0}
|
|
||||||
}, {
|
|
||||||
selector: {type: 'attribute'},
|
|
||||||
multi: true,
|
|
||||||
});
|
});
|
||||||
// Update all action-like properties' usesUsed
|
doRestWork(restType, actionContext);
|
||||||
filter.type = {$in: [
|
|
||||||
'action',
|
// Join, sort, and apply after triggers
|
||||||
'attack',
|
const afterTriggers = union(
|
||||||
'spell'
|
actionContext.triggers.anyRest?.after, actionContext.triggers[restType]?.after
|
||||||
]};
|
).sort((a, b) => a.order - b.order);
|
||||||
CreatureProperties.update(filter, {
|
applyTriggers(afterTriggers, null, actionContext);
|
||||||
$set: {usesUsed: 0}
|
|
||||||
}, {
|
// Insert log
|
||||||
selector: {type: 'action'},
|
actionContext.writeLog();
|
||||||
multi: true,
|
|
||||||
});
|
|
||||||
// Reset half hit dice on a long rest, starting with the highest dice
|
|
||||||
if (restType === 'longRest'){
|
|
||||||
let hitDice = CreatureProperties.find({
|
|
||||||
'ancestors.id': creatureId,
|
|
||||||
type: 'attribute',
|
|
||||||
attributeType: 'hitDice',
|
|
||||||
removed: {$ne: true},
|
|
||||||
inactive: {$ne: true},
|
|
||||||
}, {
|
|
||||||
fields: {
|
|
||||||
hitDiceSize: 1,
|
|
||||||
damage: 1,
|
|
||||||
value: 1,
|
|
||||||
}
|
|
||||||
}).fetch();
|
|
||||||
// Use a collator to do sorting in natural order
|
|
||||||
let collator = new Intl.Collator('en', {
|
|
||||||
numeric: true, sensitivity: 'base'
|
|
||||||
});
|
|
||||||
// Get the hit dice in decending order of hitDiceSize
|
|
||||||
let compare = (a, b) => collator.compare(b.hitDiceSize, a.hitDiceSize)
|
|
||||||
hitDice.sort(compare);
|
|
||||||
// Get the total number of hit dice that can be recovered this rest
|
|
||||||
let totalHd = hitDice.reduce((sum, hd) => sum + (hd.value || 0), 0);
|
|
||||||
let resetMultiplier = creature.settings.hitDiceResetMultiplier || 0.5;
|
|
||||||
let recoverableHd = Math.max(Math.floor(totalHd*resetMultiplier), 1);
|
|
||||||
// recover each hit dice in turn until the recoverable amount is used up
|
|
||||||
let amountToRecover, resultingDamage;
|
|
||||||
hitDice.forEach(hd => {
|
|
||||||
if (!recoverableHd) return;
|
|
||||||
amountToRecover = Math.min(recoverableHd, hd.damage || 0);
|
|
||||||
if (!amountToRecover) return;
|
|
||||||
recoverableHd -= amountToRecover;
|
|
||||||
resultingDamage = hd.damage - amountToRecover;
|
|
||||||
CreatureProperties.update(hd._id, {
|
|
||||||
$set: {damage: resultingDamage}
|
|
||||||
}, {
|
|
||||||
selector: {type: 'attribute'},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
computeCreature(creatureId);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function doRestWork(restType, actionContext) {
|
||||||
|
const creatureId = actionContext.creature._id;
|
||||||
|
// Long rests reset short rest properties as well
|
||||||
|
let resetFilter;
|
||||||
|
if (restType === 'shortRest'){
|
||||||
|
resetFilter = 'shortRest'
|
||||||
|
} else {
|
||||||
|
resetFilter = {$in: ['shortRest', 'longRest']}
|
||||||
|
}
|
||||||
|
// Only apply to active properties
|
||||||
|
let filter = {
|
||||||
|
'ancestors.id': creatureId,
|
||||||
|
reset: resetFilter,
|
||||||
|
removed: { $ne: true },
|
||||||
|
inactive: { $ne: true },
|
||||||
|
};
|
||||||
|
// update all attribute's damage
|
||||||
|
filter.type = 'attribute';
|
||||||
|
CreatureProperties.update(filter, {
|
||||||
|
$set: {
|
||||||
|
damage: 0,
|
||||||
|
dirty: true,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
selector: {type: 'attribute'},
|
||||||
|
multi: true,
|
||||||
|
});
|
||||||
|
// Update all action-like properties' usesUsed
|
||||||
|
filter.type = {$in: [
|
||||||
|
'action',
|
||||||
|
'attack',
|
||||||
|
'spell'
|
||||||
|
]};
|
||||||
|
CreatureProperties.update(filter, {
|
||||||
|
$set: {
|
||||||
|
usesUsed: 0,
|
||||||
|
dirty: true,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
selector: {type: 'action'},
|
||||||
|
multi: true,
|
||||||
|
});
|
||||||
|
// Reset half hit dice on a long rest, starting with the highest dice
|
||||||
|
if (restType === 'longRest'){
|
||||||
|
let hitDice = CreatureProperties.find({
|
||||||
|
'ancestors.id': creatureId,
|
||||||
|
type: 'attribute',
|
||||||
|
attributeType: 'hitDice',
|
||||||
|
removed: {$ne: true},
|
||||||
|
inactive: {$ne: true},
|
||||||
|
}, {
|
||||||
|
fields: {
|
||||||
|
hitDiceSize: 1,
|
||||||
|
damage: 1,
|
||||||
|
total: 1,
|
||||||
|
}
|
||||||
|
}).fetch();
|
||||||
|
// Use a collator to do sorting in natural order
|
||||||
|
let collator = new Intl.Collator('en', {
|
||||||
|
numeric: true, sensitivity: 'base'
|
||||||
|
});
|
||||||
|
// Get the hit dice in decending order of hitDiceSize
|
||||||
|
let compare = (a, b) => collator.compare(b.hitDiceSize, a.hitDiceSize)
|
||||||
|
hitDice.sort(compare);
|
||||||
|
// Get the total number of hit dice that can be recovered this rest
|
||||||
|
let totalHd = hitDice.reduce((sum, hd) => sum + (hd.total || 0), 0);
|
||||||
|
let resetMultiplier = actionContext.creature.settings.hitDiceResetMultiplier || 0.5;
|
||||||
|
let recoverableHd = Math.max(Math.floor(totalHd*resetMultiplier), 1);
|
||||||
|
// recover each hit dice in turn until the recoverable amount is used up
|
||||||
|
let amountToRecover, resultingDamage;
|
||||||
|
hitDice.forEach(hd => {
|
||||||
|
if (!recoverableHd) return;
|
||||||
|
amountToRecover = Math.min(recoverableHd, hd.damage || 0);
|
||||||
|
if (!amountToRecover) return;
|
||||||
|
recoverableHd -= amountToRecover;
|
||||||
|
resultingDamage = hd.damage - amountToRecover;
|
||||||
|
CreatureProperties.update(hd._id, {
|
||||||
|
$set: {
|
||||||
|
damage: resultingDamage,
|
||||||
|
dirty: true,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
selector: {type: 'attribute'},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default restCreature;
|
export default restCreature;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
|
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
|
||||||
|
|
||||||
let Experiences = new Mongo.Collection('experiences');
|
let Experiences = new Mongo.Collection('experiences');
|
||||||
@@ -48,20 +47,20 @@ let ExperienceSchema = new SimpleSchema({
|
|||||||
Experiences.attachSchema(ExperienceSchema);
|
Experiences.attachSchema(ExperienceSchema);
|
||||||
|
|
||||||
const insertExperienceForCreature = function({experience, creatureId, userId}){
|
const insertExperienceForCreature = function({experience, creatureId, userId}){
|
||||||
assertEditPermission(creatureId, userId);
|
|
||||||
if (experience.xp){
|
if (experience.xp){
|
||||||
Creatures.update(creatureId, {$inc: {
|
Creatures.update(creatureId, {
|
||||||
'denormalizedStats.xp': experience.xp
|
$inc: { 'denormalizedStats.xp': experience.xp },
|
||||||
}});
|
$set: { dirty: true },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (experience.levels) {
|
if (experience.levels) {
|
||||||
Creatures.update(creatureId, {$inc: {
|
Creatures.update(creatureId, {
|
||||||
'denormalizedStats.milestoneLevels': experience.levels
|
$inc: { 'denormalizedStats.milestoneLevels': experience.levels },
|
||||||
}});
|
$set: { dirty: true },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
experience.creatureId = creatureId;
|
experience.creatureId = creatureId;
|
||||||
let id = Experiences.insert(experience);
|
let id = Experiences.insert(experience);
|
||||||
computeCreature(creatureId);
|
|
||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -93,6 +92,7 @@ const insertExperience = new ValidatedMethod({
|
|||||||
}
|
}
|
||||||
let insertedIds = [];
|
let insertedIds = [];
|
||||||
creatureIds.forEach(creatureId => {
|
creatureIds.forEach(creatureId => {
|
||||||
|
assertEditPermission(creatureId, userId);
|
||||||
let id = insertExperienceForCreature({experience, creatureId, userId});
|
let id = insertExperienceForCreature({experience, creatureId, userId});
|
||||||
insertedIds.push(id);
|
insertedIds.push(id);
|
||||||
});
|
});
|
||||||
@@ -124,18 +124,19 @@ const removeExperience = new ValidatedMethod({
|
|||||||
let creatureId = experience.creatureId
|
let creatureId = experience.creatureId
|
||||||
assertEditPermission(creatureId, userId);
|
assertEditPermission(creatureId, userId);
|
||||||
if (experience.xp){
|
if (experience.xp){
|
||||||
Creatures.update(creatureId, {$inc: {
|
Creatures.update(creatureId, {
|
||||||
'denormalizedStats.xp': -experience.xp
|
$inc: { 'denormalizedStats.xp': -experience.xp },
|
||||||
}});
|
$set: { dirty: true },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (experience.levels) {
|
if (experience.levels) {
|
||||||
Creatures.update(creatureId, {$inc: {
|
Creatures.update(creatureId, {
|
||||||
'denormalizedStats.milestoneLevels': -experience.levels
|
$inc: { 'denormalizedStats.milestoneLevels': -experience.levels },
|
||||||
}});
|
$set: { dirty: true },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
experience.creatureId = creatureId;
|
experience.creatureId = creatureId;
|
||||||
let numRemoved = Experiences.remove(experienceId);
|
let numRemoved = Experiences.remove(experienceId);
|
||||||
computeCreature(creatureId);
|
|
||||||
return numRemoved;
|
return numRemoved;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -173,11 +174,11 @@ const recomputeExperiences = new ValidatedMethod({
|
|||||||
});
|
});
|
||||||
Creatures.update(creatureId, {$set: {
|
Creatures.update(creatureId, {$set: {
|
||||||
'denormalizedStats.xp': xp,
|
'denormalizedStats.xp': xp,
|
||||||
'denormalizedStats.milestoneLevels': milestoneLevels
|
'denormalizedStats.milestoneLevels': milestoneLevels,
|
||||||
|
dirty: true,
|
||||||
}});
|
}});
|
||||||
computeCreature(creatureId);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default Experiences;
|
export default Experiences;
|
||||||
export { ExperienceSchema, insertExperience, removeExperience, recomputeExperiences };
|
export { ExperienceSchema, insertExperience, insertExperienceForCreature, removeExperience, recomputeExperiences };
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
|
||||||
import LogContentSchema from '/imports/api/creature/log/LogContentSchema.js';
|
import LogContentSchema from '/imports/api/creature/log/LogContentSchema.js';
|
||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
@@ -154,7 +155,6 @@ const logRoll = new ValidatedMethod({
|
|||||||
}).validator(),
|
}).validator(),
|
||||||
run({roll, creatureId}){
|
run({roll, creatureId}){
|
||||||
const creature = Creatures.findOne(creatureId, {fields: {
|
const creature = Creatures.findOne(creatureId, {fields: {
|
||||||
variables: 1,
|
|
||||||
readers: 1,
|
readers: 1,
|
||||||
writers: 1,
|
writers: 1,
|
||||||
owner: 1,
|
owner: 1,
|
||||||
@@ -163,6 +163,7 @@ const logRoll = new ValidatedMethod({
|
|||||||
avatarPicture: 1,
|
avatarPicture: 1,
|
||||||
}});
|
}});
|
||||||
assertEditPermission(creature, this.userId);
|
assertEditPermission(creature, this.userId);
|
||||||
|
const variables = CreatureVariables.findOne({ _creatureId: creatureId });
|
||||||
let logContent = []
|
let logContent = []
|
||||||
let parsedResult = undefined;
|
let parsedResult = undefined;
|
||||||
try {
|
try {
|
||||||
@@ -175,7 +176,7 @@ const logRoll = new ValidatedMethod({
|
|||||||
let {
|
let {
|
||||||
result: compiled,
|
result: compiled,
|
||||||
context
|
context
|
||||||
} = resolve('compile', parsedResult, creature.variables);
|
} = resolve('compile', parsedResult, variables);
|
||||||
const compiledString = toString(compiled);
|
const compiledString = toString(compiled);
|
||||||
if (!equalIgnoringWhitespace(compiledString, roll)) logContent.push({
|
if (!equalIgnoringWhitespace(compiledString, roll)) logContent.push({
|
||||||
value: roll
|
value: roll
|
||||||
@@ -183,12 +184,12 @@ const logRoll = new ValidatedMethod({
|
|||||||
logContent.push({
|
logContent.push({
|
||||||
value: compiledString
|
value: compiledString
|
||||||
});
|
});
|
||||||
let {result: rolled} = resolve('roll', compiled, creature.variables, context);
|
let {result: rolled} = resolve('roll', compiled, variables, context);
|
||||||
let rolledString = toString(rolled);
|
let rolledString = toString(rolled);
|
||||||
if (rolledString !== compiledString) logContent.push({
|
if (rolledString !== compiledString) logContent.push({
|
||||||
value: rolledString
|
value: rolledString
|
||||||
});
|
});
|
||||||
let {result} = resolve('reduce', rolled, creature.variables, context);
|
let {result} = resolve('reduce', rolled, variables, context);
|
||||||
let resultString = toString(result);
|
let resultString = toString(result);
|
||||||
if (resultString !== rolledString) logContent.push({
|
if (resultString !== rolledString) logContent.push({
|
||||||
value: resultString
|
value: resultString
|
||||||
@@ -210,4 +211,4 @@ const logRoll = new ValidatedMethod({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default CreatureLogs;
|
export default CreatureLogs;
|
||||||
export { CreatureLogSchema, insertCreatureLog, logRoll};
|
export { CreatureLogSchema, insertCreatureLog, logRoll, PER_CREATURE_LOG_LIMIT};
|
||||||
|
|||||||
78
app/imports/api/engine/actions/ActionContext.js
Normal file
78
app/imports/api/engine/actions/ActionContext.js
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/log/CreatureLogs.js';
|
||||||
|
import {
|
||||||
|
getCreature, getVariables, getPropertiesOfType
|
||||||
|
} from '/imports/api/engine/loadCreatures.js';
|
||||||
|
import { groupBy, remove } from 'lodash';
|
||||||
|
|
||||||
|
export default class ActionContext{
|
||||||
|
constructor(creatureId, targetIds = [], method) {
|
||||||
|
// Get the creature
|
||||||
|
this.creature = getCreature(creatureId)
|
||||||
|
|
||||||
|
if (!this.creature) {
|
||||||
|
throw new Meteor.Error('No Creature', `No creature could be found with id: ${creatureId}`)
|
||||||
|
}
|
||||||
|
// Create a log
|
||||||
|
this.log = CreatureLogSchema.clean({
|
||||||
|
creatureId: creatureId,
|
||||||
|
creatureName: this.creature.name,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get the variables of the acting creature
|
||||||
|
this.creature.variables = getVariables(creatureId);
|
||||||
|
delete this.creature.variables._id;
|
||||||
|
delete this.creature.variables._creatureId;
|
||||||
|
// Alias as scope
|
||||||
|
this.scope = this.creature.variables;
|
||||||
|
|
||||||
|
// Get the targets and their variables
|
||||||
|
this.targets = [];
|
||||||
|
targetIds.forEach(targetId => {
|
||||||
|
let target;
|
||||||
|
if (targetId === creatureId) {
|
||||||
|
target = this.creature;
|
||||||
|
} else {
|
||||||
|
target = getCreature(targetId);
|
||||||
|
target.variables = getVariables(targetId);
|
||||||
|
delete target.variables._id;
|
||||||
|
delete target.variables._creatureId;
|
||||||
|
}
|
||||||
|
this.targets.push(target);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Store a reference to the method for inserting the log
|
||||||
|
this.method = method;
|
||||||
|
|
||||||
|
// Get triggers
|
||||||
|
this.triggers = getPropertiesOfType(creatureId, 'trigger');
|
||||||
|
// Remove deleted or inactive triggers
|
||||||
|
remove(this.triggers, trigger => trigger.removed || trigger.inactive);
|
||||||
|
// Sort triggers by order
|
||||||
|
this.triggers.sort((a, b) => a.order - b.order);
|
||||||
|
// Group the triggers into triggers.<event>.<timing> or
|
||||||
|
// triggers.doActionProperty.<propertyType>.<timing>
|
||||||
|
this.triggers = groupBy(this.triggers, 'event');
|
||||||
|
for (let event in this.triggers) {
|
||||||
|
if (event === 'doActionProperty') {
|
||||||
|
this.triggers[event] = groupBy(this.triggers[event], 'actionPropertyType');
|
||||||
|
for (let propertyType in this.triggers[event]) {
|
||||||
|
this.triggers[event][propertyType] = groupBy(this.triggers[event][propertyType], 'timing');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.triggers[event] = groupBy(this.triggers[event], 'timing');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addLog(content) {
|
||||||
|
if (content.name || content.value){
|
||||||
|
this.log.content.push(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeLog() {
|
||||||
|
insertCreatureLogWork({
|
||||||
|
log: this.log,
|
||||||
|
creature: this.creature,
|
||||||
|
method: this.method,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import action from './applyPropertyByType/applyAction.js';
|
|||||||
import adjustment from './applyPropertyByType/applyAdjustment.js';
|
import adjustment from './applyPropertyByType/applyAdjustment.js';
|
||||||
import branch from './applyPropertyByType/applyBranch.js';
|
import branch from './applyPropertyByType/applyBranch.js';
|
||||||
import buff from './applyPropertyByType/applyBuff.js';
|
import buff from './applyPropertyByType/applyBuff.js';
|
||||||
|
import buffRemover from './applyPropertyByType/applyBuffRemover.js';
|
||||||
import damage from './applyPropertyByType/applyDamage.js';
|
import damage from './applyPropertyByType/applyDamage.js';
|
||||||
import note from './applyPropertyByType/applyNote.js';
|
import note from './applyPropertyByType/applyNote.js';
|
||||||
import roll from './applyPropertyByType/applyRoll.js';
|
import roll from './applyPropertyByType/applyRoll.js';
|
||||||
@@ -13,6 +14,7 @@ const applyPropertyByType = {
|
|||||||
adjustment,
|
adjustment,
|
||||||
branch,
|
branch,
|
||||||
buff,
|
buff,
|
||||||
|
buffRemover,
|
||||||
damage,
|
damage,
|
||||||
note,
|
note,
|
||||||
roll,
|
roll,
|
||||||
@@ -21,7 +23,7 @@ const applyPropertyByType = {
|
|||||||
toggle,
|
toggle,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function applyProperty(node, opts, ...rest){
|
export default function applyProperty(node, actionContext, ...rest) {
|
||||||
opts.scope[`#${node.node.type}`] = node.node;
|
actionContext.scope[`#${node.node.type}`] = node.node;
|
||||||
return applyPropertyByType[node.node.type]?.(node, opts, ...rest);
|
applyPropertyByType[node.node.type]?.(node, actionContext, ...rest);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,23 +6,24 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
|||||||
import { adjustQuantityWork } from '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
|
import { adjustQuantityWork } from '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
|
||||||
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||||
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyAction(node, {creature, targets, scope, log}){
|
export default function applyAction(node, actionContext) {
|
||||||
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
if (prop.target === 'self') targets = [creature];
|
if (prop.target === 'self') actionContext.targets = [actionContext.creature];
|
||||||
|
const targets = actionContext.targets;
|
||||||
|
|
||||||
// Log the name and description
|
// Log the name and summary
|
||||||
let content = { name: prop.name };
|
let content = { name: prop.name };
|
||||||
if (prop.description?.text){
|
if (prop.summary?.text){
|
||||||
recalculateInlineCalculations(prop.description, scope, log);
|
recalculateInlineCalculations(prop.summary, actionContext);
|
||||||
content.value = prop.description.value;
|
content.value = prop.summary.value;
|
||||||
}
|
|
||||||
if (content.name || content.value){
|
|
||||||
log.content.push(content);
|
|
||||||
}
|
}
|
||||||
|
if (!prop.silent) actionContext.addLog(content);
|
||||||
|
|
||||||
// Spend the resources
|
// Spend the resources
|
||||||
const failed = spendResources({prop, log, scope});
|
const failed = spendResources(prop, actionContext);
|
||||||
if (failed) return;
|
if (failed) return;
|
||||||
|
|
||||||
const attack = prop.attackRoll || prop.attackRollBonus;
|
const attack = prop.attackRoll || prop.attackRollBonus;
|
||||||
@@ -31,28 +32,29 @@ export default function applyAction(node, {creature, targets, scope, log}){
|
|||||||
if (attack && attack.calculation){
|
if (attack && attack.calculation){
|
||||||
if (targets.length){
|
if (targets.length){
|
||||||
targets.forEach(target => {
|
targets.forEach(target => {
|
||||||
applyAttackToTarget({attack, target, scope, log});
|
applyAttackToTarget({attack, target, actionContext});
|
||||||
// Apply the children, but only to the current target
|
// Apply the children, but only to the current target
|
||||||
applyChildren(node, {targets: [target], scope, log});
|
actionContext.targets = [target];
|
||||||
|
applyChildren(node, actionContext);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
applyAttackWithoutTarget({attack, scope, log});
|
applyAttackWithoutTarget({attack, actionContext});
|
||||||
applyChildren(node, {creature, targets, scope, log});
|
applyChildren(node, actionContext);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
applyChildren(node, {creature, targets, scope, log});
|
applyChildren(node, actionContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyAttackWithoutTarget({attack, scope, log}){
|
function applyAttackWithoutTarget({attack, actionContext}){
|
||||||
delete scope['$attackHit'];
|
delete actionContext.scope['$attackHit'];
|
||||||
delete scope['$attackMiss'];
|
delete actionContext.scope['$attackMiss'];
|
||||||
delete scope['$criticalHit'];
|
delete actionContext.scope['$criticalHit'];
|
||||||
delete scope['$criticalMiss'];
|
delete actionContext.scope['$criticalMiss'];
|
||||||
delete scope['$attackRoll'];
|
delete actionContext.scope['$attackRoll'];
|
||||||
|
|
||||||
recalculateCalculation(attack, scope, log);
|
|
||||||
|
|
||||||
|
recalculateCalculation(attack, actionContext);
|
||||||
|
const scope = actionContext.scope;
|
||||||
let {
|
let {
|
||||||
resultPrefix,
|
resultPrefix,
|
||||||
result,
|
result,
|
||||||
@@ -65,14 +67,22 @@ function applyAttackWithoutTarget({attack, scope, log}){
|
|||||||
} else if(scope['$attackAdvantage'] === -1){
|
} else if(scope['$attackAdvantage'] === -1){
|
||||||
name += ' (Disadvantage)';
|
name += ' (Disadvantage)';
|
||||||
}
|
}
|
||||||
log.content.push({
|
if (!criticalMiss){
|
||||||
|
scope['$attackHit'] = {value: true}
|
||||||
|
}
|
||||||
|
if (!criticalHit){
|
||||||
|
scope['$attackMiss'] = {value: true};
|
||||||
|
}
|
||||||
|
|
||||||
|
actionContext.addLog({
|
||||||
name,
|
name,
|
||||||
value: `${resultPrefix}\n**${result}**`,
|
value: `${resultPrefix}\n**${result}**`,
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyAttackToTarget({attack, target, scope, log}){
|
function applyAttackToTarget({attack, target, actionContext}){
|
||||||
|
const scope = actionContext.scope;
|
||||||
delete scope['$attackHit'];
|
delete scope['$attackHit'];
|
||||||
delete scope['$attackMiss'];
|
delete scope['$attackMiss'];
|
||||||
delete scope['$criticalHit'];
|
delete scope['$criticalHit'];
|
||||||
@@ -80,7 +90,7 @@ function applyAttackToTarget({attack, target, scope, log}){
|
|||||||
delete scope['$attackDiceRoll'];
|
delete scope['$attackDiceRoll'];
|
||||||
delete scope['$attackRoll'];
|
delete scope['$attackRoll'];
|
||||||
|
|
||||||
recalculateCalculation(attack, scope, log);
|
recalculateCalculation(attack, actionContext);
|
||||||
|
|
||||||
let {
|
let {
|
||||||
resultPrefix,
|
resultPrefix,
|
||||||
@@ -101,22 +111,22 @@ function applyAttackToTarget({attack, target, scope, log}){
|
|||||||
name += ' (Disadvantage)';
|
name += ' (Disadvantage)';
|
||||||
}
|
}
|
||||||
|
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name,
|
name,
|
||||||
value: `${resultPrefix}\n**${result}**`,
|
value: `${resultPrefix}\n**${result}**`,
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
if ((result > armor) || (criticalHit)){
|
if (criticalMiss || result < armor){
|
||||||
scope['$attackHit'] = true;
|
scope['$attackMiss'] = {value: true};
|
||||||
} else {
|
} else {
|
||||||
scope['$attackMiss'] = true;
|
scope['$attackHit'] = {value: true};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value:'Target has no `armor`',
|
value:'Target has no `armor`',
|
||||||
});
|
});
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: criticalHit ? 'Critical Hit!' : criticalMiss ? 'Critical Miss!' : 'To Hit',
|
name: criticalHit ? 'Critical Hit!' : criticalMiss ? 'Critical Miss!' : 'To Hit',
|
||||||
value: `${resultPrefix}\n**${result}**`,
|
value: `${resultPrefix}\n**${result}**`,
|
||||||
inline: true,
|
inline: true,
|
||||||
@@ -127,7 +137,7 @@ function applyAttackToTarget({attack, target, scope, log}){
|
|||||||
function rollAttack(attack, scope){
|
function rollAttack(attack, scope){
|
||||||
const rollModifierText = numberToSignedString(attack.value, true);
|
const rollModifierText = numberToSignedString(attack.value, true);
|
||||||
let value, resultPrefix;
|
let value, resultPrefix;
|
||||||
if (attack.advantage === 1 || scope['$attackAdvantage']){
|
if (scope['$attackAdvantage'] === 1){
|
||||||
const [a, b] = rollDice(2, 20);
|
const [a, b] = rollDice(2, 20);
|
||||||
if (a >= b) {
|
if (a >= b) {
|
||||||
value = a;
|
value = a;
|
||||||
@@ -136,7 +146,7 @@ function rollAttack(attack, scope){
|
|||||||
value = b;
|
value = b;
|
||||||
resultPrefix = `1d20 [ ~~${a}~~, ${b} ] ${rollModifierText}`;
|
resultPrefix = `1d20 [ ~~${a}~~, ${b} ] ${rollModifierText}`;
|
||||||
}
|
}
|
||||||
} else if (attack.advantage === -1 || scope['$attackDisadvantage']){
|
} else if (scope['$attackAdvantage'] === -1){
|
||||||
const [a, b] = rollDice(2, 20);
|
const [a, b] = rollDice(2, 20);
|
||||||
if (a <= b) {
|
if (a <= b) {
|
||||||
value = a;
|
value = a;
|
||||||
@@ -161,28 +171,24 @@ function applyCrits(value, scope){
|
|||||||
let criticalMiss;
|
let criticalMiss;
|
||||||
if (criticalHit){
|
if (criticalHit){
|
||||||
scope['$criticalHit'] = {value: true};
|
scope['$criticalHit'] = {value: true};
|
||||||
scope['$attackHit'] = {value: true};
|
|
||||||
} else {
|
} else {
|
||||||
criticalMiss = value === 1;
|
criticalMiss = value === 1;
|
||||||
if (criticalMiss){
|
if (criticalMiss){
|
||||||
scope['$criticalMiss'] = 1;
|
scope['$criticalMiss'] = {value: true};
|
||||||
scope['$attackMiss'] = {value: true};
|
|
||||||
} else {
|
|
||||||
// Untargeted attacks hit by default
|
|
||||||
scope['$attackHit'] = {value: true}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {criticalHit, criticalMiss};
|
return {criticalHit, criticalMiss};
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyChildren(node, args){
|
function applyChildren(node, actionContext) {
|
||||||
node.children.forEach(child => applyProperty(child, args));
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
function spendResources({prop, log, scope}){
|
function spendResources(prop, actionContext){
|
||||||
// Check Uses
|
// Check Uses
|
||||||
if (prop.usesLeft <= 0){
|
if (prop.usesLeft <= 0){
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value: `${prop.name || 'action'} does not have enough uses left`,
|
value: `${prop.name || 'action'} does not have enough uses left`,
|
||||||
});
|
});
|
||||||
@@ -190,7 +196,7 @@ function spendResources({prop, log, scope}){
|
|||||||
}
|
}
|
||||||
// Resources
|
// Resources
|
||||||
if (prop.insufficientResources){
|
if (prop.insufficientResources){
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value: 'This creature doesn\'t have sufficient resources to perform this action',
|
value: 'This creature doesn\'t have sufficient resources to perform this action',
|
||||||
});
|
});
|
||||||
@@ -202,7 +208,7 @@ function spendResources({prop, log, scope}){
|
|||||||
let gainLog = [];
|
let gainLog = [];
|
||||||
try {
|
try {
|
||||||
prop.resources.itemsConsumed.forEach(itemConsumed => {
|
prop.resources.itemsConsumed.forEach(itemConsumed => {
|
||||||
recalculateCalculation(itemConsumed.quantity, scope, log);
|
recalculateCalculation(itemConsumed.quantity, actionContext);
|
||||||
if (!itemConsumed.itemId){
|
if (!itemConsumed.itemId){
|
||||||
throw 'No ammo was selected for this prop';
|
throw 'No ammo was selected for this prop';
|
||||||
}
|
}
|
||||||
@@ -233,7 +239,7 @@ function spendResources({prop, log, scope}){
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (e){
|
} catch (e){
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value: e,
|
value: e,
|
||||||
});
|
});
|
||||||
@@ -251,7 +257,7 @@ function spendResources({prop, log, scope}){
|
|||||||
}, {
|
}, {
|
||||||
selector: prop
|
selector: prop
|
||||||
});
|
});
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Uses left',
|
name: 'Uses left',
|
||||||
value: prop.usesLeft - 1,
|
value: prop.usesLeft - 1,
|
||||||
inline: true,
|
inline: true,
|
||||||
@@ -260,18 +266,19 @@ function spendResources({prop, log, scope}){
|
|||||||
|
|
||||||
// Damage stats
|
// Damage stats
|
||||||
prop.resources.attributesConsumed.forEach(attConsumed => {
|
prop.resources.attributesConsumed.forEach(attConsumed => {
|
||||||
recalculateCalculation(attConsumed.quantity, scope, log);
|
recalculateCalculation(attConsumed.quantity, actionContext);
|
||||||
|
|
||||||
if (!attConsumed.quantity?.value) return;
|
if (!attConsumed.quantity?.value) return;
|
||||||
let stat = scope[attConsumed.variableName];
|
let stat = actionContext.scope[attConsumed.variableName];
|
||||||
if (!stat){
|
if (!stat){
|
||||||
spendLog.push(stat.name + ': ' + ' not found');
|
spendLog.push(stat.name + ': ' + ' not found');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
damagePropertyWork({
|
damagePropertyWork({
|
||||||
property: stat,
|
prop: stat,
|
||||||
operation: 'increment',
|
operation: 'increment',
|
||||||
value: attConsumed.quantity.value,
|
value: attConsumed.quantity.value,
|
||||||
|
actionContext,
|
||||||
});
|
});
|
||||||
if (attConsumed.quantity.value > 0){
|
if (attConsumed.quantity.value > 0){
|
||||||
spendLog.push(stat.name + ': ' + attConsumed.quantity.value);
|
spendLog.push(stat.name + ': ' + attConsumed.quantity.value);
|
||||||
@@ -281,12 +288,12 @@ function spendResources({prop, log, scope}){
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Log all the spending
|
// Log all the spending
|
||||||
if (gainLog.length) log.content.push({
|
if (gainLog.length && !prop.silent) actionContext.addLog({
|
||||||
name: 'Gained',
|
name: 'Gained',
|
||||||
value: gainLog.join('\n'),
|
value: gainLog.join('\n'),
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
if (spendLog.length) log.content.push({
|
if (spendLog.length && !prop.silent) actionContext.addLog({
|
||||||
name: 'Spent',
|
name: 'Spent',
|
||||||
value: spendLog.join('\n'),
|
value: spendLog.join('\n'),
|
||||||
inline: true,
|
inline: true,
|
||||||
|
|||||||
@@ -1,41 +1,42 @@
|
|||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import recalculateCalculation from './shared/recalculateCalculation.js';
|
import recalculateCalculation from './shared/recalculateCalculation.js';
|
||||||
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyAdjustment(node, {
|
export default function applyAdjustment(node, actionContext){
|
||||||
creature, targets, scope, log
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
}){
|
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
const damageTargets = prop.target === 'self' ? [creature] : targets;
|
const damageTargets = prop.target === 'self' ? [actionContext.creature] : actionContext.targets;
|
||||||
|
|
||||||
if (!prop.amount) {
|
if (!prop.amount) {
|
||||||
return applyChildren(node, {creature, targets, scope, log});
|
return applyChildren(node, actionContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate the amount
|
// Evaluate the amount
|
||||||
recalculateCalculation(prop.amount, scope, log);
|
recalculateCalculation(prop.amount, actionContext);
|
||||||
|
|
||||||
const value = +prop.amount.value;
|
const value = +prop.amount.value;
|
||||||
if (!isFinite(value)) {
|
if (!isFinite(value)) {
|
||||||
return applyChildren(node, {creature, targets, scope, log});
|
return applyChildren(node, actionContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (damageTargets?.length) {
|
if (damageTargets?.length) {
|
||||||
damageTargets.forEach(target => {
|
damageTargets.forEach(target => {
|
||||||
let stat = target.variables[prop.stat];
|
let stat = target.variables[prop.stat];
|
||||||
if (!stat?.type) {
|
if (!stat?.type) {
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value: `Could not apply attribute damage, creature does not have \`${prop.stat}\` set`
|
value: `Could not apply attribute damage, creature does not have \`${prop.stat}\` set`
|
||||||
});
|
});
|
||||||
return applyChildren(node, {creature, targets, scope, log});
|
return applyChildren(node, actionContext);
|
||||||
}
|
}
|
||||||
damagePropertyWork({
|
damagePropertyWork({
|
||||||
property: stat,
|
prop: stat,
|
||||||
operation: prop.operation,
|
operation: prop.operation,
|
||||||
value: value,
|
value,
|
||||||
|
actionContext,
|
||||||
});
|
});
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Attribute damage',
|
name: 'Attribute damage',
|
||||||
value: `${prop.stat}${prop.operation === 'set' ? ' set to' : ''}` +
|
value: `${prop.stat}${prop.operation === 'set' ? ' set to' : ''}` +
|
||||||
` ${value}`,
|
` ${value}`,
|
||||||
@@ -43,7 +44,7 @@ export default function applyAdjustment(node, {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Attribute damage',
|
name: 'Attribute damage',
|
||||||
value: `${prop.stat}${prop.operation === 'set' ? ' set to' : ''}` +
|
value: `${prop.stat}${prop.operation === 'set' ? ' set to' : ''}` +
|
||||||
` ${value}`,
|
` ${value}`,
|
||||||
@@ -51,9 +52,10 @@ export default function applyAdjustment(node, {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return applyChildren(node, {creature, targets, scope, log});
|
return applyChildren(node, actionContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyChildren(node, args){
|
function applyChildren(node, actionContext){
|
||||||
node.children.forEach(child => applyProperty(child, args));
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +1,76 @@
|
|||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import recalculateCalculation from './shared/recalculateCalculation.js';
|
import recalculateCalculation from './shared/recalculateCalculation.js';
|
||||||
import rollDice from '/imports/parser/rollDice.js';
|
import rollDice from '/imports/parser/rollDice.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyBranch(node, {
|
export default function applyBranch(node, actionContext){
|
||||||
creature, targets, scope, log
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
}){
|
|
||||||
const applyChildren = function(){
|
const applyChildren = function(){
|
||||||
node.children.forEach(child => applyProperty(child, {
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
creature, targets, scope, log
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
}));
|
|
||||||
};
|
};
|
||||||
|
const scope = actionContext.scope;
|
||||||
|
const targets = actionContext.targets;
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
switch(prop.branchType){
|
switch(prop.branchType){
|
||||||
case 'if':
|
case 'if':
|
||||||
recalculateCalculation(prop.condition, scope, log);
|
recalculateCalculation(prop.condition, actionContext);
|
||||||
if (prop.condition?.value) applyChildren();
|
if (prop.condition?.value) applyChildren();
|
||||||
break;
|
break;
|
||||||
|
case 'index':
|
||||||
|
if (node.children.length){
|
||||||
|
recalculateCalculation(prop.condition, actionContext);
|
||||||
|
if (!isFinite(prop.condition?.value)) {
|
||||||
|
actionContext.addLog({
|
||||||
|
name: 'Branch Error',
|
||||||
|
value: 'Index did not resolve into a valid number'
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let index = Math.floor(prop.condition?.value);
|
||||||
|
if (index < 1) index = 1;
|
||||||
|
if (index > node.children.length) index = node.children.length;
|
||||||
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
applyProperty(node.children[index - 1], actionContext);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'hit':
|
case 'hit':
|
||||||
if (scope['$attackHit']?.value) applyChildren();
|
if (scope['$attackHit']?.value){
|
||||||
|
if (!targets.length && !prop.silent) actionContext.addLog({value: '**On hit**'});
|
||||||
|
applyChildren();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'miss':
|
case 'miss':
|
||||||
if (scope['$attackMiss']?.value) applyChildren();
|
if (scope['$attackMiss']?.value){
|
||||||
|
if (!targets.length && !prop.silent) actionContext.addLog({value: '**On miss**'});
|
||||||
|
applyChildren();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'failedSave':
|
case 'failedSave':
|
||||||
if (scope['$saveFailed']?.value) applyChildren();
|
if (scope['$saveFailed']?.value){
|
||||||
|
if (!targets.length && !prop.silent) actionContext.addLog({value: '**On failed save**'});
|
||||||
|
applyChildren();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'successfulSave':
|
case 'successfulSave':
|
||||||
if (scope['$saveSucceeded']?.value) applyChildren();
|
if (scope['$saveSucceeded']?.value){
|
||||||
|
if (!targets.length && !prop.silent) actionContext.addLog({value: '**On save**',});
|
||||||
|
applyChildren();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'random':
|
case 'random':
|
||||||
if (node.children.length){
|
if (node.children.length){
|
||||||
let index = rollDice(1, node.children.length)[0] - 1;
|
let index = rollDice(1, node.children.length)[0] - 1;
|
||||||
applyProperty(node.children[index], {
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
creature, targets, scope, log
|
applyProperty(node.children[index], actionContext);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'eachTarget':
|
case 'eachTarget':
|
||||||
if (targets.length){
|
if (targets.length) {
|
||||||
targets.forEach(target => {
|
targets.forEach(target => {
|
||||||
node.children.forEach(child => applyProperty(child, {
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
creature, targets: [target], scope, log
|
actionContext.targets = [target]
|
||||||
}));
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
applyChildren();
|
applyChildren();
|
||||||
|
|||||||
@@ -10,30 +10,64 @@ import { get } from 'lodash';
|
|||||||
import resolve, { map, toString } from '/imports/parser/resolve.js';
|
import resolve, { map, toString } from '/imports/parser/resolve.js';
|
||||||
import symbol from '/imports/parser/parseTree/symbol.js';
|
import symbol from '/imports/parser/parseTree/symbol.js';
|
||||||
import logErrors from './shared/logErrors.js';
|
import logErrors from './shared/logErrors.js';
|
||||||
|
import { insertCreatureLog } from '/imports/api/creature/log/CreatureLogs.js';
|
||||||
import cyrb53 from '/imports/api/engine/computation/utility/cyrb53.js';
|
import cyrb53 from '/imports/api/engine/computation/utility/cyrb53.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
import INLINE_CALCULATION_REGEX from '/imports/constants/INLINE_CALCULTION_REGEX.js';
|
||||||
|
|
||||||
export default function applyBuff(node, {creature, targets, scope, log}){
|
export default function applyBuff(node, actionContext){
|
||||||
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
let buffTargets = prop.target === 'self' ? [creature] : targets;
|
let buffTargets = prop.target === 'self' ? [actionContext.creature] : actionContext.targets;
|
||||||
|
|
||||||
// Then copy the decendants of the buff to the targets
|
// Then copy the decendants of the buff to the targets
|
||||||
let propList = [prop];
|
let propList = [prop];
|
||||||
function addChildrenToPropList(children){
|
function addChildrenToPropList(children, { skipCrystalize } = {}){
|
||||||
children.forEach(child => {
|
children.forEach(child => {
|
||||||
|
if (skipCrystalize) child.node._skipCrystalize = true;
|
||||||
propList.push(child.node);
|
propList.push(child.node);
|
||||||
addChildrenToPropList(child.children);
|
// recursively add the child's children, but don't crystalize nested buffs
|
||||||
|
addChildrenToPropList(child.children, {
|
||||||
|
skipCrystalize: skipCrystalize || child.node.type === 'buff'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
addChildrenToPropList(node.children);
|
addChildrenToPropList(node.children);
|
||||||
crystalizeVariables({propList, scope, log});
|
if (!prop.skipCrystalization) {
|
||||||
|
crystalizeVariables({propList, actionContext});
|
||||||
|
}
|
||||||
|
|
||||||
let oldParent = {
|
let oldParent = {
|
||||||
id: prop.parent.id,
|
id: prop.parent.id,
|
||||||
collection: prop.parent.collection,
|
collection: prop.parent.collection,
|
||||||
};
|
};
|
||||||
buffTargets.forEach(target => {
|
buffTargets.forEach(target => {
|
||||||
|
// Apply the buff
|
||||||
copyNodeListToTarget(propList, target, oldParent);
|
copyNodeListToTarget(propList, target, oldParent);
|
||||||
|
|
||||||
|
//Log the buff
|
||||||
|
if ((prop.name || prop.description?.value) && !prop.silent){
|
||||||
|
if (target._id === actionContext.creature._id){
|
||||||
|
// Targeting self
|
||||||
|
actionContext.addLog({
|
||||||
|
name: prop.name,
|
||||||
|
value: prop.description?.value,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Targeting other
|
||||||
|
insertCreatureLog.call({
|
||||||
|
log: {
|
||||||
|
creatureId: target._id,
|
||||||
|
content: [{
|
||||||
|
name: prop.name,
|
||||||
|
value: prop.description?.value,
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
|
||||||
// Don't apply the children of the buff, they get copied to the target instead
|
// Don't apply the children of the buff, they get copied to the target instead
|
||||||
}
|
}
|
||||||
@@ -59,8 +93,13 @@ function copyNodeListToTarget(propList, target, oldParent){
|
|||||||
* Replaces all variables with their resolved values
|
* Replaces all variables with their resolved values
|
||||||
* except variables of the form `$target.thing.total` become `thing.total`
|
* except variables of the form `$target.thing.total` become `thing.total`
|
||||||
*/
|
*/
|
||||||
function crystalizeVariables({propList, scope, log}){
|
function crystalizeVariables({propList, actionContext}){
|
||||||
propList.forEach(prop => {
|
propList.forEach(prop => {
|
||||||
|
if (prop._skipCrystalize) {
|
||||||
|
delete prop._skipCrystalize;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Iterate through all the calculations and crystalize them
|
||||||
computedSchemas[prop.type].computedFields().forEach( calcKey => {
|
computedSchemas[prop.type].computedFields().forEach( calcKey => {
|
||||||
applyFnToKey(prop, calcKey, (prop, key) => {
|
applyFnToKey(prop, calcKey, (prop, key) => {
|
||||||
const calcObj = get(prop, key);
|
const calcObj = get(prop, key);
|
||||||
@@ -80,7 +119,7 @@ function crystalizeVariables({propList, scope, log}){
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Can't strip symbols
|
// Can't strip symbols
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value: 'Variable `$target` should not be used without a property: $target.property',
|
value: 'Variable `$target` should not be used without a property: $target.property',
|
||||||
});
|
});
|
||||||
@@ -88,8 +127,8 @@ function crystalizeVariables({propList, scope, log}){
|
|||||||
return node;
|
return node;
|
||||||
} else {
|
} else {
|
||||||
// Resolve all other variables
|
// Resolve all other variables
|
||||||
const {result, context} = resolve('reduce', node, scope);
|
const {result, context} = resolve('reduce', node, actionContext.scope);
|
||||||
logErrors(context.errors, log);
|
logErrors(context.errors, actionContext);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -97,5 +136,36 @@ function crystalizeVariables({propList, scope, log}){
|
|||||||
calcObj.hash = cyrb53(calcObj.calculation);
|
calcObj.hash = cyrb53(calcObj.calculation);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
// For each key in the schema
|
||||||
|
computedSchemas[prop.type].inlineCalculationFields().forEach( calcKey => {
|
||||||
|
// That ends in .inlineCalculations
|
||||||
|
applyFnToKey(prop, calcKey, (prop, key) => {
|
||||||
|
const inlineCalcObj = get(prop, key);
|
||||||
|
if (!inlineCalcObj) return;
|
||||||
|
|
||||||
|
// If there is no text, skip
|
||||||
|
if (!inlineCalcObj.text){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace all the existing calculations
|
||||||
|
let index = -1;
|
||||||
|
inlineCalcObj.text = inlineCalcObj.text.replace(INLINE_CALCULATION_REGEX, () => {
|
||||||
|
index += 1;
|
||||||
|
return `{${inlineCalcObj.inlineCalculations[index].calculation}}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set the value to the uncomputed string
|
||||||
|
inlineCalcObj.value = inlineCalcObj.text;
|
||||||
|
|
||||||
|
// Write a new hash
|
||||||
|
const inlineCalcHash = cyrb53(inlineCalcObj.text);
|
||||||
|
if (inlineCalcHash === inlineCalcObj.hash) {
|
||||||
|
// Skip if nothing changed
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
inlineCalcObj.hash = inlineCalcHash;
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
import { findLast, difference, intersection, filter } from 'lodash';
|
||||||
|
import applyProperty from '../applyProperty.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
import { getProperyAncestors, getPropertiesOfType } from '/imports/api/engine/loadCreatures.js';
|
||||||
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
|
import { softRemove } from '/imports/api/parenting/softRemove.js';
|
||||||
|
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
|
||||||
|
|
||||||
|
export default function applyBuffRemover(node, actionContext) {
|
||||||
|
// Apply triggers
|
||||||
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
|
|
||||||
|
const prop = node.node;
|
||||||
|
|
||||||
|
// Log Name
|
||||||
|
if (prop.name && !prop.silent){
|
||||||
|
actionContext.addLog({ name: prop.name });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove buffs
|
||||||
|
if (prop.targetParentBuff) {
|
||||||
|
// Remove nearest ancestor buff
|
||||||
|
const ancestors = getProperyAncestors(actionContext.creature._id, prop._id);
|
||||||
|
const nearestBuff = findLast(ancestors, ancestor => ancestor.type === 'buff');
|
||||||
|
if (!nearestBuff) {
|
||||||
|
actionContext.addLog({
|
||||||
|
name: 'Error',
|
||||||
|
value: 'Buff remover does not have a parent buff to remove',
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
removeBuff(nearestBuff, actionContext, prop);
|
||||||
|
} else {
|
||||||
|
// Get all the buffs targeted by tags
|
||||||
|
const allBuffs = getPropertiesOfType(actionContext.creature._id, 'buff');
|
||||||
|
const targetedBuffs = filter(allBuffs, buff => {
|
||||||
|
if (buff.inactive) return false;
|
||||||
|
if (buffRemoverMatchTags(prop, buff)) return true;
|
||||||
|
});
|
||||||
|
// Remove the buffs
|
||||||
|
if (prop.removeAll) {
|
||||||
|
// Remove all matching buffs
|
||||||
|
targetedBuffs.forEach(buff => {
|
||||||
|
removeBuff(buff, actionContext, prop);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Sort in reverse order
|
||||||
|
targetedBuffs.sort((a, b) => b.order - a.order);
|
||||||
|
// Remove the one with the highest order
|
||||||
|
const buff = targetedBuffs[0];
|
||||||
|
if (buff) {
|
||||||
|
removeBuff(buff, actionContext, prop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply triggers
|
||||||
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
// Apply children
|
||||||
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeBuff(buff, actionContext, prop) {
|
||||||
|
if (!prop.silent) actionContext.addLog({
|
||||||
|
name: 'Removed',
|
||||||
|
value: `${buff.name || 'Buff'}`
|
||||||
|
});
|
||||||
|
softRemove({ _id: buff._id, collection: CreatureProperties });
|
||||||
|
}
|
||||||
|
|
||||||
|
function buffRemoverMatchTags(buffRemover, prop) {
|
||||||
|
let matched = false;
|
||||||
|
const propTags = getEffectivePropTags(prop);
|
||||||
|
// Check the target tags
|
||||||
|
if (
|
||||||
|
!buffRemover.targetTags?.length ||
|
||||||
|
difference(buffRemover.targetTags, propTags).length === 0
|
||||||
|
) {
|
||||||
|
matched = true;
|
||||||
|
}
|
||||||
|
// Check the extra tags
|
||||||
|
buffRemover.extraTags?.forEach(extra => {
|
||||||
|
if (extra.operation === 'OR') {
|
||||||
|
if (matched) return;
|
||||||
|
if (
|
||||||
|
!extra.tags.length ||
|
||||||
|
difference(extra.tags, propTags).length === 0
|
||||||
|
) {
|
||||||
|
matched = true;
|
||||||
|
}
|
||||||
|
} else if (extra.operation === 'NOT') {
|
||||||
|
if (
|
||||||
|
extra.tags.length &&
|
||||||
|
intersection(extra.tags, propTags)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return matched;
|
||||||
|
}
|
||||||
@@ -1,27 +1,30 @@
|
|||||||
|
import { some, intersection, difference, remove } from 'lodash';
|
||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import { dealDamageWork } from '/imports/api/creature/creatureProperties/methods/dealDamage.js';
|
|
||||||
import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
|
import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
|
||||||
import resolve, { Context, toString } from '/imports/parser/resolve.js';
|
import resolve, { Context, toString } from '/imports/parser/resolve.js';
|
||||||
import logErrors from './shared/logErrors.js';
|
import logErrors from './shared/logErrors.js';
|
||||||
import applyEffectsToCalculationParseNode from '/imports/api/engine/actions/applyPropertyByType/shared/applyEffectsToCalculationParseNode.js';
|
import applyEffectsToCalculationParseNode from '/imports/api/engine/actions/applyPropertyByType/shared/applyEffectsToCalculationParseNode.js';
|
||||||
|
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||||
|
import {
|
||||||
|
getPropertiesOfType
|
||||||
|
} from '/imports/api/engine/loadCreatures.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyDamage(node, {
|
export default function applyDamage(node, actionContext){
|
||||||
creature, targets, scope, log
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
}){
|
|
||||||
const applyChildren = function(){
|
const applyChildren = function(){
|
||||||
node.children.forEach(child => applyProperty(child, {
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
creature, targets, scope, log
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
|
const scope = actionContext.scope;
|
||||||
|
|
||||||
// Skip if there is no parse node to work with
|
// Skip if there is no parse node to work with
|
||||||
if (!prop.amount.parseNode) return;
|
if (!prop.amount?.parseNode) return;
|
||||||
|
|
||||||
// Choose target
|
// Choose target
|
||||||
|
let damageTargets = prop.target === 'self' ? [actionContext.creature] : actionContext.targets;
|
||||||
let damageTargets = prop.target === 'self' ? [creature] : targets;
|
|
||||||
// Determine if the hit is critical
|
// Determine if the hit is critical
|
||||||
let criticalHit = scope['$criticalHit']?.value &&
|
let criticalHit = scope['$criticalHit']?.value &&
|
||||||
prop.damageType !== 'healing' // Can't critically heal
|
prop.damageType !== 'healing' // Can't critically heal
|
||||||
@@ -35,23 +38,20 @@ export default function applyDamage(node, {
|
|||||||
const logValue = [];
|
const logValue = [];
|
||||||
const logName = prop.damageType === 'healing' ? 'Healing' : 'Damage';
|
const logName = prop.damageType === 'healing' ? 'Healing' : 'Damage';
|
||||||
|
|
||||||
// Compile the dice roll and store that string first
|
|
||||||
// const {result: compiled} = resolve('compiled', prop.amount.parseNode, scope, context);
|
|
||||||
// logValue.push(toString(compiled));
|
|
||||||
// logErrors(context.errors, log);
|
|
||||||
|
|
||||||
// roll the dice only and store that string
|
// roll the dice only and store that string
|
||||||
applyEffectsToCalculationParseNode(prop.amount, log);
|
applyEffectsToCalculationParseNode(prop.amount, actionContext.log);
|
||||||
const {result: rolled} = resolve('roll', prop.amount.parseNode, scope, context);
|
const {result: rolled} = resolve('roll', prop.amount.parseNode, scope, context);
|
||||||
logValue.push(toString(rolled));
|
if (rolled.parseType !== 'constant'){
|
||||||
logErrors(context.errors, log);
|
logValue.push(toString(rolled));
|
||||||
|
}
|
||||||
|
logErrors(context.errors, actionContext);
|
||||||
|
|
||||||
// Reset the errors so we don't log the same errors twice
|
// Reset the errors so we don't log the same errors twice
|
||||||
context.errors = [];
|
context.errors = [];
|
||||||
|
|
||||||
// Resolve the roll to a final value
|
// Resolve the roll to a final value
|
||||||
const {result: reduced} = resolve('reduce', rolled, scope, context);
|
const {result: reduced} = resolve('reduce', rolled, scope, context);
|
||||||
logErrors(context.errors, log);
|
logErrors(context.errors, actionContext);
|
||||||
|
|
||||||
// Store the result
|
// Store the result
|
||||||
if (reduced.parseType === 'constant'){
|
if (reduced.parseType === 'constant'){
|
||||||
@@ -61,14 +61,25 @@ export default function applyDamage(node, {
|
|||||||
} else {
|
} else {
|
||||||
prop.amount.value = toString(reduced);
|
prop.amount.value = toString(reduced);
|
||||||
}
|
}
|
||||||
|
let damage = +reduced.value;
|
||||||
const damage = +reduced.value;
|
|
||||||
|
|
||||||
// If we didn't end up with a constant of finite amount, give up
|
// If we didn't end up with a constant of finite amount, give up
|
||||||
if (reduced?.parseType !== 'constant' && !isFinite(reduced.value)){
|
if (reduced?.parseType !== 'constant' || !isFinite(reduced.value)){
|
||||||
return applyChildren();
|
return applyChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Round the damage to a whole number
|
||||||
|
damage = Math.floor(damage);
|
||||||
|
|
||||||
|
// Convert extra damage into the stored type
|
||||||
|
if (prop.damageType === 'extra' && scope['$lastDamageType']) {
|
||||||
|
prop.damageType = scope['$lastDamageType'];
|
||||||
|
}
|
||||||
|
// Store current damage type
|
||||||
|
if (prop.damageType !== 'healing') {
|
||||||
|
scope['$lastDamageType'] = prop.damageType;
|
||||||
|
}
|
||||||
|
|
||||||
// Memoise the damage suffix for the log
|
// Memoise the damage suffix for the log
|
||||||
let suffix = (criticalHit ? ' critical ' : ' ') +
|
let suffix = (criticalHit ? ' critical ' : ' ') +
|
||||||
prop.damageType +
|
prop.damageType +
|
||||||
@@ -78,15 +89,25 @@ export default function applyDamage(node, {
|
|||||||
// Iterate through all the targets
|
// Iterate through all the targets
|
||||||
damageTargets.forEach(target => {
|
damageTargets.forEach(target => {
|
||||||
|
|
||||||
|
// Apply weaknesses/resistances/immunities
|
||||||
|
damage = applyDamageMultipliers({
|
||||||
|
target,
|
||||||
|
damage,
|
||||||
|
damageProp: prop,
|
||||||
|
logValue
|
||||||
|
});
|
||||||
|
|
||||||
|
actionContext.target = [target];
|
||||||
// Deal the damage to the target
|
// Deal the damage to the target
|
||||||
let damageDealt = dealDamageWork({
|
let damageDealt = dealDamage({
|
||||||
creature: target,
|
target,
|
||||||
damageType: prop.damageType,
|
damageType: prop.damageType,
|
||||||
amount: damage,
|
amount: damage,
|
||||||
|
actionContext
|
||||||
});
|
});
|
||||||
|
|
||||||
// Log the damage done
|
// Log the damage done
|
||||||
if (target._id === creature._id){
|
if (target._id === actionContext.creature._id){
|
||||||
// Target is same as self, log damage as such
|
// Target is same as self, log damage as such
|
||||||
logValue.push(`**${damageDealt}** ${suffix} to self`);
|
logValue.push(`**${damageDealt}** ${suffix} to self`);
|
||||||
} else {
|
} else {
|
||||||
@@ -107,10 +128,114 @@ export default function applyDamage(node, {
|
|||||||
// There are no targets, just log the result
|
// There are no targets, just log the result
|
||||||
logValue.push(`**${damage}** ${suffix}`);
|
logValue.push(`**${damage}** ${suffix}`);
|
||||||
}
|
}
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: logName,
|
name: logName,
|
||||||
value: logValue.join('\n'),
|
value: logValue.join('\n'),
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
return applyChildren();
|
return applyChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applyDamageMultipliers({target, damage, damageProp, logValue}){
|
||||||
|
const damageType = damageProp?.damageType;
|
||||||
|
if (!damageType) return damage;
|
||||||
|
|
||||||
|
const multiplier = target?.variables?.[damageType];
|
||||||
|
if (!multiplier) return damage;
|
||||||
|
|
||||||
|
const damageTypeText = damageType == 'healing' ? 'healing': `${damageType} damage`;
|
||||||
|
|
||||||
|
if (
|
||||||
|
multiplier.immunity &&
|
||||||
|
some(multiplier.immunities, multiplierAppliesTo(damageProp))
|
||||||
|
){
|
||||||
|
logValue.push(`Immune to ${damageTypeText}`);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
if (
|
||||||
|
multiplier.resistance &&
|
||||||
|
some(multiplier.resistances, multiplierAppliesTo(damageProp))
|
||||||
|
){
|
||||||
|
logValue.push(`Resistant to ${damageTypeText}`);
|
||||||
|
damage = Math.floor(damage / 2);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
multiplier.vulnerability &&
|
||||||
|
some(multiplier.vulnerabilities, multiplierAppliesTo(damageProp))
|
||||||
|
){
|
||||||
|
logValue.push(`Vulnerable to ${damageTypeText}`);
|
||||||
|
damage = Math.floor(damage * 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return damage;
|
||||||
|
}
|
||||||
|
|
||||||
|
function multiplierAppliesTo(damageProp){
|
||||||
|
return multiplier => {
|
||||||
|
const hasRequiredTags = difference(
|
||||||
|
multiplier.includeTags, damageProp.tags
|
||||||
|
).length === 0;
|
||||||
|
|
||||||
|
const hasNoExcludedTags = intersection(
|
||||||
|
multiplier.excludeTags, damageProp.tags
|
||||||
|
).length === 0;
|
||||||
|
|
||||||
|
return hasRequiredTags && hasNoExcludedTags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function dealDamage({target, damageType, amount, actionContext}){
|
||||||
|
// Get all the health bars and do damage to them
|
||||||
|
let healthBars = getPropertiesOfType(target._id, 'attribute');
|
||||||
|
|
||||||
|
// Keep only the healthbars that can take damage/healing
|
||||||
|
remove(healthBars, (bar) =>
|
||||||
|
bar.attributeType !== 'healthBar' ||
|
||||||
|
bar.inactive ||
|
||||||
|
bar.removed ||
|
||||||
|
bar.overridden ||
|
||||||
|
(amount >= 0 && bar.healthBarNoDamage) ||
|
||||||
|
(amount < 0 && bar.healthBarNoHealing)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sort healthbars by damage/healing order or tree order as a fallback
|
||||||
|
healthBars.sort((a, b) => {
|
||||||
|
let diff;
|
||||||
|
if (amount >= 0) {
|
||||||
|
diff = a.healthBarDamageOrder - b.healthBarDamageOrder;
|
||||||
|
} else {
|
||||||
|
diff = a.healthBarHealingOrder - b.healthBarHealingOrder;
|
||||||
|
}
|
||||||
|
if (Number.isFinite(diff)) {
|
||||||
|
return diff;
|
||||||
|
} else {
|
||||||
|
return a.order - b.order;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Deal the damage to each healthbar in order until all damage is done
|
||||||
|
const totalDamage = amount;
|
||||||
|
let damageLeft = totalDamage;
|
||||||
|
if (damageType === 'healing') damageLeft = -totalDamage;
|
||||||
|
healthBars.forEach(healthBar => {
|
||||||
|
if (damageLeft === 0) return;
|
||||||
|
// Replace the healthbar by the one in the action context if we can
|
||||||
|
// The damagePropertyWork function bashes the prop with the damage
|
||||||
|
// So we can use the new value in later action properties
|
||||||
|
if (healthBar.variableName) {
|
||||||
|
const targetHealthBar = target.variables[healthBar.variableName];
|
||||||
|
if (targetHealthBar?._id === healthBar._id) {
|
||||||
|
healthBar = targetHealthBar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Do the damage
|
||||||
|
let damageAdded = damagePropertyWork({
|
||||||
|
prop: healthBar,
|
||||||
|
operation: 'increment',
|
||||||
|
value: damageLeft,
|
||||||
|
actionContext
|
||||||
|
});
|
||||||
|
damageLeft -= damageAdded;
|
||||||
|
});
|
||||||
|
return totalDamage;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,25 +1,27 @@
|
|||||||
import recalculateInlineCalculations from './shared/recalculateInlineCalculations.js';
|
import recalculateInlineCalculations from './shared/recalculateInlineCalculations.js';
|
||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyNote(node, {creature, targets, scope, log}){
|
export default function applyNote(node, actionContext){
|
||||||
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
|
|
||||||
// Log Name, summary
|
// Log Name, summary
|
||||||
let content = { name: prop.name };
|
let content = { name: prop.name };
|
||||||
if (prop.summary?.text){
|
if (prop.summary?.text){
|
||||||
recalculateInlineCalculations(prop.summary, scope, log);
|
recalculateInlineCalculations(prop.summary, actionContext);
|
||||||
content.value = prop.summary.value;
|
content.value = prop.summary.value;
|
||||||
}
|
}
|
||||||
if (content.name || content.value){
|
if (content.name || content.value){
|
||||||
log.content.push(content);
|
actionContext.addLog(content);
|
||||||
}
|
}
|
||||||
// Log description
|
// Log description
|
||||||
if (prop.description?.text){
|
if (prop.description?.text){
|
||||||
recalculateInlineCalculations(prop.description, scope, log);
|
recalculateInlineCalculations(prop.description, actionContext);
|
||||||
log.content.push({value: prop.description.value});
|
actionContext.addLog({value: prop.description.value});
|
||||||
}
|
}
|
||||||
|
// Apply triggers
|
||||||
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
// Apply children
|
// Apply children
|
||||||
node.children.forEach(child => applyProperty(child, {
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
creature, targets, scope, log
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,61 @@
|
|||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import recalculateCalculation from './shared/recalculateCalculation.js';
|
import logErrors from './shared/logErrors.js';
|
||||||
|
import applyEffectsToCalculationParseNode from '/imports/api/engine/actions/applyPropertyByType/shared/applyEffectsToCalculationParseNode.js';
|
||||||
|
import resolve, { toString } from '/imports/parser/resolve.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyRoll(node, {creature, targets, scope, log}){
|
export default function applyRoll(node, actionContext){
|
||||||
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
|
|
||||||
if (prop.roll?.calculation){
|
const applyChildren = function(){
|
||||||
recalculateCalculation(prop.roll, scope, log);
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
|
};
|
||||||
|
|
||||||
if (isFinite(prop.roll.value)){
|
if (prop.roll?.calculation){
|
||||||
scope[prop.variableName] = prop.roll.value;
|
const logValue = [];
|
||||||
|
|
||||||
|
// roll the dice only and store that string
|
||||||
|
applyEffectsToCalculationParseNode(prop.roll, actionContext);
|
||||||
|
const {result: rolled, context} = resolve('roll', prop.roll.parseNode, actionContext.scope);
|
||||||
|
if (rolled.parseType !== 'constant'){
|
||||||
|
logValue.push(toString(rolled));
|
||||||
|
}
|
||||||
|
logErrors(context.errors, actionContext);
|
||||||
|
|
||||||
|
// Reset the errors so we don't log the same errors twice
|
||||||
|
context.errors = [];
|
||||||
|
|
||||||
|
// Resolve the roll to a final value
|
||||||
|
const {result: reduced} = resolve('reduce', rolled, actionContext.scope, context);
|
||||||
|
logErrors(context.errors, actionContext);
|
||||||
|
|
||||||
|
// Store the result
|
||||||
|
if (reduced.parseType === 'constant'){
|
||||||
|
prop.roll.value = reduced.value;
|
||||||
|
} else if (reduced.parseType === 'error'){
|
||||||
|
prop.roll.value = null;
|
||||||
|
} else {
|
||||||
|
prop.roll.value = toString(reduced);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we didn't end up with a constant of finite amount, give up
|
||||||
|
if (reduced?.parseType !== 'constant' || !isFinite(reduced.value)){
|
||||||
|
return applyChildren();
|
||||||
|
}
|
||||||
|
const value = reduced.value;
|
||||||
|
|
||||||
|
actionContext.scope[prop.variableName] = value;
|
||||||
|
logValue.push(`**${value}**`);
|
||||||
|
|
||||||
|
if (!prop.silent){
|
||||||
|
actionContext.addLog({
|
||||||
|
name: prop.name,
|
||||||
|
value: logValue.join('\n'),
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
log.content.push({
|
|
||||||
name: prop.name,
|
|
||||||
value: prop.variableName + ' = ' + prop.roll.calculation + ' = ' + prop.roll.value,
|
|
||||||
inline: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return node.children.forEach(child => applyProperty(child, {
|
return applyChildren();
|
||||||
creature, targets, scope, log
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,46 +2,57 @@ import rollDice from '/imports/parser/rollDice.js';
|
|||||||
import recalculateCalculation from './shared/recalculateCalculation.js';
|
import recalculateCalculation from './shared/recalculateCalculation.js';
|
||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applySavingThrow(node, {creature, targets, scope, log}){
|
export default function applySavingThrow(node, actionContext){
|
||||||
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
|
|
||||||
let saveTargets = prop.target === 'self' ? [creature] : targets;
|
let saveTargets = prop.target === 'self' ? [actionContext.creature] : actionContext.targets;
|
||||||
|
|
||||||
recalculateCalculation(prop.dc, scope, log);
|
recalculateCalculation(prop.dc, actionContext);
|
||||||
|
|
||||||
const dc = (prop.dc?.value);
|
const dc = (prop.dc?.value);
|
||||||
if (!isFinite(dc)){
|
if (!isFinite(dc)){
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: 'Error',
|
name: 'Error',
|
||||||
value: 'Saving throw requires a DC',
|
value: 'Saving throw requires a DC',
|
||||||
});
|
});
|
||||||
return node.children.forEach(child => applyProperty(child, {
|
return node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
creature, targets, scope, log
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: prop.name,
|
name: prop.name,
|
||||||
value: ' DC ' + dc,
|
value: `DC **${dc}**`,
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
|
const scope = actionContext.scope;
|
||||||
|
|
||||||
|
// If there are no save targets, apply all children as if the save both
|
||||||
|
// succeeeded and failed
|
||||||
|
if (!saveTargets?.length){
|
||||||
|
scope['$saveFailed'] = {value: true};
|
||||||
|
scope['$saveSucceeded'] = { value: true };
|
||||||
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
|
return node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each target makes the saving throw
|
||||||
saveTargets.forEach(target => {
|
saveTargets.forEach(target => {
|
||||||
delete scope['$saveFailed'];
|
delete scope['$saveFailed'];
|
||||||
delete scope['$saveSucceeded'];
|
delete scope['$saveSucceeded'];
|
||||||
delete scope['$saveDiceRoll'];
|
delete scope['$saveDiceRoll'];
|
||||||
delete scope['$saveRoll'];
|
delete scope['$saveRoll'];
|
||||||
|
|
||||||
const applyChildren = function(){
|
const applyChildren = function () {
|
||||||
node.children.forEach(child => applyProperty(child, {
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
creature, targets: [target], scope, log
|
actionContext.targets = [target]
|
||||||
}));
|
node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
};
|
};
|
||||||
|
|
||||||
const save = target.variables[prop.stat];
|
const save = target.variables[prop.stat];
|
||||||
|
|
||||||
if (!save){
|
if (!save){
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: 'Saving throw error',
|
name: 'Saving throw error',
|
||||||
value: 'No saving throw found: ' + prop.stat,
|
value: 'No saving throw found: ' + prop.stat,
|
||||||
});
|
});
|
||||||
@@ -55,24 +66,24 @@ export default function applySavingThrow(node, {creature, targets, scope, log}){
|
|||||||
const [a, b] = rollDice(2, 20);
|
const [a, b] = rollDice(2, 20);
|
||||||
if (a >= b) {
|
if (a >= b) {
|
||||||
value = a;
|
value = a;
|
||||||
resultPrefix = `Advantage: 1d20 [ ${a}, ~~${b}~~ ] ${rollModifierText} = `;
|
resultPrefix = `Advantage\n1d20 [ ${a}, ~~${b}~~ ] ${rollModifierText}`;
|
||||||
} else {
|
} else {
|
||||||
value = b;
|
value = b;
|
||||||
resultPrefix = `Advantage: 1d20 [ ~~${a}~~, ${b} ] ${rollModifierText} = `;
|
resultPrefix = `Advantage\n1d20 [ ~~${a}~~, ${b} ] ${rollModifierText}`;
|
||||||
}
|
}
|
||||||
} else if (save.advantage === -1){
|
} else if (save.advantage === -1){
|
||||||
const [a, b] = rollDice(2, 20);
|
const [a, b] = rollDice(2, 20);
|
||||||
if (a <= b) {
|
if (a <= b) {
|
||||||
value = a;
|
value = a;
|
||||||
resultPrefix = `Disadvantage: 1d20 [ ${a}, ~~${b}~~ ] ${rollModifierText} = `;
|
resultPrefix = `Disadvantage\n1d20 [ ${a}, ~~${b}~~ ] ${rollModifierText}`;
|
||||||
} else {
|
} else {
|
||||||
value = b;
|
value = b;
|
||||||
resultPrefix = `Disadvantage: 1d20 [ ~~${a}~~, ${b} ] ${rollModifierText} = `;
|
resultPrefix = `Disadvantage\n1d20 [ ~~${a}~~, ${b} ] ${rollModifierText}`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
values = rollDice(1, 20);
|
values = rollDice(1, 20);
|
||||||
value = values[0];
|
value = values[0];
|
||||||
resultPrefix = `1d20 [ ${value} ] ${rollModifierText} = `
|
resultPrefix = `1d20 [ ${value} ] ${rollModifierText}`
|
||||||
}
|
}
|
||||||
scope['$saveDiceRoll'] = {value};
|
scope['$saveDiceRoll'] = {value};
|
||||||
const result = value + save.value || 0;
|
const result = value + save.value || 0;
|
||||||
@@ -83,9 +94,9 @@ export default function applySavingThrow(node, {creature, targets, scope, log}){
|
|||||||
} else {
|
} else {
|
||||||
scope['$saveFailed'] = {value: true};
|
scope['$saveFailed'] = {value: true};
|
||||||
}
|
}
|
||||||
log.content.push({
|
if (!prop.silent) actionContext.addLog({
|
||||||
name: 'Save',
|
name: saveSuccess ? 'Successful save' : 'Failed save',
|
||||||
value: resultPrefix + result + (saveSuccess ? 'Passed' : 'Failed'),
|
value: resultPrefix + '\n**' + result + '**',
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
return applyChildren();
|
return applyChildren();
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import recalculateCalculation from './shared/recalculateCalculation.js';
|
import recalculateCalculation from './shared/recalculateCalculation.js';
|
||||||
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
|
||||||
export default function applyToggle(node, {
|
export default function applyToggle(node, actionContext){
|
||||||
creature, targets, scope, log
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
}){
|
|
||||||
const prop = node.node;
|
const prop = node.node;
|
||||||
recalculateCalculation(prop.condition, scope, log);
|
recalculateCalculation(prop.condition, actionContext);
|
||||||
if (prop.condition?.value) {
|
if (prop.condition?.value) {
|
||||||
return node.children.forEach(child => applyProperty(child, {
|
applyNodeTriggers(node, 'after', actionContext);
|
||||||
creature, targets, scope, log
|
return node.children.forEach(child => applyProperty(child, actionContext));
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import operator from '/imports/parser/parseTree/operator.js';
|
|||||||
import { parse } from '/imports/parser/parser.js';
|
import { parse } from '/imports/parser/parser.js';
|
||||||
import logErrors from './logErrors.js';
|
import logErrors from './logErrors.js';
|
||||||
|
|
||||||
export default function applyEffectsToCalculationParseNode(calcObj, log){
|
export default function applyEffectsToCalculationParseNode(calcObj, actionContext){
|
||||||
if (!calcObj.effects) return;
|
if (!calcObj.effects) return;
|
||||||
calcObj.effects.forEach(effect => {
|
calcObj.effects.forEach(effect => {
|
||||||
if (effect.operation !== 'add') return;
|
if (effect.operation !== 'add') return;
|
||||||
@@ -18,7 +18,7 @@ export default function applyEffectsToCalculationParseNode(calcObj, log){
|
|||||||
fn: 'add'
|
fn: 'add'
|
||||||
});
|
});
|
||||||
} catch (e){
|
} catch (e){
|
||||||
logErrors([e], log)
|
logErrors([e], actionContext)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
export default function logErrors(errors, log){
|
export default function logErrors(errors, actionContext){
|
||||||
errors?.forEach(error => {
|
errors?.forEach(error => {
|
||||||
if (error.type !== 'info'){
|
if (error.type !== 'info'){
|
||||||
log.content.push({name: 'Error', value: error.message});
|
actionContext.addLog({name: 'Error', value: error.message});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import evaluateCalculation from '/imports/api/engine/computation/utility/evaluat
|
|||||||
import applyEffectsToCalculationParseNode from '/imports/api/engine/actions/applyPropertyByType/shared/applyEffectsToCalculationParseNode.js';
|
import applyEffectsToCalculationParseNode from '/imports/api/engine/actions/applyPropertyByType/shared/applyEffectsToCalculationParseNode.js';
|
||||||
import logErrors from './logErrors.js';
|
import logErrors from './logErrors.js';
|
||||||
|
|
||||||
export default function recalculateCalculation(calc, scope, log, context){
|
export default function recalculateCalculation(calc, actionContext, context){
|
||||||
if (!calc?.parseNode) return;
|
if (!calc?.parseNode) return;
|
||||||
calc._parseLevel = 'reduce';
|
calc._parseLevel = 'reduce';
|
||||||
applyEffectsToCalculationParseNode(calc, log);
|
applyEffectsToCalculationParseNode(calc, actionContext);
|
||||||
evaluateCalculation(calc, scope, context);
|
evaluateCalculation(calc, actionContext.scope, context);
|
||||||
logErrors(calc.errors, log);
|
logErrors(calc.errors, actionContext);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import embedInlineCalculations from '/imports/api/engine/computation/utility/embedInlineCalculations.js';
|
import embedInlineCalculations from '/imports/api/engine/computation/utility/embedInlineCalculations.js';
|
||||||
import recalculateCalculation from './recalculateCalculation.js'
|
import recalculateCalculation from './recalculateCalculation.js'
|
||||||
|
|
||||||
export default function recalculateInlineCalculations(inlineCalcObj, scope, log){
|
export default function recalculateInlineCalculations(inlineCalcObj, actionContext){
|
||||||
// Skip if there are no calculations
|
// Skip if there are no calculations
|
||||||
if (!inlineCalcObj?.inlineCalculations?.length) return;
|
if (!inlineCalcObj?.inlineCalculations?.length) return;
|
||||||
// Recalculate each calculation with the current scope
|
// Recalculate each calculation with the current scope
|
||||||
inlineCalcObj.inlineCalculations.forEach(calc => {
|
inlineCalcObj.inlineCalculations.forEach(calc => {
|
||||||
recalculateCalculation(calc, scope, log);
|
recalculateCalculation(calc, actionContext);
|
||||||
});
|
});
|
||||||
// Embed the new calculated values
|
// Embed the new calculated values
|
||||||
embedInlineCalculations(inlineCalcObj);
|
embedInlineCalculations(inlineCalcObj);
|
||||||
|
|||||||
111
app/imports/api/engine/actions/applyTriggers.js
Normal file
111
app/imports/api/engine/actions/applyTriggers.js
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
import recalculateCalculation from '/imports/api/engine/actions/applyPropertyByType/shared/recalculateCalculation.js';
|
||||||
|
import recalculateInlineCalculations from '/imports/api/engine/actions/applyPropertyByType/shared/recalculateInlineCalculations.js';
|
||||||
|
import { getPropertyDecendants } from '/imports/api/engine/loadCreatures.js';
|
||||||
|
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
||||||
|
import applyProperty from '/imports/api/engine/actions/applyProperty.js';
|
||||||
|
import { difference, intersection } from 'lodash';
|
||||||
|
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
|
||||||
|
|
||||||
|
export function applyNodeTriggers(node, timing, actionContext) {
|
||||||
|
const prop = node.node;
|
||||||
|
const type = prop.type;
|
||||||
|
const triggers = actionContext.triggers?.doActionProperty?.[type]?.[timing];
|
||||||
|
if (triggers) {
|
||||||
|
triggers.forEach(trigger => {
|
||||||
|
applyTrigger(trigger, prop, actionContext);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applyTriggers(triggers = [], prop, actionContext) {
|
||||||
|
// Apply the triggers
|
||||||
|
triggers.forEach(trigger => {
|
||||||
|
applyTrigger(trigger, prop, actionContext)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applyTrigger(trigger, prop, actionContext) {
|
||||||
|
// If there is a prop we are applying the trigger from,
|
||||||
|
// don't fire if the tags don't match
|
||||||
|
if (prop && !triggerMatchTags(trigger, prop)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent trigger from firing if it's inactive
|
||||||
|
if (trigger.inactive) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent triggers from firing if their condition is false
|
||||||
|
if (trigger.condition?.parseNode) {
|
||||||
|
recalculateCalculation(trigger.condition, actionContext);
|
||||||
|
if (!trigger.condition.value) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent triggers from firing themselves in a loop
|
||||||
|
if (trigger.firing) {
|
||||||
|
/*
|
||||||
|
log.content.push({
|
||||||
|
name: trigger.name || 'Trigger',
|
||||||
|
value: 'Trigger can\'t fire itself',
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
trigger.firing = true;
|
||||||
|
|
||||||
|
// Fire the trigger
|
||||||
|
const content = {
|
||||||
|
name: trigger.name || 'Trigger',
|
||||||
|
value: trigger.description,
|
||||||
|
inline: false,
|
||||||
|
}
|
||||||
|
if (trigger.description?.text){
|
||||||
|
recalculateInlineCalculations(trigger.description, actionContext);
|
||||||
|
content.value = trigger.description.value;
|
||||||
|
}
|
||||||
|
if(!trigger.silent) actionContext.addLog(content);
|
||||||
|
|
||||||
|
// Get all the trigger's properties and apply them
|
||||||
|
const properties = getPropertyDecendants(actionContext.creature._id, trigger._id);
|
||||||
|
properties.sort((a, b) => a.order - b.order);
|
||||||
|
const propertyForest = nodeArrayToTree(properties);
|
||||||
|
propertyForest.forEach(node => {
|
||||||
|
applyProperty(node, actionContext);
|
||||||
|
});
|
||||||
|
|
||||||
|
trigger.firing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function triggerMatchTags(trigger, prop) {
|
||||||
|
let matched = false;
|
||||||
|
const propTags = getEffectivePropTags(prop);
|
||||||
|
// Check the target tags
|
||||||
|
if (
|
||||||
|
!trigger.targetTags?.length ||
|
||||||
|
difference(trigger.targetTags, propTags).length === 0
|
||||||
|
) {
|
||||||
|
matched = true;
|
||||||
|
}
|
||||||
|
// Check the extra tags
|
||||||
|
trigger.extraTags?.forEach(extra => {
|
||||||
|
if (extra.operation === 'OR') {
|
||||||
|
if (matched) return;
|
||||||
|
if (
|
||||||
|
!extra.tags.length ||
|
||||||
|
difference(extra.tags, propTags).length === 0
|
||||||
|
) {
|
||||||
|
matched = true;
|
||||||
|
}
|
||||||
|
} else if (extra.operation === 'NOT') {
|
||||||
|
if (
|
||||||
|
extra.tags.length &&
|
||||||
|
intersection(extra.tags, propTags)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return matched;
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
|
||||||
import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/log/CreatureLogs.js';
|
|
||||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||||
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
||||||
|
import {
|
||||||
|
getProperyAncestors, getPropertyDecendants
|
||||||
|
} from '/imports/api/engine/loadCreatures.js';
|
||||||
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import applyProperty from './applyProperty.js';
|
import applyProperty from './applyProperty.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
|
||||||
|
|
||||||
const doAction = new ValidatedMethod({
|
const doAction = new ValidatedMethod({
|
||||||
name: 'creatureProperties.doAction',
|
name: 'creatureProperties.doAction',
|
||||||
@@ -35,51 +36,33 @@ const doAction = new ValidatedMethod({
|
|||||||
numRequests: 10,
|
numRequests: 10,
|
||||||
timeInterval: 5000,
|
timeInterval: 5000,
|
||||||
},
|
},
|
||||||
run({actionId, targetIds = [], scope}) {
|
run({ actionId, targetIds = [], scope }) {
|
||||||
|
// Get action context
|
||||||
let action = CreatureProperties.findOne(actionId);
|
let action = CreatureProperties.findOne(actionId);
|
||||||
|
const creatureId = action.ancestors[0].id;
|
||||||
|
const actionContext = new ActionContext(creatureId, targetIds, this);
|
||||||
|
|
||||||
// Check permissions
|
// Check permissions
|
||||||
let creature = getRootCreatureAncestor(action);
|
assertEditPermission(actionContext.creature, this.userId);
|
||||||
|
actionContext.targets.forEach(target => {
|
||||||
assertEditPermission(creature, this.userId);
|
|
||||||
|
|
||||||
// Get all the targets and make sure we can edit them
|
|
||||||
let targets = [];
|
|
||||||
targetIds.forEach(targetId => {
|
|
||||||
let target = Creatures.findOne(targetId);
|
|
||||||
assertEditPermission(target, this.userId);
|
assertEditPermission(target, this.userId);
|
||||||
targets.push(target);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fetch all the action's ancestor creatureProperties
|
const ancestors = getProperyAncestors(creatureId, action._id);
|
||||||
const ancestorIds = [];
|
ancestors.sort((a, b) => a.order - b.order);
|
||||||
action.ancestors.forEach(ref => {
|
|
||||||
if (ref.collection === 'creatureProperties') {
|
|
||||||
ancestorIds.push(ref.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get cursor of ancestors
|
const properties = getPropertyDecendants(creatureId, action._id);
|
||||||
const ancestors = CreatureProperties.find({
|
properties.push(action);
|
||||||
_id: {$in: ancestorIds},
|
properties.sort((a, b) => a.order - b.order);
|
||||||
}, {
|
|
||||||
sort: {order: 1},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get cursor of the properties
|
|
||||||
const properties = CreatureProperties.find({
|
|
||||||
$or: [{_id: action._id}, {'ancestors.id': action._id}],
|
|
||||||
removed: {$ne: true},
|
|
||||||
}, {
|
|
||||||
sort: {order: 1},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Do the action
|
// Do the action
|
||||||
doActionWork({creature, targets, properties, ancestors, method: this, methodScope: scope});
|
doActionWork({properties, ancestors, actionContext, methodScope: scope});
|
||||||
|
|
||||||
// Recompute all involved creatures
|
// Recompute all involved creatures
|
||||||
computeCreature(creature._id);
|
Creatures.update({
|
||||||
targets.forEach(target => {
|
_id: { $in: [creatureId, ...targetIds] }
|
||||||
computeCreature(target._id);
|
}, {
|
||||||
|
$set: {dirty: true},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -87,7 +70,7 @@ const doAction = new ValidatedMethod({
|
|||||||
export default doAction;
|
export default doAction;
|
||||||
|
|
||||||
export function doActionWork({
|
export function doActionWork({
|
||||||
creature, targets, properties, ancestors, method, methodScope = {}, log
|
properties, ancestors, actionContext, methodScope = {},
|
||||||
}){
|
}){
|
||||||
// get the docs
|
// get the docs
|
||||||
const ancestorScope = getAncestorScope(ancestors);
|
const ancestorScope = getAncestorScope(ancestors);
|
||||||
@@ -96,28 +79,15 @@ export function doActionWork({
|
|||||||
throw new Meteor.Error(`The action has ${propertyForest.length} top level properties, expected 1`);
|
throw new Meteor.Error(`The action has ${propertyForest.length} top level properties, expected 1`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the log
|
// Include the ancestry and method scope in the context scope
|
||||||
if (!log) log = CreatureLogSchema.clean({
|
Object.assign(actionContext.scope, ancestorScope, methodScope);
|
||||||
creatureId: creature._id,
|
|
||||||
creatureName: creature.name,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Apply the top level property, it is responsible for applying its children
|
// Apply the top level property, it is responsible for applying its children
|
||||||
// recursively
|
// recursively
|
||||||
const scope = {
|
applyProperty(propertyForest[0], actionContext);
|
||||||
...creature.variables,
|
|
||||||
...ancestorScope,
|
|
||||||
...methodScope
|
|
||||||
}
|
|
||||||
applyProperty(propertyForest[0], {
|
|
||||||
creature,
|
|
||||||
targets,
|
|
||||||
scope,
|
|
||||||
log,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Insert the log
|
// Insert the log
|
||||||
insertCreatureLogWork({log, creature, method});
|
actionContext.writeLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assumes ancestors are in tree order already
|
// Assumes ancestors are in tree order already
|
||||||
|
|||||||
@@ -1,11 +1,53 @@
|
|||||||
import '/imports/api/simpleSchemaConfig.js';
|
import '/imports/api/simpleSchemaConfig.js';
|
||||||
//import testTypes from './testTypes/index.js';
|
//import testTypes from './testTypes/index.js';
|
||||||
import { doActionWork } from './doAction.js';
|
import { doActionWork } from './doAction.js';
|
||||||
import createAction from './tests/createAction.testFn.js';
|
import { CreatureLogSchema } from '/imports/api/creature/log/CreatureLogs.js';
|
||||||
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
|
||||||
|
function cleanProp(prop){
|
||||||
|
let schema = CreatureProperties.simpleSchema(prop);
|
||||||
|
return schema.clean(prop);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanCreature(creature){
|
||||||
|
let schema = Creatures.simpleSchema(creature);
|
||||||
|
return schema.clean(creature);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fake ActionContext to test actions with
|
||||||
|
const creatureId = 'actionTestCreatureId';
|
||||||
|
const creatureName = 'Action Test Creature';
|
||||||
|
const testActionContext = {
|
||||||
|
creature: cleanCreature({
|
||||||
|
_id: creatureId,
|
||||||
|
}),
|
||||||
|
log: CreatureLogSchema.clean({
|
||||||
|
creatureId: creatureId,
|
||||||
|
creatureName: creatureName,
|
||||||
|
}),
|
||||||
|
scope: {},
|
||||||
|
addLog(content) {
|
||||||
|
if (content.name || content.value){
|
||||||
|
this.log.content.push(content);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
writeLog: () => { },
|
||||||
|
}
|
||||||
|
|
||||||
|
const action = cleanProp({
|
||||||
|
type: 'action',
|
||||||
|
});
|
||||||
|
const actionAncestors = [];
|
||||||
|
|
||||||
describe('Do Action', function(){
|
describe('Do Action', function(){
|
||||||
it('Does an empty action', function(){
|
it('Does an empty action', function(){
|
||||||
doActionWork(createAction({properties: [{type: 'action'}]}));
|
doActionWork({
|
||||||
|
properties: [action],
|
||||||
|
ancestors: actionAncestors,
|
||||||
|
actionContext: testActionContext,
|
||||||
|
methodScope: {},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
//testTypes.forEach(test => it(test.text, test.fn));
|
//testTypes.forEach(test => it(test.text, test.fn));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import {
|
||||||
|
getProperyAncestors, getPropertyDecendants
|
||||||
|
} from '/imports/api/engine/loadCreatures.js';
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||||
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||||
import { doActionWork } from '/imports/api/engine/actions/doAction.js';
|
import { doActionWork } from '/imports/api/engine/actions/doAction.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
|
||||||
import { CreatureLogSchema } from '/imports/api/creature/log/CreatureLogs.js';
|
|
||||||
|
|
||||||
const doAction = new ValidatedMethod({
|
const doAction = new ValidatedMethod({
|
||||||
name: 'creatureProperties.doCastSpell',
|
name: 'creatureProperties.doCastSpell',
|
||||||
@@ -40,47 +41,29 @@ const doAction = new ValidatedMethod({
|
|||||||
numRequests: 10,
|
numRequests: 10,
|
||||||
timeInterval: 5000,
|
timeInterval: 5000,
|
||||||
},
|
},
|
||||||
run({spellId, slotId, targetIds = [], scope = {}}) {
|
run({ spellId, slotId, targetIds = [], scope = {} }) {
|
||||||
|
// Get action context
|
||||||
let spell = CreatureProperties.findOne(spellId);
|
let spell = CreatureProperties.findOne(spellId);
|
||||||
|
const creatureId = spell.ancestors[0].id;
|
||||||
|
const actionContext = new ActionContext(creatureId, targetIds, this);
|
||||||
|
|
||||||
// Check permissions
|
// Check permissions
|
||||||
let creature = getRootCreatureAncestor(spell);
|
assertEditPermission(actionContext.creature, this.userId);
|
||||||
|
actionContext.targets.forEach(target => {
|
||||||
assertEditPermission(creature, this.userId);
|
|
||||||
|
|
||||||
// Get all the targets and make sure we can edit them
|
|
||||||
let targets = [];
|
|
||||||
targetIds.forEach(targetId => {
|
|
||||||
let target = Creatures.findOne(targetId);
|
|
||||||
assertEditPermission(target, this.userId);
|
assertEditPermission(target, this.userId);
|
||||||
targets.push(target);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fetch all the action's ancestor creatureProperties
|
const ancestors = getProperyAncestors(creatureId, spell._id);
|
||||||
const ancestorIds = [];
|
ancestors.sort((a, b) => a.order - b.order);
|
||||||
spell.ancestors.forEach(ref => {
|
|
||||||
if (ref.collection === 'creatureProperties') {
|
|
||||||
ancestorIds.push(ref.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get cursor of ancestors
|
const properties = getPropertyDecendants(creatureId, spell._id);
|
||||||
const ancestors = CreatureProperties.find({
|
properties.push(spell);
|
||||||
_id: {$in: ancestorIds},
|
properties.sort((a, b) => a.order - b.order);
|
||||||
}, {
|
|
||||||
sort: {order: 1},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get cursor of the properties
|
|
||||||
const properties = CreatureProperties.find({
|
|
||||||
$or: [{_id: spell._id}, {'ancestors.id': spell._id}],
|
|
||||||
removed: {$ne: true},
|
|
||||||
}, {
|
|
||||||
sort: {order: 1},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Spend the appropriate slot
|
// Spend the appropriate slot
|
||||||
let slotLevel = spell.level || 0;
|
let slotLevel = spell.level || 0;
|
||||||
let slot;
|
let slot;
|
||||||
|
|
||||||
if (slotId && !spell.castWithoutSpellSlots){
|
if (slotId && !spell.castWithoutSpellSlots){
|
||||||
slot = CreatureProperties.findOne(slotId);
|
slot = CreatureProperties.findOne(slotId);
|
||||||
if (!slot){
|
if (!slot){
|
||||||
@@ -105,36 +88,36 @@ const doAction = new ValidatedMethod({
|
|||||||
}
|
}
|
||||||
slotLevel = slot.spellSlotLevel.value;
|
slotLevel = slot.spellSlotLevel.value;
|
||||||
damagePropertyWork({
|
damagePropertyWork({
|
||||||
property: slot,
|
prop: slot,
|
||||||
operation: 'increment',
|
operation: 'increment',
|
||||||
value: 1,
|
value: 1,
|
||||||
|
actionContext,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
scope['slotLevel'] = slotLevel;
|
|
||||||
|
|
||||||
// Post the slot level spent to the log
|
// Post the slot level spent to the log
|
||||||
const log = CreatureLogSchema.clean({
|
|
||||||
creatureId: creature._id,
|
|
||||||
creatureName: creature.name,
|
|
||||||
});
|
|
||||||
if (slot?.spellSlotLevel?.value){
|
if (slot?.spellSlotLevel?.value){
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: `Casting using a level ${slotLevel} spell slot`
|
name: `Casting using a level ${slotLevel} spell slot`
|
||||||
});
|
});
|
||||||
} else if (slotLevel) {
|
} else if (slotLevel) {
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: `Casting at level ${slotLevel}`
|
name: `Casting at level ${slotLevel}`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do the action
|
actionContext.scope['slotLevel'] = slotLevel;
|
||||||
doActionWork({creature, targets, properties, ancestors, method: this, methodScope: scope, log});
|
|
||||||
|
|
||||||
// Recompute all involved creatures
|
// Do the action
|
||||||
computeCreature(creature._id);
|
doActionWork({
|
||||||
targets.forEach(target => {
|
properties, ancestors, actionContext, methodScope: scope,
|
||||||
computeCreature(target._id);
|
});
|
||||||
|
|
||||||
|
// Force the characters involved to recalculate
|
||||||
|
Creatures.update({
|
||||||
|
_id: { $in: [creatureId, ...targetIds] }
|
||||||
|
}, {
|
||||||
|
$set: { dirty: true },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
|
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/log/CreatureLogs.js';
|
|
||||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
|
||||||
import rollDice from '/imports/parser/rollDice.js';
|
import rollDice from '/imports/parser/rollDice.js';
|
||||||
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
||||||
|
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
|
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
|
||||||
|
|
||||||
const doCheck = new ValidatedMethod({
|
const doCheck = new ValidatedMethod({
|
||||||
name: 'creatureProperties.doCheck',
|
name: 'creatureProperties.doCheck',
|
||||||
@@ -25,37 +24,32 @@ const doCheck = new ValidatedMethod({
|
|||||||
},
|
},
|
||||||
run({propId, scope}) {
|
run({propId, scope}) {
|
||||||
const prop = CreatureProperties.findOne(propId);
|
const prop = CreatureProperties.findOne(propId);
|
||||||
const creature = getRootCreatureAncestor(prop);
|
const creatureId = prop.ancestors[0].id;
|
||||||
|
const actionContext = new ActionContext(creatureId, [creatureId], this);
|
||||||
|
Object.assign(actionContext.scope, scope);
|
||||||
|
|
||||||
// Check permissions
|
// Check permissions
|
||||||
assertEditPermission(creature, this.userId);
|
assertEditPermission(actionContext.creature, this.userId);
|
||||||
|
|
||||||
// Do the check
|
// Do the check
|
||||||
doCheckWork({creature, prop, method: this, methodScope: scope});
|
doCheckWork({prop, actionContext});
|
||||||
|
|
||||||
// Recompute all involved creatures
|
|
||||||
computeCreature(creature._id);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default doCheck;
|
export default doCheck;
|
||||||
|
|
||||||
export function doCheckWork({
|
export function doCheckWork({prop, actionContext}){
|
||||||
creature, prop, method, methodScope = {}
|
|
||||||
}){
|
|
||||||
// Create the log
|
|
||||||
let log = CreatureLogSchema.clean({
|
|
||||||
creatureId: creature._id,
|
|
||||||
creatureName: creature.name,
|
|
||||||
});
|
|
||||||
|
|
||||||
rollCheck({prop, log, methodScope});
|
applyTriggers(actionContext.triggers.check?.before, prop, actionContext);
|
||||||
|
rollCheck(prop, actionContext);
|
||||||
|
applyTriggers(actionContext.triggers.check?.after, prop, actionContext);
|
||||||
|
|
||||||
// Insert the log
|
// Insert the log
|
||||||
insertCreatureLogWork({log, creature, method});
|
actionContext.writeLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
function rollCheck({prop, log, methodScope}){
|
function rollCheck(prop, actionContext) {
|
||||||
|
const scope = actionContext.scope;
|
||||||
// get the modifier for the roll
|
// get the modifier for the roll
|
||||||
let rollModifier;
|
let rollModifier;
|
||||||
let logName = `${prop.name} check`;
|
let logName = `${prop.name} check`;
|
||||||
@@ -81,7 +75,7 @@ function rollCheck({prop, log, methodScope}){
|
|||||||
const rollModifierText = numberToSignedString(rollModifier, true);
|
const rollModifierText = numberToSignedString(rollModifier, true);
|
||||||
|
|
||||||
let value, values, resultPrefix;
|
let value, values, resultPrefix;
|
||||||
if (methodScope['$checkAdvantage'] === 1){
|
if (scope['$checkAdvantage'] === 1){
|
||||||
logName += ' (Advantage)';
|
logName += ' (Advantage)';
|
||||||
const [a, b] = rollDice(2, 20);
|
const [a, b] = rollDice(2, 20);
|
||||||
if (a >= b) {
|
if (a >= b) {
|
||||||
@@ -91,7 +85,7 @@ function rollCheck({prop, log, methodScope}){
|
|||||||
value = b;
|
value = b;
|
||||||
resultPrefix = `1d20 [ ~~${a}~~, ${b} ] ${rollModifierText} = `;
|
resultPrefix = `1d20 [ ~~${a}~~, ${b} ] ${rollModifierText} = `;
|
||||||
}
|
}
|
||||||
} else if (methodScope['$checkAdvantage'] === -1){
|
} else if (scope['$checkAdvantage'] === -1){
|
||||||
logName += ' (Disadvantage)';
|
logName += ' (Disadvantage)';
|
||||||
const [a, b] = rollDice(2, 20);
|
const [a, b] = rollDice(2, 20);
|
||||||
if (a <= b) {
|
if (a <= b) {
|
||||||
@@ -107,7 +101,7 @@ function rollCheck({prop, log, methodScope}){
|
|||||||
resultPrefix = `1d20 [ ${value} ] ${rollModifierText} = `
|
resultPrefix = `1d20 [ ${value} ] ${rollModifierText} = `
|
||||||
}
|
}
|
||||||
const result = (value + rollModifier) || 0;
|
const result = (value + rollModifier) || 0;
|
||||||
log.content.push({
|
actionContext.addLog({
|
||||||
name: logName,
|
name: logName,
|
||||||
value: `${resultPrefix} **${result}**`,
|
value: `${resultPrefix} **${result}**`,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
|
||||||
|
|
||||||
export default function createAction({
|
|
||||||
creature = {_id: 'creatureId'},
|
|
||||||
targets = [],
|
|
||||||
properties = [],
|
|
||||||
ancestors = [],
|
|
||||||
method
|
|
||||||
} = {}){
|
|
||||||
properties = properties.map(cleanProp);
|
|
||||||
ancestors = ancestors.map(cleanProp);
|
|
||||||
creature = cleanCreature(creature);
|
|
||||||
ancestors = ancestors.map(cleanCreature);
|
|
||||||
return {creature, targets, properties, ancestors, method};
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanProp(prop){
|
|
||||||
let schema = CreatureProperties.simpleSchema(prop);
|
|
||||||
return schema.clean(prop);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanCreature(creature){
|
|
||||||
let schema = Creatures.simpleSchema(creature);
|
|
||||||
return schema.clean(creature);
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import applyAction from './applyAction.testFn.js';
|
|
||||||
|
|
||||||
export default [{
|
|
||||||
text: 'Applies actions',
|
|
||||||
fn: applyAction,
|
|
||||||
},];
|
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
import { EJSON } from 'meteor/ejson';
|
import { EJSON } from 'meteor/ejson';
|
||||||
import createGraph from 'ngraph.graph';
|
import createGraph from 'ngraph.graph';
|
||||||
|
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
|
||||||
|
|
||||||
export default class CreatureComputation {
|
export default class CreatureComputation {
|
||||||
constructor(properties){
|
constructor(properties, creature, variables){
|
||||||
// Set up fields
|
// Set up fields
|
||||||
this.originalPropsById = {};
|
this.originalPropsById = {};
|
||||||
this.propsById = {};
|
this.propsById = {};
|
||||||
@@ -11,6 +12,8 @@ export default class CreatureComputation {
|
|||||||
this.props = properties;
|
this.props = properties;
|
||||||
this.dependencyGraph = createGraph();
|
this.dependencyGraph = createGraph();
|
||||||
this.errors = [];
|
this.errors = [];
|
||||||
|
this.creature = creature;
|
||||||
|
this.variables = variables;
|
||||||
|
|
||||||
// Store properties for easy access later
|
// Store properties for easy access later
|
||||||
properties.forEach(prop => {
|
properties.forEach(prop => {
|
||||||
@@ -21,15 +24,14 @@ export default class CreatureComputation {
|
|||||||
this.propsById[prop._id] = prop;
|
this.propsById[prop._id] = prop;
|
||||||
|
|
||||||
// Store sets of ids in each tag
|
// Store sets of ids in each tag
|
||||||
if (prop.tags){
|
getEffectivePropTags(prop).forEach(tag => {
|
||||||
prop.tags.forEach(tag => {
|
if (!tag) return;
|
||||||
if (this.propsWithTag[tag]){
|
if (this.propsWithTag[tag]) {
|
||||||
this.propsWithTag[tag].push(prop._id);
|
this.propsWithTag[tag].push(prop._id);
|
||||||
} else {
|
} else {
|
||||||
this.propsWithTag[tag] = [prop._id];
|
this.propsWithTag[tag] = [prop._id];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Store the prop in the dependency graph
|
// Store the prop in the dependency graph
|
||||||
this.dependencyGraph.addNode(prop._id, prop);
|
this.dependencyGraph.addNode(prop._id, prop);
|
||||||
|
|||||||
@@ -31,9 +31,10 @@ function childrenActive(prop){
|
|||||||
switch (prop.type){
|
switch (prop.type){
|
||||||
// Only equipped items have active children
|
// Only equipped items have active children
|
||||||
case 'item': return !!prop.equipped;
|
case 'item': return !!prop.equipped;
|
||||||
// The children of actions are always inactive
|
// The children of actions, spells, and triggers are always inactive
|
||||||
case 'action': return false;
|
case 'action': return false;
|
||||||
case 'spell': return false;
|
case 'spell': return false;
|
||||||
|
case 'trigger': return false;
|
||||||
// The children of notes are always inactive
|
// The children of notes are always inactive
|
||||||
case 'note': return false;
|
case 'note': return false;
|
||||||
// Other children are active
|
// Other children are active
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ export default function computeToggleDependencies(node, dependencyGraph){
|
|||||||
prop.enabled
|
prop.enabled
|
||||||
) return;
|
) return;
|
||||||
walkDown(node.children, child => {
|
walkDown(node.children, child => {
|
||||||
child.node._computationDetails.toggleAncestors.push(prop);
|
// Only for children that aren't inactive
|
||||||
|
if (child.node.inactive) return;
|
||||||
// The child nodes depend on the toggle condition compuation
|
// The child nodes depend on the toggle condition compuation
|
||||||
|
child.node._computationDetails.toggleAncestors.push(prop);
|
||||||
dependencyGraph.addLink(child.node._id, prop._id, 'toggle');
|
dependencyGraph.addLink(child.node._id, prop._id, 'toggle');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { get, intersection, difference } from 'lodash';
|
import { get, intersection, difference, union } from 'lodash';
|
||||||
|
|
||||||
const linkDependenciesByType = {
|
const linkDependenciesByType = {
|
||||||
action: linkAction,
|
action: linkAction,
|
||||||
@@ -14,6 +14,7 @@ const linkDependenciesByType = {
|
|||||||
effect: linkEffects,
|
effect: linkEffects,
|
||||||
proficiency: linkProficiencies,
|
proficiency: linkProficiencies,
|
||||||
roll: linkRoll,
|
roll: linkRoll,
|
||||||
|
pointBuy: linkPointBuy,
|
||||||
propertySlot: linkSlot,
|
propertySlot: linkSlot,
|
||||||
skill: linkSkill,
|
skill: linkSkill,
|
||||||
spell: linkAction,
|
spell: linkAction,
|
||||||
@@ -105,7 +106,8 @@ function linkBuff(dependencyGraph, prop){
|
|||||||
dependOnCalc({dependencyGraph, prop, key: 'duration'});
|
dependOnCalc({dependencyGraph, prop, key: 'duration'});
|
||||||
}
|
}
|
||||||
|
|
||||||
function linkClassLevel(dependencyGraph, prop){
|
function linkClassLevel(dependencyGraph, prop) {
|
||||||
|
if (prop.inactive) return;
|
||||||
// The variableName of the prop depends on the prop
|
// The variableName of the prop depends on the prop
|
||||||
if (prop.variableName && prop.level){
|
if (prop.variableName && prop.level){
|
||||||
dependencyGraph.addLink(prop.variableName, prop._id, 'classLevel');
|
dependencyGraph.addLink(prop.variableName, prop._id, 'classLevel');
|
||||||
@@ -121,17 +123,33 @@ function linkDamage(dependencyGraph, prop){
|
|||||||
dependOnCalc({dependencyGraph, prop, key: 'amount'});
|
dependOnCalc({dependencyGraph, prop, key: 'amount'});
|
||||||
}
|
}
|
||||||
|
|
||||||
function linkEffects(dependencyGraph, prop, computation){
|
function linkEffects(dependencyGraph, prop, computation) {
|
||||||
// The effect depends on its amount calculation
|
// The effect depends on its amount calculation
|
||||||
dependOnCalc({dependencyGraph, prop, key: 'amount'});
|
dependOnCalc({ dependencyGraph, prop, key: 'amount' });
|
||||||
|
// Inactive effects aren't going to impact their targeted stats
|
||||||
|
if (prop.inactive) return;
|
||||||
// The stats depend on the effect
|
// The stats depend on the effect
|
||||||
if (prop.targetByTags){
|
if (prop.inactive) {
|
||||||
|
// Inactive effects apply to no stats
|
||||||
|
return;
|
||||||
|
} else if (prop.targetByTags){
|
||||||
getEffectTagTargets(prop, computation).forEach(targetId => {
|
getEffectTagTargets(prop, computation).forEach(targetId => {
|
||||||
const targetProp = computation.propsById[targetId];
|
const targetProp = computation.propsById[targetId];
|
||||||
const key = prop.targetField || getDefaultCalculationField(targetProp);
|
if (
|
||||||
const calcObj = get(targetProp, key);
|
(targetProp.type === 'attribute' || targetProp.type === 'skill')
|
||||||
if (calcObj && calcObj.calculation){
|
&& targetProp.variableName
|
||||||
dependencyGraph.addLink(`${targetProp._id}.${key}`, prop._id , 'effect');
|
&& !prop.targetField
|
||||||
|
) {
|
||||||
|
// If the field wasn't specified and we're targeting an attribute or
|
||||||
|
// skill, just treat it like a normal effect on its variable name
|
||||||
|
dependencyGraph.addLink(targetProp.variableName, prop._id, 'effect');
|
||||||
|
} else {
|
||||||
|
// Otherwise target a field on that property
|
||||||
|
const key = prop.targetField || getDefaultCalculationField(targetProp);
|
||||||
|
const calcObj = get(targetProp, key);
|
||||||
|
if (calcObj && calcObj.calculation){
|
||||||
|
dependencyGraph.addLink(`${targetProp._id}.${key}`, prop._id , 'effect');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -144,16 +162,18 @@ function linkEffects(dependencyGraph, prop, computation){
|
|||||||
|
|
||||||
// Returns an array of IDs of the properties the effect targets
|
// Returns an array of IDs of the properties the effect targets
|
||||||
function getEffectTagTargets(effect, computation){
|
function getEffectTagTargets(effect, computation){
|
||||||
const targets = getTargetListFromTags(effect.targetTags, computation);
|
let targets = getTargetListFromTags(effect.targetTags, computation);
|
||||||
const notIds = [];
|
let notIds = [];
|
||||||
if (effect.extraTags){
|
if (effect.extraTags){
|
||||||
effect.extraTags.forEach(ex => {
|
effect.extraTags.forEach(ex => {
|
||||||
if (ex.operation === 'OR'){
|
if (ex.operation === 'OR') {
|
||||||
targets.push(...getTargetListFromTags(ex.tags, computation));
|
targets = union(targets, getTargetListFromTags(ex.tags, computation));
|
||||||
} else if (ex.operation === 'NOT'){
|
} else if (ex.operation === 'NOT'){
|
||||||
ex.tags.forEach(tag => {
|
ex.tags.forEach(tag => {
|
||||||
const idList = computation.propsWithTag[tag];
|
const idList = computation.propsWithTag[tag];
|
||||||
if (idList) notIds.push(...computation.propsWithTag[tag])
|
if (idList) {
|
||||||
|
notIds = union(notIds, computation.propsWithTag[tag]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -165,8 +185,8 @@ function getTargetListFromTags(tags, computation){
|
|||||||
const targetTagIdLists = [];
|
const targetTagIdLists = [];
|
||||||
if (!tags) return [];
|
if (!tags) return [];
|
||||||
tags.forEach(tag => {
|
tags.forEach(tag => {
|
||||||
const idList = computation.propsWithTag[tag];
|
const idList = computation.propsWithTag[tag] || [];
|
||||||
if (idList) targetTagIdLists.push(idList);
|
targetTagIdLists.push(idList);
|
||||||
});
|
});
|
||||||
const targets = intersection(...targetTagIdLists);
|
const targets = intersection(...targetTagIdLists);
|
||||||
return targets;
|
return targets;
|
||||||
@@ -208,22 +228,46 @@ function linkRoll(dependencyGraph, prop){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function linkVariableName(dependencyGraph, prop){
|
function linkVariableName(dependencyGraph, prop){
|
||||||
// The variableName of the prop depends on the prop
|
// The variableName of the prop depends on the prop if the prop is active
|
||||||
if (prop.variableName){
|
if (prop.variableName && !prop.inactive){
|
||||||
dependencyGraph.addLink(prop.variableName, prop._id, 'definition');
|
dependencyGraph.addLink(prop.variableName, prop._id, 'definition');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function linkDamageMultiplier(dependencyGraph, prop){
|
function linkDamageMultiplier(dependencyGraph, prop) {
|
||||||
|
if (prop.inactive) return;
|
||||||
prop.damageTypes.forEach(damageType => {
|
prop.damageTypes.forEach(damageType => {
|
||||||
// Remove all non-letter characters from the damage name
|
// Remove all non-letter characters from the damage name
|
||||||
const damageName = damageType.replace(/[^a-z]/gi, '')
|
const damageName = damageType.replace(/[^a-z]/gi, '')
|
||||||
dependencyGraph.addLink(`${damageName}Multiplier`, prop._id, prop.type);
|
dependencyGraph.addLink(damageName, prop._id, prop.type);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function linkPointBuy(dependencyGraph, prop){
|
||||||
|
dependOnCalc({ dependencyGraph, prop, key: 'min' });
|
||||||
|
dependOnCalc({ dependencyGraph, prop, key: 'max' });
|
||||||
|
dependOnCalc({ dependencyGraph, prop, key: 'cost' });
|
||||||
|
dependOnCalc({ dependencyGraph, prop, key: 'total' });
|
||||||
|
prop.values?.forEach(row => {
|
||||||
|
// Wrap the document in a new object so we don't bash it unintentionally
|
||||||
|
const pointBuyRow = {
|
||||||
|
...row,
|
||||||
|
type: 'pointBuyRow',
|
||||||
|
tableName: prop.name,
|
||||||
|
tableId: prop._id,
|
||||||
|
}
|
||||||
|
dependencyGraph.addNode(row._id, pointBuyRow);
|
||||||
|
linkVariableName(dependencyGraph, pointBuyRow);
|
||||||
|
dependOnCalc({ dependencyGraph, pointBuyRow, key: 'row.min' });
|
||||||
|
dependOnCalc({ dependencyGraph, pointBuyRow, key: 'row.max' });
|
||||||
|
dependOnCalc({ dependencyGraph, pointBuyRow, key: 'row.cost' });
|
||||||
|
});
|
||||||
|
if (prop.inactive) return;
|
||||||
|
}
|
||||||
|
|
||||||
function linkProficiencies(dependencyGraph, prop){
|
function linkProficiencies(dependencyGraph, prop){
|
||||||
// The stats depend on the proficiency
|
// The stats depend on the proficiency
|
||||||
|
if (prop.inactive) return;
|
||||||
prop.stats.forEach(statName => {
|
prop.stats.forEach(statName => {
|
||||||
if (!statName) return;
|
if (!statName) return;
|
||||||
dependencyGraph.addLink(statName, prop._id, prop.type);
|
dependencyGraph.addLink(statName, prop._id, prop.type);
|
||||||
@@ -235,6 +279,10 @@ function linkSavingThrow(dependencyGraph, prop){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function linkSkill(dependencyGraph, prop){
|
function linkSkill(dependencyGraph, prop){
|
||||||
|
// Depends on base value
|
||||||
|
dependOnCalc({ dependencyGraph, prop, key: 'baseValue' });
|
||||||
|
// Link dependents
|
||||||
|
if (prop.inactive) return;
|
||||||
linkVariableName(dependencyGraph, prop);
|
linkVariableName(dependencyGraph, prop);
|
||||||
// The prop depends on the variable references as the ability
|
// The prop depends on the variable references as the ability
|
||||||
if (prop.ability){
|
if (prop.ability){
|
||||||
@@ -242,9 +290,6 @@ function linkSkill(dependencyGraph, prop){
|
|||||||
}
|
}
|
||||||
// Skills depend on the creature's proficiencyBonus
|
// Skills depend on the creature's proficiencyBonus
|
||||||
dependencyGraph.addLink(prop._id, 'proficiencyBonus', 'skillProficiencyBonus');
|
dependencyGraph.addLink(prop._id, 'proficiencyBonus', 'skillProficiencyBonus');
|
||||||
|
|
||||||
// Depends on base value
|
|
||||||
dependOnCalc({dependencyGraph, prop, key: 'baseValue'});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function linkSlot(dependencyGraph, prop){
|
function linkSlot(dependencyGraph, prop){
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
||||||
import CreatureProperties,
|
import { DenormalisedOnlyCreaturePropertySchema as denormSchema }
|
||||||
{ DenormalisedOnlyCreaturePropertySchema as denormSchema }
|
|
||||||
from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import { getProperties, getCreature, getVariables } from '/imports/api/engine/loadCreatures.js';
|
||||||
import computedOnlySchemas from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
|
import computedOnlySchemas from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
|
||||||
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
|
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
|
||||||
import linkInventory from './buildComputation/linkInventory.js';
|
import linkInventory from './buildComputation/linkInventory.js';
|
||||||
@@ -32,29 +31,15 @@ import removeSchemaFields from './buildComputation/removeSchemaFields.js';
|
|||||||
|
|
||||||
export default function buildCreatureComputation(creatureId){
|
export default function buildCreatureComputation(creatureId){
|
||||||
const creature = getCreature(creatureId);
|
const creature = getCreature(creatureId);
|
||||||
|
const variables = getVariables(creatureId);
|
||||||
const properties = getProperties(creatureId);
|
const properties = getProperties(creatureId);
|
||||||
const computation = buildComputationFromProps(properties, creature);
|
const computation = buildComputationFromProps(properties, creature, variables);
|
||||||
return computation;
|
return computation;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getProperties(creatureId){
|
export function buildComputationFromProps(properties, creature, variables){
|
||||||
return CreatureProperties.find({
|
|
||||||
'ancestors.id': creatureId,
|
|
||||||
'removed': {$ne: true},
|
|
||||||
}, {
|
|
||||||
sort: {order: 1}
|
|
||||||
}).fetch();
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCreature(creatureId){
|
const computation = new CreatureComputation(properties, creature, variables);
|
||||||
return Creatures.findOne(creatureId, {
|
|
||||||
denormalizedStats: 1,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildComputationFromProps(properties, creature){
|
|
||||||
|
|
||||||
const computation = new CreatureComputation(properties);
|
|
||||||
// Dependency graph where edge(a, b) means a depends on b
|
// Dependency graph where edge(a, b) means a depends on b
|
||||||
// The graph includes all dependencies even of inactive properties
|
// The graph includes all dependencies even of inactive properties
|
||||||
// such that any properties changing without changing their dependencies
|
// such that any properties changing without changing their dependencies
|
||||||
@@ -81,8 +66,10 @@ export function buildComputationFromProps(properties, creature){
|
|||||||
|
|
||||||
// Process the properties one by one
|
// Process the properties one by one
|
||||||
properties.forEach(prop => {
|
properties.forEach(prop => {
|
||||||
|
// The prop has been processed, it's no longer dirty
|
||||||
|
delete prop.dirty;
|
||||||
|
|
||||||
let computedSchema = computedOnlySchemas[prop.type];
|
const computedSchema = computedOnlySchemas[prop.type];
|
||||||
removeSchemaFields([computedSchema, denormSchema], prop);
|
removeSchemaFields([computedSchema, denormSchema], prop);
|
||||||
|
|
||||||
// Add a place to store all the computation details
|
// Add a place to store all the computation details
|
||||||
@@ -102,6 +89,10 @@ export function buildComputationFromProps(properties, creature){
|
|||||||
// Walk the property trees computing things that need to be inherited
|
// Walk the property trees computing things that need to be inherited
|
||||||
walkDown(forest, node => {
|
walkDown(forest, node => {
|
||||||
computeInactiveStatus(node);
|
computeInactiveStatus(node);
|
||||||
|
});
|
||||||
|
// Inactive status must be complete for the whole tree before toggle deps
|
||||||
|
// are calculated
|
||||||
|
walkDown(forest, node => {
|
||||||
computeToggleDependencies(node, dependencyGraph);
|
computeToggleDependencies(node, dependencyGraph);
|
||||||
computeSlotQuantityFilled(node, dependencyGraph);
|
computeSlotQuantityFilled(node, dependencyGraph);
|
||||||
});
|
});
|
||||||
@@ -114,5 +105,6 @@ export function buildComputationFromProps(properties, creature){
|
|||||||
linkTypeDependencies(dependencyGraph, prop, computation);
|
linkTypeDependencies(dependencyGraph, prop, computation);
|
||||||
linkCalculationDependencies(dependencyGraph, prop, computation);
|
linkCalculationDependencies(dependencyGraph, prop, computation);
|
||||||
});
|
});
|
||||||
|
|
||||||
return computation;
|
return computation;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import _variable from './computeByType/computeVariable.js';
|
|||||||
import action from './computeByType/computeAction.js';
|
import action from './computeByType/computeAction.js';
|
||||||
import attribute from './computeByType/computeAttribute.js';
|
import attribute from './computeByType/computeAttribute.js';
|
||||||
import skill from './computeByType/computeSkill.js';
|
import skill from './computeByType/computeSkill.js';
|
||||||
|
import pointBuy from './computeByType/computePointBuy.js';
|
||||||
import propertySlot from './computeByType/computeSlot.js';
|
import propertySlot from './computeByType/computeSlot.js';
|
||||||
import container from './computeByType/computeContainer.js';
|
import container from './computeByType/computeContainer.js';
|
||||||
import _calculation from './computeByType/computeCalculation.js';
|
import _calculation from './computeByType/computeCalculation.js';
|
||||||
@@ -13,6 +14,7 @@ export default Object.freeze({
|
|||||||
attribute,
|
attribute,
|
||||||
container,
|
container,
|
||||||
skill,
|
skill,
|
||||||
|
pointBuy,
|
||||||
propertySlot,
|
propertySlot,
|
||||||
spell: action,
|
spell: action,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { has } from 'lodash';
|
||||||
|
import evaluateCalculation from '../../utility/evaluateCalculation.js';
|
||||||
|
|
||||||
|
export default function computePointBuy(computation, node) {
|
||||||
|
const prop = node.data;
|
||||||
|
const tableMin = prop.min?.value || null;
|
||||||
|
const tableMax = prop.max?.value || null;
|
||||||
|
prop.spent = 0;
|
||||||
|
prop.values?.forEach(row => {
|
||||||
|
// Clean up added properties
|
||||||
|
// delete row.tableId;
|
||||||
|
// delete row.tableName;
|
||||||
|
// delete row.type;
|
||||||
|
|
||||||
|
row.spent = 0;
|
||||||
|
if (row.value === undefined) return;
|
||||||
|
const min = has(row, 'min.value') ? row.min.value : tableMin;
|
||||||
|
const max = has(row, 'max.value') ? row.max.value : tableMax;
|
||||||
|
const costFunction = EJSON.clone(row.cost || prop.cost);
|
||||||
|
if (costFunction) costFunction.parseLevel = 'reduce';
|
||||||
|
|
||||||
|
// Check min and max
|
||||||
|
if (min !== null && row.value < min) {
|
||||||
|
row.value = min;
|
||||||
|
}
|
||||||
|
if (max !== null && row.value > max) {
|
||||||
|
row.value = max;
|
||||||
|
}
|
||||||
|
// Evaluate the cost function
|
||||||
|
if (!costFunction) return;
|
||||||
|
evaluateCalculation(costFunction, { ...computation.scope, value: row.value });
|
||||||
|
// Write calculation errors
|
||||||
|
costFunction.errors?.forEach(error => {
|
||||||
|
if (error?.message) {
|
||||||
|
row.errors = row.errors || [];
|
||||||
|
error.message = 'Cost calculation error.\n' + error.message;
|
||||||
|
row.errors.push(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (Number.isFinite(costFunction.value)) {
|
||||||
|
row.spent = costFunction.value;
|
||||||
|
prop.spent += costFunction.value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
prop.pointsLeft = (prop.total?.value || 0) - (prop.spent || 0);
|
||||||
|
if (prop.spent > prop.total?.value) {
|
||||||
|
prop.errors = prop.errors || [];
|
||||||
|
prop.errors.push({
|
||||||
|
type: 'pointBuyError',
|
||||||
|
message: 'Spent more than total points available',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
// by computeVariableAsSkill
|
// by computeVariableAsSkill
|
||||||
export default function computeSkill(computation, node){
|
export default function computeSkill(computation, node){
|
||||||
const prop = node.data;
|
const prop = node.data;
|
||||||
prop.proficiency = prop.baseProficiency;
|
prop.proficiency = prop.baseProficiency || 0;
|
||||||
let profBonus = computation.scope['proficiencyBonus']?.value || 0;
|
let profBonus = computation.scope['proficiencyBonus']?.value || 0;
|
||||||
// Multiply the proficiency bonus by the actual proficiency
|
// Multiply the proficiency bonus by the actual proficiency
|
||||||
if(prop.proficiency === 0.49){
|
if(prop.proficiency === 0.49){
|
||||||
|
|||||||
@@ -54,6 +54,21 @@ function combineAggregations(computation, node){
|
|||||||
|
|
||||||
function computeVariableProp(computation, node, prop){
|
function computeVariableProp(computation, node, prop){
|
||||||
if (!prop) return;
|
if (!prop) return;
|
||||||
|
|
||||||
|
// Combine damage multipliers in all props so that they can't be overridden
|
||||||
|
if (node.data.immunity){
|
||||||
|
prop.immunity = node.data.immunity;
|
||||||
|
prop.immunities = node.data.immunities;
|
||||||
|
}
|
||||||
|
if (node.data.resistance){
|
||||||
|
prop.resistance = node.data.resistance;
|
||||||
|
prop.resistances = node.data.resistances;
|
||||||
|
}
|
||||||
|
if (node.data.vulnerability){
|
||||||
|
prop.vulnerability = node.data.vulnerability;
|
||||||
|
prop.vulnerabilities = node.data.vulnerabilities;
|
||||||
|
}
|
||||||
|
|
||||||
if (prop.type === 'attribute'){
|
if (prop.type === 'attribute'){
|
||||||
computeVariableAsAttribute(computation, node, prop);
|
computeVariableAsAttribute(computation, node, prop);
|
||||||
} else if (prop.type === 'skill'){
|
} else if (prop.type === 'skill'){
|
||||||
@@ -73,21 +88,16 @@ function combineMultiplierAggregator(node){
|
|||||||
if (!aggregator) return;
|
if (!aggregator) return;
|
||||||
|
|
||||||
// Combine
|
// Combine
|
||||||
let value;
|
if (aggregator.immunities?.length){
|
||||||
if (aggregator.immunityCount){
|
node.data.immunity = true;
|
||||||
value = 0;
|
node.data.immunities = aggregator.immunities;
|
||||||
} else if (
|
}
|
||||||
aggregator.resistanceCount &&
|
if (aggregator.resistances?.length){
|
||||||
!aggregator.vulnerabilityCount
|
node.data.resistance = true;
|
||||||
){
|
node.data.resistances = aggregator.resistances;
|
||||||
value = 0.5;
|
}
|
||||||
} else if (
|
if (aggregator.vulnerabilities?.length){
|
||||||
!aggregator.resistanceCount &&
|
node.data.vulnerability = true;
|
||||||
aggregator.vulnerabilityCount
|
node.data.vulnerabilities = aggregator.vulnerabilities;
|
||||||
){
|
|
||||||
value = 2;
|
|
||||||
} else {
|
|
||||||
value = 1;
|
|
||||||
}
|
}
|
||||||
node.data.damageMultiplyValue = value;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,36 @@
|
|||||||
|
import { pick } from 'lodash';
|
||||||
|
|
||||||
export default function aggregateDamageMultipliers({node, linkedNode, link}){
|
export default function aggregateDamageMultipliers({node, linkedNode, link}){
|
||||||
if (link.data !== 'damageMultiplier') return;
|
if (link.data !== 'damageMultiplier') return;
|
||||||
const multiplierValue = linkedNode.data.value;
|
const multiplierValue = linkedNode.data.value;
|
||||||
if (multiplierValue === undefined) return;
|
if (multiplierValue === undefined) return;
|
||||||
|
|
||||||
// Store an aggregator, its presence indicates damage multipliers target this
|
// Store an aggregator, its presence indicates damage multipliers target this
|
||||||
// variable
|
// variable
|
||||||
if (!node.data.multiplierAggregator) node.data.multiplierAggregator = {
|
if (!node.data.multiplierAggregator) node.data.multiplierAggregator = {
|
||||||
immunityCount: 0,
|
immunities: [],
|
||||||
resistanceCount: 0,
|
resistances: [],
|
||||||
vulnerabilityCount: 0,
|
vulnerabilities: [],
|
||||||
}
|
}
|
||||||
// Store a short reference to the aggregator
|
// Store a short reference to the aggregator
|
||||||
const aggregator = node.data.multiplierAggregator;
|
const aggregator = node.data.multiplierAggregator;
|
||||||
// Sum the counts of each type of multiplier
|
|
||||||
|
// Make a stripped down copy of the multiplier to store in the aggregator
|
||||||
|
const keysToStore = ['_id', 'name'];
|
||||||
|
if (linkedNode.data.excludeTags?.length){
|
||||||
|
keysToStore.push('excludeTags');
|
||||||
|
}
|
||||||
|
if (linkedNode.data.includeTags?.length){
|
||||||
|
keysToStore.push('includeTags');
|
||||||
|
}
|
||||||
|
const storedMultiplier = pick(linkedNode.data, keysToStore);
|
||||||
|
|
||||||
|
// Store the multiplier in the appropriate field
|
||||||
if (multiplierValue === 0){
|
if (multiplierValue === 0){
|
||||||
aggregator.immunityCount += 1;
|
aggregator.immunities.push(storedMultiplier);
|
||||||
} else if (multiplierValue === 0.5){
|
} else if (multiplierValue === 0.5){
|
||||||
aggregator.resistanceCount += 1;
|
aggregator.resistances.push(storedMultiplier);
|
||||||
} else if (multiplierValue === 2){
|
} else if (multiplierValue === 2){
|
||||||
aggregator.vulnerabilityCount += 1;
|
aggregator.vulnerabilities.push(storedMultiplier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ export default function aggregateDefinition({node, linkedNode, link}){
|
|||||||
// get current defining prop
|
// get current defining prop
|
||||||
const definingProp = node.data.definingProp;
|
const definingProp = node.data.definingProp;
|
||||||
// Find the last defining prop
|
// Find the last defining prop
|
||||||
if (!definingProp || prop.order > definingProp.order){
|
if (
|
||||||
|
!definingProp ||
|
||||||
|
prop.type !== 'pointBuyRow' && (
|
||||||
|
definingProp.type === 'pointBuyRow' ||
|
||||||
|
prop.order > definingProp.order
|
||||||
|
)
|
||||||
|
) {
|
||||||
// override the current defining prop
|
// override the current defining prop
|
||||||
overrideProp(definingProp, node);
|
overrideProp(definingProp, node);
|
||||||
// set this prop as the new defining prop
|
// set this prop as the new defining prop
|
||||||
@@ -18,9 +24,32 @@ export default function aggregateDefinition({node, linkedNode, link}){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Aggregate the base value due to the defining properties
|
// Aggregate the base value due to the defining properties
|
||||||
const propBaseValue = prop.baseValue?.value;
|
let propBaseValue = prop.baseValue?.value;
|
||||||
|
// Point buy rows use prop.value instead of prop.baseValue
|
||||||
|
if (prop.type === 'pointBuyRow') {
|
||||||
|
propBaseValue = prop.value;
|
||||||
|
}
|
||||||
|
|
||||||
if (propBaseValue === undefined) return;
|
if (propBaseValue === undefined) return;
|
||||||
|
// Store a summary of the definition as a base value effect
|
||||||
|
node.data.effects = node.data.effects || [];
|
||||||
|
if (prop.type === 'pointBuyRow') {
|
||||||
|
node.data.effects.push({
|
||||||
|
_id: prop.tableId,
|
||||||
|
name: prop.tableName,
|
||||||
|
operation: 'base',
|
||||||
|
amount: { value: propBaseValue },
|
||||||
|
type: 'pointBuy',
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
node.data.effects.push({
|
||||||
|
_id: prop._id,
|
||||||
|
name: prop.name,
|
||||||
|
operation: 'base',
|
||||||
|
amount: { value: propBaseValue },
|
||||||
|
type: prop.type,
|
||||||
|
});
|
||||||
|
}
|
||||||
if (node.data.baseValue === undefined || propBaseValue > node.data.baseValue){
|
if (node.data.baseValue === undefined || propBaseValue > node.data.baseValue){
|
||||||
node.data.baseValue = propBaseValue;
|
node.data.baseValue = propBaseValue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ export default function aggregateEffect({node, linkedNode, link}){
|
|||||||
name: linkedNode.data.name,
|
name: linkedNode.data.name,
|
||||||
operation: linkedNode.data.operation,
|
operation: linkedNode.data.operation,
|
||||||
amount: linkedNode.data.amount && {value: linkedNode.data.amount.value},
|
amount: linkedNode.data.amount && {value: linkedNode.data.amount.value},
|
||||||
|
type: linkedNode.data.type,
|
||||||
// ancestors: linkedNode.data.ancestors,
|
// ancestors: linkedNode.data.ancestors,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,21 @@ import getAggregatorResult from './getAggregatorResult.js';
|
|||||||
*/
|
*/
|
||||||
export default function computeImplicitVariable(node){
|
export default function computeImplicitVariable(node){
|
||||||
const prop = {};
|
const prop = {};
|
||||||
|
|
||||||
|
// Combine damage multipliers
|
||||||
|
if (node.data.immunity){
|
||||||
|
prop.immunity = node.data.immunity;
|
||||||
|
prop.immunities = node.data.immunities;
|
||||||
|
}
|
||||||
|
if (node.data.resistance){
|
||||||
|
prop.resistance = node.data.resistance;
|
||||||
|
prop.resistances = node.data.resistances;
|
||||||
|
}
|
||||||
|
if (node.data.vulnerability){
|
||||||
|
prop.vulnerability = node.data.vulnerability;
|
||||||
|
prop.vulnerabilities = node.data.vulnerabilities;
|
||||||
|
}
|
||||||
|
|
||||||
const result = getAggregatorResult(node);
|
const result = getAggregatorResult(node);
|
||||||
if (result !== undefined){
|
if (result !== undefined){
|
||||||
prop.value = result;
|
prop.value = result;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import getAggregatorResult from './getAggregatorResult.js';
|
import getAggregatorResult from './getAggregatorResult.js';
|
||||||
|
|
||||||
export default function computeVariableAsAttribute(computation, node, prop){
|
export default function computeVariableAsAttribute(computation, node, prop){
|
||||||
let result = getAggregatorResult(node, prop) || 0;
|
let result = getAggregatorResult(node) || 0;
|
||||||
|
|
||||||
prop.total = result;
|
prop.total = result;
|
||||||
prop.value = prop.total - (prop.damage || 0);
|
prop.value = prop.total - (prop.damage || 0);
|
||||||
|
|||||||
@@ -29,8 +29,12 @@ export default function computeVariableAsSkill(computation, node, prop){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Combine everything to get the final result
|
// Combine everything to get the final result
|
||||||
const statBase = node.data.baseValue;
|
const statBase = node.data.baseValue || 0;
|
||||||
const aggregator = node.data.effectAggregator;
|
const aggregator = node.data.effectAggregator;
|
||||||
|
const aggregatorBase = aggregator?.base || 0;
|
||||||
|
|
||||||
|
// Store effects
|
||||||
|
prop.effects = node.data.effects;
|
||||||
|
|
||||||
// If there is no aggregator, determine if the prop can hide, then exit
|
// If there is no aggregator, determine if the prop can hide, then exit
|
||||||
if (!aggregator){
|
if (!aggregator){
|
||||||
@@ -41,7 +45,7 @@ export default function computeVariableAsSkill(computation, node, prop){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Combine aggregator
|
// Combine aggregator
|
||||||
const base = (statBase > aggregator.base ? statBase : aggregator.base) || 0;
|
const base = statBase > aggregatorBase ? statBase : aggregatorBase;
|
||||||
let result = (base + prop.abilityMod + profBonus + aggregator.add) * aggregator.mul;
|
let result = (base + prop.abilityMod + profBonus + aggregator.add) * aggregator.mul;
|
||||||
if (result < aggregator.min) result = aggregator.min;
|
if (result < aggregator.min) result = aggregator.min;
|
||||||
if (result > aggregator.max) result = aggregator.max;
|
if (result > aggregator.max) result = aggregator.max;
|
||||||
@@ -83,7 +87,7 @@ function aggregateAbilityEffects({computation, skillNode, abilityNode}){
|
|||||||
// to a skill from its ability
|
// to a skill from its ability
|
||||||
if (link.data === 'effect'){
|
if (link.data === 'effect'){
|
||||||
if (![
|
if (![
|
||||||
'advantage', 'disadvantage', 'passiveAdd', 'fail'
|
'advantage', 'disadvantage', 'passiveAdd', 'fail', 'conditional'
|
||||||
].includes(linkedNode.data.operation)){
|
].includes(linkedNode.data.operation)){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
import stripFloatingPointOddities from '/imports/api/engine/computation/utility/stripFloatingPointOddities.js';
|
import stripFloatingPointOddities from '/imports/api/engine/computation/utility/stripFloatingPointOddities.js';
|
||||||
|
|
||||||
export default function getAggregatorResult(node){
|
export default function getAggregatorResult(node){
|
||||||
// Work out the base value as the greater of the deining stat value or
|
// Work out the base value as the greater of the deining stat value
|
||||||
// the damage multiplier value
|
|
||||||
// This baseValue comes from aggregating definitions
|
// This baseValue comes from aggregating definitions
|
||||||
let statBase = node.data.baseValue;
|
let statBase = node.data.baseValue;
|
||||||
|
|
||||||
const damageMultiplyValue = node.data.damageMultiplyValue;
|
|
||||||
if (statBase === undefined || damageMultiplyValue > statBase){
|
|
||||||
statBase = damageMultiplyValue;
|
|
||||||
}
|
|
||||||
// get a reference to the aggregator
|
// get a reference to the aggregator
|
||||||
const aggregator = node.data.effectAggregator;
|
const aggregator = node.data.effectAggregator;
|
||||||
|
|
||||||
@@ -34,7 +29,7 @@ export default function getAggregatorResult(node){
|
|||||||
if (aggregator.set !== undefined) {
|
if (aggregator.set !== undefined) {
|
||||||
result = aggregator.set;
|
result = aggregator.set;
|
||||||
}
|
}
|
||||||
if (!node.definingProp?.decimal && Number.isFinite(result)){
|
if (!node.data.definingProp?.decimal && Number.isFinite(result)){
|
||||||
result = Math.floor(result);
|
result = Math.floor(result);
|
||||||
} else if (Number.isFinite(result)){
|
} else if (Number.isFinite(result)){
|
||||||
result = stripFloatingPointOddities(result);
|
result = stripFloatingPointOddities(result);
|
||||||
|
|||||||
@@ -7,9 +7,11 @@ export default function(){
|
|||||||
const computation = buildComputationFromProps(testProperties);
|
const computation = buildComputationFromProps(testProperties);
|
||||||
computeCreatureComputation(computation);
|
computeCreatureComputation(computation);
|
||||||
const scope = id => computation.scope[id];
|
const scope = id => computation.scope[id];
|
||||||
assert.equal(scope('blugeoningMultiplier').value, 1);
|
assert.isTrue(scope('blugeoning').vulnerability);
|
||||||
assert.equal(scope('customDamageMultiplier').value, 0.5);
|
assert.isTrue(scope('customDamage').resistance);
|
||||||
assert.equal(scope('slashingMultiplier').value, 0);
|
assert.isNotTrue(scope('customDamage').immunity);
|
||||||
|
assert.isNotTrue(scope('customDamage').vulnerability);
|
||||||
|
assert.isTrue(scope('slashing').immunity);
|
||||||
}
|
}
|
||||||
|
|
||||||
var testProperties = [
|
var testProperties = [
|
||||||
|
|||||||
@@ -51,17 +51,30 @@ function compute(computation, node){
|
|||||||
|
|
||||||
function pushDependenciesToStack(nodeId, graph, stack, computation){
|
function pushDependenciesToStack(nodeId, graph, stack, computation){
|
||||||
graph.forEachLinkedNode(nodeId, linkedNode => {
|
graph.forEachLinkedNode(nodeId, linkedNode => {
|
||||||
if (linkedNode._visitedChildren && !linkedNode._visited){
|
if (linkedNode._visitedChildren && !linkedNode._visited) {
|
||||||
const pather = path.nba(graph, {
|
// This is a dependency loop, find a path from the node to itself
|
||||||
oriented: true
|
// and store that path as a dependency loop error
|
||||||
});
|
const pather = path.nba(graph, { oriented: true });
|
||||||
const loop = pather.find(nodeId, nodeId);
|
let loop = [];
|
||||||
computation.errors.push({
|
// Pather doesn't like going from a node to iteself, so find all the
|
||||||
type: 'dependencyLoop',
|
// paths going from the next node back to the original node
|
||||||
details: {
|
// and return the shortest one
|
||||||
nodes: loop.map(node => node.id)
|
graph.forEachLinkedNode(nodeId, nextNode => {
|
||||||
},
|
const newLoop = pather.find(nextNode.id, nodeId);
|
||||||
});
|
if (!newLoop.length) return;
|
||||||
|
if (!loop.length || newLoop.length < loop.length - 1) {
|
||||||
|
loop = [linkedNode, ...newLoop];
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
if (loop.length) {
|
||||||
|
computation.errors.push({
|
||||||
|
type: 'dependencyLoop',
|
||||||
|
details: {
|
||||||
|
nodes: loop.map(node => node.id)
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
stack.push(linkedNode);
|
stack.push(linkedNode);
|
||||||
}, true);
|
}, true);
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
export default function getEffectivePropTags(prop) {
|
||||||
|
if (!prop.tags) return [];
|
||||||
|
const tags = [...prop.tags];
|
||||||
|
// Tags for the property type, separate #damage from #healing
|
||||||
|
if (prop.type === 'damage' && prop.damageType === 'healing') {
|
||||||
|
tags.push('#healing');
|
||||||
|
} else {
|
||||||
|
tags.push(`#${prop.type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tags for some string properties
|
||||||
|
if (prop.variableName) tags.push(prop.variableName);
|
||||||
|
if (prop.damageType) tags.push(prop.damageType);
|
||||||
|
if (prop.skillType) tags.push(prop.skillType);
|
||||||
|
if (prop.attributeType) tags.push(prop.attributeType);
|
||||||
|
if (prop.reset) tags.push(prop.reset);
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ export default function walkDown(tree, callback){
|
|||||||
let stack = [...tree];
|
let stack = [...tree];
|
||||||
while(stack.length){
|
while(stack.length){
|
||||||
let node = stack.pop();
|
let node = stack.pop();
|
||||||
callback(node);
|
callback(node, stack);
|
||||||
stack.push(...node.children);
|
stack.push(...node.children);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export default function writeAlteredProperties(computation){
|
|||||||
'deactivatedByAncestor',
|
'deactivatedByAncestor',
|
||||||
'deactivatedByToggle',
|
'deactivatedByToggle',
|
||||||
'damage',
|
'damage',
|
||||||
|
'dirty',
|
||||||
...schema.objectKeys(),
|
...schema.objectKeys(),
|
||||||
];
|
];
|
||||||
op = addChangedKeysToOp(op, keys, original, changed);
|
op = addChangedKeysToOp(op, keys, original, changed);
|
||||||
@@ -28,13 +29,14 @@ export default function writeAlteredProperties(computation){
|
|||||||
bulkWriteOperations.push(op);
|
bulkWriteOperations.push(op);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
writePropertiesSequentially(bulkWriteOperations);
|
bulkWriteProperties(bulkWriteOperations);
|
||||||
|
//if (bulkWriteOperations.length) console.log(`Wrote ${bulkWriteOperations.length} props`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addChangedKeysToOp(op, keys, original, changed) {
|
function addChangedKeysToOp(op, keys, original, changed) {
|
||||||
// Loop through all keys that can be changed by computation
|
// Loop through all keys that can be changed by computation
|
||||||
// and compile an operation that sets all those keys
|
// and compile an operation that sets all those keys
|
||||||
for (let key of keys){
|
for (let key of keys) {
|
||||||
if (!EJSON.equals(original[key], changed[key])){
|
if (!EJSON.equals(original[key], changed[key])){
|
||||||
if (!op) op = newOperation(original._id, changed.type);
|
if (!op) op = newOperation(original._id, changed.type);
|
||||||
let value = changed[key];
|
let value = changed[key];
|
||||||
@@ -79,10 +81,10 @@ function addUnsetOp(op, key){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We use this instead of bulkWriteProperties because it functions with latency
|
// If we re-enable client-side sheet recalculation, this needs to be run on
|
||||||
// compensation without needing to roll back changes, which causes multiple
|
// both client and server to preserve latency compensation. Bulkwrite breaks
|
||||||
// expensive redraws of the character sheet
|
// latency compensation and causes flickering
|
||||||
function writePropertiesSequentially(bulkWriteOps){
|
function writePropertiesSequentially(bulkWriteOps) {
|
||||||
bulkWriteOps.forEach(op => {
|
bulkWriteOps.forEach(op => {
|
||||||
let updateOneOrMany = op.updateOne || op.updateMany;
|
let updateOneOrMany = op.updateOne || op.updateMany;
|
||||||
CreatureProperties.update(updateOneOrMany.filter, updateOneOrMany.update, {
|
CreatureProperties.update(updateOneOrMany.filter, updateOneOrMany.update, {
|
||||||
@@ -101,7 +103,7 @@ function writePropertiesSequentially(bulkWriteOps){
|
|||||||
function bulkWriteProperties(bulkWriteOps){
|
function bulkWriteProperties(bulkWriteOps){
|
||||||
if (!bulkWriteOps.length) return;
|
if (!bulkWriteOps.length) return;
|
||||||
// bulkWrite is only available on the server
|
// bulkWrite is only available on the server
|
||||||
if (Meteor.isServer){
|
if (Meteor.isServer) {
|
||||||
CreatureProperties.rawCollection().bulkWrite(
|
CreatureProperties.rawCollection().bulkWrite(
|
||||||
bulkWriteOps,
|
bulkWriteOps,
|
||||||
{ordered : false},
|
{ordered : false},
|
||||||
|
|||||||
@@ -1,10 +1,61 @@
|
|||||||
|
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
|
||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import { EJSON } from 'meteor/ejson';
|
||||||
|
|
||||||
|
export default function writeScope(creatureId, computation) {
|
||||||
|
if (!creatureId) throw 'creatureId is required';
|
||||||
|
const scope = computation.scope;
|
||||||
|
let variables = computation.variables;
|
||||||
|
if (!variables) {
|
||||||
|
CreatureVariables.insert({ _creatureId: creatureId });
|
||||||
|
variables = {};
|
||||||
|
}
|
||||||
|
delete variables._id;
|
||||||
|
delete variables._creatureId;
|
||||||
|
|
||||||
|
let $set, $unset;
|
||||||
|
|
||||||
export default function writeScope(creatureId, scope){
|
|
||||||
// Remove large properties that aren't likely to be accessed
|
|
||||||
for (const key in scope){
|
for (const key in scope){
|
||||||
|
// Remove large properties that aren't likely to be accessed
|
||||||
delete scope[key].parent;
|
delete scope[key].parent;
|
||||||
delete scope[key].ancestors;
|
delete scope[key].ancestors;
|
||||||
|
|
||||||
|
// Remove empty keys
|
||||||
|
for (const subKey in scope[key]) {
|
||||||
|
if (scope[key][subKey] === undefined) {
|
||||||
|
delete scope[key][subKey]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only update changed fields
|
||||||
|
if (!EJSON.equals(variables[key], scope[key])) {
|
||||||
|
if (!$set) $set = {};
|
||||||
|
/* Log detailed diffs
|
||||||
|
const diff = omitBy(variables[key], (v, k) => EJSON.equals(scope[key][k], v));
|
||||||
|
for (let subkey in diff) {
|
||||||
|
console.log(`${key}.${subkey}: ${variables[key][subkey]} => ${scope[key][subkey]}`)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
// Set the changed key in the creature variables
|
||||||
|
$set[key] = scope[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all the keys that no longer exist in scope
|
||||||
|
for (const key in variables) {
|
||||||
|
if (!scope[key]) {
|
||||||
|
if (!$unset) $unset = {};
|
||||||
|
$unset[key] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($set || $unset) {
|
||||||
|
const update = {};
|
||||||
|
if ($set) update.$set = $set;
|
||||||
|
if ($unset) update.$unset = $unset;
|
||||||
|
CreatureVariables.update({_creatureId: creatureId}, update);
|
||||||
|
}
|
||||||
|
if (computation.creature?.dirty) {
|
||||||
|
Creatures.update({_id: creatureId}, {$unset: { dirty: 1 }});
|
||||||
}
|
}
|
||||||
Creatures.update(creatureId, {$set: {variables: scope}});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,32 @@ import writeErrors from './computation/writeComputation/writeErrors.js';
|
|||||||
|
|
||||||
export default function computeCreature(creatureId){
|
export default function computeCreature(creatureId){
|
||||||
if (Meteor.isClient) return;
|
if (Meteor.isClient) return;
|
||||||
|
// console.log('compute ' + creatureId);
|
||||||
const computation = buildCreatureComputation(creatureId);
|
const computation = buildCreatureComputation(creatureId);
|
||||||
|
computeComputation(computation, creatureId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeComputation(computation, creatureId) {
|
||||||
try {
|
try {
|
||||||
computeCreatureComputation(computation);
|
computeCreatureComputation(computation);
|
||||||
writeAlteredProperties(computation);
|
writeAlteredProperties(computation);
|
||||||
writeScope(creatureId, computation.scope);
|
writeScope(creatureId, computation);
|
||||||
} catch (e){
|
} catch (e){
|
||||||
|
const errorText = e.reason || e.message || e.toString();
|
||||||
computation.errors.push({
|
computation.errors.push({
|
||||||
type: 'crash',
|
type: 'crash',
|
||||||
details: e.reason,
|
details: { error: errorText },
|
||||||
});
|
});
|
||||||
|
const logError = {
|
||||||
|
creatureId,
|
||||||
|
computeError: errorText,
|
||||||
|
};
|
||||||
|
if (e.stack) {
|
||||||
|
logError.location = e.stack.split('\n')[1];
|
||||||
|
}
|
||||||
|
console.error(logError);
|
||||||
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
writeErrors(creatureId, computation.errors);
|
writeErrors(creatureId, computation.errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now just recompute the whole creature, TODO only recompute a single
|
|
||||||
// connected section of the depdendency graph
|
|
||||||
export function computeCreatureDependencyGroup(property){
|
|
||||||
let creatureId = property.ancestors[0].id;
|
|
||||||
computeCreature(creatureId);
|
|
||||||
}
|
|
||||||
|
|||||||
303
app/imports/api/engine/loadCreatures.js
Normal file
303
app/imports/api/engine/loadCreatures.js
Normal file
@@ -0,0 +1,303 @@
|
|||||||
|
import { debounce } from 'lodash';
|
||||||
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables';
|
||||||
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
|
import computeCreature from './computeCreature';
|
||||||
|
|
||||||
|
const COMPUTE_DEBOUNCE_TIME = 100; // ms
|
||||||
|
export const loadedCreatures = new Map(); // creatureId => {creature, properties, etc.}
|
||||||
|
|
||||||
|
export function loadCreature(creatureId, subscription) {
|
||||||
|
if (!creatureId) throw 'creatureId is required';
|
||||||
|
let creature = loadedCreatures.get(creatureId);
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
creature.subs.add(subscription);
|
||||||
|
} else {
|
||||||
|
creature = new LoadedCreature(subscription, creatureId);
|
||||||
|
loadedCreatures.set(creatureId, creature);
|
||||||
|
}
|
||||||
|
subscription.onStop(() => {
|
||||||
|
unloadCreature(creatureId, subscription);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function unloadCreature(creatureId, subscription) {
|
||||||
|
if (!creatureId) throw 'creatureId is required';
|
||||||
|
const creature = loadedCreatures.get(creatureId);
|
||||||
|
if (!creature) return;
|
||||||
|
creature.subs.delete(subscription);
|
||||||
|
if (creature.subs.size === 0) {
|
||||||
|
creature.stop();
|
||||||
|
loadedCreatures.delete(creatureId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSingleProperty(creatureId, propertyId) {
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
const creature = loadedCreatures.get(creatureId);
|
||||||
|
const property = creature.properties.get(propertyId);
|
||||||
|
const cloneProp = EJSON.clone(property);
|
||||||
|
return cloneProp;
|
||||||
|
}
|
||||||
|
// console.time(`Cache miss on creature properties: ${creatureId}`)
|
||||||
|
const prop = CreatureProperties.findOne({
|
||||||
|
_id: propertyId,
|
||||||
|
'ancestors.id': creatureId,
|
||||||
|
'removed': {$ne: true},
|
||||||
|
}, {
|
||||||
|
sort: { order: 1 },
|
||||||
|
});
|
||||||
|
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
|
||||||
|
return prop;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getProperties(creatureId) {
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
const creature = loadedCreatures.get(creatureId);
|
||||||
|
const props = Array.from(creature.properties.values());
|
||||||
|
const cloneProps = EJSON.clone(props);
|
||||||
|
return cloneProps
|
||||||
|
}
|
||||||
|
// console.time(`Cache miss on creature properties: ${creatureId}`)
|
||||||
|
const props = CreatureProperties.find({
|
||||||
|
'ancestors.id': creatureId,
|
||||||
|
'removed': {$ne: true},
|
||||||
|
}, {
|
||||||
|
sort: { order: 1 },
|
||||||
|
}).fetch();
|
||||||
|
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPropertiesOfType(creatureId, propType) {
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
const creature = loadedCreatures.get(creatureId);
|
||||||
|
const props = []
|
||||||
|
for (const prop of creature.properties.values()){
|
||||||
|
if (prop.type === propType) {
|
||||||
|
props.push(prop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const cloneProps = EJSON.clone(props);
|
||||||
|
return cloneProps
|
||||||
|
}
|
||||||
|
// console.time(`Cache miss on creature properties: ${creatureId}`)
|
||||||
|
const props = CreatureProperties.find({
|
||||||
|
'ancestors.id': creatureId,
|
||||||
|
'removed': { $ne: true },
|
||||||
|
'type': propType,
|
||||||
|
}, {
|
||||||
|
sort: { order: 1 },
|
||||||
|
}).fetch();
|
||||||
|
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCreature(creatureId) {
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
const loadedCreature = loadedCreatures.get(creatureId);
|
||||||
|
const creature = loadedCreature.creature;
|
||||||
|
if (creature) {
|
||||||
|
const cloneCreature = EJSON.clone(creature);
|
||||||
|
return cloneCreature;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// console.time(`Cache miss on Creature: ${creatureId}`);
|
||||||
|
const creature = Creatures.findOne(creatureId);
|
||||||
|
// console.timeEnd(`Cache miss on Creature: ${creatureId}`);
|
||||||
|
return creature;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getVariables(creatureId) {
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
const loadedCreature = loadedCreatures.get(creatureId);
|
||||||
|
const variables = loadedCreature.variables;
|
||||||
|
if (variables) {
|
||||||
|
const cloneVarables = EJSON.clone(variables);
|
||||||
|
return cloneVarables;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// console.time(`Cache miss on variables: ${creatureId}`);
|
||||||
|
const variables = CreatureVariables.findOne({_creatureId: creatureId});
|
||||||
|
// console.timeEnd(`Cache miss on variables: ${creatureId}`);
|
||||||
|
return variables;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getProperyAncestors(creatureId, propertyId) {
|
||||||
|
const prop = getSingleProperty(creatureId, propertyId);
|
||||||
|
if (!prop) return [];
|
||||||
|
const ancestorIds = [];
|
||||||
|
prop.ancestors.forEach(ref => {
|
||||||
|
if (ref.collection === 'creatureProperties') {
|
||||||
|
ancestorIds.push(ref.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
// Get the ancestor properties from the cache
|
||||||
|
const creature = loadedCreatures.get(creatureId);
|
||||||
|
const props = [];
|
||||||
|
ancestorIds.forEach(id => {
|
||||||
|
const prop = creature.properties.get(id);
|
||||||
|
if (prop) {
|
||||||
|
props.push(prop);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const cloneProps = EJSON.clone(props);
|
||||||
|
return cloneProps
|
||||||
|
} else {
|
||||||
|
// Fetch from database
|
||||||
|
return CreatureProperties.find({
|
||||||
|
_id: { $in: ancestorIds },
|
||||||
|
removed: {$ne: true},
|
||||||
|
}, {
|
||||||
|
sort: { order: 1 },
|
||||||
|
}).fetch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPropertyDecendants(creatureId, propertyId) {
|
||||||
|
const property = getSingleProperty(creatureId, propertyId);
|
||||||
|
if (!property) return [];
|
||||||
|
// This prop will always appear at the same position in the ancestor array
|
||||||
|
// of its decendants, so only check there
|
||||||
|
const expectedAncestorPostition = property.ancestors.length;
|
||||||
|
if (loadedCreatures.has(creatureId)) {
|
||||||
|
const creature = loadedCreatures.get(creatureId);
|
||||||
|
const props = [];
|
||||||
|
for(const prop of creature.properties.values()){
|
||||||
|
if (prop.ancestors[expectedAncestorPostition]?.id === propertyId) {
|
||||||
|
props.push(prop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const cloneProps = EJSON.clone(props);
|
||||||
|
return cloneProps
|
||||||
|
} else {
|
||||||
|
return CreatureProperties.find({
|
||||||
|
'ancestors.id': propertyId,
|
||||||
|
removed: { $ne: true },
|
||||||
|
}, {
|
||||||
|
sort: { order: 1 },
|
||||||
|
}).fetch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoadedCreature {
|
||||||
|
constructor(sub, creatureId) {
|
||||||
|
// This may be called from a subscription, but we don't want the observers
|
||||||
|
// to be destroyed with it, so use a non-reactive context to observe
|
||||||
|
// the required documents
|
||||||
|
const self = this;
|
||||||
|
Tracker.nonreactive(() => {
|
||||||
|
self.subs = new Set([sub]);
|
||||||
|
|
||||||
|
const compute = debounce(Meteor.bindEnvironment(() => {
|
||||||
|
computeCreature(creatureId);
|
||||||
|
}), COMPUTE_DEBOUNCE_TIME);
|
||||||
|
|
||||||
|
self.properties = new Map();
|
||||||
|
// Observe all creature properties which are needed for computation
|
||||||
|
self.propertyObserver = CreatureProperties.find({
|
||||||
|
'ancestors.id': creatureId,
|
||||||
|
removed: { $ne: true },
|
||||||
|
}, {
|
||||||
|
sort: { order: 1 },
|
||||||
|
}).observeChanges({
|
||||||
|
added(id, fields) {
|
||||||
|
fields._id = id;
|
||||||
|
self.addProperty(fields);
|
||||||
|
if (fields.dirty) compute();
|
||||||
|
},
|
||||||
|
changed(id, fields) {
|
||||||
|
self.changeProperty(id, fields);
|
||||||
|
if (fields.dirty) compute();
|
||||||
|
},
|
||||||
|
removed(id) {
|
||||||
|
self.removeProperty(id);
|
||||||
|
compute();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Observe the creature itself
|
||||||
|
self.creatureObserver = Creatures.find({
|
||||||
|
_id: creatureId,
|
||||||
|
}).observeChanges({
|
||||||
|
added(id, fields) {
|
||||||
|
fields._id = id;
|
||||||
|
self.addCreature(fields)
|
||||||
|
if (fields.dirty) compute();
|
||||||
|
},
|
||||||
|
changed(id, fields) {
|
||||||
|
self.changeCreature(id, fields);
|
||||||
|
if (fields.dirty) compute();
|
||||||
|
},
|
||||||
|
removed(id) {
|
||||||
|
self.removeCreature(id);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Observe the creature's variables
|
||||||
|
self.variablesObserver = CreatureVariables.find({
|
||||||
|
_creatureId: creatureId,
|
||||||
|
}, {
|
||||||
|
fields: { _creatureId: 0},
|
||||||
|
}).observeChanges({
|
||||||
|
added(id, fields) {
|
||||||
|
fields._id = id;
|
||||||
|
self.addVariables(fields)
|
||||||
|
},
|
||||||
|
changed(id, fields) {
|
||||||
|
self.changeVariables(id, fields);
|
||||||
|
},
|
||||||
|
removed(id) {
|
||||||
|
self.removeVariables(id);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
stop() {
|
||||||
|
this.propertyObserver.stop();
|
||||||
|
this.creatureObserver.stop();
|
||||||
|
this.variablesObserver.stop();
|
||||||
|
}
|
||||||
|
addProperty(prop) {
|
||||||
|
this.properties.set(prop._id, prop);
|
||||||
|
}
|
||||||
|
changeProperty(id, fields) {
|
||||||
|
LoadedCreature.changeMap(id, fields, this.properties);
|
||||||
|
}
|
||||||
|
removeProperty(id) {
|
||||||
|
this.properties.delete(id)
|
||||||
|
}
|
||||||
|
addCreature(creature) {
|
||||||
|
this.creature = creature;
|
||||||
|
}
|
||||||
|
changeCreature(id, fields) {
|
||||||
|
LoadedCreature.changeDoc(this.creature, fields);
|
||||||
|
}
|
||||||
|
removeCreature() {
|
||||||
|
delete this.creature;
|
||||||
|
}
|
||||||
|
addVariables(variables) {
|
||||||
|
this.variables = variables;
|
||||||
|
}
|
||||||
|
changeVariables(id, fields) {
|
||||||
|
LoadedCreature.changeDoc(this.variables, fields);
|
||||||
|
}
|
||||||
|
removeVariables() {
|
||||||
|
delete this.variables;
|
||||||
|
}
|
||||||
|
static changeMap(id, fields, map) {
|
||||||
|
const doc = map.get(id);
|
||||||
|
LoadedCreature.changeDoc(doc, fields);
|
||||||
|
}
|
||||||
|
static changeDoc(doc, fields) {
|
||||||
|
if (!doc) return;
|
||||||
|
for (let key in fields) {
|
||||||
|
if (key === undefined) {
|
||||||
|
delete doc[key];
|
||||||
|
} else {
|
||||||
|
doc[key] = fields[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/imports/api/files/UserImages.js
Normal file
19
app/imports/api/files/UserImages.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { createS3FilesCollection } from '/imports/api/files/s3FileStorage.js';
|
||||||
|
|
||||||
|
const UserImages = createS3FilesCollection({
|
||||||
|
collectionName: 'userImages',
|
||||||
|
storagePath: Meteor.isDevelopment ? '/DiceCloud/userImages/' : 'assets/app/userImages',
|
||||||
|
onBeforeUpload(file) {
|
||||||
|
// Allow upload files under 10MB
|
||||||
|
if (file.size > 10485760) {
|
||||||
|
return 'Please upload with size equal or less than 10MB';
|
||||||
|
}
|
||||||
|
// Allow common image extensions
|
||||||
|
if (!/gif|png|jpe?g|webp/i.test(file.extension || '')) {
|
||||||
|
return 'Please upload an image file only';
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default UserImages;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
// https://github.com/VeliovGroup/Meteor-Files/blob/master/docs/aws-s3-integration.md
|
// https://github.com/VeliovGroup/Meteor-Files/blob/master/docs/aws-s3-integration.md
|
||||||
|
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
import { each, clone } from 'lodash';
|
import { each, clone } from 'lodash';
|
||||||
import { Random } from 'meteor/random';
|
import { Random } from 'meteor/random';
|
||||||
@@ -37,8 +36,9 @@ if (Meteor.isServer && Meteor.settings.useS3) {
|
|||||||
secretAccessKey: s3Conf.secret,
|
secretAccessKey: s3Conf.secret,
|
||||||
endpoint: s3Conf.endpoint,
|
endpoint: s3Conf.endpoint,
|
||||||
sslEnabled: true, // optional
|
sslEnabled: true, // optional
|
||||||
|
maxRetries: 10,
|
||||||
httpOptions: {
|
httpOptions: {
|
||||||
timeout: 6000,
|
timeout: 12000,
|
||||||
agent: false
|
agent: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -47,14 +47,18 @@ if (Meteor.isServer && Meteor.settings.useS3) {
|
|||||||
collectionName,
|
collectionName,
|
||||||
storagePath,
|
storagePath,
|
||||||
onBeforeUpload,
|
onBeforeUpload,
|
||||||
debug = Meteor.isProduction,
|
onAfterUpload,
|
||||||
|
debug = !Meteor.isProduction,
|
||||||
allowClientCode = false,
|
allowClientCode = false,
|
||||||
}){
|
}){
|
||||||
const collection = new FilesCollection({
|
const collection = new FilesCollection({
|
||||||
collectionName,
|
collectionName,
|
||||||
storagePath,
|
storagePath,
|
||||||
onBeforeUpload,
|
onBeforeUpload,
|
||||||
onAfterUpload(fileRef){
|
onAfterUpload(fileRef) {
|
||||||
|
// Call the provided afterUpload hook first
|
||||||
|
onAfterUpload?.(fileRef);
|
||||||
|
|
||||||
// Start moving files to AWS:S3
|
// Start moving files to AWS:S3
|
||||||
// after fully received by the Meteor server
|
// after fully received by the Meteor server
|
||||||
|
|
||||||
@@ -213,25 +217,24 @@ if (Meteor.isServer && Meteor.settings.useS3) {
|
|||||||
return collection;
|
return collection;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Meteor.isServer){
|
|
||||||
console.log('No S3 details specified, files will be stored in the local filesystem');
|
|
||||||
}
|
|
||||||
createS3FilesCollection = function({
|
createS3FilesCollection = function({
|
||||||
collectionName,
|
collectionName,
|
||||||
storagePath,
|
storagePath,
|
||||||
onBeforeUpload,
|
onBeforeUpload,
|
||||||
debug = Meteor.isProduction,
|
onAfterUpload,
|
||||||
|
debug = !Meteor.isProduction,
|
||||||
allowClientCode = false,
|
allowClientCode = false,
|
||||||
}){
|
}){
|
||||||
const collection = new FilesCollection({
|
const collection = new FilesCollection({
|
||||||
collectionName,
|
collectionName,
|
||||||
storagePath,
|
storagePath,
|
||||||
onBeforeUpload,
|
onBeforeUpload,
|
||||||
|
onAfterUpload,
|
||||||
debug,
|
debug,
|
||||||
allowClientCode,
|
allowClientCode,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Meteor.isServer){
|
if (Meteor.isServer) {
|
||||||
// Use the normal file system to read files
|
// Use the normal file system to read files
|
||||||
collection.readJSONFile = async function(file){
|
collection.readJSONFile = async function(file){
|
||||||
const fileString = await fsp.readFile(file.path, 'utf8');
|
const fileString = await fsp.readFile(file.path, 'utf8');
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ let LibrarySchema = new SimpleSchema({
|
|||||||
type: String,
|
type: String,
|
||||||
max: STORAGE_LIMITS.name,
|
max: STORAGE_LIMITS.name,
|
||||||
},
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
optional: true,
|
||||||
|
max: STORAGE_LIMITS.summary,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
LibrarySchema.extend(SharingSchema);
|
LibrarySchema.extend(SharingSchema);
|
||||||
@@ -76,6 +81,29 @@ const updateLibraryName = new ValidatedMethod({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const updateLibraryDescription = new ValidatedMethod({
|
||||||
|
name: 'libraries.updateDescription',
|
||||||
|
validate: new SimpleSchema({
|
||||||
|
_id: {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.id
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
}).validator(),
|
||||||
|
mixins: [RateLimiterMixin],
|
||||||
|
rateLimit: {
|
||||||
|
numRequests: 5,
|
||||||
|
timeInterval: 5000,
|
||||||
|
},
|
||||||
|
run({_id, description}){
|
||||||
|
let library = Libraries.findOne(_id);
|
||||||
|
assertEditPermission(library, this.userId);
|
||||||
|
Libraries.update(_id, {$set: {description}});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const removeLibrary = new ValidatedMethod({
|
const removeLibrary = new ValidatedMethod({
|
||||||
name: 'libraries.remove',
|
name: 'libraries.remove',
|
||||||
validate: new SimpleSchema({
|
validate: new SimpleSchema({
|
||||||
@@ -102,4 +130,4 @@ export function removeLibaryWork(libraryId){
|
|||||||
LibraryNodes.remove({'ancestors.id': libraryId});
|
LibraryNodes.remove({'ancestors.id': libraryId});
|
||||||
}
|
}
|
||||||
|
|
||||||
export { LibrarySchema, insertLibrary, updateLibraryName, removeLibrary };
|
export { LibrarySchema, insertLibrary, updateLibraryName, updateLibraryDescription, removeLibrary };
|
||||||
|
|||||||
135
app/imports/api/library/LibraryCollections.js
Normal file
135
app/imports/api/library/LibraryCollections.js
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||||
|
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||||
|
import SimpleSchema from 'simpl-schema';
|
||||||
|
import SharingSchema from '/imports/api/sharing/SharingSchema.js';
|
||||||
|
import simpleSchemaMixin from '/imports/api/creature/mixins/simpleSchemaMixin.js';
|
||||||
|
import { assertEditPermission, assertOwnership } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
|
import { getUserTier } from '/imports/api/users/patreon/tiers.js'
|
||||||
|
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LibraryCollections are groups of libraries that are subscribed together at once
|
||||||
|
*/
|
||||||
|
const LibraryCollections = new Mongo.Collection('libraryCollections');
|
||||||
|
|
||||||
|
const LibraryCollectionSchema = new SimpleSchema({
|
||||||
|
name: {
|
||||||
|
type: String,
|
||||||
|
optional: true,
|
||||||
|
max: STORAGE_LIMITS.name,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
optional: true,
|
||||||
|
max: STORAGE_LIMITS.summary,
|
||||||
|
},
|
||||||
|
libraries: {
|
||||||
|
type: Array,
|
||||||
|
defaultValue: [],
|
||||||
|
maxCount: STORAGE_LIMITS.libraryCollectionCount,
|
||||||
|
},
|
||||||
|
'libraries.$': {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
LibraryCollectionSchema.extend(SharingSchema);
|
||||||
|
LibraryCollections.attachSchema(LibraryCollectionSchema);
|
||||||
|
|
||||||
|
export default LibraryCollections;
|
||||||
|
|
||||||
|
const insertLibraryCollection = new ValidatedMethod({
|
||||||
|
name: 'libraryCollections.insert',
|
||||||
|
mixins: [
|
||||||
|
simpleSchemaMixin,
|
||||||
|
],
|
||||||
|
schema: LibraryCollectionSchema.omit('owner'),
|
||||||
|
run(libraryCollection) {
|
||||||
|
if (!this.userId) {
|
||||||
|
throw new Meteor.Error('LibraryCollections.methods.insert.denied',
|
||||||
|
'You need to be logged in to insert a library');
|
||||||
|
}
|
||||||
|
let tier = getUserTier(this.userId);
|
||||||
|
if (!tier.paidBenefits){
|
||||||
|
throw new Meteor.Error('LibraryCollections.methods.insert.denied',
|
||||||
|
`The ${tier.name} tier does not allow you to insert a library collection`);
|
||||||
|
}
|
||||||
|
libraryCollection.owner = this.userId;
|
||||||
|
return LibraryCollections.insert(libraryCollection);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const updateLibraryCollection = new ValidatedMethod({
|
||||||
|
name: 'libraryCollections.update',
|
||||||
|
mixins: [
|
||||||
|
simpleSchemaMixin,
|
||||||
|
],
|
||||||
|
schema: {
|
||||||
|
_id: {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
type: LibraryCollectionSchema
|
||||||
|
.pick('name', 'description', 'libraries')
|
||||||
|
.extend({ //make libraries optional
|
||||||
|
libraries: {
|
||||||
|
optional: true,
|
||||||
|
defaultValue: undefined,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
rateLimit: {
|
||||||
|
numRequests: 5,
|
||||||
|
timeInterval: 5000,
|
||||||
|
},
|
||||||
|
run({_id, update}){
|
||||||
|
const libraryCollection = LibraryCollections.findOne(_id, {
|
||||||
|
fields: {
|
||||||
|
owner: 1,
|
||||||
|
writers: 1,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assertEditPermission(libraryCollection, this.userId);
|
||||||
|
return LibraryCollections.update(_id, {$set: update});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const removeLibraryCollection = new ValidatedMethod({
|
||||||
|
name: 'libraryCollections.remove',
|
||||||
|
validate: new SimpleSchema({
|
||||||
|
_id: {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.id
|
||||||
|
},
|
||||||
|
}).validator(),
|
||||||
|
mixins: [RateLimiterMixin],
|
||||||
|
rateLimit: {
|
||||||
|
numRequests: 5,
|
||||||
|
timeInterval: 5000,
|
||||||
|
},
|
||||||
|
run({_id}){
|
||||||
|
const libraryCollection = LibraryCollections.findOne(_id, {
|
||||||
|
fields: {
|
||||||
|
owner: 1,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assertOwnership(libraryCollection, this.userId);
|
||||||
|
return LibraryCollections.remove(_id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function getLibraryIdsByCollectionId(libraryCollectionId) {
|
||||||
|
const libraryCollection = LibraryCollections.findOne(libraryCollectionId)
|
||||||
|
return libraryCollection?.libraries || [];
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
LibraryCollectionSchema,
|
||||||
|
insertLibraryCollection,
|
||||||
|
updateLibraryCollection,
|
||||||
|
removeLibraryCollection,
|
||||||
|
getLibraryIdsByCollectionId,
|
||||||
|
};
|
||||||
39
app/imports/api/library/getCreatureLibraryIds.js
Normal file
39
app/imports/api/library/getCreatureLibraryIds.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import LibraryCollections from '/imports/api/library/LibraryCollections.js';
|
||||||
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import getUserLibraryIds from './getUserLibraryIds';
|
||||||
|
import { intersection, union } from 'lodash';
|
||||||
|
|
||||||
|
export default function getCreatureLibraryIds(creature, userId) {
|
||||||
|
if (!userId) return [];
|
||||||
|
|
||||||
|
// Get the ids of libraries the user is permitted to view
|
||||||
|
const userLibIds = getUserLibraryIds(userId);
|
||||||
|
|
||||||
|
// If given a creature Id, get the creature document
|
||||||
|
if (typeof creature === 'string') {
|
||||||
|
creature = Creatures.findOne(creature, {
|
||||||
|
fields: {
|
||||||
|
allowedLibraries: 1,
|
||||||
|
allowedLibraryCollections: 1,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!creature) return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the creature does not restrict the libraries, let it use them all
|
||||||
|
if (!creature.allowedLibraryCollections && !creature.allowedLibraries) {
|
||||||
|
return userLibIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the ids of the libraries that the creature allows
|
||||||
|
const allowedCollections = creature.allowedLibraryCollections || [];
|
||||||
|
let creatureLibIds = creature.allowedLibraries || [];
|
||||||
|
LibraryCollections.find({
|
||||||
|
_id: { $in: allowedCollections }
|
||||||
|
}, { fields: { libraries: 1 } }).forEach(collection => {
|
||||||
|
creatureLibIds = union(creatureLibIds, collection.libraries);
|
||||||
|
});
|
||||||
|
|
||||||
|
// return all the ids that the creature allows and the user can view
|
||||||
|
return intersection(userLibIds, creatureLibIds);
|
||||||
|
}
|
||||||
31
app/imports/api/library/getUserLibraryIds.js
Normal file
31
app/imports/api/library/getUserLibraryIds.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import LibraryCollections from '/imports/api/library/LibraryCollections.js';
|
||||||
|
import Libraries from '/imports/api/library/Libraries.js';
|
||||||
|
import { union } from 'lodash';
|
||||||
|
|
||||||
|
export default function getUserLibraryIds(userId) {
|
||||||
|
if (!userId) return [];
|
||||||
|
const user = Meteor.users.findOne(userId);
|
||||||
|
let subbedIds = user?.subscribedLibraries || [];
|
||||||
|
const subCollections = user?.subscribedLibraryCollections || [];
|
||||||
|
LibraryCollections.find({
|
||||||
|
$or: [
|
||||||
|
{ owner: userId },
|
||||||
|
{ writers: userId },
|
||||||
|
{ readers: userId },
|
||||||
|
{ _id: { $in: subCollections }, public: true },
|
||||||
|
]
|
||||||
|
}, { fields: { libraries: 1 } }).forEach(collection => {
|
||||||
|
subbedIds = union(subbedIds, collection.libraries);
|
||||||
|
});
|
||||||
|
const libraryIds = Libraries.find({
|
||||||
|
$or: [
|
||||||
|
{ owner: userId },
|
||||||
|
{ writers: userId },
|
||||||
|
{ readers: userId },
|
||||||
|
{ _id: { $in: subbedIds }, public: true },
|
||||||
|
]
|
||||||
|
}, {
|
||||||
|
fields: { _id: 1 }
|
||||||
|
}).map(lib => lib._id);
|
||||||
|
return libraryIds;
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
|
|||||||
import { assertDocEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
import { assertDocEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||||
import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js';
|
import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js';
|
||||||
import getCollectionByName from '/imports/api/parenting/getCollectionByName.js';
|
import getCollectionByName from '/imports/api/parenting/getCollectionByName.js';
|
||||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
|
||||||
const organizeDoc = new ValidatedMethod({
|
const organizeDoc = new ValidatedMethod({
|
||||||
name: 'organize.organizeDoc',
|
name: 'organize.organizeDoc',
|
||||||
@@ -57,10 +57,11 @@ const organizeDoc = new ValidatedMethod({
|
|||||||
let parentCreatures = getCreatureAncestors(parent);
|
let parentCreatures = getCreatureAncestors(parent);
|
||||||
if (!skipRecompute){
|
if (!skipRecompute){
|
||||||
let creaturesToRecompute = union(docCreatures, parentCreatures);
|
let creaturesToRecompute = union(docCreatures, parentCreatures);
|
||||||
// Recompute the creatures
|
// Mark the creatures for recompute
|
||||||
creaturesToRecompute.forEach(id => {
|
Creatures.update({
|
||||||
// Some Dependencies depend on ancestry, so a full recompute is needed
|
_id: { $in: creaturesToRecompute }
|
||||||
computeCreature(id);
|
}, {
|
||||||
|
$set: { dirty: true },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -85,9 +86,14 @@ const reorderDoc = new ValidatedMethod({
|
|||||||
assertDocEditPermission(doc, this.userId);
|
assertDocEditPermission(doc, this.userId);
|
||||||
safeUpdateDocOrder({docRef, order});
|
safeUpdateDocOrder({docRef, order});
|
||||||
// Recompute the affected creatures
|
// Recompute the affected creatures
|
||||||
getCreatureAncestors(doc).forEach(id => {
|
const ancestors = getCreatureAncestors(doc);
|
||||||
computeCreature(id);
|
if (ancestors.length) {
|
||||||
});
|
Creatures.update({
|
||||||
|
_id: { $in: ancestors }
|
||||||
|
}, {
|
||||||
|
$set: { dirty: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -40,17 +40,22 @@ const restoreError = function(){
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export function restore({_id, collection}){
|
export function restore({ _id, collection, extraUpdates}){
|
||||||
if (typeof collection === 'string') {
|
if (typeof collection === 'string') {
|
||||||
collection = getCollectionByName(collection);
|
collection = getCollectionByName(collection);
|
||||||
}
|
}
|
||||||
|
const update = {
|
||||||
|
$unset: {
|
||||||
|
removed: 1,
|
||||||
|
removedAt: 1,
|
||||||
|
},
|
||||||
|
...extraUpdates
|
||||||
|
}
|
||||||
|
|
||||||
let numUpdated = collection.update({
|
let numUpdated = collection.update({
|
||||||
_id,
|
_id,
|
||||||
removedWith: {$exists: false}
|
removedWith: {$exists: false}
|
||||||
}, { $unset: {
|
}, update , {
|
||||||
removed: 1,
|
|
||||||
removedAt: 1,
|
|
||||||
}}, {
|
|
||||||
selector: {type: 'any'},
|
selector: {type: 'any'},
|
||||||
},);
|
},);
|
||||||
if (numUpdated === 0) restoreError();
|
if (numUpdated === 0) restoreError();
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Actions are things a character can do
|
* Actions are things a character can do
|
||||||
* Any rolls that are children of actions will be rolled when taking the action
|
|
||||||
* Any actions that are children of this action will be considered alternatives
|
|
||||||
* to this action
|
|
||||||
*/
|
*/
|
||||||
let ActionSchema = createPropertySchema({
|
let ActionSchema = createPropertySchema({
|
||||||
name: {
|
name: {
|
||||||
@@ -117,6 +114,11 @@ let ActionSchema = createPropertySchema({
|
|||||||
type: 'fieldToCompute',
|
type: 'fieldToCompute',
|
||||||
optional: true,
|
optional: true,
|
||||||
},
|
},
|
||||||
|
// Prevent the property from showing up in the log
|
||||||
|
silent: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const ComputedOnlyActionSchema = createPropertySchema({
|
const ComputedOnlyActionSchema = createPropertySchema({
|
||||||
|
|||||||
@@ -31,6 +31,11 @@ const AdjustmentSchema = createPropertySchema({
|
|||||||
allowedValues: ['set', 'increment'],
|
allowedValues: ['set', 'increment'],
|
||||||
defaultValue: 'increment',
|
defaultValue: 'increment',
|
||||||
},
|
},
|
||||||
|
// Prevent the property from showing up in the log
|
||||||
|
silent: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const ComputedOnlyAdjustmentSchema = createPropertySchema({
|
const ComputedOnlyAdjustmentSchema = createPropertySchema({
|
||||||
|
|||||||
@@ -47,6 +47,35 @@ let AttributeSchema = createPropertySchema({
|
|||||||
spellSlotLevel: {
|
spellSlotLevel: {
|
||||||
type: 'fieldToCompute',
|
type: 'fieldToCompute',
|
||||||
optional: true,
|
optional: true,
|
||||||
|
},
|
||||||
|
// For type healthBar midColor, and lowColor can be set separately from the
|
||||||
|
// property's color, which is used as the undamaged color
|
||||||
|
'healthBarColorMid': {
|
||||||
|
type: String,
|
||||||
|
regEx: /^#([a-f0-9]{3}){1,2}\b$/i,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
|
'healthBarColorLow': {
|
||||||
|
type: String,
|
||||||
|
regEx: /^#([a-f0-9]{3}){1,2}\b$/i,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
|
// Control how the health bar takes damage or healing
|
||||||
|
healthBarNoDamage: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
|
healthBarNoHealing: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
|
healthBarDamageOrder: {
|
||||||
|
type: SimpleSchema.Integer,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
|
healthBarHealingOrder: {
|
||||||
|
type: SimpleSchema.Integer,
|
||||||
|
optional: true,
|
||||||
},
|
},
|
||||||
// The starting value, before effects
|
// The starting value, before effects
|
||||||
baseValue: {
|
baseValue: {
|
||||||
@@ -67,6 +96,16 @@ let AttributeSchema = createPropertySchema({
|
|||||||
decimal: {
|
decimal: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
optional: true,
|
optional: true,
|
||||||
|
},
|
||||||
|
// Can the total after damage be negative
|
||||||
|
ignoreLowerLimit: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
|
// Can the damage value be negative
|
||||||
|
ignoreUpperLimit: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
},
|
},
|
||||||
// Automatically zero the adjustment on these conditions
|
// Automatically zero the adjustment on these conditions
|
||||||
reset: {
|
reset: {
|
||||||
@@ -133,6 +172,16 @@ let ComputedOnlyAttributeSchema = createPropertySchema({
|
|||||||
optional: true,
|
optional: true,
|
||||||
removeBeforeCompute: true,
|
removeBeforeCompute: true,
|
||||||
},
|
},
|
||||||
|
// A list of effect ids targeting this attribute
|
||||||
|
effects: {
|
||||||
|
type: Array,
|
||||||
|
optional: true,
|
||||||
|
removeBeforeCompute: true,
|
||||||
|
},
|
||||||
|
'effects.$': {
|
||||||
|
type: Object,
|
||||||
|
blackbox: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const ComputedAttributeSchema = new SimpleSchema()
|
const ComputedAttributeSchema = new SimpleSchema()
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user