script-engine-lib 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/.eslintrc.js +177 -0
  2. package/.vscode/launch.json +15 -0
  3. package/.vscode/settings.json +15 -0
  4. package/dist/decorators/action-before-testing.d.ts +2 -0
  5. package/dist/decorators/action-before-testing.js +23 -0
  6. package/dist/decorators/action-before-testing.js.map +1 -0
  7. package/dist/decorators/action-before-testing.test.d.ts +1 -0
  8. package/dist/decorators/action-before-testing.test.js +82 -0
  9. package/dist/decorators/action-before-testing.test.js.map +1 -0
  10. package/dist/engine/script-engine.d.ts +35 -0
  11. package/dist/engine/script-engine.js +175 -0
  12. package/dist/engine/script-engine.js.map +1 -0
  13. package/dist/engine/script-engine.test.d.ts +1 -0
  14. package/dist/engine/script-engine.test.js +1277 -0
  15. package/dist/engine/script-engine.test.js.map +1 -0
  16. package/dist/engine/story-engine.d.ts +35 -0
  17. package/dist/engine/story-engine.js +175 -0
  18. package/dist/engine/story-engine.js.map +1 -0
  19. package/dist/engine/story-engine.test.d.ts +1 -0
  20. package/dist/engine/story-engine.test.js +1277 -0
  21. package/dist/engine/story-engine.test.js.map +1 -0
  22. package/dist/index.d.ts +5 -0
  23. package/dist/index.js +29 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/interfaces.d.ts +51 -0
  26. package/dist/interfaces.js +27 -0
  27. package/dist/interfaces.js.map +1 -0
  28. package/dist/simulator/decorators.d.ts +2 -0
  29. package/dist/simulator/decorators.js +23 -0
  30. package/dist/simulator/decorators.js.map +1 -0
  31. package/dist/simulator/decorators.test.d.ts +1 -0
  32. package/dist/simulator/decorators.test.js +81 -0
  33. package/dist/simulator/decorators.test.js.map +1 -0
  34. package/dist/simulator/event-branches-map.d.ts +16 -0
  35. package/dist/simulator/event-branches-map.js +114 -0
  36. package/dist/simulator/event-branches-map.js.map +1 -0
  37. package/dist/simulator/event-branches-map.test.d.ts +1 -0
  38. package/dist/simulator/event-branches-map.test.js +120 -0
  39. package/dist/simulator/event-branches-map.test.js.map +1 -0
  40. package/dist/simulator/event-test-simulator.d.ts +42 -0
  41. package/dist/simulator/event-test-simulator.js +373 -0
  42. package/dist/simulator/event-test-simulator.js.map +1 -0
  43. package/dist/simulator/event-test-simulator.test.d.ts +1 -0
  44. package/dist/simulator/event-test-simulator.test.js +1960 -0
  45. package/dist/simulator/event-test-simulator.test.js.map +1 -0
  46. package/dist/simulator/execution-history.d.ts +6 -0
  47. package/dist/simulator/execution-history.js +22 -0
  48. package/dist/simulator/execution-history.js.map +1 -0
  49. package/dist/simulator/helpers/event-branches-map.d.ts +16 -0
  50. package/dist/simulator/helpers/event-branches-map.js +114 -0
  51. package/dist/simulator/helpers/event-branches-map.js.map +1 -0
  52. package/dist/simulator/helpers/event-branches-map.test.d.ts +1 -0
  53. package/dist/simulator/helpers/event-branches-map.test.js +120 -0
  54. package/dist/simulator/helpers/event-branches-map.test.js.map +1 -0
  55. package/dist/simulator/helpers/execution-history.d.ts +6 -0
  56. package/dist/simulator/helpers/execution-history.js +22 -0
  57. package/dist/simulator/helpers/execution-history.js.map +1 -0
  58. package/dist/simulator/helpers/script-branches-map.d.ts +16 -0
  59. package/dist/simulator/helpers/script-branches-map.js +114 -0
  60. package/dist/simulator/helpers/script-branches-map.js.map +1 -0
  61. package/dist/simulator/helpers/script-branches-map.test.d.ts +1 -0
  62. package/dist/simulator/helpers/script-branches-map.test.js +120 -0
  63. package/dist/simulator/helpers/script-branches-map.test.js.map +1 -0
  64. package/dist/simulator/script-test-simulator.d.ts +42 -0
  65. package/dist/simulator/script-test-simulator.js +373 -0
  66. package/dist/simulator/script-test-simulator.js.map +1 -0
  67. package/dist/simulator/script-test-simulator.test.d.ts +1 -0
  68. package/dist/simulator/script-test-simulator.test.js +1960 -0
  69. package/dist/simulator/script-test-simulator.test.js.map +1 -0
  70. package/dist/structured-text/index.d.ts +9 -0
  71. package/dist/structured-text/index.js +8 -0
  72. package/dist/structured-text/index.js.map +1 -0
  73. package/dist/structured-text/rich-text-separator.d.ts +48 -0
  74. package/dist/structured-text/rich-text-separator.js +146 -0
  75. package/dist/structured-text/rich-text-separator.js.map +1 -0
  76. package/dist/structured-text/rich-text-separator.test.d.ts +1 -0
  77. package/dist/structured-text/rich-text-separator.test.js +166 -0
  78. package/dist/structured-text/rich-text-separator.test.js.map +1 -0
  79. package/dist/structured-text/rich-text-validator.d.ts +19 -0
  80. package/dist/structured-text/rich-text-validator.js +73 -0
  81. package/dist/structured-text/rich-text-validator.js.map +1 -0
  82. package/dist/structured-text/rich-text-validator.test.d.ts +1 -0
  83. package/dist/structured-text/rich-text-validator.test.js +59 -0
  84. package/dist/structured-text/rich-text-validator.test.js.map +1 -0
  85. package/dist/test.spec.js.map +1 -0
  86. package/dist/test.test.d.ts +1 -0
  87. package/dist/test.test.js +9 -0
  88. package/dist/test.test.js.map +1 -0
  89. package/package.json +47 -0
  90. package/tsconfig.json +24 -0
  91. package/vitest.config.ts +1 -0
