board-game-engine 0.0.7 → 0.0.9

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.
@@ -0,0 +1,23 @@
1
+ const { devices } = require('@playwright/test')
2
+
3
+ const PORT = 5174
4
+
5
+ module.exports = {
6
+ testDir: 'e2e',
7
+ fullyParallel: false,
8
+ forbidOnly: !!process.env.CI,
9
+ retries: process.env.CI ? 2 : 0,
10
+ workers: 1,
11
+ reporter: 'html',
12
+ use: {
13
+ baseURL: `http://localhost:${PORT}`,
14
+ trace: 'on-first-retry',
15
+ viewport: { width: 800, height: 600 },
16
+ },
17
+ projects: [{ name: 'firefox', use: { ...devices['Desktop Firefox'] } }],
18
+ webServer: {
19
+ command: `npx http-server . -p ${PORT} -c-1`,
20
+ url: `http://localhost:${PORT}`,
21
+ reuseExistingServer: !process.env.CI,
22
+ },
23
+ }
@@ -15,14 +15,13 @@ import createPayload from '../utils/create-payload.js';
15
15
  export class Client {
16
16
  constructor (options) {
17
17
  this.options = options
18
- this.moveBuilder = {
19
- targets: [],
20
- stepIndex: 0,
21
- eliminatedMoves: []
22
- }
23
- this.optimisticWinner = null
24
- this.game = options.game
18
+ this.game = options.boardgameIOGame
25
19
  || gameFactory(JSON.parse(options.gameRules), options.gameName)
20
+
21
+ if (!options.boardgameIOGame) {
22
+ this.moveBuilder = { targets: [], stepIndex: 0, eliminatedMoves: [] }
23
+ this.optimisticWinner = null
24
+ }
26
25
  }
27
26
 
28
27
  connect () {
@@ -56,7 +55,8 @@ export class Client {
56
55
  this.client.start()
57
56
  return this
58
57
  } catch (error) {
59
- console.error('Failed to join game:', error)
58
+ console.error('Failed to join game:', error?.message ?? error)
59
+ if (error?.stack) console.error(error.stack)
60
60
  }
61
61
  }
62
62
 
@@ -65,38 +65,45 @@ export class Client {
65
65
  }
66
66
 
67
67
  getState () {
68
- let state
69
- let moves
70
- let gameover
71
-
72
68
  const clientState = this.client?.getState()
69
+ if (!clientState) return {}
73
70
 
74
- if (clientState) {
75
- state = {
76
- ...clientState,
77
- G: deserialize(JSON.stringify(clientState.G), registry),
78
- originalG: clientState.G,
71
+ if (this.options.boardgameIOGame) {
72
+ return {
73
+ state: clientState,
74
+ gameover: clientState?.ctx?.gameover,
75
+ moves: this.client.moves,
79
76
  }
77
+ }
80
78
 
81
- gameover = state?.ctx?.gameover
82
-
83
- moves = !gameover
84
- ? Object.entries(getCurrentMoves(state, this.client)).reduce((acc, [moveName, rawMove]) => {
85
- const move = (payload) => {
86
- this.client.moves[moveName](preparePayload(payload))
87
- }
88
- move.moveInstance = rawMove.moveInstance
89
- return { ...acc, [moveName]: move }
90
- }, {})
91
- : []
79
+ const state = {
80
+ ...clientState,
81
+ G: deserialize(JSON.stringify(clientState.G), registry),
82
+ originalG: clientState.G,
92
83
  }
93
84
 
94
- const { allClickable, possibleMoveMeta } = getPossibleMoves(state, moves, this.moveBuilder)
85
+ const gameover = state?.ctx?.gameover
86
+
87
+ const moves = !gameover
88
+ ? Object.entries(getCurrentMoves(state, this.client)).reduce((acc, [moveName, rawMove]) => {
89
+ const move = (payload) => {
90
+ this.client.moves[moveName](preparePayload(payload))
91
+ }
92
+ move.moveInstance = rawMove.moveInstance
93
+ return { ...acc, [moveName]: move }
94
+ }, {})
95
+ : []
96
+
97
+ const possibleMoves = getPossibleMoves(state, moves, this.moveBuilder)
98
+ const allClickable = possibleMoves.allClickable
99
+ const possibleMoveMeta = possibleMoves.possibleMoveMeta
95
100
 
96
101
  return { state, gameover, moves, allClickable, possibleMoveMeta }
97
102
  }
98
103
 
99
104
  doStep (_target) {
105
+ if (this.options.boardgameIOGame) return
106
+
100
107
  const { state, moves, possibleMoveMeta } = this.getState()
101
108
 
102
109
  const target = _target.abstract
@@ -141,12 +148,14 @@ export class Client {
141
148
  }
142
149
 
143
150
  reset () {
151
+ if (this.options.boardgameIOGame) return
144
152
  this.moveBuilder = { targets: [], stepIndex: 0, eliminatedMoves: [] };
145
153
  this.optimisticWinner = null
146
154
  this.update()
147
155
  }
148
156
 
149
157
  undoStep () {
158
+ if (this.options.boardgameIOGame) return
150
159
  if (this.moveBuilder.targets.length) {
151
160
  this.moveBuilder = {
152
161
  targets: this.moveBuilder.targets.slice(0, -1),
@@ -12,7 +12,7 @@ class Bank {
12
12
  }
13
13
 
14
14
  createEntity (definition = {}, options) {
15
- const entity = new (registry[definition.type || 'Entity'])(
15
+ const entity = new (registry[definition.entityType || 'Entity'])(
16
16
  {
17
17
  bank: this,
18
18
  fromBank: true,
@@ -3,7 +3,7 @@ import Condition from "./condition.js";
3
3
  export default class IsFull extends Condition {
4
4
  checkCondition(bgioArguments, rule, payload, context) {
5
5
  return {
6
- conditionIsMet: payload.target.spaces.every(space => space.entities.length)
6
+ conditionIsMet: payload.target.spaces.every(space => space?.entities.length)
7
7
  };
8
8
  }
9
9
  }
@@ -17,7 +17,7 @@ export default class WouldCondition extends Condition {
17
17
  const payload = {
18
18
  arguments: targets.reduce((acc, target, i) => ({
19
19
  ...acc,
20
- [argNameMap[context.moveInstance.rule.type][i]]: target
20
+ [argNameMap[context.moveInstance.rule.moveType][i]]: target
21
21
  }), {})
22
22
  }
23
23
 
@@ -21,11 +21,11 @@ import transformJSON from "../utils/json-transformer.js";
21
21
  // want to treat as first-class citizens
22
22
  const invariantEntities = [
23
23
  {
24
- type: "Space",
24
+ entityType: "Space",
25
25
  count: "Infinity",
26
26
  },
27
27
  {
28
- type: "Board",
28
+ entityType: "Board",
29
29
  name: 'sharedBoard'
30
30
  },
31
31
  {
@@ -51,7 +51,7 @@ function expandInitialPlacements (rules, entities) {
51
51
 
52
52
  if (rules.personalBoard) {
53
53
  entities.push({
54
- type: "Board",
54
+ entityType: "Board",
55
55
  name: 'personalBoard',
56
56
  perPlayer: true
57
57
  })
@@ -75,7 +75,7 @@ function expandInitialPlacements (rules, entities) {
75
75
 
76
76
  if (placement.destination.name === 'personalBoard') {
77
77
  return {
78
- type: 'ForEach',
78
+ moveType: 'ForEach',
79
79
  arguments: {
80
80
  targets: {
81
81
  type: 'ctxPath',
@@ -83,7 +83,7 @@ function expandInitialPlacements (rules, entities) {
83
83
  }
84
84
  },
85
85
  move: {
86
- type: 'PlaceNew',
86
+ moveType: 'PlaceNew',
87
87
  entity: {
88
88
  state,
89
89
  conditions: [{
@@ -120,7 +120,7 @@ function expandInitialPlacements (rules, entities) {
120
120
  }
121
121
  } else {
122
122
  return {
123
- type: 'PlaceNew',
123
+ moveType: 'PlaceNew',
124
124
  entity: {
125
125
  state,
126
126
  conditions: [{
@@ -145,12 +145,7 @@ function expandInitialPlacements (rules, entities) {
145
145
  }
146
146
  }
147
147
 
148
- const keyMappings = [
149
- ['thatMatches', 'conditions'],
150
- ['entityType', 'type'],
151
- ['moveType', 'type'],
152
- ['endConditions', 'endIf'],
153
- ]
148
+ const keyMappings = []
154
149
 
155
150
  const simpleReplacements = [
156
151
  [
@@ -62,7 +62,7 @@ function revivePayload (serializablePayload, G) {
62
62
  }
63
63
 
64
64
  export function getMoveInstance (moveRule) {
65
- switch (moveRule.type) {
65
+ switch (moveRule.moveType) {
66
66
  case 'MoveEntity':
67
67
  return new MoveEntity(moveRule);
68
68
  case 'PlaceNew':
@@ -8,7 +8,7 @@ export default class SpaceGroup extends Entity {
8
8
 
9
9
  makeSpaces (bank) {
10
10
  return Array(this.getSpacesCount()).fill()
11
- .map((_, i) => bank.createEntity({ type: 'Space', index: i }))
11
+ .map((_, i) => bank.createEntity({ entityType: 'Space', index: i }))
12
12
  }
13
13
 
14
14
  getEmptySpaces() {
package/src/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { Client } from './client/client'
2
1
  export { default as gameFactory } from './game-factory/game-factory'
2
+ export { Client } from './client/client'
@@ -9,7 +9,7 @@ const argNamesMap = {
9
9
 
10
10
  // this might not be where special handling for setstate wants to live
11
11
  export default function getSteps (bgioState, moveRule) {
12
- return argNamesMap[moveRule.type]
12
+ return argNamesMap[moveRule.moveType]
13
13
  .filter(argName => moveRule.arguments[argName].playerChoice)
14
14
  .map(argName => ({
15
15
  argName,
@@ -0,0 +1,4 @@
1
+ {
2
+ "status": "passed",
3
+ "failedTests": []
4
+ }
package/babel.config.js DELETED
@@ -1,6 +0,0 @@
1
- // only used for jest
2
- module.exports = {
3
- presets: [
4
- ['@babel/preset-env', { targets: { node: 'current' } }]
5
- ]
6
- }
@@ -1,7 +0,0 @@
1
- /* eslint-env jest */
2
-
3
- describe('functional test', () => {
4
- test('should have a test', () => {
5
- expect(true).toBe(true)
6
- })
7
- })
package/jest.config.js DELETED
@@ -1,21 +0,0 @@
1
- module.exports = {
2
- testEnvironment: 'node',
3
- transform: {
4
- '^.+\\.(js|ts)$': ['babel-jest', {
5
- presets: [
6
- '@babel/preset-typescript',
7
- ['@babel/preset-env', {
8
- modules: 'cjs',
9
- targets: { node: 'current' }
10
- }]
11
- ],
12
- plugins: ['add-module-exports']
13
- }]
14
- },
15
- clearMocks: true,
16
- moduleFileExtensions: ['js', 'ts', 'json'],
17
- testMatch: [
18
- '**/__tests__/**/*.(js|ts)',
19
- '**/*.(test|spec).(js|ts)'
20
- ]
21
- }
@@ -1,54 +0,0 @@
1
- {
2
- "playerCountRange": [2, 2],
3
- "sharedBoard": {
4
- "grid": {
5
- "type": "grid",
6
- "width": 3,
7
- "height": 3
8
- }
9
- },
10
- "pieces": [
11
- {
12
- "name": "playerMarker",
13
- "perPlayer": true
14
- }
15
- ],
16
- "winCondition": {
17
- "type": "bingo",
18
- "boardPath": ["sharedBoard", "grid"],
19
- "piece": {
20
- "name": "playerMarker"
21
- }
22
- },
23
- "round": {
24
- "loopUntil": false,
25
- "phases": [
26
- {
27
- "type": "sequentialPlayerTurn",
28
- "actions": [
29
- {
30
- "type": "movePiece",
31
- "piece": {
32
- "name": "playerMarker"
33
- },
34
- "from": "player",
35
- "to": ["sharedBoard", "grid"],
36
- "conditions": [
37
- {
38
- "type": "doesNotContain",
39
- "piece": "any"
40
- }
41
- ]
42
- }
43
- ]
44
- }
45
- ]
46
- },
47
- "drawCondition": {
48
- "type": "blackout",
49
- "boardPath": ["sharedBoard", "grid"],
50
- "piece": {
51
- "name": "playerMarker"
52
- }
53
- }
54
- }