Skip to content

Commit 6cf81e8

Browse files
committed
create initial game state outside reducer
1 parent 8d08381 commit 6cf81e8

23 files changed

Lines changed: 130 additions & 177 deletions

src/ai/bot.test.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import Game from '../core/game';
10-
import { CreateGameReducer } from '../core/reducer';
10+
import { InitializeGame } from '../core/reducer';
1111
import { MAKE_MOVE, GAME_EVENT } from '../core/action-types';
1212
import { makeMove } from '../core/action-creators';
1313
import { Simulate, Bot, RandomBot, MCTSBot } from './bot';
@@ -86,16 +86,14 @@ describe('Simulate', () => {
8686
};
8787

8888
test('multiple bots', () => {
89-
const reducer = CreateGameReducer({ game: TicTacToe });
90-
const state = reducer(undefined, { type: 'init' });
89+
const state = InitializeGame({ game: TicTacToe });
9190
const { state: endState } = Simulate({ game: TicTacToe, bots, state });
9291
expect(endState.ctx.gameover).not.toBe(undefined);
9392
});
9493

9594
test('single bot', () => {
9695
const bot = new RandomBot({ seed: 'test', enumerate });
97-
const reducer = CreateGameReducer({ game: TicTacToe });
98-
const state = reducer(undefined, { type: 'init' });
96+
const state = InitializeGame({ game: TicTacToe });
9997
const { state: endState } = Simulate({
10098
game: TicTacToe,
10199
bots: bot,
@@ -146,8 +144,7 @@ describe('MCTSBot', () => {
146144

147145
test('game that never ends', () => {
148146
const game = Game({});
149-
const reducer = CreateGameReducer({ game });
150-
const state = reducer(undefined, { type: 'init' });
147+
const state = InitializeGame({ game });
151148
const bot = new MCTSBot({ seed: 'test', game, enumerate: () => [] });
152149
const { state: endState } = Simulate({ game, bots: bot, state });
153150
expect(endState.ctx.turn).toBe(0);
@@ -165,17 +162,17 @@ describe('MCTSBot', () => {
165162
}),
166163
};
167164

168-
const reducer = CreateGameReducer({ game: TicTacToe });
165+
const initialState = InitializeGame({ game: TicTacToe });
169166

170167
for (let i = 0; i < 5; i++) {
171-
const state = reducer(undefined, { type: 'init' });
168+
const state = initialState;
172169
const { state: endState } = Simulate({ game: TicTacToe, bots, state });
173170
expect(endState.ctx.gameover).not.toEqual({ winner: '0' });
174171
}
175172
});
176173

177174
test('MCTSBot vs. MCTSBot', () => {
178-
const reducer = CreateGameReducer({ game: TicTacToe });
175+
const initialState = InitializeGame({ game: TicTacToe });
179176
const iterations = 400;
180177

181178
for (let i = 0; i < 5; i++) {
@@ -195,7 +192,7 @@ describe('MCTSBot', () => {
195192
iterations,
196193
}),
197194
};
198-
const state = reducer(undefined, { type: 'init' });
195+
const state = initialState;
199196
const { state: endState } = Simulate({ game: TicTacToe, bots, state });
200197
expect(endState.ctx.gameover).toEqual({ draw: true });
201198
}
@@ -209,8 +206,7 @@ describe('MCTSBot', () => {
209206
},
210207
});
211208

212-
const reducer = CreateGameReducer({ game: TicTacToe });
213-
const state = reducer(undefined, { type: 'init' });
209+
const state = InitializeGame({ game: TicTacToe });
214210

215211
for (let i = 0; i < 10; i++) {
216212
const bot = new MCTSBot({

src/client/client.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as Actions from '../core/action-types';
1111
import * as ActionCreators from '../core/action-creators';
1212
import { SocketIO } from './transport/socketio';
1313
import { Local, LocalMaster } from './transport/local';
14-
import { CreateGameReducer } from '../core/reducer';
14+
import { InitializeGame, CreateGameReducer } from '../core/reducer';
1515

1616
/**
1717
* createDispatchers
@@ -117,8 +117,13 @@ class _ClientImpl {
117117
};
118118
}
119119

120+
let initialState = null;
121+
if (multiplayer === undefined) {
122+
initialState = InitializeGame({ game, numPlayers });
123+
}
124+
120125
this.reset = () => {
121-
this.store.dispatch(ActionCreators.reset());
126+
this.store.dispatch(ActionCreators.reset(initialState));
122127
};
123128
this.undo = () => {
124129
this.store.dispatch(ActionCreators.undo());
@@ -209,7 +214,7 @@ class _ClientImpl {
209214
enhancer = applyMiddleware(LogMiddleware, TransportMiddleware);
210215
}
211216

212-
this.store = createStore(this.reducer, enhancer);
217+
this.store = createStore(this.reducer, initialState, enhancer);
213218

214219
if (multiplayer && multiplayer.master_ !== undefined) {
215220
this.transport = new Local({

src/client/client.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { createStore } from 'redux';
10-
import { CreateGameReducer } from '../core/reducer';
10+
import { InitializeGame, CreateGameReducer } from '../core/reducer';
1111
import { Client, GetOpts, createMoveDispatchers } from './client';
1212
import { Local } from './transport/local';
1313
import { SocketIO } from './transport/socketio';
@@ -243,9 +243,10 @@ describe('move dispatchers', () => {
243243
},
244244
});
245245
const reducer = CreateGameReducer({ game });
246+
const initialState = InitializeGame({ game });
246247

247248
test('basic', () => {
248-
const store = createStore(reducer);
249+
const store = createStore(reducer, initialState);
249250
const api = createMoveDispatchers(game.moveNames, store);
250251

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

270271
test('with undefined playerID - singleplayer mode', () => {
271-
const store = createStore(reducer);
272+
const store = createStore(reducer, initialState);
272273
const api = createMoveDispatchers(game.moveNames, store);
273274
api.B();
274275
expect(store.getState().G).toMatchObject({ moved: '0' });
275276
});
276277

277278
test('with undefined playerID - multiplayer mode', () => {
278-
const store = createStore(reducer);
279+
const store = createStore(reducer, initialState);
279280
const api = createMoveDispatchers(
280281
game.moveNames,
281282
store,
@@ -288,14 +289,14 @@ describe('move dispatchers', () => {
288289
});
289290

290291
test('with null playerID - singleplayer mode', () => {
291-
const store = createStore(reducer);
292+
const store = createStore(reducer, initialState);
292293
const api = createMoveDispatchers(game.moveNames, store, null);
293294
api.B();
294295
expect(store.getState().G).toMatchObject({ moved: '0' });
295296
});
296297

297298
test('with null playerID - multiplayer mode', () => {
298-
const store = createStore(reducer);
299+
const store = createStore(reducer, initialState);
299300
const api = createMoveDispatchers(game.moveNames, store, null, null, true);
300301
api.B();
301302
expect(store.getState().G).toMatchObject({ moved: null });

src/client/log/log.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Client } from '../client';
1111
import { makeMove, gameEvent } from '../../core/action-creators';
1212
import Game from '../../core/game';
1313
import { GameLog } from './log';
14-
import { CreateGameReducer } from '../../core/reducer';
14+
import { InitializeGame, CreateGameReducer } from '../../core/reducer';
1515
import Enzyme from 'enzyme';
1616
import Adapter from 'enzyme-adapter-react-16';
1717

@@ -28,7 +28,7 @@ describe('layout', () => {
2828
},
2929
});
3030
const reducer = CreateGameReducer({ game });
31-
const state = reducer(undefined, { type: 'init' });
31+
const state = InitializeGame({ game });
3232

3333
test('sanity', () => {
3434
const log = [
@@ -161,7 +161,7 @@ describe('pinning', () => {
161161
});
162162

163163
const reducer = CreateGameReducer({ game });
164-
let state = reducer(undefined, { type: 'init' });
164+
let state = InitializeGame({ game });
165165
const initialState = state;
166166
const log = [
167167
{ action: makeMove('A') },
@@ -240,7 +240,7 @@ describe('pinning', () => {
240240
describe('payload', () => {
241241
const game = Game({});
242242
const reducer = CreateGameReducer({ game });
243-
const state = reducer(undefined, { type: 'init' });
243+
const state = InitializeGame({ game });
244244

245245
const log = [
246246
{ action: makeMove('moveA'), payload: { test_payload: 'payload123' } },

src/client/react-native.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ test('update gameID / playerID', () => {
123123
},
124124
}),
125125
board: TestBoard,
126-
multiplayer: true,
126+
multiplayer: { local: true },
127127
});
128128
game = Enzyme.mount(<Board gameID="a" playerID="1" credentials="foo" />);
129129
const m = game.instance().client.transport;

src/client/react.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ test('update gameID / playerID', () => {
135135
},
136136
}),
137137
board: TestBoard,
138-
multiplayer: true,
138+
multiplayer: { local: true },
139139
});
140140
game = Enzyme.mount(<Board gameID="a" playerID="1" credentials="foo" />);
141141
const m = game.instance().client.transport;

src/client/transport/local.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class Local {
131131
*/
132132
async updateGameID(id) {
133133
this.gameID = this.gameName + ':' + id;
134-
const action = ActionCreators.reset();
134+
const action = ActionCreators.reset(null);
135135
this.store.dispatch(action);
136136
await this.master.onSync(this.gameID, this.playerID, this.numPlayers);
137137
}
@@ -142,7 +142,7 @@ export class Local {
142142
*/
143143
async updatePlayerID(id) {
144144
this.playerID = id;
145-
const action = ActionCreators.reset();
145+
const action = ActionCreators.reset(null);
146146
this.store.dispatch(action);
147147
await this.master.onSync(this.gameID, this.playerID, this.numPlayers);
148148
}

src/client/transport/local.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createStore } from 'redux';
1010
import { Local, LocalMaster } from './local';
1111
import Game from '../../core/game';
1212
import { makeMove, gameEvent } from '../../core/action-creators';
13-
import { CreateGameReducer } from '../../core/reducer';
13+
import { InitializeGame, CreateGameReducer } from '../../core/reducer';
1414

1515
describe('LocalMaster', () => {
1616
const game = Game({});
@@ -91,7 +91,9 @@ describe('Local', () => {
9191
let store = null;
9292

9393
beforeEach(() => {
94-
m.store = store = createStore(CreateGameReducer({ game }));
94+
const reducer = CreateGameReducer({ game });
95+
const initialState = InitializeGame({ game });
96+
m.store = store = createStore(reducer, initialState);
9597
});
9698

9799
test('returns a valid store', () => {

src/client/transport/socketio.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class SocketIO {
128128
updateGameID(id) {
129129
this.gameID = this.gameName + ':' + id;
130130

131-
const action = ActionCreators.reset();
131+
const action = ActionCreators.reset(null);
132132
this.store.dispatch(action);
133133

134134
if (this.socket) {
@@ -143,7 +143,7 @@ export class SocketIO {
143143
updatePlayerID(id) {
144144
this.playerID = id;
145145

146-
const action = ActionCreators.reset();
146+
const action = ActionCreators.reset(null);
147147
this.store.dispatch(action);
148148

149149
if (this.socket) {

src/client/transport/socketio.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createStore } from 'redux';
1010
import { SocketIO } from './socketio';
1111
import Game from '../../core/game';
1212
import { makeMove } from '../../core/action-creators';
13-
import { CreateGameReducer } from '../../core/reducer';
13+
import { InitializeGame, CreateGameReducer } from '../../core/reducer';
1414
import * as Actions from '../../core/action-types';
1515

1616
class MockSocket {
@@ -85,7 +85,9 @@ describe('multiplayer', () => {
8585
let store = null;
8686

8787
beforeEach(() => {
88-
m.store = store = createStore(CreateGameReducer({ game }));
88+
const reducer = CreateGameReducer({ game });
89+
const initialState = InitializeGame({ game });
90+
m.store = store = createStore(reducer, initialState);
8991
});
9092

9193
test('returns a valid store', () => {

0 commit comments

Comments
 (0)