Skip to content

Commit 6843c08

Browse files
committed
fix: Script element is not executed correctly
1 parent 74d2697 commit 6843c08

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
### _Feature_
66

77
- `Persistent.equals`, `Persistent.notEquals` now support lambda or lambda handler as argument
8+
- Added `ScriptCtx.$` to get the namespace
9+
10+
### Fixed
11+
12+
- Script element is not executed correctly
813

914
## [0.8.3]
1015

src/game/nlcore/elements/built-in/Gallery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export class Gallery<Metadata extends Record<string, any>> extends Service<Galle
203203
game: ctx.game,
204204
liveGame: ctx.liveGame,
205205
storable: ctx.storable,
206+
$: ctx.$,
206207
};
207208
const parsedMetadata = typeof metadata === "function" ? metadata(context) : metadata;
208209
this.unlocked[name] = parsedMetadata;

src/game/nlcore/elements/condition.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ export class Lambda<T = any> {
4545

4646
/**@internal */
4747
getCtx({ gameState }: { gameState: GameState }): LambdaCtx {
48+
const liveGame = gameState.game.getLiveGame();
49+
const storable = liveGame.getStorable();
4850
return {
4951
gameState,
5052
game: gameState.game,
51-
liveGame: gameState.game.getLiveGame(),
52-
storable: gameState.game.getLiveGame().getStorable(),
53+
liveGame,
54+
storable,
55+
$: (namespace: string) => storable.getNamespace(namespace),
5356
};
5457
}
5558

src/game/nlcore/elements/script.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import {LogicAction} from "@core/action/logicAction";
44
import {Actionable} from "@core/action/actionable";
55
import {GameState} from "@player/gameState";
66
import {Chained, Proxied} from "@core/action/chain";
7-
import type {Storable} from "@core/elements/persistent/storable";
7+
import type {Namespace, Storable} from "@core/elements/persistent/storable";
88
import {ScriptAction} from "@core/action/actions/scriptAction";
99
import {LiveGame} from "@core/game/liveGame";
10+
import { NameSpaceContent } from "./persistent/type";
11+
12+
export type NamespaceGetter = <T extends NameSpaceContent<keyof T>>(namespace: string) => Namespace<T>;
1013

1114
export interface ScriptCtx {
1215
gameState: GameState;
1316
game: Game;
1417
liveGame: LiveGame;
1518
storable: Storable;
19+
$: NamespaceGetter;
1620
}
1721

1822
type ScriptRun = (ctx: ScriptCtx) => ScriptCleaner | void;
@@ -22,21 +26,35 @@ export type ScriptCleaner = () => void;
2226
export class Script extends Actionable<object> {
2327
/**@internal */
2428
static getCtx({gameState}: { gameState: GameState }): ScriptCtx {
29+
const liveGame = gameState.game.getLiveGame();
30+
const storable = liveGame.getStorable();
2531
return {
2632
gameState,
2733
game: gameState.game,
28-
liveGame: gameState.game.getLiveGame(),
29-
storable: gameState.game.getLiveGame().getStorable(),
34+
liveGame,
35+
storable,
36+
$: (namespace: string) => storable.getNamespace(namespace),
3037
};
3138
}
3239

40+
public static execute(handler: ScriptRun): Proxied<Script, Chained<LogicAction.Actions>> {
41+
return new Script(handler) as Proxied<Script, Chained<LogicAction.Actions>>;
42+
}
43+
3344
/**@internal */
3445
readonly handler: ScriptRun;
3546

3647
constructor(handler: ScriptRun) {
3748
super();
3849
this.handler = handler;
39-
return this.chain() satisfies Proxied<Script, Chained<LogicAction.Actions>>;
50+
51+
const chain = this.chain();
52+
const action = new ScriptAction(
53+
chain,
54+
ScriptAction.ActionTypes.action,
55+
new ContentNode<Script>().setContent(this)
56+
);
57+
return this.chain(action) satisfies Proxied<Script, Chained<LogicAction.Actions>>;
4058
}
4159

4260
/**@internal */

0 commit comments

Comments
 (0)