Skip to content

Commit

Permalink
make changeActionPlayers an opt-in event
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed May 20, 2018
1 parent 5946a87 commit 7a80f66
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
3 changes: 3 additions & 0 deletions examples/react/modules/turnorder/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const TurnExample = Game({
return nextG;
},
},

flow: {
changeActionPlayers: true,

onTurnBegin: (G, ctx) => {
const currentPlayer = ctx.currentPlayer;
const playersNext = [...G.players];
Expand Down
14 changes: 4 additions & 10 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ test('event dispatchers', () => {
const reducer = createGameReducer({ game, numPlayers: 2 });
const store = createStore(reducer);
const api = createEventDispatchers(game.flow.eventNames, store);
expect(Object.getOwnPropertyNames(api)).toEqual([
'endTurn',
'changeActionPlayers',
]);
expect(Object.getOwnPropertyNames(api)).toEqual(['endTurn']);
expect(store.getState().ctx.turn).toBe(0);
api.endTurn();
expect(store.getState().ctx.turn).toBe(1);
Expand All @@ -87,6 +84,7 @@ test('event dispatchers', () => {
flow: {
endPhase: true,
endGame: true,
changeActionPlayers: true,
},
});
const reducer = createGameReducer({ game, numPlayers: 2 });
Expand Down Expand Up @@ -115,7 +113,7 @@ test('event dispatchers', () => {
const reducer = createGameReducer({ game, numPlayers: 2 });
const store = createStore(reducer);
const api = createEventDispatchers(game.flow.eventNames, store);
expect(Object.getOwnPropertyNames(api)).toEqual(['changeActionPlayers']);
expect(Object.getOwnPropertyNames(api)).toEqual([]);
}

{
Expand All @@ -128,11 +126,7 @@ test('event dispatchers', () => {
const reducer = createGameReducer({ game, numPlayers: 2 });
const store = createStore(reducer);
const api = createEventDispatchers(game.flow.eventNames, store);
expect(Object.getOwnPropertyNames(api)).toEqual([
'endTurn',
'endPhase',
'changeActionPlayers',
]);
expect(Object.getOwnPropertyNames(api)).toEqual(['endTurn', 'endPhase']);
expect(store.getState().ctx.turn).toBe(0);
api.endTurn();
expect(store.getState().ctx.turn).toBe(1);
Expand Down
32 changes: 25 additions & 7 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ export function Flow({
*
* @param {...object} endGame - Set to true to enable the `endGame` event.
*
* @param {...object} changeActionPlayers - Set to true to enable the `changeActionPlayers` event.
*
* @param {...object} allowedMoves - List of moves that are allowed.
* This can be either a list of
* move names or a function with the
Expand Down Expand Up @@ -241,6 +243,7 @@ export function FlowWithPhases({
endTurn,
endPhase,
endGame,
changeActionPlayers,
undoableMoves,
allowedMoves,
optimisticUpdate,
Expand All @@ -255,6 +258,9 @@ export function FlowWithPhases({
if (endGame === undefined) {
endGame = false;
}
if (changeActionPlayers === undefined) {
changeActionPlayers = false;
}
if (optimisticUpdate === undefined) {
optimisticUpdate = () => true;
}
Expand Down Expand Up @@ -514,6 +520,13 @@ export function FlowWithPhases({
return { ...state, ctx: { ...state.ctx, gameover: arg } };
}

function changeActionPlayersEvent(state, actionPlayers) {
if (actionPlayers && actionPlayers.length) {
return { ...state, ctx: { ...state.ctx, actionPlayers } };
}
return state;
}

function processMove(state, action, dispatch) {
const conf = phaseMap[state.ctx.phase];

Expand Down Expand Up @@ -592,13 +605,18 @@ export function FlowWithPhases({
};

let enabledEvents = {};
if (endTurn) enabledEvents['endTurn'] = endTurnEvent;
if (endPhase) enabledEvents['endPhase'] = endPhaseEvent;
if (endGame) enabledEvents['endGame'] = endGameEvent;

enabledEvents['changeActionPlayers'] = (state, actionPlayers) => {
return { ...state, ctx: { ...state.ctx, actionPlayers } };
};
if (endTurn) {
enabledEvents['endTurn'] = endTurnEvent;
}
if (endPhase) {
enabledEvents['endPhase'] = endPhaseEvent;
}
if (endGame) {
enabledEvents['endGame'] = endGameEvent;
}
if (changeActionPlayers) {
enabledEvents['changeActionPlayers'] = changeActionPlayersEvent;
}

return Flow({
ctx: numPlayers => ({
Expand Down
4 changes: 3 additions & 1 deletion src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ test('resetGame', () => {
});

test('change action players', () => {
const flow = FlowWithPhases({});
const flow = FlowWithPhases({ changeActionPlayers: true });
const state = { ctx: {} };
const newState = flow.processGameEvent(state, {
type: 'changeActionPlayers',
Expand All @@ -610,6 +610,8 @@ test('change action players', () => {

test('change action players - reducer', () => {
let game = Game({
flow: { changeActionPlayers: true },

moves: {
playMilitia: (G, ctx) => {
// change which players need to act
Expand Down

0 comments on commit 7a80f66

Please sign in to comment.