Skip to content

Commit

Permalink
add createGame to StorageAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Mar 24, 2020
1 parent ca2ac84 commit 62f58d2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/server/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class AsyncStorage extends StorageAPI.Async {

constructor(args: any = {}) {
super();
const { fetch, setState, setMetadata, listGames, wipe } = args;
const { createGame, fetch, setState, setMetadata, listGames, wipe } = args;
this.mocks = {
createGame: createGame || jest.fn(),
setState: setState || jest.fn(),
fetch: fetch || jest.fn(() => ({})),
setMetadata: setMetadata || jest.fn(),
Expand All @@ -31,6 +32,10 @@ class AsyncStorage extends StorageAPI.Async {

async connect() {}

async createGame(...args) {
this.mocks.createGame(...args);
}

async fetch(...args) {
return this.mocks.fetch(...args);
}
Expand Down
18 changes: 18 additions & 0 deletions src/server/db/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export interface ListGamesOpts {
gameName?: string;
}

/**
* Options passed when creating a new game.
*/
export interface CreateGameOpts {
initialState: State;
metadata: Server.GameMetadata;
}

export abstract class Async {
/* istanbul ignore next */
type() {
Expand All @@ -48,6 +56,11 @@ export abstract class Async {
*/
abstract connect();

/**
* Create a new game.
*/
abstract createGame(gameID: string, opts: CreateGameOpts): Promise<void>;

/**
* Update the game state.
*/
Expand Down Expand Up @@ -92,6 +105,11 @@ export abstract class Sync {
return;
}

/**
* Create a new game.
*/
abstract createGame(gameID: string, opts: CreateGameOpts): void;

/**
* Update the game state.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/server/db/flatfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ describe('FlatFile', () => {
// Create game.
const state: unknown = { a: 1 };
const metadata: unknown = { metadata: true };
await db.setState('gameID', state as State);
await db.setMetadata('gameID', metadata as Server.GameMetadata);

await db.createGame('gameID', {
initialState: state as State,
metadata: metadata as Server.GameMetadata,
});

// Must return created game.
const result2 = await db.fetch('gameID', { state: true, metadata: true });
Expand Down
8 changes: 8 additions & 0 deletions src/server/db/flatfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class FlatFile extends StorageAPI.Async {
return;
}

async createGame(
gameID: string,
opts: StorageAPI.CreateGameOpts
): Promise<void> {
await this.setState(gameID, opts.initialState);
await this.setMetadata(gameID, opts.metadata);
}

async fetch<O extends StorageAPI.FetchOpts>(
gameID: string,
opts: O
Expand Down
11 changes: 7 additions & 4 deletions src/server/db/inmemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ describe('InMemory', () => {
let stateEntry: unknown = { a: 1 };

// Create game.
db.setMetadata('gameID', {
gameName: 'tic-tac-toe',
} as Server.GameMetadata);
db.setState('gameID', stateEntry as State);
db.createGame('gameID', {
metadata: {
gameName: 'tic-tac-toe',
} as Server.GameMetadata,
initialState: stateEntry as State,
});

// Must return created game.
const { state } = db.fetch('gameID', { state: true });
expect(state).toEqual(stateEntry);
Expand Down
8 changes: 8 additions & 0 deletions src/server/db/inmemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export class InMemory extends StorageAPI.Sync {
this.log = new Map();
}

/**
* Create a new game.
*/
createGame(gameID: string, opts: StorageAPI.CreateGameOpts) {
this.setState(gameID, opts.initialState);
this.setMetadata(gameID, opts.metadata);
}

/**
* Write the game metadata to the in-memory object.
*/
Expand Down

0 comments on commit 62f58d2

Please sign in to comment.