Skip to content

Commit

Permalink
create initial game state outside reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Jan 11, 2019
1 parent 8d08381 commit 6cf81e8
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 177 deletions.
22 changes: 9 additions & 13 deletions src/ai/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import Game from '../core/game';
import { CreateGameReducer } from '../core/reducer';
import { InitializeGame } from '../core/reducer';
import { MAKE_MOVE, GAME_EVENT } from '../core/action-types';
import { makeMove } from '../core/action-creators';
import { Simulate, Bot, RandomBot, MCTSBot } from './bot';
Expand Down Expand Up @@ -86,16 +86,14 @@ describe('Simulate', () => {
};

test('multiple bots', () => {
const reducer = CreateGameReducer({ game: TicTacToe });
const state = reducer(undefined, { type: 'init' });
const state = InitializeGame({ game: TicTacToe });
const { state: endState } = Simulate({ game: TicTacToe, bots, state });
expect(endState.ctx.gameover).not.toBe(undefined);
});

test('single bot', () => {
const bot = new RandomBot({ seed: 'test', enumerate });
const reducer = CreateGameReducer({ game: TicTacToe });
const state = reducer(undefined, { type: 'init' });
const state = InitializeGame({ game: TicTacToe });
const { state: endState } = Simulate({
game: TicTacToe,
bots: bot,
Expand Down Expand Up @@ -146,8 +144,7 @@ describe('MCTSBot', () => {

test('game that never ends', () => {
const game = Game({});
const reducer = CreateGameReducer({ game });
const state = reducer(undefined, { type: 'init' });
const state = InitializeGame({ game });
const bot = new MCTSBot({ seed: 'test', game, enumerate: () => [] });
const { state: endState } = Simulate({ game, bots: bot, state });
expect(endState.ctx.turn).toBe(0);
Expand All @@ -165,17 +162,17 @@ describe('MCTSBot', () => {
}),
};

const reducer = CreateGameReducer({ game: TicTacToe });
const initialState = InitializeGame({ game: TicTacToe });

for (let i = 0; i < 5; i++) {
const state = reducer(undefined, { type: 'init' });
const state = initialState;
const { state: endState } = Simulate({ game: TicTacToe, bots, state });
expect(endState.ctx.gameover).not.toEqual({ winner: '0' });
}
});

test('MCTSBot vs. MCTSBot', () => {
const reducer = CreateGameReducer({ game: TicTacToe });
const initialState = InitializeGame({ game: TicTacToe });
const iterations = 400;

for (let i = 0; i < 5; i++) {
Expand All @@ -195,7 +192,7 @@ describe('MCTSBot', () => {
iterations,
}),
};
const state = reducer(undefined, { type: 'init' });
const state = initialState;
const { state: endState } = Simulate({ game: TicTacToe, bots, state });
expect(endState.ctx.gameover).toEqual({ draw: true });
}
Expand All @@ -209,8 +206,7 @@ describe('MCTSBot', () => {
},
});

const reducer = CreateGameReducer({ game: TicTacToe });
const state = reducer(undefined, { type: 'init' });
const state = InitializeGame({ game: TicTacToe });

for (let i = 0; i < 10; i++) {
const bot = new MCTSBot({
Expand Down
11 changes: 8 additions & 3 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as Actions from '../core/action-types';
import * as ActionCreators from '../core/action-creators';
import { SocketIO } from './transport/socketio';
import { Local, LocalMaster } from './transport/local';
import { CreateGameReducer } from '../core/reducer';
import { InitializeGame, CreateGameReducer } from '../core/reducer';

/**
* createDispatchers
Expand Down Expand Up @@ -117,8 +117,13 @@ class _ClientImpl {
};
}

let initialState = null;
if (multiplayer === undefined) {
initialState = InitializeGame({ game, numPlayers });
}

this.reset = () => {
this.store.dispatch(ActionCreators.reset());
this.store.dispatch(ActionCreators.reset(initialState));
};
this.undo = () => {
this.store.dispatch(ActionCreators.undo());
Expand Down Expand Up @@ -209,7 +214,7 @@ class _ClientImpl {
enhancer = applyMiddleware(LogMiddleware, TransportMiddleware);
}

this.store = createStore(this.reducer, enhancer);
this.store = createStore(this.reducer, initialState, enhancer);

if (multiplayer && multiplayer.master_ !== undefined) {
this.transport = new Local({
Expand Down
13 changes: 7 additions & 6 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { createStore } from 'redux';
import { CreateGameReducer } from '../core/reducer';
import { InitializeGame, CreateGameReducer } from '../core/reducer';
import { Client, GetOpts, createMoveDispatchers } from './client';
import { Local } from './transport/local';
import { SocketIO } from './transport/socketio';
Expand Down Expand Up @@ -243,9 +243,10 @@ describe('move dispatchers', () => {
},
});
const reducer = CreateGameReducer({ game });
const initialState = InitializeGame({ game });

test('basic', () => {
const store = createStore(reducer);
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(game.moveNames, store);

expect(Object.getOwnPropertyNames(api)).toEqual(['A', 'B', 'C']);
Expand All @@ -268,14 +269,14 @@ describe('move dispatchers', () => {
});

test('with undefined playerID - singleplayer mode', () => {
const store = createStore(reducer);
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(game.moveNames, store);
api.B();
expect(store.getState().G).toMatchObject({ moved: '0' });
});

test('with undefined playerID - multiplayer mode', () => {
const store = createStore(reducer);
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(
game.moveNames,
store,
Expand All @@ -288,14 +289,14 @@ describe('move dispatchers', () => {
});

test('with null playerID - singleplayer mode', () => {
const store = createStore(reducer);
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(game.moveNames, store, null);
api.B();
expect(store.getState().G).toMatchObject({ moved: '0' });
});

test('with null playerID - multiplayer mode', () => {
const store = createStore(reducer);
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(game.moveNames, store, null, null, true);
api.B();
expect(store.getState().G).toMatchObject({ moved: null });
Expand Down
8 changes: 4 additions & 4 deletions src/client/log/log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Client } from '../client';
import { makeMove, gameEvent } from '../../core/action-creators';
import Game from '../../core/game';
import { GameLog } from './log';
import { CreateGameReducer } from '../../core/reducer';
import { InitializeGame, CreateGameReducer } from '../../core/reducer';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Expand All @@ -28,7 +28,7 @@ describe('layout', () => {
},
});
const reducer = CreateGameReducer({ game });
const state = reducer(undefined, { type: 'init' });
const state = InitializeGame({ game });

test('sanity', () => {
const log = [
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('pinning', () => {
});

const reducer = CreateGameReducer({ game });
let state = reducer(undefined, { type: 'init' });
let state = InitializeGame({ game });
const initialState = state;
const log = [
{ action: makeMove('A') },
Expand Down Expand Up @@ -240,7 +240,7 @@ describe('pinning', () => {
describe('payload', () => {
const game = Game({});
const reducer = CreateGameReducer({ game });
const state = reducer(undefined, { type: 'init' });
const state = InitializeGame({ game });

const log = [
{ action: makeMove('moveA'), payload: { test_payload: 'payload123' } },
Expand Down
2 changes: 1 addition & 1 deletion src/client/react-native.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ test('update gameID / playerID', () => {
},
}),
board: TestBoard,
multiplayer: true,
multiplayer: { local: true },
});
game = Enzyme.mount(<Board gameID="a" playerID="1" credentials="foo" />);
const m = game.instance().client.transport;
Expand Down
2 changes: 1 addition & 1 deletion src/client/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test('update gameID / playerID', () => {
},
}),
board: TestBoard,
multiplayer: true,
multiplayer: { local: true },
});
game = Enzyme.mount(<Board gameID="a" playerID="1" credentials="foo" />);
const m = game.instance().client.transport;
Expand Down
4 changes: 2 additions & 2 deletions src/client/transport/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class Local {
*/
async updateGameID(id) {
this.gameID = this.gameName + ':' + id;
const action = ActionCreators.reset();
const action = ActionCreators.reset(null);
this.store.dispatch(action);
await this.master.onSync(this.gameID, this.playerID, this.numPlayers);
}
Expand All @@ -142,7 +142,7 @@ export class Local {
*/
async updatePlayerID(id) {
this.playerID = id;
const action = ActionCreators.reset();
const action = ActionCreators.reset(null);
this.store.dispatch(action);
await this.master.onSync(this.gameID, this.playerID, this.numPlayers);
}
Expand Down
6 changes: 4 additions & 2 deletions src/client/transport/local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createStore } from 'redux';
import { Local, LocalMaster } from './local';
import Game from '../../core/game';
import { makeMove, gameEvent } from '../../core/action-creators';
import { CreateGameReducer } from '../../core/reducer';
import { InitializeGame, CreateGameReducer } from '../../core/reducer';

describe('LocalMaster', () => {
const game = Game({});
Expand Down Expand Up @@ -91,7 +91,9 @@ describe('Local', () => {
let store = null;

beforeEach(() => {
m.store = store = createStore(CreateGameReducer({ game }));
const reducer = CreateGameReducer({ game });
const initialState = InitializeGame({ game });
m.store = store = createStore(reducer, initialState);
});

test('returns a valid store', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/client/transport/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class SocketIO {
updateGameID(id) {
this.gameID = this.gameName + ':' + id;

const action = ActionCreators.reset();
const action = ActionCreators.reset(null);
this.store.dispatch(action);

if (this.socket) {
Expand All @@ -143,7 +143,7 @@ export class SocketIO {
updatePlayerID(id) {
this.playerID = id;

const action = ActionCreators.reset();
const action = ActionCreators.reset(null);
this.store.dispatch(action);

if (this.socket) {
Expand Down
6 changes: 4 additions & 2 deletions src/client/transport/socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createStore } from 'redux';
import { SocketIO } from './socketio';
import Game from '../../core/game';
import { makeMove } from '../../core/action-creators';
import { CreateGameReducer } from '../../core/reducer';
import { InitializeGame, CreateGameReducer } from '../../core/reducer';
import * as Actions from '../../core/action-types';

class MockSocket {
Expand Down Expand Up @@ -85,7 +85,9 @@ describe('multiplayer', () => {
let store = null;

beforeEach(() => {
m.store = store = createStore(CreateGameReducer({ game }));
const reducer = CreateGameReducer({ game });
const initialState = InitializeGame({ game });
m.store = store = createStore(reducer, initialState);
});

test('returns a valid store', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/core/action-creators.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ export const update = (state, deltalog) => ({

/**
* Used to reset the game state.
* @param {object} state - The initial state.
*/
export const reset = () => ({
export const reset = state => ({
type: Actions.RESET,
state,
clientOnly: true,
});

Expand Down
7 changes: 3 additions & 4 deletions src/core/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Events } from './events';
import { makeMove } from './action-creators';
import Game from './game';
import { CreateGameReducer } from './reducer';
import { InitializeGame, CreateGameReducer } from './reducer';

test('constructor', () => {
const flow = {};
Expand Down Expand Up @@ -52,9 +52,8 @@ test('update ctx', () => {
},
},
});
const reducer = CreateGameReducer({ game, numPlayers: 2 });

let state = reducer(undefined, { type: 'init' });
const reducer = CreateGameReducer({ game });
let state = InitializeGame({ game });
expect(state.ctx.turn).toBe(0);
state = reducer(state, makeMove('A'));
expect(state.ctx.turn).toBe(1);
Expand Down

0 comments on commit 6cf81e8

Please sign in to comment.