Compare commits

...

157 Commits

Author SHA1 Message Date
Stefan Zermatten
27f1f4e720 Fixed a bug with dialog animations hiding buttons by failing to clean up 2021-01-12 13:51:36 +02:00
Stefan Zermatten
d63b8c835d Fixed bug in last release where unlimited slots were always hidden on hide when full 2021-01-12 13:51:11 +02:00
Stefan Zermatten
85f3881935 Removed alert about data loss, most data structures are stable and it's being alarmist 2021-01-12 13:40:02 +02:00
Stefan Zermatten
5e4299e6db removed stray console logging 2021-01-12 13:24:08 +02:00
Stefan Zermatten
0e663f36db Slot fillers that count as more than one slot now update the space left correctly.
Slot fillers removed from a library can no longer be added to a slot.
If a slot has limited space left, this will be reflected on the error 
finding slots message
2021-01-12 13:22:48 +02:00
Stefan Zermatten
de9ea5922c Fixed bugs when a spell list does not have limit on prepared spells 2021-01-12 13:10:36 +02:00
Stefan Zermatten
a2cfe43e74 Disabled the direct damage input in the attribute form: it can't be edited anyway 2021-01-12 13:04:25 +02:00
Stefan Zermatten
026c11c13b Made sure attributes show their currentValue instead of Value 2021-01-12 13:03:33 +02:00
Stefan Zermatten
403f2663c2 Fixed bugs with item display, equipment will now automatically move to the first property with the 'equipment' tag, carried items will move to the first property with the 'carried' tag 2021-01-12 12:54:02 +02:00
Stefan Zermatten
28c042343e All children of infinite slots will now hide when "hide when full" is active 2021-01-12 10:36:07 +02:00
Stefan Zermatten
3f116875a1 Children of slots now display in the correct order 2021-01-12 10:35:37 +02:00
Stefan Zermatten
ae5b4b7d5c Made inactive toggle decendants specifically included when recomputing active properties 2021-01-11 22:03:54 +02:00
Stefan Zermatten
75835d74f6 Merge pull request #259 from Ganonsmasher/version-2
Found a fix for toggles.
2020-12-17 17:19:54 +02:00
Ganonsmasher
583b652fc4 Found a fix for toggles.
It doesn't fix the problem of their property being absent on the stats page cards, but they can compute properly and disable their children with this.
2020-12-15 18:08:29 -05:00
Stefan Zermatten
df317a8942 Merge branch 'version-2' of https://github.com/ThaumRystra/DiceCloud into version-2 2020-11-20 14:21:52 +02:00
Stefan Zermatten
ff0e9b1ff9 Added Lord of Junk to Patreon Paragons 2020-11-20 14:13:47 +02:00
Stefan Zermatten
fa24430a7f Fixed parsing of variable names with numbers and stacked dice rolls like dd8-> 1d(1d8) 2020-11-13 10:04:37 +02:00
Stefan Zermatten
fde2f821e7 Fixed parser not handling whitespace 2020-11-12 21:44:08 +02:00
Stefan Zermatten
827430c987 Fixed edit permission errors for some creature prop methods 2020-11-12 21:25:48 +02:00
Stefan Zermatten
2a1aa02e97 Added true and false keywords, fixed grammar ambiguity in if statements 2020-11-12 15:11:24 +02:00
Stefan Zermatten
aeb347084f Fixed ambiguitiy in grammar caused by previous fixes 2020-11-12 13:47:10 +02:00
Stefan Zermatten
005bc162cb Fixed != not being matched because ! was matched first. Fixed presedence for & | and relational operators 2020-11-12 13:45:14 +02:00
Stefan Zermatten
9941d91bb8 Fixed != operator, separated == and ===, != and !== for strictness control 2020-11-12 13:44:01 +02:00
Stefan Zermatten
23c77690a1 Healing damage type now heals instead of damaging 2020-11-12 13:01:09 +02:00
Stefan Zermatten
525b528d9a Added attribute damage and self damage results to actions and log. 2020-11-12 12:57:48 +02:00
Stefan Zermatten
3917f63d5e Increased subscription rate limit to prevent infinitely spinning characters 2020-11-12 11:04:04 +02:00
Stefan Zermatten
bd056ab042 Improved subscription permissions, should now work as expected for public documents 2020-11-12 10:48:46 +02:00
Stefan Zermatten
cd84b2562a Fixed an error with finding deployed version SHA 2020-11-10 14:48:09 +02:00
Stefan Zermatten
c7436ffb1e Caught error where git couldn't be used to get version 2020-11-10 14:37:32 +02:00
Stefan Zermatten
be7d7f898f disabled short and log rest buttons if user has no edit permission 2020-11-10 14:22:09 +02:00
Stefan Zermatten
3024168e95 Replaced old parser with new parser 2020-11-10 14:07:22 +02:00
Stefan Zermatten
1f0678b50b Added not operator to the parser 2020-11-05 15:32:01 +02:00
Stefan Zermatten
4dad2c41e5 Updated parser to accept underscores in variable names 2020-11-05 14:50:44 +02:00
Stefan Zermatten
46385dd9b2 Build details with no slots hidden moved to a dialog 2020-11-05 14:27:01 +02:00
Stefan Zermatten
7cb65954b5 Added the ability to hide slots when full 2020-11-05 14:05:17 +02:00
Stefan Zermatten
749799d869 Denormalised slot fill total to database 2020-11-05 13:35:55 +02:00
Stefan Zermatten
3293dad671 Limited what fields are included when fetching recompute documents to improve performance 2020-11-05 12:59:48 +02:00
Stefan Zermatten
88df942c59 Fixed an error with missing identity details in patreon request 2020-11-05 12:59:26 +02:00
Stefan Zermatten
9722bbc667 Characters now recompute on subscribe if they haven't been computed in the current version 2020-11-04 14:27:31 +02:00
Stefan Zermatten
2fb0ba79c6 began work to get inactive state of properties denormalised 2020-11-03 15:57:14 +02:00
Stefan Zermatten
3f7ddd62fc Merge branch 'version-2' of https://github.com/ThaumRystra/DiceCloud1 into version-2 2020-10-27 10:41:06 +02:00
Stefan Zermatten
227d6c5aae Markdown and calculations now supported in slot filler descriptions 2020-10-27 10:39:20 +02:00
Stefan Zermatten
147ef97576 Markdown now supported in slot filler descriptions 2020-10-27 10:34:01 +02:00
Stefan Zermatten
54806b0f3c Merge branch 'version-2' of https://github.com/ThaumRystra/DiceCloud1 into version-2 2020-10-19 11:39:24 +02:00
Stefan Zermatten
1165158d46 Forced creatures to reorder their docs before recomputing 2020-10-19 11:39:21 +02:00
Stefan Zermatten
32de70cd45 Class levels now have a description field 2020-10-17 19:33:56 +02:00
Stefan Zermatten
68499e4de5 You can now click on properties filling slots to view their details dialog 2020-10-17 19:28:54 +02:00
Stefan Zermatten
3f4cb8e26b Added undo buttons for deleting properties off a creature 2020-10-17 19:10:37 +02:00
Stefan Zermatten
ebab41838c Used tree node views in slot fill selection 2020-10-17 16:56:23 +02:00
Stefan Zermatten
e8da7a6c17 Moved snackbars to their own store and component 2020-10-17 16:06:27 +02:00
Stefan Zermatten
46189c68df All property forms now allow tags 2020-10-17 13:42:24 +02:00
Stefan Zermatten
9fa997ed24 Started moving snackbars to vue store, still needs to be separated into its own module 2020-10-16 13:38:58 +02:00
Stefan Zermatten
e3bf6557ec fixed bug in dialog store 2020-10-16 10:35:51 +02:00
Stefan Zermatten
7aa3e5a217 Stringified errors from scheduled deletion 2020-10-16 10:23:34 +02:00
Stefan Zermatten
dc1b025090 Increased delete job frequency to 10 minutes 2020-10-16 10:00:07 +02:00
Stefan Zermatten
2e370a9884 Fixed removed slots not being hidden 2020-10-15 16:42:57 +02:00
Stefan Zermatten
384fa076f1 hotfix tags not filtering correctly 2020-10-15 16:12:32 +02:00
Stefan Zermatten
7922e30ddc Added tags to some properties. Added condition to class levels 2020-10-15 16:00:32 +02:00
Stefan Zermatten
1ba4f76763 Class levels can now have conditions 2020-10-15 15:57:19 +02:00
Stefan Zermatten
839f91c3b2 Fixed icons not going to dark mode when slot filling cards are selected 2020-10-15 15:30:11 +02:00
Stefan Zermatten
26567ce840 slot fill cards with pictures no longer get icons 2020-10-15 15:25:10 +02:00
Stefan Zermatten
2a729a4eca Made slot fill dialog a list of cards to leverage pretty slotFillers 2020-10-15 15:24:14 +02:00
Stefan Zermatten
ed17d9e2d2 Added slotfiller property type to increase control over slot filling 2020-10-15 14:54:58 +02:00
Stefan Zermatten
8e9405b5ad Allowed slots with unlimited children, improved slot ui text 2020-10-15 13:50:46 +02:00
Stefan Zermatten
7fc783dcad Removed recompute button 2020-10-15 13:50:14 +02:00
Stefan Zermatten
b15ad7e51a Merge branch 'version-2' of https://github.com/ThaumRystra/DiceCloud1 into version-2 2020-10-15 13:49:49 +02:00
Stefan Zermatten
8a3d2474fc Merge pull request #254 from JoeZwet/version-2
fix: prevent discord mention exploit
2020-10-15 13:48:30 +02:00
Joe van der Zwet
09371e7d54 add requested changes 2020-10-16 00:47:32 +13:00
Joe van der Zwet
0776d33909 fix: prevent discord mention exploit 2020-10-16 00:36:32 +13:00
Stefan Zermatten
6e98d71c3c Improved slot UI look and feel 2020-10-15 13:00:29 +02:00
Stefan Zermatten
8f89f4b63f Ensured all subscriptions return empty arrays instead of errors or ready 2020-10-15 12:34:46 +02:00
Stefan Zermatten
c0070d017e Removed debugging code 2020-10-14 15:54:16 +02:00
Stefan Zermatten
51569592ab First implementation on Slots UI 2020-10-14 14:45:26 +02:00
Stefan Zermatten
d2cb86ac27 Fixed broken logging for actions 2020-10-14 11:33:25 +02:00
Stefan Zermatten
bde9183158 Log optimistic UI now fixed, rolls are now instant 2020-10-14 11:25:05 +02:00
Stefan Zermatten
0cc9e01754 Renamed, moved LogTab to CharacterLog 2020-10-14 09:37:00 +02:00
Stefan Zermatten
9856471202 Stopped log making toast if it's visible 2020-10-14 09:33:17 +02:00
Stefan Zermatten
4f77782a7a log messages are now aligned right 2020-10-13 13:53:30 +02:00
Stefan Zermatten
5f13aaa031 Fixed empty strings in log input 2020-10-13 13:44:06 +02:00
Stefan Zermatten
1321cf6a96 Moved log tab to right drawer 2020-10-13 13:42:18 +02:00
Stefan Zermatten
dee8249f61 Creature logs are now removed with creatures 2020-10-13 12:43:55 +02:00
Stefan Zermatten
0af0afc0d0 Discord webhooks now mirror character log 2020-10-13 12:42:02 +02:00
Stefan Zermatten
a104fc8a87 Fixing broken casing on file pt.2 2020-10-06 10:55:12 +02:00
Stefan Zermatten
46f452987f Fixing broken casing on file 2020-10-06 10:54:52 +02:00
Stefan Zermatten
a87cb1286a Improved custom rolls on log tab 2020-10-06 09:53:08 +02:00
Stefan Zermatten
844588cdbf Started adding text input to log tab 2020-09-30 16:24:33 +02:00
Stefan Zermatten
a6a96fc19f Started work on character log for rolls to be stored 2020-09-29 22:34:30 +02:00
Stefan Zermatten
75ab43da00 Started work on UI for rolling checks 2020-09-29 16:37:28 +02:00
Stefan Zermatten
df7000889b fixed security deps 2020-09-29 10:54:37 +02:00
Stefan Zermatten
65754dea80 removed damage multipliers from health bar card, it has its own card 2020-09-29 10:54:27 +02:00
Stefan Zermatten
30cca39e7c Merge branch 'version-2' of https://github.com/ThaumRystra/DiceCloud1 into version-2 2020-09-28 13:58:03 +02:00
Stefan Zermatten
5ad5c914fb Added gitignore file for renders 2020-09-24 19:00:28 +02:00
Stefan Zermatten
f27550362a Fixed various parser bugs, implemented unary operators 2020-09-18 22:13:12 +02:00
Stefan Zermatten
50f7977a60 Fixed patreon update write location 2020-09-18 20:37:16 +02:00
Stefan Zermatten
bc5c465a32 Started work on checks 2020-09-18 14:00:29 +02:00
Stefan Zermatten
c8ddf9d547 Added the ability to double all number of dice to roll using context 2020-09-18 12:24:08 +02:00
Stefan Zermatten
6570665c1e Added functions and ensured the context was being passed around correctly 2020-09-18 11:52:44 +02:00
Stefan Zermatten
06f17a6d33 Parser now uses context to store details of the computation 2020-09-18 10:14:53 +02:00
Stefan Zermatten
b69ad6c306 Removed unused parser code 2020-09-10 11:39:27 +02:00
Stefan Zermatten
5dec760452 Parser now works with variables passed into scope 2020-09-10 11:38:28 +02:00
Stefan Zermatten
ede4e1367d Continued work on parser, now calling functions and rolling correctly 2020-09-10 00:14:24 +02:00
Stefan Zermatten
81645df2a6 Lots of work on the parser including testing interface 2020-09-09 17:09:50 +02:00
Stefan Zermatten
445171ce80 Added preferences subheader to accounts page 2020-09-09 13:58:48 +02:00
Stefan Zermatten
dedab7b046 Added patreon tier refresh button, autorefresh tier on login daily 2020-09-09 13:55:21 +02:00
Stefan Zermatten
a5c16ba83a Overhauled inventory tab again. Closer in functionality to V1 2020-08-22 00:36:17 +02:00
Stefan Zermatten
46501f2759 Spells that aren't prepared no longer count as active properties 2020-08-21 16:34:52 +02:00
Stefan Zermatten
a6ed1004be Added Resistance, Vulnerability, and Immunity to the health bar card 2020-08-21 16:27:01 +02:00
Stefan Zermatten
8539356b9e Added UI to prepare spells 2020-08-21 16:04:31 +02:00
Stefan Zermatten
93db5e9288 Made library link readonly instead of disabled so it can be copied 2020-08-10 04:23:55 +02:00
Stefan Zermatten
b4cb91a892 Multiple libraries can now be opened, allowing library items to be moved 2020-08-10 04:19:54 +02:00
Stefan Zermatten
9c93747845 Fixed bug where array accessors were attempting to use the substitution engine prematurely 2020-08-10 04:14:53 +02:00
Stefan Zermatten
a51154e434 Prevented test webhooks being sent in production 2020-07-26 19:56:08 +02:00
Stefan Zermatten
1bde0db0ba Fixed features showing up when disabled by an ancestor 2020-07-26 19:53:07 +02:00
Stefan Zermatten
bc001202ec Fixed spell list tiles not being opaque 2020-07-26 19:47:44 +02:00
Stefan Zermatten
c7985af83b Continued work on tabletops, hidden from main app and all methods disallowed for non-admins 2020-07-26 19:45:07 +02:00
Stefan Zermatten
0f20cd4bd9 Implemented drag and drop on spells page 2020-07-26 19:39:50 +02:00
Stefan Zermatten
1ac01941c7 Fixed bug where multiple classes woudn't show up in persona tab 2020-07-26 16:43:26 +02:00
Stefan Zermatten
95d8d2cb9a Started work on tabletop view 2020-07-17 23:31:12 +02:00
Stefan Zermatten
47345b3694 Experimenting with webhooks. 2020-07-13 16:38:24 +02:00
Stefan Zermatten
308168791b Made dX rolls work as 1dX 2020-06-30 15:15:55 +02:00
Stefan Zermatten
7be4280508 Began implementing dice rolls in the maths parser 2020-06-30 14:40:20 +02:00
Stefan Zermatten
56f9e82326 Improved spells tab to be more in line with the v1 implementation 2020-06-29 14:52:47 +02:00
Stefan Zermatten
6ddea8a8ab Improved slot schema, added ui for slots 2020-06-29 14:15:49 +02:00
Stefan Zermatten
e1ddfb2cab Fixed a bug where registering a property without disabledByToggle breaks recompuation 2020-06-24 18:20:56 +02:00
Stefan Zermatten
d36d5b15d0 hide unused spell card 2020-06-24 18:17:35 +02:00
Stefan Zermatten
2af687361e Improved spell appearance in spell tab 2020-06-23 01:36:48 +02:00
Stefan Zermatten
e572807082 Attributes of type spell slot now store their slot level 2020-06-23 01:36:21 +02:00
Stefan Zermatten
c44aeac198 Added 'prepared' field to spells 2020-06-22 13:45:47 +02:00
Stefan Zermatten
757cf5c34b Fixed spells not being able to be inserted or editing in characters 2020-06-22 13:45:35 +02:00
Stefan Zermatten
8cbfec25b3 Added the setting to swap ability scores and modifiers to the account page 2020-06-22 13:22:53 +02:00
Stefan Zermatten
c4dc5895aa Relaxed rate limiting on icon search, improved error messaging 2020-06-22 00:20:40 +02:00
Stefan Zermatten
cffe0ee574 Added minimal UI to display applied buffs 2020-06-22 00:14:07 +02:00
Stefan Zermatten
ce51be7b8e moved proficiencies after actions on the stats tab 2020-06-21 23:57:19 +02:00
Stefan Zermatten
315073bd8e Refactored actions and let actions apply buffs to self 2020-06-21 23:54:51 +02:00
Stefan Zermatten
50b99ef54f Improved performance of adding library properties with many decendants 2020-06-21 23:24:07 +02:00
Stefan Zermatten
9b01f5fb45 Improved actions UI, Actions (including spells) can now have icons 2020-06-17 13:23:13 +02:00
Stefan Zermatten
389785f5db Fixed bug where library large screen view won't scroll 2020-06-17 13:22:48 +02:00
Stefan Zermatten
e1bfb173ab Overhauled action detail view 2020-06-16 13:51:58 +02:00
Stefan Zermatten
ecba587253 Fixed a bug with proficiency forms not editing proficiency correctly 2020-06-16 12:35:50 +02:00
Stefan Zermatten
3f540d0f14 Overhaul of character action components, actions now consume resources 2020-06-15 22:30:27 +02:00
Stefan Zermatten
dc18734d1f Backend work to support actions consuming their resources on use 2020-06-13 23:11:49 +02:00
Stefan Zermatten
1535e00093 Denormalized some calculations into recomputation step 2020-06-07 21:08:53 +02:00
Stefan Zermatten
5198c655e9 Added subscription rate limiting 2020-06-06 14:30:15 +02:00
Stefan Zermatten
8d41643136 Increased damage property rate limit to 4/s 2020-06-06 14:25:23 +02:00
Stefan Zermatten
ea8d036c72 Added rate limiting to all methods 2020-06-06 14:23:13 +02:00
Stefan Zermatten
93d566e263 Exposed methods and publications to http requests, changed method names 2020-06-06 12:31:07 +02:00
Stefan Zermatten
b4da32f9ab Fixed soft removed documents never getting permanently removed 2020-06-05 23:08:31 +02:00
Stefan Zermatten
986fe8fd93 Added an autofocus field to most forms 2020-06-05 22:39:21 +02:00
Stefan Zermatten
dd4596851e Improved class level viewer and tree node view 2020-06-05 22:25:22 +02:00
Stefan Zermatten
bc3fc9574a Added loading and empty state to experience list 2020-06-05 22:20:40 +02:00
Stefan Zermatten
db1ae5db3d Iterated on XP system 2020-06-05 21:48:28 +02:00
Stefan Zermatten
d1e7eb2fa0 Added basic XP system 2020-06-05 16:14:26 +02:00
Stefan Zermatten
efb8b87a2d Alphabetized properties by displayed name 2020-05-31 22:39:15 +02:00
Stefan Zermatten
b04b915c7b Removed stray logging 2020-05-31 22:36:27 +02:00
Stefan Zermatten
21b823f85c Dark mode now with 20% more dark 2020-05-31 22:25:04 +02:00
Stefan Zermatten
4631579181 Character toolbar now correctly uses dark and light text where appropriate 2020-05-31 22:22:42 +02:00
Stefan Zermatten
edf3920e84 Character sheet toolbars now match the color of the character 2020-05-31 22:16:38 +02:00
247 changed files with 9123 additions and 1835 deletions

View File

@@ -3,7 +3,7 @@
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.
accounts-password@1.6.0
accounts-password@1.6.2
accounts-ui@1.3.1
random@1.2.0
dburles:collection-helpers
@@ -13,7 +13,7 @@ momentjs:moment
dburles:mongo-collection-instances
percolate:migrations
accounts-google@1.3.3
email@1.2.3
email@2.0.0
meteorhacks:subs-manager
chuangbo:marked
meteor-base@1.4.0
@@ -38,7 +38,7 @@ ongoworks:speakingurl
service-configuration@1.0.11
google-config-ui@1.0.1
dynamic-import@0.5.2
ddp-rate-limiter@1.0.7
ddp-rate-limiter@1.0.9
rate-limit@1.0.9
meteortesting:mocha
mdg:validated-method
@@ -50,3 +50,6 @@ akryum:vue-component
accounts-patreon
bozhao:link-accounts
peerlibrary:reactive-publish
simple:rest
simple:rest-method-mixin
mikowals:batch-insert

View File

@@ -1 +1 @@
METEOR@1.10.2
METEOR@1.11.1

View File

