@mnbroatch/boardgame.io 0.0.1
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/LICENSE +21 -0
- package/README.md +102 -0
- package/ai/package.json +7 -0
- package/client/package.json +7 -0
- package/core/package.json +7 -0
- package/debug/package.json +7 -0
- package/dist/boardgameio.es.js +14238 -0
- package/dist/boardgameio.js +14277 -0
- package/dist/boardgameio.min.js +16 -0
- package/dist/cjs/Debug-9d141c06.js +9586 -0
- package/dist/cjs/ai-e0e8a768.js +377 -0
- package/dist/cjs/ai.js +20 -0
- package/dist/cjs/client-76dec77b.js +258 -0
- package/dist/cjs/client-a22d7500.js +524 -0
- package/dist/cjs/client.js +26 -0
- package/dist/cjs/core.js +52 -0
- package/dist/cjs/debug.js +18 -0
- package/dist/cjs/filter-player-view-bb02e2f6.js +89 -0
- package/dist/cjs/initialize-267fcd69.js +61 -0
- package/dist/cjs/internal.js +25 -0
- package/dist/cjs/master-2904879d.js +320 -0
- package/dist/cjs/master.js +18 -0
- package/dist/cjs/multiplayer.js +23 -0
- package/dist/cjs/plugin-random-7425844d.js +229 -0
- package/dist/cjs/plugins.js +59 -0
- package/dist/cjs/react-native.js +182 -0
- package/dist/cjs/react.js +727 -0
- package/dist/cjs/reducer-16eec232.js +1203 -0
- package/dist/cjs/server.js +4087 -0
- package/dist/cjs/socketio-7a0837eb.js +478 -0
- package/dist/cjs/testing.js +30 -0
- package/dist/cjs/transport-b1874dfa.js +37 -0
- package/dist/cjs/turn-order-b2ff8740.js +1136 -0
- package/dist/cjs/util-fcfd8fb8.js +140 -0
- package/dist/esm/Debug-0141fe2d.js +9577 -0
- package/dist/esm/ai-5c06e761.js +371 -0
- package/dist/esm/ai.js +8 -0
- package/dist/esm/client-2e653027.js +522 -0
- package/dist/esm/client-5f57c3f2.js +255 -0
- package/dist/esm/client.js +16 -0
- package/dist/esm/core.js +40 -0
- package/dist/esm/debug.js +10 -0
- package/dist/esm/filter-player-view-2c6cc96f.js +87 -0
- package/dist/esm/initialize-11d626ca.js +59 -0
- package/dist/esm/internal.js +10 -0
- package/dist/esm/master-fa8f2e43.js +318 -0
- package/dist/esm/master.js +10 -0
- package/dist/esm/multiplayer.js +14 -0
- package/dist/esm/plugin-random-087f861e.js +226 -0
- package/dist/esm/plugins.js +55 -0
- package/dist/esm/react-native.js +173 -0
- package/dist/esm/react.js +716 -0
- package/dist/esm/reducer-c46da7e5.js +1198 -0
- package/dist/esm/socketio-c22ffa65.js +455 -0
- package/dist/esm/testing.js +26 -0
- package/dist/esm/transport-ce07b771.js +35 -0
- package/dist/esm/turn-order-376d315e.js +1091 -0
- package/dist/esm/util-b6147cef.js +135 -0
- package/dist/types/packages/ai.d.ts +5 -0
- package/dist/types/packages/client.d.ts +3 -0
- package/dist/types/packages/core.d.ts +5 -0
- package/dist/types/packages/debug.d.ts +2 -0
- package/dist/types/packages/internal.d.ts +8 -0
- package/dist/types/packages/master.d.ts +2 -0
- package/dist/types/packages/multiplayer.d.ts +3 -0
- package/dist/types/packages/plugins.d.ts +3 -0
- package/dist/types/packages/react-native.d.ts +2 -0
- package/dist/types/packages/react.d.ts +3 -0
- package/dist/types/packages/server.d.ts +6 -0
- package/dist/types/packages/testing.d.ts +1 -0
- package/dist/types/src/ai/ai.d.ts +53 -0
- package/dist/types/src/ai/ai.test.d.ts +1 -0
- package/dist/types/src/ai/bot.d.ts +40 -0
- package/dist/types/src/ai/mcts-bot.d.ts +60 -0
- package/dist/types/src/ai/random-bot.d.ts +27 -0
- package/dist/types/src/client/client.d.ts +104 -0
- package/dist/types/src/client/client.test.d.ts +1 -0
- package/dist/types/src/client/debug/tests/debug.test.d.ts +1 -0
- package/dist/types/src/client/manager.d.ts +61 -0
- package/dist/types/src/client/react.d.ts +75 -0
- package/dist/types/src/client/react.ssr.test.d.ts +4 -0
- package/dist/types/src/client/react.test.d.ts +1 -0
- package/dist/types/src/client/transport/dummy.d.ts +18 -0
- package/dist/types/src/client/transport/local.d.ts +59 -0
- package/dist/types/src/client/transport/local.test.d.ts +1 -0
- package/dist/types/src/client/transport/socketio.d.ts +45 -0
- package/dist/types/src/client/transport/socketio.test.d.ts +1 -0
- package/dist/types/src/client/transport/transport.d.ts +50 -0
- package/dist/types/src/client/transport/transport.test.d.ts +1 -0
- package/dist/types/src/core/action-creators.d.ts +144 -0
- package/dist/types/src/core/action-types.d.ts +10 -0
- package/dist/types/src/core/backwards-compatibility.d.ts +12 -0
- package/dist/types/src/core/constants.d.ts +6 -0
- package/dist/types/src/core/errors.d.ts +15 -0
- package/dist/types/src/core/flow.d.ts +28 -0
- package/dist/types/src/core/flow.test.d.ts +1 -0
- package/dist/types/src/core/game-methods.d.ts +9 -0
- package/dist/types/src/core/game.d.ts +26 -0
- package/dist/types/src/core/game.test.d.ts +1 -0
- package/dist/types/src/core/initialize.d.ts +9 -0
- package/dist/types/src/core/logger.d.ts +2 -0
- package/dist/types/src/core/player-view.d.ts +7 -0
- package/dist/types/src/core/player-view.test.d.ts +1 -0
- package/dist/types/src/core/reducer.d.ts +155 -0
- package/dist/types/src/core/reducer.test.d.ts +1 -0
- package/dist/types/src/core/turn-order.d.ts +179 -0
- package/dist/types/src/core/turn-order.test.d.ts +8 -0
- package/dist/types/src/lobby/client.d.ts +194 -0
- package/dist/types/src/lobby/client.test.d.ts +1 -0
- package/dist/types/src/lobby/connection.d.ts +44 -0
- package/dist/types/src/lobby/connection.test.d.ts +1 -0
- package/dist/types/src/lobby/create-match-form.d.ts +26 -0
- package/dist/types/src/lobby/login-form.d.ts +23 -0
- package/dist/types/src/lobby/match-instance.d.ts +31 -0
- package/dist/types/src/lobby/react.d.ts +113 -0
- package/dist/types/src/lobby/react.ssr.test.d.ts +4 -0
- package/dist/types/src/lobby/react.test.d.ts +1 -0
- package/dist/types/src/master/filter-player-view.d.ts +96 -0
- package/dist/types/src/master/filter-player-view.test.d.ts +1 -0
- package/dist/types/src/master/master.d.ts +94 -0
- package/dist/types/src/master/master.test.d.ts +1 -0
- package/dist/types/src/plugins/events/events.d.ts +54 -0
- package/dist/types/src/plugins/events/events.test.d.ts +1 -0
- package/dist/types/src/plugins/main.d.ts +75 -0
- package/dist/types/src/plugins/main.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-events.d.ts +5 -0
- package/dist/types/src/plugins/plugin-immer.d.ts +7 -0
- package/dist/types/src/plugins/plugin-immer.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-log.d.ts +14 -0
- package/dist/types/src/plugins/plugin-log.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-player.d.ts +29 -0
- package/dist/types/src/plugins/plugin-player.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-random.d.ts +4 -0
- package/dist/types/src/plugins/plugin-serializable.d.ts +7 -0
- package/dist/types/src/plugins/plugin-serializable.test.d.ts +1 -0
- package/dist/types/src/plugins/random/random.alea.d.ts +19 -0
- package/dist/types/src/plugins/random/random.d.ts +54 -0
- package/dist/types/src/plugins/random/random.test.d.ts +1 -0
- package/dist/types/src/server/api.d.ts +13 -0
- package/dist/types/src/server/api.test.d.ts +1 -0
- package/dist/types/src/server/auth.d.ts +38 -0
- package/dist/types/src/server/auth.test.d.ts +1 -0
- package/dist/types/src/server/cors.d.ts +4 -0
- package/dist/types/src/server/cors.test.d.ts +1 -0
- package/dist/types/src/server/db/base.d.ts +192 -0
- package/dist/types/src/server/db/flatfile.d.ts +44 -0
- package/dist/types/src/server/db/flatfile.test.d.ts +1 -0
- package/dist/types/src/server/db/index.d.ts +4 -0
- package/dist/types/src/server/db/index.test.d.ts +1 -0
- package/dist/types/src/server/db/inmemory.d.ts +43 -0
- package/dist/types/src/server/db/inmemory.test.d.ts +1 -0
- package/dist/types/src/server/db/localstorage.d.ts +7 -0
- package/dist/types/src/server/db/localstorage.test.d.ts +1 -0
- package/dist/types/src/server/index.d.ts +68 -0
- package/dist/types/src/server/index.test.d.ts +1 -0
- package/dist/types/src/server/transport/pubsub/generic-pub-sub.d.ts +6 -0
- package/dist/types/src/server/transport/pubsub/in-memory-pub-sub.d.ts +7 -0
- package/dist/types/src/server/transport/pubsub/in-memory-pub-sub.test.d.ts +1 -0
- package/dist/types/src/server/transport/socketio-simultaneous.test.d.ts +1 -0
- package/dist/types/src/server/transport/socketio.d.ts +65 -0
- package/dist/types/src/server/transport/socketio.test.d.ts +1 -0
- package/dist/types/src/server/util.d.ts +35 -0
- package/dist/types/src/testing/mock-random.d.ts +15 -0
- package/dist/types/src/testing/mock-random.test.d.ts +1 -0
- package/dist/types/src/types.d.ts +387 -0
- package/internal/package.json +7 -0
- package/master/package.json +7 -0
- package/multiplayer/package.json +7 -0
- package/package.json +211 -0
- package/plugins/package.json +7 -0
- package/react/package.json +7 -0
- package/react-native/package.json +7 -0
- package/server/package.json +6 -0
- package/src/ai/ai.test.ts +433 -0
- package/src/ai/ai.ts +84 -0
- package/src/ai/bot.ts +122 -0
- package/src/ai/mcts-bot.ts +331 -0
- package/src/ai/random-bot.ts +20 -0
- package/src/client/client.test.ts +993 -0
- package/src/client/client.ts +588 -0
- package/src/client/debug/Debug.svelte +239 -0
- package/src/client/debug/Menu.svelte +65 -0
- package/src/client/debug/ai/AI.svelte +215 -0
- package/src/client/debug/ai/Options.svelte +48 -0
- package/src/client/debug/info/Info.svelte +22 -0
- package/src/client/debug/info/Item.svelte +24 -0
- package/src/client/debug/log/Log.svelte +157 -0
- package/src/client/debug/log/LogEvent.svelte +149 -0
- package/src/client/debug/log/LogMetadata.svelte +7 -0
- package/src/client/debug/log/PhaseMarker.svelte +27 -0
- package/src/client/debug/log/TurnMarker.svelte +23 -0
- package/src/client/debug/main/ClientSwitcher.svelte +59 -0
- package/src/client/debug/main/Controls.svelte +58 -0
- package/src/client/debug/main/Hotkey.svelte +84 -0
- package/src/client/debug/main/InteractiveFunction.svelte +85 -0
- package/src/client/debug/main/Main.svelte +121 -0
- package/src/client/debug/main/Move.svelte +68 -0
- package/src/client/debug/main/PlayerInfo.svelte +70 -0
- package/src/client/debug/mcts/Action.svelte +22 -0
- package/src/client/debug/mcts/MCTS.svelte +78 -0
- package/src/client/debug/mcts/Table.svelte +98 -0
- package/src/client/debug/tests/JSONTree.mock.svelte +3 -0
- package/src/client/debug/tests/debug.test.ts +183 -0
- package/src/client/debug/utils/shortcuts.js +50 -0
- package/src/client/debug/utils/shortcuts.test.js +49 -0
- package/src/client/manager.ts +177 -0
- package/src/client/react-native.js +136 -0
- package/src/client/react-native.test.js +229 -0
- package/src/client/react.ssr.test.tsx +24 -0
- package/src/client/react.test.tsx +213 -0
- package/src/client/react.tsx +192 -0
- package/src/client/transport/dummy.ts +19 -0
- package/src/client/transport/local.test.ts +353 -0
- package/src/client/transport/local.ts +230 -0
- package/src/client/transport/socketio.test.ts +328 -0
- package/src/client/transport/socketio.ts +210 -0
- package/src/client/transport/transport.test.ts +27 -0
- package/src/client/transport/transport.ts +95 -0
- package/src/core/action-creators.ts +159 -0
- package/src/core/action-types.ts +18 -0
- package/src/core/backwards-compatibility.ts +23 -0
- package/src/core/constants.ts +6 -0
- package/src/core/errors.ts +35 -0
- package/src/core/flow.test.ts +2433 -0
- package/src/core/flow.ts +897 -0
- package/src/core/game-methods.ts +9 -0
- package/src/core/game.test.ts +286 -0
- package/src/core/game.ts +114 -0
- package/src/core/initialize.ts +77 -0
- package/src/core/logger.test.js +90 -0
- package/src/core/logger.ts +18 -0
- package/src/core/player-view.test.ts +50 -0
- package/src/core/player-view.ts +39 -0
- package/src/core/reducer.test.ts +991 -0
- package/src/core/reducer.ts +532 -0
- package/src/core/turn-order.test.ts +1123 -0
- package/src/core/turn-order.ts +473 -0
- package/src/lobby/client.test.ts +385 -0
- package/src/lobby/client.ts +358 -0
- package/src/lobby/connection.test.ts +207 -0
- package/src/lobby/connection.ts +162 -0
- package/src/lobby/create-match-form.tsx +122 -0
- package/src/lobby/login-form.tsx +75 -0
- package/src/lobby/match-instance.tsx +135 -0
- package/src/lobby/react.ssr.test.tsx +22 -0
- package/src/lobby/react.test.tsx +594 -0
- package/src/lobby/react.tsx +402 -0
- package/src/master/filter-player-view.test.ts +381 -0
- package/src/master/filter-player-view.ts +102 -0
- package/src/master/master.test.ts +1068 -0
- package/src/master/master.ts +492 -0
- package/src/plugins/events/events.test.ts +108 -0
- package/src/plugins/events/events.ts +209 -0
- package/src/plugins/main.test.ts +411 -0
- package/src/plugins/main.ts +314 -0
- package/src/plugins/plugin-events.ts +40 -0
- package/src/plugins/plugin-immer.test.ts +86 -0
- package/src/plugins/plugin-immer.ts +37 -0
- package/src/plugins/plugin-log.test.ts +37 -0
- package/src/plugins/plugin-log.ts +40 -0
- package/src/plugins/plugin-player.test.ts +172 -0
- package/src/plugins/plugin-player.ts +100 -0
- package/src/plugins/plugin-random.ts +40 -0
- package/src/plugins/plugin-serializable.test.ts +40 -0
- package/src/plugins/plugin-serializable.ts +55 -0
- package/src/plugins/random/random.alea.ts +109 -0
- package/src/plugins/random/random.test.ts +167 -0
- package/src/plugins/random/random.ts +198 -0
- package/src/server/api.test.ts +1699 -0
- package/src/server/api.ts +527 -0
- package/src/server/auth.test.ts +275 -0
- package/src/server/auth.ts +89 -0
- package/src/server/cors.test.ts +121 -0
- package/src/server/cors.ts +7 -0
- package/src/server/db/base.ts +296 -0
- package/src/server/db/flatfile.test.ts +221 -0
- package/src/server/db/flatfile.ts +228 -0
- package/src/server/db/index.test.ts +8 -0
- package/src/server/db/index.ts +12 -0
- package/src/server/db/inmemory.test.ts +143 -0
- package/src/server/db/inmemory.ts +143 -0
- package/src/server/db/localstorage.test.ts +73 -0
- package/src/server/db/localstorage.ts +44 -0
- package/src/server/index.test.ts +265 -0
- package/src/server/index.ts +175 -0
- package/src/server/transport/pubsub/generic-pub-sub.ts +11 -0
- package/src/server/transport/pubsub/in-memory-pub-sub.test.ts +47 -0
- package/src/server/transport/pubsub/in-memory-pub-sub.ts +28 -0
- package/src/server/transport/socketio-simultaneous.test.ts +603 -0
- package/src/server/transport/socketio.test.ts +303 -0
- package/src/server/transport/socketio.ts +279 -0
- package/src/server/util.ts +85 -0
- package/src/testing/mock-random.test.ts +45 -0
- package/src/testing/mock-random.ts +27 -0
- package/src/types.ts +511 -0
- package/testing/package.json +7 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
const assertString = (str, label) => {
|
|
2
|
+
if (!str || typeof str !== 'string') {
|
|
3
|
+
throw new Error(`Expected ${label} string, got "${str}".`);
|
|
4
|
+
}
|
|
5
|
+
};
|
|
6
|
+
const assertGameName = (name) => assertString(name, 'game name');
|
|
7
|
+
const assertMatchID = (id) => assertString(id, 'match ID');
|
|
8
|
+
const validateBody = (body, schema) => {
|
|
9
|
+
if (!body)
|
|
10
|
+
throw new Error(`Expected body, got “${body}”.`);
|
|
11
|
+
for (const key in schema) {
|
|
12
|
+
const propSchema = schema[key];
|
|
13
|
+
const types = Array.isArray(propSchema) ? propSchema : [propSchema];
|
|
14
|
+
const received = body[key];
|
|
15
|
+
if (!types.includes(typeof received)) {
|
|
16
|
+
const union = types.join('|');
|
|
17
|
+
throw new TypeError(`Expected body.${key} to be of type ${union}, got “${received}”.`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
class LobbyClientError extends Error {
|
|
22
|
+
constructor(message, details) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.details = details;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create a boardgame.io Lobby API client.
|
|
29
|
+
* @param server The API’s base URL, e.g. `http://localhost:8000`.
|
|
30
|
+
*/
|
|
31
|
+
class LobbyClient {
|
|
32
|
+
constructor({ server = '' } = {}) {
|
|
33
|
+
// strip trailing slash if passed
|
|
34
|
+
this.server = server.replace(/\/$/, '');
|
|
35
|
+
}
|
|
36
|
+
async request(route, init) {
|
|
37
|
+
const response = await fetch(this.server + route, init);
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
let details;
|
|
40
|
+
try {
|
|
41
|
+
details = await response.clone().json();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
try {
|
|
45
|
+
details = await response.text();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
details = error.message;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw new LobbyClientError(`HTTP status ${response.status}`, details);
|
|
52
|
+
}
|
|
53
|
+
return response.json();
|
|
54
|
+
}
|
|
55
|
+
async post(route, opts) {
|
|
56
|
+
let init = {
|
|
57
|
+
method: 'post',
|
|
58
|
+
body: JSON.stringify(opts.body),
|
|
59
|
+
headers: { 'Content-Type': 'application/json' },
|
|
60
|
+
};
|
|
61
|
+
if (opts.init)
|
|
62
|
+
init = {
|
|
63
|
+
...init,
|
|
64
|
+
...opts.init,
|
|
65
|
+
headers: { ...init.headers, ...opts.init.headers },
|
|
66
|
+
};
|
|
67
|
+
return this.request(route, init);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Get a list of the game names available on this server.
|
|
71
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
72
|
+
* @return Array of game names.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* lobbyClient.listGames()
|
|
76
|
+
* .then(console.log); // => ['chess', 'tic-tac-toe']
|
|
77
|
+
*/
|
|
78
|
+
async listGames(init) {
|
|
79
|
+
return this.request('/games', init);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get a list of the matches for a specific game type on the server.
|
|
83
|
+
* @param gameName The game to list for, e.g. 'tic-tac-toe'.
|
|
84
|
+
* @param where Options to filter matches by update time or gameover state
|
|
85
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
86
|
+
* @return Array of match metadata objects.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* lobbyClient.listMatches('tic-tac-toe', where: { isGameover: false })
|
|
90
|
+
* .then(data => console.log(data.matches));
|
|
91
|
+
* // => [
|
|
92
|
+
* // {
|
|
93
|
+
* // matchID: 'xyz',
|
|
94
|
+
* // gameName: 'tic-tac-toe',
|
|
95
|
+
* // players: [{ id: 0, name: 'Alice' }, { id: 1 }]
|
|
96
|
+
* // },
|
|
97
|
+
* // ...
|
|
98
|
+
* // ]
|
|
99
|
+
*/
|
|
100
|
+
async listMatches(gameName, where, init) {
|
|
101
|
+
assertGameName(gameName);
|
|
102
|
+
let query = '';
|
|
103
|
+
if (where) {
|
|
104
|
+
const queries = [];
|
|
105
|
+
const { isGameover, updatedBefore, updatedAfter } = where;
|
|
106
|
+
if (isGameover !== undefined)
|
|
107
|
+
queries.push(`isGameover=${isGameover}`);
|
|
108
|
+
if (updatedBefore)
|
|
109
|
+
queries.push(`updatedBefore=${updatedBefore}`);
|
|
110
|
+
if (updatedAfter)
|
|
111
|
+
queries.push(`updatedAfter=${updatedAfter}`);
|
|
112
|
+
if (queries.length > 0)
|
|
113
|
+
query = '?' + queries.join('&');
|
|
114
|
+
}
|
|
115
|
+
return this.request(`/games/${gameName}${query}`, init);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get metadata for a specific match.
|
|
119
|
+
* @param gameName The match’s game type, e.g. 'tic-tac-toe'.
|
|
120
|
+
* @param matchID Match ID for the match to fetch.
|
|
121
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
122
|
+
* @return A match metadata object.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* lobbyClient.getMatch('tic-tac-toe', 'xyz').then(console.log);
|
|
126
|
+
* // => {
|
|
127
|
+
* // matchID: 'xyz',
|
|
128
|
+
* // gameName: 'tic-tac-toe',
|
|
129
|
+
* // players: [{ id: 0, name: 'Alice' }, { id: 1 }]
|
|
130
|
+
* // }
|
|
131
|
+
*/
|
|
132
|
+
async getMatch(gameName, matchID, init) {
|
|
133
|
+
assertGameName(gameName);
|
|
134
|
+
assertMatchID(matchID);
|
|
135
|
+
return this.request(`/games/${gameName}/${matchID}`, init);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create a new match for a specific game type.
|
|
139
|
+
* @param gameName The game to create a match for, e.g. 'tic-tac-toe'.
|
|
140
|
+
* @param body Options required to configure match creation.
|
|
141
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
142
|
+
* @return An object containing the created `matchID`.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* lobbyClient.createMatch('tic-tac-toe', { numPlayers: 2 })
|
|
146
|
+
* .then(console.log);
|
|
147
|
+
* // => { matchID: 'xyz' }
|
|
148
|
+
*/
|
|
149
|
+
async createMatch(gameName, body, init) {
|
|
150
|
+
assertGameName(gameName);
|
|
151
|
+
validateBody(body, { numPlayers: 'number' });
|
|
152
|
+
return this.post(`/games/${gameName}/create`, { body, init });
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Join a match using its matchID.
|
|
156
|
+
* @param gameName The match’s game type, e.g. 'tic-tac-toe'.
|
|
157
|
+
* @param matchID Match ID for the match to join.
|
|
158
|
+
* @param body Options required to join match.
|
|
159
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
160
|
+
* @return Object containing `playerCredentials` for the player who joined.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* lobbyClient.joinMatch('tic-tac-toe', 'xyz', {
|
|
164
|
+
* playerID: '1',
|
|
165
|
+
* playerName: 'Bob',
|
|
166
|
+
* }).then(console.log);
|
|
167
|
+
* // => { playerID: '1', playerCredentials: 'random-string' }
|
|
168
|
+
*/
|
|
169
|
+
async joinMatch(gameName, matchID, body, init) {
|
|
170
|
+
assertGameName(gameName);
|
|
171
|
+
assertMatchID(matchID);
|
|
172
|
+
validateBody(body, {
|
|
173
|
+
playerID: ['string', 'undefined'],
|
|
174
|
+
playerName: 'string',
|
|
175
|
+
});
|
|
176
|
+
return this.post(`/games/${gameName}/${matchID}/join`, { body, init });
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Leave a previously joined match.
|
|
180
|
+
* @param gameName The match’s game type, e.g. 'tic-tac-toe'.
|
|
181
|
+
* @param matchID Match ID for the match to leave.
|
|
182
|
+
* @param body Options required to leave match.
|
|
183
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
184
|
+
* @return Promise resolves if successful.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* lobbyClient.leaveMatch('tic-tac-toe', 'xyz', {
|
|
188
|
+
* playerID: '1',
|
|
189
|
+
* credentials: 'credentials-returned-when-joining',
|
|
190
|
+
* })
|
|
191
|
+
* .then(() => console.log('Left match.'))
|
|
192
|
+
* .catch(error => console.error('Error leaving match', error));
|
|
193
|
+
*/
|
|
194
|
+
async leaveMatch(gameName, matchID, body, init) {
|
|
195
|
+
assertGameName(gameName);
|
|
196
|
+
assertMatchID(matchID);
|
|
197
|
+
validateBody(body, { playerID: 'string', credentials: 'string' });
|
|
198
|
+
await this.post(`/games/${gameName}/${matchID}/leave`, { body, init });
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Update a player’s name or custom metadata.
|
|
202
|
+
* @param gameName The match’s game type, e.g. 'tic-tac-toe'.
|
|
203
|
+
* @param matchID Match ID for the match to update.
|
|
204
|
+
* @param body Options required to update player.
|
|
205
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
206
|
+
* @return Promise resolves if successful.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* lobbyClient.updatePlayer('tic-tac-toe', 'xyz', {
|
|
210
|
+
* playerID: '0',
|
|
211
|
+
* credentials: 'credentials-returned-when-joining',
|
|
212
|
+
* newName: 'Al',
|
|
213
|
+
* })
|
|
214
|
+
* .then(() => console.log('Updated player data.'))
|
|
215
|
+
* .catch(error => console.error('Error updating data', error));
|
|
216
|
+
*/
|
|
217
|
+
async updatePlayer(gameName, matchID, body, init) {
|
|
218
|
+
assertGameName(gameName);
|
|
219
|
+
assertMatchID(matchID);
|
|
220
|
+
validateBody(body, { playerID: 'string', credentials: 'string' });
|
|
221
|
+
await this.post(`/games/${gameName}/${matchID}/update`, { body, init });
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Create a new match based on the configuration of the current match.
|
|
225
|
+
* @param gameName The match’s game type, e.g. 'tic-tac-toe'.
|
|
226
|
+
* @param matchID Match ID for the match to play again.
|
|
227
|
+
* @param body Options required to configure match.
|
|
228
|
+
* @param init Optional RequestInit interface to override defaults.
|
|
229
|
+
* @return Object containing `nextMatchID`.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* lobbyClient.playAgain('tic-tac-toe', 'xyz', {
|
|
233
|
+
* playerID: '0',
|
|
234
|
+
* credentials: 'credentials-returned-when-joining',
|
|
235
|
+
* })
|
|
236
|
+
* .then(({ nextMatchID }) => {
|
|
237
|
+
* return lobbyClient.joinMatch('tic-tac-toe', nextMatchID, {
|
|
238
|
+
* playerID: '0',
|
|
239
|
+
* playerName: 'Al',
|
|
240
|
+
* })
|
|
241
|
+
* })
|
|
242
|
+
* .then({ playerCredentials } => {
|
|
243
|
+
* console.log(playerCredentials);
|
|
244
|
+
* })
|
|
245
|
+
* .catch(console.error);
|
|
246
|
+
*/
|
|
247
|
+
async playAgain(gameName, matchID, body, init) {
|
|
248
|
+
assertGameName(gameName);
|
|
249
|
+
assertMatchID(matchID);
|
|
250
|
+
validateBody(body, { playerID: 'string', credentials: 'string' });
|
|
251
|
+
return this.post(`/games/${gameName}/${matchID}/playAgain`, { body, init });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export { LobbyClient as L, LobbyClientError as a };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { C as Client } from './client-2e653027.js';
|
|
2
|
+
export { L as LobbyClient, a as LobbyClientError } from './client-5f57c3f2.js';
|
|
3
|
+
import 'nanoid/non-secure';
|
|
4
|
+
import './Debug-0141fe2d.js';
|
|
5
|
+
import './turn-order-376d315e.js';
|
|
6
|
+
import 'immer';
|
|
7
|
+
import './plugin-random-087f861e.js';
|
|
8
|
+
import 'lodash.isplainobject';
|
|
9
|
+
import 'flatted';
|
|
10
|
+
import './reducer-c46da7e5.js';
|
|
11
|
+
import 'rfc6902';
|
|
12
|
+
import './ai-5c06e761.js';
|
|
13
|
+
import 'setimmediate';
|
|
14
|
+
import 'redux';
|
|
15
|
+
import './initialize-11d626ca.js';
|
|
16
|
+
import './transport-ce07b771.js';
|
package/dist/esm/core.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export { C as ActivePlayers, h as GameMethod, w as INVALID_MOVE, k as Stage, T as TurnOrder } from './turn-order-376d315e.js';
|
|
2
|
+
import 'immer';
|
|
3
|
+
import './plugin-random-087f861e.js';
|
|
4
|
+
import 'lodash.isplainobject';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Copyright 2018 The boardgame.io Authors
|
|
8
|
+
*
|
|
9
|
+
* Use of this source code is governed by a MIT-style
|
|
10
|
+
* license that can be found in the LICENSE file or at
|
|
11
|
+
* https://opensource.org/licenses/MIT.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* PlayerView reducers.
|
|
15
|
+
*/
|
|
16
|
+
const PlayerView = {
|
|
17
|
+
/**
|
|
18
|
+
* STRIP_SECRETS
|
|
19
|
+
*
|
|
20
|
+
* Reducer which removes a key named `secret` and
|
|
21
|
+
* removes all the keys in `players`, except for the one
|
|
22
|
+
* corresponding to the current playerID.
|
|
23
|
+
*/
|
|
24
|
+
STRIP_SECRETS: ({ G, playerID }) => {
|
|
25
|
+
const r = { ...G };
|
|
26
|
+
if (r.secret !== undefined) {
|
|
27
|
+
delete r.secret;
|
|
28
|
+
}
|
|
29
|
+
if (r.players) {
|
|
30
|
+
r.players = playerID
|
|
31
|
+
? {
|
|
32
|
+
[playerID]: r.players[playerID],
|
|
33
|
+
}
|
|
34
|
+
: {};
|
|
35
|
+
}
|
|
36
|
+
return r;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { PlayerView };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { D as Debug } from './Debug-0141fe2d.js';
|
|
2
|
+
import './turn-order-376d315e.js';
|
|
3
|
+
import 'immer';
|
|
4
|
+
import './plugin-random-087f861e.js';
|
|
5
|
+
import 'lodash.isplainobject';
|
|
6
|
+
import 'flatted';
|
|
7
|
+
import './reducer-c46da7e5.js';
|
|
8
|
+
import 'rfc6902';
|
|
9
|
+
import './ai-5c06e761.js';
|
|
10
|
+
import 'setimmediate';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { P as PlayerView } from './turn-order-376d315e.js';
|
|
2
|
+
import { createPatch } from 'rfc6902';
|
|
3
|
+
|
|
4
|
+
const applyPlayerView = (game, playerID, state) => ({
|
|
5
|
+
...state,
|
|
6
|
+
G: game.playerView({ G: state.G, ctx: state.ctx, playerID }),
|
|
7
|
+
plugins: PlayerView(state, { playerID, game }),
|
|
8
|
+
deltalog: undefined,
|
|
9
|
+
_undo: [],
|
|
10
|
+
_redo: [],
|
|
11
|
+
});
|
|
12
|
+
/** Gets a function that filters the TransportData for a given player and game. */
|
|
13
|
+
const getFilterPlayerView = (game) => (playerID, payload) => {
|
|
14
|
+
switch (payload.type) {
|
|
15
|
+
case 'patch': {
|
|
16
|
+
const [matchID, stateID, prevState, state] = payload.args;
|
|
17
|
+
const log = redactLog(state.deltalog, playerID);
|
|
18
|
+
const filteredState = applyPlayerView(game, playerID, state);
|
|
19
|
+
const newStateID = state._stateID;
|
|
20
|
+
const prevFilteredState = applyPlayerView(game, playerID, prevState);
|
|
21
|
+
const patch = createPatch(prevFilteredState, filteredState);
|
|
22
|
+
return {
|
|
23
|
+
type: 'patch',
|
|
24
|
+
args: [matchID, stateID, newStateID, patch, log],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
case 'update': {
|
|
28
|
+
const [matchID, state] = payload.args;
|
|
29
|
+
const log = redactLog(state.deltalog, playerID);
|
|
30
|
+
const filteredState = applyPlayerView(game, playerID, state);
|
|
31
|
+
return {
|
|
32
|
+
type: 'update',
|
|
33
|
+
args: [matchID, filteredState, log],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
case 'sync': {
|
|
37
|
+
const [matchID, syncInfo] = payload.args;
|
|
38
|
+
const filteredState = applyPlayerView(game, playerID, syncInfo.state);
|
|
39
|
+
const log = redactLog(syncInfo.log, playerID);
|
|
40
|
+
const newSyncInfo = {
|
|
41
|
+
...syncInfo,
|
|
42
|
+
state: filteredState,
|
|
43
|
+
log,
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
type: 'sync',
|
|
47
|
+
args: [matchID, newSyncInfo],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
default: {
|
|
51
|
+
return payload;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Redact the log.
|
|
57
|
+
*
|
|
58
|
+
* @param {Array} log - The game log (or deltalog).
|
|
59
|
+
* @param {String} playerID - The playerID that this log is
|
|
60
|
+
* to be sent to.
|
|
61
|
+
*/
|
|
62
|
+
function redactLog(log, playerID) {
|
|
63
|
+
if (log === undefined) {
|
|
64
|
+
return log;
|
|
65
|
+
}
|
|
66
|
+
return log.map((logEvent) => {
|
|
67
|
+
// filter for all other players and spectators.
|
|
68
|
+
if (playerID !== null && +playerID === +logEvent.action.payload.playerID) {
|
|
69
|
+
return logEvent;
|
|
70
|
+
}
|
|
71
|
+
if (logEvent.redact !== true) {
|
|
72
|
+
return logEvent;
|
|
73
|
+
}
|
|
74
|
+
const payload = {
|
|
75
|
+
...logEvent.action.payload,
|
|
76
|
+
args: null,
|
|
77
|
+
};
|
|
78
|
+
const filteredEvent = {
|
|
79
|
+
...logEvent,
|
|
80
|
+
action: { ...logEvent.action, payload },
|
|
81
|
+
};
|
|
82
|
+
const { redact, ...remaining } = filteredEvent;
|
|
83
|
+
return remaining;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { getFilterPlayerView as g };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { P as ProcessGameConfig } from './reducer-c46da7e5.js';
|
|
2
|
+
import { z as Setup, E as Enhance, j as GetAPIs, x as FlushAndValidate } from './turn-order-376d315e.js';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Copyright 2020 The boardgame.io Authors
|
|
6
|
+
*
|
|
7
|
+
* Use of this source code is governed by a MIT-style
|
|
8
|
+
* license that can be found in the LICENSE file or at
|
|
9
|
+
* https://opensource.org/licenses/MIT.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Creates the initial game state.
|
|
13
|
+
*/
|
|
14
|
+
function InitializeGame({ game, numPlayers, setupData, }) {
|
|
15
|
+
game = ProcessGameConfig(game);
|
|
16
|
+
if (!numPlayers) {
|
|
17
|
+
numPlayers = 2;
|
|
18
|
+
}
|
|
19
|
+
const ctx = game.flow.ctx(numPlayers);
|
|
20
|
+
let state = {
|
|
21
|
+
// User managed state.
|
|
22
|
+
G: {},
|
|
23
|
+
// Framework managed state.
|
|
24
|
+
ctx,
|
|
25
|
+
// Plugin related state.
|
|
26
|
+
plugins: {},
|
|
27
|
+
};
|
|
28
|
+
// Run plugins over initial state.
|
|
29
|
+
state = Setup(state, { game });
|
|
30
|
+
state = Enhance(state, { game, playerID: undefined });
|
|
31
|
+
const pluginAPIs = GetAPIs(state);
|
|
32
|
+
state.G = game.setup({ ...pluginAPIs, ctx: state.ctx }, setupData);
|
|
33
|
+
let initial = {
|
|
34
|
+
...state,
|
|
35
|
+
// List of {G, ctx} pairs that can be undone.
|
|
36
|
+
_undo: [],
|
|
37
|
+
// List of {G, ctx} pairs that can be redone.
|
|
38
|
+
_redo: [],
|
|
39
|
+
// A monotonically non-decreasing ID to ensure that
|
|
40
|
+
// state updates are only allowed from clients that
|
|
41
|
+
// are at the same version that the server.
|
|
42
|
+
_stateID: 0,
|
|
43
|
+
};
|
|
44
|
+
initial = game.flow.init(initial);
|
|
45
|
+
[initial] = FlushAndValidate(initial, { game });
|
|
46
|
+
// Initialize undo stack.
|
|
47
|
+
if (!game.disableUndo) {
|
|
48
|
+
initial._undo = [
|
|
49
|
+
{
|
|
50
|
+
G: initial.G,
|
|
51
|
+
ctx: initial.ctx,
|
|
52
|
+
plugins: initial.plugins,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
return initial;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { InitializeGame as I };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { I as InitializeGame } from './initialize-11d626ca.js';
|
|
2
|
+
export { C as CreateGameReducer, P as ProcessGameConfig } from './reducer-c46da7e5.js';
|
|
3
|
+
export { g as getFilterPlayerView } from './filter-player-view-2c6cc96f.js';
|
|
4
|
+
export { A as Async, S as Sync, c as createMatch } from './util-b6147cef.js';
|
|
5
|
+
export { T as Transport } from './transport-ce07b771.js';
|
|
6
|
+
import './turn-order-376d315e.js';
|
|
7
|
+
import 'immer';
|
|
8
|
+
import './plugin-random-087f861e.js';
|
|
9
|
+
import 'lodash.isplainobject';
|
|
10
|
+
import 'rfc6902';
|