@energy8platform/platform-core 0.16.0

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.
Files changed (57) hide show
  1. package/README.md +482 -0
  2. package/bin/simulate.ts +139 -0
  3. package/dist/dev-bridge.cjs.js +237 -0
  4. package/dist/dev-bridge.cjs.js.map +1 -0
  5. package/dist/dev-bridge.d.ts +141 -0
  6. package/dist/dev-bridge.esm.js +235 -0
  7. package/dist/dev-bridge.esm.js.map +1 -0
  8. package/dist/index.cjs.js +569 -0
  9. package/dist/index.cjs.js.map +1 -0
  10. package/dist/index.d.ts +439 -0
  11. package/dist/index.esm.js +560 -0
  12. package/dist/index.esm.js.map +1 -0
  13. package/dist/loading.cjs.js +190 -0
  14. package/dist/loading.cjs.js.map +1 -0
  15. package/dist/loading.d.ts +86 -0
  16. package/dist/loading.esm.js +185 -0
  17. package/dist/loading.esm.js.map +1 -0
  18. package/dist/lua.cjs.js +1129 -0
  19. package/dist/lua.cjs.js.map +1 -0
  20. package/dist/lua.d.ts +319 -0
  21. package/dist/lua.esm.js +1119 -0
  22. package/dist/lua.esm.js.map +1 -0
  23. package/dist/simulation.cjs.js +374 -0
  24. package/dist/simulation.cjs.js.map +1 -0
  25. package/dist/simulation.d.ts +190 -0
  26. package/dist/simulation.esm.js +368 -0
  27. package/dist/simulation.esm.js.map +1 -0
  28. package/dist/vite.cjs.js +179 -0
  29. package/dist/vite.cjs.js.map +1 -0
  30. package/dist/vite.d.ts +13 -0
  31. package/dist/vite.esm.js +176 -0
  32. package/dist/vite.esm.js.map +1 -0
  33. package/package.json +100 -0
  34. package/scripts/install-simulate.mjs +101 -0
  35. package/src/EventEmitter.ts +55 -0
  36. package/src/PlatformSession.ts +156 -0
  37. package/src/dev-bridge/DevBridge.ts +305 -0
  38. package/src/dev-bridge/index.ts +2 -0
  39. package/src/index.ts +98 -0
  40. package/src/loading/CSSPreloader.ts +129 -0
  41. package/src/loading/index.ts +3 -0
  42. package/src/loading/logo.ts +95 -0
  43. package/src/lua/ActionRouter.ts +132 -0
  44. package/src/lua/LuaEngine.ts +412 -0
  45. package/src/lua/LuaEngineAPI.ts +314 -0
  46. package/src/lua/PersistentState.ts +80 -0
  47. package/src/lua/SessionManager.ts +227 -0
  48. package/src/lua/SimulationRunner.ts +192 -0
  49. package/src/lua/fengari.d.ts +10 -0
  50. package/src/lua/index.ts +28 -0
  51. package/src/lua/types.ts +149 -0
  52. package/src/simulation/NativeSimulationRunner.ts +367 -0
  53. package/src/simulation/ParallelSimulationRunner.ts +156 -0
  54. package/src/simulation/SimulationWorker.ts +44 -0
  55. package/src/simulation/index.ts +21 -0
  56. package/src/types.ts +85 -0
  57. package/src/vite/index.ts +196 -0
