Skip to content

Commit

Permalink
retire triggers and introduce onMove instead
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Feb 4, 2018
1 parent 79a3e63 commit cc7d44f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 62 deletions.
31 changes: 2 additions & 29 deletions docs/api/Game.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ game state and the moves. The moves are converted to a
The turn automatically ends if this function returns true (checked after each move).
* `flow.onTurnEnd` (_function_): _(G, ctx) => G_
Code to run at the end of a turn.
* `flow.triggers` (_array_): An array of objects with the format:
`{ condition: (G, ctx) => boolean, action: (G, ctx) => G }`
At the end of each move, if `condition` is `true`, then the corresponding
`action` is executed.
* `flow.onMove` (_function_): _(G, ctx) => G_
Code to run at the end of a move.
* `flow.movesPerTurn` (_number_): Ends the turn automatically if a certain number
of moves have been made.
* `flow.phases` (_array_): Optional list of game phases. See
Expand Down Expand Up @@ -130,28 +128,3 @@ const game = Game({
}
});
```

#### With triggers

```js
import { Game } from 'boardgame.io/core';
const game = Game({
setup: (numPlayers) => {
...
},
moves: {
...
},
flow: {
triggers: [
{
condition: G => isMilestone(G),
action: G => ({ ...G, milestone: true })
}
]
}
});
```
34 changes: 15 additions & 19 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,10 @@ export function Flow({ ctx, events, init, validator, processMove }) {
* @param {...object} onTurnEnd - Any code to run when a turn ends.
* (G, ctx) => G
*
* @param {...object} turnOrder - Customize the turn order (see turn-order.js).
* @param {...object} onMove - Any code to run at the end of a move.
* (G, ctx) => G
*
* @param {...object} triggers - An array of objects with the format:
* {
* condition: (G, ctx) => boolean,
* action: (G, ctx) => action,
* }
* Whenever `condition` is true the `action` is run.
* Triggers are processed one after the other in the
* order they are defined at the end of each move.
* @param {...object} turnOrder - Customize the turn order (see turn-order.js).
*
* @param {...object} endTurn - Set to false to disable the `endTurn` event.
*
Expand Down Expand Up @@ -145,6 +139,9 @@ export function Flow({ ctx, events, init, validator, processMove }) {
* // A phase-specific onTurnEnd.
* onTurnEnd: (G, ctx) => G,
*
* // A phase-specific onMove.
* onMove - (G, ctx) => G,
*
* // A phase-specific turnOrder.
* turnOrder: TurnOrder.DEFAULT,
*
Expand All @@ -161,8 +158,8 @@ export function FlowWithPhases({
endTurnIf,
endGameIf,
onTurnEnd,
onMove,
turnOrder,
triggers,
endTurn,
endPhase,
}) {
Expand All @@ -177,8 +174,8 @@ export function FlowWithPhases({
if (!endTurnIf) endTurnIf = () => false;
if (!endGameIf) endGameIf = () => undefined;
if (!onTurnEnd) onTurnEnd = G => G;
if (!onMove) onMove = G => G;
if (!turnOrder) turnOrder = TurnOrder.DEFAULT;
if (!triggers) triggers = [];

let phaseKeys = [];
let phaseMap = {};
Expand Down Expand Up @@ -208,6 +205,9 @@ export function FlowWithPhases({
if (conf.onTurnEnd === undefined) {
conf.onTurnEnd = onTurnEnd;
}
if (conf.onMove === undefined) {
conf.onMove = onMove;
}
if (conf.turnOrder === undefined) {
conf.turnOrder = turnOrder;
}
Expand Down Expand Up @@ -308,15 +308,11 @@ export function FlowWithPhases({
const currentPlayerMoves = state.ctx.currentPlayerMoves + 1;
state = { ...state, ctx: { ...state.ctx, currentPlayerMoves } };

// Process triggers.
for (const trigger of triggers) {
if (trigger.condition(state.G, state.ctx)) {
const G = trigger.action(state.G, state.ctx);
state = { ...state, G };
}
}

const conf = phaseMap[state.ctx.phase];

const G = conf.onMove(state.G, state.ctx);
state = { ...state, G };

const gameover = conf.endGameIf(state.G, state.ctx);

// End the turn automatically if endTurnIf is true or if endGameIf returns.
Expand Down
34 changes: 20 additions & 14 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,28 @@ test('onTurnEnd', () => {
}
});

test('triggers', () => {
const triggers = [
{
condition: G => G.milestone,
action: G => ({ ...G, action: true }),
},
];
test('onMove', () => {
const onMove = () => ({ A: true });

let flow = FlowWithPhases({ triggers });
let state = { G: {}, ctx: flow.ctx(2) };
{
let flow = FlowWithPhases({ onMove });
let state = { G: {}, ctx: flow.ctx(2) };
state = flow.processMove(state);
expect(state.G).toEqual({ A: true });
}

state = flow.processMove(state);
expect(state.G.action).toBe(undefined);
state.G.milestone = true;
state = flow.processMove(state);
expect(state.G.action).toBe(true);
{
let flow = FlowWithPhases({
onMove,
phases: [{ name: 'A' }, { name: 'B', onMove: () => ({ B: true }) }],
});
let state = { G: {}, ctx: flow.ctx(2) };
state = flow.processMove(state);
expect(state.G).toEqual({ A: true });
state = flow.processGameEvent(state, { type: 'endPhase' });
state = flow.processMove(state);
expect(state.G).toEqual({ B: true });
}
});

test('init', () => {
Expand Down

0 comments on commit cc7d44f

Please sign in to comment.