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
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { EventDefinition } from '../interfaces';
|
|
2
|
-
export interface StoryEngineOptions {
|
|
3
|
-
readonly manualTestingMode?: boolean;
|
|
4
|
-
}
|
|
5
|
-
export declare abstract class StoryEngineFunctions {
|
|
6
|
-
abstract onPlayerChoice(choices: readonly string[]): void;
|
|
7
|
-
abstract onDialog(text: string, speaker: string): void;
|
|
8
|
-
}
|
|
9
|
-
export declare enum StoryEngineState {
|
|
10
|
-
running = "running",
|
|
11
|
-
waitingForPlayerChoice = "waitingForPlayerChoice",
|
|
12
|
-
finished = "finished"
|
|
13
|
-
}
|
|
14
|
-
export declare class StoryEngine<EventFunctions extends StoryEngineFunctions> {
|
|
15
|
-
private eventDefinitions;
|
|
16
|
-
private jsEngine;
|
|
17
|
-
private stack;
|
|
18
|
-
private playerBranchingChoices;
|
|
19
|
-
private manualTestingMode;
|
|
20
|
-
get variables(): {
|
|
21
|
-
[key: string]: any;
|
|
22
|
-
};
|
|
23
|
-
get state(): StoryEngineState;
|
|
24
|
-
private get running();
|
|
25
|
-
constructor(eventDefinitions: Readonly<Record<string, EventDefinition>>, functions: EventFunctions, variables: {
|
|
26
|
-
[key: string]: any;
|
|
27
|
-
}, options?: StoryEngineOptions);
|
|
28
|
-
startEvent(eventId: string): void;
|
|
29
|
-
next(): void;
|
|
30
|
-
playerChoice(choice: number): void;
|
|
31
|
-
private jumpToEvent;
|
|
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.StoryEngine = exports.StoryEngineState = exports.StoryEngineFunctions = void 0;
|
|
4
|
-
const helpers_lib_1 = require("helpers-lib");
|
|
5
|
-
class StoryEngineFunctions {
|
|
6
|
-
}
|
|
7
|
-
exports.StoryEngineFunctions = StoryEngineFunctions;
|
|
8
|
-
var StoryEngineState;
|
|
9
|
-
(function (StoryEngineState) {
|
|
10
|
-
StoryEngineState["running"] = "running";
|
|
11
|
-
StoryEngineState["waitingForPlayerChoice"] = "waitingForPlayerChoice";
|
|
12
|
-
StoryEngineState["finished"] = "finished";
|
|
13
|
-
})(StoryEngineState || (exports.StoryEngineState = StoryEngineState = {}));
|
|
14
|
-
class StoryEngine {
|
|
15
|
-
get variables() {
|
|
16
|
-
return this.jsEngine.variables;
|
|
17
|
-
}
|
|
18
|
-
get state() {
|
|
19
|
-
if (this.playerBranchingChoices) {
|
|
20
|
-
return StoryEngineState.waitingForPlayerChoice;
|
|
21
|
-
}
|
|
22
|
-
else if (this.running) {
|
|
23
|
-
return StoryEngineState.running;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
return StoryEngineState.finished;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
get running() {
|
|
30
|
-
return !this.stack.isEmpty;
|
|
31
|
-
}
|
|
32
|
-
constructor(eventDefinitions, 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.eventDefinitions = eventDefinitions;
|
|
38
|
-
this.jsEngine = new helpers_lib_1.JSEngine(functions, variables);
|
|
39
|
-
}
|
|
40
|
-
startEvent(eventId) {
|
|
41
|
-
if (!this.stack.isEmpty) {
|
|
42
|
-
throw new Error(`StoryEngine: Existing event haven't been concluded yet.`);
|
|
43
|
-
}
|
|
44
|
-
this.jumpToEvent(eventId);
|
|
45
|
-
}
|
|
46
|
-
next() {
|
|
47
|
-
if (this.stack.isEmpty) {
|
|
48
|
-
throw new Error(`StoryEngine: There is no active event to iterate.`);
|
|
49
|
-
}
|
|
50
|
-
if (this.state !== StoryEngineState.running) {
|
|
51
|
-
throw new Error(`StoryEngine: 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(`StoryEngine: No player branching choices available.`);
|
|
59
|
-
}
|
|
60
|
-
let selectedActions = this.playerBranchingChoices[choice];
|
|
61
|
-
if (!selectedActions) {
|
|
62
|
-
throw new Error(`StoryEngine: Invalid player choice.`);
|
|
63
|
-
}
|
|
64
|
-
this.insertActionsToStack(selectedActions);
|
|
65
|
-
this.playerBranchingChoices = undefined;
|
|
66
|
-
if (this.running) {
|
|
67
|
-
this.next();
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
jumpToEvent(eventId) {
|
|
71
|
-
let eventDefinition = this.eventDefinitions[eventId];
|
|
72
|
-
if (!eventDefinition) {
|
|
73
|
-
throw new Error(`StoryEngine: Event definition not found: ${eventId}`);
|
|
74
|
-
}
|
|
75
|
-
this.insertActionsToStack(eventDefinition.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 'jumpToEvent': {
|
|
88
|
-
this.jumpToEvent(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(`StoryEngine: 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(`StoryEngine: 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(`StoryEngine: 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(`StoryEngine: 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(`StoryEngine: onPlayerChoice function shall not be decorated as a JSEngineFunction.`);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
exports.StoryEngine = StoryEngine;
|
|
175
|
-
//# sourceMappingURL=story-engine.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|