Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Commit

Permalink
Add game key db requests and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslong committed Sep 12, 2016
1 parent 7da24c2 commit 01af18e
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/dbtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export type GetMessagesFact = Prom.Factory<GetMessagesParams, GetMessagesResult>
export type GetPlayerParams = string;
export type GetPlayerFact = Prom.Factory<GetPlayerParams, Player.PlayerState>;

export type GameKey = { gameKey: string; };
export type AddGameKeyParams = GameKey;
export type AddGameKeyFact = Prom.Factory<AddGameKeyParams, GameKey>;

export type GetGameKeyParams = string;
export type GetGameKeyFact = Prom.Factory<GetGameKeyParams, GameKey>;

export interface PromiseFactories extends DBCalls {
send: Prom.Factory<Message.MessageData, string>;
}
Expand All @@ -55,6 +62,8 @@ export interface DBCalls {
getMessage: GetMessageFact;
getMessages: GetMessagesFact;
getPlayer: GetPlayerFact;
addGameKey: AddGameKeyFact;
getGameKey: GetGameKeyFact;
}

export type SendMessageParams = Message.MessageData;
Expand All @@ -74,5 +83,7 @@ export function createPromiseFactories (calls: DBCalls, send: SendMessage)
getMessage: calls.getMessage,
getMessages: calls.getMessages,
getPlayer: calls.getPlayer,
addGameKey: calls.addGameKey,
getGameKey: calls.getGameKey,
};
}
32 changes: 32 additions & 0 deletions core/src/localdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import Prom = require('./utils/promise');
export interface DBState {
players: Map.Map<Player.PlayerState>;
messages: Map.Map<Message.MessageState>;
gameKeys: Map.Map<DBTypes.GameKey>;
}

export function createDB (): DBState
{
return {
players: {},
messages: {},
gameKeys: {},
};
}

Expand Down Expand Up @@ -53,6 +55,8 @@ export function createLocalDBCalls (db: DBState, timeoutMs: number)
getMessage: factory(getMessage),
getMessages: factory(getMessages),
getPlayer: factory(getPlayer),
addGameKey: factory(addGameKey),
getGameKey: factory(getGameKey),
};
}

Expand Down Expand Up @@ -230,3 +234,31 @@ export function getPlayer (
const data = db.players[email] || null;
return promise(null, data);
}

export function addGameKey (
db: DBState,
key: DBTypes.AddGameKeyParams)
{
let error: Error = undefined;

const inUse = (db.gameKeys[key.gameKey] !== undefined);

if (inUse) {
error = {
code: 'ADD GAME KEY',
message: 'could not add game key',
};
} else {
db.gameKeys[key.gameKey] = key;
}

return promise(error, key);
}

export function getGameKey (
db: DBState,
key: DBTypes.GetGameKeyParams)
{
const data = db.gameKeys[key] || null;
return promise(null, data);
}
3 changes: 3 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ConfigState {
debugDBTimeoutMs: number;
messagesTableName: string;
playersTableName: string;
gameKeyTableName: string;
emailDomain: string;
updateIntervalMs: number;
content: {
Expand Down Expand Up @@ -65,6 +66,7 @@ const debugConfig: ConfigState = {
credentials: null,
messagesTableName: 'message-dev',
playersTableName: 'player-dev',
gameKeyTableName: 'game-key-dev',
emailDomain: 'nsa.playtopsecret.com',
updateIntervalMs: 1000,
content: {
Expand Down Expand Up @@ -92,6 +94,7 @@ const releaseConfig: ConfigState = {
credentials: null,
messagesTableName: 'message-dev',
playersTableName: 'player-dev',
gameKeyTableName: 'game-key-dev',
emailDomain: 'nsa.playtopsecret.com',
updateIntervalMs: 1000,
content: {
Expand Down
42 changes: 42 additions & 0 deletions server/src/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function createDynamoDBCalls (config: Config.ConfigState): DBTypes.DBCall
const credentials = config.credentials;
const messageTable = config.messagesTableName;
const playerTable = config.playersTableName;
const gameKeyTable = config.gameKeyTableName;
const accessKeyId = credentials.awsAccessKeyId;
const secretAccessKey = credentials.awsSecretAccessKey;
const region = credentials.awsRegion;
Expand All @@ -47,6 +48,8 @@ export function createDynamoDBCalls (config: Config.ConfigState): DBTypes.DBCall
deleteMessage: bind(doc, messageTable, deleteMessage),
getMessage: bind(doc, messageTable, getMessage),
getMessages: bind(doc, messageTable, getMessages),
addGameKey: bind(doc, gameKeyTable, addGameKey),
getGameKey: bind(doc, gameKeyTable, getGameKey),
};
}

Expand Down Expand Up @@ -288,6 +291,45 @@ export function deleteAllMessages (
);
};

export function addGameKey (
docClient: DynamoDoc,
gameKeyTableName: string,
key: DBTypes.AddGameKeyParams)
{
const awsParams: DynamoDB.PutParams = {
Item: true,
TableName: gameKeyTableName,
ReturnValues: 'NONE',
};

return promise(
'addGameKey',
docClient,
docClient.put,
awsParams,
() => key);
};

export function getGameKey (
docClient: DynamoDoc,
gameKeyTableName: string,
gameKey: DBTypes.GetGameKeyParams)
{
const awsParams: DynamoDB.GetParams = {
Key: {
gameKey,
},
TableName: gameKeyTableName,
};

return promise(
'getGameKey',
docClient,
docClient.get,
awsParams,
extractItem);
};

export function extractAttributes<T> (data: { Attributes: T })
{
return data ? data.Attributes : null;
Expand Down
5 changes: 5 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export function createMessage (
numberOfChildren);
}

export function createGameKey ()
{
return { gameKey: 'test key' };
}

export function testGameData ()
{
const contentPath = 'content';
Expand Down
15 changes: 15 additions & 0 deletions test/server.dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const modifiedPlayer = Helpers.assign(player, { utcOffset: 10 });
const message0 = TestHelpers.createMessage0();
const modifiedMessage0 = Helpers.assign(message0, { fallbackSent: true });
const message1 = TestHelpers.createMessage1();
const key = TestHelpers.createGameKey();

describe('DB', function () {
describe('addPlayer', function () {
Expand Down Expand Up @@ -148,4 +149,18 @@ describe('DB', function () {
]);
})
});

describe('addGameKey', function () {
it('should return new game key', function () {
const promise = db.addGameKey(key);
return Chai.assert.eventually.deepEqual(promise, key);
})
});

describe('getGameKey', function () {
it('should return the game key', function () {
const promise = db.getGameKey(key.gameKey);
return Chai.assert.eventually.deepEqual(promise, key);
})
});
});
20 changes: 20 additions & 0 deletions test/server.localdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,24 @@ describe('DB', function () {
]);
})
});

describe('addGameKey', function () {
it('should return the added key', function () {
const db = TestHelpers.createDB();
const key = TestHelpers.createGameKey();
const promise = db.addGameKey(key);
return Chai.assert.eventually.equal(promise, key);
})
});

describe('getGameKey', function () {
it('should return the key', function () {
const db = TestHelpers.createDB();
const key = TestHelpers.createGameKey();
const promise = db.addGameKey(key).then(valid =>
db.getGameKey(key.gameKey)
);
return Chai.assert.eventually.equal(promise, key);
})
});
});

0 comments on commit 01af18e

Please sign in to comment.