Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiplayer client creation to be overridden #246

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ class _ClientImpl {
this.credentials = credentials;

let server = undefined;
if (multiplayer instanceof Object && 'server' in multiplayer) {
server = multiplayer.server;
let MultiplayerClientImpl = Multiplayer;
if (multiplayer instanceof Object) {
if ('server' in multiplayer) {
server = multiplayer.server;
}
if ('clientImpl' in multiplayer) {
MultiplayerClientImpl = multiplayer.clientImpl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're missing a unit test that covers this line.

}
multiplayer = true;
}

Expand Down Expand Up @@ -134,7 +140,7 @@ class _ClientImpl {
this.store = null;

if (multiplayer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just add a new option in the multiplayer argument that allows specifying a custom client. Something like:

Client({
  game: TicTacToe,
  multiplayer: {
    server: 'hostname:port',
    clientImpl: <object>,
  }
})

That way it's a tiny surgical change and the rest of the interface remains unchanged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update as requested

Note that this interface does not reflect that serverUrl and socketOpt are parameters for a specific Multiplayer client type

this.multiplayerClient = new Multiplayer({
this.multiplayerClient = new MultiplayerClientImpl({
gameID: gameID,
playerID: playerID,
gameName: game.name,
Expand Down Expand Up @@ -264,7 +270,7 @@ class _ClientImpl {
*
* @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>' }
* @param {...object} multiplayer - Set to true or { server: '<host>:<port>', clientImpl?: Multiplayer client class }
* to make a multiplayer client. The second
* syntax specifies a non-default socket server.
* @param {...object} socketOpts - Options to pass to socket.io.
Expand Down