board-game-engine-react 0.0.17 → 0.0.20

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,357 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.js
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ Client: () => Client4,
33
+ Game: () => Game,
34
+ useGameserverConnection: () => useGameserverConnection
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var BoardGameEngine2 = __toESM(require("board-game-engine"));
38
+
39
+ // src/components/game/game.js
40
+ var import_react7 = __toESM(require("react"));
41
+
42
+ // src/components/entity/entity.js
43
+ var import_react4 = __toESM(require("react"));
44
+
45
+ // src/contexts/game-context.js
46
+ var import_react = __toESM(require("react"));
47
+ var import_jsx_runtime = require("react/jsx-runtime");
48
+ var GameContext = (0, import_react.createContext)({
49
+ clickTarget: () => {
50
+ }
51
+ });
52
+ function GameProvider({ gameConnection, children, isSpectator }) {
53
+ (0, import_react.useEffect)(() => {
54
+ if (gameConnection.state._stateID === 0) {
55
+ gameConnection.reset();
56
+ }
57
+ }, [gameConnection.state._stateID]);
58
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(GameContext.Provider, { value: {
59
+ clickTarget: (target) => {
60
+ if (!isSpectator) {
61
+ gameConnection.doStep(target);
62
+ }
63
+ },
64
+ undoStep: () => {
65
+ gameConnection.undoStep();
66
+ },
67
+ allClickable: gameConnection.optimisticWinner || isSpectator ? /* @__PURE__ */ new Set() : gameConnection.allClickable,
68
+ currentMoveTargets: gameConnection.optimisticWinner || isSpectator ? [] : gameConnection.moveBuilder.targets
69
+ }, children });
70
+ }
71
+ var useGame = () => (0, import_react.useContext)(GameContext);
72
+
73
+ // src/components/board/grid.js
74
+ var import_react2 = __toESM(require("react"));
75
+ var import_jsx_runtime2 = require("react/jsx-runtime");
76
+ function Grid({ grid }) {
77
+ const { width, height, spaces } = grid.attributes;
78
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
79
+ "div",
80
+ {
81
+ className: "grid",
82
+ style: {
83
+ display: "inline-grid",
84
+ width: "100%",
85
+ gridTemplateColumns: `repeat(${width}, 1fr)`,
86
+ gridTemplateRows: `repeat(${height}, 1fr)`
87
+ },
88
+ children: spaces.map((space, index) => {
89
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
90
+ "div",
91
+ {
92
+ className: "grid__cell",
93
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Entity, { entity: space })
94
+ },
95
+ index
96
+ );
97
+ })
98
+ }
99
+ );
100
+ }
101
+
102
+ // src/components/space/space.js
103
+ var import_react3 = __toESM(require("react"));
104
+ var import_jsx_runtime3 = require("react/jsx-runtime");
105
+ function Space({ space }) {
106
+ const { clickTarget, allClickable, currentMoveTargets } = useGame();
107
+ const { entities, entityId } = space.attributes;
108
+ const clickable = allClickable.has(space);
109
+ const targeted = currentMoveTargets?.map((e) => e.entityId).includes(entityId);
110
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
111
+ "a",
112
+ {
113
+ className: [
114
+ "space",
115
+ clickable && "space--clickable",
116
+ targeted && "space--targeted"
117
+ ].filter(Boolean).join(" "),
118
+ onClick: () => clickTarget(space),
119
+ style: {
120
+ display: "inline-block",
121
+ flex: "1"
122
+ },
123
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
124
+ "div",
125
+ {
126
+ className: "space__entity-grid",
127
+ style: {
128
+ display: "flex",
129
+ height: "100%",
130
+ width: "100%",
131
+ flexWrap: "wrap"
132
+ },
133
+ children: [
134
+ Array.from({ length: entities.length }, (_, i) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
135
+ "div",
136
+ {
137
+ className: "space__entity-grid__cell",
138
+ style: {
139
+ display: "inline-block"
140
+ },
141
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Entity, { entity: entities[i] })
142
+ },
143
+ i
144
+ )),
145
+ !entities.length && space.attributes.name
146
+ ]
147
+ }
148
+ )
149
+ }
150
+ );
151
+ }
152
+
153
+ // src/components/entity/entity.js
154
+ var import_jsx_runtime4 = require("react/jsx-runtime");
155
+ function Entity({ entity }) {
156
+ const { clickTarget, allClickable } = useGame();
157
+ const isClickable = allClickable.has(entity);
158
+ const attributes = entity.attributes;
159
+ const entityType = attributes.entityType ?? attributes.type;
160
+ switch (entityType) {
161
+ case "Grid":
162
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Grid, { grid: entity, isClickable });
163
+ case "Space":
164
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Space, { space: entity, isClickable });
165
+ default:
166
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
167
+ "div",
168
+ {
169
+ onClick: (e) => {
170
+ if (isClickable) {
171
+ e.stopPropagation();
172
+ clickTarget(entity);
173
+ }
174
+ },
175
+ className: [
176
+ "entity",
177
+ attributes.player && `player-${attributes.player}`,
178
+ isClickable && "entity--clickable"
179
+ ].filter(Boolean).join(" "),
180
+ children: entity.rule.displayProperties?.map((property, i) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { children: [
181
+ property,
182
+ ": ",
183
+ entity.attributes[property]?.toString()
184
+ ] }, i))
185
+ }
186
+ );
187
+ }
188
+ }
189
+
190
+ // src/components/abstract-choices/abstract-choices.js
191
+ var import_react5 = __toESM(require("react"));
192
+ var import_jsx_runtime5 = require("react/jsx-runtime");
193
+ function AbstractChoices() {
194
+ const { clickTarget, allClickable, undoStep, currentMoveTargets } = useGame();
195
+ const abstractChoices = [...allClickable].filter((c) => c.abstract);
196
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { position: "relative" }, children: [
197
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
198
+ "button",
199
+ {
200
+ style: { visibility: "hidden" },
201
+ className: "button button--style-b button--x-small abstract-choices__choice",
202
+ children: "Spacer"
203
+ }
204
+ ),
205
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
206
+ "div",
207
+ {
208
+ style: {
209
+ position: "absolute",
210
+ top: 0,
211
+ width: "100%"
212
+ },
213
+ children: [
214
+ !!currentMoveTargets.length && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
215
+ "button",
216
+ {
217
+ className: "button button--style-c button--x-small abstract-choices__choice abstract-choices__choice--undo",
218
+ onClick: undoStep,
219
+ children: "Undo"
220
+ }
221
+ ),
222
+ abstractChoices.map((choice, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
223
+ "button",
224
+ {
225
+ className: "button button--style-b button--x-small abstract-choices__choice",
226
+ onClick: () => clickTarget(choice),
227
+ children: choice.value
228
+ },
229
+ i
230
+ ))
231
+ ]
232
+ }
233
+ )
234
+ ] });
235
+ }
236
+
237
+ // src/components/game-status/game-status.js
238
+ var import_react6 = __toESM(require("react"));
239
+ var import_jsx_runtime6 = require("react/jsx-runtime");
240
+ function GameStatus({ gameConnection }) {
241
+ const players = gameConnection.client.matchData;
242
+ const winner = gameConnection.state.ctx.gameover?.winner;
243
+ const draw = gameConnection.state.ctx.gameover?.draw;
244
+ let winnerString = "";
245
+ if (draw) {
246
+ winnerString = "Draw!";
247
+ } else if (players && winner) {
248
+ winnerString = `${players[winner].name} Wins!`;
249
+ } else if (winner) {
250
+ winnerString = `Player ${winner} Wins!`;
251
+ }
252
+ return gameConnection.state.ctx.gameover && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "game-status", children: winnerString });
253
+ }
254
+
255
+ // src/components/game/game.js
256
+ var import_jsx_runtime7 = require("react/jsx-runtime");
257
+ function Game({ gameConnection, loading, isSpectator }) {
258
+ const G = gameConnection?.state?.G;
259
+ return G ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
260
+ GameProvider,
261
+ {
262
+ gameConnection,
263
+ isSpectator,
264
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "game", children: [
265
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(AbstractChoices, {}),
266
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
267
+ "div",
268
+ {
269
+ className: "shared-board",
270
+ style: {
271
+ width: "100%",
272
+ display: "flex",
273
+ flexWrap: "wrap",
274
+ justifyContent: "center",
275
+ alignItems: "center",
276
+ gap: "1em"
277
+ },
278
+ children: G.sharedBoard.entities.map((entity, i) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Entity, { entity }, i))
279
+ }
280
+ ),
281
+ G.personalBoards && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "personal-boards", children: G.personalBoards.map((board, i) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
282
+ "div",
283
+ {
284
+ className: "personal-board",
285
+ style: {
286
+ width: "100%",
287
+ display: "grid",
288
+ gridAutoFlow: "column",
289
+ gridAutoRows: "1fr",
290
+ gap: "1em"
291
+ },
292
+ children: board.entities.map((entity, j) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Entity, { entity }, j))
293
+ },
294
+ i
295
+ )) }),
296
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(GameStatus, { gameConnection })
297
+ ] })
298
+ }
299
+ ) : loading;
300
+ }
301
+
302
+ // src/use-gameserver-connection.js
303
+ var import_react8 = require("react");
304
+ var BoardGameEngine = __toESM(require("board-game-engine"));
305
+ var Client2 = BoardGameEngine.Client;
306
+ var useGameserverConnection = ({
307
+ server,
308
+ gameId,
309
+ gameRules,
310
+ gameName,
311
+ boardgameIOGame,
312
+ boardgamePlayerID,
313
+ clientToken,
314
+ numPlayers,
315
+ debug,
316
+ singlePlayer = false,
317
+ enabled = true
318
+ }) => {
319
+ const [_, forceUpdate] = (0, import_react8.useReducer)((x) => x + 1, 0);
320
+ const [connection, setConnection] = (0, import_react8.useState)(null);
321
+ (0, import_react8.useEffect)(() => {
322
+ if (!gameRules && !boardgameIOGame || !singlePlayer && (!gameId || !clientToken || !enabled || !server)) return;
323
+ const options = {
324
+ server,
325
+ numPlayers,
326
+ onClientUpdate: () => {
327
+ forceUpdate();
328
+ },
329
+ debug,
330
+ gameId,
331
+ gameRules,
332
+ boardgameIOGame,
333
+ gameName,
334
+ boardgamePlayerID,
335
+ clientToken,
336
+ singlePlayer
337
+ };
338
+ const newConnection = new Client2(options);
339
+ newConnection.connect();
340
+ setConnection(newConnection);
341
+ return () => {
342
+ connection?.client?.stop();
343
+ setConnection(null);
344
+ };
345
+ }, [gameId, boardgamePlayerID, clientToken, gameRules, boardgameIOGame, enabled]);
346
+ if (connection) {
347
+ return Object.assign(
348
+ connection,
349
+ connection?.getState?.()
350
+ );
351
+ } else {
352
+ return {};
353
+ }
354
+ };
355
+
356
+ // src/index.js
357
+ var Client4 = BoardGameEngine2.Client;
@@ -1,12 +1,15 @@
1
- @charset "UTF-8";
1
+ /* src/styles.css */
2
2
  :root {
3
3
  --dark-orange: #F58F29;
4
4
  --dark-amethyst: #380036;
5
- --lipstick-red: #EC0B43; }
6
-
7
- html, body, #storybook-root, .story {
8
- height: 100%; }
9
-
5
+ --lipstick-red: #EC0B43;
6
+ }
7
+ html,
8
+ body,
9
+ #storybook-root,
10
+ .story {
11
+ height: 100%;
12
+ }
10
13
  .button {
11
14
  border: 3px solid var(--dark-amethyst);
12
15
  display: inline-block;
@@ -17,33 +20,34 @@ html, body, #storybook-root, .story {
17
20
  cursor: pointer;
18
21
  border-radius: .3em;
19
22
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.15);
20
- margin: .5em; }
21
-
23
+ margin: .5em;
24
+ }
22
25
  .button--style-b {
23
26
  background-color: var(--dark-amethyst);
24
- color: white; }
25
-
27
+ color: white;
28
+ }
26
29
  .button--style-c {
27
30
  background-color: var(--lipstick-red);
28
- color: white; }
29
-
31
+ color: white;
32
+ }
30
33
  .button--x-small {
31
34
  font-size: .9em;
32
35
  padding: .3em .5em;
33
- border-radius: .2em; }
34
-
36
+ border-radius: .2em;
37
+ }
35
38
  .game {
36
- color: black; }
37
-
38
- .shared-board, .personal-board {
39
- padding: 1em 0; }
40
-
39
+ color: black;
40
+ }
41
+ .shared-board,
42
+ .personal-board {
43
+ padding: 1em 0;
44
+ }
41
45
  .grid {
42
46
  border: 1px solid black;
43
47
  box-sizing: border-box;
44
48
  background-color: #333;
45
- max-width: 500px; }
46
-
49
+ max-width: 500px;
50
+ }
47
51
  .grid__cell {
48
52
  border: 1px solid black;
49
53
  background-color: #fff;
@@ -53,35 +57,35 @@ html, body, #storybook-root, .story {
53
57
  justify-content: center;
54
58
  color: #666;
55
59
  position: relative;
56
- overflow: auto; }
57
-
60
+ overflow: auto;
61
+ }
58
62
  .grid__cell .space {
59
63
  height: 100%;
60
- width: 100%; }
61
-
64
+ width: 100%;
65
+ }
62
66
  .space {
63
67
  box-sizing: border-box;
64
68
  min-width: 10px;
65
- min-height: 10px; }
66
-
69
+ min-height: 10px;
70
+ }
67
71
  .space:not(:has(.entity)):not(.grid__cell > :only-child) {
68
- border: 1px solid grey; }
69
-
72
+ border: 1px solid grey;
73
+ }
70
74
  .space--clickable {
71
- background-color: rgba(144, 238, 144, 0.4); }
72
-
75
+ background-color: rgba(144, 238, 144, .4);
76
+ }
73
77
  .space--targeted {
74
- background-color: grey; }
75
-
78
+ background-color: grey;
79
+ }
76
80
  .space__entity-grid {
77
81
  box-sizing: border-box;
78
82
  align-items: center;
79
- justify-content: center; }
80
-
83
+ justify-content: center;
84
+ }
81
85
  .space__entity-grid__cell {
82
86
  box-sizing: border-box;
83
- overflow: auto; }
84
-
87
+ overflow: auto;
88
+ }
85
89
  .entity {
86
90
  padding: .2em;
87
91
  min-height: 20px;
@@ -92,26 +96,26 @@ html, body, #storybook-root, .story {
92
96
  font-size: .5em;
93
97
  font-family: "Nunito", sans-serif;
94
98
  font-weight: bold;
95
- display: inline-block; }
96
-
99
+ display: inline-block;
100
+ }
97
101
  .entity.player-0 {
98
- background-color: pink; }
99
-
102
+ background-color: pink;
103
+ }
100
104
  .entity--clickable {
101
- background-color: rgba(144, 238, 144, 0.4); }
102
-
105
+ background-color: rgba(144, 238, 144, .4);
106
+ }
103
107
  .entity.player-0.entity--clickable {
104
- background-color: #f0e0d8; }
105
-
108
+ background-color: #f0e0d8;
109
+ }
106
110
  .entity.player-1 {
107
- background-color: lightblue; }
108
-
111
+ background-color: lightblue;
112
+ }
109
113
  .entity.player-1.entity--clickable {
110
- background-color: paleturquoise; }
111
-
114
+ background-color: paleturquoise;
115
+ }
112
116
  .debug-panel .pane {
113
- max-width: 420px !important; }
114
-
117
+ max-width: 420px !important;
118
+ }
115
119
  .game-status {
116
120
  text-align: center;
117
121
  position: absolute;
@@ -125,23 +129,24 @@ html, body, #storybook-root, .story {
125
129
  background-color: var(--dark-orange);
126
130
  color: black;
127
131
  box-sizing: border-box;
128
- font-size: 3em; }
129
-
132
+ font-size: 3em;
133
+ }
130
134
  .debug-panel .visibility-toggle {
131
- top: 70px !important; }
132
-
135
+ top: 70px !important;
136
+ }
133
137
  .debug-panel .visibility-toggle svg {
134
- display: none; }
135
-
138
+ display: none;
139
+ }
136
140
  .debug-panel .visibility-toggle .icon::after {
137
141
  position: relative;
138
142
  left: 3px;
139
- content: "⚙️";
143
+ content: "\2699\fe0f";
140
144
  font-size: 20px;
141
145
  display: block;
142
146
  width: 20px;
143
- height: 20px; }
144
-
147
+ height: 20px;
148
+ }
145
149
  .debug-panel .menu {
146
150
  position: relative;
147
- top: 70px !important; }
151
+ top: 70px !important;
152
+ }