package/.eslintrc.js ADDED
@@ -0,0 +1,177 @@
1
+ module.exports = {
2
+ env: {
3
+ browser: true,
4
+ es6: true,
5
+ node: true
6
+ },
7
+ extends: [
8
+ 'eslint:recommended',
9
+ 'plugin:@typescript-eslint/eslint-recommended',
10
+ 'plugin:@typescript-eslint/recommended',
11
+ 'plugin:import/recommended',
12
+ 'plugin:import/electron',
13
+ 'plugin:import/typescript',
14
+ 'plugin:@typescript-eslint/eslint-recommended',
15
+ 'plugin:@typescript-eslint/recommended',
16
+ 'prettier'
17
+ ],
18
+ parser: '@typescript-eslint/parser',
19
+ parserOptions: {
20
+ sourceType: 'module'
21
+ },
22
+ root: true,
23
+ env: {
24
+ node: true,
25
+ jest: true
26
+ },
27
+ plugins: ['@typescript-eslint', '@typescript-eslint/eslint-plugin', 'no-null'],
28
+ ignorePatterns: ['**/*.js', 'vite.config.ts', 'out/**/*', 'dist/**/*', 'node_modules/**/*'],
29
+ rules: {
30
+ 'no-null/no-null': 2,
31
+ '@typescript-eslint/adjacent-overload-signatures': 'error',
32
+ '@typescript-eslint/ban-ts-comment': 'off',
33
+ '@typescript-eslint/ban-types': 'error',
34
+ '@typescript-eslint/consistent-type-definitions': 'error',
35
+ '@typescript-eslint/dot-notation': 'off',
36
+ '@typescript-eslint/explicit-function-return-type': [
37
+ "warn",
38
+ { "allowExpressions": true }
39
+ ],
40
+ '@typescript-eslint/explicit-member-accessibility': [
41
+ 'off',
42
+ {
43
+ accessibility: 'explicit'
44
+ }
45
+ ],
46
+ '@typescript-eslint/explicit-module-boundary-types': 'warn',
47
+ '@typescript-eslint/indent': ['off', 2],
48
+ '@typescript-eslint/interface-name-prefix': 'off',
49
+ '@typescript-eslint/member-delimiter-style': [
50
+ 'error',
51
+ {
52
+ multiline: {
53
+ delimiter: 'semi',
54
+ requireLast: true
55
+ },
56
+ singleline: {
57
+ delimiter: 'semi',
58
+ requireLast: false
59
+ }
60
+ }
61
+ ],
62
+ '@typescript-eslint/naming-convention': [
63
+ 'error',
64
+ {
65
+ selector: 'variable',
66
+ modifiers: ['const'],
67
+ format: ['PascalCase', 'UPPER_CASE']
68
+ }
69
+ ],
70
+ '@typescript-eslint/no-array-constructor': 'error',
71
+ '@typescript-eslint/no-empty-function': 'off',
72
+ '@typescript-eslint/no-empty-interface': 'error',
73
+ '@typescript-eslint/no-explicit-any': 'off',
74
+ '@typescript-eslint/no-extra-non-null-assertion': 'error',
75
+ '@typescript-eslint/no-inferrable-types': [
76
+ 'error',
77
+ {
78
+ ignoreParameters: true,
79
+ ignoreProperties: true
80
+ }
81
+ ],
82
+ '@typescript-eslint/no-misused-new': 'error',
83
+ '@typescript-eslint/no-namespace': 'error',
84
+ '@typescript-eslint/no-non-null-asserted-optional-chain': 'error',
85
+ '@typescript-eslint/no-non-null-assertion': 'off',
86
+ '@typescript-eslint/no-this-alias': 'error',
87
+ '@typescript-eslint/no-unused-expressions': 'off',
88
+ '@typescript-eslint/no-unused-vars': 'off',
89
+ '@typescript-eslint/no-var-requires': 'error',
90
+ '@typescript-eslint/prefer-as-const': 'error',
91
+ '@typescript-eslint/prefer-function-type': 'error',
92
+ '@typescript-eslint/prefer-namespace-keyword': 'error',
93
+ '@typescript-eslint/quotes': ['error', 'single', { allowTemplateLiterals: true }],
94
+ '@typescript-eslint/semi': ['error', 'always'],
95
+ '@typescript-eslint/triple-slash-reference': 'error',
96
+ '@typescript-eslint/type-annotation-spacing': 'error',
97
+ '@typescript-eslint/unified-signatures': 'error',
98
+ '@typescript-eslint/no-shadow': 'warn',
99
+ 'arrow-body-style': 'error',
100
+ 'arrow-parens': [2, 'as-needed'],
101
+ 'brace-style': ['error', '1tbs'],
102
+ 'constructor-super': 'error',
103
+ curly: 'error',
104
+ 'eol-last': 'error',
105
+ eqeqeq: ['error', 'smart'],
106
+ 'guard-for-in': 'off',
107
+ 'id-blacklist': 'off',
108
+ 'id-match': 'off',
109
+ 'max-len': [
110
+ 'error',
111
+ {
112
+ code: 170
113
+ }
114
+ ],
115
+ 'no-array-constructor': 'off',
116
+ 'no-bitwise': 'error',
117
+ 'no-caller': 'error',
118
+ 'no-console': [
119
+ 'error',
120
+ {
121
+ allow: [
122
+ 'log',
123
+ 'warn',
124
+ 'dir',
125
+ 'timeLog',
126
+ 'assert',
127
+ 'clear',
128
+ 'count',
129
+ 'countReset',
130
+ 'group',
131
+ 'groupEnd',
132
+ 'table',
133
+ 'info',
134
+ 'dirxml',
135
+ 'error',
136
+ 'groupCollapsed',
137
+ 'Console',
138
+ 'profile',
139
+ 'profileEnd',
140
+ 'timeStamp',
141
+ 'context'
142
+ ]
143
+ }
144
+ ],
145
+ 'no-debugger': 'error',
146
+ 'no-empty': 'off',
147
+ 'no-empty-function': 'off',
148
+ 'no-eval': 'error',
149
+ 'no-fallthrough': 'error',
150
+ 'no-new-wrappers': 'error',
151
+ 'no-restricted-imports': 'error',
152
+ 'no-shadow': 'off',
153
+ 'no-throw-literal': 'error',
154
+ 'no-trailing-spaces': 'error',
155
+ 'no-undef-init': 'error',
156
+ 'no-underscore-dangle': 'off',
157
+ 'no-unused-labels': 'error',
158
+ 'no-unused-vars': 'off',
159
+ 'no-var': 'error',
160
+ 'prefer-const': 'off',
161
+ radix: 'off',
162
+ semi: [
163
+ 'error',
164
+ 'always',
165
+ {
166
+ omitLastInOneLineBlock: true
167
+ }
168
+ ],
169
+ 'spaced-comment': [
170
+ 'error',
171
+ 'always',
172
+ {
173
+ markers: ['/']
174
+ }
175
+ ]
176
+ }
177
+ };
@@ -0,0 +1,15 @@
1
+ {
2
+ "configurations": [
3
+ {
4
+ "name": "Unit Tests",
5
+ "type": "node",
6
+ "request": "launch",
7
+ "autoAttachChildProcesses": true,
8
+ "skipFiles": ["<node_internals>/**", "**/node_modules/**"],
9
+ "program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
10
+ "args": ["run"],
11
+ "smartStep": true,
12
+ "console": "integratedTerminal"
13
+ }
14
+ ]
15
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "javascript.preferences.importModuleSpecifier": "relative",
3
+ "typescript.preferences.importModuleSpecifier": "relative",
4
+ "workbench.activityBar.location": "top",
5
+ "window.zoomLevel": 0,
6
+ "editor.stickyScroll.enabled": false,
7
+ "workbench.tree.enableStickyScroll": false,
8
+ "terminal.integrated.shellIntegration.decorationsEnabled": "never",
9
+ "editor.wrappingIndent": "indent",
10
+ "editor.codeActionsOnSave": {
11
+ "source.organizeImports": "always"
12
+ },
13
+ "typescript.tsdk": "node_modules/typescript/lib",
14
+ "typescript.enablePromptUseWorkspaceTsdk": true
15
+ }
@@ -0,0 +1,2 @@
1
+ export declare const ActionBeforeTestingFlag = "actionsBeforeTesting";
2
+ export declare function ActionBeforeTesting(): (_: unknown, propertyKey: string, descriptor: PropertyDescriptor) => void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ActionBeforeTesting = exports.ActionBeforeTestingFlag = void 0;
4
+ const helpers_lib_1 = require("helpers-lib");
5
+ exports.ActionBeforeTestingFlag = 'actionsBeforeTesting';
6
+ function ActionBeforeTesting() {
7
+ return function (_, propertyKey, descriptor) {
8
+ let originalFunction = descriptor.value;
9
+ helpers_lib_1.JSEngine.setAsJSEngineFunction(originalFunction);
10
+ descriptor.value = function (...args) {
11
+ let hasFlag = Reflect.getOwnMetadata(exports.ActionBeforeTestingFlag, this);
12
+ if (hasFlag) {
13
+ return originalFunction.apply(this, args);
14
+ }
15
+ else {
16
+ throw new Error(`"${String(propertyKey)}(...)" can only be called at "actions before testing".`);
17
+ }
18
+ };
19
+ helpers_lib_1.MetaDataHelper.carryMetaDataOfFunction(originalFunction, descriptor.value);
20
+ };
21
+ }
22
+ exports.ActionBeforeTesting = ActionBeforeTesting;
23
+ //# sourceMappingURL=action-before-testing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"action-before-testing.js","sourceRoot":"","sources":["../../src/decorators/action-before-testing.ts"],"names":[],"mappings":";;;AAAA,6CAAuD;AAE1C,QAAA,uBAAuB,GAAG,sBAAsB,CAAC;AAE9D,SAAgB,mBAAmB;IACjC,OAAO,UAAU,CAAU,EAAE,WAAmB,EAAE,UAA8B;QAC9E,IAAI,gBAAgB,GAAG,UAAU,CAAC,KAAK,CAAC;QACxC,sBAAQ,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;QAEjD,UAAU,CAAC,KAAK,GAAG,UAAU,GAAG,IAAW;YACzC,IAAI,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,+BAAuB,EAAE,IAAI,CAAC,CAAC;YACpE,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,gBAAgB,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAC5C,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC,WAAW,CAAC,wDAAwD,CAAC,CAAC;YACnG,CAAC;QACH,CAAC,CAAC;QAEF,4BAAc,CAAC,uBAAuB,CAAC,gBAAgB,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;IAC7E,CAAC,CAAC;AACJ,CAAC;AAhBD,kDAgBC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const helpers_lib_1 = require("helpers-lib");
13
+ const vitest_1 = require("vitest");
14
+ const script_test_simulator_1 = require("../simulator/script-test-simulator");
15
+ const action_before_testing_1 = require("./action-before-testing");
16
+ let SampleFunctions = class SampleFunctions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
17
+ onPlayerChoice(choices) { }
18
+ onDialog(text, speaker) { }
19
+ onEnd() { }
20
+ onScriptBranchingEnd() { }
21
+ exclusive() { }
22
+ sayHi(message) { }
23
+ };
24
+ __decorate([
25
+ (0, action_before_testing_1.ActionBeforeTesting)(),
26
+ __metadata("design:type", Function),
27
+ __metadata("design:paramtypes", []),
28
+ __metadata("design:returntype", void 0)
29
+ ], SampleFunctions.prototype, "exclusive", null);
30
+ __decorate([
31
+ (0, action_before_testing_1.ActionBeforeTesting)(),
32
+ __metadata("design:type", Function),
33
+ __metadata("design:paramtypes", [String]),
34
+ __metadata("design:returntype", void 0)
35
+ ], SampleFunctions.prototype, "sayHi", null);
36
+ SampleFunctions = __decorate([
37
+ (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
38
+ ], SampleFunctions);
39
+ (0, vitest_1.describe)('ScriptTestSimulator', () => {
40
+ let functions;
41
+ (0, vitest_1.beforeEach)(() => {
42
+ functions = new SampleFunctions();
43
+ });
44
+ (0, vitest_1.test)('sample 1 - valid call', () => {
45
+ let actionsBeforeTesting = [{ type: 'command', value: 'exclusive()' }];
46
+ let scripts = [
47
+ {
48
+ id: 'script1',
49
+ actions: [{ type: 'command', value: 'a = 1' }]
50
+ }
51
+ ];
52
+ let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
53
+ let jsEngine = new helpers_lib_1.JSEngine(functions, {});
54
+ (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting)).not.toThrow();
55
+ });
56
+ (0, vitest_1.test)('sample 2 - call during execution error', () => {
57
+ let scripts = [
58
+ {
59
+ id: 'script1',
60
+ actions: [{ type: 'command', value: 'exclusive()' }]
61
+ }
62
+ ];
63
+ let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
64
+ let jsEngine = new helpers_lib_1.JSEngine(functions, {});
65
+ (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrow('"exclusive(...)" can only be called at "actions before testing"');
66
+ });
67
+ (0, vitest_1.test)('sample 3 - wrong parameter count error', () => {
68
+ let scripts = [
69
+ {
70
+ id: 'script1',
71
+ actions: [{ type: 'command', value: 'exclusive("a")' }]
72
+ }
73
+ ];
74
+ let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
75
+ let jsEngine = new helpers_lib_1.JSEngine(functions, {});
76
+ (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrow('Error: Unexpected argument has sent to exclusive. Expected: 0, Received: 1');
77
+ });
78
+ (0, vitest_1.test)('sample 4 - preserve parameter count', () => {
79
+ (0, vitest_1.expect)(functions.sayHi.length).toBe(1);
80
+ });
81
+ });
82
+ //# sourceMappingURL=action-before-testing.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"action-before-testing.test.js","sourceRoot":"","sources":["../../src/decorators/action-before-testing.test.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,6CAAqD;AACrD,mCAA4D;AAG5D,8EAAyG;AACzG,mEAA8D;AAG9D,IAAM,eAAe,GAArB,MAAM,eAAgB,SAAQ,sDAA8B;IAC1D,cAAc,CAAC,OAA0B,IAAS,CAAC;IACnD,QAAQ,CAAC,IAAY,EAAE,OAAe,IAAS,CAAC;IAChD,KAAK,KAAU,CAAC;IAChB,oBAAoB,KAAU,CAAC;IAG/B,SAAS,KAAU,CAAC;IAGpB,KAAK,CAAC,OAAe,IAAS,CAAC;CAChC,CAAA;AAJC;IADC,IAAA,2CAAmB,GAAE;;;;gDACF;AAGpB;IADC,IAAA,2CAAmB,GAAE;;;;4CACS;AAV3B,eAAe;IADpB,IAAA,0BAAY,EAAC,EAAE,iCAAiC,EAAE,IAAI,EAAE,CAAC;GACpD,eAAe,CAWpB;AAED,IAAA,iBAAQ,EAAC,qBAAqB,EAAE,GAAG,EAAE;IACnC,IAAI,SAA0B,CAAC;IAE/B,IAAA,mBAAU,EAAC,GAAG,EAAE;QACd,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,uBAAuB,EAAE,GAAG,EAAE;QACjC,IAAI,oBAAoB,GAAiC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;QACrG,IAAI,OAAO,GAAuB;YAChC;gBACE,EAAE,EAAE,SAAS;gBACb,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;aAC/C;SACF,CAAC;QAEF,IAAI,SAAS,GAAG,IAAI,2CAAmB,CAAC,OAAO,CAAC,CAAC;QACjD,IAAI,QAAQ,GAAG,IA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E,EAAE,CAAC,CAAC,CAAC,OAAO,CACtE,4EAA4E,CAC7E,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,qCAAqC,EAAE,GAAG,EAAE;QAC/C,IAAA,eAAM,EAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { ScriptDefinition } from '../interfaces';
2
+ export interface ScriptEngineOptions {
3
+ readonly manualTestingMode?: boolean;
4
+ }
5
+ export declare abstract class ScriptEngineFunctions {
6
+ abstract onPlayerChoice(choices: readonly string[]): void;
7
+ abstract onDialog(text: string, speaker: string): void;
8
+ }
9
+ export declare enum ScriptEngineState {
10
+ running = "running",
11
+ waitingForPlayerChoice = "waitingForPlayerChoice",
12
+ finished = "finished"
13
+ }
14
+ export declare class ScriptEngine<ScriptFunctions extends ScriptEngineFunctions> {
15
+ private scriptDefinitions;
16
+ private jsEngine;
17
+ private stack;
18
+ private playerBranchingChoices;
19
+ private manualTestingMode;
20
+ get variables(): {
21
+ [key: string]: any;
22
+ };
23
+ get state(): ScriptEngineState;
24
+ private get running();
25
+ constructor(scriptDefinitions: Readonly<Record<string, ScriptDefinition>>, functions: ScriptFunctions, variables: {
26
+ [key: string]: any;
27
+ }, options?: ScriptEngineOptions);
28
+ startScript(scriptId: string): void;
29
+ next(): void;
30
+ playerChoice(choice: number): void;
31
+ private jumpToScript;
32
+ private executeAction;
33
+ private insertActionsToStack;
34
+ private validateReservedFunctions;
35
+ }
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScriptEngine = exports.ScriptEngineState = exports.ScriptEngineFunctions = void 0;
4
+ const helpers_lib_1 = require("helpers-lib");
5
+ class ScriptEngineFunctions {
6
+ }
7
+ exports.ScriptEngineFunctions = ScriptEngineFunctions;
8
+ var ScriptEngineState;
9
+ (function (ScriptEngineState) {
10
+ ScriptEngineState["running"] = "running";
11
+ ScriptEngineState["waitingForPlayerChoice"] = "waitingForPlayerChoice";
12
+ ScriptEngineState["finished"] = "finished";
13
+ })(ScriptEngineState || (exports.ScriptEngineState = ScriptEngineState = {}));
14
+ class ScriptEngine {
15
+ get variables() {
16
+ return this.jsEngine.variables;
17
+ }
18
+ get state() {
19
+ if (this.playerBranchingChoices) {
20
+ return ScriptEngineState.waitingForPlayerChoice;
21
+ }
22
+ else if (this.running) {
23
+ return ScriptEngineState.running;
24
+ }
25
+ else {
26
+ return ScriptEngineState.finished;
27
+ }
28
+ }
29
+ get running() {
30
+ return !this.stack.isEmpty;
31
+ }
32
+ constructor(scriptDefinitions, functions, variables, options) {
33
+ this.stack = new helpers_lib_1.Stack();
34
+ this.manualTestingMode = false;
35
+ this.validateReservedFunctions(functions);
36
+ this.manualTestingMode = options?.manualTestingMode ? true : false;
37
+ this.scriptDefinitions = scriptDefinitions;
38
+ this.jsEngine = new helpers_lib_1.JSEngine(functions, variables);
39
+ }
40
+ startScript(scriptId) {
41
+ if (!this.stack.isEmpty) {
42
+ throw new Error(`ScriptEngine: Existing script haven't been concluded yet.`);
43
+ }
44
+ this.jumpToScript(scriptId);
45
+ }
46
+ next() {
47
+ if (this.stack.isEmpty) {
48
+ throw new Error(`ScriptEngine: There is no active script to iterate.`);
49
+ }
50
+ if (this.state !== ScriptEngineState.running) {
51
+ throw new Error(`ScriptEngine: The engine is not in running state. Next action cannot be executed. State: ${this.state}`);
52
+ }
53
+ let action = this.stack.pop();
54
+ this.executeAction(action);
55
+ }
56
+ playerChoice(choice) {
57
+ if (!this.playerBranchingChoices) {
58
+ throw new Error(`ScriptEngine: No player branching choices available.`);
59
+ }
60
+ let selectedActions = this.playerBranchingChoices[choice];
61
+ if (!selectedActions) {
62
+ throw new Error(`ScriptEngine: Invalid player choice.`);
63
+ }
64
+ this.insertActionsToStack(selectedActions);
65
+ this.playerBranchingChoices = undefined;
66
+ if (this.running) {
67
+ this.next();
68
+ }
69
+ }
70
+ jumpToScript(scriptId) {
71
+ let scriptDefinition = this.scriptDefinitions[scriptId];
72
+ if (!scriptDefinition) {
73
+ throw new Error(`ScriptEngine: Script definition not found: ${scriptId}`);
74
+ }
75
+ this.insertActionsToStack(scriptDefinition.actions);
76
+ }
77
+ executeAction(action) {
78
+ switch (action.type) {
79
+ case 'command':
80
+ this.jsEngine.execute(action.value);
81
+ break;
82
+ case 'dialog': {
83
+ let dialogItem = action.value;
84
+ this.jsEngine.functions.onDialog(this.jsEngine.string(dialogItem.text), dialogItem.speaker);
85
+ break;
86
+ }
87
+ case 'jumpToScript': {
88
+ this.jumpToScript(action.value);
89
+ if (this.running) {
90
+ this.next();
91
+ }
92
+ break;
93
+ }
94
+ case 'branchByCondition': {
95
+ let branches = action.value;
96
+ let selectedBranch = branches.find(branch => {
97
+ if (branch.condition) {
98
+ return this.jsEngine.boolean(branch.condition);
99
+ }
100
+ else {
101
+ return true;
102
+ }
103
+ });
104
+ if (!selectedBranch) {
105
+ throw new Error(`ScriptEngine: No branch is fulfilled the condition in branchByCondition action.`);
106
+ }
107
+ else {
108
+ this.insertActionsToStack(selectedBranch.actions);
109
+ }
110
+ if (this.running) {
111
+ this.next();
112
+ }
113
+ break;
114
+ }
115
+ case 'branchByChance': {
116
+ let validBranches = action.value.filter(branch => {
117
+ if (branch.condition) {
118
+ return this.jsEngine.boolean(branch.condition);
119
+ }
120
+ else {
121
+ return true;
122
+ }
123
+ });
124
+ if (validBranches.length === 0) {
125
+ throw new Error(`ScriptEngine: No branch is fulfilled the condition in branchByChance action.`);
126
+ }
127
+ else if (this.manualTestingMode) {
128
+ this.playerBranchingChoices = validBranches.map(branch => branch.actions);
129
+ this.jsEngine.functions.onPlayerChoice(validBranches.map(branch => branch.label));
130
+ }
131
+ else {
132
+ let selectedActions = helpers_lib_1.Random.pickRandomElementWithWeight(validBranches.map(branch => ({
133
+ value: branch.actions,
134
+ weight: helpers_lib_1.Comparator.isString(branch.weight) ? this.jsEngine.number(branch.weight) : branch.weight
135
+ })));
136
+ this.insertActionsToStack(selectedActions);
137
+ if (this.running) {
138
+ this.next();
139
+ }
140
+ }
141
+ break;
142
+ }
143
+ case 'branchByPlayerChoice': {
144
+ let validBranches = action.value.filter(branch => {
145
+ if (branch.condition) {
146
+ return this.jsEngine.boolean(branch.condition);
147
+ }
148
+ else {
149
+ return true;
150
+ }
151
+ });
152
+ if (validBranches.length === 0) {
153
+ throw new Error(`ScriptEngine: No branch is fulfilled the condition in branchByPlayerChoice action.`);
154
+ }
155
+ else {
156
+ this.playerBranchingChoices = validBranches.map(branch => branch.actions);
157
+ this.jsEngine.functions.onPlayerChoice(validBranches.map(branch => this.jsEngine.string(branch.text)));
158
+ }
159
+ break;
160
+ }
161
+ default:
162
+ throw new Error(`ScriptEngine: Unknown action type: ${action.type}`);
163
+ }
164
+ }
165
+ insertActionsToStack(actions) {
166
+ this.stack.add(...actions);
167
+ }
168
+ validateReservedFunctions(functions) {
169
+ if (helpers_lib_1.JSEngine.isJSEngineFunction(functions.onPlayerChoice)) {
170
+ throw new Error(`ScriptEngine: onPlayerChoice function shall not be decorated as a JSEngineFunction.`);
171
+ }
172
+ }
173
+ }
174
+ exports.ScriptEngine = ScriptEngine;
175
+ //# sourceMappingURL=script-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-engine.js","sourceRoot":"","sources":["../../src/engine/script-engine.ts"],"names":[],"mappings":";;;AAAA,6CAAkE;AAelE,MAAsB,qBAAqB;CAG1C;AAHD,sDAGC;AAED,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IAC3B,wCAAmB,CAAA;IACnB,sEAAiD,CAAA;IACjD,0CAAqB,CAAA;AACvB,CAAC,EAJW,iBAAiB,iCAAjB,iBAAiB,QAI5B;AAED,MAAa,YAAY;IASvB,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC;IACjC,CAAC;IAED,I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