()
| 14 | const { Engine, Fact } = require('json-rules-engine') |
| 15 | |
| 16 | async function start () { |
| 17 | /** |
| 18 | * Setup a new engine |
| 19 | */ |
| 20 | const engine = new Engine([], { replaceFactsInEventParams: true }) |
| 21 | |
| 22 | // in-memory "database" |
| 23 | let currentHighScore = null |
| 24 | const currentHighScoreFact = new Fact('currentHighScore', () => currentHighScore) |
| 25 | |
| 26 | /** |
| 27 | * Rule for when you've gotten the high score |
| 28 | * event will include your score and initials. |
| 29 | */ |
| 30 | const highScoreRule = { |
| 31 | conditions: { |
| 32 | any: [ |
| 33 | { |
| 34 | fact: 'currentHighScore', |
| 35 | operator: 'equal', |
| 36 | value: null |
| 37 | }, |
| 38 | { |
| 39 | fact: 'score', |
| 40 | operator: 'greaterThan', |
| 41 | value: { |
| 42 | fact: 'currentHighScore', |
| 43 | path: '$.score' |
| 44 | } |
| 45 | } |
| 46 | ] |
| 47 | }, |
| 48 | event: { |
| 49 | type: 'highscore', |
| 50 | params: { |
| 51 | initials: { fact: 'initials' }, |
| 52 | score: { fact: 'score' } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Rule for when the game is over and you don't have the high score |
| 59 | * event will include the previous high score |
| 60 | */ |
| 61 | const gameOverRule = { |
| 62 | conditions: { |
| 63 | all: [ |
| 64 | { |
| 65 | fact: 'score', |
| 66 | operator: 'lessThanInclusive', |
| 67 | value: { |
| 68 | fact: 'currentHighScore', |
| 69 | path: '$.score' |
| 70 | } |
| 71 | } |
| 72 | ] |
| 73 | }, |
no test coverage detected
searching dependent graphs…