board-game-engine 2.0.1 → 2.1.1

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 (251) hide show
  1. package/dist/board-game-engine.cjs +5509 -5613
  2. package/dist/board-game-engine.js +5509 -5613
  3. package/dist/board-game-engine.min.js +22 -22
  4. package/dist/board-game-engine.mjs +5508 -5611
  5. package/package.json +4 -10
  6. package/playwright-report/index.html +1 -1
  7. package/scripts/build.mjs +2 -2
  8. package/src/client/client.js +224 -0
  9. package/src/game-factory/bank/bank-slot.js +69 -0
  10. package/src/game-factory/bank/bank.js +114 -0
  11. package/src/game-factory/{board.ts → board.js} +1 -1
  12. package/src/game-factory/condition/condition-factory.js +52 -0
  13. package/src/game-factory/condition/condition.js +39 -0
  14. package/src/game-factory/condition/{contains-condition.ts → contains-condition.js} +4 -5
  15. package/src/game-factory/condition/{contains-same-condition.ts → contains-same-condition.js} +5 -8
  16. package/src/game-factory/condition/{evaluate-condition.ts → evaluate-condition.js} +3 -4
  17. package/src/game-factory/condition/every-condition.js +25 -0
  18. package/src/game-factory/condition/has-line-condition.js +14 -0
  19. package/src/game-factory/condition/in-line-condition.js +19 -0
  20. package/src/game-factory/condition/is-condition.js +23 -0
  21. package/src/game-factory/condition/is-full-condition.js +9 -0
  22. package/src/game-factory/condition/{no-possible-moves-condition.ts → no-possible-moves-condition.js} +2 -3
  23. package/src/game-factory/condition/{not-condition.ts → not-condition.js} +2 -3
  24. package/src/game-factory/condition/{or-condition.ts → or-condition.js} +2 -3
  25. package/src/game-factory/condition/position-condition.js +12 -0
  26. package/src/game-factory/condition/{some-condition.ts → some-condition.js} +3 -5
  27. package/src/game-factory/condition/would-condition.js +94 -0
  28. package/src/game-factory/entity.js +29 -0
  29. package/src/game-factory/expand-game-rules.js +271 -0
  30. package/src/game-factory/game-factory.js +240 -0
  31. package/{dist/game-factory/move/end-turn.d.ts → src/game-factory/move/end-turn.js} +4 -2
  32. package/src/game-factory/move/for-each.js +18 -0
  33. package/src/game-factory/move/move-entity.js +16 -0
  34. package/src/game-factory/move/move-factory.js +89 -0
  35. package/src/game-factory/move/move.js +131 -0
  36. package/src/game-factory/move/pass-turn.js +10 -0
  37. package/{dist/game-factory/move/pass.d.ts → src/game-factory/move/pass.js} +4 -2
  38. package/src/game-factory/move/place-new.js +33 -0
  39. package/src/game-factory/move/remove-entity.js +7 -0
  40. package/src/game-factory/move/set-active-players.js +23 -0
  41. package/src/game-factory/move/set-state.js +11 -0
  42. package/src/game-factory/move/shuffle.js +7 -0
  43. package/src/game-factory/move/take-from.js +7 -0
  44. package/src/game-factory/space/space.js +30 -0
  45. package/src/game-factory/space-group/grid.js +43 -0
  46. package/src/game-factory/space-group/space-group.js +29 -0
  47. package/src/index.js +2 -0
  48. package/src/utils/{any-valid-moves.ts → any-valid-moves.js} +49 -54
  49. package/src/utils/check-conditions.js +28 -0
  50. package/src/utils/create-payload.js +16 -0
  51. package/src/utils/deserialize-bgio-arguments.js +8 -0
  52. package/src/utils/do-moves.js +18 -0
  53. package/src/utils/entity-matches.js +20 -0
  54. package/src/utils/find-met-condition.js +22 -0
  55. package/src/utils/get-current-moves.js +12 -0
  56. package/src/utils/get-scenario-results.js +23 -0
  57. package/src/utils/get-steps.js +29 -0
  58. package/src/utils/get.js +25 -0
  59. package/src/utils/{grid-contains-sequence.ts → grid-contains-sequence.js} +33 -71
  60. package/src/utils/json-transformer.js +12 -0
  61. package/src/utils/prepare-payload.js +17 -0
  62. package/src/utils/resolve-entity.js +9 -0
  63. package/src/utils/resolve-expression.js +10 -0
  64. package/src/utils/resolve-properties.js +149 -0
  65. package/src/utils/simulate-move.js +25 -0
  66. package/.github/workflows/ci.yml +0 -33
  67. package/dist/client/client.d.ts +0 -76
  68. package/dist/client/client.d.ts.map +0 -1
  69. package/dist/game-factory/bank/bank-slot.d.ts +0 -30
  70. package/dist/game-factory/bank/bank-slot.d.ts.map +0 -1
  71. package/dist/game-factory/bank/bank.d.ts +0 -34
  72. package/dist/game-factory/bank/bank.d.ts.map +0 -1
  73. package/dist/game-factory/board.d.ts +0 -4
  74. package/dist/game-factory/board.d.ts.map +0 -1
  75. package/dist/game-factory/condition/condition-factory.d.ts +0 -4
  76. package/dist/game-factory/condition/condition-factory.d.ts.map +0 -1
  77. package/dist/game-factory/condition/condition.d.ts +0 -13
  78. package/dist/game-factory/condition/condition.d.ts.map +0 -1
  79. package/dist/game-factory/condition/contains-condition.d.ts +0 -8
  80. package/dist/game-factory/condition/contains-condition.d.ts.map +0 -1
  81. package/dist/game-factory/condition/contains-same-condition.d.ts +0 -7
  82. package/dist/game-factory/condition/contains-same-condition.d.ts.map +0 -1
  83. package/dist/game-factory/condition/evaluate-condition.d.ts +0 -8
  84. package/dist/game-factory/condition/evaluate-condition.d.ts.map +0 -1
  85. package/dist/game-factory/condition/every-condition.d.ts +0 -12
  86. package/dist/game-factory/condition/every-condition.d.ts.map +0 -1
  87. package/dist/game-factory/condition/has-line-condition.d.ts +0 -8
  88. package/dist/game-factory/condition/has-line-condition.d.ts.map +0 -1
  89. package/dist/game-factory/condition/in-line-condition.d.ts +0 -8
  90. package/dist/game-factory/condition/in-line-condition.d.ts.map +0 -1
  91. package/dist/game-factory/condition/is-condition.d.ts +0 -8
  92. package/dist/game-factory/condition/is-condition.d.ts.map +0 -1
  93. package/dist/game-factory/condition/is-full-condition.d.ts +0 -7
  94. package/dist/game-factory/condition/is-full-condition.d.ts.map +0 -1
  95. package/dist/game-factory/condition/no-possible-moves-condition.d.ts +0 -7
  96. package/dist/game-factory/condition/no-possible-moves-condition.d.ts.map +0 -1
  97. package/dist/game-factory/condition/not-condition.d.ts +0 -7
  98. package/dist/game-factory/condition/not-condition.d.ts.map +0 -1
  99. package/dist/game-factory/condition/or-condition.d.ts +0 -7
  100. package/dist/game-factory/condition/or-condition.d.ts.map +0 -1
  101. package/dist/game-factory/condition/position-condition.d.ts +0 -7
  102. package/dist/game-factory/condition/position-condition.d.ts.map +0 -1
  103. package/dist/game-factory/condition/some-condition.d.ts +0 -8
  104. package/dist/game-factory/condition/some-condition.d.ts.map +0 -1
  105. package/dist/game-factory/condition/would-condition.d.ts +0 -8
  106. package/dist/game-factory/condition/would-condition.d.ts.map +0 -1
  107. package/dist/game-factory/entity.d.ts +0 -13
  108. package/dist/game-factory/entity.d.ts.map +0 -1
  109. package/dist/game-factory/expand-game-rules.d.ts +0 -3
  110. package/dist/game-factory/expand-game-rules.d.ts.map +0 -1
  111. package/dist/game-factory/game-factory.d.ts +0 -10
  112. package/dist/game-factory/game-factory.d.ts.map +0 -1
  113. package/dist/game-factory/move/end-turn.d.ts.map +0 -1
  114. package/dist/game-factory/move/for-each.d.ts +0 -5
  115. package/dist/game-factory/move/for-each.d.ts.map +0 -1
  116. package/dist/game-factory/move/index.d.ts +0 -6
  117. package/dist/game-factory/move/index.d.ts.map +0 -1
  118. package/dist/game-factory/move/move-entity.d.ts +0 -7
  119. package/dist/game-factory/move/move-entity.d.ts.map +0 -1
  120. package/dist/game-factory/move/move-factory.d.ts +0 -18
  121. package/dist/game-factory/move/move-factory.d.ts.map +0 -1
  122. package/dist/game-factory/move/move.d.ts +0 -54
  123. package/dist/game-factory/move/move.d.ts.map +0 -1
  124. package/dist/game-factory/move/pass-turn.d.ts +0 -5
  125. package/dist/game-factory/move/pass-turn.d.ts.map +0 -1
  126. package/dist/game-factory/move/pass.d.ts.map +0 -1
  127. package/dist/game-factory/move/place-new.d.ts +0 -5
  128. package/dist/game-factory/move/place-new.d.ts.map +0 -1
  129. package/dist/game-factory/move/remove-entity.d.ts +0 -5
  130. package/dist/game-factory/move/remove-entity.d.ts.map +0 -1
  131. package/dist/game-factory/move/set-active-players.d.ts +0 -5
  132. package/dist/game-factory/move/set-active-players.d.ts.map +0 -1
  133. package/dist/game-factory/move/set-state.d.ts +0 -5
  134. package/dist/game-factory/move/set-state.d.ts.map +0 -1
  135. package/dist/game-factory/move/shuffle.d.ts +0 -5
  136. package/dist/game-factory/move/shuffle.d.ts.map +0 -1
  137. package/dist/game-factory/move/take-from.d.ts +0 -11
  138. package/dist/game-factory/move/take-from.d.ts.map +0 -1
  139. package/dist/game-factory/space/space.d.ts +0 -10
  140. package/dist/game-factory/space/space.d.ts.map +0 -1
  141. package/dist/game-factory/space-group/grid.d.ts +0 -15
  142. package/dist/game-factory/space-group/grid.d.ts.map +0 -1
  143. package/dist/game-factory/space-group/space-group.d.ts +0 -20
  144. package/dist/game-factory/space-group/space-group.d.ts.map +0 -1
  145. package/dist/index.d.ts +0 -6
  146. package/dist/index.d.ts.map +0 -1
  147. package/dist/registry.d.ts +0 -17
  148. package/dist/registry.d.ts.map +0 -1
  149. package/dist/types/bagel-types.d.ts +0 -339
  150. package/dist/types/bagel-types.d.ts.map +0 -1
  151. package/dist/types/index.d.ts +0 -3
  152. package/dist/types/index.d.ts.map +0 -1
  153. package/dist/types/rule-with-conditions.d.ts +0 -9
  154. package/dist/types/rule-with-conditions.d.ts.map +0 -1
  155. package/dist/utils/any-valid-moves.d.ts +0 -2
  156. package/dist/utils/any-valid-moves.d.ts.map +0 -1
  157. package/dist/utils/bgio-resolve-types.d.ts +0 -25
  158. package/dist/utils/bgio-resolve-types.d.ts.map +0 -1
  159. package/dist/utils/check-conditions.d.ts +0 -7
  160. package/dist/utils/check-conditions.d.ts.map +0 -1
  161. package/dist/utils/create-payload.d.ts +0 -5
  162. package/dist/utils/create-payload.d.ts.map +0 -1
  163. package/dist/utils/deserialize-bgio-arguments.d.ts +0 -3
  164. package/dist/utils/deserialize-bgio-arguments.d.ts.map +0 -1
  165. package/dist/utils/do-moves.d.ts +0 -8
  166. package/dist/utils/do-moves.d.ts.map +0 -1
  167. package/dist/utils/entity-matches.d.ts +0 -6
  168. package/dist/utils/entity-matches.d.ts.map +0 -1
  169. package/dist/utils/find-met-condition.d.ts +0 -6
  170. package/dist/utils/find-met-condition.d.ts.map +0 -1
  171. package/dist/utils/get-current-moves.d.ts +0 -24
  172. package/dist/utils/get-current-moves.d.ts.map +0 -1
  173. package/dist/utils/get-scenario-results.d.ts +0 -3
  174. package/dist/utils/get-scenario-results.d.ts.map +0 -1
  175. package/dist/utils/get-steps.d.ts +0 -13
  176. package/dist/utils/get-steps.d.ts.map +0 -1
  177. package/dist/utils/get.d.ts +0 -7
  178. package/dist/utils/get.d.ts.map +0 -1
  179. package/dist/utils/grid-contains-sequence.d.ts +0 -27
  180. package/dist/utils/grid-contains-sequence.d.ts.map +0 -1
  181. package/dist/utils/json-transformer.d.ts +0 -8
  182. package/dist/utils/json-transformer.d.ts.map +0 -1
  183. package/dist/utils/prepare-payload.d.ts +0 -2
  184. package/dist/utils/prepare-payload.d.ts.map +0 -1
  185. package/dist/utils/resolve-entity.d.ts +0 -3
  186. package/dist/utils/resolve-entity.d.ts.map +0 -1
  187. package/dist/utils/resolve-expression.d.ts +0 -6
  188. package/dist/utils/resolve-expression.d.ts.map +0 -1
  189. package/dist/utils/resolve-properties.d.ts +0 -4
  190. package/dist/utils/resolve-properties.d.ts.map +0 -1
  191. package/dist/utils/simulate-move.d.ts +0 -16
  192. package/dist/utils/simulate-move.d.ts.map +0 -1
  193. package/src/client/client.ts +0 -306
  194. package/src/game-factory/bank/bank-slot.ts +0 -81
  195. package/src/game-factory/bank/bank.ts +0 -125
  196. package/src/game-factory/condition/condition-factory.ts +0 -59
  197. package/src/game-factory/condition/condition.ts +0 -50
  198. package/src/game-factory/condition/every-condition.ts +0 -27
  199. package/src/game-factory/condition/has-line-condition.ts +0 -15
  200. package/src/game-factory/condition/in-line-condition.ts +0 -25
  201. package/src/game-factory/condition/is-condition.ts +0 -24
  202. package/src/game-factory/condition/is-full-condition.ts +0 -10
  203. package/src/game-factory/condition/position-condition.ts +0 -13
  204. package/src/game-factory/condition/would-condition.ts +0 -104
  205. package/src/game-factory/entity.ts +0 -37
  206. package/src/game-factory/expand-game-rules.ts +0 -263
  207. package/src/game-factory/game-factory.ts +0 -263
  208. package/src/game-factory/move/end-turn.ts +0 -7
  209. package/src/game-factory/move/for-each.ts +0 -20
  210. package/src/game-factory/move/move-entity.ts +0 -18
  211. package/src/game-factory/move/move-factory.ts +0 -107
  212. package/src/game-factory/move/move.ts +0 -147
  213. package/src/game-factory/move/pass-turn.ts +0 -15
  214. package/src/game-factory/move/pass.ts +0 -7
  215. package/src/game-factory/move/place-new.ts +0 -42
  216. package/src/game-factory/move/remove-entity.ts +0 -11
  217. package/src/game-factory/move/set-active-players.ts +0 -26
  218. package/src/game-factory/move/set-state.ts +0 -14
  219. package/src/game-factory/move/shuffle.ts +0 -9
  220. package/src/game-factory/move/take-from.ts +0 -12
  221. package/src/game-factory/space/space.ts +0 -36
  222. package/src/game-factory/space-group/grid.ts +0 -48
  223. package/src/game-factory/space-group/space-group.ts +0 -44
  224. package/src/index.ts +0 -5
  225. package/src/types/bagel-types.ts +0 -449
  226. package/src/types/boardgame-io-core.d.ts +0 -7
  227. package/src/types/index.ts +0 -70
  228. package/src/types/rule-with-conditions.ts +0 -9
  229. package/src/utils/bgio-resolve-types.ts +0 -27
  230. package/src/utils/check-conditions.ts +0 -28
  231. package/src/utils/create-payload.ts +0 -19
  232. package/src/utils/deserialize-bgio-arguments.ts +0 -10
  233. package/src/utils/do-moves.ts +0 -22
  234. package/src/utils/entity-matches.ts +0 -30
  235. package/src/utils/expr-eval.d.ts +0 -6
  236. package/src/utils/find-met-condition.ts +0 -23
  237. package/src/utils/get-current-moves.ts +0 -39
  238. package/src/utils/get-scenario-results.ts +0 -30
  239. package/src/utils/get-steps.ts +0 -38
  240. package/src/utils/get.ts +0 -28
  241. package/src/utils/json-transformer.ts +0 -17
  242. package/src/utils/prepare-payload.ts +0 -20
  243. package/src/utils/resolve-entity.ts +0 -15
  244. package/src/utils/resolve-expression.ts +0 -16
  245. package/src/utils/resolve-properties.ts +0 -172
  246. package/src/utils/simulate-move.ts +0 -32
  247. package/src/wackson.d.ts +0 -4
  248. package/tsconfig.build.json +0 -14
  249. package/tsconfig.json +0 -21
  250. /package/src/game-factory/move/{index.ts → index.js} +0 -0
  251. /package/src/{registry.ts → registry.js} +0 -0
