script-engine-lib 0.4.3 → 1.0.0-rc0
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.
- package/dist/index.d.mts +191 -0
- package/dist/index.d.ts +191 -5
- package/dist/index.js +21 -29
- package/dist/index.mjs +21 -0
- package/package.json +56 -52
- package/src/index.ts +10 -0
- package/dist/decorators/action-before-testing.d.ts +0 -2
- package/dist/decorators/action-before-testing.js +0 -23
- package/dist/decorators/action-before-testing.test.d.ts +0 -1
- package/dist/decorators/action-before-testing.test.js +0 -82
- package/dist/engine/script-engine.d.ts +0 -35
- package/dist/engine/script-engine.js +0 -175
- package/dist/engine/script-engine.test.d.ts +0 -1
- package/dist/engine/script-engine.test.js +0 -1277
- package/dist/engine/story-engine.d.ts +0 -35
- package/dist/engine/story-engine.js +0 -175
- package/dist/engine/story-engine.test.d.ts +0 -1
- package/dist/engine/story-engine.test.js +0 -1277
- package/dist/interfaces.d.ts +0 -51
- package/dist/interfaces.js +0 -27
- package/dist/simulator/decorators.d.ts +0 -2
- package/dist/simulator/decorators.js +0 -23
- package/dist/simulator/decorators.test.d.ts +0 -1
- package/dist/simulator/decorators.test.js +0 -81
- package/dist/simulator/event-branches-map.d.ts +0 -16
- package/dist/simulator/event-branches-map.js +0 -114
- package/dist/simulator/event-branches-map.test.d.ts +0 -1
- package/dist/simulator/event-branches-map.test.js +0 -120
- package/dist/simulator/event-test-simulator.d.ts +0 -42
- package/dist/simulator/event-test-simulator.js +0 -373
- package/dist/simulator/event-test-simulator.test.d.ts +0 -1
- package/dist/simulator/event-test-simulator.test.js +0 -1960
- package/dist/simulator/execution-history.d.ts +0 -6
- package/dist/simulator/execution-history.js +0 -22
- package/dist/simulator/helpers/event-branches-map.d.ts +0 -16
- package/dist/simulator/helpers/event-branches-map.js +0 -114
- package/dist/simulator/helpers/event-branches-map.test.d.ts +0 -1
- package/dist/simulator/helpers/event-branches-map.test.js +0 -120
- package/dist/simulator/helpers/execution-history.d.ts +0 -6
- package/dist/simulator/helpers/execution-history.js +0 -22
- package/dist/simulator/helpers/script-branches-map.d.ts +0 -16
- package/dist/simulator/helpers/script-branches-map.js +0 -114
- package/dist/simulator/helpers/script-branches-map.test.d.ts +0 -1
- package/dist/simulator/helpers/script-branches-map.test.js +0 -120
- package/dist/simulator/script-test-simulator.d.ts +0 -42
- package/dist/simulator/script-test-simulator.js +0 -373
- package/dist/simulator/script-test-simulator.test.d.ts +0 -1
- package/dist/simulator/script-test-simulator.test.js +0 -2013
- package/dist/structured-text/index.d.ts +0 -9
- package/dist/structured-text/index.js +0 -8
- package/dist/structured-text/rich-text-separator.d.ts +0 -48
- package/dist/structured-text/rich-text-separator.js +0 -146
- package/dist/structured-text/rich-text-separator.test.d.ts +0 -1
- package/dist/structured-text/rich-text-separator.test.js +0 -166
- package/dist/structured-text/rich-text-validator.d.ts +0 -19
- package/dist/structured-text/rich-text-validator.js +0 -73
- package/dist/structured-text/rich-text-validator.test.d.ts +0 -1
- package/dist/structured-text/rich-text-validator.test.js +0 -59
- package/dist/test.spec.d.ts +0 -0
- package/dist/test.spec.js +0 -7
- package/dist/test.test.d.ts +0 -1
- package/dist/test.test.js +0 -9
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { ActionBeforeTesting } from './decorators/action-before-testing';
|
|
2
|
+
export * from './interfaces';
|
|
3
|
+
export { JSEngine, JSEngineFunction } from './js-engine/js-engine';
|
|
4
|
+
export { ScriptEngine, ScriptEngineFunctions, ScriptEngineOptions, ScriptEngineState } from './script-engine/script-engine';
|
|
5
|
+
export {
|
|
6
|
+
ScriptEngineSimulatorFunctions,
|
|
7
|
+
ScriptTestSimulator,
|
|
8
|
+
ScriptTestSimulatorOptions
|
|
9
|
+
} from './simulator/script-test-simulator';
|
|
10
|
+
export * from './structured-text';
|
|
@@ -1,23 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,82 +0,0 @@
|
|
|
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
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
idle = "idle",
|
|
11
|
-
running = "running",
|
|
12
|
-
waitingForPlayerChoice = "waitingForPlayerChoice"
|
|
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
|
-
start(scriptId: string): void;
|
|
29
|
-
next(): void;
|
|
30
|
-
playerChoice(choice: number): void;
|
|
31
|
-
private jumpTo;
|
|
32
|
-
private executeAction;
|
|
33
|
-
private insertActionsToStack;
|
|
34
|
-
private validateReservedFunctions;
|
|
35
|
-
}
|
|
@@ -1,175 +0,0 @@
|
|
|
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["idle"] = "idle";
|
|
11
|
-
ScriptEngineState["running"] = "running";
|
|
12
|
-
ScriptEngineState["waitingForPlayerChoice"] = "waitingForPlayerChoice";
|
|
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.idle;
|
|
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
|
-
start(scriptId) {
|
|
41
|
-
if (!this.stack.isEmpty) {
|
|
42
|
-
throw new Error(`ScriptEngine: Existing script haven't been concluded yet.`);
|
|
43
|
-
}
|
|
44
|
-
this.jumpTo(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
|
-
jumpTo(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 'jumpTo': {
|
|
88
|
-
this.jumpTo(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, index) => branch.label ?? `Anonymous ${index}`));
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|