Skip to content

Commit

Permalink
move player check from server to flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Hanke committed Mar 12, 2018
1 parent 07c4a5c commit ab086ce
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import { TurnOrder } from './turn-order';
* the client while waiting for
* the result of execution from
* the server.
* @param {...object} canMakeMove - (G, ctx, playerID) => boolean
* Predicate to determine whether the player
* identified by playerID is allowed to make a move.
*/
export function Flow({
ctx,
Expand All @@ -44,12 +47,16 @@ export function Flow({
validator,
processMove,
optimisticUpdate,
canMakeMove,
}) {
if (!ctx) ctx = () => ({});
if (!events) events = {};
if (!init) init = state => state;
if (!validator) validator = () => true;
if (!processMove) processMove = state => state;
if (!canMakeMove)
canMakeMove = (G, ctx, playerID) =>
ctx.currentPlayer === 'any' || playerID === ctx.currentPlayer;

if (optimisticUpdate === undefined) {
optimisticUpdate = () => true;
Expand Down Expand Up @@ -89,6 +96,8 @@ export function Flow({
},

optimisticUpdate,

canMakeMove,
};
}

Expand Down Expand Up @@ -198,6 +207,7 @@ export function FlowWithPhases({
endPhase,
undo,
optimisticUpdate,
canMakeMove,
}) {
// Attach defaults.
if (endPhase === undefined && phases) {
Expand Down Expand Up @@ -501,5 +511,6 @@ export function FlowWithPhases({
events: enabledEvents,
validator,
processMove,
canMakeMove,
});
}
20 changes: 20 additions & 0 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,23 @@ test('undo / redo', () => {
state = reducer(state, gameEvent('undo'));
expect(state.G).toEqual({ A: true });
});

test('canMakeMove', () => {
// default behaviour
let flow = Flow({});
expect(flow.canMakeMove({}, {}, 0)).toBe(false);
expect(flow.canMakeMove({}, { currentPlayer: 0 }, 0)).toBe(true);
expect(flow.canMakeMove({}, { currentPlayer: 'any' }, 0)).toBe(true);

// no one can make a move
flow = Flow({ canMakeMove: () => false });
expect(flow.canMakeMove({}, {}, 0)).toBe(false);
expect(flow.canMakeMove({}, { currentPlayer: 0 }, 0)).toBe(false);
expect(flow.canMakeMove({}, {}, 'any')).toBe(false);

// flow with phases passes canMakeMove
flow = FlowWithPhases({ canMakeMove: () => false });
expect(flow.canMakeMove({}, {}, 0)).toBe(false);
expect(flow.canMakeMove({}, { currentPlayer: 0 }, 0)).toBe(false);
expect(flow.canMakeMove({}, {}, 'any')).toBe(false);
});
8 changes: 2 additions & 6 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ export function Server({ games, db, _clientInfo, _roomInfo }) {
return;
}

// Bail out if the player making the move is not
// the current player.
if (
state.ctx.currentPlayer != 'any' &&
playerID != state.ctx.currentPlayer
) {
// Check whether the player is allowed to make the move
if (!game.flow.canMakeMove(game, state.ctx, playerID)) {
return;
}

Expand Down

0 comments on commit ab086ce

Please sign in to comment.