@@ -1,27 +0,0 @@
1
- import type { RuleWithConditions } from "../types/rule-with-conditions.js";
2
-
3
- /** boardgame.io match args + engine `G` after setup (deserialized, many fields). */
4
- export type BgioResolveState = {
5
- ctx: Record<string, unknown>;
6
- G: Record<string, unknown>;
7
- playerID?: string;
8
- } & Record<string, unknown>;
9
-
10
- /** Bank API on `G` used by resolution and moves (subset of {@link Bank} methods). */
11
- export type BankApi = {
12
- find: (a: unknown, t: unknown, c: unknown) => unknown;
13
- findAll: (a: unknown, t: unknown, c: unknown) => unknown[];
14
- findParent: (t: unknown) => unknown;
15
- getOne: (a: BgioResolveState, rule: RuleWithConditions & { state?: unknown }, c: Record<string, unknown>) => unknown;
16
- getMultiple: (
17
- a: BgioResolveState,
18
- rule: RuleWithConditions & { state?: unknown },
19
- count: number,
20
- c: Record<string, unknown>
21
- ) => unknown[];
22
- returnToBank: (a: BgioResolveState, entity: { entityId: number; rule: Record<string, unknown> }) => void;
23
- };
24
-
25
- export function bankOf (bg: BgioResolveState): BankApi {
26
- return (bg.G as { bank: BankApi }).bank;
27
- }
@@ -1,28 +0,0 @@
1
- import conditionFactory from "../game-factory/condition/condition-factory.js";
2
- import type { Condition as ConditionRule } from "../types/bagel-types.js";
3
-
4
- export default function checkConditions (
5
- bgioArguments: unknown,
6
- conditions: ConditionRule[] = [],
7
- payload: Record<string, unknown> = {},
8
- context: Record<string, unknown> = {}
9
- ) {
10
- const results: unknown[] = [];
11
- let failedAt: unknown;
12
- for (const conditionRule of conditions) {
13
- const result = conditionFactory(conditionRule as ConditionRule)!
14
- .check(bgioArguments, payload, context);
15
- if (!(result as { conditionIsMet: boolean }).conditionIsMet) {
16
- failedAt = conditionRule;
17
- break;
18
- } else {
19
- results.push(result);
20
- }
21
- }
22
-
23
- return {
24
- results,
25
- failedAt,
26
- conditionsAreMet: results.length === conditions.length,
27
- };
28
- }
@@ -1,19 +0,0 @@
1
- import getSteps from "./get-steps.js";
2
-
3
- export default function createPayload (
4
- bgioState: Parameters<typeof getSteps>[0],
5
- moveRule: Parameters<typeof getSteps>[1],
6
- targets: unknown[],
7
- _context: Record<string, unknown>
8
- ) {
9
- const argNames = getSteps(
10
- bgioState,
11
- moveRule
12
- ).map((s) => s.argName);
13
- return {
14
- arguments: targets.reduce<Record<string, unknown>>((acc, target, i) => ({
15
- ...acc,
16
- [argNames[i]]: target,
17
- }), {}),
18
- };
19
- }
@@ -1,10 +0,0 @@
1
- import { deserialize } from "wackson";
2
- import { registry } from "../registry.js";
3
- import type { BgioResolveState } from "./bgio-resolve-types.js";
4
-
5
- export default function deserializeBgioArguments (bgioArguments: BgioResolveState): BgioResolveState {
6
- return {
7
- ...bgioArguments,
8
- G: deserialize(JSON.stringify(bgioArguments.G), registry) as Record<string, unknown>,
9
- };
10
- }
@@ -1,22 +0,0 @@
1
- import type { MoveDefinition } from "../types/bagel-types.js";
2
- import moveFactory from "../game-factory/move/move-factory.js";
3
-
4
- export default function doMoves (
5
- bgioArguments: { G: unknown; [k: string]: unknown },
6
- moves: MoveDefinition[] = [],
7
- context: { game: Record<string, unknown> } & Record<string, unknown>
8
- ) {
9
- if (!moves?.length) {
10
- return bgioArguments.G;
11
- }
12
-
13
- moves.forEach((moveRule) => {
14
- moveFactory(moveRule, context.game).moveInstance!.doMove(
15
- bgioArguments,
16
- undefined,
17
- context
18
- );
19
- });
20
-
21
- return bgioArguments.G;
22
- }
@@ -1,30 +0,0 @@
1
- import matches from "lodash/matches.js";
2
- import resolveProperties from "./resolve-properties.js";
3
- import type { BgioResolveState } from "./bgio-resolve-types.js";
4
-
5
- function resolveMatcher (
6
- bgioArguments: BgioResolveState,
7
- matcher: Record<string, unknown>,
8
- context: Record<string, unknown>
9
- ) {
10
- const resolvedMatcher = { ...matcher };
11
- delete resolvedMatcher.state;
12
- delete resolvedMatcher.stateGroups;
13
- return resolveProperties(bgioArguments, resolvedMatcher, context);
14
- }
15
-
16
- function getEntityMatcher (entity: { rule: Record<string, unknown>; state?: Record<string, unknown> }) {
17
- return {
18
- ...entity.rule,
19
- ...entity.state,
20
- };
21
- }
22
-
23
- export default function entityMatches (
24
- bgioArguments: BgioResolveState,
25
- matcher: Record<string, unknown>,
26
- entity: { rule: Record<string, unknown>; state?: Record<string, unknown> },
27
- context: Record<string, unknown>
28
- ): boolean {
29
- return matches(resolveMatcher(bgioArguments, matcher, context))(getEntityMatcher(entity));
30
- }
@@ -1,6 +0,0 @@
1
- declare module "expr-eval" {
2
- export class Parser {
3
- functions: Record<string, (...args: unknown[]) => unknown>;
4
- evaluate (expression: string, variables?: Record<string, unknown>): number;
5
- }
6
- }
@@ -1,23 +0,0 @@
1
- import conditionFactory from "../game-factory/condition/condition-factory.js";
2
- import type { Condition as ConditionRule } from "../types/bagel-types.js";
3
-
4
- export default function findMetCondition (
5
- bgioArguments: unknown,
6
- conditions: ConditionRule[] = [],
7
- payload: Record<string, unknown>,
8
- context: Record<string, unknown>,
9
- ) {
10
- let success: { conditionRule: unknown; [k: string]: unknown } | undefined;
11
- for (const conditionRule of conditions) {
12
- const result = conditionFactory(conditionRule as ConditionRule)!
13
- .check(bgioArguments, payload, context);
14
- if ((result as { conditionIsMet: boolean }).conditionIsMet) {
15
- success = {
16
- ...result as object,
17
- conditionRule,
18
- };
19
- break;
20
- }
21
- }
22
- return success;
23
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * Minimal shape of the boardgame.io client (or stand-in) used to resolve the active move set.
3
- */
4
- export interface GetCurrentMovesClient {
5
- game: {
6
- phases?: Record<string, unknown>;
7
- moves?: Record<string, unknown>;
8
- turn?: { stages?: Record<string, unknown> };
9
- };
10
- playerID?: string;
11
- stageName?: string;
12
- }
13
-
14
- /** `state` shape expected by {@link getCurrentMoves} (boardgame.io match state). */
15
- export interface GetCurrentMovesState {
16
- ctx: {
17
- phase?: string;
18
- activePlayers?: Record<string, string>;
19
- currentPlayer?: string;
20
- };
21
- }
22
-
23
- // get the most specific set of moves for current stage/phase
24
- // this will probably all break for complex stages with multiple active players
25
- export default function getCurrentMoves (
26
- state: GetCurrentMovesState,
27
- client: GetCurrentMovesClient
28
- ): Record<string, unknown> {
29
- const { game, playerID, stageName } = client;
30
- const phaseName = state.ctx.phase;
31
-
32
- const stageNameToUse = stageName ?? state.ctx.activePlayers?.[playerID ?? state.ctx.currentPlayer ?? ""];
33
- const phaseOrRoot =
34
- (game as { phases?: Record<string, unknown> }).phases?.[phaseName as string] ?? game;
35
- const stageOrPhaseOrRoot =
36
- (phaseOrRoot as { turn?: { stages?: Record<string, unknown> } }).turn?.stages?.[stageNameToUse as string] ?? phaseOrRoot;
37
-
38
- return (stageOrPhaseOrRoot as { moves?: Record<string, unknown> }).moves ?? {};
39
- }
@@ -1,30 +0,0 @@
1
- import checkConditions from "./check-conditions.js";
2
- import resolveProperties from "./resolve-properties.js";
3
- import type { BgioResolveState } from "./bgio-resolve-types.js";
4
- import type { Condition } from "../types/bagel-types.js";
5
- export default function getScenarioResults (
6
- bgioArguments: BgioResolveState,
7
- scenarios: unknown[]
8
- ) {
9
- let match: { scenario: { result?: unknown; [k: string]: unknown }; conditionResults: { results: unknown[] } } | undefined;
10
- for (const scenario of scenarios) {
11
- const conditionResults = checkConditions(
12
- bgioArguments,
13
- (scenario as { conditions?: Condition[] }).conditions
14
- );
15
- if (conditionResults.conditionsAreMet) {
16
- match = { scenario: scenario as { result?: unknown }, conditionResults };
17
- break;
18
- }
19
- }
20
-
21
- if (match?.scenario?.result) {
22
- return resolveProperties(
23
- bgioArguments,
24
- match.scenario.result,
25
- { results: match.conditionResults.results }
26
- );
27
- } else {
28
- return match;
29
- }
30
- }
@@ -1,38 +0,0 @@
1
- import { bankOf, type BgioResolveState } from "./bgio-resolve-types.js";
2
-
3
- // controls order of what players need to click first
4
- const argNamesMap: Record<string, string[]> = {
5
- PlaceNew: ["destination"],
6
- RemoveEntity: ["entity"],
7
- MoveEntity: ["entity", "destination"],
8
- TakeFrom: ["source", "destination"],
9
- SetState: ["entity", "state"],
10
- };
11
-
12
- // this might not be where special handling for setstate wants to live
13
- export default function getSteps (
14
- bgioState: BgioResolveState,
15
- moveRule: {
16
- moveType: string;
17
- arguments: Record<string, { playerChoice?: boolean; possibleValues?: unknown[]; [k: string]: unknown }>;
18
- }
19
- ) {
20
- const names = argNamesMap[moveRule.moveType];
21
- if (!names) return [];
22
- return names
23
- .filter((argName) => moveRule.arguments[argName]?.playerChoice)
24
- .map((argName) => ({
25
- argName,
26
- getClickable: argName === "state"
27
- ? () => (moveRule.arguments[argName].possibleValues as unknown[]).map((value) => ({
28
- abstract: true,
29
- ...moveRule.arguments[argName],
30
- value,
31
- }))
32
- : (context: Record<string, unknown>) => bankOf(bgioState).findAll(
33
- bgioState,
34
- moveRule.arguments[argName],
35
- context
36
- ),
37
- }));
38
- }
package/src/utils/get.ts DELETED
@@ -1,28 +0,0 @@
1
- type PathStep = string | number | { flatten: boolean; map?: (string | number)[] };
2
-
3
- export default function get (obj: unknown, pathArray: PathStep[]): unknown {
4
- let current: unknown = obj;
5
-
6
- for (const step of pathArray) {
7
- if (current === undefined) {
8
- return current;
9
- }
10
-
11
- if (step && typeof step === "object" && "flatten" in step && step.flatten) {
12
- if (!Array.isArray(current)) {
13
- return undefined;
14
- }
15
-
16
- let flat = current.flat() as unknown[];
17
-
18
- if (step.map) {
19
- flat = flat.map((item) => get(item, step.map!));
20
- }
21
- current = flat;
22
- } else {
23
- current = (current as Record<string | number, unknown>)[step as string | number];
24
- }
25
- }
26
-
27
- return current;
28
- }
@@ -1,17 +0,0 @@
1
- type TransformRule = {
2
- test: (val: unknown) => boolean;
3
- replace: (val: unknown) => unknown;
4
- };
5
-
6
- /** Recurse and replace. Circular references not allowed. */
7
- export default function transformJSON (data: unknown, rules: TransformRule[]): unknown {
8
- return JSON.parse(JSON.stringify(data), (_key, value: unknown) => {
9
- let result = value;
10
- for (const rule of rules) {
11
- if (rule.test(result)) {
12
- result = rule.replace(result);
13
- }
14
- }
15
- return result;
16
- });
17
- }
@@ -1,20 +0,0 @@
1
- import { serialize } from "wackson";
2
-
3
- // todo: remember why reducing to id was preferred?
4
- export default function preparePayload (payload: unknown): unknown {
5
- const p = payload as {
6
- arguments?: Record<string, { abstract?: boolean; entityId?: unknown }>;
7
- } | null | undefined;
8
- if (p?.arguments) {
9
- const payloadCopy = {
10
- ...p,
11
- arguments: Object.entries(p.arguments).reduce<Record<string, unknown>>((acc, [key, argument]) => ({
12
- ...acc,
13
- [key]: argument.abstract ? argument : argument.entityId,
14
- }), {}),
15
- };
16
- return JSON.parse(serialize(payloadCopy, { deduplicateInstances: false }));
17
- } else {
18
- return payload;
19
- }
20
- }
@@ -1,15 +0,0 @@
1
- import isPlainObject from "lodash/isPlainObject.js";
2
- import { bankOf, type BgioResolveState } from "./bgio-resolve-types.js";
3
-
4
- const abstractTargetNames = ["state"];
5
-
6
- export default function resolveEntity (
7
- bgioArguments: BgioResolveState,
8
- target: unknown,
9
- context: unknown,
10
- targetName: string | undefined
11
- ): unknown {
12
- return !abstractTargetNames.includes(targetName ?? "") && isPlainObject(target)
13
- ? bankOf(bgioArguments).find(bgioArguments, target, context)
14
- : target;
15
- }
@@ -1,16 +0,0 @@
1
- import { Parser } from "expr-eval";
2
- import resolveProperties from "./resolve-properties.js";
3
- import type { BgioResolveState } from "./bgio-resolve-types.js";
4
-
5
- const parser = new Parser();
6
- (parser.functions as Record<string, (...args: unknown[]) => unknown>).sum = (...args: unknown[]) =>
7
- (args[0] as number[]).reduce((acc, val) => acc + val, 0);
8
-
9
- export default function resolveExpression (
10
- bgioArguments: BgioResolveState,
11
- rule: { expression: string; arguments?: unknown },
12
- context: Record<string, unknown>
13
- ): unknown {
14
- const args = resolveProperties(bgioArguments, rule.arguments, context) as Record<string, unknown>;
15
- return parser.evaluate(rule.expression, args);
16
- }
@@ -1,172 +0,0 @@
1
- import isPlainObject from "lodash/isPlainObject.js";
2
- import pick from "lodash/pick.js";
3
- import get from "./get.js";
4
- import resolveExpression from "./resolve-expression.js";
5
- import resolveEntity from "./resolve-entity.js";
6
- import { bankOf, type BgioResolveState } from "./bgio-resolve-types.js";
7
-
8
- export type { BgioResolveState };
9
-
10
- const resolutionTerminators = [
11
- "conditions",
12
- "move",
13
- "then",
14
- "mapping",
15
- ];
16
-
17
- export default function resolveProperties (
18
- bgioArguments: BgioResolveState,
19
- obj: unknown,
20
- context: Record<string, unknown> = {},
21
- key?: string
22
- ): unknown {
23
- if (!isPlainObject(obj) && !Array.isArray(obj)) {
24
- return obj;
25
- }
26
-
27
- let resolvedProperties: Record<string, unknown> | unknown[] = Array.isArray(obj)
28
- ? [...obj]
29
- : { ...(obj as Record<string, unknown>) };
30
-
31
- Object.entries(obj as Record<string, unknown>).forEach(([k, value]) => {
32
- if (!resolutionTerminators.includes(k)) {
33
- (resolvedProperties as Record<string, unknown>)[k] = resolveProperties(bgioArguments, value, context, k);
34
- }
35
- });
36
-
37
- const resolved = resolveProperty(bgioArguments, resolvedProperties, context);
38
-
39
- const resolveAsEntity = (resolved as { resolveAsEntity?: boolean } | null)?.resolveAsEntity
40
- || key === "target"
41
- || key === "targets";
42
-
43
- return resolveAsEntity
44
- ? resolveEntity(
45
- bgioArguments,
46
- resolved,
47
- context,
48
- key
49
- )
50
- : resolved;
51
- }
52
-
53
- function resolveProperty (
54
- bgioArguments: BgioResolveState,
55
- value: unknown,
56
- context: Record<string, unknown>
57
- ): unknown {
58
- const v = value as Record<string, unknown> | null;
59
- if (v?.type === "expression") {
60
- const expr = v as { expression: string; arguments?: unknown };
61
- return resolveExpression(
62
- bgioArguments,
63
- {
64
- ...expr,
65
- arguments: resolveProperties(bgioArguments, expr.arguments, context, "arguments"),
66
- },
67
- context
68
- );
69
- } else if (v?.type === "count") {
70
- return bankOf(bgioArguments).findAll(
71
- bgioArguments,
72
- value,
73
- context
74
- ).length;
75
- } else if (v?.type === "contextPath") {
76
- return get(context, v.path as Parameters<typeof get>[1]);
77
- } else if (v?.type === "ctxPath") {
78
- return get(bgioArguments.ctx, v.path as Parameters<typeof get>[1]);
79
- } else if (v?.type === "gamePath") {
80
- return get(bgioArguments.G, v.path as Parameters<typeof get>[1]);
81
- } else if (v?.type === "relativePath" || v?.type === "RelativePath") {
82
- const target = resolveProperties(bgioArguments, v.target, context, "target");
83
- return get((target as { attributes?: unknown })?.attributes, v.path as Parameters<typeof get>[1]) ?? null;
84
- } else if (v?.type === "parent" || v?.type === "Parent") {
85
- const originalTarget = v.target
86
- ? resolveProperties(bgioArguments, v.target, context, "target")
87
- : context.originalTarget;
88
- return bankOf(bgioArguments).findParent(originalTarget) ?? null;
89
- } else if (v?.type === "map") {
90
- return getMappedTargets(
91
- bgioArguments,
92
- v.targets,
93
- v.mapping,
94
- context
95
- ).map((mappedTarget) => mappedTarget.value);
96
- } else if (v?.type === "mapMax") {
97
- const mappedTargets = getMappedTargets(
98
- bgioArguments,
99
- v.targets,
100
- v.mapping,
101
- context
102
- );
103
- let maxValue: number | undefined;
104
- const maxTargets: unknown[] = [];
105
- for (let i = 0, len = mappedTargets.length; i < len; i++) {
106
- const { target, value: val } = mappedTargets[i];
107
- if (maxValue === undefined || (val as number) > maxValue) {
108
- maxValue = val as number;
109
- maxTargets.length = 0;
110
- maxTargets.push(target);
111
- } else if (val === maxValue) {
112
- maxTargets.push(target);
113
- }
114
- }
115
- return maxTargets;
116
- } else if (v?.type === "pick" || v?.type === "Pick") {
117
- const target = resolveProperties(bgioArguments, v.target, context, "target");
118
- return pick(
119
- resolveProperties(
120
- bgioArguments,
121
- (target as { attributes?: unknown })?.attributes,
122
- context,
123
- "attributes"
124
- ) as object,
125
- v.properties as string[]
126
- );
127
- } else if (v?.type === "coordinates" || v?.type === "Coordinates") {
128
- const originalTarget = v.target
129
- ? resolveProperties(bgioArguments, v.target, context, "target")
130
- : context.originalTarget;
131
- const parent = bankOf(bgioArguments).findParent(originalTarget) as { getCoordinates: (i: unknown) => unknown };
132
- return parent.getCoordinates((originalTarget as { rule: { index: unknown } }).rule.index);
133
- } else if (v?.type === "relativeCoordinates") {
134
- const originalTarget = v.target
135
- ? resolveProperties(bgioArguments, v.target, context, "target")
136
- : context.originalTarget;
137
- const parent = bankOf(bgioArguments).findParent(originalTarget) as {
138
- getCoordinates: (i: unknown) => unknown;
139
- getRelativeCoordinates: (a: unknown, b: unknown) => unknown;
140
- spaces: unknown[];
141
- getIndex: (c: unknown) => number;
142
- };
143
- const oldCoordinates =
144
- parent.getCoordinates((originalTarget as { rule: { index: unknown } }).rule.index);
145
- const newCoordinates =
146
- parent.getRelativeCoordinates(
147
- oldCoordinates,
148
- resolveProperties(bgioArguments, v.location, context, "location")
149
- );
150
- return (newCoordinates && parent.spaces[parent.getIndex(newCoordinates)]) ?? null;
151
- } else {
152
- return value;
153
- }
154
- }
155
-
156
- function getMappedTargets (
157
- bgioArguments: BgioResolveState,
158
- targetsRule: unknown,
159
- mapping: unknown,
160
- context: Record<string, unknown>
161
- ): Array<{ target: unknown; value: unknown }> {
162
- (targetsRule as { resolveAsEntity?: boolean }).resolveAsEntity = true;
163
- const resolved = resolveProperties(bgioArguments, targetsRule, context) as unknown[] | undefined;
164
- return resolved?.map((target) => ({
165
- target,
166
- value: resolveProperties(
167
- bgioArguments,
168
- mapping,
169
- { ...context, loopTarget: target },
170
- ),
171
- })) ?? [];
172
- }
@@ -1,32 +0,0 @@
1
- import { serialize, deserialize } from "wackson";
2
- import { registry } from "../registry.js";
3
- import type { BgioResolveState } from "./bgio-resolve-types.js";
4
-
5
- export default function simulateMove (
6
- bgioArguments: BgioResolveState,
7
- payload: { arguments: Record<string, { abstract?: boolean; entityId?: unknown } | number> },
8
- context: { moveInstance: { doMove: (...args: unknown[]) => unknown } }
9
- ) {
10
- const simulatedG = deserialize(serialize(bgioArguments.G) as string, registry) as {
11
- bank: { locate: (id: unknown) => unknown };
12
- };
13
- const newBgioArguments = {
14
- ...bgioArguments,
15
- G: simulatedG,
16
- };
17
- const simulatedPayload = { ...payload, arguments: {} as Record<string, unknown> };
18
- Object.entries(payload.arguments).forEach(([argName, arg]) => {
19
- simulatedPayload.arguments[argName] = (arg as { abstract?: boolean }).abstract
20
- ? arg
21
- : simulatedG.bank.locate(typeof arg === "number" ? arg : (arg as { entityId: unknown }).entityId);
22
- });
23
-
24
- context.moveInstance.doMove(
25
- newBgioArguments,
26
- simulatedPayload,
27
- context,
28
- { skipCheck: true }
29
- );
30
-
31
- return simulatedG;
32
- }
package/src/wackson.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare module "wackson" {
2
- export function serialize (value: unknown, options?: { deduplicateInstances?: boolean }): string;
3
- export function deserialize (json: string, registry?: unknown): unknown;
4
- }
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "noEmit": false,
5
- "emitDeclarationOnly": true,
6
- "declaration": true,
7
- "declarationMap": true,
8
- "outDir": "dist",
9
- "rootDir": "src",
10
- "stripInternal": true
11
- },
12
- "include": ["src/**/*.ts"],
13
- "exclude": ["node_modules", "dist", "**/*.spec.ts", "e2e"]
14
- }
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "ESNext",
5
- "moduleResolution": "Bundler",
6
- "lib": ["ES2020", "DOM"],
7
- "strict": true,
8
- "noImplicitAny": true,
9
- "skipLibCheck": true,
10
- "noEmit": true,
11
- "allowJs": false,
12
- "resolveJsonModule": true,
13
- "isolatedModules": true,
14
- "verbatimModuleSyntax": true,
15
- "noUnusedLocals": true,
16
- "noUnusedParameters": true,
17
- "rootDir": "."
18
- },
19
- "include": ["src/**/*.ts", "src/**/*.d.ts"],
20
- "exclude": ["node_modules", "dist"]
21
- }
File without changes
File without changes