board-game-engine-react 1.0.0 → 1.0.2

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
@@ -18,7 +18,7 @@ npm install board-game-engine-react react
18
18
 
19
19
  ### `Game`
20
20
 
21
- Root UI component. Renders the current game state (shared board, personal boards, abstract choices, status). Wrap with a provider that supplies `gameConnection`.
21
+ Root UI component. Renders the current game state (shared board, personal boards, abstract choices, status). Pass a `gameConnection` object from `useGameserverConnection`.
22
22
 
23
23
  ```jsx
24
24
  import { Game, useGameserverConnection } from 'board-game-engine-react'
@@ -26,10 +26,11 @@ import { Game, useGameserverConnection } from 'board-game-engine-react'
26
26
  function App() {
27
27
  const gameConnection = useGameserverConnection({
28
28
  server: 'https://your-server.com',
29
- gameId: 'room-1',
29
+ matchID: 'room-1',
30
30
  gameRules: myGameRules,
31
- clientToken: '…',
32
- // …other options
31
+ credentials: '…',
32
+ playerID: '0',
33
+ numPlayers: 2,
33
34
  })
34
35
 
35
36
  return (
@@ -42,31 +43,40 @@ function App() {
42
43
  }
43
44
  ```
44
45
 
46
+ Single-player (no server): omit `server`, `matchID`, and `credentials` and pass `gameRules` and `numPlayers`:
47
+
48
+ ```jsx
49
+ const gameConnection = useGameserverConnection({
50
+ gameRules: myGameRules,
51
+ numPlayers: 2,
52
+ })
53
+ ```
54
+
45
55
  | Prop | Type | Description |
46
56
  |-------------------|---------|-------------|
47
57
  | `gameConnection` | object | Connection/state object (e.g. from `useGameserverConnection`). |
48
- | `loading` | node | Rendered while there is no game state (e.g. connecting). |
49
- | `isSpectator` | boolean | If true, disables making moves. |
58
+ | `loading` | node | Rendered while there is no game state (e.g. connecting). |
59
+ | `isSpectator` | boolean | If true, disables making moves. |
50
60
 
51
61
  ---
52
62
 
53
63
  ### `useGameserverConnection(options)`
54
64
 
55
- Hook that creates and maintains a connection to a game server. Returns an object that includes connection state and methods; pass this object as `gameConnection` to `<Game />`.
56
-
57
- | Option | Type | Description |
58
- |---------------------|---------|-------------|
59
- | `server` | string | Game server URL (required unless `singlePlayer`). |
60
- | `gameId` | string | Room/game id (required unless `singlePlayer`). |
61
- | `clientToken` | string | Auth token for the client (required unless `singlePlayer`). |
62
- | `gameRules` | object | Game definition from board-game-engine (for server-driven games). |
63
- | `boardgameIOGame` | object | boardgame.io game config (alternative to `gameRules`). |
64
- | `gameName` | string | Game name sent to server. |
65
- | `boardgamePlayerID` | string | Player id (e.g. `'0'`, `'1'`). |
66
- | `numPlayers` | number | Number of players. |
67
- | `debug` | boolean | Enable debug logging. |
68
- | `singlePlayer` | boolean | If true, run locally without a server (default: `false`). |
69
- | `enabled` | boolean | If false, skip connecting (default: `true`). |
65
+ Hook that creates and maintains a connection to a game server (or a local game when no server is configured). Returns an object that includes connection state and methods; pass this object as `gameConnection` to `<Game />`.
66
+
67
+ | Option | Type | Description |
68
+ |-------------------|---------|-------------|
69
+ | `server` | string | Game server URL (required for multiplayer). |
70
+ | `matchID` | string | Room/match id (required for multiplayer). |
71
+ | `credentials` | string | Auth credentials for the client (required for multiplayer). |
72
+ | `gameRules` | object | Game definition from board-game-engine. |
73
+ | `boardgameIOGame` | object | boardgame.io game config (alternative to `gameRules`). |
74
+ | `gameName` | string | Game name sent to server. |
75
+ | `playerID` | string | Player id (e.g. `'0'`, `'1'`). |
76
+ | `numPlayers` | number | Number of players. |
77
+ | `multiplayer` | boolean | Use server multiplayer; omit or false for local/single-player. |
78
+ | `debug` | boolean | Enable debug logging. |
79
+ | `enabled` | boolean | If false, skip connecting (default: `true`). |
70
80
 
