Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): playerID in playerView can be string or null #990

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/documentation/secret-state.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Secret State

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

This is easily accomplished at the UI layer (by not
Expand All @@ -18,6 +18,7 @@ from that specific player.
```js
const game = {
// ...
// `playerID` could also be null or undefined for spectators.
playerView: (G, ctx, playerID) => {
return StripSecrets(G, playerID);
},
Expand Down
15 changes: 10 additions & 5 deletions src/core/player-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ test('secret', () => {
expect(newG).toEqual({});
});

test('players', () => {
describe('players', () => {
const G = {
players: {
'0': {},
'1': {},
},
};

{
test('playerID: "0"', () => {
const newG = PlayerView.STRIP_SECRETS(G, {} as Ctx, '0');
expect(newG.players).toEqual({ '0': {} });
}
});

{
test('playerID: "1"', () => {
const newG = PlayerView.STRIP_SECRETS(G, {} as Ctx, '1');
expect(newG.players).toEqual({ '1': {} });
}
});

test('playerID: null', () => {
const newG = PlayerView.STRIP_SECRETS(G, {} as Ctx, null);
expect(newG.players).toEqual({});
});
});
10 changes: 6 additions & 4 deletions src/core/player-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ export const PlayerView = {
* removes all the keys in `players`, except for the one
* corresponding to the current playerID.
*/
STRIP_SECRETS: (G: any, ctx: Ctx, playerID: PlayerID) => {
STRIP_SECRETS: (G: any, ctx: Ctx, playerID: PlayerID | null) => {
const r = { ...G };

if (r.secret !== undefined) {
delete r.secret;
}

if (r.players) {
r.players = {
[playerID]: r.players[playerID],
};
r.players = playerID
? {
[playerID]: r.players[playerID],
}
: {};
}

return r;
Expand Down
9 changes: 6 additions & 3 deletions src/master/filter-player-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { TransportData, IntermediateTransportData } from './master';

const applyPlayerView = (
game: Game,
playerID: string,
playerID: string | null,
state: State
): State => ({
...state,
Expand All @@ -19,7 +19,10 @@ const applyPlayerView = (
/** Gets a function that filters the TransportData for a given player and game. */
export const getFilterPlayerView =
(game: Game) =>
(playerID: string, payload: IntermediateTransportData): TransportData => {
(
playerID: string | null,
payload: IntermediateTransportData
): TransportData => {
switch (payload.type) {
case 'patch': {
const [matchID, stateID, prevState, state] = payload.args;
Expand Down Expand Up @@ -69,7 +72,7 @@ export const getFilterPlayerView =
* @param {String} playerID - The playerID that this log is
* to be sent to.
*/
export function redactLog(log: LogEntry[], playerID: PlayerID) {
export function redactLog(log: LogEntry[], playerID: PlayerID | null) {
if (log === undefined) {
return log;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export interface Game<
};
endIf?: (G: G, ctx: CtxWithPlugins) => any;
onEnd?: (G: G, ctx: CtxWithPlugins) => any;
playerView?: (G: G, ctx: CtxWithPlugins, playerID: PlayerID) => any;
playerView?: (G: G, ctx: CtxWithPlugins, playerID: PlayerID | null) => any;
plugins?: Array<Plugin<any, any, G>>;
ai?: {
enumerate: (
Expand Down