Skip to content

Commit 9cd3fdf

Browse files
Stefan-Hankenicolodavis
authored andcommitted
allow to modify actionPlayers via Events (#157)
* allow to modify actionPlayers via Events * minor style tweak * remove trailing whitespace
1 parent 8c6fee1 commit 9cd3fdf

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/client/client.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ test('event dispatchers', () => {
7777
'undo',
7878
'redo',
7979
'endTurn',
80+
'changeActionPlayers',
8081
]);
8182
expect(store.getState().ctx.turn).toBe(0);
8283
api.endTurn();
@@ -99,6 +100,7 @@ test('event dispatchers', () => {
99100
'endTurn',
100101
'endPhase',
101102
'endGame',
103+
'changeActionPlayers',
102104
]);
103105
expect(store.getState().ctx.turn).toBe(0);
104106
api.endTurn();
@@ -118,7 +120,7 @@ test('event dispatchers', () => {
118120
const reducer = createGameReducer({ game, numPlayers: 2 });
119121
const store = createStore(reducer);
120122
const api = createEventDispatchers(game.flow.eventNames, store);
121-
expect(Object.getOwnPropertyNames(api)).toEqual([]);
123+
expect(Object.getOwnPropertyNames(api)).toEqual(['changeActionPlayers']);
122124
}
123125

124126
{
@@ -136,6 +138,7 @@ test('event dispatchers', () => {
136138
'redo',
137139
'endTurn',
138140
'endPhase',
141+
'changeActionPlayers',
139142
]);
140143
expect(store.getState().ctx.turn).toBe(0);
141144
api.endTurn();

src/core/flow.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ export function FlowWithPhases({
563563
if (endPhase) enabledEvents['endPhase'] = endPhaseEvent;
564564
if (endGame) enabledEvents['endGame'] = endGameEvent;
565565

566+
enabledEvents['changeActionPlayers'] = (state, actionPlayers) => {
567+
return { G: state.G, ctx: { ...state.ctx, actionPlayers } };
568+
};
569+
566570
return Flow({
567571
ctx: numPlayers => ({
568572
numPlayers,

src/core/flow.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,61 @@ test('endGame', () => {
617617
expect(t.ctx.gameover).toBe(42);
618618
}
619619
});
620+
621+
test('change action players', () => {
622+
const flow = FlowWithPhases({});
623+
const state = { ctx: {} };
624+
const newState = flow.processGameEvent(state, {
625+
type: 'changeActionPlayers',
626+
args: [[1, 2]],
627+
});
628+
expect(newState.ctx.actionPlayers).toMatchObject([1, 2]);
629+
});
630+
631+
test('change action players - reducer', () => {
632+
let game = Game({
633+
moves: {
634+
playMilitia: (G, ctx) => {
635+
// change which players need to act
636+
ctx.events.changeActionPlayers([1, 2, 3]);
637+
return { ...G, playedCard: 'Militia' };
638+
},
639+
dropCards: (G, ctx) => {
640+
if (G.playedCard === 'Militia') {
641+
let actedOnMilitia = G.actedOnMilitia || [];
642+
actedOnMilitia.push(ctx.playerID);
643+
644+
// this player did drop and must not take another action.
645+
var newActionPlayers = [...ctx.actionPlayers].filter(
646+
pn => pn !== ctx.playerID
647+
);
648+
ctx.events.changeActionPlayers(newActionPlayers);
649+
650+
let playedCard = G.playedCard;
651+
if (actedOnMilitia.length === 3) {
652+
ctx.events.changeActionPlayers([0]);
653+
actedOnMilitia = undefined;
654+
playedCard = undefined;
655+
}
656+
return { ...G, actedOnMilitia, playedCard };
657+
} else {
658+
return G;
659+
}
660+
},
661+
},
662+
});
663+
664+
const reducer = createGameReducer({ game, numPlayers: 4 });
665+
666+
let state = reducer(undefined, { type: 'init' });
667+
state = reducer(state, makeMove('playMilitia'));
668+
expect(state.ctx.actionPlayers).toMatchObject([1, 2, 3]);
669+
670+
state = reducer(state, makeMove('dropCards', undefined, 1));
671+
expect(state.ctx.actionPlayers).toMatchObject([2, 3]);
672+
state = reducer(state, makeMove('dropCards', undefined, 3));
673+
expect(state.ctx.actionPlayers).toMatchObject([2]);
674+
state = reducer(state, makeMove('dropCards', undefined, 2));
675+
expect(state.ctx.actionPlayers).toMatchObject([0]);
676+
expect(state.G).toMatchObject({});
677+
});

0 commit comments

Comments
 (0)