Skip to content

Commit dc31a66

Browse files
committed
expose allowedMoves in ctx
1 parent da4711a commit dc31a66

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/core/flow.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,15 @@ export function FlowWithPhases({
330330
};
331331

332332
// Helper to perform start-of-phase initialization.
333-
const startPhase = function(state, phaseConfig) {
333+
const startPhase = function(state, config) {
334+
const G = config.onPhaseBegin(state.G, ctx);
335+
334336
const ctx = { ...state.ctx };
335-
const G = phaseConfig.onPhaseBegin(state.G, ctx);
336-
ctx.playOrderPos = phaseConfig.turnOrder.first(G, ctx);
337+
ctx.playOrderPos = config.turnOrder.first(G, ctx);
337338
ctx.currentPlayer = getCurrentPlayer(ctx.playOrder, ctx.playOrderPos);
338339
ctx.actionPlayers = [ctx.currentPlayer];
340+
ctx.allowedMoves = config.allowedMoves(G, ctx);
341+
339342
return { ...state, G, ctx };
340343
};
341344

@@ -347,7 +350,10 @@ export function FlowWithPhases({
347350
plainCtx = Events.detach(plainCtx);
348351
const _undo = [{ G, ctx: plainCtx }];
349352

350-
return { ...state, G, _undo, _redo: [] };
353+
const ctx = { ...state.ctx };
354+
ctx.allowedMoves = config.allowedMoves(G, ctx);
355+
356+
return { ...state, G, ctx, _undo, _redo: [] };
351357
};
352358

353359
const startGame = function(state, config) {
@@ -542,6 +548,10 @@ export function FlowWithPhases({
542548
return { ...state, ctx: { ...state.ctx, gameover } };
543549
}
544550

551+
// Update allowedMoves.
552+
const allowedMoves = conf.allowedMoves(state.G, state.ctx);
553+
state = { ...state, ctx: { ...state.ctx, allowedMoves } };
554+
545555
// Update undo / redo state.
546556
if (!endTurn) {
547557
const undo = state._undo || [];

src/server/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ test('action', async () => {
175175
G: {},
176176
ctx: {
177177
_random: { seed: 0 },
178+
allowedMoves: null,
178179
currentPlayer: '0',
179180
actionPlayers: ['0'],
180181
currentPlayerMoves: 0,
@@ -188,6 +189,7 @@ test('action', async () => {
188189
],
189190
ctx: {
190191
_random: { seed: 0 },
192+
allowedMoves: null,
191193
currentPlayer: '0',
192194
actionPlayers: ['0'],
193195
currentPlayerMoves: 0,
@@ -206,6 +208,7 @@ test('action', async () => {
206208
G: {},
207209
ctx: {
208210
_random: { seed: 0 },
211+
allowedMoves: null,
209212
currentPlayer: '1',
210213
actionPlayers: ['1'],
211214
currentPlayerMoves: 0,
@@ -219,6 +222,7 @@ test('action', async () => {
219222
],
220223
ctx: {
221224
_random: undefined,
225+
allowedMoves: null,
222226
currentPlayer: '1',
223227
actionPlayers: ['1'],
224228
currentPlayerMoves: 0,

0 commit comments

Comments
 (0)