@firestone-hs/simulate-bgs-battle 1.1.357 → 1.1.359

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/README.md CHANGED
@@ -39,6 +39,26 @@ rm -rf dist && tsc && rm -rf dist/node_modules && 'cp' -rf dist/ /e/Source/zerot
39
39
  rm -rf dist && tsc && rm -rf dist/node_modules && npm publish
40
40
  ```
41
41
 
42
+ # Profiling
43
+
44
+ To profile a Node.js application using Chrome's DevTools, you can use the `--inspect` and `--inspect-brk` flags when running your script. Here's how you can do it:
45
+
46
+ 1. Run your script with the `--inspect-brk` flag. This will start the inspector and pause execution until you connect with the debugger.
47
+
48
+ ```bash
49
+ node --inspect-brk -r ts-node/register full-test.ts
50
+ ```
51
+
52
+ 2. Open Chrome and navigate to `chrome://inspect`.
53
+ 3. Click on the "Open dedicated DevTools for Node" link. This will open a new DevTools window.
54
+ 4. In the DevTools window, click on the "Profiler" tab.
55
+ 5. Click on the "Start" button to start profiling.
56
+ 6. Go back to your terminal and press `Enter` to continue script execution.
57
+ 7. Once your script finishes executing, go back to the DevTools window and click on the "Stop" button to stop profiling.
58
+ 8. You can now analyze the CPU profile in the DevTools window.
59
+
60
+ Remember to replace `node` with `npx ts-node` if you're using TypeScript without compiling to JavaScript first.
61
+
42
62
  # Reference
43
63
 
44
64
  Used this project as template: https://github.com/alukach/aws-sam-typescript-boilerplate
@@ -1 +1 @@
1
- {"version":3,"file":"bgs-board-info.js","sourceRoot":"","sources":["../src/bgs-board-info.ts"],"names":[],"mappings":"","sourcesContent":["import { BgsPlayerEntity } from './bgs-player-entity';\r\nimport { BoardEntity } from './board-entity';\r\nimport { BoardSecret } from './board-secret';\r\n\r\nexport interface BgsBoardInfo {\r\n\treadonly player: BgsPlayerEntity;\r\n\treadonly board: BoardEntity[];\r\n\treadonly secrets?: BoardSecret[];\r\n}\r\n"]}
1
+ {"version":3,"file":"bgs-board-info.js","sourceRoot":"","sources":["../src/bgs-board-info.ts"],"names":[],"mappings":"","sourcesContent":["import { BgsPlayerEntity } from './bgs-player-entity';\r\nimport { BoardEntity } from './board-entity';\r\nimport { BoardSecret } from './board-secret';\r\n\r\nexport interface BgsBoardInfo {\r\n\treadonly player: BgsPlayerEntity;\r\n\treadonly board: BoardEntity[];\r\n\t/** @deprecated */\r\n\treadonly secrets?: BoardSecret[];\r\n}\r\n"]}
@@ -15,9 +15,11 @@ async function sleep(ms) {
15
15
  }
16
16
  exports.sleep = sleep;