@@ -1,7 +1,7 @@
accounts-base@1.6.0
accounts-base@1.7.0
accounts-google@1.3.3
accounts-oauth@1.2.0
accounts-password@1.6.0
accounts-password@1.6.2
accounts-patreon@0.1.0
accounts-ui@1.3.1
accounts-ui-unstyled@1.4.2
@@ -10,7 +10,7 @@ akryum:vue-component@0.15.2
akryum:vue-component-dev-client@0.4.7
akryum:vue-component-dev-server@0.1.4
akryum:vue-router2@0.2.3
aldeed:collection2@3.0.6
aldeed:collection2@3.2.1
aldeed:schema-index@3.0.0
allow-deny@1.1.0
autoupdate@1.6.0
@@ -20,8 +20,8 @@ base64@1.0.12
binary-heap@1.0.11
blaze@2.3.4
blaze-tools@1.0.10
boilerplate-generator@1.7.0
bozhao:link-accounts@2.1.1
boilerplate-generator@1.7.1
bozhao:link-accounts@2.2.1
caching-compiler@1.2.2
caching-html-compiler@1.1.3
callback-hook@1.3.0
@@ -34,17 +34,17 @@ dburles:mongo-collection-instances@0.3.5
ddp@1.4.0
ddp-client@2.3.3
ddp-common@1.4.0
ddp-rate-limiter@1.0.7
ddp-server@2.3.1
ddp-rate-limiter@1.0.9
ddp-server@2.3.2
deps@1.0.12
diff-sequence@1.1.1
dynamic-import@0.5.2
dynamic-import@0.5.3
ecmascript@0.14.3
ecmascript-runtime@0.7.0
ecmascript-runtime-client@0.10.0
ecmascript-runtime-server@0.9.0
ecmascript-runtime-client@0.11.0
ecmascript-runtime-server@0.10.0
ejson@1.1.1
email@1.2.3
email@2.0.0
es5-shim@4.8.0
fetch@0.1.1
geojson-utils@1.0.10
@@ -69,10 +69,11 @@ meteor@1.9.3
meteor-base@1.4.0
meteorhacks:picker@1.0.3
meteorhacks:subs-manager@1.6.4
meteortesting:browser-tests@1.3.3
meteortesting:browser-tests@1.3.4
meteortesting:mocha@1.1.5
meteortesting:mocha-core@7.0.1
minifier-css@1.5.0
mikowals:batch-insert@1.2.0
minifier-css@1.5.3
minifier-js@2.6.0
minimongo@1.6.0
mobile-experience@1.1.0
@@ -80,14 +81,14 @@ mobile-status-bar@1.1.0
modern-browsers@0.1.5
modules@0.15.0
modules-runtime@0.12.0
momentjs:moment@2.24.0
momentjs:moment@2.29.1
mongo@1.10.0
mongo-decimal@0.1.1
mongo-dev-server@1.1.0
mongo-id@1.0.7
npm-bcrypt@0.9.3
npm-mongo@3.7.0
oauth@1.3.0
npm-mongo@3.8.1
oauth@1.3.2
oauth2@1.3.0
observe-sequence@1.0.16
ongoworks:speakingurl@9.0.0
@@ -110,12 +111,15 @@ reactive-var@1.0.11
reload@1.3.0
retry@1.1.0
routepolicy@1.1.0
seba:minifiers-autoprefixer@1.1.2
seba:minifiers-autoprefixer@1.2.1
service-configuration@1.0.11
session@1.2.0
sha@1.0.9
shell-server@0.5.0
socket-stream-client@0.3.0
simple:json-routes@2.1.0
simple:rest@1.1.1
simple:rest-method-mixin@1.0.1
socket-stream-client@0.3.1
spacebars@1.0.15
spacebars-compiler@1.1.3
srp@1.1.0
@@ -129,6 +133,6 @@ templating-tools@1.1.2
tmeasday:check-npm-versions@0.3.2
tracker@1.2.0
underscore@1.0.10
url@1.3.0
url@1.3.1
webapp@1.9.1
webapp-hashing@1.0.9

View File

@@ -1,11 +0,0 @@
import SimpleSchema from 'simpl-schema';
let Encounters = new Mongo.Collection("encounters");
let EncounterSchema = new SimpleSchema({
//an encounter is a single flow of time all parties in an encounter are in-sync time wise
});
Encounters.attachSchema(EncounterSchema);
export default Encounters;

View File

@@ -1,49 +0,0 @@
import SimpleSchema from 'simpl-schema';
let Parties = new Mongo.Collection("parties");
let partySchema = new SimpleSchema({
name: {
type: String,
defaultValue: "New Party",
trim: false,
optional: true,
},
characters: {
type: Array,
defaultValue: [],
},
characters: {
type: String,
regEx: SimpleSchema.RegEx.Id,
index: 1,
},
owner: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
});
Parties.attachSchema(partySchema);
Parties.allow({
insert: function(userId, doc) {
return userId && doc.owner === userId;
},
update: function(userId, doc, fields, modifier) {
return userId && doc.owner === userId;
},
remove: function(userId, doc) {
return userId && doc.owner === userId;
},
fetch: ["owner"],
});
Parties.deny({
update: function(userId, docs, fields, modifier) {
// can't change owners
return _.contains(fields, "owner");
}
});
export default Parties;

View File

