MCPcopy Create free account
hub / github.com/Monogatari/Monogatari / prepareAction

Function prepareAction

src/engine/lifecycle.ts:138–179  ·  view source on GitHub ↗
(engine: VisualNovelEngine, statement: unknown, { cycle, extras }: { cycle: 'Application' | 'Revert'; extras?: Record<string, unknown> })

Source from the content-addressed store, hash-verified

136export function prepareAction(engine: VisualNovelEngine, statement: Record<string, unknown>, { cycle, extras }: { cycle: 'Application' | 'Revert'; extras?: Record<string, unknown> }): ActionInstance | null;
137export function prepareAction(engine: VisualNovelEngine, statement: string | Record<string, unknown>, { cycle, extras }: { cycle: 'Application' | 'Revert'; extras?: Record<string, unknown> }): ActionInstance | null;
138export function prepareAction (engine: VisualNovelEngine, statement: unknown, { cycle, extras }: { cycle: 'Application' | 'Revert'; extras?: Record<string, unknown> }): ActionInstance | null {
139 let action;
140 let interpolatedStatement: string[] | undefined;
141
142 // Use the correct matching function (matchString or matchObject)
143 // depending on the type of the current statement. If the statement
144 // is a pure js function, it won't be reverted since we don't
145 // know what to do to revert it.
146 if (typeof statement === 'string') {
147 interpolatedStatement = engine.replaceVariables (statement).split (' ');
148
149 // Check if it matches using the matchString method
150 action = engine.actions ().find (a => a.matchString (interpolatedStatement!));
151 } else if (typeof statement === 'object' && statement !== null) {
152
153 // Check if it matches using the matchObject method
154 action = engine.actions ().find (a => a.matchObject (statement as Record<string, unknown>));
155 }
156
157 if (typeof action !== 'undefined') {
158 const act = new action (typeof statement === 'string' ? interpolatedStatement : statement);
159
160 // The original statement is set just in case the action needs
161 // access to it. By this point, statement is known to be string | Record<string, unknown>
162 // (functions are handled earlier and returned directly).
163 act._setStatement (statement as string | Record<string, unknown>);
164
165 // The current cycle is also set just in case the action needs to
166 // know what cycle it's currently being performed.
167 act._setCycle (cycle);
168
169 // Monogatari is set as the context of the action so that it can
170 // access all its functionalities
171 act.setContext (engine);
172
173 act.setExtras(extras || {});
174
175 return act;
176 }
177
178 return null;
179}
180
181// ============================================================================
182// Statement Execution

Callers 2

revertFunction · 0.85
runFunction · 0.85

Calls 8

replaceVariablesMethod · 0.80
_setStatementMethod · 0.80
_setCycleMethod · 0.80
setContextMethod · 0.80
setExtrasMethod · 0.80
actionsMethod · 0.45
matchStringMethod · 0.45
matchObjectMethod · 0.45

Tested by

no test coverage detected