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.
- package/dist/board-game-engine.js +61 -53
- package/dist/board-game-engine.min.js +1 -1
- package/e2e/bge.spec.js +267 -0
- package/e2e/bgio-minimal.spec.js +70 -0
- package/e2e/examples.spec.js +34 -0
- package/e2e/fixtures/bge-checkers.html +39 -0
- package/e2e/fixtures/bge-minimal.html +43 -0
- package/e2e/fixtures/bge-ttt.html +45 -0
- package/e2e/fixtures/bgio-minimal-debug.html +47 -0
- package/e2e/fixtures/bgio-minimal.html +44 -0
- package/e2e/fixtures/minimal-game.json +4 -0
- package/examples/checkers.json +793 -0
- package/examples/connect-four.json +126 -0
- package/examples/eights.json +653 -0
- package/examples/index.html +219 -0
- package/examples/reversi.json +215 -0
- package/examples/tic-tac-toe.json +53 -0
- package/package.json +8 -5
- package/playwright-report/index.html +85 -0
- package/playwright.config.cjs +23 -0
- package/src/client/client.js +38 -29
- package/src/game-factory/bank/bank.js +1 -1
- package/src/game-factory/condition/is-full-condition.js +1 -1
- package/src/game-factory/condition/would-condition.js +1 -1
- package/src/game-factory/expand-game-rules.js +7 -12
- package/src/game-factory/move/move-factory.js +1 -1
- package/src/game-factory/space-group/space-group.js +1 -1
- package/src/index.js +1 -1
- package/src/utils/get-steps.js +1 -1
- package/test-results/.last-run.json +4 -0
- package/babel.config.js +0 -6
- package/board-game-engine.test.js +0 -7
- package/jest.config.js +0 -21
- package/tic-tac-toe-verbose.json +0 -54
|
@@ -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
|
+
}
|
package/src/client/client.js
CHANGED
|
@@ -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.
|
|
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 (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
|
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),
|
|
@@ -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
|
|
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.
|
|
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
|
-
|
|
24
|
+
entityType: "Space",
|
|
25
25
|
count: "Infinity",
|
|
26
26
|
},
|
|
27
27
|
{
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
[
|
|
@@ -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({
|
|
11
|
+
.map((_, i) => bank.createEntity({ entityType: 'Space', index: i }))
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
getEmptySpaces() {
|
package/src/index.js
CHANGED
package/src/utils/get-steps.js
CHANGED
|
@@ -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.
|
|
12
|
+
return argNamesMap[moveRule.moveType]
|
|
13
13
|
.filter(argName => moveRule.arguments[argName].playerChoice)
|
|
14
14
|
.map(argName => ({
|
|
15
15
|
argName,
|
package/babel.config.js
DELETED
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
|
-
}
|
package/tic-tac-toe-verbose.json
DELETED
|
@@ -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
|
-
}
|