Skip to content

Commit

Permalink
retire Game(). call it internally instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Sep 10, 2019
1 parent 063dbcb commit 10ef457
Show file tree
Hide file tree
Showing 28 changed files with 353 additions and 388 deletions.
2 changes: 1 addition & 1 deletion packages/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://opensource.org/licenses/MIT.
*/

import Game from '../src/core/game.js';
import { Game } from '../src/core/game.js';
import {
InitializeGame,
CreateGameReducer,
Expand Down
4 changes: 2 additions & 2 deletions src/ai/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* https://opensource.org/licenses/MIT.
*/

import Game from '../core/game';
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';
import { Game } from '../core/game';

function IsVictory(cells) {
const positions = [
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('MCTSBot', () => {
});

test('game that never ends', () => {
const game = Game({});
const game = {};
const state = InitializeGame({ game });
const bot = new MCTSBot({ seed: 'test', game, enumerate: () => [] });
const { state: endState } = Simulate({ game, bots: bot, state });
Expand Down
16 changes: 9 additions & 7 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { createStore, compose, applyMiddleware } from 'redux';
import * as Actions from '../core/action-types';
import * as ActionCreators from '../core/action-creators';
import { Game } from '../core/game';
import { error } from '../core/logger';
import { SocketIO } from './transport/socketio';
import { Local, LocalMaster } from './transport/local';
Expand Down Expand Up @@ -92,15 +93,15 @@ class _ClientImpl {
credentials,
enhancer,
}) {
this.game = game;
this.game = Game(game);
this.playerID = playerID;
this.gameID = gameID;
this.credentials = credentials;
this.multiplayer = multiplayer;
this.subscribeCallback = () => {};

this.reducer = CreateGameReducer({
game,
game: this.game,
numPlayers,
multiplayer,
});
Expand All @@ -124,7 +125,7 @@ class _ClientImpl {

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

this.reset = () => {
Expand Down Expand Up @@ -252,24 +253,25 @@ class _ClientImpl {
}

if (multiplayer.local === true) {
if (localMaster_ === null || localMaster_.game !== game) {
localMaster_ = new LocalMaster(game);
if (localMaster_ === null || localMaster_.config !== game) {
localMaster_ = new LocalMaster(this.game);
localMaster_.config = game;
}

this.transport = new Local({
master: localMaster_,
store: this.store,
gameID: gameID,
playerID: playerID,
gameName: game.name,
gameName: this.game.name,
numPlayers,
});
} else if (multiplayer.server !== undefined) {
this.transport = new SocketIO({
store: this.store,
gameID: gameID,
playerID: playerID,
gameName: game.name,
gameName: this.game.name,
numPlayers,
server: multiplayer.server,
socketOpts,
Expand Down
48 changes: 24 additions & 24 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import { createStore } from 'redux';
import { InitializeGame, CreateGameReducer } from '../core/reducer';
import { Client, createMoveDispatchers } from './client';
import { Game } from '../core/game';
import { Local } from './transport/local';
import { SocketIO } from './transport/socketio';
import { update, sync, makeMove, gameEvent } from '../core/action-creators';
import Game from '../core/game';
import { RandomBot } from '../ai/bot';
import { error } from '../core/logger';

Expand All @@ -23,11 +23,11 @@ jest.mock('../core/logger', () => ({

test('move api', () => {
const client = Client({
game: Game({
game: {
moves: {
A: (G, ctx, arg) => ({ arg }),
},
}),
},
});

expect(client.getState().G).toEqual({});
Expand All @@ -39,7 +39,7 @@ describe('namespaced moves', () => {
let client;
beforeAll(() => {
client = Client({
game: Game({
game: {
moves: {
A: () => 'A',
},
Expand All @@ -52,7 +52,7 @@ describe('namespaced moves', () => {
},
},
},
}),
},
});
});

Expand Down Expand Up @@ -88,13 +88,13 @@ describe('namespaced moves', () => {

test('isActive', () => {
const client = Client({
game: Game({
game: {
moves: {
A: (G, ctx, arg) => ({ arg }),
},

endIf: G => G.arg == 42,
}),
},
});

expect(client.getState().G).toEqual({});
Expand All @@ -106,7 +106,7 @@ test('isActive', () => {

describe('step', () => {
const client = Client({
game: Game({
game: {
setup: () => ({ moved: false }),

moves: {
Expand All @@ -118,7 +118,7 @@ describe('step', () => {
endIf(G) {
if (G.moved) return true;
},
}),
},

ai: {
bot: RandomBot,
Expand All @@ -134,7 +134,7 @@ describe('step', () => {

test('does not crash on empty action', () => {
const client = Client({
game: Game({}),
game: {},

ai: {
bot: RandomBot,
Expand All @@ -153,7 +153,7 @@ describe('multiplayer', () => {

beforeAll(() => {
client = Client({
game: Game({ moves: { A: () => {} } }),
game: { moves: { A: () => {} } },
multiplayer: { server: host + ':' + port },
});
client.connect();
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('multiplayer', () => {

beforeAll(() => {
client = Client({
game: Game({}),
game: {},
multiplayer: true,
});
client.connect();
Expand All @@ -203,7 +203,7 @@ describe('multiplayer', () => {

beforeAll(() => {
spec = {
game: Game({ moves: { A: (G, ctx) => ({ A: ctx.playerID }) } }),
game: { moves: { A: (G, ctx) => ({ A: ctx.playerID }) } },
multiplayer: { local: true },
};

Expand Down Expand Up @@ -255,7 +255,7 @@ describe('multiplayer', () => {

beforeAll(() => {
client = Client({
game: Game({ moves: { A: () => {} } }),
game: { moves: { A: () => {} } },
multiplayer: { transport: CustomTransport },
});
});
Expand All @@ -274,7 +274,7 @@ describe('multiplayer', () => {
describe('invalid spec', () => {
test('logs error', () => {
Client({
game: Game({ moves: { A: () => {} } }),
game: { moves: { A: () => {} } },
multiplayer: { blah: true },
});
expect(error).toHaveBeenCalledWith('invalid multiplayer spec');
Expand All @@ -292,11 +292,11 @@ test('accepts enhancer for store', () => {
};
};
const client = Client({
game: Game({
game: {
moves: {
A: (G, ctx, arg) => ({ arg }),
},
}),
},
enhancer: spyEnhancer,
});

Expand All @@ -307,7 +307,7 @@ test('accepts enhancer for store', () => {

describe('event dispatchers', () => {
test('default', () => {
const game = Game({});
const game = {};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual(['endTurn']);
expect(client.getState().ctx.turn).toBe(0);
Expand All @@ -316,11 +316,11 @@ describe('event dispatchers', () => {
});

test('all events', () => {
const game = Game({
const game = {
endPhase: true,
endGame: true,
setActionPlayers: true,
});
};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual([
'endTurn',
Expand All @@ -334,10 +334,10 @@ describe('event dispatchers', () => {
});

test('no events', () => {
const game = Game({
const game = {
endPhase: false,
endTurn: false,
});
};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual([]);
});
Expand Down Expand Up @@ -418,11 +418,11 @@ describe('log handling', () => {

beforeEach(() => {
client = Client({
game: Game({
game: {
moves: {
A: () => ({}),
},
}),
},
});
});

Expand Down
5 changes: 2 additions & 3 deletions src/client/debug/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import React from 'react';
import { stringify } from 'flatted';
import { Client } from '../client';
import { sync } from '../../core/action-creators';
import Game from '../../core/game';
import { createStore } from 'redux';
import { Debug } from './debug';
import Mousetrap from 'mousetrap';
Expand Down Expand Up @@ -147,11 +146,11 @@ describe('log', () => {

test('hover', () => {
const overrideGameState = jest.fn();
const game = Game({
const game = {
moves: {
A: (G, ctx, arg) => ({ arg }),
},
});
};

const client = Client({ game });
client.moves.A(42);
Expand Down
15 changes: 7 additions & 8 deletions src/client/log/log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import React from 'react';
import { Client } from '../client';
import { makeMove, automaticGameEvent } from '../../core/action-creators';
import Game from '../../core/game';
import { GameLog } from './log';
import { InitializeGame, CreateGameReducer } from '../../core/reducer';
import Enzyme from 'enzyme';
Expand All @@ -18,13 +17,13 @@ import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });

describe('layout', () => {
const game = Game({
const game = {
startingPhase: 'A',
phases: {
A: { next: 'B' },
B: { next: 'A' },
},
});
};
const reducer = CreateGameReducer({ game });
const state = InitializeGame({ game });

Expand Down Expand Up @@ -73,7 +72,7 @@ describe('time travel', () => {
let hoverState;

beforeAll(() => {
const game = Game({
const game = {
moves: {
A: (G, ctx, arg) => {
return { arg };
Expand All @@ -83,7 +82,7 @@ describe('time travel', () => {
turn: {
endIf: G => G && G.arg == 42,
},
});
};

client = Client({ game });
const initialState = client.getState()._initial;
Expand Down Expand Up @@ -157,12 +156,12 @@ describe('time travel', () => {
});

describe('pinning', () => {
const game = Game({
const game = {
moves: {
A: () => ({ A: true }),
B: () => ({ B: true }),
},
});
};

const reducer = CreateGameReducer({ game });
let state = InitializeGame({ game });
Expand Down Expand Up @@ -242,7 +241,7 @@ describe('pinning', () => {
});

describe('payload', () => {
const game = Game({});
const game = {};
const reducer = CreateGameReducer({ game });
const state = InitializeGame({ game });

Expand Down

0 comments on commit 10ef457

Please sign in to comment.