Skip to content

Commit

Permalink
allow multiplayer client creation to be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
amitport committed Aug 1, 2018
1 parent 268aab8 commit 18ebdad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
49 changes: 23 additions & 26 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,25 @@ export const createEventDispatchers = createDispatchers.bind(null, 'gameEvent');
export const createMoveDispatchers = createDispatchers.bind(null, 'makeMove');

/**
* Implementation of Client (see below).
* Client
*
* boardgame.io JS client.
*
* @param {...object} game - The return value of `Game`.
* @param {...object} numPlayers - The number of players.
* @param {...object} multiplayer - Set to true or { server: '<host>:<port>' }
* to make a multiplayer client. The second
* syntax specifies a non-default socket server.
* @param {...object} socketOpts - Options to pass to socket.io.
* @param {...object} gameID - The gameID that you want to connect to.
* @param {...object} playerID - The playerID associated with this client.
* @param {...string} credentials - The authentication credentials associated with this client.
*
* Returns:
* A JS object that provides an API to interact with the
* game by dispatching moves and events.
*/
class _ClientImpl {
export class Client {
constructor({
game,
ai,
Expand Down Expand Up @@ -134,7 +150,7 @@ class _ClientImpl {
this.store = null;

if (multiplayer) {
this.multiplayerClient = new Multiplayer({
this.multiplayerClient = this.createMultiplayerClient({
gameID: gameID,
playerID: playerID,
gameName: game.name,
Expand All @@ -155,6 +171,10 @@ class _ClientImpl {
this.createDispatchers();
}

createMultiplayerClient(opt) {
return new Multiplayer(opt);
}

subscribe(fn) {
this.store.subscribe(fn);

Expand Down Expand Up @@ -256,26 +276,3 @@ class _ClientImpl {
this.createDispatchers();
}
}

/**
* Client
*
* boardgame.io JS client.
*
* @param {...object} game - The return value of `Game`.
* @param {...object} numPlayers - The number of players.
* @param {...object} multiplayer - Set to true or { server: '<host>:<port>' }
* to make a multiplayer client. The second
* syntax specifies a non-default socket server.
* @param {...object} socketOpts - Options to pass to socket.io.
* @param {...object} gameID - The gameID that you want to connect to.
* @param {...object} playerID - The playerID associated with this client.
* @param {...string} credentials - The authentication credentials associated with this client.
*
* Returns:
* A JS object that provides an API to interact with the
* game by dispatching moves and events.
*/
export function Client(opts) {
return new _ClientImpl(opts);
}
12 changes: 6 additions & 6 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Game from '../core/game';
import { RandomBot } from '../ai/bot';

test('move api', () => {
const client = Client({
const client = new Client({
game: Game({
moves: {
A: (G, ctx, arg) => ({ arg }),
Expand All @@ -32,7 +32,7 @@ test('move api', () => {
});

test('isActive', () => {
const client = Client({
const client = new Client({
game: Game({
moves: {
A: (G, ctx, arg) => ({ arg }),
Expand All @@ -52,7 +52,7 @@ test('isActive', () => {
});

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

Expand Down Expand Up @@ -82,7 +82,7 @@ describe('step', () => {
});

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

ai: {
Expand All @@ -98,7 +98,7 @@ test('multiplayer server set when provided', () => {
let host = 'host';
let port = '4321';

const client = Client({
const client = new Client({
game: Game({}),
multiplayer: { server: host + ':' + port },
});
Expand All @@ -118,7 +118,7 @@ test('accepts enhancer for store', () => {
dispatch: (spyDispatcher = jest.fn(vanillaStore.dispatch)),
};
};
const client = Client({
const client = new Client({
game: Game({
moves: {
A: (G, ctx, arg) => ({ arg }),
Expand Down
2 changes: 1 addition & 1 deletion src/client/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function Client({ game, numPlayers, board, multiplayer, enhancer }) {
constructor(props) {
super(props);

this.client = RawClient({
this.client = new RawClient({
game,
numPlayers,
multiplayer,
Expand Down
2 changes: 1 addition & 1 deletion src/client/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function Client({
constructor(props) {
super(props);

this.client = RawClient({
this.client = new RawClient({
game,
ai,
numPlayers,
Expand Down

0 comments on commit 18ebdad

Please sign in to comment.