Skip to content

Commit f18c63a

Browse files
ben196888delucis
andauthored
fix(types): playerID in playerView can be string or null (#990)
Co-authored-by: delucis <[email protected]>
1 parent 3010b25 commit f18c63a

5 files changed

Lines changed: 25 additions & 14 deletions

File tree

docs/documentation/secret-state.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Secret State
22

33
In some games you might need to hide information from
4-
players. For example, you might not want to reveal the
4+
players or spectators. For example, you might not want to reveal the
55
hands of opponents in card games.
66

77
This is easily accomplished at the UI layer (by not
@@ -18,6 +18,7 @@ from that specific player.
1818
```js
1919
const game = {
2020
// ...
21+
// `playerID` could also be null or undefined for spectators.
2122
playerView: (G, ctx, playerID) => {
2223
return StripSecrets(G, playerID);
2324
},

src/core/player-view.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@ test('secret', () => {
2121
expect(newG).toEqual({});
2222
});
2323

24-
test('players', () => {
24+
describe('players', () => {
2525
const G = {
2626
players: {
2727
'0': {},
2828
'1': {},
2929
},
3030
};
3131

32-
{
32+
test('playerID: "0"', () => {
3333
const newG = PlayerView.STRIP_SECRETS(G, {} as Ctx, '0');
3434
expect(newG.players).toEqual({ '0': {} });
35-
}
35+
});
3636

37-
{
37+
test('playerID: "1"', () => {
3838
const newG = PlayerView.STRIP_SECRETS(G, {} as Ctx, '1');
3939
expect(newG.players).toEqual({ '1': {} });
40-
}
40+
});
41+
42+
test('playerID: null', () => {
43+
const newG = PlayerView.STRIP_SECRETS(G, {} as Ctx, null);
44+
expect(newG.players).toEqual({});
45+
});
4146
});

src/core/player-view.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ export const PlayerView = {
1919
* removes all the keys in `players`, except for the one
2020
* corresponding to the current playerID.
2121
*/
22-
STRIP_SECRETS: (G: any, ctx: Ctx, playerID: PlayerID) => {
22+
STRIP_SECRETS: (G: any, ctx: Ctx, playerID: PlayerID | null) => {
2323
const r = { ...G };
2424

2525
if (r.secret !== undefined) {
2626
delete r.secret;
2727
}
2828

2929
if (r.players) {
30-
r.players = {
31-
[playerID]: r.players[playerID],
32-
};
30+
r.players = playerID
31+
? {
32+
[playerID]: r.players[playerID],
33+
}
34+
: {};
3335
}
3436

3537
return r;

src/master/filter-player-view.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { TransportData, IntermediateTransportData } from './master';
55

66
const applyPlayerView = (
77
game: Game,
8-
playerID: string,
8+
playerID: string | null,
99
state: State
1010
): State => ({
1111
...state,
@@ -19,7 +19,10 @@ const applyPlayerView = (
1919
/** Gets a function that filters the TransportData for a given player and game. */
2020
export const getFilterPlayerView =
2121
(game: Game) =>
22-
(playerID: string, payload: IntermediateTransportData): TransportData => {
22+
(
23+
playerID: string | null,
24+
payload: IntermediateTransportData
25+
): TransportData => {
2326
switch (payload.type) {
2427
case 'patch': {
2528
const [matchID, stateID, prevState, state] = payload.args;
@@ -69,7 +72,7 @@ export const getFilterPlayerView =
6972
* @param {String} playerID - The playerID that this log is
7073
* to be sent to.
7174
*/
72-
export function redactLog(log: LogEntry[], playerID: PlayerID) {
75+
export function redactLog(log: LogEntry[], playerID: PlayerID | null) {
7376
if (log === undefined) {
7477
return log;
7578
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export interface Game<
311311
};
312312
endIf?: (G: G, ctx: CtxWithPlugins) => any;
313313
onEnd?: (G: G, ctx: CtxWithPlugins) => any;
314-
playerView?: (G: G, ctx: CtxWithPlugins, playerID: PlayerID) => any;
314+
playerView?: (G: G, ctx: CtxWithPlugins, playerID: PlayerID | null) => any;
315315
plugins?: Array<Plugin<any, any, G>>;
316316
ai?: {
317317
enumerate: (

0 commit comments

Comments
 (0)