server/player.js
function(client, relayServer, id) {
  this.client = client;
  this.relayServer = relayServer;
  this.id = id;

  debug("" + id + ": start player");

  var addPlayerToGame = function(data) {
    var game = this.relayServer.addPlayerToGame(this, data.gameId, data.data);
    if (!game) {
      // TODO: Make this URL set from some global so we can set it some place else.
      debug("game does not exist:", data.gameId);
      this.disconnect();
    } else {
      this.setGame(game);
    }
  }.bind(this);

  this.setGame = function(game) {
    this.game = game;
  }.bind(this);

  var assignAsServerForGame = function(data) {
    this.client.on('message', undefined);
    this.client.on('disconnect', undefined);
    this.relayServer.assignAsClientForGame(data, this.client);
  }.bind(this);

  var passMessageFromPlayerToGame = function(data) {
    if (!this.game) {
      console.warn("player " + this.id + " has no game");
      return;
    }
    this.game.send(this, {
      cmd: 'update',
      id: this.id,
      data: data,
    });
  }.bind(this);

  var messageHandlers = {
    'join':   addPlayerToGame,
    'server': assignAsServerForGame,
    'update': passMessageFromPlayerToGame,
  };

  var onMessage = function(message) {
    var cmd = message.cmd;
    var handler = messageHandlers[cmd];
    if (!handler) {
      console.error("unknown player message: " + cmd);
      return;
    }

    handler(message.data);
  };

  var onDisconnect = function() {
    debug("" + this.id + ": disconnected");
    if (this.game) {
      this.game.removePlayer(this);
    }
    this.disconnect();
  }.bind(this);

  client.on('message', onMessage);
  client.on('disconnect', onDisconnect);
}
function(client, relayServer, id) {
  this.client = client;
  this.relayServer = relayServer;
  this.id = id;

  debug("" + id + ": start player");

  var addPlayerToGame = function(data) {
    var game = this.relayServer.addPlayerToGame(this, data.gameId, data.data);
    if (!game) {
      // TODO: Make this URL set from some global so we can set it some place else.
      debug("game does not exist:", data.gameId);
      this.disconnect();
    } else {
      this.setGame(game);
    }
  }.bind(this);

  this.setGame = function(game) {
    this.game = game;
  }.bind(this);

  var assignAsServerForGame = function(data) {
    this.client.on('message', undefined);
    this.client.on('disconnect', undefined);
    this.relayServer.assignAsClientForGame(data, this.client);
  }.bind(this);

  var passMessageFromPlayerToGame = function(data) {
    if (!this.game) {
      console.warn("player " + this.id + " has no game");
      return;
    }
    this.game.send(this, {
      cmd: 'update',
      id: this.id,
      data: data,
    });
  }.bind(this);

  var messageHandlers = {
    'join':   addPlayerToGame,
    'server': assignAsServerForGame,
    'update': passMessageFromPlayerToGame,
  };

  var onMessage = function(message) {
    var cmd = message.cmd;
    var handler = messageHandlers[cmd];
    if (!handler) {
      console.error("unknown player message: " + cmd);
      return;
    }

    handler(message.data);
  };

  var onDisconnect = function() {
    debug("" + this.id + ": disconnected");
    if (this.game) {
      this.game.removePlayer(this);
    }
    this.disconnect();
  }.bind(this);

  client.on('message', onMessage);
  client.on('disconnect', onDisconnect);
}
function ( client , relayServer , id ) { this . client = client ; this . relayServer = relayServer ; this . id = id ; debug ( "" + id + ": start player" ) ; var addPlayerToGame = function ( data ) { var game = this . relayServer . addPlayerToGame ( this , data . gameId , data . data ) ; if ( ! game ) { // TODO: Make this URL set from some global so we can set it some place else. debug ( "game does not exist:" , data . gameId ) ; this . disconnect ( ) ; } else { this . setGame ( game ) ; } } . bind ( this ) ; this . setGame = function ( game ) { this . game = game ; } . bind ( this ) ; var assignAsServerForGame = function ( data ) { this . client . on ( 'message' , undefined ) ; this . client . on ( 'disconnect' , undefined ) ; this . relayServer . assignAsClientForGame ( data , this . client ) ; } . bind ( this ) ; var passMessageFromPlayerToGame = function ( data ) { if ( ! this . game ) { console . warn ( "player " + this . id + " has no game" ) ; return ; } this . game . send ( this , { cmd : 'update' , id : this . id , data : data , } ) ; } . bind ( this ) ; var messageHandlers = { 'join' : addPlayerToGame , 'server' : assignAsServerForGame , 'update' : passMessageFromPlayerToGame , } ; var onMessage = function ( message ) { var cmd = message . cmd ; var handler = messageHandlers [ cmd ] ; if ( ! handler ) { console . error ( "unknown player message: " + cmd ) ; return ; } handler ( message . data ) ; } ; var onDisconnect = function ( ) { debug ( "" + this . id + ": disconnected" ) ; if ( this . game ) { this . game . removePlayer ( this ) ; } this . disconnect ( ) ; } . bind ( this ) ; client . on ( 'message' , onMessage ) ; client . on ( 'disconnect' , onDisconnect ) ; }
A Player in a game.

@constructor
@param {!Client} client The websocket for this player
@param {!RelayServer} relayServer The server managing this
player.
@param {string} id a unique id for this player.
A Player in a game .