@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import SimpleSchema from 'simpl-schema';
import ColorSchema from '/imports/api/properties/subSchemas/ColorSchema.js';
@@ -8,7 +9,7 @@ import { recomputeCreature } from '/imports/api/creature/computation/recomputeCr
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
import Creatures from '/imports/api/creature/Creatures.js';
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
import { softRemove } from '/imports/api/parenting/softRemove.js';
import { softRemove, restore } from '/imports/api/parenting/softRemove.js';
import SoftRemovableSchema from '/imports/api/parenting/SoftRemovableSchema.js';
import propertySchemasIndex from '/imports/api/properties/computedPropertySchemasIndex.js';
import {
@@ -18,6 +19,10 @@ import {
} from '/imports/api/parenting/parenting.js';
import {setDocToLastOrder} from '/imports/api/parenting/order.js';
import { storedIconsSchema } from '/imports/api/icons/Icons.js';
import { reorderDocs } from '/imports/api/parenting/order.js';
import '/imports/api/creature/actions/doAction.js';
import '/imports/api/creature/creatureProperties/manageEquipment.js';
let CreatureProperties = new Mongo.Collection('creatureProperties');
@@ -40,7 +45,22 @@ let CreaturePropertySchema = new SimpleSchema({
icon: {
type: storedIconsSchema,
optional: true,
}
},
// Denormalised flag if this property is inactive on the sheet for any reason
// Including being disabled, or a decendent of a disabled property
inactive: {
type: Boolean,
optional: true,
index: 1,
},
// Denormalised flag if this property was made inactive by an inactive
// ancestor. True if this property has an inactive ancestor even if this
// property is itself inactive
deactivatedByAncestor: {
type: Boolean,
optional: true,
index: 1,
},
});
for (let key in propertySchemasIndex){
@@ -55,7 +75,7 @@ for (let key in propertySchemasIndex){
});
}
function getCreature(property){
export function getCreature(property){
if (!property) throw new Meteor.Error('No property provided');
let creature = Creatures.findOne(property.ancestors[0].id);
if (!creature) throw new Meteor.Error('Creature does not exist');
@@ -70,14 +90,20 @@ function assertPropertyEditPermission(property, userId){
function recomputeCreatures(property){
for (let ref of property.ancestors){
if (ref.collection === 'creatures') {
reorderDocs({collection: CreatureProperties, ancestorId: ref.id});
recomputeCreature.call({charId: ref.id});
}
}
}
const insertProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.insert',
name: 'creatureProperties.insert',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({creatureProperty}) {
delete creatureProperty._id;
assertPropertyEditPermission(creatureProperty, this.userId);
@@ -88,13 +114,18 @@ const insertProperty = new ValidatedMethod({
});
const duplicateProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.duplicate',
name: 'creatureProperties.duplicate',
validate: new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
}
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}) {
let creatureProperty = CreatureProperties.findOne(_id);
assertPropertyEditPermission(creatureProperty, this.userId);
@@ -105,7 +136,7 @@ const duplicateProperty = new ValidatedMethod({
});
const insertPropertyFromLibraryNode = new ValidatedMethod({
name: 'CreatureProperties.methods.insertPropertyFromLibraryNode',
name: 'creatureProperties.insertPropertyFromLibraryNode',
validate: new SimpleSchema({
nodeId: {
type: String,
@@ -115,6 +146,11 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
type: RefSchema,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({nodeId, parentRef}) {
// get the new ancestry for the properties
let {parentDoc, ancestors} = getAncestry({parentRef});
@@ -140,7 +176,7 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
'ancestors.id': nodeId,
removed: {$ne: true},
}).fetch();
// The root node is last in the array of nodes
// The root node is last in the array of nodes
nodes.push(node);
// re-map all the ancestors
@@ -163,24 +199,23 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
});
// Insert the creature properties
let docId;
nodes.forEach(doc => {
docId = CreatureProperties.insert(doc);
});
let insertedDocIds = CreatureProperties.batchInsert(nodes);
// get the root inserted doc
let rootId = insertedDocIds[insertedDocIds.length - 1];
// Recompute the creatures doc was attached to
let doc = CreatureProperties.findOne(docId);
recomputeCreatures(doc);
recomputeCreatures(node);
// Return the docId of the last property, the inserted root property
return docId;
return rootId;
},
})
const updateProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.update',
name: 'creatureProperties.update',
validate({_id, path}){
if (!_id) return false;
if (!_id) throw new Meteor.Error('No _id', '_id is required');
// We cannot change these fields with a simple update
switch (path[0]){
case 'type':
@@ -192,6 +227,11 @@ const updateProperty = new ValidatedMethod({
'This property can\'t be updated directly');
}
},
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, value}) {
let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId);
@@ -210,8 +250,86 @@ const updateProperty = new ValidatedMethod({
},
});
export function damagePropertyWork({property, operation, value}){
if (operation === 'set'){
let currentValue = property.value;
// Set represents what we want the value to be after damage
// So we need the actual damage to get to that value
let damage = currentValue - value;
// Damage can't exceed total value
if (damage > currentValue) damage = currentValue;
// Damage must be positive
if (damage < 0) damage = 0;
CreatureProperties.update(property._id, {
$set: {damage}
}, {
selector: property
});
return currentValue - damage;
} else if (operation === 'increment'){
let currentValue = property.value - (property.damage || 0);
let currentDamage = property.damage;
let increment = value;
// Can't increase damage above the remaining value
if (increment > currentValue) increment = currentValue;
// Can't decrease damage below zero
if (-increment > currentDamage) increment = -currentDamage;
CreatureProperties.update(property._id, {
$inc: {damage: increment}
}, {
selector: property
});
return increment;
}
}
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: {
damageMultipliers: 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: property, operation, value})
});
recomputeCreature.call({charId: creatureId});
}
})
const damageProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.adjust',
name: 'creatureProperties.damage',
validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id,
operation: {
@@ -220,6 +338,11 @@ const damageProperty = new ValidatedMethod({
},
value: Number,
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 20,
timeInterval: 5000,
},
run({_id, operation, value}) {
let currentProperty = CreatureProperties.findOne(_id);
// Check permissions
@@ -232,40 +355,94 @@ const damageProperty = new ValidatedMethod({
`Property of type "${currentProperty.type}" can't be damaged`
);
}
if (operation === 'set'){
let currentValue = currentProperty.value;
// Set represents what we want the value to be after damage
// So we need the actual damage to get to that value
let damage = currentValue - value;
// Damage can't exceed total value
if (damage > currentValue) damage = currentValue;
// Damage must be positive
if (damage < 0) damage = 0;
CreatureProperties.update(_id, {
$set: {damage}
}, {
selector: currentProperty
});
} else if (operation === 'increment'){
let currentValue = currentProperty.value - (currentProperty.damage || 0);
let currentDamage = currentProperty.damage;
let increment = value;
// Can't increase damage above the remaining value
if (increment > currentValue) increment = currentValue;
// Can't decrease damage below zero
if (-increment > currentDamage) increment = -currentDamage;
CreatureProperties.update(_id, {
$inc: {damage: increment}
}, {
selector: currentProperty
});
}
damagePropertyWork({property: currentProperty, operation, value})
recomputeCreatures(currentProperty);
},
});
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}) {
let creature = Creatures.findOne(creatureId, {
fields: {
damageMultipliers: 1,
owner: 1,
readers: 1,
writers: 1,
},
});
// Check permissions
assertEditPermission(creature, this.userId);
let healthBars = CreatureProperties.find({
'ancestors.id': creatureId,
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);
let damageLeft = totalDamage;
if (damageType === 'healing') damageLeft = -totalDamage;
healthBars.forEach(healthBar => {
if (damageLeft === 0) return;
let damageAdded = damagePropertyWork({
property: healthBar,
operation: 'increment',
value: damageLeft,
});
damageLeft -= damageAdded;
});
recomputeCreature.call({charId: creatureId});
return totalDamage;
},
});
export function adjustQuantityWork({property, operation, value}){
// Check if property has quantity
let schema = CreatureProperties.simpleSchema(property);
if (!schema.allowsKey('quantity')){
throw new Meteor.Error(
'Adjust quantity failed',
`Property of type "${property.type}" doesn't have a quantity`
);
}
if (operation === 'set'){
CreatureProperties.update(property._id, {
$set: {quantity: value}
}, {
selector: property
});
} else if (operation === 'increment'){
// value here is 'damage'
value = -value;
let currentQuantity = property.quantity;
if (currentQuantity + value < 0) value = -currentQuantity;
CreatureProperties.update(property._id, {
$inc: {quantity: value}
}, {
selector: property
});
}
}
const adjustQuantity = new ValidatedMethod({
name: 'CreatureProperties.methods.adjustQuantity',
name: 'creatureProperties.adjustQuantity',
validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id,
operation: {
@@ -274,41 +451,65 @@ const adjustQuantity = new ValidatedMethod({
},
value: Number,
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, operation, value}) {
let currentProperty = CreatureProperties.findOne(_id);
// Check permissions
assertPropertyEditPermission(currentProperty, this.userId);
// Check if property can take damage
let schema = CreatureProperties.simpleSchema(currentProperty);
if (!schema.allowsKey('quantity')){
throw new Meteor.Error(
'Adjust quantity failed',
`Property of type "${currentProperty.type}" doesn't have a quantity`
);
}
if (operation === 'set'){
CreatureProperties.update(_id, {
$set: {quantity: value}
}, {
selector: currentProperty
});
} else if (operation === 'increment'){
// value here is 'damage'
value = -value;
let currentQuantity = currentProperty.quantity;
if (currentQuantity + value < 0) value = -currentQuantity;
CreatureProperties.update(_id, {
$inc: {quantity: value}
}, {
selector: currentProperty
});
}
adjustQuantityWork({property: currentProperty, operation, value});
recomputeCreatures(currentProperty);
},
});
const selectAmmoItem = new ValidatedMethod({
name: 'creatureProperties.selectAmmoItem',
validate: new SimpleSchema({
actionId: SimpleSchema.RegEx.Id,
itemId: SimpleSchema.RegEx.Id,
itemConsumedIndex: Number,
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({actionId, itemId, itemConsumedIndex}) {
let action = CreatureProperties.findOne(actionId);
// Check permissions
assertPropertyEditPermission(action, this.userId);
// Check that this index has a document to edit
let itemConsumed = action.resources.itemsConsumed[itemConsumedIndex];
if (!itemConsumed){
throw new Meteor.Error('Resouce not found',
'Could not set ammo, because the ammo document was not found');
}
let itemToLink = CreatureProperties.findOne(itemId);
if (!itemToLink){
throw new Meteor.Error('Item not found',
'Could not set ammo: the item was not found');
}
let path = `resources.itemsConsumed.${itemConsumedIndex}.itemId`;
CreatureProperties.update(actionId, {
$set: {[path]: itemId}
}, {
selector: action,
});
recomputeCreatures(action);
},
});
const pushToProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.push',
name: 'creatureProperties.push',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, value}){
let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId);
@@ -322,8 +523,13 @@ const pushToProperty = new ValidatedMethod({
});
const pullFromProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.pull',
name: 'creatureProperties.pull',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, itemId}){
let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId);
@@ -338,10 +544,15 @@ const pullFromProperty = new ValidatedMethod({
});
const softRemoveProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.softRemove',
name: 'creatureProperties.softRemove',
validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}){
let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId);
@@ -350,6 +561,23 @@ const softRemoveProperty = new ValidatedMethod({
}
});
const restoreProperty = new ValidatedMethod({
name: 'creatureProperties.restore',
validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}){
let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId);
restore({_id, collection: CreatureProperties});
recomputeCreatures(property);
}
});
export default CreatureProperties;
export {
@@ -358,9 +586,13 @@ export {
duplicateProperty,
insertPropertyFromLibraryNode,
updateProperty,
dealDamage,
damagePropertiesByName,
damageProperty,
adjustQuantity,
selectAmmoItem,
pushToProperty,
pullFromProperty,
softRemoveProperty,
softRemoveProperty,
restoreProperty,
};

View File

@@ -1,10 +1,11 @@
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import SimpleSchema from 'simpl-schema';
import deathSaveSchema from '/imports/api/properties/subSchemas/DeathSavesSchema.js'
import ColorSchema from '/imports/api/properties/subSchemas/ColorSchema.js';
import SharingSchema from '/imports/api/sharing/SharingSchema.js';
import {assertEditPermission} from '/imports/api/sharing/sharingPermissions.js';
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import '/imports/api/creature/removeCreature.js';
import '/imports/api/creature/restCreature.js';
@@ -39,7 +40,12 @@ let CreatureSettingsSchema = new SimpleSchema({
optional: true,
min: 0,
max: 1,
}
},
discordWebhook: {
type: String,
optional: true,
max: 200,
},
});
let CreatureSchema = new SimpleSchema({
@@ -65,23 +71,35 @@ let CreatureSchema = new SimpleSchema({
type: String,
optional: true,
},
// Mechanics
deathSave: {
type: deathSaveSchema,
defaultValue: {},
},
xp: {
// Stats that are computed and denormalised outside of recomputation
denormalizedStats: {
type: Object,
defaultValue: {},
},
// Sum of all XP gained by this character
'denormalizedStats.xp': {
type: SimpleSchema.Integer,
defaultValue: 0,
},
weightCarried: {
// Sum of all levels granted by milestone XP
'denormalizedStats.milestoneLevels': {
type: SimpleSchema.Integer,
defaultValue: 0,
},
// Sum of all weights of items and containers that are carried
'denormalizedStats.weightCarried': {
type: Number,
defaultValue: 0,
},
level: {
type: SimpleSchema.Integer,
defaultValue: 0,
// Version of computation engine that was last used to compute this creature
computeVersion: {
type: String,
optional: true,
},
type: {
type: String,
@@ -99,6 +117,17 @@ let CreatureSchema = new SimpleSchema({
defaultValue: {}
},
// Tabletop
tabletop: {
type: String,
regEx: SimpleSchema.RegEx.id,
optional: true,
},
initiativeRoll: {
type: SimpleSchema.Integer,
optional: true,
},
// Settings
settings: {
type: CreatureSettingsSchema,
@@ -113,20 +142,22 @@ Creatures.attachSchema(CreatureSchema);
const insertCreature = new ValidatedMethod({
name: 'Creatures.methods.insertCreature',
name: 'creatures.insertCreature',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run() {
if (!this.userId) {
throw new Meteor.Error('Creatures.methods.insert.denied',
'You need to be logged in to insert a creature');
}
let tier = getUserTier(this.userId);
if (!tier.paidBenefits){
throw new Meteor.Error('Creatures.methods.insert.denied',
`The ${tier.name} tier does not allow you to insert a creature`);
}
assertUserHasPaidBenefits(this.userId);
// Create the creature document
let charId = Creatures.insert({
@@ -139,22 +170,41 @@ const insertCreature = new ValidatedMethod({
});
const updateCreature = new ValidatedMethod({
name: 'Creatures.methods.update',
name: 'creatures.update',
validate({_id, path}){
if (!_id) return false;
// Allowed fields
let allowedFields = ['name', 'alignment', 'gender', 'picture', 'avatarPicture', 'settings'];
let allowedFields = [
'name',
'alignment',
'gender',
'picture',
'avatarPicture',
'color',
'settings',
];
if (!allowedFields.includes(path[0])){
throw new Meteor.Error('Creatures.methods.update.denied',
'This field can\'t be updated using this method');
}
},
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, value}) {
let creature = Creatures.findOne(_id);
assertEditPermission(creature, this.userId);
Creatures.update(_id, {
$set: {[path.join('.')]: value},
});
if (value === undefined || value === null){
Creatures.update(_id, {
$unset: {[path.join('.')]: 1},
});
} else {
Creatures.update(_id, {
$set: {[path.join('.')]: value},
});
}
},
});

View File

@@ -0,0 +1,28 @@
import SimpleSchema from 'simpl-schema';
let Parties = new Mongo.Collection('parties');
let partySchema = new SimpleSchema({
name: {
type: String,
defaultValue: 'New Party',
trim: false,
optional: true,
},
creatures: {
type: Array,
defaultValue: [],
},
'creatures.$': {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
owner: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
});
Parties.attachSchema(partySchema);
export default Parties;

View File

@@ -0,0 +1,11 @@
import spendResources from '/imports/api/creature/actions/spendResources.js'
import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
export default function applyAction({prop, creature}){
spendResources(prop);
insertCreatureLog.call({
log: {
text: prop.name,
creatureId: creature._id},
});
}

View File

@@ -0,0 +1,56 @@
import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js';
import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
import { damagePropertiesByName } from '/imports/api/creature/CreatureProperties.js';
export default function applyAdjustment({
prop,
creature,
targets,
actionContext
}){
let damageTargets = prop.target === 'self' ? [creature] : targets;
let scope = {
...creature.variables,
...actionContext,
};
try {
var {result, errors} = evaluateString(prop.amount, scope, 'reduce');
if (typeof result !== 'number') {
return insertCreatureLog.call({ log: {
text: errors.join(', ') || 'Something went wrong',
creatureId: creature._id,
}});
}
} catch (e){
return insertCreatureLog.call({ log: {
text: e.toString(),
creatureId: creature._id,
}});
}
if (damageTargets) {
damageTargets.forEach(target => {
if (prop.target === 'each'){
result = evaluateString(prop.amount, scope, 'reduce');
}
damagePropertiesByName.call({
creatureId: target._id,
variableName: prop.stat,
operation: prop.operation || 'increment',
value: result
});
insertCreatureLog.call({
log: {
text: `${prop.stat} ${prop.operation === 'set' ? 'set to' : ''} ${-result}`,
creatureId: target._id,
}
});
});
} else {
insertCreatureLog.call({
log: {
text: `${prop.stat} ${prop.operation === 'set' ? 'set to' : ''} ${-result}`,
creatureId: creature._id,
}
});
}
}

View File

@@ -0,0 +1,18 @@
import roll from '/imports/parser/roll.js';
import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
export default function applyAttack({
prop,
//children,
creature,
//targets,
//actionContext
}){
let result = roll(1, 20)[0] + prop.rollBonusResult;
insertCreatureLog.call({
log: {
text: `${prop.name} attack. ${result} to hit`,
creatureId: creature._id,
}
});
}

View File

@@ -0,0 +1,61 @@
import {
setLineageOfDocs,
renewDocIds
} from '/imports/api/parenting/parenting.js';
import {setDocToLastOrder} from '/imports/api/parenting/order.js';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
export default function applyBuff({
prop,
children,
creature,
targets = [],
//actionContext,
}){
let buffTargets = prop.target === 'self' ? [creature] : targets;
//let scope = {
// ...creature.variables,
// ...actionContext,
//};
// TODO
// If the target is not self, walk through all decendants and replace
// variables in calculations with their values from the creature scope
// If the target is self, replace all the target.x references with just x
// Then copy the decendants of the buff to the targets
prop.applied = true;
let propList = [prop];
function addChildrenToPropList(children){
children.forEach(child => {
propList.push(child.node);
addChildrenToPropList(child.children);
});
}
addChildrenToPropList(children);
let oldParent = {
id: prop.parent.id,
collection: prop.parent.collection,
};
buffTargets.forEach(target => {
copyNodeListToTarget(propList, target, oldParent);
});
}
function copyNodeListToTarget(propList, target, oldParent){
let ancestry = [{collection: 'creatures', id: target._id}];
setLineageOfDocs({
docArray: propList,
newAncestry: ancestry,
oldParent,
});
renewDocIds({
docArray: propList,
});
setDocToLastOrder({
collection: CreatureProperties,
doc: propList[0],
});
CreatureProperties.batchInsert(propList);
}

View File

@@ -0,0 +1,63 @@
import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js';
import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
import { dealDamage } from '/imports/api/creature/CreatureProperties.js';
export default function applyDamage({
prop,
creature,
targets,
actionContext
}){
let damageTargets = prop.target === 'self' ? [creature] : targets;
let scope = {
...creature.variables,
...actionContext,
};
try {
var {result, errors} = evaluateString(prop.amount, scope, 'reduce');
if (typeof result !== 'number') {
return insertCreatureLog.call({ log: {
text: errors.join(', '),
creatureId: creature._id,
}});
}
} catch (e){
return insertCreatureLog.call({ log: {
text: e.toString(),
creatureId: creature._id,
}});
}
if (damageTargets) {
damageTargets.forEach(target => {
if (prop.target === 'each'){
result = evaluateString(prop.amount, scope, 'reduce');
}
let damageDealt = dealDamage.call({
creatureId: target._id,
damageType: prop.damageType,
amount: result,
});
insertCreatureLog.call({
log: {
text: `Recieved ${damageDealt} ${prop.damageType}${prop.damageType !== 'healing'? ' damage': ''}`,
creatureId: target._id,
}
});
if (target._id !== creature._id){
insertCreatureLog.call({
log: {
text: `Dealt ${damageDealt} ${prop.damageType}${prop.damageType !== 'healing'? ' damage': ''}`,
creatureId: creature._id,
}
});
}
});
} else {
insertCreatureLog.call({
log: {
text: `${result} ${prop.damageType}${prop.damageType !== 'healing'? ' damage': ''}`,
creatureId: creature._id,
}
});
}
}

View File

@@ -0,0 +1,67 @@
import applyAction from '/imports/api/creature/actions/applyAction.js';
import applyAdjustment from '/imports/api/creature/actions/applyAdjustment.js';
import applyAttack from '/imports/api/creature/actions/applyAttack.js';
import applyDamage from '/imports/api/creature/actions/applyDamage.js';
import applyBuff from '/imports/api/creature/actions/applyBuff.js';
function applyProperty(options){
let prop = options.prop;
if (
prop.disabled === true || // ignore disabled props
prop.equipped === false || // ignore unequipped items
prop.toggleResult === false || // ignore untoggled toggles
prop.applied === true // ignore buffs that are already applied
){
return false;
}
switch (prop.type){
case 'action':
case 'spell':
applyAction(options);
return true;
case 'attack':
applyAction(options);
applyAttack(options);
return true;
case 'damage':
applyDamage(options);
return true;
case 'adjustment':
applyAdjustment(options);
return true;
case 'buff':
applyBuff(options);
return false;
case 'roll':
// applyRoll(options);
return true;
case 'savingThrow':
// applySavingThrow(options);
return false;
}
}
export default function applyProperties({
forest,
creature,
targets,
actionContext
}){
forest.forEach(child => {
let walkChildren = applyProperty({
prop: child.node,
children: child.children,
creature,
targets,
actionContext
});
if (walkChildren){
applyProperties({
forest: child.children,
creature,
targets,
actionContext
});
}
});
}

View File

@@ -0,0 +1,62 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import CreatureProperties, { getCreature } from '/imports/api/creature/CreatureProperties.js';
import { assertEditPermission } from '/imports/api/creature/creaturePermissions.js';
import { recomputeCreatureByDoc } from '/imports/api/creature/computation/recomputeCreature.js';
import { nodesToTree } from '/imports/api/parenting/parenting.js';
import applyProperties from '/imports/api/creature/actions/applyProperties.js';
const doAction = new ValidatedMethod({
name: 'creatureProperties.doAction',
validate: new SimpleSchema({
actionId: SimpleSchema.RegEx.Id,
targetId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({actionId, targetId}) {
let action = CreatureProperties.findOne(actionId);
// Check permissions
let creature = getCreature(action);
assertEditPermission(creature, this.userId);
let target = undefined;
if (targetId) {
target = getCreature(targetId);
assertEditPermission(target, this.userId);
}
doActionWork({action, creature, target});
// Note this only recomputes the top-level creature, not the nearest one
recomputeCreatureByDoc(creature);
if (target){
recomputeCreatureByDoc(target);
}
},
});
function doActionWork({action, creature, target}){
let actionContext = {};
let decendantForest = nodesToTree({
collection: CreatureProperties,
ancestorId: action._id,
});
let startingForest = [{
node: action,
children: decendantForest,
}];
applyProperties({
forest: startingForest,
creature,
target,
actionContext
});
}
export default doAction;

View File

@@ -0,0 +1,56 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import Creatures from '/imports/api/creature/Creatures.js';
import { assertEditPermission } from '/imports/api/creature/creaturePermissions.js';
import roll from '/imports/parser/roll.js';
const doCheck = new ValidatedMethod({
name: 'creature.doCheck',
validate: new SimpleSchema({
creatureId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
attributeName: {
type: String,
optional: true,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({creatureId, attributeName}) {
let creature = Creatures.findOne(creatureId);
assertEditPermission(creature, this.userId);
let bonus = getAttributeValue({creature, attributeName})
return doCheckWork({bonus});
},
});
function getAttributeValue({creature, attributeName}){
let att = creature.variables[attributeName];
if (!att) throw new Meteor.Error('No such attribute',
`This creature does not have a ${attributeName} property`);
let bonus = att.attributeType === 'ability'? att.modifier : att.value;
return bonus || 0;
}
export function doCheckWork({bonus, advantage = 0}){
let rolls = roll(2,20);
let chosenRoll;
if (advantage === 1){
chosenRoll = Math.max.apply(rolls);
} else if (advantage === -1){
chosenRoll = Math.min.apply(rolls);
} else {
chosenRoll = rolls[0];
}
let result = chosenRoll + bonus;
return {rolls, bonus, chosenRoll, result};
}
export default doCheck;

View File

@@ -0,0 +1,57 @@
import CreatureProperties, { damagePropertyWork, adjustQuantityWork } from '/imports/api/creature/CreatureProperties.js';
export default function spendResources(action){
// Check Uses
if (action.usesUsed >= action.usesResult){
throw new Meteor.Error('Insufficient Uses',
'This action has no uses left');
}
// Resources
if (action.insufficientResources){
throw new Meteor.Error('Insufficient Resources',
'This creature doesn\'t have sufficient resources to perform this action');
}
// Items
let itemQuantityAdjustments = [];
action.resources.itemsConsumed.forEach(itemConsumed => {
if (!itemConsumed.itemId){
throw new Meteor.Error('Ammo not selected',
'No ammo was selected for this action');
}
let item = CreatureProperties.findOne(itemConsumed.itemId);
if (!item || item.ancestors[0].id !== action.ancestors[0].id){
throw new Meteor.Error('Ammo not found',
'The action\'s ammo was not found on the creature');
}
if (!item.equipped){
throw new Meteor.Error('Ammo not equipped',
'The selected ammo is not equipped');
}
if (!itemConsumed.quantity) return;
itemQuantityAdjustments.push({
property: item,
operation: 'increment',
value: itemConsumed.quantity,
});
});
// No more errors should be thrown after this line
// Now that we have confirmed that there are no errors, do actual work
//Items
itemQuantityAdjustments.forEach(adjustQuantityWork);
// Use uses
CreatureProperties.update(action._id, {
$inc: {usesUsed: 1}
}, {
selector: action
});
// Damage stats
action.resources.attributesConsumed.forEach(attConsumed => {
if (!attConsumed.quantity) return;
let stat = CreatureProperties.findOne(attConsumed.statId);
damagePropertyWork({
property: stat,
operation: 'increment',
value: attConsumed.quantity,
});
});
}

View File

@@ -3,7 +3,7 @@ import { includes, cloneDeep } from 'lodash';
// The computation memo is an in-memory data structure used only during the
// computation process
export default class ComputationMemo {
constructor(props){
constructor(props, creature){
this.statsByVariableName = {};
this.extraStatsByVariableName = {};
this.statsById = {};
@@ -15,6 +15,10 @@ export default class ComputationMemo {
this.classes = {};
this.togglesById = {};
this.toggleIds = new Set();
// Equipped items that might be used as ammo
this.equipmentById = {};
// Properties that have calculations, but don't impact other properties
this.endStepPropsById = {};
// First note all the ids of all the toggles
props.forEach((prop) => {
if (
@@ -38,6 +42,10 @@ export default class ComputationMemo {
) {
// Add all the stats
this.addStat(prop);
} else if (
prop.type === 'item'
) {
this.addEquipment(prop);
} else {
return true;
}
@@ -49,8 +57,19 @@ export default class ComputationMemo {
this.addProficiency(prop);
} else if (prop.type === 'classLevel'){
this.addClassLevel(prop);
} else {
this.addEndStepProp(prop);
}
});
for (let name in creature.denormalizedStats){
if (!this.statsByVariableName[name]){
this.statsByVariableName[name] = {
variableName: name,
value: creature.denormalizedStats[name],
computationDetails: propDetailsByType.denormalizedStat(),
}
}
}
}
registerProperty(prop){
this.originalPropsById[prop._id] = cloneDeep(prop);
@@ -172,6 +191,14 @@ export default class ComputationMemo {
});
return targets;
}
addEquipment(prop){
prop = this.registerProperty(prop);
this.equipmentById[prop._id] = prop;
}
addEndStepProp(prop){
prop = this.registerProperty(prop);
this.endStepPropsById[prop._id] = prop;
}
}
function isAbility(prop){
@@ -197,10 +224,17 @@ function isSkillOperation(prop){
}
function propDetails(prop){
return propDetailsByType[prop.type]() || {};
return propDetailsByType[prop.type] && propDetailsByType[prop.type]() ||
propDetailsByType.default();
}
const propDetailsByType = {
default(){
return {
toggleAncestors: [],
disabledByToggle: false,
};
},
toggle(){
return {
computed: false,
@@ -251,4 +285,10 @@ const propDetailsByType = {
disabledByToggle: false,
};
},
denormalizedStat(){
return {
toggleAncestors: [],
disabledByToggle: false,
};
}
}

View File

@@ -4,10 +4,10 @@ export default class EffectAggregator{
constructor(stat, memo){
delete this.baseValueErrors;
if (stat.baseValueCalculation){
let {value, errors} = evaluateCalculation(stat.baseValueCalculation, memo);
this.statBaseValue = value;
if (errors.length){
this.baseValueErrors = errors;
let {result, context} = evaluateCalculation(stat.baseValueCalculation, memo);
this.statBaseValue = result.value;
if (context.errors.length){
this.baseValueErrors = context.errors;
}
this.base = this.statBaseValue;
} else {

View File

@@ -1,7 +1,7 @@
import math from '/imports/math.js';
import bareSymbolSubtitutor from '/imports/api/creature/computation/utility/bareSymbolSubtitutor.js';
import { parse, CompilationContext } from '/imports/parser/parser.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default function evaluateString(string, scope){
export default function evaluateString(string, scope, fn = 'compile'){
let errors = [];
if (!string){
errors.push('No string provided');
@@ -11,88 +11,19 @@ export default function evaluateString(string, scope){
if (!scope) errors.push('No scope provided');
// Parse the string using mathjs
let calc;
let node;
try {
calc = math.parse(string);
node = parse(string);
} catch (e) {
errors.push(e);
return {result: string, errors};
}
// Replace all bare symbols with symbol.value
let transformedCalc = calc.transform(bareSymbolSubtitutor(scope));
// Evaluate the expression to a number or return with substitutions
try {
let result = transformedCalc.evaluate(scope);
return {result, errors};
} catch (e1){
errors.push(e1);
try {
let result = simplifyWithAccessors(transformedCalc, scope).toHTML();
return {result, errors};
} catch (e2){
errors.push(e2);
return {result: transformedCalc.toHTML(), errors};
}
let context = new CompilationContext();
let result = node[fn](scope, context);
if (result instanceof ConstantNode){
return {result: result.value, errors: context.errors}
} else {
return {result: result.toString(), errors: context.errors};
}
}
function simplifyWithAccessors(calc, scope){
let noAccessorCalc = calc.transform(substituteAccessors(scope));
return math.simplify(noAccessorCalc);
}
// returns a function to replace all accessors with either their resolved value
// or a symbol to simplify with
function substituteAccessors(scope){
return function(node){
if (node.isAccessorNode){
try {
return evaluateAccessor(node, scope);
} catch (e) {
return replaceAccessorWithSymbol(node);
}
} else {
return node;
}
}
}
// Throws error if symbol is undefined in scope
function evaluateAccessor(node, scope){
let value = node.evaluate(scope);
if (value === undefined){
throw 'Undefined symbol'
}
return new math.ConstantNode(value);
}
function replaceAccessorWithSymbol(node){
let symbolNode = new math.SymbolNode(node.toString());
return symbolNode;
}
/*
function overrideSymbolNodeHTML(symbolNode){
let safeName = escape(symbolNode.name);
symbolNode.toHTML = function(){
console.log('running custom tohtml function')
return `<span class="math-symbol math-substitution-failed">${safeName}</span>`
}
return symbolNode;
}
// Escape special HTML characters
// Copied directly from math.js source to help with overriding toHTML
function escape (value) {
let text = String(value)
text = text.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
return text
}
*/

View File

@@ -1,9 +1,10 @@
import computeStat from '/imports/api/creature/computation/computeStat.js';
import applyToggles from '/imports/api/creature/computation/applyToggles.js';
import evaluateCalculation from '/imports/api/creature/computation/evaluateCalculation.js';
export default function combineStat(stat, aggregator, memo){
if (stat.type === 'attribute'){
combineAttribute(stat, aggregator);
combineAttribute(stat, aggregator, memo);
} else if (stat.type === 'skill'){
combineSkill(stat, aggregator, memo);
} else if (stat.type === 'damageMultiplier'){
@@ -28,14 +29,19 @@ function getAggregatorResult(stat, aggregator){
return result;
}
function combineAttribute(stat, aggregator){
function combineAttribute(stat, aggregator, memo){
stat.value = getAggregatorResult(stat, aggregator);
stat.baseValue = aggregator.statBaseValue;
stat.baseValueErrors = aggregator.baseValueErrors;
if (stat.attributeType === 'ability') {
stat.modifier = Math.floor((stat.value - 10) / 2);
if (stat.attributeType === 'spellSlot'){
let {result, context} = evaluateCalculation(stat.spellSlotLevelCalculation, memo);
stat.spellSlotLevelValue = result.value;
stat.spellSlotLevelErrors = context.errors;
}
stat.currentValue = stat.value - (stat.damage || 0);
if (stat.attributeType === 'ability') {
stat.modifier = Math.floor((stat.currentValue - 10) / 2);
}
stat.hide = aggregator.hasNoEffects &&
stat.baseValue === undefined ||
undefined

View File

@@ -34,10 +34,10 @@ export default function computeEffect(effect, memo){
} else if(_.contains(['advantage', 'disadvantage', 'fail'], effect.operation)){
effect.result = 1;
} else {
let {value, errors} = evaluateCalculation(effect.calculation, memo);
effect.result = value;
if (errors.length){
effect.errors = errors;
let {result, context} = evaluateCalculation(effect.calculation, memo);
effect.result = result.value;
if (context.errors.length){
effect.errors = context.errors;
}
}
effect.computationDetails.computed = true;

View File

@@ -0,0 +1,94 @@
import evaluateCalculation from '/imports/api/creature/computation/evaluateCalculation.js';
export default function computeEndStepProperty(prop, memo){
switch (prop.type){
case 'action':
case 'spell':
computeAction(prop, memo);
break;
case 'attack':
computeAction(prop, memo);
computeAttack(prop, memo);
break;
case 'savingThrow':
computeSavingThrow(prop, memo);
break;
case 'spellList':
computeSpellList(prop, memo);
break;
case 'propertySlot':
computeSlot(prop, memo);
}
}
function computeAction(prop, memo){
// Uses
let {result, context} = evaluateCalculation(prop.uses, memo);
prop.usesResult = result.value;
if (context.errors.length){
prop.usesErrors = context.errors;
} else {
delete prop.usesErrors;
}
prop.insufficientResources = undefined;
if (prop.usesUsed >= prop.usesResult){
prop.insufficientResources = true;
}
if (!prop.resources) return;
// Attributes consumed
prop.resources.attributesConsumed.forEach((attConsumed, i) => {
if (attConsumed.variableName){
let stat = memo.statsByVariableName[attConsumed.variableName];
prop.resources.attributesConsumed[i].statId = stat && stat._id;
prop.resources.attributesConsumed[i].statName = stat && stat.name;
let available = stat && stat.currentValue || 0;
prop.resources.attributesConsumed[i].available = available;
if (available < attConsumed.quantity){
prop.insufficientResources = true;
}
}
});
// Items consumed
prop.resources.itemsConsumed.forEach((itemConsumed, i) => {
let item = itemConsumed.itemId && memo.equipmentById[itemConsumed.itemId];
prop.resources.itemsConsumed[i].itemId = item && item._id;
let available = item && item.quantity || 0;
prop.resources.itemsConsumed[i].available = available;
let name = item && item.name;
if (item && item.quantity !== 1 && item.plural){
name = item.plural;
}
prop.resources.itemsConsumed[i].itemName = name;
prop.resources.itemsConsumed[i].itemIcon = item && item.icon;
prop.resources.itemsConsumed[i].itemColor = item && item.color;
if (!item || available < itemConsumed.quantity){
prop.insufficientResources = true;
}
});
}
function computePropertyField(prop, memo, fieldName, fn){
let {result, context} = evaluateCalculation(prop[fieldName], memo, fn);
prop[`${fieldName}Result`] = result.value;
if (context.errors.length){
prop[`${fieldName}Errors`] = context.errors;
} else {
delete prop[`${fieldName}Errors`];
}
}
function computeAttack(prop, memo){
computePropertyField(prop, memo, 'rollBonus');
}
function computeSavingThrow(prop, memo){
computePropertyField(prop, memo, 'dc');
}
function computeSpellList(prop, memo){
computePropertyField(prop, memo, 'maxPrepared');
}
function computeSlot(prop, memo){
computePropertyField(prop, memo, 'slotCondition');
}

View File

@@ -3,6 +3,7 @@ import computeLevels from '/imports/api/creature/computation/computeLevels.js';
import computeStat from '/imports/api/creature/computation/computeStat.js';
import computeEffect from '/imports/api/creature/computation/computeEffect.js';
import computeToggle from '/imports/api/creature/computation/computeToggle.js';
import computeEndStepProperty from '/imports/api/creature/computation/computeEndStepProperty.js';
export default function computeMemo(memo){
// Compute level
@@ -15,8 +16,12 @@ export default function computeMemo(memo){
each(memo.unassignedEffects, effect => {
computeEffect(effect, memo);
});
// Compute toggles which didn't already get computed by dependencies
forOwn(memo.togglesById, toggle => {
computeToggle(toggle, memo);
});
// Compute class levels
// Compute end step properties
forOwn(memo.endStepPropsById, prop => {
computeEndStepProperty(prop, memo);
});
}

View File

@@ -26,10 +26,10 @@ export default function computeToggle(toggle, memo){
} else if (Number.isFinite(+toggle.condition)){
toggle.toggleResult = !!+toggle.condition;
} else {
let {value, errors} = evaluateCalculation(toggle.condition, memo);
toggle.toggleResult = value;
if (errors.length){
toggle.errors = errors;
let {result, context} = evaluateCalculation(toggle.condition, memo);
toggle.toggleResult = !!result.value;
if (context.errors.length){
toggle.errors = context.errors;
}
}
toggle.computationDetails.computed = true;

View File

@@ -1,98 +1,41 @@
import computeStat from '/imports/api/creature/computation/computeStat.js';
import math from '/imports/math.js';
import { parse, CompilationContext } from '/imports/parser/parser.js';
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
import AccessorNode from '/imports/parser/parseTree/AccessorNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
/* Convert a calculation into a constant output and errors*/
export default function evaluateCalculation(string, memo){
if (!string) return string;
export default function evaluateCalculation(string, memo, fn = 'reduce'){
if (!string) return {
context: {errors: []},
result: new ConstantNode({value: string, type: 'string'}),
};
let errors = [];
// Parse the string using mathjs
// Parse the string
let calc;
try {
calc = math.parse(string);
calc = parse(string);
} catch (e) {
errors.push({
type: 'parsing',
message: e.message || e
});
return {errors, value: string};
return {
context: {errors},
result: new ConstantNode({value: string, type: 'string'}),
};
}
// Ensure all symbol nodes are defined and coputed
calc.traverse(node => {
if (node.isSymbolNode){
if (node instanceof SymbolNode || node instanceof AccessorNode){
let stat = memo.statsByVariableName[node.name];
if (stat && !stat.computationDetails.computed){
computeStat(stat, memo);
}
}
});
// Replace all symbols with their subtitution
let substitutedCalc = calc.transform(
symbolSubtitutor(memo.statsByVariableName, errors)
);
// Evaluate the expression to a number or return with substitutions
try {
let value = substitutedCalc.evaluate(memo.statsByVariableName);
if (typeof value === 'object') value = value.toString();
return {errors, value};
} catch (e){
errors.push({
type: 'evaluation',
message: e.message || e
});
let value = substitutedCalc.toString();
return {errors, value};
}
}
// returns a function to replace all symbols with either their resolved value
// or zero, keeping the errors
function symbolSubtitutor(scope, errors){
return function(node){
// mark symbol nodes that are children of function nodes to be skipped
if (node.isFunctionNode){
let fn = node.fn;
if (fn && fn.isSymbolNode){
fn.skipReplacement = true;
}
return node;
} else if (node.isSymbolNode && node.skipReplacement !== true){
//bare symbols of name "stat", should search for stat.value
let stat = scope[node.name];
if (stat){
if (stat.value === undefined){
errors.push({
type: 'subsitution',
message: `${node.name} does not have a value, set to 0`
});
return new math.ConstantNode(0);
} else {
return new math.ConstantNode(stat.value);
}
} else {
try {
return new math.ConstantNode(node.evaluate(scope));
} catch (e) {
errors.push({
type: 'subsitution',
message: `${node.name} not found, set to 0`
});
return new math.ConstantNode(0);
}
}
} else if (node.isAccessorNode){
try {
let value = node.evaluate(scope);
if (value === undefined) throw 'Not found';
return new math.ConstantNode(value);
} catch (e) {
errors.push({
type: 'subsitution',
message: `${node.toString()} not found, set to 0`
});
return new math.ConstantNode(0);
}
} else {
return node;
}
}
// Evaluate
let context = new CompilationContext();
let result = calc[fn](memo.statsByVariableName, context);
return {result, context};
}

View File

@@ -1,24 +1,35 @@
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import SimpleSchema from 'simpl-schema';
import { assertEditPermission } from '/imports/api/creature/creaturePermissions.js';
import ComputationMemo from '/imports/api/creature/computation/ComputationMemo.js';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
import computeMemo from '/imports/api/creature/computation/computeMemo.js';
import getActiveProperties from '/imports/api/creature/getActiveProperties.js';
import writeAlteredProperties from '/imports/api/creature/computation/writeAlteredProperties.js';
import writeCreatureVariables from '/imports/api/creature/computation/writeCreatureVariables.js';
import { recomputeDamageMultipliersById } from '/imports/api/creature/damageMultiplierDenormalise/recomputeDamageMultipliers.js'
import { recomputeDamageMultipliersById } from '/imports/api/creature/denormalise/recomputeDamageMultipliers.js';
import recomputeInactiveProperties from '/imports/api/creature/denormalise/recomputeInactiveProperties.js';
import recomputeSlotFullness from '/imports/api/creature/denormalise/recomputeSlotFullness.js';
import Creatures from '/imports/api/creature/Creatures.js';
export const recomputeCreature = new ValidatedMethod({
name: 'Creatures.methods.recomputeCreature',
name: 'creatures.recomputeCreature',
validate: new SimpleSchema({
charId: { type: String }
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({charId}) {
let creature = Creatures.findOne(charId);
// Permission
assertEditPermission(charId, this.userId);
assertEditPermission(creature, this.userId);
// Work, call this direcly if you are already in a method that has checked
// for permission to edit a given character
recomputeCreatureById(charId);
@@ -33,8 +44,21 @@ const calculationPropertyTypes = [
'proficiency',
'classLevel',
'toggle',
'item',
// End step types
'action',
'attack',
'savingThrow',
'spellList',
'spell',
'propertySlot',
];
export function recomputeCreatureById(creatureId){
let creature = Creatures.findOne(creatureId);
recomputeCreatureByDoc(creature);
}
/**
* This function is the heart of DiceCloud. It recomputes a creature's stats,
* distilling down effects and proficiencies into the final stats that make up
@@ -71,17 +95,44 @@ const calculationPropertyTypes = [
* - Mark the stat as computed
* - Write the computed results back to the database
*/
export function recomputeCreatureById(creatureId){
let props = getActiveProperties({
ancestorId: creatureId,
filter: {type: {$in: calculationPropertyTypes}},
includeUntoggled: true,
// TODO filter out expensive fields, particularly icon field
});
let computationMemo = new ComputationMemo(props);
export function recomputeCreatureByDoc(creature){
const creatureId = creature._id;
// find all toggles that have conditions, even if they are inactive
let toggleIds = CreatureProperties.find({
'ancestors.id': creatureId,
type: 'toggle',
removed: {$ne: true},
condition: { $exists: true },
}, {
fields: {_id: 1},
}).map(t => t._id);
// Find all the active properties
let props = CreatureProperties.find({
'ancestors.id': creatureId,
removed: {$ne: true},
type: {$in: calculationPropertyTypes},
$or: [
{inactive: {$ne: true}},
// But also the inactive computed toggles and their decendants
{'ancestors.id': {$in: toggleIds}},
{_id: {$in: toggleIds}},
]
}, {
fields: { // Filter out potentially large fields
icon: 0,
summary: 0,
description: 0,
},
sort: {
order: 1,
}
}).fetch();
let computationMemo = new ComputationMemo(props, creature);
recomputeInactiveProperties(creatureId);
computeMemo(computationMemo);
writeAlteredProperties(computationMemo);
writeCreatureVariables(computationMemo, creatureId);
recomputeDamageMultipliersById(creatureId);
recomputeSlotFullness(creatureId);
return computationMemo;
}

View File

@@ -1,24 +0,0 @@
import math from '/imports/math.js';
export default function bareSymbolSubtitutor(scope){
return function(node, path){
if (!scope) return node;
if (node.isFunctionNode){
let fn = node.fn;
if (fn && fn.isSymbolNode){
fn.skipReplacement = true;
}
return node;
} else if (
node.isSymbolNode &&
path !== 'object' &&
node.skipReplacement !== true
) {
let stat = scope[node.name];
if (!stat) return node;
return new math.ConstantNode(stat.value);
} else {
return node;
}
}
}

View File

@@ -1,16 +1,31 @@
import { Meteor } from 'meteor/meteor'
import { isEqual, forOwn } from 'lodash';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
// Schemas
// Calculated props
import { ComputedOnlySkillSchema } from '/imports/api/properties/Skills.js';
import { ComputedOnlyAttributeSchema } from '/imports/api/properties/Attributes.js';
import { ComputedOnlyEffectSchema } from '/imports/api/properties/Effects.js';
import { ComputedOnlyToggleSchema } from '/imports/api/properties/Toggles.js';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
// End step props
import { ComputedOnlyActionSchema } from '/imports/api/properties/Actions.js';
import { ComputedOnlyAttackSchema } from '/imports/api/properties/Attacks.js';
import { ComputedOnlySavingThrowSchema } from '/imports/api/properties/SavingThrows.js';
import { ComputedOnlySpellListSchema } from '/imports/api/properties/SpellLists.js';
import { ComputedOnlySpellSchema } from '/imports/api/properties/Spells.js';
import { ComputedOnlySlotSchema } from '/imports/api/properties/Slots.js';
const schemasByType = {
'skill': ComputedOnlySkillSchema,
'attribute': ComputedOnlyAttributeSchema,
'effect': ComputedOnlyEffectSchema,
'toggle': ComputedOnlyToggleSchema,
'action': ComputedOnlyActionSchema,
'attack': ComputedOnlyAttackSchema,
'savingThrow': ComputedOnlySavingThrowSchema,
'spellList': ComputedOnlySpellListSchema,
'spell': ComputedOnlySpellSchema,
'propertySlot': ComputedOnlySlotSchema,
};
export default function writeAlteredProperties(memo){

View File

@@ -1,5 +1,6 @@
import { pick, forOwn } from 'lodash';
import Creatures from '/imports/api/creature/Creatures.js';
import VERSION from '/imports/constants/VERSION.js';
export default function writeCreatureVariables(memo, creatureId) {
const fields = [
@@ -31,5 +32,8 @@ export default function writeCreatureVariables(memo, creatureId) {
let condensedStat = pick(stat, fields);
memo.creatureVariables[variableName] = condensedStat;
});
Creatures.update(creatureId, {$set: {variables: memo.creatureVariables}});
Creatures.update(creatureId, {$set: {
variables: memo.creatureVariables,
computeVersion: VERSION,
}});
}

View File

@@ -24,6 +24,6 @@ export function assertEditPermission(creature, userId) {
}
export function assertViewPermission(creature, userId) {
creature = getCreature(creature, {owner: 1, writers: 1, public: 1});
creature = getCreature(creature, {owner: 1, readers:1, writers: 1, public: 1});
viewPermission(creature, userId);
}

View File

@@ -0,0 +1,7 @@
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
import getClosestPropertyAncestorCreature from '/imports/api/creature/creatureProperties/getClosestPropertyAncestorCreature.js';
export default function assertPropertyEditPermission(prop, userId){
let creature = getClosestPropertyAncestorCreature(prop);
assertEditPermission(creature, userId);
}

View File

@@ -0,0 +1,7 @@
import Creatures from '/imports/api/creature/Creatures.js';
import getClosestPropertyAncestorCreatureId from '/imports/api/creature/creatureProperties/getClosestPropertyAncestorCreatureId.js';
export default function getClosestPropertyAncestorCreature(prop){
let creatureId = getClosestPropertyAncestorCreatureId(prop);
return Creatures.findOne(creatureId);
}

View File

@@ -0,0 +1,13 @@
export default function getClosestPropertyAncestorCreatureId(prop){
if (!prop.ancestors) throw 'Property has no ancestors';
let creatureId;
// Find the last ancestor in the creature collection
for (let i = prop.ancestors.length - 1; i >= 0; i--){
if (prop.ancestors[i].collection === 'creatures'){
creatureId = prop.ancestors[i].id;
break;
}
}
if (!creatureId) throw 'This property has no creature ancestors';
return creatureId;
}

View File

@@ -0,0 +1,64 @@
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
import { organizeDoc } from '/imports/api/parenting/organizeMethods.js';
import getClosestPropertyAncestorCreature from '/imports/api/creature/creatureProperties/getClosestPropertyAncestorCreature.js';
import INVENTORY_TAGS from '/imports/constants/INVENTORY_TAGS.js';
function getParentRefByTag(creatureId, tag){
let prop = CreatureProperties.findOne({
'ancestors.id': creatureId,
removed: {$ne: true},
inactive: {$ne: true},
tags: tag,
}, {
sort: {order: 1},
});
if (prop){
return {id: prop._id, collection: 'creatureProperties'};
} else {
return {id: creatureId, collection: 'creatures'};
}
}
// Equipping or unequipping an item will also change its parent
const equipItem = new ValidatedMethod({
name: 'creatureProperties.equip',
validate({_id, equipped}){
if (!_id) throw new Meteor.Error('No _id', '_id is required');
if (equipped !== true && equipped !== false) {
throw new Meteor.Error('No equipped', 'equipped is required to be true or false');
}
},
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, equipped}) {
let item = CreatureProperties.findOne(_id);
if (item.type !== 'item') throw new Meteor.Error('wrong type',
'Equip and unequip can only be performed on items');
let creature = getClosestPropertyAncestorCreature(item);
assertEditPermission(creature, this.userId);
CreatureProperties.update(_id, {
$set: {equipped},
}, {
selector: {type: 'item'},
});
let tag = equipped ? INVENTORY_TAGS.equipment : INVENTORY_TAGS.carried;
let parentRef = getParentRefByTag(creature._id, tag);
// organizeDoc handles recompuation
organizeDoc.call({
docRef: {
id: _id,
collection: 'creatureProperties',
},
parentRef,
order: Number.MAX_SAFE_INTEGER,
});
},
});
export { equipItem, getParentRefByTag }

View File

@@ -1,4 +1,5 @@
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import SimpleSchema from 'simpl-schema';
import { assertEditPermission } from '/imports/api/creature/creaturePermissions.js';
import Creatures from '/imports/api/creature/Creatures.js';
@@ -6,12 +7,18 @@ import getActiveProperties from '/imports/api/creature/getActiveProperties.js';
export const recomputeDamageMultipliers = new ValidatedMethod({
name: 'Creatures.methods.recomputeDamageMultipliers',
name: 'creatures.recomputeDamageMultipliers',
validate: new SimpleSchema({
creatureId: { type: String }
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({creatureId}) {
// Permission
assertEditPermission(creatureId, this.userId);

View File

@@ -0,0 +1,59 @@
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
export default function recomputeInactiveProperties(ancestorId){
let disabledFilter = {
'ancestors.id': ancestorId,
$or: [
{disabled: true}, // Everything can be disabled
{type: 'buff', applied: false}, // Buffs can be applied
{type: 'item', equipped: {$ne: true}},
{type: 'toggle', toggleResult: false},
{type: 'spell', prepared: {$ne: true}, alwaysPrepared: {$ne: true}},
],
};
let disabledIds = CreatureProperties.find(disabledFilter, {
fields: {_id: 1},
}).map(prop => prop._id);
// Deactivate relevant properties
// Inactive properties
CreatureProperties.update({
'ancestors.id': ancestorId,
'_id': {$in: disabledIds},
$or: [{inactive: {$ne: true}}, {deactivatedByAncestor: true}],
}, {
$set: {inactive: true},
$unset: {deactivatedByAncestor: 1},
}, {
multi: true,
selector: {type: 'any'},
});
// Decendants of inactive properties
CreatureProperties.update({
'ancestors.id': {$eq: ancestorId, $in: disabledIds},
$or: [{inactive: {$ne: true}}, {deactivatedByAncestor: {$ne: true}}],
}, {
$set: {
inactive: true,
deactivatedByAncestor: true,
},
}, {
multi: true,
selector: {type: 'any'},
});
// Remove inactive from all the properties that are inactive but shouldn't be
CreatureProperties.update({
'ancestors.id': {$eq: ancestorId, $nin: disabledIds},
'_id': {$nin: disabledIds},
$or: [{inactive: true}, {deactivatedByAncestor: true}],
}, {
$unset: {
inactive: 1,
deactivatedByAncestor: 1,
},
}, {
multi: true,
selector: {type: 'any'},
});
}

View File

@@ -0,0 +1,39 @@
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
// n + 1 database queries + n potential updates for n slots. Could be sped up.
export default function recomputeSlotFullness(ancestorId){
CreatureProperties.find({
'ancestors.id': ancestorId,
type: 'propertySlot',
}).forEach(slot => {
let children = CreatureProperties.find({
'parent.id': slot._id,
removed: {$ne: true},
}, {
fields: {
slotQuantityFilled: 1,
type: 1
}
}).fetch();
let totalFilled = 0;
children.forEach(child => {
if (child.type === 'slotFiller'){
totalFilled += child.slotQuantityFilled;
} else {
totalFilled++;
}
});
let spaceLeft;
if (slot.quantityExpected === 0){
spaceLeft = null;
} else {
spaceLeft = slot.quantityExpected - totalFilled;
}
if (slot.totalFilled !== totalFilled || slot.spaceLeft !== spaceLeft){
CreatureProperties.update(slot._id, {
$set: {totalFilled, spaceLeft},
}, {
selector: {type: 'propertySlot'}
});
}
});
}

View File

@@ -0,0 +1,197 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
import { assertEditPermission } from '/imports/api/creature/creaturePermissions.js';
import Creatures from '/imports/api/creature/Creatures.js';
import { recomputeCreatureById } from '/imports/api/creature/computation/recomputeCreature.js';
let Experiences = new Mongo.Collection('experiences');
let ExperienceSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
// The amount of XP this experience gives
xp: {
type: SimpleSchema.Integer,
optional: true,
min: 0,
},
// Setting levels instead of value grants whole levels
levels: {
type: SimpleSchema.Integer,
optional: true,
min: 0,
index: 1,
},
// The real-world date that it occured, usually sorted by date
date: {
type: Date,
autoValue: function() {
// If the date isn't set, set it to now
if (!this.isSet) {
return new Date();
}
},
index: 1,
},
creatureId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
index: 1,
},
});
Experiences.attachSchema(ExperienceSchema);
const insertExperienceForCreature = function({experience, creatureId, userId}){
assertEditPermission(creatureId, userId);
if (experience.xp){
Creatures.update(creatureId, {$inc: {
'denormalizedStats.xp': experience.xp
}});
}
if (experience.levels) {
Creatures.update(creatureId, {$inc: {
'denormalizedStats.milestoneLevels': experience.levels
}});
}
experience.creatureId = creatureId;
let id = Experiences.insert(experience);
recomputeCreatureById(creatureId);
return id;
};
const insertExperience = new ValidatedMethod({
name: 'experiences.insert',
validate: new SimpleSchema({
experience: {
type: ExperienceSchema.omit('creatureId'),
},
creatureIds: {
type: Array,
max: 12,
},
'creatureIds.$': {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({experience, creatureIds}) {
let userId = this.userId;
if (!userId) {
throw new Meteor.Error('Experiences.methods.insert.denied',
'You need to be logged in to insert an experience');
}
let tier = getUserTier(this.userId);
if (!tier.paidBenefits){
throw new Meteor.Error('Experiences.methods.insert.denied',
`The ${tier.name} tier does not allow you to grant experience`);
}
let insertedIds = [];
creatureIds.forEach(creatureId => {
let id = insertExperienceForCreature({experience, creatureId, userId});
insertedIds.push(id);
});
return insertedIds;
},
});
const removeExperience = new ValidatedMethod({
name: 'experiences.remove',
validate: new SimpleSchema({
experienceId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({experienceId}) {
let userId = this.userId;
if (!userId) {
throw new Meteor.Error('Experiences.methods.remove.denied',
'You need to be logged in to remove an experience');
}
let tier = getUserTier(this.userId);
if (!tier.paidBenefits){
throw new Meteor.Error('Experiences.methods.remove.denied',
`The ${tier.name} tier does not allow you to remove an experience`);
}
let experience = Experiences.findOne(experienceId);
if (!experience) return;
let creatureId = experience.creatureId
assertEditPermission(creatureId, userId);
if (experience.xp){
Creatures.update(creatureId, {$inc: {
'denormalizedStats.xp': -experience.xp
}});
}
if (experience.levels) {
Creatures.update(creatureId, {$inc: {
'denormalizedStats.milestoneLevels': -experience.levels
}});
}
experience.creatureId = creatureId;
let numRemoved = Experiences.remove(experienceId);
recomputeCreatureById(creatureId);
return numRemoved;
},
});
const recomputeExperiences = new ValidatedMethod({
name: 'experiences.recompute',
validate: new SimpleSchema({
creatureId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({creatureId}) {
let userId = this.userId;
if (!userId) {
throw new Meteor.Error('Experiences.methods.recompute.denied',
'You need to be logged in to recompute a creature\'s experiences');
}
let tier = getUserTier(this.userId);
if (!tier.paidBenefits){
throw new Meteor.Error('Experiences.methods.recompute.denied',
`The ${tier.name} tier does not allow you to recompute a creature's experiences`);
}
assertEditPermission(creatureId, userId);
let xp = 0;
let milestoneLevels = 0;
Experiences.find({
creatureId
}, {
fields: {xp: 1, levels: 1}
}).forEach(experience => {
xp += experience.xp || 0;
milestoneLevels += experience.levels || 0;
});
Creatures.update(creatureId, {$set: {
'denormalizedStats.xp': xp,
'denormalizedStats.milestoneLevels': milestoneLevels
}});
recomputeCreatureById(creatureId);
},
});
export default Experiences;
export { ExperienceSchema, insertExperience, removeExperience, recomputeExperiences };

View File

@@ -4,17 +4,30 @@ import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
export default function getActiveProperties({
ancestorId,
filter = {},
options,
includeUntoggled = false
options = {sort: {order: 1}},
includeUntoggled = false,
includeUnprepared = false,
includeUnequipped = false,
excludeAncestors,
}){
filter = getActivePropertyFilter({ancestorId, filter, includeUntoggled});
filter = getActivePropertyFilter({
ancestorId,
filter,
includeUntoggled,
includeUnprepared,
includeUnequipped,
excludeAncestors,
});
return CreatureProperties.find(filter, options).fetch();
}
export function getActivePropertyFilter({
ancestorId,
filter = {},
includeUntoggled = false
includeUntoggled = false,
includeUnprepared = false,
includeUnequipped = false,
excludeAncestors = [],
}){
if (!ancestorId){
throw 'Ancestor Id is required to get active properties'
@@ -24,13 +37,22 @@ export function getActivePropertyFilter({
'ancestors.id': ancestorId,
$or: [
{disabled: true}, // Everything can be disabled
{equipped: false}, // Items can be equipped
{applied: false}, // Buffs can be applied
],
};
if (!includeUnequipped){
disabledAncestorsFilter.$or.push({type: 'item', equipped: {$ne: true}});
}
if (!includeUntoggled){
disabledAncestorsFilter.$or.push({toggleResult: false});
}
if (!includeUnprepared){
disabledAncestorsFilter.$or.push({
type: 'spell',
prepared: {$ne: true},
alwaysPrepared: {$ne: true}
});
}
let disabledAncestorIds = CreatureProperties.find(disabledAncestorsFilter, {
fields: {_id: 1},
}).map(prop => prop._id);
@@ -47,9 +69,12 @@ export function getActivePropertyFilter({
// Get all the properties that are decendents of the ancestor of interest but
// aren't from the excluded decendents
if (filter['ancestors.id'] && Meteor.isClient){
console.warn('Filtering on ancestor id is ignored')
}
filter['ancestors.id'] = {
$eq: ancestorId,
$nin: disabledAncestorIds,
$nin: disabledAncestorIds.concat(excludeAncestors),
};
// Get properties that aren't removed
filter.removed = {$ne: true};

View File

@@ -1,7 +1,7 @@
import SimpleSchema from 'simpl-schema';
let ExperienceSchema = new SimpleSchema({
name: {
title: {
type: String,
optional: true,
},
@@ -10,11 +10,6 @@ let ExperienceSchema = new SimpleSchema({
type: String,
optional: true,
},
// The amount of XP this experience gives
value: {
type: SimpleSchema.Integer,
optional: true,
},
// The real-world date that it occured
date: {
type: Date,
@@ -30,6 +25,20 @@ let ExperienceSchema = new SimpleSchema({
type: String,
optional: true,
},
// Tags to better find this entry later
tags: {
type: Array,
defaultValue: [],
},
'tags.$': {
type: String,
},
// ID of the journal this entry belongs to
journalId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
index: 1,
}
});
export { ExperienceSchema };

View File

@@ -0,0 +1,176 @@
import SimpleSchema from 'simpl-schema';
import Creatures from '/imports/api/creature/Creatures.js';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import {assertEditPermission} from '/imports/api/creature/creaturePermissions.js';
import { parse, CompilationContext } from '/imports/parser/parser.js';
const PER_CREATURE_LOG_LIMIT = 100;
if (Meteor.isServer){
var sendWebhookAsCreature = require('/imports/server/discord/sendWebhook.js').sendWebhookAsCreature;
}
let CreatureLogs = new Mongo.Collection('creatureLogs');
let CreatureLogSchema = new SimpleSchema({
text: {
type: String,
},
type: {
type: String,
allowedValues: ['roll', 'change', 'damage', 'info'],
defaultValue: 'info',
},
// The real-world date that it occured, usually sorted by date
date: {
type: Date,
autoValue: function() {
// If the date isn't set, set it to now
if (!this.isSet) {
return new Date();
}
},
index: 1,
},
creatureId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
index: 1,
},
});
CreatureLogs.attachSchema(CreatureLogSchema);
function removeOldLogs(creatureId){
// Find the first log that is over the limit
let firstExpiredLog = CreatureLogs.find({
creatureId
}, {
sort: {date: -1},
skip: PER_CREATURE_LOG_LIMIT,
});
// Remove all logs older than the one over the limit
CreatureLogs.remove({
creatureId,
date: {$lte: firstExpiredLog.date},
});
}
function logWebhook({log, creature}){
if (Meteor.isServer){
sendWebhookAsCreature({
creature,
content: log.text,
});
}
}
const insertCreatureLog = new ValidatedMethod({
name: 'creatureLogs.methods.insertCreatureLog',
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
validate: new SimpleSchema({
log: CreatureLogSchema.omit('type', 'date'),
}).validator(),
run({log}){
const creatureId = log.creatureId;
const creature = Creatures.findOne(creatureId, {fields: {
readers: 1,
writers: 1,
owner: 1,
'settings.discordWebhook': 1,
name: 1,
avatarPicture: 1,
}});
assertEditPermission(creature, this.userId);
// Build the new log
if (typeof log === 'string'){
log = {text: log};
}
log.date = new Date();
// Insert it
let id = CreatureLogs.insert(log);
if (Meteor.isServer){
this.unblock();
removeOldLogs(creatureId);
logWebhook({log, creature});
}
return id;
},
});
function equalIgnoringWhitespace(a, b){
if (typeof a !== 'string' || typeof b !== 'string') return a === b;
return a.replace(/\s/g,'') === b.replace(/\s/g, '');
}
const logRoll = new ValidatedMethod({
name: 'creatureLogs.methods.logForCreature',
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
validate: new SimpleSchema({
roll: {
type: String,
},
creatureId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
run({roll, creatureId}){
const creature = Creatures.findOne(creatureId, {fields: {
variables: 1,
readers: 1,
writers: 1,
owner: 1,
'settings.discordWebhook': 1,
name: 1,
avatarPicture: 1,
}});
assertEditPermission(creature, this.userId);
let parsedResult = parse(roll);
let logText;
if (parsedResult === null) {
logText = 'Unexpected end of input';
}
else try {
logText = [];
let rollContext = new CompilationContext();
let compiled = parsedResult.compile(creature.variables, rollContext);
let compiledString = compiled.toString();
if (!equalIgnoringWhitespace(compiledString, roll)) logText.push(roll);
logText.push(compiledString);
let rolled = compiled.roll(creature.variables, rollContext);
let rolledString = rolled.toString();
if (rolledString !== compiledString) logText.push(rolled.toString());
let result = rolled.reduce(creature.variables, rollContext);
let resultString = result.toString();
if (resultString !== rolledString) logText.push(resultString);
logText = logText.join('\n\n');
} catch (e){
logText = 'Calculation error';
}
const log = {
text: logText,
creatureId,
date: new Date(),
};
let id = CreatureLogs.insert(log);
if (Meteor.isServer){
this.unblock();
removeOldLogs(creatureId);
logWebhook({log, creature});
}
return id;
},
});
export default CreatureLogs;
export { CreatureLogSchema, insertCreatureLog, logRoll};

View File

@@ -1,20 +1,31 @@
import SimpleSchema from 'simpl-schema';
import Creatures from '/imports/api/creature/Creatures.js';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js'
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertOwnership } from '/imports/api/creature/creaturePermissions.js';
import Creatures from '/imports/api/creature/Creatures.js';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
import CreatureLogs from '/imports/api/creature/log/CreatureLogs.js';
import Experiences from '/imports/api/creature/experience/Experiences.js';
function removeRelatedDocuments(charId){
CreatureProperties.remove({'ancestors.id': charId});
};
function removeRelatedDocuments(creatureId){
CreatureProperties.remove({'ancestors.id': creatureId});
CreatureLogs.remove({creatureId});
Experiences.remove({creatureId});
}
const removeCreature = new ValidatedMethod({
name: "Creatures.methods.removeCreature", // DDP method name
name: 'Creatures.methods.removeCreature', // DDP method name
validate: new SimpleSchema({
charId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({charId}) {
assertOwnership(charId, this.userId)
Creatures.remove(charId);

View File

@@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import Creatures from '/imports/api/creature/Creatures.js';
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
import getActiveProperties, { getActivePropertyFilter } from '/imports/api/creature/getActiveProperties.js';
@@ -18,6 +19,11 @@ const restCreature = new ValidatedMethod({
allowedValues: ['shortRest', 'longRest'],
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({creatureId, restType}) {
let creature = Creatures.findOne(creatureId, {
fields: {

View File

@@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertAdmin } from '/imports/api/sharing/sharingPermissions.js';
let Icons = new Mongo.Collection('icons');
@@ -48,7 +49,7 @@ Icons.attachSchema(iconsSchema);
// This method does not validate icons against the schema, use wisely;
const writeIcons = new ValidatedMethod({
name: 'icons.methods.write',
name: 'icons.write',
validate: null,
run(icons){
assertAdmin(this.userId);
@@ -60,7 +61,7 @@ const writeIcons = new ValidatedMethod({
});
const findIcons = new ValidatedMethod({
name: 'icons.methods.find',
name: 'icons.find',
validate: new SimpleSchema({
search: {
type: String,
@@ -68,6 +69,11 @@ const findIcons = new ValidatedMethod({
optional: true,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 20,
timeInterval: 10000,
},
run({search}){
if (!search) return [];
if (!Meteor.isServer) return;

View File

@@ -1,4 +1,5 @@
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';
@@ -34,7 +35,7 @@ Libraries.attachSchema(LibrarySchema);
export default Libraries;
const insertLibrary = new ValidatedMethod({
name: 'Libraries.methods.insert',
name: 'libraries.insert',
mixins: [
simpleSchemaMixin,
],
@@ -55,7 +56,7 @@ const insertLibrary = new ValidatedMethod({
});
const updateLibraryName = new ValidatedMethod({
name: 'Libraries.methods.updateName',
name: 'libraries.updateName',
validate: new SimpleSchema({
_id: {
type: String,
@@ -65,6 +66,11 @@ const updateLibraryName = new ValidatedMethod({
type: String,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, name}){
let library = Libraries.findOne(_id);
assertEditPermission(library, this.userId);
@@ -73,7 +79,7 @@ const updateLibraryName = new ValidatedMethod({
});
const setLibraryDefault = new ValidatedMethod({
name: 'Libraries.methods.makeLibraryDefault',
name: 'libraries.makeLibraryDefault',
validate: new SimpleSchema({
_id: {
type: String,
@@ -83,6 +89,11 @@ const setLibraryDefault = new ValidatedMethod({
type: Boolean,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, isDefault}) {
if (!Meteor.users.isAdmin()){
throw new Meteor.Error('Permission denied', 'User must be admin to set libraries as default');
@@ -92,13 +103,18 @@ const setLibraryDefault = new ValidatedMethod({
});
const removeLibrary = new ValidatedMethod({
name: 'Libraries.methods.remove',
name: 'libraries.remove',
validate: new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.id
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}){
let library = Libraries.findOne(_id);
assertOwnership(library, this.userId);

View File

@@ -1,6 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import SimpleSchema from 'simpl-schema';
import ColorSchema from '/imports/api/properties/subSchemas/ColorSchema.js';
import ChildSchema from '/imports/api/parenting/ChildSchema.js';
@@ -56,8 +57,13 @@ function assertNodeEditPermission(node, userId){
}
const insertNode = new ValidatedMethod({
name: 'LibraryNodes.methods.insert',
name: 'libraryNodes.insert',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run(libraryNode) {
delete libraryNode._id;
assertNodeEditPermission(libraryNode, this.userId);
@@ -66,13 +72,18 @@ const insertNode = new ValidatedMethod({
});
const duplicateNode = new ValidatedMethod({
name: 'LibraryNodes.methods.duplicate',
name: 'libraryNodes.duplicate',
validate: new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
}
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}) {
let libraryNode = LibraryNodes.findOne(_id);
assertNodeEditPermission(libraryNode, this.userId);
@@ -82,7 +93,7 @@ const duplicateNode = new ValidatedMethod({
})
const updateLibraryNode = new ValidatedMethod({
name: 'LibraryNodes.methods.update',
name: 'libraryNodes.update',
validate({_id, path}){
if (!_id) return false;
// We cannot change these fields with a simple update
@@ -94,6 +105,11 @@ const updateLibraryNode = new ValidatedMethod({
return false;
}
},
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, value}) {
let node = LibraryNodes.findOne(_id);
assertNodeEditPermission(node, this.userId);
@@ -112,8 +128,13 @@ const updateLibraryNode = new ValidatedMethod({
});
const pushToLibraryNode = new ValidatedMethod({
name: 'LibraryNodes.methods.push',
name: 'libraryNodes.push',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, value}){
let node = LibraryNodes.findOne(_id);
assertNodeEditPermission(node, this.userId);
@@ -126,8 +147,13 @@ const pushToLibraryNode = new ValidatedMethod({
});
const pullFromLibraryNode = new ValidatedMethod({
name: 'LibraryNodes.methods.pull',
name: 'libraryNodes.pull',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id, path, itemId}){
let node = LibraryNodes.findOne(_id);
assertNodeEditPermission(node, this.userId);
@@ -141,10 +167,15 @@ const pullFromLibraryNode = new ValidatedMethod({
});
const softRemoveLibraryNode = new ValidatedMethod({
name: 'LibraryNodes.methods.softRemove',
name: 'libraryNodes.softRemove',
validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}){
let node = LibraryNodes.findOne(_id);
assertNodeEditPermission(node, this.userId);

View File

@@ -1,6 +1,7 @@
import SimpleSchema from 'simpl-schema';
import { union } from 'lodash';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { updateParent } from '/imports/api/parenting/parenting.js';
import { reorderDocs, safeUpdateDocOrder } from '/imports/api/parenting/order.js';
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
@@ -10,7 +11,7 @@ import getCollectionByName from '/imports/api/parenting/getCollectionByName.js';
import { recomputeCreatureById } from '/imports/api/creature/computation/recomputeCreature.js';
const organizeDoc = new ValidatedMethod({
name: 'organize.methods.organizeDoc',
name: 'organize.organizeDoc',
validate: new SimpleSchema({
docRef: RefSchema,
parentRef: RefSchema,
@@ -19,6 +20,11 @@ const organizeDoc = new ValidatedMethod({
// Should end in 0.5 to place it reliably between two existing documents
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({docRef, parentRef, order}) {
let doc = fetchDocByRef(docRef);
let collection = getCollectionByName(docRef.collection);
@@ -54,7 +60,7 @@ const organizeDoc = new ValidatedMethod({
});
const reorderDoc = new ValidatedMethod({
name: 'organize.methods.reorderDoc',
name: 'organize.reorderDoc',
validate: new SimpleSchema({
docRef: RefSchema,
order: {
@@ -62,6 +68,11 @@ const reorderDoc = new ValidatedMethod({
// Should end in 0.5 to place it reliably between two existing documents
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({docRef, order}) {
let doc = fetchDocByRef(docRef);
assertDocEditPermission(doc, this.userId);

View File

@@ -130,7 +130,7 @@ export function renewDocIds({docArray, collectionMap}){
const remapReference = ref => {
if (idMap[ref.id]){
ref.id = idMap[ref.id];
ref.collection = collectionMap[ref.collection] || ref.collection;
ref.collection = collectionMap && collectionMap[ref.collection] || ref.collection;
}
}
docArray.forEach(doc => {
@@ -154,6 +154,14 @@ export function updateParent({docRef, parentRef}){
// Get the parent and its ancestry
let {parentDoc, parent, ancestors} = getAncestry({parentRef});
// Check that the doc isn't its own ancestor
ancestors.forEach(ancestor => {
if (docRef.id === ancestor.id){
throw new Meteor.Error('invalid parenting',
'A doc can\'t be its own ancestor')
}
});
// If the doc and its parent are in the same collection, apply the allowed
// parent rules based on type
if (docRef.collection === parentRef.collection){
@@ -204,17 +212,11 @@ export function getName(doc){
}
}
export function nodesToTree({collection, ancestorId, filter, options}){
// Store a dict of all the nodes
export function nodeArrayToTree(nodes){
// Store a dict and list of all the nodes
let nodeIndex = {};
let nodeList = [];
if (!options) options = {};
options.sort = {order: 1};
collection.find({
'ancestors.id': ancestorId,
removed: {$ne: true},
...filter,
}, options).forEach( node => {
nodes.forEach( node => {
let treeNode = {
node: node,
children: [],
@@ -238,3 +240,12 @@ export function nodesToTree({collection, ancestorId, filter, options}){
});
return forest;
}
export function nodesToTree({collection, ancestorId, filter = {}, options = {}}){
if (!('ancestors.id' in filter)) filter['ancestors.id'] = ancestorId;
if (!('removed' in filter)) filter['removed'] = {$ne: true};
if (!options.sort) options.sort = {order: 1};
if (!('order' in options.sort)) options.sort.order = 1;
let nodes = collection.find(filter, options);
return nodeArrayToTree(nodes);
}

View File

@@ -41,16 +41,21 @@ const restoreError = function(){
};
export function restore({_id, collection}){
collection = getCollectionByName(collection);
if (typeof collection === 'string') {
collection = getCollectionByName(collection);
}
let numUpdated = collection.update({
_id,
removedWith: {$exists: false}
}, { $unset: {
removed: 1,
removedAt: 1,
}});
}}, {
selector: {type: 'any'},
},);
if (numUpdated === 0) restoreError();
updateDescendants({
collection,
ancestorId: _id,
filter: {
removedWith: _id,

View File

@@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import ResourcesSchema from '/imports/api/properties/subSchemas/ResourcesSchema.js'
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import { storedIconsSchema } from '/imports/api/icons/Icons.js'
/*
* Actions are things a character can do
@@ -12,6 +13,10 @@ let ActionSchema = new SimpleSchema({
type: String,
optional: true,
},
summary: {
type: String,
optional: true,
},
description: {
type: String,
optional: true,
@@ -40,13 +45,60 @@ let ActionSchema = new SimpleSchema({
'tags.$': {
type: String,
},
// Duplicate the ResourceSchema here so we can extend it elegantly.
resources: {
type: ResourcesSchema,
type: Object,
defaultValue: {},
},
'resources.itemsConsumed': {
type: Array,
defaultValue: [],
},
'resources.itemsConsumed.$': {
type: Object,
},
'resources.itemsConsumed.$._id': {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue(){
if (!this.isSet) return Random.id();
}
},
'resources.itemsConsumed.$.tag': {
type: String,
optional: true,
},
'resources.itemsConsumed.$.quantity': {
type: Number,
defaultValue: 1,
},
'resources.itemsConsumed.$.itemId': {
type: String,
optional: true,
},
'resources.attributesConsumed': {
type: Array,
defaultValue: [],
},
'resources.attributesConsumed.$': {
type: Object,
},
'resources.attributesConsumed.$._id': {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue(){
if (!this.isSet) return Random.id();
}
},
'resources.attributesConsumed.$.variableName': {
type: String,
optional: true,
},
'resources.attributesConsumed.$.quantity': {
type: Number,
defaultValue: 1,
},
// Calculation of how many times this action can be used
// Only set if this action tracks its own uses, rather than adjusting
// resources
uses: {
type: String,
optional: true,
@@ -64,4 +116,69 @@ let ActionSchema = new SimpleSchema({
},
});
export { ActionSchema };
const ComputedOnlyActionSchema = new SimpleSchema({
usesResult: {
type: SimpleSchema.Integer,
optional: true,
},
usesErrors: {
type: Array,
optional: true,
},
'usesErrors.$':{
type: ErrorSchema,
},
resources: Object,
'resources.itemsConsumed': Array,
'resources.itemsConsumed.$': Object,
'resources.itemsConsumed.$.available': {
type: Number,
optional: true,
},
// This appears both in the computed and uncomputed schema because it can be
// set by both a computation or a form
'resources.itemsConsumed.$.itemId': {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
'resources.itemsConsumed.$.itemName': {
type: String,
optional: true,
},
'resources.itemsConsumed.$.itemIcon': {
type: storedIconsSchema,
optional: true,
},
'resources.itemsConsumed.$.itemColor': {
type: String,
optional: true,
},
'resources.attributesConsumed': Array,
'resources.attributesConsumed.$': Object,
'resources.attributesConsumed.$.available': {
type: Number,
optional: true,
},
'resources.attributesConsumed.$.statId': {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
'resources.attributesConsumed.$.statName': {
type: String,
optional: true,
},
// True if the uses left is zero, or any item or attribute consumed is
// insufficient
insufficientResources: {
type: Boolean,
optional: true,
},
});
const ComputedActionSchema = new SimpleSchema()
.extend(ActionSchema)
.extend(ComputedOnlyActionSchema);
export { ActionSchema, ComputedOnlyActionSchema, ComputedActionSchema};

View File

@@ -2,6 +2,7 @@ import SimpleSchema from 'simpl-schema';
const AdjustmentSchema = new SimpleSchema({
// The roll that determines how much to change the attribute
// This can be simplified, but should only compute when activated
amount: {
type: String,
optional: true,
@@ -22,6 +23,11 @@ const AdjustmentSchema = new SimpleSchema({
type: String,
optional: true,
},
operation: {
type: String,
allowedValues: ['set', 'increment'],
defaultValue: 'increment',
},
});
export { AdjustmentSchema };

View File

@@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import { ActionSchema } from '/imports/api/properties/Actions.js';
import { ActionSchema, ComputedOnlyActionSchema } from '/imports/api/properties/Actions.js';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
// Attacks are special instances of actions
let AttackSchema = new SimpleSchema()
@@ -25,4 +26,24 @@ let AttackSchema = new SimpleSchema()
},
});
export { AttackSchema };
const ComputedOnlyAttackSchema = new SimpleSchema()
.extend(ComputedOnlyActionSchema)
.extend({
rollBonusResult: {
type: Number,
optional: true,
},
rollBonusErrors: {
type: Array,
optional: true,
},
'rollBonusErrors.$':{
type: ErrorSchema,
},
});
const ComputedAttackSchema = new SimpleSchema()
.extend(AttackSchema)
.extend(ComputedOnlyAttackSchema);
export { AttackSchema, ComputedOnlyAttackSchema, ComputedAttackSchema };

View File

@@ -39,6 +39,11 @@ let AttributeSchema = new SimpleSchema({
type: String,
allowedValues: ['d4', 'd6', 'd8', 'd10', 'd12', 'd20'],
optional: true,
},
// For type spellSlot, the level needs to be stored separately
spellSlotLevelCalculation: {
type: String,
optional: true,
},
// The starting value, before effects
baseValueCalculation: {
@@ -81,6 +86,18 @@ let ComputedOnlyAttributeSchema = new SimpleSchema({
},
'baseValueErrors.$': {
type: ErrorSchema,
},
// The result of spellSlotLevelCalculation
spellSlotLevelValue: {
type: SimpleSchema.oneOf(Number, String, Boolean),
optional: true,
},
spellSlotLevelErrors: {
type: Array,
optional: true,
},
'spellSlotLevelErrors.$': {
type: ErrorSchema,
},
// The computed value of the attribute
value: {

View File

@@ -6,6 +6,10 @@ let ClassLevelSchema = new SimpleSchema({
type: String,
optional: true,
},
description: {
type: String,
optional: true,
},
// The name of this class level's variable
variableName: {
type: String,
@@ -23,6 +27,11 @@ let ClassLevelSchema = new SimpleSchema({
'nextLevelTags.$': {
type: String,
},
// Same as in SlotFillers.js
slotFillerCondition: {
type: String,
optional: true,
},
});
export { ClassLevelSchema };

View File

@@ -3,6 +3,7 @@ import DAMAGE_TYPES from '/imports/constants/DAMAGE_TYPES.js';
const DamageSchema = new SimpleSchema({
// The roll that determines how much to damage the attribute
// This can be simplified, but only computed when applied
amount: {
type: String,
optional: true,

View File

@@ -38,7 +38,6 @@ let EffectSchema = new SimpleSchema({
},
'stats.$': {
type: String,
optional: true,
},
});

View File

@@ -12,7 +12,6 @@ let ProficiencySchema = new SimpleSchema({
},
'stats.$': {
type: String,
optional: true,
},
// A number representing how proficient the character is
value: {

View File

@@ -19,7 +19,7 @@ import SimpleSchema from 'simpl-schema';
* child rolls are applied
*/
let RollSchema = new SimpleSchema({
// The roll
// The roll, can be simplified, but only computed in context
roll: {
type: String,
optional: true,

View File

@@ -1,4 +1,5 @@
import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
// These are the rolls made when saves are called for
// For the saving throw bonus or proficiency, see ./Skills.js
@@ -18,4 +19,22 @@ let SavingThrowSchema = new SimpleSchema ({
},
});
export { SavingThrowSchema };
const ComputedOnlySavingThrowSchema = new SimpleSchema({
dcResult: {
type: Number,
optional: true,
},
dcErrors: {
type: Array,
optional: true,
},
'dcErrors.$':{
type: ErrorSchema,
},
});
const ComputedSavingThrowSchema = new SimpleSchema()
.extend(SavingThrowSchema)
.extend(ComputedOnlySavingThrowSchema);
export { SavingThrowSchema, ComputedOnlySavingThrowSchema, ComputedSavingThrowSchema };

View File

@@ -0,0 +1,38 @@
// SlotFiller fillers specifically fill a slot with a bit more control than
// other properties
import SimpleSchema from 'simpl-schema';
let SlotFillerSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
picture: {
type: String,
optional: true,
},
description: {
type: String,
optional: true,
},
// Overrides the type when searching for properties
slotFillerType: {
type: String,
optional: true,
},
// Fill more than one quantity in a slot, like feats and ability score
// improvements, filtered out of UI if there isn't space in quantityExpected
slotQuantityFilled: {
type: SimpleSchema.Integer,
defaultValue: 1,
min: 0,
},
// Filters out of UI if condition isn't met, but isn't otherwise enforced
slotFillerCondition: {
type: String,
optional: true,
},
});
export { SlotFillerSchema };

View File

@@ -1,6 +1,19 @@
import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
let SlotSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
description: {
type: String,
optional: true,
},
slotType: {
type: String,
optional: true,
},
slotTags: {
type: Array,
defaultValue: [],
@@ -8,6 +21,51 @@ let SlotSchema = new SimpleSchema({
'slotTags.$': {
type: String,
},
quantityExpected: {
type: SimpleSchema.Integer,
defaultValue: 1,
min: 0,
},
ignored: {
type: Boolean,
optional: true,
},
slotCondition: {
type: String,
optional: true,
},
hideWhenFull: {
type: Boolean,
optional: true,
}
});
export { SlotSchema };
const ComputedOnlySlotSchema = new SimpleSchema({
// The computed result of the effect
slotConditionResult: {
type: SimpleSchema.oneOf(Number, String, Boolean),
optional: true,
},
// The errors encountered while computing the result
slotConditionErrors: {
type: Array,
optional: true,
},
'slotConditionErrors.$':{
type: ErrorSchema,
},
totalFilled: {
type: SimpleSchema.Integer,
defaultValue: 0,
},
spaceLeft: {
type: SimpleSchema.Integer,
optional: true,
},
});
const ComputedSlotSchema = new SimpleSchema()
.extend(ComputedOnlySlotSchema)
.extend(SlotSchema);
export { SlotSchema, ComputedSlotSchema, ComputedOnlySlotSchema };

View File

@@ -1,4 +1,6 @@
import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import VARIABLE_NAME_REGEX from '/imports/constants/VARIABLE_NAME_REGEX.js';
let SpellListSchema = new SimpleSchema({
name: {
@@ -9,6 +11,12 @@ let SpellListSchema = new SimpleSchema({
type: String,
optional: true,
},
variableName: {
type: String,
regEx: VARIABLE_NAME_REGEX,
min: 2,
optional: true,
},
// Calculation of how many spells in this list can be prepared
maxPrepared: {
type: String,
@@ -16,4 +24,22 @@ let SpellListSchema = new SimpleSchema({
},
});
export { SpellListSchema }
const ComputedOnlySpellListSchema = new SimpleSchema({
maxPreparedResult: {
type: Number,
optional: true,
},
maxPreparedErrors: {
type: Array,
optional: true,
},
'maxPreparedErrors.$':{
type: ErrorSchema,
},
});
const ComputedSpellListSchema = new SimpleSchema()
.extend(SpellListSchema)
.extend(ComputedOnlySpellListSchema);
export { SpellListSchema, ComputedOnlySpellListSchema, ComputedSpellListSchema };

View File

@@ -1,5 +1,6 @@
import { ActionSchema } from '/imports/api/properties/Actions.js';
import { ActionSchema, ComputedOnlyActionSchema } from '/imports/api/properties/Actions.js';
import SimpleSchema from 'simpl-schema';
import VARIABLE_NAME_REGEX from '/imports/constants/VARIABLE_NAME_REGEX.js';
const magicSchools = [
'abjuration',
@@ -25,6 +26,10 @@ let SpellSchema = new SimpleSchema({})
type: Boolean,
optional: true,
},
prepared: {
type: Boolean,
optional: true,
},
// This spell ignores spell slot rules
castWithoutSpellSlots: {
type: Boolean,
@@ -33,14 +38,6 @@ let SpellSchema = new SimpleSchema({})
hasAttackRoll: {
type: Boolean,
optional: true,
},
// Spell lists that this spell appears on
spellLists: {
type: Array,
defaultValue: [],
},
'spellLists.$': {
type: String,
},
description: {
type: String,
@@ -93,4 +90,11 @@ let SpellSchema = new SimpleSchema({})
},
});
export { SpellSchema };
const ComputedOnlySpellSchema = new SimpleSchema()
.extend(ComputedOnlyActionSchema);
const ComputedSpellSchema = new SimpleSchema()
.extend(SpellSchema)
.extend(ComputedOnlySpellSchema);
export { SpellSchema, ComputedOnlySpellSchema, ComputedSpellSchema };

View File

@@ -25,7 +25,7 @@ const ToggleSchema = new SimpleSchema({
const ComputedOnlyToggleSchema = new SimpleSchema({
// The computed result of the effect
toggleResult: {
type: SimpleSchema.oneOf(Number, String, Boolean),
type: Boolean,
optional: true,
},
// The errors encountered while computing the result

View File

@@ -1,7 +1,7 @@
import SimpleSchema from 'simpl-schema';
import { ActionSchema } from '/imports/api/properties/Actions.js';
import { ComputedActionSchema } from '/imports/api/properties/Actions.js';
import { AdjustmentSchema } from '/imports/api/properties/Adjustments.js';
import { AttackSchema } from '/imports/api/properties/Attacks.js';
import { ComputedAttackSchema } from '/imports/api/properties/Attacks.js';
import { ComputedAttributeSchema } from '/imports/api/properties/Attributes.js';
import { BuffSchema } from '/imports/api/properties/Buffs.js';
import { ClassLevelSchema } from '/imports/api/properties/ClassLevels.js';
@@ -9,41 +9,41 @@ import { ContainerSchema } from '/imports/api/properties/Containers.js';
import { DamageSchema } from '/imports/api/properties/Damages.js';
import { DamageMultiplierSchema } from '/imports/api/properties/DamageMultipliers.js';
import { ComputedEffectSchema } from '/imports/api/properties/Effects.js';
import { ExperienceSchema } from '/imports/api/properties/Experiences.js';
import { FeatureSchema } from '/imports/api/properties/Features.js';
import { FolderSchema } from '/imports/api/properties/Folders.js';
import { ItemSchema } from '/imports/api/properties/Items.js';
import { NoteSchema } from '/imports/api/properties/Notes.js';
import { ProficiencySchema } from '/imports/api/properties/Proficiencies.js';
import { RollSchema } from '/imports/api/properties/Rolls.js';
import { SavingThrowSchema } from '/imports/api/properties/SavingThrows.js';
import { ComputedSavingThrowSchema } from '/imports/api/properties/SavingThrows.js';
import { ComputedSkillSchema } from '/imports/api/properties/Skills.js';
import { SlotSchema } from '/imports/api/properties/Slots.js';
import { SpellSchema } from '/imports/api/properties/Spells.js';
import { SpellListSchema } from '/imports/api/properties/SpellLists.js';
import { ComputedSlotSchema } from '/imports/api/properties/Slots.js';
import { SlotFillerSchema } from '/imports/api/properties/SlotFillers.js';
import { ComputedSpellSchema } from '/imports/api/properties/Spells.js';
import { ComputedSpellListSchema } from '/imports/api/properties/SpellLists.js';
import { ToggleSchema } from '/imports/api/properties/Toggles.js';
const propertySchemasIndex = {
action: ActionSchema,
action: ComputedActionSchema,
adjustment: AdjustmentSchema,
attack: AttackSchema,
attack: ComputedAttackSchema,
attribute: ComputedAttributeSchema,
buff: BuffSchema,
classLevel: ClassLevelSchema,
damage: DamageSchema,
damageMultiplier: DamageMultiplierSchema,
effect: ComputedEffectSchema,
experience: ExperienceSchema,
feature: FeatureSchema,
folder: FolderSchema,
note: NoteSchema,
proficiency: ProficiencySchema,
propertySlot: ComputedSlotSchema,
roll: RollSchema,
savingThrow: SavingThrowSchema,
savingThrow: ComputedSavingThrowSchema,
skill: ComputedSkillSchema,
slot: SlotSchema,
spellList: SpellListSchema,
spell: SpellSchema,
slotFiller: SlotFillerSchema,
spellList: ComputedSpellListSchema,
spell: ComputedSpellSchema,
toggle: ToggleSchema,
container: ContainerSchema,
item: ItemSchema,

View File

@@ -8,7 +8,6 @@ import { ClassLevelSchema } from '/imports/api/properties/ClassLevels.js';
import { DamageSchema } from '/imports/api/properties/Damages.js';
import { DamageMultiplierSchema } from '/imports/api/properties/DamageMultipliers.js';
import { EffectSchema } from '/imports/api/properties/Effects.js';
import { ExperienceSchema } from '/imports/api/properties/Experiences.js';
import { FeatureSchema } from '/imports/api/properties/Features.js';
import { FolderSchema } from '/imports/api/properties/Folders.js';
import { NoteSchema } from '/imports/api/properties/Notes.js';
@@ -17,6 +16,7 @@ import { RollSchema } from '/imports/api/properties/Rolls.js';
import { SavingThrowSchema } from '/imports/api/properties/SavingThrows.js';
import { SkillSchema } from '/imports/api/properties/Skills.js';
import { SlotSchema } from '/imports/api/properties/Slots.js';
import { SlotFillerSchema } from '/imports/api/properties/SlotFillers.js';
import { SpellListSchema } from '/imports/api/properties/SpellLists.js';
import { SpellSchema } from '/imports/api/properties/Spells.js';
import { ToggleSchema } from '/imports/api/properties/Toggles.js';
@@ -33,15 +33,15 @@ const propertySchemasIndex = {
damage: DamageSchema,
damageMultiplier: DamageMultiplierSchema,
effect: EffectSchema,
experience: ExperienceSchema,
feature: FeatureSchema,
folder: FolderSchema,
note: NoteSchema,
proficiency: ProficiencySchema,
propertySlot: SlotSchema,
roll: RollSchema,
savingThrow: SavingThrowSchema,
skill: SkillSchema,
slot: SlotSchema,
slotFiller: SlotFillerSchema,
spellList: SpellListSchema,
spell: SpellSchema,
toggle: ToggleSchema,

View File

@@ -1,11 +1,9 @@
import SimpleSchema from 'simpl-schema';
const ErrorSchema = new SimpleSchema({
// The roll that determines how much to change the attribute
message: {
type: String,
},
// Who this adjustment applies to
type: {
type: String,
},

View File

@@ -17,6 +17,10 @@ const ItemConsumedSchema = new SimpleSchema({
type: Number,
defaultValue: 1,
},
itemId: {
type: String,
optional: true,
},
});
export default ItemConsumedSchema;

View File

@@ -4,13 +4,19 @@ import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js';
import getCollectionByName from '/imports/api/parenting/getCollectionByName.js';
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
const setPublic = new ValidatedMethod({
name: 'sharing.methods.setPublic',
name: 'sharing.setPublic',
validate: new SimpleSchema({
docRef: RefSchema,
isPublic: { type: Boolean },
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({docRef, isPublic}){
let doc = fetchDocByRef(docRef);
assertOwnership(doc, this.userId);
@@ -21,7 +27,7 @@ const setPublic = new ValidatedMethod({
});
const updateUserSharePermissions = new ValidatedMethod({
name: 'sharing.methods.updateUserSharePermissions',
name: 'sharing.updateUserSharePermissions',
validate: new SimpleSchema({
docRef: RefSchema,
userId: {
@@ -33,6 +39,11 @@ const updateUserSharePermissions = new ValidatedMethod({
allowedValues: ['reader', 'writer', 'none'],
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({docRef, userId, role}){
let doc = fetchDocByRef(docRef);
if (role === 'none'){

View File

@@ -88,18 +88,18 @@ export function assertDocEditPermission(doc, userId){
}
export function assertViewPermission(doc, userId) {
assertIdValid(userId);
assertdocExists(doc);
if (doc.public) return true;
assertIdValid(userId);
if (
doc.owner === userId ||
doc.public ||
_.contains(doc.readers, userId) ||
_.contains(doc.writers, userId)
){
return true;
} else {
throw new Meteor.Error('View permission denied',
'You do not have permission to view this character');
'You do not have permission to view this document');
}
}

View File

@@ -0,0 +1,115 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import Creatures from '/imports/api/creature/Creatures.js';
import Tabletops, { assertUserInTabletop } from '/imports/api/tabletop/Tabletops.js';
let Messages = new Mongo.Collection('messages');
let MessagesSchema = new SimpleSchema({
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
content: {
type: String,
max: 1000,
},
timestamp: {
type: Date,
index: 1,
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
username: {
type: String,
},
});
Messages.attachSchema(MessagesSchema);
const sendMessage = new ValidatedMethod({
name: 'messages.send',
validate: new SimpleSchema({
content: {
type: String,
max: 1000,
},
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({content, tabletopId}) {
let user = Meteor.user();
if (!user) {
throw new Meteor.Error('messages.send.denied',
'You need to be logged in to send a message');
}
assertUserInTabletop(tabletopId, this.userId);
return Messages.insert({
content,
tabletopId,
timestamp: new Date(),
userId: user._id,
username: user.username,
});
},
});
const removeMessages = new ValidatedMethod({
name: 'messages.remove',
validate: new SimpleSchema({
messageId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({messageId, tabletopId}) {
if (!this.userId) {
throw new Meteor.Error('messages.remove.denied',
'You need to be logged in to remove a tabletop');
}
let message = Messages.findOne(messageId);
let tabletop = Tabletops.findOne(message.tabletopId);
if (this.userId !== message.userId && this.userId !== tabletop.gameMaster){
throw new Meteor.Error('messages.remove.denied',
'You don\'t have permission to remove this message');
}
let removed = Messages.remove({
_id: messageId,
});
Creatures.update({
tabletop: tabletopId,
}, {
$unset: {tabletop: 1},
});
return removed;
},
});
export default Messages;
export { sendMessage, removeMessages };

View File

@@ -0,0 +1,201 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import Creatures from '/imports/api/creature/Creatures.js';
let Tabletops = new Mongo.Collection('tabletops');
const InitiativeSchema = new SimpleSchema({
active: {
type: Boolean,
defaultValue: false,
},
roundNumber: {
type: SimpleSchema.Integer,
defaultValue: 0,
},
initiativeNumber: {
type: SimpleSchema.Integer,
optional: true,
},
activeCreature: {
type: String,
regEx: SimpleSchema.RegEx.id,
optional: true,
},
});
// All creatures in a tabletop have a shared time and space.
let TabletopSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
initiative: {
type: InitiativeSchema,
defaultValue: {},
},
gameMaster: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
players: {
type: Array,
defaultValue: [],
},
'players.$': {
type: String,
regEx: SimpleSchema.RegEx.id,
},
});
Tabletops.attachSchema(TabletopSchema);
function assertUserIsTabletopOwner(tabletopId, userId){
let tabletop = Tabletops.findOne(tabletopId);
if (!tabletop){
throw new Meteor.Error('Tabletop does not exist',
'No tabletop could be found for the given tabletop id');
}
if (tabletop.gameMaster !== userId){
throw new Meteor.Error('Not the owner',
'The user is not the owner of the given tabletop');
}
}
export function assertUserInTabletop(tabletopId, userId){
let tabletop = Tabletops.findOne(tabletopId);
if (!tabletop){
throw new Meteor.Error('Tabletop does not exist',
'No tabletop could be found for the given tabletop id');
}
if (tabletop.gameMaster !== userId && !tabletop.players.includes(userId)){
throw new Meteor.Error('Not in tabletop',
'The user is not the gamemaster or a player in the given tabletop');
}
}
function assertUserIsAdmin(userId){
if (!Meteor.users.isAdmin(userId)){
throw new Meteor.Error('Admin only',
'This is restricted to admins for now');
}
}
const insertTabletop = new ValidatedMethod({
name: 'tabletops.insert',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run() {
if (!this.userId) {
throw new Meteor.Error('tabletops.insert.denied',
'You need to be logged in to insert a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserIsAdmin(this.userId);
return Tabletops.insert({
gameMaster: this.userId,
});
},
});
const removeTabletop = new ValidatedMethod({
name: 'tabletops.remove',
validate: new SimpleSchema({
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({tabletopId}) {
if (!this.userId) {
throw new Meteor.Error('tabletops.remove.denied',
'You need to be logged in to remove a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserIsTabletopOwner(tabletopId, this.userId);
assertUserIsAdmin(this.userId);
let removed = Tabletops.remove({
_id: tabletopId,
});
Creatures.update({
tabletop: tabletopId,
}, {
$unset: {tabletop: 1},
});
return removed;
},
});
const addCreaturesToTabletop = new ValidatedMethod({
name: 'tabletops.addCreatures',
validate: new SimpleSchema({
'creatureIds': {
type: Array,
},
'creatureIds.$': {
type: String,
regEx: SimpleSchema.RegEx.id,
},
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({tabletopId, creatureIds}) {
if (!this.userId) {
throw new Meteor.Error('tabletops.addCreatures.denied',
'You need to be logged in to remove a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserInTabletop(tabletopId, this.userId);
assertUserIsAdmin(this.userId);
Creatures.update({
_id: {$in: creatureIds},
$or: [
{writers: this.userId},
{owner: this.userId},
],
}, {
$set: {tabletop: tabletopId},
}, {
multi: true,
});
},
});
export default Tabletops;
export { insertTabletop, removeTabletop, addCreaturesToTabletop };

View File

@@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
let Invites= new Mongo.Collection('invites');
@@ -85,13 +86,18 @@ function alignInvitesWithPatreonTier(user){
}
const getInviteToken = new ValidatedMethod({
name: 'Invites.methods.getToken',
name: 'invites.getToken',
validate: new SimpleSchema({
inviteId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({inviteId}) {
let invite = Invites.findOne(inviteId);
if (this.userId !== invite.inviter) {
@@ -109,12 +115,17 @@ const getInviteToken = new ValidatedMethod({
});
const acceptInviteToken = new ValidatedMethod({
name: 'Invites.methods.acceptToken',
name: 'invites.acceptToken',
validate: new SimpleSchema({
inviteToken: {
type: String,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({inviteToken}) {
if (!this.userId) {
throw new Meteor.Error('Invites.methods.acceptToken.denied',
@@ -146,13 +157,18 @@ const acceptInviteToken = new ValidatedMethod({
});
const revokeInvite = new ValidatedMethod({
name: 'Invites.methods.revokeInvite',
name: 'invites.revokeInvite',
validate: new SimpleSchema({
inviteId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({inviteId}) {
if (!this.userId) {
throw new Meteor.Error('Invites.methods.revokeInvite.denied',

View File

@@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
const userSchema = new SimpleSchema({
username: {
@@ -82,13 +83,27 @@ const userSchema = new SimpleSchema({
blackbox: true,
optional: true,
},
preferences: {
type: Object,
optional: true,
defaultValue: {},
},
'preferences.swapAbilityScoresAndModifiers': {
type: Boolean,
optional: true,
},
});
Meteor.users.attachSchema(userSchema);
Meteor.users.generateApiKey = new ValidatedMethod({
name: 'Users.methods.generateApiKey',
name: 'users.generateApiKey',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run(){
if(Meteor.isClient) return;
var user = Meteor.users.findOne(this.userId);
@@ -100,10 +115,15 @@ Meteor.users.generateApiKey = new ValidatedMethod({
});
Meteor.users.setDarkMode = new ValidatedMethod({
name: 'Users.methods.setDarkMode',
name: 'users.setDarkMode',
validate: new SimpleSchema({
darkMode: { type: Boolean },
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({darkMode}){
if (!this.userId) return;
Meteor.users.update(this.userId, {$set: {darkMode}});
@@ -111,7 +131,7 @@ Meteor.users.setDarkMode = new ValidatedMethod({
});
Meteor.users.sendVerificationEmail = new ValidatedMethod({
name: 'Users.methods.sendVerificationEmail',
name: 'users.sendVerificationEmail',
validate: new SimpleSchema({
userId:{
type: String,
@@ -121,6 +141,11 @@ Meteor.users.sendVerificationEmail = new ValidatedMethod({
type: String,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({userId, address}){
userId = this.userId || userId;
let user = Meteor.users.findOne(userId);
@@ -143,8 +168,13 @@ Meteor.users.isAdmin = function(userId){
}
Meteor.users.canPickUsername = new ValidatedMethod({
name: 'Users.methods.canPickUsername',
name: 'users.canPickUsername',
validate: userSchema.pick('username').validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({username}){
if (Meteor.isClient) return;
let user = Accounts.findUserByUsername(username);
@@ -157,8 +187,13 @@ Meteor.users.canPickUsername = new ValidatedMethod({
});
Meteor.users.setUsername = new ValidatedMethod({
name: 'Users.methods.setUsername',
name: 'users.setUsername',
validate: userSchema.pick('username').validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({username}){
if (!this.userId) throw 'Can only set your username if logged in';
if (Meteor.isClient) return;
@@ -166,8 +201,38 @@ Meteor.users.setUsername = new ValidatedMethod({
}
});
Meteor.users.setPreference = new ValidatedMethod({
name: 'users.setPreference',
validate: new SimpleSchema({
preference:{
type: String,
},
value: {
type: SimpleSchema.oneOf(Boolean),
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({preference, value}){
if (!this.userId) throw 'You can only set preferences once logged in';
let prefPath = `preferences.${preference}`
if (value == true){
return Meteor.users.update(this.userId, {
$set: {[prefPath]: true},
});
} else {
return Meteor.users.update(this.userId, {
$unset: {[prefPath]: 1},
});
}
},
});
Meteor.users.subscribeToLibrary = new ValidatedMethod({
name: 'Users.methods.subscribeToLibrary',
name: 'users.subscribeToLibrary',
validate: new SimpleSchema({
libraryId:{
type: String,
@@ -177,6 +242,11 @@ Meteor.users.subscribeToLibrary = new ValidatedMethod({
type: Boolean,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({libraryId, subscribe}){
if (!this.userId) throw 'Can only subscribe if logged in';
if (subscribe){
@@ -192,12 +262,17 @@ Meteor.users.subscribeToLibrary = new ValidatedMethod({
});
Meteor.users.findUserByUsernameOrEmail = new ValidatedMethod({
name: 'Users.methods.findUserByUsernameOrEmail',
name: 'users.findUserByUsernameOrEmail',
validate: new SimpleSchema({
usernameOrEmail:{
type: String,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({usernameOrEmail}){
if (Meteor.isClient) return;
let user = Accounts.findUserByUsername(usernameOrEmail) ||

View File

@@ -77,5 +77,13 @@ export function getUserTier(user){
}
}
export function assertUserHasPaidBenefits(user){
let tier = getUserTier(user);
if (!tier.paidBenefits){
throw new Meteor.Error('Creatures.methods.insert.denied',
`The ${tier.name} tier does not allow you to insert a creature`);
}
}
export default TIERS;
export { GUEST_TIER };

View File

@@ -1,5 +1,4 @@
import request from 'request';
if (!Meteor.isServer) throw 'Server only, do not import this code in the client';
const config = ServiceConfiguration.configurations.findOne({service: 'patreon'});
@@ -62,7 +61,7 @@ const updateIdentity = Meteor.wrapAsync(function(accessToken, userId, callback){
}
try {
let identity = JSON.parse(body);
let membership = identity.included[0];
let membership = identity.included && identity.included[0];
let entitledAmount = membership && membership.attributes
.currently_entitled_amount_cents || 0;
writeEntitledCents(userId, entitledAmount);
@@ -85,7 +84,7 @@ const updatePatreonDetails = function(user){
throw new Meteor.Error('no-patreon-access', 'Patreon access token not found for this user');
}
let accessToken = user.services.patreon.accessToken;
if (user.services.patreon.tokenExpiryDate < new Date()){
if (user.services.patreon.expiresAt < new Date()){
// Token expired, refresh it before continuing
accessToken = refreshAccessToken(user.services.patreon.refreshToken, user._id);
}
@@ -96,7 +95,7 @@ Meteor.methods({
updateMyPatreonDetails(){
const userId = this.userId;
if (!userId) throw new Meteor.Error('not-logged-in', 'You must be logged in to update Patreon details');
const user = Meteor.users.findOne(userId, {fields: {patreon: 1}});
const user = Meteor.users.findOne(userId, {fields: {services: 1}});
updatePatreonDetails(user);
},
});
@@ -113,9 +112,9 @@ const writePatreonToken = function(userId, {
// Write
Meteor.users.update(userId, {
$set: {
'patreon.accessToken': access_token,
'patreon.refreshToken': refresh_token,
'patreon.tokenExpiryDate': expiryDate,
'services.patreon.accessToken': access_token,
'services.patreon.refreshToken': refresh_token,
'services.patreon.expiresAt': expiryDate,
},
$unset: {
'patreon.error': 1,
@@ -127,6 +126,7 @@ const writeEntitledCents = function(userId, amount){
Meteor.users.update(userId, {
$set: {
'services.patreon.entitledCents': amount,
'services.patreon.lastUpdatedIdentity': new Date(),
},
$unset: {
'patreon.error': 1,
@@ -134,4 +134,4 @@ const writeEntitledCents = function(userId, amount){
});
};
export { updatePatreonDetails };
export default updatePatreonDetails;

View File

@@ -0,0 +1,12 @@
import updatePatreonDetails from '/imports/api/users/patreon/updatePatreonDetails.js';
const ONE_DAY = 24 * 60 * 60 * 1000;
Accounts.onLogin(({user}) => {
let patreon = user.services && user.services.patreon;
if (patreon){
const timeSinceIdentityUpdate = new Date() - patreon.lastUpdatedIdentity;
if (timeSinceIdentityUpdate > ONE_DAY){
updatePatreonDetails(user);
}
}
});

View File

@@ -0,0 +1,7 @@
const INVENTORY_TAGS = Object.freeze({
inventory: 'inventory',
equipment: 'equipment',
carried: 'carried',
});
export default INVENTORY_TAGS;

View File

@@ -3,10 +3,6 @@ const PROPERTIES = Object.freeze({
icon: '$vuetify.icons.action',
name: 'Action'
},
adjustment: {
icon: '$vuetify.icons.attribute_damage',
name: 'Attribute damage'
},
attack: {
icon: '$vuetify.icons.attack',
name: 'Attack'
@@ -15,6 +11,10 @@ const PROPERTIES = Object.freeze({
icon: '$vuetify.icons.attribute',
name: 'Attribute'
},
adjustment: {
icon: '$vuetify.icons.attribute_damage',
name: 'Attribute damage'
},
buff: {
icon: '$vuetify.icons.buff',
name: 'Buff'
@@ -23,6 +23,10 @@ const PROPERTIES = Object.freeze({
icon: '$vuetify.icons.class_level',
name: 'Class level'
},
container: {
icon: 'work',
name: 'Container'
},
damage: {
icon: '$vuetify.icons.damage',
name: 'Damage'
@@ -35,10 +39,6 @@ const PROPERTIES = Object.freeze({
icon: '$vuetify.icons.effect',
name: 'Effect'
},
experience: {
icon: '$vuetify.icons.experience',
name: 'Experience'
},
feature: {
icon: 'subject',
name: 'Feature'
@@ -47,6 +47,10 @@ const PROPERTIES = Object.freeze({
icon: 'folder',
name: 'Folder'
},
item: {
icon: '$vuetify.icons.item',
name: 'Item'
},
note: {
icon: 'note',
name: 'Note'
@@ -67,6 +71,14 @@ const PROPERTIES = Object.freeze({
icon: '$vuetify.icons.skill',
name: 'Skill'
},
propertySlot: {
icon: 'tab_unselected',
name: 'Slot'
},
slotFiller: {
icon: 'picture_in_picture',
name: 'Slot filler'
},
spellList: {
icon: '$vuetify.icons.spell_list',
name: 'Spell list'
@@ -75,14 +87,6 @@ const PROPERTIES = Object.freeze({
icon: '$vuetify.icons.spell',
name: 'Spell'
},
container: {
icon: 'work',
name: 'Container'
},
item: {
icon: '$vuetify.icons.item',
name: 'Item'
},
toggle: {
icon: '$vuetify.icons.toggle',
name: 'Toggle'

View File

@@ -11,6 +11,10 @@ const SVG_ICONS = Object.freeze({
name: 'attribute_damage',
shape: 'M 150,53.907593 V 396.625 H 86.563 L 258.093,497.188 429.656,396.626 H 366.22 V 53.908593 Z M 260.25,94.782 c 0.684,-0.028 1.34,-0.018 2,0.094 34.59085,1.140414 45.59236,58.23402 22.719,86.438 41.13,7.74 60.86,67.412 48.75,125.75 h -33.033 l -7.562,101.563 h -72.5 l -6.688,-101.563 h -31.593 c -10.68,-56.805 3.537,-119.376 48.47,-126.343 -23.10419,-27.90601 -9.69693,-87.09653 27.375,-85.845 0.677,0 1.378,-0.066 2.062,-0.094 z',
},
'baby-face': {
name: 'baby_face',
shape: 'M254.443 49.593c-9.011.085-18 1.243-25.056 3.412-7.057 2.17-11.811 5.395-13.676 8.17-7.133 10.617-9.076 19.519-8.1 27.63.977 8.11 5.102 15.92 12.159 23.443 9.846 10.495 25.39 19.81 42.857 26.955-7.836-14.753-11.888-28.818-10.357-42.717 1.826-16.589 11.956-31.077 29.351-43.768-.63-.174-1.186-.377-1.85-.533-7.244-1.703-16.316-2.676-25.328-2.592zm37.79 17.461c-14.638 10.454-20.887 20.609-22.075 31.4-1.474 13.394 5.377 30.64 20.887 52.175l14.023 19.468c12.6-8.667 25.012-12.72 36.782-12.959a54.52 54.52 0 0 1 5.888.2c15.544 1.368 29.047 8.774 41.418 17.423l-10.312 14.752c-11.223-7.846-21.99-13.307-32.684-14.248-10.693-.94-22.044 2.044-36.463 13.838l-11.394-13.932a102.983 102.983 0 0 1 5.824-4.43l-22.74-6.167c-28.693-7.783-56.841-20.93-74.742-40.012-8.951-9.54-15.368-20.858-16.903-33.607-.569-4.726-.43-9.582.42-14.502C121.752 105.216 73 177.51 73 262.775c0 110.751 82.246 199.637 183 199.637s183-88.886 183-199.637c0-97.123-63.252-177.427-146.768-195.72zM170.15 157.138c13.872.282 28.637 5.837 43.547 18.033l-11.394 13.932c-14.419-11.794-25.77-14.779-36.463-13.838-10.693.941-21.46 6.402-32.684 14.248l-10.312-14.752c12.371-8.649 25.874-16.055 41.418-17.424a54.52 54.52 0 0 1 5.888-.199zm-2.15 40c26.955 0 49 22.045 49 49s-22.045 48.998-49 48.998c-26.386 0-48.053-21.125-48.957-47.3a32.955 32.955 0 0 1 0-3.395c.904-26.175 22.571-47.303 48.957-47.303zm176 0c26.955 0 49 22.045 49 49s-22.045 48.998-49 48.998c-26.386 0-48.053-21.125-48.957-47.3a32.955 32.955 0 0 1 0-3.395c.904-26.175 22.571-47.303 48.957-47.303zm-176 17.998c-1.378 0-2.73.097-4.059.268C176.22 220.226 185 232.224 185 246.138c0 13.914-8.78 25.91-21.059 30.733 1.329.17 2.681.267 4.059.267 17.228 0 31-13.772 31-31s-13.772-31.002-31-31.002zm176 0c-1.378 0-2.73.097-4.059.268C352.22 220.226 361 232.224 361 246.138c0 13.914-8.78 25.91-21.059 30.733 1.329.17 2.681.267 4.059.267 17.228 0 31-13.772 31-31s-13.772-31.002-31-31.002zm-284.746 3.006c-14.197 2.45-23.466 7.41-29.065 13.145-7.967 8.162-9.918 18.531-7.39 30.328 3.681 17.18 18.154 35.225 36.076 43.775A235.517 235.517 0 0 1 55 262.775c0-15.287 1.47-30.215 4.254-44.633zm393.492 0A235.355 235.355 0 0 1 457 262.775c0 14.575-1.339 28.823-3.875 42.615 17.922-8.55 32.395-26.595 36.076-43.775 2.528-11.797.577-22.166-7.39-30.328-5.599-5.735-14.868-10.695-29.065-13.145zM152 231.136c-8.391 0-15 6.61-15 15.002 0 3.263 1.008 6.248 2.723 8.688l23.545-18.65c-2.732-3.099-6.734-5.04-11.268-5.04zm176 0c-8.391 0-15 6.61-15 15.002 0 3.263 1.008 6.248 2.723 8.688l23.545-18.65c-2.732-3.099-6.734-5.04-11.268-5.04zm-98.41 49.95c8 6.34 13.916 10.984 18.228 13.718 4.313 2.735 6.56 3.356 8.182 3.356 1.623 0 3.87-.621 8.182-3.356 4.312-2.734 10.228-7.378 18.228-13.718l11.18 14.103c-8 6.34-14.084 11.208-19.772 14.815-5.687 3.606-11.44 6.154-17.818 6.154-6.377 0-12.13-2.548-17.818-6.154-5.688-3.607-11.772-8.474-19.772-14.815zm-82.393 51.1h217.606l-4.336 12.046s-8.333 23.283-25.164 46.664C318.472 414.277 292.167 439.09 256 439.09c-36.167 0-62.472-24.812-79.303-48.193-16.83-23.38-25.164-46.664-25.164-46.664zM218 350.137v32h32v-32z',
},
'back-and-forth': {
name: 'effect',
shape: 'M241.844 28.625l-21.188 5.063L33.25 78.53l-9.594 2.282 2.813 9.47 54.718 184.03 6.156 20.782 10.875-18.75 36.624-63.125 39.344 22.655 9.375-16.188-47.47-27.312L128 187.72l-4.656 8.06-30.406 52.47-45.75-153.844 156.625-37.47-30.344 52.345-4.69 8.126 8.126 4.656L332.75 211.75l-17.594 30.344 16.22 9.312 22.25-38.375 4.687-8.124-8.125-4.656-155.844-89.688 36.594-63.093 10.906-18.845zm-28.25 176.47l-57.438 99.31 155.22 89.5 8.093 4.658-4.69 8.093-44.06 76.25 218.81-52.5-63.874-215.47-44.094 76.25-4.656 8.064-8.094-4.656-155.218-89.5z',

View File

@@ -0,0 +1,16 @@
const VERSION = Meteor.isClient ?
'CLIENT' :
process.env.CONTAINER_VERSION || getVersionFromGit();
export default VERSION;
function getVersionFromGit(){
try {
return require('child_process')
.execSync('git rev-parse --short HEAD')
.toString().trim();
} catch (e){
return 'GIT_VERSION_FAIL'
}
}

View File

@@ -1,8 +1,20 @@
import { create, all } from 'mathjs';
const math = create(all);
math.import({
'if': function(pred, a, b) {
return pred ? a : b;
},
'roll': function(number, diceSize){
let randomSrc = DDP.randomStream('diceRoller');
if (number > 100) throw 'Can only roll 100 dice at a time';
let rollTotal = 0;
let i, roll;
for (i = 0; i < number; i++){
roll = ~~(randomSrc.fraction() * diceSize) + 1
rollTotal += roll;
}
return rollTotal;
}
});

View File

@@ -1,7 +0,0 @@
// All of the compile functions are provided for use in compiling parse trees
// Every compile function takes in ParseNodes as arguements and returns a single
// ConstantNode as a result
const compileFunctions = {};
export compileFunctions;

View File

@@ -1,15 +0,0 @@
export default function sum(inputNode) {
let node = inputNode.roll();
if (node.type === 'numberArray'){
let total = node.value.reduce((total, num) => total + num, 0);
return new ConstantNode({type: 'number', value: total});
} else {
let errors = node.errors || [];
errors.push(`Could not sum ${node.value}`);
return new ConstantNode({
type: 'uncompiledNode',
value: node.value,
errors,
});
}
}

View File

@@ -0,0 +1,94 @@
export default {
'abs': {
comment: 'Returns the absolute value of a number',
examples: [
{input: 'abs(9)', result: '9'},
{input: 'abs(-3)', result: '3'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.abs,
},
'sqrt': {
comment: 'Returns the square root of a number',
examples: [
{input: 'sqrt(16)', result: '4'},
{input: 'sqrt(10)', result: '3.1622776601683795'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.sqrt,
},
'max': {
comment: 'Returns the largest of the given numbers',
examples: [{input: 'min(12, 6, 3, 168)', result: '168'}],
argumentType: 'number',
resultType: 'number',
fn: Math.max,
},
'min': {
comment: 'Returns the smallest of the given numbers',
examples: [{input: 'min(12, 6, 3, 168)', result: '3'}],
argumentType: 'number',
resultType: 'number',
fn: Math.min,
},
'round': {
comment: 'Returns the value of a number rounded to the nearest integer',
examples: [
{input: 'round(5.95)', result: '6'},
{input: 'round(5.5)', result: '6'},
{input: 'round(5.05)', result: '5'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.round,
},
'floor': {
comment: 'Rounds a number down to the next smallest integer',
examples: [
{input: 'floor(5.95)', result: '5'},
{input: 'floor(5.05)', result: '5'},
{input: 'floor(5)', result: '5'},
{input: 'floor(-5.5)', result: '-6'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.floor,
},
'ceil': {
comment: 'Rounds a number up to the next largest integer',
examples: [
{input: 'ceil(5.95)', result: '6'},
{input: 'ceil(5.05)', result: '6'},
{input: 'ceil(5)', result: '5'},
{input: 'ceil(-5.5)', result: '-5'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.ceil,
},
'trunc': {
comment: 'Returns the integer part of a number by removing any fractional digits',
examples: [
{input: 'trunc(5.95)', result: '5'},
{input: 'trunc(5.05)', result: '5'},
{input: 'trunc(5)', result: '5'},
{input: 'trunc(-5.5)', result: '-5'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.trunc,
},
'sign': {
comment: 'Returns either a positive or negative 1, indicating the sign of a number, or zero',
examples: [
{input: 'sign(-3)', result: '-1'},
{input: 'sign(3)', result: '1'},
{input: 'sign(0)', result: '0'},
],
argumentType: 'number',
resultType: 'number',
fn: Math.sign,
}
}

View File

@@ -2,11 +2,19 @@
// http://github.com/Hardmath123/nearley
function id(x) { return x[0]; }
import AccessorNode from '/imports/parser/parseTree/AccessorNode.js';
import ArrayNode from '/imports/parser/parseTree/ArrayNode.js';
import CallNode from '/imports/parser/parseTree/CallNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
import IfNode from '/imports/parser/parseTree/IfNode.js';
import IfNode from '/imports/parser/parseTree/IfNode.js';
import IndexNode from '/imports/parser/parseTree/IndexNode.js';
import OperatorNode from '/imports/parser/parseTree/OperatorNode.js';
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
import ParenthesisNode from '/imports/parser/parseTree/ParenthesisNode.js';
import RollNode from '/imports/parser/parseTree/RollNode.js';
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
import UnaryOperatorNode from '/imports/parser/parseTree/UnaryOperatorNode.js';
import NotOperatorNode from '/imports/parser/parseTree/NotOperatorNode.js';
import moo from 'moo';
const lexer = moo.compile({
@@ -16,26 +24,30 @@ function id(x) { return x[0]; }
value: s => s.slice(1, -1),
},
name: {
match: /[a-zA-Z]+\w*?/,
match: /[a-zA-Z_]*[a-ce-zA-Z_][a-zA-Z0-9_]*/,
type: moo.keywords({
'keywords': ['if', 'else', 'd'],
'keywords': ['true', 'false'],
}),
},
space: {
match: /\s+/,
lineBreaks: true,
},
separators: [',', '.'],
diceOperator: ['d'],
separator: [',', ';'],
period: ['.'],
ifOperator: ['?'],
elseOperator: [':'],
multiplicativeOperator: ['*', '/'],
exponentOperator: ['^'],
additiveOperator: ['+', '-'],
unaryOperator: ['-'],
andOperator: ['&', '&&'],
orOperator: ['|', '||'],
stringDelimiters: ['\"', '\''],
equalityOperator: ['=', '==', '===', '!=', '!=='],
notOperator: ['!'],
relationalOperator: ['>', '<', '>=', '<='],
brackets: ['(', ')', '{', '}'],
brackets: ['(', ')', '{', '}', '[', ']'],
});
function nuller() { return null; }
@@ -49,50 +61,78 @@ function id(x) { return x[0]; }
}
let Lexer = lexer;
let ParserRules = [
{"name": "ifStatement", "symbols": [{"literal":"if"}, "_", {"literal":"("}, "_", "expression", "_", {"literal":")"}, "_", "ifStatement", "_", {"literal":"else"}, "_", "ifStatement"], "postprocess":
d => new IfNode({condition: d[4], consequent: d[8], alternative: d[12]})
{"name": "spacedExpression", "symbols": ["_", "expression", "_"], "postprocess": d => d[1]},
{"name": "expression", "symbols": ["ifStatement"], "postprocess": id},
{"name": "ifStatement", "symbols": ["orExpression", "_", (lexer.has("ifOperator") ? {type: "ifOperator"} : ifOperator), "_", "orExpression", "_", (lexer.has("elseOperator") ? {type: "elseOperator"} : elseOperator), "_", "ifStatement"], "postprocess":
d => new IfNode({condition: d[0], consequent: d[4], alternative: d[8]})
},
{"name": "ifStatement", "symbols": ["expression"], "postprocess": id},
{"name": "expression", "symbols": ["equalityExpression"], "postprocess": d => d[0]},
{"name": "equalityExpression", "symbols": ["equalityExpression", "_", (lexer.has("equalityOperator") ? {type: "equalityOperator"} : equalityOperator), "_", "relationalExpression"], "postprocess": d => operator(d, 'equality')},
{"name": "equalityExpression", "symbols": ["relationalExpression"], "postprocess": id},
{"name": "relationalExpression", "symbols": ["relationalExpression", "_", (lexer.has("relationalOperator") ? {type: "relationalOperator"} : relationalOperator), "_", "orExpression"], "postprocess": d => operator(d, 'relation')},
{"name": "relationalExpression", "symbols": ["orExpression"], "postprocess": id},
{"name": "ifStatement", "symbols": ["orExpression"], "postprocess": id},
{"name": "orExpression", "symbols": ["orExpression", "_", (lexer.has("orOperator") ? {type: "orOperator"} : orOperator), "_", "andExpression"], "postprocess": d => operator(d, 'or')},
{"name": "orExpression", "symbols": ["andExpression"], "postprocess": id},
{"name": "andExpression", "symbols": ["andExpression", "_", (lexer.has("andOperator") ? {type: "andOperator"} : andOperator), "_", "additiveExpression"], "postprocess": d => operator(d, 'and')},
{"name": "andExpression", "symbols": ["additiveExpression"], "postprocess": id},
{"name": "andExpression", "symbols": ["andExpression", "_", (lexer.has("andOperator") ? {type: "andOperator"} : andOperator), "_", "equalityExpression"], "postprocess": d => operator(d, 'and')},
{"name": "andExpression", "symbols": ["equalityExpression"], "postprocess": id},
{"name": "equalityExpression", "symbols": ["equalityExpression", "_", (lexer.has("equalityOperator") ? {type: "equalityOperator"} : equalityOperator), "_", "relationalExpression"], "postprocess": d => operator(d, 'equality')},
{"name": "equalityExpression", "symbols": ["relationalExpression"], "postprocess": id},
{"name": "relationalExpression", "symbols": ["relationalExpression", "_", (lexer.has("relationalOperator") ? {type: "relationalOperator"} : relationalOperator), "_", "additiveExpression"], "postprocess": d => operator(d, 'relation')},
{"name": "relationalExpression", "symbols": ["additiveExpression"], "postprocess": id},
{"name": "additiveExpression", "symbols": ["additiveExpression", "_", (lexer.has("additiveOperator") ? {type: "additiveOperator"} : additiveOperator), "_", "multiplicativeExpression"], "postprocess": d => operator(d, 'add')},
{"name": "additiveExpression", "symbols": ["multiplicativeExpression"], "postprocess": id},
{"name": "multiplicativeExpression", "symbols": ["multiplicativeExpression", "_", (lexer.has("multiplicativeOperator") ? {type: "multiplicativeOperator"} : multiplicativeOperator), "_", "rollExpression"], "postprocess": d => operator(d, 'multiply')},
{"name": "multiplicativeExpression", "symbols": ["rollExpression"], "postprocess": id},
{"name": "rollExpression", "symbols": ["rollExpression", "_", {"literal":"d"}, "_", "exponentExpression"], "postprocess": d => operator(d, 'roll')},
{"name": "rollExpression", "symbols": ["exponentExpression"], "postprocess": id},
{"name": "rollExpression", "symbols": ["rollExpression", "_", (lexer.has("diceOperator") ? {type: "diceOperator"} : diceOperator), "_", "exponentExpression"], "postprocess": d => new RollNode({left: d[0], right: d[4]})},
{"name": "rollExpression", "symbols": ["singleRollExpression"], "postprocess": id},
{"name": "singleRollExpression", "symbols": [{"literal":"d"}, "_", "singleRollExpression"], "postprocess": d => new RollNode({left: new ConstantNode({value: 1, type: 'number'}), right: d[2]})},
{"name": "singleRollExpression", "symbols": ["exponentExpression"], "postprocess": id},
{"name": "exponentExpression", "symbols": ["callExpression", "_", (lexer.has("exponentOperator") ? {type: "exponentOperator"} : exponentOperator), "_", "exponentExpression"], "postprocess": d => operator(d, 'exponent')},
{"name": "exponentExpression", "symbols": ["callExpression"], "postprocess": id},
{"name": "exponentExpression", "symbols": ["unaryExpression"], "postprocess": id},
{"name": "unaryExpression", "symbols": [(lexer.has("additiveOperator") ? {type: "additiveOperator"} : additiveOperator), "_", "unaryExpression"], "postprocess": d => new UnaryOperatorNode({operator: d[0].value, right: d[2]})},
{"name": "unaryExpression", "symbols": ["notExpression"], "postprocess": id},
{"name": "notExpression", "symbols": [(lexer.has("notOperator") ? {type: "notOperator"} : notOperator), "_", "notExpression"], "postprocess": d => new NotOperatorNode({right: d[2]})},
{"name": "notExpression", "symbols": ["callExpression"], "postprocess": id},
{"name": "callExpression", "symbols": ["name", "_", "arguments"], "postprocess":
d => new CallNode ({type: "call", fn: d[0], arguments: d[2]})
d => new CallNode ({functionName: d[0].name, args: d[2]})
},
{"name": "callExpression", "symbols": ["parenthesizedExpression"], "postprocess": id},
{"name": "callExpression", "symbols": ["indexExpression"], "postprocess": id},
{"name": "arguments$ebnf$1$subexpression$1", "symbols": ["expression"], "postprocess": d => d[0]},
{"name": "arguments$ebnf$1", "symbols": ["arguments$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "arguments$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "arguments$ebnf$2", "symbols": []},
{"name": "arguments$ebnf$2$subexpression$1", "symbols": ["_", {"literal":","}, "_", "expression"], "postprocess": d => d[3]},
{"name": "arguments$ebnf$2$subexpression$1", "symbols": ["_", (lexer.has("separator") ? {type: "separator"} : separator), "_", "expression"], "postprocess": d => d[3]},
{"name": "arguments$ebnf$2", "symbols": ["arguments$ebnf$2", "arguments$ebnf$2$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "arguments", "symbols": [{"literal":"("}, "_", "arguments$ebnf$1", "arguments$ebnf$2", "_", {"literal":")"}], "postprocess":
d => [d[2], ...d[3]]
},
{"name": "indexExpression", "symbols": ["arrayExpression", {"literal":"["}, "_", "expression", "_", {"literal":"]"}], "postprocess": d => new IndexNode ({array: d[0], index: d[3]})},
{"name": "indexExpression", "symbols": ["arrayExpression"], "postprocess": id},
{"name": "arrayExpression$ebnf$1$subexpression$1", "symbols": ["expression"], "postprocess": d => d[0]},
{"name": "arrayExpression$ebnf$1", "symbols": ["arrayExpression$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "arrayExpression$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "arrayExpression$ebnf$2", "symbols": []},
{"name": "arrayExpression$ebnf$2$subexpression$1", "symbols": ["_", (lexer.has("separator") ? {type: "separator"} : separator), "_", "expression"], "postprocess": d => d[3]},
{"name": "arrayExpression$ebnf$2", "symbols": ["arrayExpression$ebnf$2", "arrayExpression$ebnf$2$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "arrayExpression", "symbols": [{"literal":"["}, "_", "arrayExpression$ebnf$1", "arrayExpression$ebnf$2", "_", {"literal":"]"}], "postprocess":
d => new ArrayNode({values: d[2] ? [d[2], ...d[3]] : []})
},
{"name": "parenthesizedExpression", "symbols": [{"literal":"("}, "_", "expression", "_", {"literal":")"}], "postprocess": d => d[2]},
{"name": "parenthesizedExpression", "symbols": ["valueExpression"], "postprocess": id},
{"name": "arrayExpression", "symbols": ["parenthesizedExpression"], "postprocess": id},
{"name": "parenthesizedExpression", "symbols": [{"literal":"("}, "_", "expression", "_", {"literal":")"}], "postprocess": d => new ParenthesisNode({content: d[2]})},
{"name": "parenthesizedExpression", "symbols": ["accessorExpression"], "postprocess": id},
{"name": "accessorExpression$ebnf$1$subexpression$1", "symbols": [{"literal":"."}, "name"], "postprocess": d => d[1].name},
{"name": "accessorExpression$ebnf$1", "symbols": ["accessorExpression$ebnf$1$subexpression$1"]},
{"name": "accessorExpression$ebnf$1$subexpression$2", "symbols": [{"literal":"."}, "name"], "postprocess": d => d[1].name},
{"name": "accessorExpression$ebnf$1", "symbols": ["accessorExpression$ebnf$1", "accessorExpression$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "accessorExpression", "symbols": ["name", "accessorExpression$ebnf$1"], "postprocess": d=> new AccessorNode({name: d[0], path: d[1]})},
{"name": "accessorExpression", "symbols": ["valueExpression"], "postprocess": id},
{"name": "valueExpression", "symbols": ["name"], "postprocess": id},
{"name": "valueExpression", "symbols": ["number"], "postprocess": id},
{"name": "valueExpression", "symbols": ["string"], "postprocess": id},
{"name": "number", "symbols": [(lexer.has("number") ? {type: "number"} : number)], "postprocess": d => new ConstantNode({value: d[0].value, type: 'number'})},
{"name": "valueExpression", "symbols": ["boolean"], "postprocess": id},
{"name": "number", "symbols": [(lexer.has("number") ? {type: "number"} : number)], "postprocess": d => new ConstantNode({value: +d[0].value, type: 'number'})},
{"name": "name", "symbols": [(lexer.has("name") ? {type: "name"} : name)], "postprocess": d => new SymbolNode({name: d[0].value})},
{"name": "string", "symbols": [(lexer.has("string") ? {type: "string"} : string)], "postprocess": d => new ConstantNode({value: d[0].value, type: 'string'})},
{"name": "boolean", "symbols": [{"literal":"true"}], "postprocess": d => new ConstantNode({value: true, type: 'boolean'})},
{"name": "boolean", "symbols": [{"literal":"false"}], "postprocess": d => new ConstantNode({value: false, type: 'boolean'})},
{"name": "_", "symbols": []},
{"name": "_", "symbols": [(lexer.has("space") ? {type: "space"} : space)], "postprocess": nuller}
];
let ParserStart = "ifStatement";
let ParserStart = "spacedExpression";
export default { Lexer, ParserRules, ParserStart };

View File

@@ -1,10 +1,18 @@
@preprocessor esmodule
@{%
import AccessorNode from '/imports/parser/parseTree/AccessorNode.js';
import ArrayNode from '/imports/parser/parseTree/ArrayNode.js';
import CallNode from '/imports/parser/parseTree/CallNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
import IfNode from '/imports/parser/parseTree/IfNode.js';
import IfNode from '/imports/parser/parseTree/IfNode.js';
import IndexNode from '/imports/parser/parseTree/IndexNode.js';
import OperatorNode from '/imports/parser/parseTree/OperatorNode.js';
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
import ParenthesisNode from '/imports/parser/parseTree/ParenthesisNode.js';
import RollNode from '/imports/parser/parseTree/RollNode.js';
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
import UnaryOperatorNode from '/imports/parser/parseTree/UnaryOperatorNode.js';
import NotOperatorNode from '/imports/parser/parseTree/NotOperatorNode.js';
import moo from 'moo';
const lexer = moo.compile({
@@ -14,26 +22,30 @@
value: s => s.slice(1, -1),
},
name: {
match: /[a-zA-Z]+\w*?/,
match: /[a-zA-Z_]*[a-ce-zA-Z_][a-zA-Z0-9_]*/,
type: moo.keywords({
'keywords': ['if', 'else', 'd'],
'keywords': ['true', 'false'],
}),
},
space: {
match: /\s+/,
lineBreaks: true,
},
separators: [',', '.'],
diceOperator: ['d'],
separator: [',', ';'],
period: ['.'],
ifOperator: ['?'],
elseOperator: [':'],
multiplicativeOperator: ['*', '/'],
exponentOperator: ['^'],
additiveOperator: ['+', '-'],
unaryOperator: ['-'],
andOperator: ['&', '&&'],
orOperator: ['|', '||'],
stringDelimiters: ['\"', '\''],
equalityOperator: ['=', '==', '===', '!=', '!=='],
notOperator: ['!'],
relationalOperator: ['>', '<', '>=', '<='],
brackets: ['(', ')', '{', '}'],
brackets: ['(', ')', '{', '}', '[', ']'],
});
function nuller() { return null; }
@@ -50,21 +62,16 @@
# Use the Moo lexer
@lexer lexer
ifStatement ->
"if" _ "(" _ expression _ ")" _ ifStatement _ "else" _ ifStatement {%
d => new IfNode({condition: d[4], consequent: d[8], alternative: d[12]})
%}
| expression {% id %}
spacedExpression ->
_ expression _ {% d => d[1] %}
expression ->
equalityExpression {% d => d[0] %}
ifStatement {% id %}
equalityExpression ->
equalityExpression _ %equalityOperator _ relationalExpression {% d => operator(d, 'equality') %}
| relationalExpression {% id %}
relationalExpression ->
relationalExpression _ %relationalOperator _ orExpression {% d => operator(d, 'relation') %}
ifStatement ->
orExpression _ %ifOperator _ orExpression _ %elseOperator _ ifStatement {%
d => new IfNode({condition: d[0], consequent: d[4], alternative: d[8]})
%}
| orExpression {% id %}
orExpression ->
@@ -72,7 +79,15 @@ orExpression ->
| andExpression {% id %}
andExpression ->
andExpression _ %andOperator _ additiveExpression {% d => operator(d, 'and') %}
andExpression _ %andOperator _ equalityExpression {% d => operator(d, 'and') %}
| equalityExpression {% id %}
equalityExpression ->
equalityExpression _ %equalityOperator _ relationalExpression {% d => operator(d, 'equality') %}
| relationalExpression {% id %}
relationalExpression ->
relationalExpression _ %relationalOperator _ additiveExpression {% d => operator(d, 'relation') %}
| additiveExpression {% id %}
additiveExpression ->
@@ -84,36 +99,63 @@ multiplicativeExpression ->
| rollExpression {% id %}
rollExpression ->
rollExpression _ "d" _ exponentExpression {% d => operator(d, 'roll') %}
rollExpression _ %diceOperator _ exponentExpression {% d => new RollNode({left: d[0], right: d[4]}) %}
| singleRollExpression {% id %}
singleRollExpression ->
"d" _ singleRollExpression {% d => new RollNode({left: new ConstantNode({value: 1, type: 'number'}), right: d[2]}) %}
| exponentExpression {% id %}
exponentExpression ->
callExpression _ %exponentOperator _ exponentExpression {% d => operator(d, 'exponent') %}
| unaryExpression {% id %}
unaryExpression ->
%additiveOperator _ unaryExpression {% d => new UnaryOperatorNode({operator: d[0].value, right: d[2]})%}
| notExpression {% id %}
notExpression ->
%notOperator _ notExpression {% d => new NotOperatorNode({right: d[2]})%}
| callExpression {% id %}
callExpression ->
name _ arguments {%
d => new CallNode ({type: "call", fn: d[0], arguments: d[2]})
d => new CallNode ({functionName: d[0].name, args: d[2]})
%}
| indexExpression {% id %}
arguments ->
"(" _ (expression {% d => d[0] %}):? ( _ %separator _ expression {% d => d[3] %} ):* _ ")" {%
d => [d[2], ...d[3]]
%}
indexExpression ->
arrayExpression "[" _ expression _ "]" {% d => new IndexNode ({array: d[0], index: d[3]}) %}
| arrayExpression {% id %}
arrayExpression ->
"[" _ (expression {% d => d[0] %}):? ( _ %separator _ expression {% d => d[3] %} ):* _ "]" {%
d => new ArrayNode({values: d[2] ? [d[2], ...d[3]] : []})
%}
| parenthesizedExpression {% id %}
arguments ->
"(" _ (expression {% d => d[0] %}):? ( _ "," _ expression {% d => d[3] %} ):* _ ")" {%
d => [d[2], ...d[3]]
%}
parenthesizedExpression ->
"(" _ expression _ ")" {% d => d[2] %}
"(" _ expression _ ")" {% d => new ParenthesisNode({content: d[2]}) %}
| accessorExpression {% id %}
accessorExpression ->
name ( "." name {% d => d[1].name %} ):+ {% d=> new AccessorNode({name: d[0], path: d[1]}) %}
| valueExpression {% id %}
valueExpression ->
name {% id %}
| number {% id %}
| string {% id %}
| boolean {% id %}
# A number or a function of a number
number ->
%number {% d => new ConstantNode({value: d[0].value, type: 'number'}) %}
%number {% d => new ConstantNode({value: +d[0].value, type: 'number'}) %}
name ->
%name {% d => new SymbolNode({name: d[0].value}) %}
@@ -121,6 +163,10 @@ name ->
string ->
%string {% d => new ConstantNode({value: d[0].value, type: 'string'}) %}
boolean ->
"true" {% d => new ConstantNode({value: true, type: 'boolean'}) %}
| "false" {% d => new ConstantNode({value: false, type: 'boolean'}) %}
_ ->
null
| %space {% nuller %}

View File

@@ -0,0 +1,47 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class AccessorNode extends ParseNode {
constructor({name, path}) {
super(...arguments);
this.name = name;
this.path = path;
}
compile(scope){
let value = scope && scope[this.name];
// For objects, get their value
this.path.forEach(name => {
if (value === undefined) return;
value = value[name];
});
let type = typeof value;
if (type === 'string' || type === 'number' || type === 'boolean'){
return new ConstantNode({value, type, previousNodes: [this]});
} else if (type === 'undefined'){
return new AccessorNode({
name: this.name,
path: this.path,
});
} else {
throw new Meteor.Error(`Unexpected case: ${this.name} resolved to ${value}`);
}
}
reduce(scope, context){
let result = this.compile(scope, context);
if (result instanceof AccessorNode){
if (context) context.storeError({
type: 'info',
message: `${result.toString()} not found, set to 0`
});
return new ConstantNode({
type: 'number',
value: 0,
});
} else {
return result;
}
}
toString(){
return `${this.name}.${this.path.join('.')}`;
}
}

View File

@@ -0,0 +1,19 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
export default class ArrayNode extends ParseNode {
constructor({values}) {
super(...arguments);
this.values = values;
}
resolve(fn, scope, context){
let values = this.values.map(node => node[fn](scope, context));
return new ArrayNode({values});
}
toString(){
return `[${this.values.map(node => node.toString()).join(', ')}]`;
}
traverse(fn){
fn(this);
this.values.forEach(value => value.traverse(fn));
}
}

View File

@@ -1 +1,73 @@
//TODO
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
import functions from '/imports/parser/functions.js';
export default class CallNode extends ParseNode {
constructor({functionName, args}) {
super(...arguments);
this.functionName = functionName;
this.args = args;
}
resolve(fn, scope, context){
let func = functions[this.functionName];
if (!func) return new ErrorNode({
node: this,
error: `${this.functionName} is not a function`,
context,
});
let args = castArgsToType({fn, scope, context, args: this.args, type: func.argumentType});
if (args.failed){
if (fn === 'reduce'){
return new ErrorNode({
node: this,
error: 'Could not convert all arguments to the correct type',
context,
});
} else {
return new CallNode({
functionName: this.functionName,
args: args,
});
}
} else {
try {
let value = func.fn.apply(null, args);
return new ConstantNode({
value,
type: 'number',
previousNodes: [this],
});
} catch (error) {
return new ErrorNode({
node: this,
error,
context,
});
}
}
}
toString(){
return `${this.functionName}(${this.args.map(node => node.toString()).join(', ')})`;
}
traverse(fn){
fn(this);
this.args.forEach(arg => arg.traverse(fn));
}
}
function castArgsToType({fn, scope, context, args, type}){
let resolvedArgs = args.map(node => node[fn](scope, context))
let result = [];
if (type === 'number'){
resolvedArgs.forEach(node => {
if (node.isNumber){
result.push(node.value);
} else {
resolvedArgs.failed = true;
}
})
}
if (resolvedArgs.failed) return resolvedArgs;
return result;
}

View File

@@ -1,21 +1,22 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
export default class ConstantNode extends ParseNode {
constructor({value, type, errors}){
super();
// string, number, boolean, numberArray, uncompiledNode
constructor({value, type}){
super(...arguments);
// string, number, boolean, uncompiledNode
this.type = type;
this.value = value;
if (errors) this.errors = errors;
}
compile(){
return this;
}
reduce(){
if (this.type === 'numberArray'){
return this.value.reduce((total, num) => total + num, 0);
} else {
return this;
}
toString(){
return `${this.value}`;
}
get isNumber(){
return this.type === 'number';
}
get isInteger(){
return this.type === 'number' && Number.isInteger(this.value);
}
}

View File

@@ -0,0 +1,21 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
export default class ErrorNode extends ParseNode {
constructor({node, error, context}) {
super(...arguments);
this.node = node;
this.error = error;
if (context){
context.storeError({
type: 'error',
message: error,
});
}
}
compile(){
return this;
}
toString(){
return '###';
}
}

View File

@@ -3,38 +3,35 @@ import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class IfNode extends ParseNode {
constructor({condition, consequent, alternative}){
super();
super(...arguments);
this.condition = condition;
this.consequent = consequent;
this.alternative = alternative;
}
compile(){
let condition = this.condition.compile();
let consequent = this.consequent.compile();
let alternative = this.alternative.compile();
if (
condition.type !== 'string' &&
condition.type !== 'number' &&
condition.type !== 'boolean'
){
// Handle unresolved condition
return new ConstantNode({
value: `if (${condition.value}) ${consequent.value} else ${alternative.value}`,
type: 'uncompiledNode',
errors: [
...condition.errors,
...consequent.errors,
...alternative.errors,
],
});
} else {
// So long as the condition reolves, return the correct alternative,
// even if it's unresolved
toString(){
let {condition, consequent, alternative} = this;
return `${condition.toString()} ? ${consequent.toString()} : ${alternative.toString()}`
}
resolve(fn, scope, context){
let condition = this.condition[fn](scope, context);
if (condition instanceof ConstantNode){
if (condition.value){
return consequent;
return this.consequent[fn](scope, context);
} else {
return alternative;
return this.alternative[fn](scope, context);
}
} else {
return new IfNode({
condition: condition,
consequent: this.consequent,
alternative: this.alternative,
});
}
}
traverse(fn){
fn(this);
this.condition.traverse(fn);
this.consequent.traverse(fn);
this.alternative.traverse(fn);
}
}

View File

@@ -0,0 +1,32 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
export default class IndexNode extends ParseNode {
constructor({array, index}) {
super(...arguments);
this.array = array;
this.index = index;
}
resolve(fn, scope, context){
let index = this.index[fn](scope, context);
if (index.isInteger){
let selection = this.array.values[index.value - 1];
if (selection){
let result = selection[fn](scope, context);
return result;
}
}
return new IndexNode({
index,
array: this.array[fn](scope, context),
previousNodes: [this],
});
}
toString(){
return `${this.array.toString()}[${this.index.toString()}]`;
}
traverse(fn){
fn(this);
this.array.traverse(fn);
this.index.traverse(fn);
}
}

View File

@@ -0,0 +1,31 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class NotOperatorNode extends ParseNode {
constructor({right}) {
super(...arguments);
this.right = right;
}
resolve(fn, scope, context){
let rightNode = this.right[fn](scope, context);
if (!(rightNode instanceof ConstantNode)){
return new NotOperatorNode({
right: rightNode,
});
}
let right = rightNode.value;
let result = !right;
return new ConstantNode({
value: result,
type: typeof result,
});
}
toString(){
let {right} = this;
return `!${right.toString()}`;
}
traverse(fn){
fn(this);
this.right.traverse(fn);
}
}

View File

@@ -1,11 +1,62 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class OperatorNode extends ParseNode {
constructor({left, right, operator, fn}) {
super();
super(...arguments);
this.left = left;
this.right = right;
this.fn = fn;
this.operator = operator;
}
resolve(fn, scope, context){
let leftNode = this.left[fn](scope, context);
let rightNode = this.right[fn](scope, context);
let left, right;
if (!(leftNode instanceof ConstantNode) || !(rightNode instanceof ConstantNode)){
return new OperatorNode({
left: leftNode,
right: rightNode,
operator: this.operator,
fn: this.fn,
});
} else {
left = leftNode.value;
right = rightNode.value;
}
let result;
switch(this.operator){
case '+': result = left + right; break;
case '-': result = left - right; break;
case '*': result = left * right; break;
case '/': result = left / right; break;
case '^': result = Math.pow(left, right); break;
case '&':
case '&&': result = left && right; break;
case '|':
case '||': result = left || right; break;
case '=':
case '==': result = left == right; break;
case '===': result = left === right; break;
case '!=': result = left != right; break;
case '!==': result = left !== right; break;
case '>': result = left > right; break;
case '<': result = left < right; break;
case '>=': result = left >= right; break;
case '<=': result = left <= right; break;
}
return new ConstantNode({
value: result,
type: typeof result,
});
}
toString(){
let {left, right, operator} = this;
return `${left.toString()} ${operator} ${right.toString()}`;
}
traverse(fn){
fn(this);
this.left.traverse(fn);
this.right.traverse(fn);
}
}

View File

@@ -0,0 +1,27 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
export default class ParenthesisNode extends ParseNode {
constructor({content}) {
super(...arguments);
this.content = content;
}
resolve(fn, scope, context){
let content = this.content[fn](scope, context);
if (
content.constructor.name === 'IfNode' ||
content.constructor.name === 'OperatorNode' ||
content.constructor.name === 'RollNode'
){
return new ParenthesisNode({content, previousNodes: [this]});
} else {
return content;
}
}
toString(){
return `(${this.content.toString()})`;
}
traverse(fn){
fn(this);
this.content.traverse(fn);
}
}

View File

@@ -1,14 +1,33 @@
export default class ParseNode {
// Compiling a node must return a ConstantNode
compile(){
throw new Meteor.Error('Compile not implemented on ' + this);
toString(){
throw new Meteor.Error('toString not implemented on ' + this.constructor.name);
}
compile(scope, context){
// Returns a ParseNode, a ConstantNode if possible
if(this.resolve) {
return this.resolve('compile', scope, context);
} else {
throw new Meteor.Error('Compile not implemented on ' + this.constructor.name);
}
}
// Compile, but turn rolls into arrays
roll(){
return this.compile();
roll(scope, context){
if (this.resolve){
return this.resolve('roll', scope, context);
} else {
return this.compile(scope, context);
}
}
// Compile, turn rolls into arrays, and reduce those arrays into single values
reduce(){
return this.compileAndRoll()
reduce(scope, context){
if (this.resolve){
return this.resolve('reduce', scope, context);
} else {
return this.roll(scope, context);
}
}
// If traverse isn't implemented, just apply it to the current node
traverse(fn){
fn(this);
}
}

View File

@@ -0,0 +1,22 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class RollArrayNode extends ParseNode {
constructor({values}) {
super(...arguments);
this.values = values;
}
compile(){
return this;
}
toString(){
return `[${this.values.join(', ')}]`;
}
reduce(){
let total = this.values.reduce((a, b) => a + b);
return new ConstantNode({
value: total,
type: 'number',
});
}
}

Some files were not shown because too many files have changed in this diff Show More