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

feat(server): Enable game setup to perform validation #831

Merged
merged 2 commits into from Oct 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/documentation/api/Game.md
Expand Up @@ -10,6 +10,12 @@
// passed through the Game Creation API.
setup: (ctx, setupData) => G,

// Optional function to validate the setupData before
// matches are created. If this returns a value,
// an error will be reported to the user and match
// creation is aborted.
validateSetupData: (setupData, numPlayers) => 'setupData is not valid!',

moves: {
// short-form move.
A: (G, ctx) => {},
Expand Down
41 changes: 41 additions & 0 deletions src/server/api.test.ts
Expand Up @@ -102,6 +102,19 @@ describe('.createRouter', () => {
}
: {},
},
{
name: 'validate',
setup: (_, setupData) =>
setupData
? {
numTokens: setupData.tokens,
}
: {},
validateSetupData: (setupData, numPlayers) =>
numPlayers == 2 && setupData.tokens !== 2
? 'Two player games must use two tokens'
: undefined,
},
];
});

Expand Down Expand Up @@ -226,6 +239,34 @@ describe('.createRouter', () => {
});
});

describe('with setupData validation', () => {
test('creates game if validation passes', async () => {
response = await request(app.callback())
.post('/games/validate/create')
.send({
numPlayers: 2,
setupData: {
tokens: 2,
},
});

expect(response.status).toEqual(200);
});

test('returns error if validation fails', async () => {
response = await request(app.callback())
.post('/games/validate/create')
.send({
numPlayers: 2,
setupData: {
tokens: 3,
},
});

expect(response.status).toEqual(400);
});
});

describe('with unlisted option', () => {
beforeEach(async () => {
response = await request(app.callback())
Expand Down
4 changes: 4 additions & 0 deletions src/server/api.ts
Expand Up @@ -124,6 +124,10 @@ export const createRouter = ({
const game = games.find(g => g.name === gameName);
if (!game) ctx.throw(404, 'Game ' + gameName + ' not found');

const setupDataError =
game.validateSetupData && game.validateSetupData(setupData, numPlayers);
if (setupDataError !== undefined) ctx.throw(400, setupDataError);

const matchID = await CreateMatch({
db,
game,
Expand Down
12 changes: 10 additions & 2 deletions src/types.ts
Expand Up @@ -231,13 +231,21 @@ interface PhaseMap<G extends any = any, CtxWithPlugins extends Ctx = Ctx> {
[phaseName: string]: PhaseConfig<G, CtxWithPlugins>;
}

export interface Game<G extends any = any, CtxWithPlugins extends Ctx = Ctx> {
export interface Game<
G extends any = any,
CtxWithPlugins extends Ctx = Ctx,
SetupData extends any = any
> {
name?: string;
minPlayers?: number;
maxPlayers?: number;
disableUndo?: boolean;
seed?: string | number;
setup?: (ctx: CtxWithPlugins, setupData?: any) => any;
setup?: (ctx: CtxWithPlugins, setupData?: SetupData) => any;
validateSetupData?: (
setupData: SetupData | undefined,
numPlayers: number
) => string | undefined;
delucis marked this conversation as resolved.
Show resolved Hide resolved
moves?: MoveMap<G, CtxWithPlugins>;
phases?: PhaseMap<G, CtxWithPlugins>;
turn?: TurnConfig<G, CtxWithPlugins>;
Expand Down