Skip to content

Commit

Permalink
make master write to proper namepspaced keys
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Jan 9, 2019
1 parent 617bb2b commit d20d26c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/master/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ export class Master {
* along with a deltalog.
*/
async onUpdate(action, stateID, gameID, playerID) {
let state = await this.storageAPI.get(gameID);
const key = `${this.game.name}:${gameID}`;
let state = await this.storageAPI.get(key);

if (state === undefined) {
logging.error(`game not found, gameID=[${gameID}]`);
logging.error(`game not found, gameID=[${key}]`);
return { error: 'game not found' };
}

Expand Down Expand Up @@ -161,21 +162,23 @@ export class Master {
log = [...log, ...state.deltalog];
const stateWithLog = { ...state, log };

await this.storageAPI.set(gameID, stateWithLog);
await this.storageAPI.set(key, stateWithLog);
}

/**
* Called when the client connects / reconnects.
* Returns the latest game state and the entire log.
*/
async onSync(gameID, playerID, numPlayers) {
const key = `${this.game.name}:${gameID}`;

const reducer = CreateGameReducer({ game: this.game, numPlayers });
let state = await this.storageAPI.get(gameID);
let state = await this.storageAPI.get(key);

if (state === undefined) {
const store = createStore(reducer);
state = store.getState();
await this.storageAPI.set(gameID, state);
await this.storageAPI.set(key, state);
}

const filteredState = {
Expand Down
4 changes: 3 additions & 1 deletion src/master/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ describe('update', async () => {
test('invalid gameID', async () => {
await master.onUpdate(action, 1, 'unknown', '1');
expect(sendAll).not.toHaveBeenCalled();
expect(error).toHaveBeenCalledWith(`game not found, gameID=[unknown]`);
expect(error).toHaveBeenCalledWith(
`game not found, gameID=[default:unknown]`
);
});

test('invalid stateID', async () => {
Expand Down

0 comments on commit d20d26c

Please sign in to comment.