Skip to content

Commit

Permalink
Allowed moves as function (#164)
Browse files Browse the repository at this point in the history
* initial implementation of allowedMoves as function

* upated an allowed moves test to use a function

* added mentions of allowedMoves as function in docs

* update Game.md
  • Loading branch information
jstacoder authored and nicolodavis committed Mar 21, 2018
1 parent 45f3757 commit 9324c58
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/api/Game.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const game = Game({
onTurnEnd: ...
onPhaseBegin: ...
onPhaseEnd: ...
allowedMoves: ...
...
},
{
Expand All @@ -129,6 +130,7 @@ const game = Game({
onTurnEnd: ...
onPhaseBegin: ...
onPhaseEnd: ...
allowedMoves: ...
...
},
]
Expand Down
11 changes: 8 additions & 3 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ export function Flow({
* // A phase-specific movesPerTurn.
* movesPerTurn: integer,
*
* // List of moves that are allowed in this phase.
* allowedMoves: ['moveA', ...],
* // List of moves or a function that returns a list of moves
* // that are allowed in this phase.
* allowedMoves: (G, ctx) => ['moveA', ...],
* }
*/
export function FlowWithPhases({
Expand Down Expand Up @@ -295,6 +296,10 @@ export function FlowWithPhases({
if (conf.turnOrder === undefined) {
conf.turnOrder = turnOrder;
}
if (conf.allowedMoves && typeof conf.allowedMoves != 'function') {
const { allowedMoves } = conf;
conf.allowedMoves = () => allowedMoves;
}
}

const endTurnIfWrap = (G, ctx) => {
Expand Down Expand Up @@ -542,7 +547,7 @@ export function FlowWithPhases({
const canMakeMoveWrap = (G, ctx, opts) => {
const conf = phaseMap[ctx.phase] || {};
if (conf.allowedMoves) {
const set = new Set(conf.allowedMoves);
const set = new Set(conf.allowedMoves({ G, ctx }));
if (!set.has(opts.type)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ test('canMakeMove', () => {

flow: {
phases: [
{ name: 'A', allowedMoves: 'A' },
{ name: 'A', allowedMoves: () => ['A'] },
{ name: 'B', allowedMoves: 'B' },
{ name: 'C' },
],
Expand Down

0 comments on commit 9324c58

Please sign in to comment.