71
81
  ---
72
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "board-game-engine-react",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "React library for using board-game-engine",
5
5
  "main": "./dist/board-game-engine-react.cjs",
6
6
  "module": "./dist/board-game-engine-react.mjs",
@@ -94,7 +94,7 @@
94
94
  "vite": "^5.0.0"
95
95
  },
96
96
  "dependencies": {
97
- "board-game-engine": "file:../board-game-engine",
97
+ "board-game-engine": "^1.0.5",
98
98
  "react": "^18.2.0",
99
99
  "react-error-boundary": "^3.1.4",
100
100
  "react-transition-group": "^4.4.5"
@@ -11,40 +11,62 @@ export default {
11
11
  args: {
12
12
  gameRules: JSON.stringify(ticTacToe, null, 2),
13
13
  numPlayers: 2,
14
+ isSpectator: false,
14
15
  },
15
16
  argTypes: {
16
- gameRules: {
17
- control: 'text'
18
- },
19
- numPlayers: {
20
- control: 'number'
21
- }
17
+ gameRules: { control: 'text' },
18
+ numPlayers: { control: 'number' },
19
+ isSpectator: { control: 'boolean' },
20
+ },
21
+ }
22
+
23
+ const parseGameRules = (gameRules, fallback) => {
24
+ if (typeof gameRules !== 'string') return gameRules
25
+ try {
26
+ return JSON.parse(gameRules || '{}')
27
+ } catch {
28
+ return fallback
22
29
  }
23
30
  }
24
31
 
25
- const Template = ({ gameRules, numPlayers }) => {
32
+ const fallbackRender = ({ error }) => <div>Invalid Game Rules {error}</div>
33
+
34
+ const Template = ({ gameRules, numPlayers, loading, isSpectator }) => {
35
+ const rules = parseGameRules(gameRules, ticTacToe)
26
36
  const gameConnection = useGameserverConnection({
27
- gameRules,
28
- singlePlayer: true,
37
+ gameRules: rules,
29
38
  numPlayers,
30
39
  })
31
40
 
32
41
  return (
33
42
  <div className="story">
34
- <ErrorBoundary
35
- resetKeys={[gameRules, numPlayers]}
36
- fallbackRender={({ error }) => {
37
- return (
38
- <div>
39
- Invalid Game Rules {error}
40
- </div>
41
- )
42
- }}
43
- >
44
- <Game gameConnection={gameConnection} />
43
+ <ErrorBoundary resetKeys={[gameRules, numPlayers]} fallbackRender={fallbackRender}>
44
+ <Game
45
+ gameConnection={gameConnection}
46
+ loading={loading}
47
+ isSpectator={isSpectator}
48
+ />
45
49
  </ErrorBoundary>
46
50
  </div>
47
51
  )
48
52
  }
49
53
 
50
54
  export const Basic = Template.bind({})
55
+
56
+ export const WithLoading = (args) => (
57
+ <Template
58
+ {...args}
59
+ loading={<div className="story-loading">Loading game…</div>}
60
+ />
61
+ )
62
+ WithLoading.args = {
63
+ gameRules: JSON.stringify(ticTacToe, null, 2),
64
+ numPlayers: 2,
65
+ }
66
+
67
+ export const Spectator = Template.bind({})
68
+ Spectator.args = {
69
+ gameRules: JSON.stringify(ticTacToe, null, 2),
70
+ numPlayers: 2,
71
+ isSpectator: true,
72
+ }