@@ -4,15 +4,19 @@ import {LogicAction} from "@core/action/logicAction";
44import { Actionable } from "@core/action/actionable" ;
55import { GameState } from "@player/gameState" ;
66import { 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" ;
88import { ScriptAction } from "@core/action/actions/scriptAction" ;
99import { 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
1114export interface ScriptCtx {
1215 gameState : GameState ;
1316 game : Game ;
1417 liveGame : LiveGame ;
1518 storable : Storable ;
19+ $ : NamespaceGetter ;
1620}
1721
1822type ScriptRun = ( ctx : ScriptCtx ) => ScriptCleaner | void ;
@@ -22,21 +26,35 @@ export type ScriptCleaner = () => void;
2226export 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