@@ -0,0 +1,1129 @@
1
+ 'use strict';
2
+
3
+ var fengari = require('fengari');
4
+
5
+ const { lua: lua$1, lauxlib: lauxlib$1 } = fengari;
6
+ const { to_luastring: to_luastring$1, to_jsstring: to_jsstring$1 } = fengari;
7
+ /** Cache for to_luastring() results — avoids re-encoding the same keys every iteration */
8
+ const luaStringCache = new Map();
9
+ function cachedToLuastring(s) {
10
+ let cached = luaStringCache.get(s);
11
+ if (!cached) {
12
+ cached = to_luastring$1(s);
13
+ luaStringCache.set(s, cached);
14
+ }
15
+ return cached;
16
+ }
17
+ /**
18
+ * Seeded xoshiro128** PRNG for deterministic simulation/replay.
19
+ * Period: 2^128 - 1
20
+ */
21
+ function createSeededRng(seed) {
22
+ let s0 = (seed >>> 0) | 1;
23
+ let s1 = (seed * 1103515245 + 12345) >>> 0;
24
+ let s2 = (seed * 6364136223846793005 + 1442695040888963407) >>> 0;
25
+ let s3 = (seed * 1442695040888963407 + 6364136223846793005) >>> 0;
26
+ return () => {
27
+ const result = (((s1 * 5) << 7) * 9) >>> 0;
28
+ const t = s1 << 9;
29
+ s2 ^= s0;
30
+ s3 ^= s1;
31
+ s1 ^= s2;
32
+ s0 ^= s3;
33
+ s2 ^= t;
34
+ s3 = ((s3 << 11) | (s3 >>> 21)) >>> 0;
35
+ return result / 4294967296;
36
+ };
37
+ }
38
+ /**
39
+ * Implements and registers all platform `engine.*` functions into a Lua state.
40
+ */
41
+ class LuaEngineAPI {
42
+ rng;
43
+ logger;
44
+ gameDefinition;
45
+ constructor(gameDefinition, rng, logger) {
46
+ this.gameDefinition = gameDefinition;
47
+ this.rng = rng ?? Math.random;
48
+ this.logger = logger ?? ((level, msg) => {
49
+ const fn = level === 'error' ? console.error
50
+ : level === 'warn' ? console.warn
51
+ : level === 'debug' ? console.debug
52
+ : console.log;
53
+ fn(`[Lua:${level}] ${msg}`);
54
+ });
55
+ }
56
+ /** Register `engine` global table on the Lua state */
57
+ register(L) {
58
+ // Create the `engine` table
59
+ lua$1.lua_newtable(L);
60
+ this.registerFunction(L, 'random', (LS) => {
61
+ const min = lauxlib$1.luaL_checkinteger(LS, 1);
62
+ const max = lauxlib$1.luaL_checkinteger(LS, 2);
63
+ const result = this.random(Number(min), Number(max));
64
+ lua$1.lua_pushinteger(LS, result);
65
+ return 1;
66
+ });
67
+ this.registerFunction(L, 'random_float', (LS) => {
68
+ lua$1.lua_pushnumber(LS, this.randomFloat());
69
+ return 1;
70
+ });
71
+ this.registerFunction(L, 'random_weighted', (LS) => {
72
+ lauxlib$1.luaL_checktype(LS, 1, lua$1.LUA_TTABLE);
73
+ const weights = [];
74
+ const len = lua$1.lua_rawlen(LS, 1);
75
+ for (let i = 1; i <= len; i++) {
76
+ lua$1.lua_rawgeti(LS, 1, i);
77
+ weights.push(lua$1.lua_tonumber(LS, -1));
78
+ lua$1.lua_pop(LS, 1);
79
+ }
80
+ const result = this.randomWeighted(weights);
81
+ lua$1.lua_pushinteger(LS, result);
82
+ return 1;
83
+ });
84
+ this.registerFunction(L, 'shuffle', (LS) => {
85
+ lauxlib$1.luaL_checktype(LS, 1, lua$1.LUA_TTABLE);
86
+ const arr = [];
87
+ const len = lua$1.lua_rawlen(LS, 1);
88
+ for (let i = 1; i <= len; i++) {
89
+ lua$1.lua_rawgeti(LS, 1, i);
90
+ arr.push(luaToJS(LS, -1));
91
+ lua$1.lua_pop(LS, 1);
92
+ }
93
+ const shuffled = this.shuffle(arr);
94
+ pushJSArray(LS, shuffled);
95
+ return 1;
96
+ });
97
+ this.registerFunction(L, 'log', (LS) => {
98
+ const level = to_jsstring$1(lauxlib$1.luaL_checkstring(LS, 1));
99
+ const msg = to_jsstring$1(lauxlib$1.luaL_checkstring(LS, 2));
100
+ this.logger(level, msg);
101
+ return 0;
102
+ });
103
+ this.registerFunction(L, 'get_config', (LS) => {
104
+ const config = this.getConfig();
105
+ pushJSObject(LS, config);
106
+ return 1;
107
+ });
108
+ // Set the table as global `engine`
109
+ lua$1.lua_setglobal(L, to_luastring$1('engine'));
110
+ }
111
+ // ─── engine.* implementations ─────────────────────────
112
+ random(min, max) {
113
+ return Math.floor(this.rng() * (max - min + 1)) + min;
114
+ }
115
+ randomFloat() {
116
+ return this.rng();
117
+ }
118
+ randomWeighted(weights) {
119
+ const totalWeight = weights.reduce((a, b) => a + b, 0);
120
+ let roll = this.rng() * totalWeight;
121
+ for (let i = 0; i < weights.length; i++) {
122
+ roll -= weights[i];
123
+ if (roll < 0)
124
+ return i + 1; // 1-based index
125
+ }
126
+ return weights.length; // fallback to last
127
+ }
128
+ shuffle(arr) {
129
+ const copy = [...arr];
130
+ for (let i = copy.length - 1; i > 0; i--) {
131
+ const j = Math.floor(this.rng() * (i + 1));
132
+ [copy[i], copy[j]] = [copy[j], copy[i]];
133
+ }
134
+ return copy;
135
+ }
136
+ getConfig() {
137
+ const def = this.gameDefinition;
138
+ let betLevels = [];
139
+ if (Array.isArray(def.bet_levels)) {
140
+ betLevels = def.bet_levels;
141
+ }
142
+ else if (def.bet_levels && 'levels' in def.bet_levels && def.bet_levels.levels) {
143
+ betLevels = def.bet_levels.levels;
144
+ }
145
+ return {
146
+ id: def.id,
147
+ type: def.type,
148
+ bet_levels: betLevels,
149
+ };
150
+ }
151
+ // ─── Helpers ──────────────────────────────────────────
152
+ registerFunction(L, name, fn) {
153
+ lua$1.lua_pushcfunction(L, fn);
154
+ lua$1.lua_setfield(L, -2, to_luastring$1(name));
155
+ }
156
+ }
157
+ // ─── Lua ↔ JS marshalling ───────────────────────────────
158
+ /** Read a Lua value at the given stack index and return its JS equivalent */
159
+ function luaToJS(L, idx) {
160
+ const type = lua$1.lua_type(L, idx);
161
+ switch (type) {
162
+ case lua$1.LUA_TNIL:
163
+ return null;
164
+ case lua$1.LUA_TBOOLEAN:
165
+ return lua$1.lua_toboolean(L, idx);
166
+ case lua$1.LUA_TNUMBER:
167
+ if (lua$1.lua_isinteger(L, idx)) {
168
+ return Number(lua$1.lua_tointeger(L, idx));
169
+ }
170
+ return lua$1.lua_tonumber(L, idx);
171
+ case lua$1.LUA_TSTRING:
172
+ return to_jsstring$1(lua$1.lua_tostring(L, idx));
173
+ case lua$1.LUA_TTABLE:
174
+ return luaTableToJS(L, idx);
175
+ default:
176
+ return null;
177
+ }
178
+ }
179
+ /** Convert a Lua table to a JS object or array */
180
+ function luaTableToJS(L, idx) {
181
+ // Normalize index to absolute
182
+ if (idx < 0)
183
+ idx = lua$1.lua_gettop(L) + idx + 1;
184
+ // Check if it's an array (sequential integer keys starting at 1)
185
+ const len = lua$1.lua_rawlen(L, idx);
186
+ if (len > 0) {
187
+ // Verify it's a pure array by checking key 1 exists
188
+ lua$1.lua_rawgeti(L, idx, 1);
189
+ const hasFirst = lua$1.lua_type(L, -1) !== lua$1.LUA_TNIL;
190
+ lua$1.lua_pop(L, 1);
191
+ if (hasFirst) {
192
+ // Check if there are also string keys (mixed table)
193
+ let hasStringKeys = false;
194
+ lua$1.lua_pushnil(L);
195
+ while (lua$1.lua_next(L, idx) !== 0) {
196
+ lua$1.lua_pop(L, 1); // pop value
197
+ if (lua$1.lua_type(L, -1) === lua$1.LUA_TSTRING) {
198
+ hasStringKeys = true;
199
+ lua$1.lua_pop(L, 1); // pop key
200
+ break;
201
+ }
202
+ }
203
+ if (!hasStringKeys) {
204
+ // Pure array
205
+ const arr = [];
206
+ for (let i = 1; i <= len; i++) {
207
+ lua$1.lua_rawgeti(L, idx, i);
208
+ arr.push(luaToJS(L, -1));
209
+ lua$1.lua_pop(L, 1);
210
+ }
211
+ return arr;
212
+ }
213
+ }
214
+ }
215
+ // Object (or mixed table)
216
+ const obj = {};
217
+ lua$1.lua_pushnil(L);
218
+ while (lua$1.lua_next(L, idx) !== 0) {
219
+ const keyType = lua$1.lua_type(L, -2);
220
+ let key;
221
+ if (keyType === lua$1.LUA_TSTRING) {
222
+ key = to_jsstring$1(lua$1.lua_tostring(L, -2));
223
+ }
224
+ else if (keyType === lua$1.LUA_TNUMBER) {
225
+ key = String(lua$1.lua_tonumber(L, -2));
226
+ }
227
+ else {
228
+ lua$1.lua_pop(L, 1);
229
+ continue;
230
+ }
231
+ obj[key] = luaToJS(L, -1);
232
+ lua$1.lua_pop(L, 1);
233
+ }
234
+ return obj;
235
+ }
236
+ /** Push a JS value onto the Lua stack */
237
+ function pushJSValue(L, value) {
238
+ if (value === null || value === undefined) {
239
+ lua$1.lua_pushnil(L);
240
+ }
241
+ else if (typeof value === 'boolean') {
242
+ lua$1.lua_pushboolean(L, value ? 1 : 0);
243
+ }
244
+ else if (typeof value === 'number') {
245
+ if (Number.isInteger(value)) {
246
+ lua$1.lua_pushinteger(L, value);
247
+ }
248
+ else {
249
+ lua$1.lua_pushnumber(L, value);
250
+ }
251
+ }
252
+ else if (typeof value === 'string') {
253
+ lua$1.lua_pushstring(L, cachedToLuastring(value));
254
+ }
255
+ else if (Array.isArray(value)) {
256
+ pushJSArray(L, value);
257
+ }
258
+ else if (typeof value === 'object') {
259
+ pushJSObject(L, value);
260
+ }
261
+ else {
262
+ lua$1.lua_pushnil(L);
263
+ }
264
+ }
265
+ /** Push a JS array as a Lua table (1-based) */
266
+ function pushJSArray(L, arr) {
267
+ lua$1.lua_createtable(L, arr.length, 0);
268
+ for (let i = 0; i < arr.length; i++) {
269
+ pushJSValue(L, arr[i]);
270
+ lua$1.lua_rawseti(L, -2, i + 1);
271
+ }
272
+ }
273
+ /** Push a JS object as a Lua table */
274
+ function pushJSObject(L, obj) {
275
+ const keys = Object.keys(obj);
276
+ lua$1.lua_createtable(L, 0, keys.length);
277
+ for (const key of keys) {
278
+ pushJSValue(L, obj[key]);
279
+ lua$1.lua_setfield(L, -2, cachedToLuastring(key));
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Replicates the platform's action dispatch and transition evaluation.
285
+ * Routes play requests to the correct action, evaluates transition conditions
286
+ * against current variables to determine next actions and session operations.
287
+ */
288
+ class ActionRouter {
289
+ actions;
290
+ constructor(gameDefinition) {
291
+ this.actions = gameDefinition.actions;
292
+ }
293
+ /** Look up action by name and validate prerequisites */
294
+ resolveAction(actionName, hasSession) {
295
+ const action = this.actions[actionName];
296
+ if (!action) {
297
+ throw new Error(`Unknown action: "${actionName}". Available: ${Object.keys(this.actions).join(', ')}`);
298
+ }
299
+ if (action.requires_session && !hasSession) {
300
+ throw new Error(`Action "${actionName}" requires an active session`);
301
+ }
302
+ return action;
303
+ }
304
+ /** Evaluate transitions in order, return the first matching rule */
305
+ evaluateTransitions(action, variables) {
306
+ for (const rule of action.transitions) {
307
+ if (evaluateCondition(rule.condition, variables)) {
308
+ return { rule, nextActions: rule.next_actions };
309
+ }
310
+ }
311
+ throw new Error(`No matching transition for action with stage "${action.stage}". ` +
312
+ `Variables: ${JSON.stringify(variables)}`);
313
+ }
314
+ }
315
+ // ─── Condition Evaluator ────────────────────────────────
316
+ /**
317
+ * Evaluates a transition condition expression against variables.
318
+ *
319
+ * Supports:
320
+ * - "always" → true
321
+ * - Simple comparisons: "var > 0", "var == 1", "var >= 10", "var != 0", "var < 5", "var <= 3"
322
+ * - Logical connectives: "expr && expr", "expr || expr"
323
+ *
324
+ * This covers all patterns used by the platform's govaluate conditions.
325
+ */
326
+ function evaluateCondition(condition, variables) {
327
+ const trimmed = condition.trim();
328
+ if (trimmed === 'always')
329
+ return true;
330
+ // Handle || (OR) — lowest precedence
331
+ if (trimmed.includes('||')) {
332
+ const parts = splitOnOperator(trimmed, '||');
333
+ return parts.some(part => evaluateCondition(part, variables));
334
+ }
335
+ // Handle && (AND)
336
+ if (trimmed.includes('&&')) {
337
+ const parts = splitOnOperator(trimmed, '&&');
338
+ return parts.every(part => evaluateCondition(part, variables));
339
+ }
340
+ // Single comparison: "variable op value"
341
+ return evaluateComparison(trimmed, variables);
342
+ }
343
+ function splitOnOperator(expr, operator) {
344
+ const parts = [];
345
+ let depth = 0;
346
+ let current = '';
347
+ for (let i = 0; i < expr.length; i++) {
348
+ if (expr[i] === '(')
349
+ depth++;
350
+ else if (expr[i] === ')')
351
+ depth--;
352
+ if (depth === 0 && expr.substring(i, i + operator.length) === operator) {
353
+ parts.push(current);
354
+ current = '';
355
+ i += operator.length - 1;
356
+ }
357
+ else {
358
+ current += expr[i];
359
+ }
360
+ }
361
+ parts.push(current);
362
+ return parts;
363
+ }
364
+ function evaluateComparison(expr, variables) {
365
+ // Match: variable_name operator value
366
+ const match = expr.trim().match(/^\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*(>=|<=|!=|==|>|<)\s*(-?\d+(?:\.\d+)?)\s*$/);
367
+ if (!match) {
368
+ throw new Error(`Cannot parse condition: "${expr}"`);
369
+ }
370
+ const [, varName, op, valueStr] = match;
371
+ const left = variables[varName] ?? 0;
372
+ const right = parseFloat(valueStr);
373
+ switch (op) {
374
+ case '>': return left > right;
375
+ case '>=': return left >= right;
376
+ case '<': return left < right;
377
+ case '<=': return left <= right;
378
+ case '==': return left === right;
379
+ case '!=': return left !== right;
380
+ default: return false;
381
+ }
382
+ }
383
+
384
+ const MAX_SESSION_SPINS = 200;
385
+ /**
386
+ * Manages session lifecycle matching the platform server behavior:
387
+ * - createSession: initial spin counted (spinsPlayed=1, totalWin=spinWin)
388
+ * - updateSession: accumulates win, decrements spins, checks max win cap on session level
389
+ * - completeSession: returns cumulative totalWin, cleans up session vars
390
+ * - Safety cap: 200 spins max per session
391
+ */
392
+ class SessionManager {
393
+ session = null;
394
+ get isActive() {
395
+ return this.session !== null && !this.session.completed;
396
+ }
397
+ get current() {
398
+ if (!this.session)
399
+ return null;
400
+ return this.toSessionData();
401
+ }
402
+ get sessionTotalWin() {
403
+ return this.session?.totalWin ?? 0;
404
+ }
405
+ /** Get the fixed bet amount from the session (server uses session bet, not request bet) */
406
+ get sessionBet() {
407
+ return this.session?.bet;
408
+ }
409
+ /** Get spinsVarName to restore free_spins_remaining into variables */
410
+ get spinsVarName() {
411
+ return this.session?.spinsVarName;
412
+ }
413
+ get spinsRemaining() {
414
+ return this.session?.spinsRemaining ?? 0;
415
+ }
416
+ /**
417
+ * Create a new session from a transition rule.
418
+ * Server behavior: initial spin is already counted (spinsPlayed=1, totalWin includes spinWin).
419
+ */
420
+ createSession(rule, variables, bet, spinWin, maxWinCap) {
421
+ let spinsRemaining = -1;
422
+ let spinsVarName;
423
+ if (rule.session_config?.total_spins_var) {
424
+ spinsVarName = rule.session_config.total_spins_var;
425
+ spinsRemaining = variables[spinsVarName] ?? -1;
426
+ }
427
+ const persistentVarNames = rule.session_config?.persistent_vars ?? [];
428
+ const persistentVars = {};
429
+ for (const varName of persistentVarNames) {
430
+ persistentVars[varName] = variables[varName] ?? 0;
431
+ }
432
+ this.session = {
433
+ spinsRemaining,
434
+ spinsPlayed: 1, // initial spin counts
435
+ totalWin: spinWin, // initial spin win included
436
+ completed: false,
437
+ maxWinReached: false,
438
+ bet,
439
+ maxWinCap,
440
+ spinsVarName,
441
+ persistentVarNames,
442
+ persistentVars,
443
+ persistentData: {},
444
+ };
445
+ return this.toSessionData();
446
+ }
447
+ /**
448
+ * Update session after a bonus spin.
449
+ * Server behavior: accumulate win, decrement spins, check retrigger, check max win cap,
450
+ * safety cap at 200 spins.
451
+ */
452
+ updateSession(rule, variables, spinWin) {
453
+ if (!this.session)
454
+ throw new Error('No active session');
455
+ // Accumulate win and count spin
456
+ this.session.totalWin += spinWin;
457
+ this.session.spinsPlayed++;
458
+ // Decrement spins (only for non-unlimited sessions)
459
+ if (this.session.spinsRemaining > 0) {
460
+ this.session.spinsRemaining--;
461
+ }
462
+ // Handle retrigger (add_spins_var)
463
+ if (rule.add_spins_var) {
464
+ const extraSpins = variables[rule.add_spins_var] ?? 0;
465
+ if (extraSpins > 0 && this.session.spinsRemaining >= 0) {
466
+ this.session.spinsRemaining += extraSpins;
467
+ }
468
+ }
469
+ // Safety cap: server limits sessions to 200 spins
470
+ if (this.session.spinsPlayed >= MAX_SESSION_SPINS) {
471
+ this.session.spinsRemaining = 0;
472
+ }
473
+ // Update session persistent vars from current variables
474
+ for (const varName of this.session.persistentVarNames) {
475
+ if (varName in variables) {
476
+ this.session.persistentVars[varName] = variables[varName];
477
+ }
478
+ }
479
+ // Check max win cap (on session level, not per spin)
480
+ if (this.session.maxWinCap !== undefined && this.session.totalWin >= this.session.maxWinCap) {
481
+ this.session.totalWin = this.session.maxWinCap;
482
+ this.session.spinsRemaining = 0;
483
+ this.session.maxWinReached = true;
484
+ }
485
+ // Auto-complete if spins exhausted or explicit complete
486
+ if (this.session.spinsRemaining === 0 || rule.complete_session) {
487
+ this.session.completed = true;
488
+ }
489
+ return this.toSessionData();
490
+ }
491
+ /**
492
+ * Complete the session explicitly.
493
+ * Returns cumulative totalWin and list of session-scoped var names to clean up.
494
+ */
495
+ completeSession() {
496
+ if (!this.session)
497
+ throw new Error('No active session to complete');
498
+ this.session.completed = true;
499
+ const totalWin = this.session.totalWin;
500
+ const session = this.toSessionData();
501
+ const sessionVarNames = [...this.session.persistentVarNames];
502
+ this.session = null;
503
+ return { totalWin, session, sessionVarNames };
504
+ }
505
+ /** Mark max win reached — stops the session */
506
+ markMaxWinReached() {
507
+ if (this.session) {
508
+ this.session.maxWinReached = true;
509
+ this.session.completed = true;
510
+ }
511
+ }
512
+ /** Store _persist_* data extracted from Lua result */
513
+ storePersistData(data) {
514
+ if (!this.session)
515
+ return;
516
+ for (const key of Object.keys(data)) {
517
+ if (key.startsWith('_persist_')) {
518
+ const cleanKey = key.slice('_persist_'.length);
519
+ this.session.persistentData[cleanKey] = data[key];
520
+ }
521
+ }
522
+ }
523
+ /** Get persistent params to inject into next execute() call */
524
+ getPersistentParams() {
525
+ if (!this.session)
526
+ return {};
527
+ const params = {};
528
+ // Session persistent vars (float64) → state.variables
529
+ for (const [key, value] of Object.entries(this.session.persistentVars)) {
530
+ params[key] = value;
531
+ }
532
+ // _persist_ complex data → _ps_* in state.params
533
+ for (const [key, value] of Object.entries(this.session.persistentData)) {
534
+ params[`_ps_${key}`] = value;
535
+ }
536
+ return params;
537
+ }
538
+ /** Reset all session state */
539
+ reset() {
540
+ this.session = null;
541
+ }
542
+ toSessionData() {
543
+ if (!this.session)
544
+ throw new Error('No session');
545
+ return {
546
+ spinsRemaining: this.session.spinsRemaining,
547
+ spinsPlayed: this.session.spinsPlayed,
548
+ totalWin: Math.round(this.session.totalWin * 100) / 100,
549
+ completed: this.session.completed,
550
+ maxWinReached: this.session.maxWinReached,
551
+ betAmount: this.session.bet,
552
+ };
553
+ }
554
+ }
555
+
556
+ /**
557
+ * Manages cross-spin persistent state — variables that survive between base game spins.
558
+ * Separate from session-scoped persistence (handled by SessionManager).
559
+ *
560
+ * Handles two mechanisms:
561
+ * 1. Numeric vars declared in `persistent_state.vars` — stored in state.variables
562
+ * 2. Complex data with `_persist_game_*` prefix — stored separately, injected as `_ps_*`
563
+ */
564
+ class PersistentState {
565
+ config;
566
+ vars = {};
567
+ gameData = {};
568
+ constructor(config) {
569
+ this.config = config;
570
+ }
571
+ /** Load persistent vars into variables map before execute() */
572
+ loadIntoVariables(variables) {
573
+ if (!this.config)
574
+ return;
575
+ for (const varName of this.config.vars) {
576
+ if (varName in this.vars) {
577
+ variables[varName] = this.vars[varName];
578
+ }
579
+ }
580
+ }
581
+ /** Save persistent vars from variables map after execute() */
582
+ saveFromVariables(variables) {
583
+ if (!this.config)
584
+ return;
585
+ for (const varName of this.config.vars) {
586
+ if (varName in variables) {
587
+ this.vars[varName] = variables[varName];
588
+ }
589
+ }
590
+ }
591
+ /** Extract _persist_game_* keys from Lua return data, store them */
592
+ storeGameData(data) {
593
+ for (const key of Object.keys(data)) {
594
+ if (key.startsWith('_persist_game_')) {
595
+ const cleanKey = key.slice('_persist_game_'.length);
596
+ this.gameData[cleanKey] = data[key];
597
+ delete data[key]; // remove from client data
598
+ }
599
+ }
600
+ }
601
+ /** Get _ps_* params for next execute() call */
602
+ getGameDataParams() {
603
+ const params = {};
604
+ for (const [key, value] of Object.entries(this.gameData)) {
605
+ params[`_ps_${key}`] = value;
606
+ }
607
+ return params;
608
+ }
609
+ /** Get exposed vars for client data.persistent_state */
610
+ getExposedVars() {
611
+ if (!this.config?.exposed_vars?.length)
612
+ return undefined;
613
+ const exposed = {};
614
+ for (const varName of this.config.exposed_vars) {
615
+ if (varName in this.vars) {
616
+ exposed[varName] = this.vars[varName];
617
+ }
618
+ }
619
+ return exposed;
620
+ }
621
+ /** Reset all state */
622
+ reset() {
623
+ this.vars = {};
624
+ this.gameData = {};
625
+ }
626
+ }
627
+
628
+ const { lua, lauxlib, lualib } = fengari;
629
+ const { to_luastring, to_jsstring } = fengari;
630
+ /** Default engine variables matching the server's NewGameState() */
631
+ const DEFAULT_VARIABLES = {
632
+ multiplier: 1,
633
+ total_multiplier: 1,
634
+ global_multiplier: 1,
635
+ last_win_amount: 0,
636
+ free_spins_awarded: 0,
637
+ };
638
+ /**
639
+ * Runs Lua game scripts locally, replicating the platform's server-side execution.
640
+ *
641
+ * Implements the full lifecycle matching `casino_platform/internal/usecase/game_usecase.go`:
642
+ * action routing → state assembly → Lua execute() → result extraction →
643
+ * transition evaluation → session management.
644
+ */
645
+ class LuaEngine {
646
+ L;
647
+ api;
648
+ actionRouter;
649
+ sessionManager;
650
+ persistentState;
651
+ gameDefinition;
652
+ variables = {};
653
+ simulationMode;
654
+ /** Reusable state objects to avoid per-iteration allocation */
655
+ _stateVars = {};
656
+ _stateParams = {};
657
+ constructor(config) {
658
+ this.gameDefinition = config.gameDefinition;
659
+ this.simulationMode = config.simulationMode ?? false;
660
+ const rng = config.seed !== undefined
661
+ ? createSeededRng(config.seed)
662
+ : undefined;
663
+ this.api = new LuaEngineAPI(config.gameDefinition, rng, config.logger);
664
+ this.actionRouter = new ActionRouter(config.gameDefinition);
665
+ this.sessionManager = new SessionManager();
666
+ this.persistentState = new PersistentState(config.gameDefinition.persistent_state);
667
+ this.L = lauxlib.luaL_newstate();
668
+ lualib.luaL_openlibs(this.L);
669
+ // Polyfill Lua 5.1/5.2 functions removed in 5.3
670
+ lauxlib.luaL_dostring(this.L, to_luastring(`
671
+ math.pow = function(a, b) return a ^ b end
672
+ math.atan2 = math.atan2 or function(y, x) return math.atan(y, x) end
673
+ math.log10 = math.log10 or function(x) return math.log(x, 10) end
674
+ math.cosh = math.cosh or function(x) return (math.exp(x) + math.exp(-x)) / 2 end
675
+ math.sinh = math.sinh or function(x) return (math.exp(x) - math.exp(-x)) / 2 end
676
+ math.tanh = math.tanh or function(x) return math.sinh(x) / math.cosh(x) end
677
+ math.frexp = math.frexp or function(x)
678
+ if x == 0 then return 0, 0 end
679
+ local e = math.floor(math.log(math.abs(x), 2)) + 1
680
+ return x / (2 ^ e), e
681
+ end
682
+ math.ldexp = math.ldexp or function(m, e) return m * (2 ^ e) end
683
+ unpack = unpack or table.unpack
684
+ loadstring = loadstring or load
685
+ table.getn = table.getn or function(t) return #t end
686
+ `));
687
+ this.api.register(this.L);
688
+ this.loadScript(config.script);
689
+ }
690
+ get session() {
691
+ return this.sessionManager.current;
692
+ }
693
+ get persistentVars() {
694
+ return { ...this.variables };
695
+ }
696
+ /**
697
+ * Execute a play action — replicates server's Play() function.
698
+ */
699
+ execute(params) {
700
+ const { action: actionName, params: clientParams } = params;
701
+ // 1. Resolve action
702
+ const action = this.actionRouter.resolveAction(actionName, this.sessionManager.isActive);
703
+ // 2. Determine bet — server uses session bet for session actions
704
+ let bet = params.bet;
705
+ if (this.sessionManager.isActive && this.sessionManager.sessionBet !== undefined) {
706
+ bet = this.sessionManager.sessionBet;
707
+ }
708
+ // 3. Build state.variables (matching server's NewGameState + restore)
709
+ // Reuse pooled object to avoid per-iteration allocation
710
+ const stateVars = this._stateVars;
711
+ // Clear previous keys
712
+ for (const key in stateVars)
713
+ delete stateVars[key];
714
+ // Apply defaults, then engine vars, then bet
715
+ Object.assign(stateVars, DEFAULT_VARIABLES, this.variables);
716
+ stateVars.bet = bet;
717
+ // Load cross-spin persistent state
718
+ this.persistentState.loadIntoVariables(stateVars);
719
+ // Load session persistent vars + restore spinsRemaining
720
+ if (this.sessionManager.isActive) {
721
+ const sessionParams = this.sessionManager.getPersistentParams();
722
+ for (const [k, v] of Object.entries(sessionParams)) {
723
+ if (typeof v === 'number') {
724
+ stateVars[k] = v;
725
+ }
726
+ }
727
+ // Restore spinsRemaining into the variable the script reads
728
+ if (this.sessionManager.spinsVarName) {
729
+ stateVars[this.sessionManager.spinsVarName] = this.sessionManager.spinsRemaining;
730
+ }
731
+ // Also set free_spins_remaining for convenience
732
+ stateVars.free_spins_remaining = this.sessionManager.spinsRemaining;
733
+ }
734
+ // 4. Build state.params (reuse pooled object)
735
+ const stateParams = this._stateParams;
736
+ for (const key in stateParams)
737
+ delete stateParams[key];
738
+ if (clientParams)
739
+ Object.assign(stateParams, clientParams);
740
+ stateParams._action = actionName;
741
+ // Inject session _ps_* persistent data
742
+ if (this.sessionManager.isActive) {
743
+ const sessionParams = this.sessionManager.getPersistentParams();
744
+ for (const [k, v] of Object.entries(sessionParams)) {
745
+ if (typeof v !== 'number') {
746
+ stateParams[k] = v;
747
+ }
748
+ }
749
+ }
750
+ // Inject cross-spin _ps_* game data
751
+ const gameDataParams = this.persistentState.getGameDataParams();
752
+ Object.assign(stateParams, gameDataParams);
753
+ // Handle buy bonus
754
+ if (action.buy_bonus_mode && this.gameDefinition.buy_bonus) {
755
+ const mode = this.gameDefinition.buy_bonus.modes[action.buy_bonus_mode];
756
+ if (mode) {
757
+ stateParams.buy_bonus = true;
758
+ stateParams.buy_bonus_mode = action.buy_bonus_mode;
759
+ if (mode.scatter_distribution) {
760
+ stateParams.forced_scatter_count = this.pickFromDistribution(mode.scatter_distribution);
761
+ }
762
+ }
763
+ }
764
+ // Handle ante bet
765
+ if (clientParams?.ante_bet && this.gameDefinition.ante_bet) {
766
+ stateParams.ante_bet = true;
767
+ }
768
+ // 5. Execute Lua (server: executor.Execute(stage, state))
769
+ const luaResult = this.callLuaExecute(action.stage, actionName, stateParams, stateVars);
770
+ // 6. Process result (server: ApplyLuaResult)
771
+ const totalWinMultiplier = typeof luaResult.total_win === 'number' ? luaResult.total_win : 0;
772
+ const resultVariables = (luaResult.variables ?? {});
773
+ const spinWin = Math.round(totalWinMultiplier * bet * 100) / 100;
774
+ // Merge ONLY Lua return variables into engine state (not the whole stateVars).
775
+ // On the server, state.Variables is a temporary object rebuilt each call.
776
+ // Only the Lua result's `variables` table persists between calls.
777
+ Object.assign(this.variables, resultVariables);
778
+ // Also update stateVars for transition evaluation below
779
+ Object.assign(stateVars, resultVariables);
780
+ // Build client data (everything except special keys)
781
+ const data = {};
782
+ for (const [key, value] of Object.entries(luaResult)) {
783
+ if (key !== 'total_win' && key !== 'variables') {
784
+ data[key] = value;
785
+ }
786
+ }
787
+ // 7. Handle _persist_* and _persist_game_* keys
788
+ this.sessionManager.storePersistData(data);
789
+ this.persistentState.storeGameData(data);
790
+ // Save cross-spin persistent state (from stateVars which has Lua result merged)
791
+ this.persistentState.saveFromVariables(stateVars);
792
+ // Add exposed persistent vars to client data
793
+ const exposedVars = this.persistentState.getExposedVars();
794
+ if (exposedVars) {
795
+ data.persistent_state = exposedVars;
796
+ }
797
+ // Remove _persist_* keys from client data
798
+ for (const key of Object.keys(data)) {
799
+ if (key.startsWith('_persist_')) {
800
+ delete data[key];
801
+ }
802
+ }
803
+ // 8. Evaluate transitions (server uses state.Variables which is stateVars)
804
+ const { rule, nextActions } = this.actionRouter.evaluateTransitions(action, stateVars);
805
+ // 9. Determine credit behavior (server: creditNow logic)
806
+ let creditDeferred = action.credit === 'defer' || rule.credit_override === 'defer';
807
+ // 10. Session lifecycle (server: create/update/complete session)
808
+ let session = this.sessionManager.current;
809
+ let resultTotalWin = spinWin;
810
+ let sessionCompleted = false;
811
+ // Calculate max win cap for session
812
+ const maxWinCap = this.calculateMaxWinCap(bet);
813
+ if (rule.creates_session && !this.sessionManager.isActive) {
814
+ // CREATE SESSION — initial spin counted (server: createSession includes spinWin)
815
+ session = this.sessionManager.createSession(rule, stateVars, bet, spinWin, maxWinCap);
816
+ creditDeferred = true;
817
+ resultTotalWin = spinWin;
818
+ // Clear the trigger variable — it was consumed to set spinsRemaining
819
+ if (rule.session_config?.total_spins_var) {
820
+ delete this.variables[rule.session_config.total_spins_var];
821
+ }
822
+ }
823
+ else if (this.sessionManager.isActive) {
824
+ // UPDATE SESSION — accumulate win, check completion
825
+ session = this.sessionManager.updateSession(rule, stateVars, spinWin);
826
+ if (session?.completed) {
827
+ // SESSION COMPLETED — server returns session.TotalWin as result.TotalWin
828
+ const completed = this.sessionManager.completeSession();
829
+ session = completed.session;
830
+ resultTotalWin = completed.totalWin;
831
+ sessionCompleted = true;
832
+ creditDeferred = false;
833
+ // Clean up session-scoped variables
834
+ for (const varName of completed.sessionVarNames) {
835
+ delete this.variables[varName];
836
+ }
837
+ }
838
+ else {
839
+ // Mid-session: totalWin = spinWin, credit deferred
840
+ resultTotalWin = spinWin;
841
+ creditDeferred = true;
842
+ }
843
+ }
844
+ // No session: resultTotalWin = spinWin (already set)
845
+ // Apply max win cap for non-session spins
846
+ if (!this.sessionManager.isActive && !sessionCompleted && maxWinCap !== undefined && resultTotalWin > maxWinCap) {
847
+ resultTotalWin = maxWinCap;
848
+ this.variables.max_win_reached = 1;
849
+ data.max_win_reached = true;
850
+ }
851
+ return {
852
+ totalWin: Math.round(resultTotalWin * 100) / 100,
853
+ data,
854
+ nextActions,
855
+ session,
856
+ // In simulation mode, return reference directly (caller only reads, never mutates)
857
+ variables: this.simulationMode ? this.variables : { ...this.variables },
858
+ creditDeferred,
859
+ };
860
+ }
861
+ reset() {
862
+ this.variables = {};
863
+ this.sessionManager.reset();
864
+ this.persistentState.reset();
865
+ }
866
+ destroy() {
867
+ if (this.L) {
868
+ lua.lua_close(this.L);
869
+ this.L = null;
870
+ }
871
+ }
872
+ // ─── Private ──────────────────────────────────────────
873
+ loadScript(source) {
874
+ const status = lauxlib.luaL_dostring(this.L, to_luastring(source));
875
+ if (status !== lua.LUA_OK) {
876
+ const err = to_jsstring(lua.lua_tostring(this.L, -1));
877
+ lua.lua_pop(this.L, 1);
878
+ throw new Error(`Failed to load Lua script: ${err}`);
879
+ }
880
+ lua.lua_getglobal(this.L, cachedToLuastring('execute'));
881
+ if (lua.lua_type(this.L, -1) !== lua.LUA_TFUNCTION) {
882
+ lua.lua_pop(this.L, 1);
883
+ throw new Error('Lua script must define a global `execute(state)` function');
884
+ }
885
+ lua.lua_pop(this.L, 1);
886
+ }
887
+ callLuaExecute(stage, action, params, variables) {
888
+ lua.lua_getglobal(this.L, cachedToLuastring('execute'));
889
+ // Build state table: {stage, action, params, variables}
890
+ lua.lua_createtable(this.L, 0, 4);
891
+ // state.stage
892
+ lua.lua_pushstring(this.L, cachedToLuastring(stage));
893
+ lua.lua_setfield(this.L, -2, cachedToLuastring('stage'));
894
+ // state.action (server sets this at top level)
895
+ lua.lua_pushstring(this.L, cachedToLuastring(action));
896
+ lua.lua_setfield(this.L, -2, cachedToLuastring('action'));
897
+ // state.params
898
+ pushJSValue(this.L, params);
899
+ lua.lua_setfield(this.L, -2, cachedToLuastring('params'));
900
+ // state.variables
901
+ pushJSValue(this.L, variables);
902
+ lua.lua_setfield(this.L, -2, cachedToLuastring('variables'));
903
+ const status = lua.lua_pcall(this.L, 1, 1, 0);
904
+ if (status !== lua.LUA_OK) {
905
+ const err = to_jsstring(lua.lua_tostring(this.L, -1));
906
+ lua.lua_pop(this.L, 1);
907
+ throw new Error(`Lua execute() failed: ${err}`);
908
+ }
909
+ if (this.simulationMode) {
910
+ // Fast path: extract only total_win, variables, _persist_* keys
911
+ const result = {};
912
+ lua.lua_getfield(this.L, -1, cachedToLuastring('total_win'));
913
+ result.total_win = lua.lua_type(this.L, -1) === lua.LUA_TNUMBER
914
+ ? lua.lua_tonumber(this.L, -1) : 0;
915
+ lua.lua_pop(this.L, 1);
916
+ lua.lua_getfield(this.L, -1, cachedToLuastring('variables'));
917
+ if (lua.lua_type(this.L, -1) === lua.LUA_TTABLE) {
918
+ result.variables = luaToJS(this.L, -1);
919
+ }
920
+ lua.lua_pop(this.L, 1);
921
+ // Scan for _persist_* keys (different stages may or may not have them)
922
+ lua.lua_pushnil(this.L);
923
+ while (lua.lua_next(this.L, -2) !== 0) {
924
+ if (lua.lua_type(this.L, -2) === lua.LUA_TSTRING) {
925
+ const key = to_jsstring(lua.lua_tostring(this.L, -2));
926
+ if (key.startsWith('_persist_')) {
927
+ result[key] = luaToJS(this.L, -1);
928
+ }
929
+ }
930
+ lua.lua_pop(this.L, 1);
931
+ }
932
+ lua.lua_pop(this.L, 1);
933
+ return result;
934
+ }
935
+ // Full path
936
+ const result = luaToJS(this.L, -1);
937
+ lua.lua_pop(this.L, 1);
938
+ if (!result || typeof result !== 'object' || Array.isArray(result)) {
939
+ throw new Error('Lua execute() must return a table');
940
+ }
941
+ return result;
942
+ }
943
+ calculateMaxWinCap(bet) {
944
+ const mw = this.gameDefinition.max_win;
945
+ if (!mw)
946
+ return undefined;
947
+ const caps = [];
948
+ if (mw.multiplier !== undefined)
949
+ caps.push(bet * mw.multiplier);
950
+ if (mw.fixed !== undefined)
951
+ caps.push(mw.fixed);
952
+ return caps.length > 0 ? Math.min(...caps) : undefined;
953
+ }
954
+ pickFromDistribution(distribution) {
955
+ const entries = Object.entries(distribution);
956
+ const totalWeight = entries.reduce((sum, [, w]) => sum + w, 0);
957
+ let roll = this.api.randomFloat() * totalWeight;
958
+ for (const [value, weight] of entries) {
959
+ roll -= weight;
960
+ if (roll < 0)
961
+ return parseInt(value, 10);
962
+ }
963
+ return parseInt(entries[entries.length - 1][0], 10);
964
+ }
965
+ }
966
+
967
+ /**
968
+ * Runs N iterations of a Lua game script and collects RTP statistics.
969
+ * Supports regular spins, buy bonus, and ante bet simulation.
970
+ *
971
+ * @example
972
+ * ```ts
973
+ * const runner = new SimulationRunner({
974
+ * script: luaSource,
975
+ * gameDefinition,
976
+ * iterations: 1_000_000,
977
+ * bet: 1.0,
978
+ * seed: 42,
979
+ * onProgress: (done, total) => console.log(`${done}/${total}`),
980
+ * });
981
+ *
982
+ * const result = runner.run();
983
+ * console.log(`RTP: ${result.totalRtp.toFixed(2)}%`);
984
+ * ```
985
+ */
986
+ class SimulationRunner {
987
+ config;
988
+ constructor(config) {
989
+ this.config = config;
990
+ }
991
+ run() {
992
+ const { script, gameDefinition, iterations, bet, seed, action: startAction = 'spin', params, progressInterval = 100_000, onProgress, } = this.config;
993
+ const engine = new LuaEngine({
994
+ script,
995
+ gameDefinition,
996
+ seed,
997
+ logger: () => { },
998
+ simulationMode: true,
999
+ });
1000
+ const spinCost = this.calculateSpinCost(startAction, bet, gameDefinition, params);
1001
+ let totalWagered = 0;
1002
+ let totalWon = 0;
1003
+ let baseGameWin = 0;
1004
+ let bonusWin = 0;
1005
+ let hits = 0;
1006
+ let maxWinMultiplier = 0;
1007
+ let maxWinHits = 0;
1008
+ let bonusTriggered = 0;
1009
+ let bonusSpinsPlayed = 0;
1010
+ const startTime = Date.now();
1011
+ try {
1012
+ for (let i = 0; i < iterations; i++) {
1013
+ totalWagered += spinCost;
1014
+ let roundWin = 0;
1015
+ let roundBonusWin = 0;
1016
+ // Execute the starting action
1017
+ let result = engine.execute({
1018
+ action: startAction,
1019
+ bet,
1020
+ params,
1021
+ });
1022
+ const baseWin = result.totalWin;
1023
+ // If a session was created, play through it using nextActions from the engine
1024
+ if (result.session && !result.session.completed) {
1025
+ bonusTriggered++;
1026
+ let safetyLimit = 10_000;
1027
+ while (result.session && !result.session.completed && safetyLimit-- > 0) {
1028
+ const nextAction = result.nextActions[0];
1029
+ result = engine.execute({ action: nextAction, bet });
1030
+ bonusSpinsPlayed++;
1031
+ }
1032
+ // Session completion returns cumulative totalWin (includes trigger spin).
1033
+ // Use it as the full round win — don't add baseWin separately.
1034
+ roundWin = result.totalWin;
1035
+ roundBonusWin = roundWin - baseWin;
1036
+ }
1037
+ else {
1038
+ // No session — just base game win
1039
+ roundWin = baseWin;
1040
+ }
1041
+ baseGameWin += baseWin;
1042
+ bonusWin += roundBonusWin;
1043
+ totalWon += roundWin;
1044
+ if (roundWin > 0)
1045
+ hits++;
1046
+ const roundMultiplier = roundWin / bet;
1047
+ if (roundMultiplier > maxWinMultiplier) {
1048
+ maxWinMultiplier = roundMultiplier;
1049
+ }
1050
+ if (result.variables?.max_win_reached === 1) {
1051
+ maxWinHits++;
1052
+ }
1053
+ // Progress reporting
1054
+ if (onProgress && (i + 1) % progressInterval === 0) {
1055
+ onProgress(i + 1, iterations);
1056
+ }
1057
+ }
1058
+ }
1059
+ finally {
1060
+ engine.destroy();
1061
+ }
1062
+ const durationMs = Date.now() - startTime;
1063
+ return {
1064
+ gameId: gameDefinition.id,
1065
+ action: startAction,
1066
+ iterations,
1067
+ durationMs,
1068
+ totalRtp: totalWagered > 0 ? (totalWon / totalWagered) * 100 : 0,
1069
+ baseGameRtp: totalWagered > 0 ? (baseGameWin / totalWagered) * 100 : 0,
1070
+ bonusRtp: totalWagered > 0 ? (bonusWin / totalWagered) * 100 : 0,
1071
+ hitFrequency: iterations > 0 ? (hits / iterations) * 100 : 0,
1072
+ maxWin: Math.round(maxWinMultiplier * 100) / 100,
1073
+ maxWinHits,
1074
+ bonusTriggered,
1075
+ bonusSpinsPlayed,
1076
+ _raw: { totalWagered, totalWon, baseGameWin, bonusWin, hits },
1077
+ };
1078
+ }
1079
+ /** Calculate the real cost of one spin (accounting for buy bonus / ante bet) */
1080
+ calculateSpinCost(action, bet, gameDefinition, params) {
1081
+ // Check if this is a buy bonus action
1082
+ const actionDef = gameDefinition.actions[action];
1083
+ if (actionDef?.buy_bonus_mode && gameDefinition.buy_bonus) {
1084
+ const mode = gameDefinition.buy_bonus.modes[actionDef.buy_bonus_mode];
1085
+ if (mode) {
1086
+ return bet * mode.cost_multiplier;
1087
+ }
1088
+ }
1089
+ // Check ante bet
1090
+ if (params?.ante_bet && gameDefinition.ante_bet) {
1091
+ return bet * gameDefinition.ante_bet.cost_multiplier;
1092
+ }
1093
+ return bet;
1094
+ }
1095
+ }
1096
+ /** Format a SimulationResult for console output */
1097
+ function formatSimulationResult(result) {
1098
+ const lines = [
1099
+ '',
1100
+ '--- Simulation Results ---',
1101
+ `Game: ${result.gameId}`,
1102
+ `Action: ${result.action}`,
1103
+ `Iterations: ${result.iterations.toLocaleString()}`,
1104
+ `Duration: ${(result.durationMs / 1000).toFixed(1)}s`,
1105
+ `Total RTP: ${result.totalRtp.toFixed(2)}%`,
1106
+ `Base Game RTP: ${result.baseGameRtp.toFixed(2)}%`,
1107
+ `Bonus RTP: ${result.bonusRtp.toFixed(2)}%`,
1108
+ `Hit Frequency: ${result.hitFrequency.toFixed(2)}%`,
1109
+ `Max Win: ${result.maxWin.toFixed(2)}x`,
1110
+ `Max Win Hits: ${result.maxWinHits} (rounds capped by max_win)`,
1111
+ ];
1112
+ if (result.bonusTriggered > 0) {
1113
+ const frequency = Math.round(result.iterations / result.bonusTriggered);
1114
+ lines.push(`Bonus Triggered: ${result.bonusTriggered.toLocaleString()} (1 in ${frequency} spins)`);
1115
+ lines.push(`Bonus Spins Played: ${result.bonusSpinsPlayed.toLocaleString()}`);
1116
+ }
1117
+ return lines.join('\n');
1118
+ }
1119
+
1120
+ exports.ActionRouter = ActionRouter;
1121
+ exports.LuaEngine = LuaEngine;
1122
+ exports.LuaEngineAPI = LuaEngineAPI;
1123
+ exports.PersistentState = PersistentState;
1124
+ exports.SessionManager = SessionManager;
1125
+ exports.SimulationRunner = SimulationRunner;
1126
+ exports.createSeededRng = createSeededRng;
1127
+ exports.evaluateCondition = evaluateCondition;
1128
+ exports.formatSimulationResult = formatSimulationResult;
1129
+ //# sourceMappingURL=lua.cjs.js.map