17
17
  const groupByFunction = (keyExtractor) => (array) => {
18
- return array.reduce((objectsByKeyValue, obj) => {
18
+ return (array !== null && array !== void 0 ? array : []).reduce((objectsByKeyValue, obj) => {
19
+ var _a;
19
20
  const value = keyExtractor(obj);
20
- objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
21
+ objectsByKeyValue[value] = (_a = objectsByKeyValue[value]) !== null && _a !== void 0 ? _a : [];
22
+ objectsByKeyValue[value].push(obj);
21
23
  return objectsByKeyValue;
22
24
  }, {});
23
25
  };
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/services/utils.ts"],"names":[],"mappings":";;;AAEA,SAAS,cAAc,CAAI,KAAmB,EAAE,aAAqB;IACpE,MAAM,WAAW,GAAQ,CAAC,GAAG,KAAK,CAAC,CAAC;IACpC,MAAM,MAAM,GAAU,EAAE,CAAC;IACzB,OAAO,WAAW,CAAC,MAAM,EAAE;QAC1B,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC;KAClD;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAgBQ,wCAAc;AAdvB,KAAK,UAAU,KAAK,CAAC,EAAE;IACtB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC1D,CAAC;AAYwB,sBAAK;AAVvB,MAAM,eAAe,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GK,MAAM,YAAY,GAAG,CAAI,KAAU,EAAO,EAAE;IAClD,IAAI,YAAY,GAAG,KAAK,CAAC,MAAM,CAAC;IAChC,IAAI,WAAW,GAAG,CAAC,CAAC;IAGpB,OAAO,YAAY,IAAI,CAAC,EAAE;QAEzB,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,CAAC;QACvD,YAAY,EAAE,CAAC;QAGf,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;KACtF;IAED,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAfW,QAAA,YAAY,gBAevB","sourcesContent":["import { BoardEntity } from '../board-entity';\r\n\r\nfunction partitionArray<T>(array: readonly T[], partitionSize: number): readonly T[][] {\r\n\tconst workingCopy: T[] = [...array];\r\n\tconst result: T[][] = [];\r\n\twhile (workingCopy.length) {\r\n\t\tresult.push(workingCopy.splice(0, partitionSize));\r\n\t}\r\n\treturn result;\r\n}\r\n\r\nasync function sleep(ms) {\r\n\treturn new Promise((resolve) => setTimeout(resolve, ms));\r\n}\r\n\r\nexport const groupByFunction =\r\n\t<T>(keyExtractor: (obj: T) => string | number) =>\r\n\t(array: readonly T[]): { [key: string]: readonly T[] } => {\r\n\t\treturn array.reduce((objectsByKeyValue, obj) => {\r\n\t\t\tconst value = keyExtractor(obj);\r\n\t\t\tobjectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);\r\n\t\t\treturn objectsByKeyValue;\r\n\t\t}, {});\r\n\t};\r\n\r\nexport { partitionArray, sleep };\r\n\r\nexport const pickRandom = <T>(array: readonly T[]): T => {\r\n\tif (!array?.length) {\r\n\t\treturn null;\r\n\t}\r\n\treturn array[Math.floor(Math.random() * array.length)];\r\n};\r\n\r\nexport const pickRandomAlive = (board: BoardEntity[]): BoardEntity => {\r\n\tconst targetBoard = board.filter((e) => e.health > 0 && !e.definitelyDead);\r\n\tconst chosenEntity = pickRandom(targetBoard);\r\n\treturn chosenEntity;\r\n};\r\n\r\nexport const pickRandomLowestHealth = (board: BoardEntity[]): BoardEntity => {\r\n\tconst targetBoard = board.filter((e) => e.health > 0 && !e.definitelyDead);\r\n\tconst lowestHealth = Math.min(...targetBoard.map((e) => e.health));\r\n\tconst entitiesWithLowestHealth = targetBoard.filter((e) => e.health === lowestHealth);\r\n\tconst chosenEntity = pickRandom(entitiesWithLowestHealth);\r\n\treturn chosenEntity;\r\n};\r\n\r\nexport const encode = (input: string): string => {\r\n\t// return compressToEncodedURIComponent(input);\r\n\tconst buff = Buffer.from(input, 'utf-8');\r\n\tconst base64 = buff.toString('base64');\r\n\treturn base64;\r\n};\r\n\r\nexport const decode = (base64: string): string => {\r\n\tconst buff = Buffer.from(base64, 'base64');\r\n\tconst str = buff.toString('utf-8');\r\n\treturn str;\r\n};\r\n\r\nexport const pickMultipleRandomDifferent = <T>(list: T[], n: number): T[] => {\r\n\tconst shuffled = shuffleArray([...list]);\r\n\treturn shuffled.slice(0, n);\r\n};\r\n\r\n// https://stackoverflow.com/a/2450976/548701\r\nexport const shuffleArray = <T>(array: T[]): T[] => {\r\n\tlet currentIndex = array.length;\r\n\tlet randomIndex = 0;\r\n\r\n\t// While there remain elements to shuffle...\r\n\twhile (currentIndex != 0) {\r\n\t\t// Pick a remaining element...\r\n\t\trandomIndex = Math.floor(Math.random() * currentIndex);\r\n\t\tcurrentIndex--;\r\n\r\n\t\t// And swap it with the current element.\r\n\t\t[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];\r\n\t}\r\n\r\n\treturn array;\r\n};\r\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/services/utils.ts"],"names":[],"mappings":";;;AAEA,SAAS,cAAc,CAAI,KAAmB,EAAE,aAAqB;IACpE,MAAM,WAAW,GAAQ,CAAC,G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MAAM,MAAM,GAAG,CAAC,MAAc,EAAU,EAAE;IAChD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC3C,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACnC,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC;AAJW,QAAA,MAAM,UAIjB;AAEK,MAAM,2BAA2B,GAAG,CAAI,IAAS,EAAE,CAAS,EAAO,EAAE;IAC3E,MAAM,QAAQ,GAAG,IAAA,oBAAY,EAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACzC,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC7B,CAAC,CAAC;AAHW,QAAA,2BAA2B,+BAGtC;AAGK,MAAM,YAAY,GAAG,CAAI,KAAU,EAAO,EAAE;IAClD,IAAI,YAAY,GAAG,KAAK,CAAC,MAAM,CAAC;IAChC,IAAI,WAAW,GAAG,CAAC,CAAC;IAGpB,OAAO,YAAY,IAAI,CAAC,EAAE;QAEzB,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,CAAC;QACvD,YAAY,EAAE,CAAC;QAGf,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;KACtF;IAED,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAfW,QAAA,YAAY,gBAevB","sourcesContent":["import { BoardEntity } from '../board-entity';\r\n\r\nfunction partitionArray<T>(array: readonly T[], partitionSize: number): readonly T[][] {\r\n\tconst workingCopy: T[] = [...array];\r\n\tconst result: T[][] = [];\r\n\twhile (workingCopy.length) {\r\n\t\tresult.push(workingCopy.splice(0, partitionSize));\r\n\t}\r\n\treturn result;\r\n}\r\n\r\nasync function sleep(ms) {\r\n\treturn new Promise((resolve) => setTimeout(resolve, ms));\r\n}\r\n\r\nexport const groupByFunction =\r\n\t<T>(keyExtractor: (obj: T) => string | number) =>\r\n\t(array: readonly T[]): { [key: string]: readonly T[] } => {\r\n\t\treturn (array ?? []).reduce((objectsByKeyValue, obj) => {\r\n\t\t\tconst value = keyExtractor(obj);\r\n\t\t\tobjectsByKeyValue[value] = objectsByKeyValue[value] ?? [];\r\n\t\t\t// Using push instead of concat is thousands of times faster on big arrays\r\n\t\t\tobjectsByKeyValue[value].push(obj);\r\n\t\t\treturn objectsByKeyValue;\r\n\t\t}, {});\r\n\t};\r\n\r\nexport { partitionArray, sleep };\r\n\r\nexport const pickRandom = <T>(array: readonly T[]): T => {\r\n\tif (!array?.length) {\r\n\t\treturn null;\r\n\t}\r\n\treturn array[Math.floor(Math.random() * array.length)];\r\n};\r\n\r\nexport const pickRandomAlive = (board: BoardEntity[]): BoardEntity => {\r\n\tconst targetBoard = board.filter((e) => e.health > 0 && !e.definitelyDead);\r\n\tconst chosenEntity = pickRandom(targetBoard);\r\n\treturn chosenEntity;\r\n};\r\n\r\nexport const pickRandomLowestHealth = (board: BoardEntity[]): BoardEntity => {\r\n\tconst targetBoard = board.filter((e) => e.health > 0 && !e.definitelyDead);\r\n\tconst lowestHealth = Math.min(...targetBoard.map((e) => e.health));\r\n\tconst entitiesWithLowestHealth = targetBoard.filter((e) => e.health === lowestHealth);\r\n\tconst chosenEntity = pickRandom(entitiesWithLowestHealth);\r\n\treturn chosenEntity;\r\n};\r\n\r\nexport const encode = (input: string): string => {\r\n\t// return compressToEncodedURIComponent(input);\r\n\tconst buff = Buffer.from(input, 'utf-8');\r\n\tconst base64 = buff.toString('base64');\r\n\treturn base64;\r\n};\r\n\r\nexport const decode = (base64: string): string => {\r\n\tconst buff = Buffer.from(base64, 'base64');\r\n\tconst str = buff.toString('utf-8');\r\n\treturn str;\r\n};\r\n\r\nexport const pickMultipleRandomDifferent = <T>(list: T[], n: number): T[] => {\r\n\tconst shuffled = shuffleArray([...list]);\r\n\treturn shuffled.slice(0, n);\r\n};\r\n\r\n// https://stackoverflow.com/a/2450976/548701\r\nexport const shuffleArray = <T>(array: T[]): T[] => {\r\n\tlet currentIndex = array.length;\r\n\tlet randomIndex = 0;\r\n\r\n\t// While there remain elements to shuffle...\r\n\twhile (currentIndex != 0) {\r\n\t\t// Pick a remaining element...\r\n\t\trandomIndex = Math.floor(Math.random() * currentIndex);\r\n\t\tcurrentIndex--;\r\n\r\n\t\t// And swap it with the current element.\r\n\t\t[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];\r\n\t}\r\n\r\n\treturn array;\r\n};\r\n"]}
@@ -23,7 +23,7 @@ exports.default = async (event) => {
23
23
  return response;
24
24
  };
25
25
  const simulateBattle = (battleInput, cards, cardsData) => {
26
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
26
+ var _a, _b, _c, _d, _e;
27
27
  const start = Date.now();
28
28
  const maxAcceptableDuration = ((_a = battleInput.options) === null || _a === void 0 ? void 0 : _a.maxAcceptableDuration) || 8000;
29
29
  const numberOfSimulations = ((_b = battleInput.options) === null || _b === void 0 ? void 0 : _b.numberOfSimulations) || 5000;
@@ -43,21 +43,107 @@ const simulateBattle = (battleInput, cards, cardsData) => {
43
43
  averageDamageWon: undefined,
44
44
  averageDamageLost: undefined,
45
45
  };
46
+ const spectator = new spectator_1.Spectator(battleInput.playerBoard.player.cardId, battleInput.playerBoard.player.heroPowerId, battleInput.opponentBoard.player.cardId, battleInput.opponentBoard.player.heroPowerId);
47
+ const inputReady = buildFinalInput(battleInput, cards, cardsData);
48
+ !((_c = battleInput.options) === null || _c === void 0 ? void 0 : _c.skipInfoLogs) && console.time('simulation');
49
+ const outcomes = {};
50
+ for (let i = 0; i < numberOfSimulations; i++) {
51
+ const input = cloneInput3(inputReady);
52
+ const simulator = new simulator_1.Simulator(cards, cardsData);
53
+ const battleResult = simulator.simulateSingleBattle(input.playerBoard.board, input.playerBoard.player, input.opponentBoard.board, input.opponentBoard.player, input.gameState, spectator);
54
+ if (Date.now() - start > maxAcceptableDuration) {
55
+ console.warn('Stopping simulation after', i, 'iterations and ', Date.now() - start, 'ms', battleResult);
56
+ break;
57
+ }
58
+ if (!battleResult) {
59
+ continue;
60
+ }
61
+ if (battleResult.result === 'won') {
62
+ simulationResult.won++;
63
+ simulationResult.damageWon += battleResult.damageDealt;
64
+ if (battleResult.damageDealt >= battleInput.opponentBoard.player.hpLeft) {
65
+ simulationResult.wonLethal++;
66
+ }
67
+ }
68
+ else if (battleResult.result === 'lost') {
69
+ simulationResult.lost++;
70
+ simulationResult.damageLost += battleResult.damageDealt;
71
+ outcomes[battleResult.damageDealt] = ((_d = outcomes[battleResult.damageDealt]) !== null && _d !== void 0 ? _d : 0) + 1;
72
+ if (battleInput.playerBoard.player.hpLeft &&
73
+ battleResult.damageDealt >= battleInput.playerBoard.player.hpLeft) {
74
+ simulationResult.lostLethal++;
75
+ }
76
+ }
77
+ else if (battleResult.result === 'tied') {
78
+ simulationResult.tied++;
79
+ }
80
+ spectator.commitBattleResult(battleResult.result);
81
+ }
82
+ updateSimulationResult(simulationResult, inputReady);
83
+ !((_e = battleInput.options) === null || _e === void 0 ? void 0 : _e.skipInfoLogs) && console.timeEnd('simulation');
84
+ spectator.prune();
85
+ simulationResult.outcomeSamples = spectator.buildOutcomeSamples();
86
+ return simulationResult;
87
+ };
88
+ exports.simulateBattle = simulateBattle;
89
+ const cloneInput = (input) => {
90
+ return structuredClone(input);
91
+ };
92
+ const cloneInput2 = (input) => {
93
+ return JSON.parse(input);
94
+ };
95
+ const cloneInput3 = (input) => {
96
+ const result = {
97
+ gameState: {
98
+ currentTurn: input.gameState.currentTurn,
99
+ anomalies: input.gameState.anomalies,
100
+ validTribes: input.gameState.validTribes,
101
+ },
102
+ heroHasDied: input.heroHasDied,
103
+ playerBoard: cloneBoard(input.playerBoard),
104
+ opponentBoard: cloneBoard(input.opponentBoard),
105
+ options: null,
106
+ };
107
+ return result;
108
+ };
109
+ const cloneBoard = (board) => {
110
+ var _a, _b;
111
+ const result = {
112
+ player: {
113
+ ...board.player,
114
+ hand: (_a = board.player.hand) === null || _a === void 0 ? void 0 : _a.map((entity) => cloneEntity(entity)),
115
+ secrets: (_b = board.player.secrets) === null || _b === void 0 ? void 0 : _b.map((secret) => ({ ...secret })),
116
+ globalInfo: { ...board.player.globalInfo },
117
+ },
118
+ board: board.board.map((entity) => cloneEntity(entity)),
119
+ };
120
+ return result;
121
+ };
122
+ const cloneEntity = (entity) => {
123
+ var _a;
124
+ const result = {
125
+ ...entity,
126
+ enchantments: (_a = entity.enchantments) === null || _a === void 0 ? void 0 : _a.map((enchant) => ({ ...enchant })),
127
+ };
128
+ return result;
129
+ };
130
+ const buildFinalInput = (battleInput, cards, cardsData) => {
131
+ var _a, _b, _c, _d;
46
132
  const playerInfo = battleInput.playerBoard;
47
133
  const opponentInfo = battleInput.opponentBoard;
48
134
  const playerBoard = playerInfo.board.map((entity) => ({ ...(0, utils_1.addImpliedMechanics)(entity, cardsData), friendly: true }));
49
- const playerHand = (_c = playerInfo.player.hand) === null || _c === void 0 ? void 0 : _c.map((entity) => ({
135
+ const playerHand = (_a = playerInfo.player.hand) === null || _a === void 0 ? void 0 : _a.map((entity) => ({
50
136
  ...(0, utils_1.addImpliedMechanics)(entity, cardsData),
51
137
  friendly: true,
52
138
  }));
53
- playerInfo.player.secrets = (_d = playerInfo.secrets) === null || _d === void 0 ? void 0 : _d.filter((e) => !!(e === null || e === void 0 ? void 0 : e.cardId));
139
+ playerInfo.player.secrets = (_b = playerInfo.secrets) === null || _b === void 0 ? void 0 : _b.filter((e) => !!(e === null || e === void 0 ? void 0 : e.cardId));
54
140
  playerInfo.player.friendly = true;
55
141
  const opponentBoard = opponentInfo.board.map((entity) => ({
56
142
  ...(0, utils_1.addImpliedMechanics)(entity, cardsData),
57
143
  friendly: false,
58
144
  }));
59
- const opponentHand = (_e = opponentInfo.player.hand) === null || _e === void 0 ? void 0 : _e.map((entity) => ({ ...(0, utils_1.addImpliedMechanics)(entity, cardsData), friendly: false }));
60
- opponentInfo.player.secrets = (_f = opponentInfo.secrets) === null || _f === void 0 ? void 0 : _f.filter((e) => !!(e === null || e === void 0 ? void 0 : e.cardId));
145
+ const opponentHand = (_c = opponentInfo.player.hand) === null || _c === void 0 ? void 0 : _c.map((entity) => ({ ...(0, utils_1.addImpliedMechanics)(entity, cardsData), friendly: false }));
146
+ opponentInfo.player.secrets = (_d = opponentInfo.secrets) === null || _d === void 0 ? void 0 : _d.filter((e) => !!(e === null || e === void 0 ? void 0 : e.cardId));
61
147
  opponentInfo.player.friendly = false;
62
148
  (0, auras_1.setMissingAuras)(playerBoard, playerInfo.player, opponentInfo.player, cards);
63
149
  (0, auras_1.setMissingAuras)(opponentBoard, opponentInfo.player, playerInfo.player, cards);
@@ -80,42 +166,9 @@ const simulateBattle = (battleInput, cards, cardsData) => {
80
166
  },
81
167
  gameState: battleInput.gameState,
82
168
  };
83
- const inputStr = JSON.stringify(inputReady);
84
- const spectator = new spectator_1.Spectator(battleInput.playerBoard.player.cardId, battleInput.playerBoard.player.heroPowerId, battleInput.opponentBoard.player.cardId, battleInput.opponentBoard.player.heroPowerId);
85
- !((_g = battleInput.options) === null || _g === void 0 ? void 0 : _g.skipInfoLogs) && console.time('simulation');
86
- const outcomes = {};
87
- for (let i = 0; i < numberOfSimulations; i++) {
88
- const simulator = new simulator_1.Simulator(cards, cardsData);
89
- const input = JSON.parse(inputStr);
90
- const battleResult = simulator.simulateSingleBattle(input.playerBoard.board, input.playerBoard.player, input.opponentBoard.board, input.opponentBoard.player, input.gameState, spectator);
91
- if (Date.now() - start > maxAcceptableDuration) {
92
- console.warn('Stopping simulation after', i, 'iterations and ', Date.now() - start, 'ms', battleResult);
93
- break;
94
- }
95
- if (!battleResult) {
96
- continue;
97
- }
98
- if (battleResult.result === 'won') {
99
- simulationResult.won++;
100
- simulationResult.damageWon += battleResult.damageDealt;
101
- if (battleResult.damageDealt >= battleInput.opponentBoard.player.hpLeft) {
102
- simulationResult.wonLethal++;
103
- }
104
- }
105
- else if (battleResult.result === 'lost') {
106
- simulationResult.lost++;
107
- simulationResult.damageLost += battleResult.damageDealt;
108
- outcomes[battleResult.damageDealt] = ((_h = outcomes[battleResult.damageDealt]) !== null && _h !== void 0 ? _h : 0) + 1;
109
- if (battleInput.playerBoard.player.hpLeft &&
110
- battleResult.damageDealt >= battleInput.playerBoard.player.hpLeft) {
111
- simulationResult.lostLethal++;
112
- }
113
- }
114
- else if (battleResult.result === 'tied') {
115
- simulationResult.tied++;
116
- }
117
- spectator.commitBattleResult(battleResult.result);
118
- }
169
+ return inputReady;
170
+ };
171
+ const updateSimulationResult = (simulationResult, input) => {
119
172
  const totalMatches = simulationResult.won + simulationResult.tied + simulationResult.lost;
120
173
  simulationResult.wonPercent = checkRounding(Math.round((10 * (100 * simulationResult.won)) / totalMatches) / 10, simulationResult.won, totalMatches);
121
174
  simulationResult.wonLethalPercent = checkRounding(Math.round((10 * (100 * simulationResult.wonLethal)) / totalMatches) / 10, simulationResult.wonLethal, totalMatches);
@@ -128,18 +181,15 @@ const simulateBattle = (battleInput, cards, cardsData) => {
128
181
  simulationResult.averageDamageLost = simulationResult.lost
129
182
  ? simulationResult.damageLost / simulationResult.lost
130
183
  : 0;
131
- if (simulationResult.averageDamageWon > 0 && simulationResult.averageDamageWon < playerInfo.player.tavernTier) {
132
- console.warn('average damage won issue', simulationResult, playerInfo);
184
+ if (simulationResult.averageDamageWon > 0 &&
185
+ simulationResult.averageDamageWon < input.playerBoard.player.tavernTier) {
186
+ console.warn('average damage won issue');
133
187
  }
134
- if (simulationResult.averageDamageLost > 0 && simulationResult.averageDamageLost < opponentInfo.player.tavernTier) {
135
- console.warn('average damage lost issue', simulationResult, opponentInfo);
188
+ if (simulationResult.averageDamageLost > 0 &&
189
+ simulationResult.averageDamageLost < input.opponentBoard.player.tavernTier) {
190
+ console.warn('average damage lost issue', simulationResult);
136
191
  }
137
- !((_j = battleInput.options) === null || _j === void 0 ? void 0 : _j.skipInfoLogs) && console.timeEnd('simulation');
138
- spectator.prune();
139
- simulationResult.outcomeSamples = spectator.buildOutcomeSamples();
140
- return simulationResult;
141
192
  };
142
- exports.simulateBattle = simulateBattle;
143
193
  const checkRounding = (roundedValue, initialValue, totalValue) => {
144
194
  if (roundedValue === 0 && initialValue !== 0) {
145
195
  return 0.01;
@@ -1 +1 @@
1
- {"version":3,"file":"simulate-bgs-battle.js","sourceRoot":"","sources":["../src/simulate-bgs-battle.ts"],"names":[],"mappings":";;;AACA,iEAAwE;AAGxE,mDAA+C;AAE/C,8CAA0E;AAC1E,sDAAmD;AACnD,gEAA6D;AAC7D,mCAA8C;AAE9C,MAAM,KAAK,GAAG,IAAI,gCAAe,EAAE,CAAC;AAKpC,kBAAe,KAAK,EAAE,KAAK,EAAgB,EAAE;;IAC5C,MAAM,WAAW,GAAkB,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC1D,MAAM,KAAK,CAAC,iBAAiB,EAAE,CAAC;IAChC,MAAM,SAAS,GAAG,IAAI,sBAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC9C,SAAS,CAAC,YAAY,CACrB,MAAA,MAAA,WAAW,CAAC,SAAS,0CAAE,WAAW,mCAAI,MAAA,WAAW,CAAC,OAAO,0CAAE,WAAW,EACtE,MAAA,MAAA,WAAW,CAAC,SAAS,0CAAE,SAAS,mCAAI,EAAE,CACtC,CAAC;IACF,MAAM,gBAAgB,GAAG,IAAA,sBAAc,EAAC,WAAW,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;IAEvE,MAAM,QAAQ,GAAG;QAChB,UAAU,EAAE,GAAG;QACf,eAAe,EAAE,KAAK;QACtB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC;KACtC,CAAC;IACF,OAAO,QAAQ,CAAC;AACjB,CAAC,CAAC;AAEK,MAAM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AM,aAAa,GAAG,CAAC,YAAoB,EAAE,YAAoB,EAAE,UAAkB,EAAU,EAAE;IAChG,IAAI,YAAY,KAAK,CAAC,IAAI,YAAY,KAAK,CAAC,EAAE;QAC7C,OAAO,IAAI,CAAC;KACZ;IACD,IAAI,YAAY,KAAK,GAAG,IAAI,YAAY,KAAK,UAAU,EAAE;QACxD,OAAO,IAAI,CAAC;KACZ;IACD,OAAO,YAAY,CAAC;AACrB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,CAAC,KAA6B,EAA0B,EAAE;IACnF,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzD,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAC7B,GAAG,MAAM;QACT,YAAY,EAAE,0BAA0B,CAAC,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC;KACxE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEW,QAAA,iBAAiB,GAAG;;;;;;;;;;;;;;;;;;CAoBhC,CAAC;AAEF,MAAM,0BAA0B,GAAG,CAClC,YAA2E,EAC3E,SAA4B,EACoC,EAAE;IAClE,OAAO,YAAY,CAAC,MAAM,CACzB,CAAC,OAAO,EAAE,EAAE,CACX,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAChD,yBAAiB,CAAC,OAAO,CAAC,OAAO,CAAC,MAAiB,CAAC,KAAK,CAAC,CAAC,CAC5D,CAAC;AACH,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\r\nimport { AllCardsService, CardIds } from '@firestone-hs/reference-data';\r\nimport { BgsBattleInfo } from './bgs-battle-info';\r\nimport { BoardEntity } from './board-entity';\r\nimport { CardsData } from './cards/cards-data';\r\nimport { SimulationResult } from './simulation-result';\r\nimport { setImplicitDataHero, setMissingAuras } from './simulation/auras';\r\nimport { Simulator } from './simulation/simulator';\r\nimport { Spectator } from './simulation/spectator/spectator';\r\nimport { addImpliedMechanics } from './utils';\r\n\r\nconst cards = new AllCardsService();\r\n\r\n// This example demonstrates a NodeJS 8.10 async handler[1], however of course you could use\r\n// the more traditional callback-style handler.\r\n// [1]: https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/\r\nexport default async (event): Promise<any> => {\r\n\tconst battleInput: BgsBattleInfo = JSON.parse(event.body);\r\n\tawait cards.initializeCardsDb();\r\n\tconst cardsData = new CardsData(cards, false);\r\n\tcardsData.inititialize(\r\n\t\tbattleInput.gameState?.validTribes ?? battleInput.options?.validTribes,\r\n\t\tbattleInput.gameState?.anomalies ?? [],\r\n\t);\r\n\tconst simulationResult = simulateBattle(battleInput, cards, cardsData);\r\n\r\n\tconst response = {\r\n\t\tstatusCode: 200,\r\n\t\tisBase64Encoded: false,\r\n\t\tbody: JSON.stringify(simulationResult),\r\n\t};\r\n\treturn response;\r\n};\r\n\r\nexport const simulateBattle = (\r\n\tbattleInput: BgsBattleInfo,\r\n\tcards: AllCardsService,\r\n\tcardsData: CardsData,\r\n): SimulationResult => {\r\n\tconst start = Date.now();\r\n\r\n\tconst maxAcceptableDuration = battleInput.options?.maxAcceptableDuration || 8000;\r\n\tconst numberOfSimulations = battleInput.options?.numberOfSimulations || 5000;\r\n\r\n\tconst simulationResult: SimulationResult = {\r\n\t\twonLethal: 0,\r\n\t\twon: 0,\r\n\t\ttied: 0,\r\n\t\tlost: 0,\r\n\t\tlostLethal: 0,\r\n\t\tdamageWon: 0,\r\n\t\tdamageLost: 0,\r\n\t\twonLethalPercent: undefined,\r\n\t\twonPercent: undefined,\r\n\t\ttiedPercent: undefined,\r\n\t\tlostPercent: undefined,\r\n\t\tlostLethalPercent: undefined,\r\n\t\taverageDamageWon: undefined,\r\n\t\taverageDamageLost: undefined,\r\n\t};\r\n\r\n\tconst playerInfo = battleInput.playerBoard;\r\n\tconst opponentInfo = battleInput.opponentBoard;\r\n\r\n\tconst playerBoard = playerInfo.board.map(\r\n\t\t(entity) => ({ ...addImpliedMechanics(entity, cardsData), friendly: true } as BoardEntity),\r\n\t);\r\n\tconst playerHand = playerInfo.player.hand?.map(\r\n\t\t(entity) =>\r\n\t\t\t({\r\n\t\t\t\t...addImpliedMechanics(entity, cardsData),\r\n\t\t\t\tfriendly: true,\r\n\t\t\t\t// locked: cardsData.getTavernLevel(entity.cardId) > playerInfo.player.tavernTier,\r\n\t\t\t} as BoardEntity),\r\n\t);\r\n\tplayerInfo.player.secrets = playerInfo.secrets?.filter((e) => !!e?.cardId);\r\n\tplayerInfo.player.friendly = true;\r\n\tconst opponentBoard = opponentInfo.board.map(\r\n\t\t(entity) =>\r\n\t\t\t({\r\n\t\t\t\t...addImpliedMechanics(entity, cardsData),\r\n\t\t\t\tfriendly: false,\r\n\t\t\t\t// locked: cardsData.getTavernLevel(entity.cardId) > playerInfo.player.tavernTier,\r\n\t\t\t} as BoardEntity),\r\n\t);\r\n\tconst opponentHand = opponentInfo.player.hand?.map(\r\n\t\t(entity) => ({ ...addImpliedMechanics(entity, cardsData), friendly: false } as BoardEntity),\r\n\t);\r\n\topponentInfo.player.secrets = opponentInfo.secrets?.filter((e) => !!e?.cardId);\r\n\topponentInfo.player.friendly = false;\r\n\r\n\t// When using the simulator, the aura is not applied when receiving the board state. When\r\n\tsetMissingAuras(playerBoard, playerInfo.player, opponentInfo.player, cards);\r\n\tsetMissingAuras(opponentBoard, opponentInfo.player, playerInfo.player, cards);\r\n\t// Avenge, maxHealth, etc.\r\n\t// setImplicitData(playerBoard, cardsData);\r\n\t// setImplicitData(opponentBoard, cardsData);\r\n\t// Avenge, globalInfo\r\n\tsetImplicitDataHero(playerInfo.player, cardsData, true);\r\n\tsetImplicitDataHero(opponentInfo.player, cardsData, false);\r\n\r\n\t// We do this so that we can have mutated objects inside the simulation and still\r\n\t// be able to start from a fresh copy for each simulation\r\n\tconst inputReady: BgsBattleInfo = {\r\n\t\tplayerBoard: {\r\n\t\t\tboard: playerBoard,\r\n\t\t\tplayer: {\r\n\t\t\t\t...playerInfo.player,\r\n\t\t\t\thand: playerHand,\r\n\t\t\t},\r\n\t\t},\r\n\t\topponentBoard: {\r\n\t\t\tboard: opponentBoard,\r\n\t\t\tplayer: {\r\n\t\t\t\t...opponentInfo.player,\r\n\t\t\t\thand: opponentHand,\r\n\t\t\t},\r\n\t\t},\r\n\t\tgameState: battleInput.gameState,\r\n\t} as BgsBattleInfo;\r\n\tconst inputStr = JSON.stringify(inputReady);\r\n\tconst spectator = new Spectator(\r\n\t\tbattleInput.playerBoard.player.cardId,\r\n\t\tbattleInput.playerBoard.player.heroPowerId,\r\n\t\tbattleInput.opponentBoard.player.cardId,\r\n\t\tbattleInput.opponentBoard.player.heroPowerId,\r\n\t);\r\n\t!battleInput.options?.skipInfoLogs && console.time('simulation');\r\n\tconst outcomes = {};\r\n\tfor (let i = 0; i < numberOfSimulations; i++) {\r\n\t\tconst simulator = new Simulator(cards, cardsData);\r\n\t\tconst input: BgsBattleInfo = JSON.parse(inputStr);\r\n\t\tconst battleResult = simulator.simulateSingleBattle(\r\n\t\t\tinput.playerBoard.board,\r\n\t\t\tinput.playerBoard.player,\r\n\t\t\tinput.opponentBoard.board,\r\n\t\t\tinput.opponentBoard.player,\r\n\t\t\tinput.gameState,\r\n\t\t\tspectator,\r\n\t\t);\r\n\t\tif (Date.now() - start > maxAcceptableDuration) {\r\n\t\t\t// Can happen in case of inifinite boards, or a bug. Don't hog the user's computer in that case\r\n\t\t\tconsole.warn('Stopping simulation after', i, 'iterations and ', Date.now() - start, 'ms', battleResult);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tif (!battleResult) {\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tif (battleResult.result === 'won') {\r\n\t\t\tsimulationResult.won++;\r\n\t\t\tsimulationResult.damageWon += battleResult.damageDealt;\r\n\t\t\tif (battleResult.damageDealt >= battleInput.opponentBoard.player.hpLeft) {\r\n\t\t\t\tsimulationResult.wonLethal++;\r\n\t\t\t}\r\n\t\t} else if (battleResult.result === 'lost') {\r\n\t\t\tsimulationResult.lost++;\r\n\t\t\tsimulationResult.damageLost += battleResult.damageDealt;\r\n\t\t\toutcomes[battleResult.damageDealt] = (outcomes[battleResult.damageDealt] ?? 0) + 1;\r\n\t\t\tif (\r\n\t\t\t\tbattleInput.playerBoard.player.hpLeft &&\r\n\t\t\t\tbattleResult.damageDealt >= battleInput.playerBoard.player.hpLeft\r\n\t\t\t) {\r\n\t\t\t\tsimulationResult.lostLethal++;\r\n\t\t\t}\r\n\t\t} else if (battleResult.result === 'tied') {\r\n\t\t\tsimulationResult.tied++;\r\n\t\t}\r\n\t\tspectator.commitBattleResult(battleResult.result);\r\n\t}\r\n\tconst totalMatches = simulationResult.won + simulationResult.tied + simulationResult.lost;\r\n\tsimulationResult.wonPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.won)) / totalMatches) / 10,\r\n\t\tsimulationResult.won,\r\n\t\ttotalMatches,\r\n\t);\r\n\tsimulationResult.wonLethalPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.wonLethal)) / totalMatches) / 10,\r\n\t\tsimulationResult.wonLethal,\r\n\t\ttotalMatches,\r\n\t);\r\n\tsimulationResult.lostPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.lost)) / totalMatches) / 10,\r\n\t\tsimulationResult.lost,\r\n\t\ttotalMatches,\r\n\t);\r\n\tsimulationResult.lostLethalPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.lostLethal)) / totalMatches) / 10,\r\n\t\tsimulationResult.lostLethal,\r\n\t\ttotalMatches,\r\n\t);\r\n\t// simulationResult.tiedPercent = checkRounding(Math.round((10 * (100 * simulationResult.tied)) / totalMatches) / 10, simulationResult.tied, totalMatches);\r\n\tsimulationResult.tiedPercent = checkRounding(\r\n\t\t100 - simulationResult.lostPercent - simulationResult.wonPercent,\r\n\t\tsimulationResult.tied,\r\n\t\ttotalMatches,\r\n\t);\r\n\r\n\tsimulationResult.wonLethalPercent = Math.round((10 * (100 * simulationResult.wonLethal)) / totalMatches) / 10;\r\n\tsimulationResult.lostLethalPercent = Math.round((10 * (100 * simulationResult.lostLethal)) / totalMatches) / 10;\r\n\tsimulationResult.averageDamageWon = simulationResult.won ? simulationResult.damageWon / simulationResult.won : 0;\r\n\tsimulationResult.averageDamageLost = simulationResult.lost\r\n\t\t? simulationResult.damageLost / simulationResult.lost\r\n\t\t: 0;\r\n\tif (simulationResult.averageDamageWon > 0 && simulationResult.averageDamageWon < playerInfo.player.tavernTier) {\r\n\t\tconsole.warn('average damage won issue', simulationResult, playerInfo);\r\n\t}\r\n\tif (simulationResult.averageDamageLost > 0 && simulationResult.averageDamageLost < opponentInfo.player.tavernTier) {\r\n\t\tconsole.warn('average damage lost issue', simulationResult, opponentInfo);\r\n\t}\r\n\t!battleInput.options?.skipInfoLogs && console.timeEnd('simulation');\r\n\tspectator.prune();\r\n\tsimulationResult.outcomeSamples = spectator.buildOutcomeSamples();\r\n\treturn simulationResult;\r\n};\r\n\r\nconst checkRounding = (roundedValue: number, initialValue: number, totalValue: number): number => {\r\n\tif (roundedValue === 0 && initialValue !== 0) {\r\n\t\treturn 0.01;\r\n\t}\r\n\tif (roundedValue === 100 && initialValue !== totalValue) {\r\n\t\treturn 99.9;\r\n\t}\r\n\treturn roundedValue;\r\n};\r\n\r\nconst cleanEnchantments = (board: readonly BoardEntity[]): readonly BoardEntity[] => {\r\n\tconst entityIds = board.map((entity) => entity.entityId);\r\n\treturn board.map((entity) => ({\r\n\t\t...entity,\r\n\t\tenchantments: cleanEnchantmentsForEntity(entity.enchantments, entityIds),\r\n\t}));\r\n};\r\n\r\nexport const validEnchantments = [\r\n\tCardIds.ReplicatingMenace_ReplicatingMenaceEnchantment_BG_BOT_312e,\r\n\tCardIds.ReplicatingMenace_ReplicatingMenaceEnchantment_TB_BaconUps_032e,\r\n\tCardIds.LivingSpores_LivingSporesEnchantment,\r\n\tCardIds.Leapfrogger_LeapfrogginEnchantment_BG21_000e,\r\n\tCardIds.Leapfrogger_LeapfrogginEnchantment_BG21_000_Ge,\r\n\tCardIds.Sneed_SneedsReplicator,\r\n\tCardIds.SneedsReplicator_ReplicateEnchantment,\r\n\tCardIds.EarthRecollectionEnchantment, // Spirit Raptor\r\n\tCardIds.FireRecollectionEnchantment,\r\n\tCardIds.LightningRecollectionEnchantment,\r\n\tCardIds.WaterRecollectionEnchantment,\r\n\tCardIds.EarthInvocation_ElementEarthEnchantment, // Summon a 1/1\r\n\t// CardIds.FireInvocation_ElementFireEnchantment, // Attack is doubled, probably no use to keep it\r\n\t// CardIds.WaterInvocation_ElementWaterEnchantment, // +3 health and taunt, same\r\n\tCardIds.LightningInvocation, // Deal 1 damage to 5 enemy minions\r\n\tCardIds.SurfNSurf_CrabRidingEnchantment_BG27_004e,\r\n\tCardIds.SurfNSurf_CrabRidingEnchantment_BG27_004_Ge,\r\n\tCardIds.RecurringNightmare_NightmareInsideEnchantment_BG26_055e,\r\n\tCardIds.RecurringNightmare_NightmareInsideEnchantment_BG26_055_Ge,\r\n];\r\n\r\nconst cleanEnchantmentsForEntity = (\r\n\tenchantments: { cardId: string; originEntityId?: number; timing: number }[],\r\n\tentityIds: readonly number[],\r\n): { cardId: string; originEntityId?: number; timing: number }[] => {\r\n\treturn enchantments.filter(\r\n\t\t(enchant) =>\r\n\t\t\tentityIds.indexOf(enchant.originEntityId) !== -1 ||\r\n\t\t\tvalidEnchantments.indexOf(enchant.cardId as CardIds) !== -1,\r\n\t);\r\n};\r\n"]}
1
+ {"version":3,"file":"simulate-bgs-battle.js","sourceRoot":"","sources":["../src/simulate-bgs-battle.ts"],"names":[],"mappings":";;;AACA,iEAAwE;AAIxE,mDAA+C;AAE/C,8CAA0E;AAC1E,sDAAmD;AACnD,gEAA6D;AAC7D,mCAA8C;AAE9C,MAAM,KAAK,GAAG,IAAI,gCAAe,EAAE,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M,CAAC,UAAU,EACzE;QACD,OAAO,CAAC,IAAI,CAAC,2BAA2B,EAAE,gBAAgB,CAAC,CAAC;KAC5D;AACF,CAAC,CAAC;AAEF,MAAM,aAAa,GAAG,CAAC,YAAoB,EAAE,YAAoB,EAAE,UAAkB,EAAU,EAAE;IAChG,IAAI,YAAY,KAAK,CAAC,IAAI,YAAY,KAAK,CAAC,EAAE;QAC7C,OAAO,IAAI,CAAC;KACZ;IACD,IAAI,YAAY,KAAK,GAAG,IAAI,YAAY,KAAK,UAAU,EAAE;QACxD,OAAO,IAAI,CAAC;KACZ;IACD,OAAO,YAAY,CAAC;AACrB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,CAAC,KAA6B,EAA0B,EAAE;IACnF,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzD,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAC7B,GAAG,MAAM;QACT,YAAY,EAAE,0BAA0B,CAAC,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC;KACxE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEW,QAAA,iBAAiB,GAAG;;;;;;;;;;;;;;;;;;CAoBhC,CAAC;AAEF,MAAM,0BAA0B,GAAG,CAClC,YAA2E,EAC3E,SAA4B,EACoC,EAAE;IAClE,OAAO,YAAY,CAAC,MAAM,CACzB,CAAC,OAAO,EAAE,EAAE,CACX,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAChD,yBAAiB,CAAC,OAAO,CAAC,OAAO,CAAC,MAAiB,CAAC,KAAK,CAAC,CAAC,CAC5D,CAAC;AACH,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\r\nimport { AllCardsService, CardIds } from '@firestone-hs/reference-data';\r\nimport { BgsBattleInfo } from './bgs-battle-info';\r\nimport { BgsBoardInfo } from './bgs-board-info';\r\nimport { BoardEntity } from './board-entity';\r\nimport { CardsData } from './cards/cards-data';\r\nimport { SimulationResult } from './simulation-result';\r\nimport { setImplicitDataHero, setMissingAuras } from './simulation/auras';\r\nimport { Simulator } from './simulation/simulator';\r\nimport { Spectator } from './simulation/spectator/spectator';\r\nimport { addImpliedMechanics } from './utils';\r\n\r\nconst cards = new AllCardsService();\r\n\r\n// This example demonstrates a NodeJS 8.10 async handler[1], however of course you could use\r\n// the more traditional callback-style handler.\r\n// [1]: https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/\r\nexport default async (event): Promise<any> => {\r\n\tconst battleInput: BgsBattleInfo = JSON.parse(event.body);\r\n\tawait cards.initializeCardsDb();\r\n\tconst cardsData = new CardsData(cards, false);\r\n\tcardsData.inititialize(\r\n\t\tbattleInput.gameState?.validTribes ?? battleInput.options?.validTribes,\r\n\t\tbattleInput.gameState?.anomalies ?? [],\r\n\t);\r\n\tconst simulationResult = simulateBattle(battleInput, cards, cardsData);\r\n\r\n\tconst response = {\r\n\t\tstatusCode: 200,\r\n\t\tisBase64Encoded: false,\r\n\t\tbody: JSON.stringify(simulationResult),\r\n\t};\r\n\treturn response;\r\n};\r\n\r\nexport const simulateBattle = (\r\n\tbattleInput: BgsBattleInfo,\r\n\tcards: AllCardsService,\r\n\tcardsData: CardsData,\r\n): SimulationResult => {\r\n\t// !battleInput.options?.skipInfoLogs && console.time('full-sim');\r\n\tconst start = Date.now();\r\n\tconst maxAcceptableDuration = battleInput.options?.maxAcceptableDuration || 8000;\r\n\tconst numberOfSimulations = battleInput.options?.numberOfSimulations || 5000;\r\n\tconst simulationResult: SimulationResult = {\r\n\t\twonLethal: 0,\r\n\t\twon: 0,\r\n\t\ttied: 0,\r\n\t\tlost: 0,\r\n\t\tlostLethal: 0,\r\n\t\tdamageWon: 0,\r\n\t\tdamageLost: 0,\r\n\t\twonLethalPercent: undefined,\r\n\t\twonPercent: undefined,\r\n\t\ttiedPercent: undefined,\r\n\t\tlostPercent: undefined,\r\n\t\tlostLethalPercent: undefined,\r\n\t\taverageDamageWon: undefined,\r\n\t\taverageDamageLost: undefined,\r\n\t};\r\n\r\n\tconst spectator = new Spectator(\r\n\t\tbattleInput.playerBoard.player.cardId,\r\n\t\tbattleInput.playerBoard.player.heroPowerId,\r\n\t\tbattleInput.opponentBoard.player.cardId,\r\n\t\tbattleInput.opponentBoard.player.heroPowerId,\r\n\t);\r\n\tconst inputReady = buildFinalInput(battleInput, cards, cardsData);\r\n\t// const inputStr = JSON.stringify(inputReady);\r\n\t!battleInput.options?.skipInfoLogs && console.time('simulation');\r\n\tconst outcomes = {};\r\n\tfor (let i = 0; i < numberOfSimulations; i++) {\r\n\t\t// const input: BgsBattleInfo = cloneInput(inputReady);\r\n\t\t// const input: BgsBattleInfo = cloneInput2(inputStr);\r\n\t\tconst input: BgsBattleInfo = cloneInput3(inputReady);\r\n\t\tconst simulator = new Simulator(cards, cardsData);\r\n\t\tconst battleResult = simulator.simulateSingleBattle(\r\n\t\t\tinput.playerBoard.board,\r\n\t\t\tinput.playerBoard.player,\r\n\t\t\tinput.opponentBoard.board,\r\n\t\t\tinput.opponentBoard.player,\r\n\t\t\tinput.gameState,\r\n\t\t\tspectator,\r\n\t\t);\r\n\t\tif (Date.now() - start > maxAcceptableDuration) {\r\n\t\t\t// Can happen in case of inifinite boards, or a bug. Don't hog the user's computer in that case\r\n\t\t\tconsole.warn('Stopping simulation after', i, 'iterations and ', Date.now() - start, 'ms', battleResult);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tif (!battleResult) {\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tif (battleResult.result === 'won') {\r\n\t\t\tsimulationResult.won++;\r\n\t\t\tsimulationResult.damageWon += battleResult.damageDealt;\r\n\t\t\tif (battleResult.damageDealt >= battleInput.opponentBoard.player.hpLeft) {\r\n\t\t\t\tsimulationResult.wonLethal++;\r\n\t\t\t}\r\n\t\t} else if (battleResult.result === 'lost') {\r\n\t\t\tsimulationResult.lost++;\r\n\t\t\tsimulationResult.damageLost += battleResult.damageDealt;\r\n\t\t\toutcomes[battleResult.damageDealt] = (outcomes[battleResult.damageDealt] ?? 0) + 1;\r\n\t\t\tif (\r\n\t\t\t\tbattleInput.playerBoard.player.hpLeft &&\r\n\t\t\t\tbattleResult.damageDealt >= battleInput.playerBoard.player.hpLeft\r\n\t\t\t) {\r\n\t\t\t\tsimulationResult.lostLethal++;\r\n\t\t\t}\r\n\t\t} else if (battleResult.result === 'tied') {\r\n\t\t\tsimulationResult.tied++;\r\n\t\t}\r\n\t\tspectator.commitBattleResult(battleResult.result);\r\n\t}\r\n\tupdateSimulationResult(simulationResult, inputReady);\r\n\t!battleInput.options?.skipInfoLogs && console.timeEnd('simulation');\r\n\tspectator.prune();\r\n\tsimulationResult.outcomeSamples = spectator.buildOutcomeSamples();\r\n\t// !battleInput.options?.skipInfoLogs && console.timeEnd('full-sim');\r\n\treturn simulationResult;\r\n};\r\n\r\nconst cloneInput = (input: BgsBattleInfo): BgsBattleInfo => {\r\n\treturn structuredClone(input);\r\n};\r\nconst cloneInput2 = (input: string): BgsBattleInfo => {\r\n\treturn JSON.parse(input);\r\n};\r\nconst cloneInput3 = (input: BgsBattleInfo): BgsBattleInfo => {\r\n\tconst result: BgsBattleInfo = {\r\n\t\tgameState: {\r\n\t\t\tcurrentTurn: input.gameState.currentTurn,\r\n\t\t\tanomalies: input.gameState.anomalies,\r\n\t\t\tvalidTribes: input.gameState.validTribes,\r\n\t\t},\r\n\t\theroHasDied: input.heroHasDied,\r\n\t\tplayerBoard: cloneBoard(input.playerBoard),\r\n\t\topponentBoard: cloneBoard(input.opponentBoard),\r\n\t\toptions: null,\r\n\t};\r\n\treturn result;\r\n};\r\nconst cloneBoard = (board: BgsBoardInfo): BgsBoardInfo => {\r\n\tconst result: BgsBoardInfo = {\r\n\t\tplayer: {\r\n\t\t\t...board.player,\r\n\t\t\thand: board.player.hand?.map((entity) => cloneEntity(entity)),\r\n\t\t\tsecrets: board.player.secrets?.map((secret) => ({ ...secret })),\r\n\t\t\tglobalInfo: { ...board.player.globalInfo },\r\n\t\t},\r\n\t\tboard: board.board.map((entity) => cloneEntity(entity)),\r\n\t};\r\n\treturn result;\r\n};\r\nconst cloneEntity = (entity: BoardEntity): BoardEntity => {\r\n\tconst result: BoardEntity = {\r\n\t\t...entity,\r\n\t\tenchantments: entity.enchantments?.map((enchant) => ({ ...enchant })),\r\n\t};\r\n\treturn result;\r\n};\r\n\r\nconst buildFinalInput = (battleInput: BgsBattleInfo, cards: AllCardsService, cardsData: CardsData): BgsBattleInfo => {\r\n\tconst playerInfo = battleInput.playerBoard;\r\n\tconst opponentInfo = battleInput.opponentBoard;\r\n\r\n\tconst playerBoard = playerInfo.board.map(\r\n\t\t(entity) => ({ ...addImpliedMechanics(entity, cardsData), friendly: true } as BoardEntity),\r\n\t);\r\n\tconst playerHand = playerInfo.player.hand?.map(\r\n\t\t(entity) =>\r\n\t\t\t({\r\n\t\t\t\t...addImpliedMechanics(entity, cardsData),\r\n\t\t\t\tfriendly: true,\r\n\t\t\t\t// locked: cardsData.getTavernLevel(entity.cardId) > playerInfo.player.tavernTier,\r\n\t\t\t} as BoardEntity),\r\n\t);\r\n\tplayerInfo.player.secrets = playerInfo.secrets?.filter((e) => !!e?.cardId);\r\n\tplayerInfo.player.friendly = true;\r\n\tconst opponentBoard = opponentInfo.board.map(\r\n\t\t(entity) =>\r\n\t\t\t({\r\n\t\t\t\t...addImpliedMechanics(entity, cardsData),\r\n\t\t\t\tfriendly: false,\r\n\t\t\t\t// locked: cardsData.getTavernLevel(entity.cardId) > playerInfo.player.tavernTier,\r\n\t\t\t} as BoardEntity),\r\n\t);\r\n\tconst opponentHand = opponentInfo.player.hand?.map(\r\n\t\t(entity) => ({ ...addImpliedMechanics(entity, cardsData), friendly: false } as BoardEntity),\r\n\t);\r\n\topponentInfo.player.secrets = opponentInfo.secrets?.filter((e) => !!e?.cardId);\r\n\topponentInfo.player.friendly = false;\r\n\r\n\t// When using the simulator, the aura is not applied when receiving the board state. When\r\n\tsetMissingAuras(playerBoard, playerInfo.player, opponentInfo.player, cards);\r\n\tsetMissingAuras(opponentBoard, opponentInfo.player, playerInfo.player, cards);\r\n\t// Avenge, maxHealth, etc.\r\n\t// setImplicitData(playerBoard, cardsData);\r\n\t// setImplicitData(opponentBoard, cardsData);\r\n\t// Avenge, globalInfo\r\n\tsetImplicitDataHero(playerInfo.player, cardsData, true);\r\n\tsetImplicitDataHero(opponentInfo.player, cardsData, false);\r\n\r\n\t// We do this so that we can have mutated objects inside the simulation and still\r\n\t// be able to start from a fresh copy for each simulation\r\n\tconst inputReady: BgsBattleInfo = {\r\n\t\tplayerBoard: {\r\n\t\t\tboard: playerBoard,\r\n\t\t\tplayer: {\r\n\t\t\t\t...playerInfo.player,\r\n\t\t\t\thand: playerHand,\r\n\t\t\t},\r\n\t\t},\r\n\t\topponentBoard: {\r\n\t\t\tboard: opponentBoard,\r\n\t\t\tplayer: {\r\n\t\t\t\t...opponentInfo.player,\r\n\t\t\t\thand: opponentHand,\r\n\t\t\t},\r\n\t\t},\r\n\t\tgameState: battleInput.gameState,\r\n\t} as BgsBattleInfo;\r\n\treturn inputReady;\r\n};\r\n\r\nconst updateSimulationResult = (simulationResult: SimulationResult, input: BgsBattleInfo) => {\r\n\tconst totalMatches = simulationResult.won + simulationResult.tied + simulationResult.lost;\r\n\tsimulationResult.wonPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.won)) / totalMatches) / 10,\r\n\t\tsimulationResult.won,\r\n\t\ttotalMatches,\r\n\t);\r\n\tsimulationResult.wonLethalPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.wonLethal)) / totalMatches) / 10,\r\n\t\tsimulationResult.wonLethal,\r\n\t\ttotalMatches,\r\n\t);\r\n\tsimulationResult.lostPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.lost)) / totalMatches) / 10,\r\n\t\tsimulationResult.lost,\r\n\t\ttotalMatches,\r\n\t);\r\n\tsimulationResult.lostLethalPercent = checkRounding(\r\n\t\tMath.round((10 * (100 * simulationResult.lostLethal)) / totalMatches) / 10,\r\n\t\tsimulationResult.lostLethal,\r\n\t\ttotalMatches,\r\n\t);\r\n\t// simulationResult.tiedPercent = checkRounding(Math.round((10 * (100 * simulationResult.tied)) / totalMatches) / 10, simulationResult.tied, totalMatches);\r\n\tsimulationResult.tiedPercent = checkRounding(\r\n\t\t100 - simulationResult.lostPercent - simulationResult.wonPercent,\r\n\t\tsimulationResult.tied,\r\n\t\ttotalMatches,\r\n\t);\r\n\r\n\tsimulationResult.wonLethalPercent = Math.round((10 * (100 * simulationResult.wonLethal)) / totalMatches) / 10;\r\n\tsimulationResult.lostLethalPercent = Math.round((10 * (100 * simulationResult.lostLethal)) / totalMatches) / 10;\r\n\tsimulationResult.averageDamageWon = simulationResult.won ? simulationResult.damageWon / simulationResult.won : 0;\r\n\tsimulationResult.averageDamageLost = simulationResult.lost\r\n\t\t? simulationResult.damageLost / simulationResult.lost\r\n\t\t: 0;\r\n\tif (\r\n\t\tsimulationResult.averageDamageWon > 0 &&\r\n\t\tsimulationResult.averageDamageWon < input.playerBoard.player.tavernTier\r\n\t) {\r\n\t\tconsole.warn('average damage won issue');\r\n\t}\r\n\tif (\r\n\t\tsimulationResult.averageDamageLost > 0 &&\r\n\t\tsimulationResult.averageDamageLost < input.opponentBoard.player.tavernTier\r\n\t) {\r\n\t\tconsole.warn('average damage lost issue', simulationResult);\r\n\t}\r\n};\r\n\r\nconst checkRounding = (roundedValue: number, initialValue: number, totalValue: number): number => {\r\n\tif (roundedValue === 0 && initialValue !== 0) {\r\n\t\treturn 0.01;\r\n\t}\r\n\tif (roundedValue === 100 && initialValue !== totalValue) {\r\n\t\treturn 99.9;\r\n\t}\r\n\treturn roundedValue;\r\n};\r\n\r\nconst cleanEnchantments = (board: readonly BoardEntity[]): readonly BoardEntity[] => {\r\n\tconst entityIds = board.map((entity) => entity.entityId);\r\n\treturn board.map((entity) => ({\r\n\t\t...entity,\r\n\t\tenchantments: cleanEnchantmentsForEntity(entity.enchantments, entityIds),\r\n\t}));\r\n};\r\n\r\nexport const validEnchantments = [\r\n\tCardIds.ReplicatingMenace_ReplicatingMenaceEnchantment_BG_BOT_312e,\r\n\tCardIds.ReplicatingMenace_ReplicatingMenaceEnchantment_TB_BaconUps_032e,\r\n\tCardIds.LivingSpores_LivingSporesEnchantment,\r\n\tCardIds.Leapfrogger_LeapfrogginEnchantment_BG21_000e,\r\n\tCardIds.Leapfrogger_LeapfrogginEnchantment_BG21_000_Ge,\r\n\tCardIds.Sneed_SneedsReplicator,\r\n\tCardIds.SneedsReplicator_ReplicateEnchantment,\r\n\tCardIds.EarthRecollectionEnchantment, // Spirit Raptor\r\n\tCardIds.FireRecollectionEnchantment,\r\n\tCardIds.LightningRecollectionEnchantment,\r\n\tCardIds.WaterRecollectionEnchantment,\r\n\tCardIds.EarthInvocation_ElementEarthEnchantment, // Summon a 1/1\r\n\t// CardIds.FireInvocation_ElementFireEnchantment, // Attack is doubled, probably no use to keep it\r\n\t// CardIds.WaterInvocation_ElementWaterEnchantment, // +3 health and taunt, same\r\n\tCardIds.LightningInvocation, // Deal 1 damage to 5 enemy minions\r\n\tCardIds.SurfNSurf_CrabRidingEnchantment_BG27_004e,\r\n\tCardIds.SurfNSurf_CrabRidingEnchantment_BG27_004_Ge,\r\n\tCardIds.RecurringNightmare_NightmareInsideEnchantment_BG26_055e,\r\n\tCardIds.RecurringNightmare_NightmareInsideEnchantment_BG26_055_Ge,\r\n];\r\n\r\nconst cleanEnchantmentsForEntity = (\r\n\tenchantments: { cardId: string; originEntityId?: number; timing: number }[],\r\n\tentityIds: readonly number[],\r\n): { cardId: string; originEntityId?: number; timing: number }[] => {\r\n\treturn enchantments.filter(\r\n\t\t(enchant) =>\r\n\t\t\tentityIds.indexOf(enchant.originEntityId) !== -1 ||\r\n\t\t\tvalidEnchantments.indexOf(enchant.cardId as CardIds) !== -1,\r\n\t);\r\n};\r\n"]}
@@ -4,7 +4,7 @@ import { BoardEntity } from '../board-entity';
4
4
  import { CardsData } from '../cards/cards-data';
5
5
  import { SharedState } from './shared-state';
6
6
  import { Spectator } from './spectator/spectator';
7
- export declare const simulateAttack: (attackingBoard: BoardEntity[], attackingBoardHero: BgsPlayerEntity, defendingBoard: BoardEntity[], defendingBoardHero: BgsPlayerEntity, allCards: AllCardsService, spawns: CardsData, sharedState: SharedState, spectator: Spectator) => number;
7
+ export declare const simulateAttack: (attackingBoard: BoardEntity[], attackingBoardHero: BgsPlayerEntity, defendingBoard: BoardEntity[], defendingBoardHero: BgsPlayerEntity, allCards: AllCardsService, spawns: CardsData, sharedState: SharedState, spectator: Spectator) => void;
8
8
  export declare const doFullAttack: (attackingEntity: BoardEntity, attackingBoard: BoardEntity[], attackingBoardHero: BgsPlayerEntity, defendingEntity: BoardEntity, defendingBoard: BoardEntity[], defendingBoardHero: BgsPlayerEntity, allCards: AllCardsService, spawns: CardsData, sharedState: SharedState, spectator: Spectator) => void;
9
9
  export declare const findNearestEnemies: (attackingBoard: BoardEntity[], entity: BoardEntity, entityIndexFromRight: number, defendingBoard: BoardEntity[], numberOfTargets: number, allCards: AllCardsService) => BoardEntity[];
10
10
  export declare const getNeighbours: (board: BoardEntity[], entity: BoardEntity, deadEntityIndexFromRight?: number) => readonly BoardEntity[];
@@ -20,7 +20,6 @@ const simulateAttack = (attackingBoard, attackingBoardHero, defendingBoard, defe
20
20
  return;
21
21
  }
22
22
  const attackingEntity = getAttackingEntity(attackingBoard, allCards);
23
- const attackingEntityIndex = attackingBoard.map((e) => e.entityId).indexOf(attackingEntity === null || attackingEntity === void 0 ? void 0 : attackingEntity.entityId);
24
23
  if (attackingEntity) {
25
24
  attackingEntity.attacking = true;
26
25
  const numberOfAttacks = attackingEntity.windfury ? 2 : 1;
@@ -41,7 +40,6 @@ const simulateAttack = (attackingBoard, attackingBoardHero, defendingBoard, defe
41
40
  attackingEntity.attacking = false;
42
41
  attackingEntity.hasAttacked = true;
43
42
  }
44
- return attackingEntityIndex;
45
43
  };
46
44
  exports.simulateAttack = simulateAttack;
47
45
  const doFullAttack = (attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, allCards, spawns, sharedState, spectator) => {