Skip to content

Commit

Permalink
make Random API present in first onTurnBegin and onPhaseBegin
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Jun 15, 2018
1 parent 0201f96 commit 7a61f09
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/client/react-native.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ test('undo/redo', () => {

board.props.undo();
expect(board.props.G).toEqual(initial.G);
expect(board.props.ctx).toEqual(initial.ctx);

board.props.redo();
expect(board.props.G).toEqual({ arg: 42 });
Expand Down
1 change: 0 additions & 1 deletion src/client/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ test('undo/redo', () => {

board.props.undo();
expect(board.props.G).toEqual(initial.G);
expect(board.props.ctx).toEqual(initial.ctx);

board.props.redo();
expect(board.props.G).toEqual({ arg: 42 });
Expand Down
14 changes: 14 additions & 0 deletions src/core/random.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ test('Random API is not executed optimisitically', () => {
expect(state.G.die).not.toBeDefined();
}
});

test('onTurnBegin has the Random API at the beginning of the game', () => {
let random;
const game = Game({
flow: {
onTurnBegin: (G, ctx) => {
random = ctx.random;
},
},
});
const reducer = CreateGameReducer({ game });
reducer(undefined, { type: 'init' });
expect(random).toBeDefined();
});
8 changes: 3 additions & 5 deletions src/core/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ export function CreateGameReducer({ game, numPlayers, multiplayer }) {
_initial: {},
};

// Initialize PRNG state.
initial.ctx = random.update(initial.ctx);

const state = game.flow.init({ G: initial.G, ctx: initial.ctx });
const state = game.flow.init({ G: initial.G, ctx: ctxWithAPI });

initial.G = state.G;
initial.ctx = state.ctx;
initial._undo = state._undo;
initial.ctx = random.update(state.ctx);
initial.ctx = Random.detach(initial.ctx);

const deepCopy = obj => JSON.parse(JSON.stringify(obj));
initial._initial = deepCopy(initial);
Expand Down

0 comments on commit 7a61f09

Please sign in to comment.