board-game-engine-react 0.0.6 → 0.0.8
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "board-game-engine-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "React library for using board-game-engine",
|
|
5
5
|
"main": "dist/board-game-engine-react.js",
|
|
6
6
|
"scripts": {
|
|
@@ -79,6 +79,7 @@
|
|
|
79
79
|
"storybook": "^8.4.6"
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
|
+
"board-game-engine": "^0.0.8",
|
|
82
83
|
"react": "^18.2.0",
|
|
83
84
|
"react-error-boundary": "^3.1.4",
|
|
84
85
|
"react-transition-group": "^4.4.5"
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useEffect, useReducer, useState } from 'react'
|
|
2
|
+
import * as BoardGameEngine from 'board-game-engine'
|
|
3
|
+
const Client = BoardGameEngine.Client
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export const useGameserverConnection = ({
|
|
7
|
+
server,
|
|
8
|
+
gameId,
|
|
9
|
+
gameRules,
|
|
10
|
+
gameName,
|
|
11
|
+
boardgameIOGame,
|
|
12
|
+
boardgamePlayerID,
|
|
13
|
+
clientToken,
|
|
14
|
+
numPlayers,
|
|
15
|
+
debug,
|
|
16
|
+
singlePlayer = false,
|
|
17
|
+
enabled = true,
|
|
18
|
+
}) => {
|
|
19
|
+
const [_, forceUpdate] = useReducer(x => x + 1, 0)
|
|
20
|
+
const [connection, setConnection] = useState(null)
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (!gameRules && !boardgameIOGame || !singlePlayer && (!gameId || !clientToken || !enabled || !server)) return
|
|
24
|
+
|
|
25
|
+
const options = {
|
|
26
|
+
server,
|
|
27
|
+
numPlayers,
|
|
28
|
+
onClientUpdate: () => {
|
|
29
|
+
forceUpdate()
|
|
30
|
+
},
|
|
31
|
+
debug,
|
|
32
|
+
gameId,
|
|
33
|
+
gameRules,
|
|
34
|
+
boardgameIOGame,
|
|
35
|
+
gameName,
|
|
36
|
+
boardgamePlayerID,
|
|
37
|
+
clientToken,
|
|
38
|
+
singlePlayer,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const newConnection = new Client(options)
|
|
42
|
+
|
|
43
|
+
newConnection.connect()
|
|
44
|
+
|
|
45
|
+
setConnection(newConnection)
|
|
46
|
+
|
|
47
|
+
return () => {
|
|
48
|
+
connection?.client?.stop()
|
|
49
|
+
setConnection(null)
|
|
50
|
+
}
|
|
51
|
+
}, [gameId, boardgamePlayerID, clientToken, gameRules, boardgameIOGame, enabled])
|
|
52
|
+
|
|
53
|
+
if (connection) {
|
|
54
|
+
return Object.assign(
|
|
55
|
+
connection,
|
|
56
|
+
connection?.getState?.(),
|
|
57
|
+
)
|
|
58
|
+
} else {
|
|
59
|
+
return {}
|
|
60
|
+
}
|
|
61
|
+
}
|