/
player.js
59 lines (50 loc) · 1.32 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var Game = require(__dirname+'/game')
, i18n = require('i18n');
var Player = function(name,email,socket) {
this.name=name;
this.email=email;
var _socket=socket;
this.notify = function (type,data,fn){
if(_socket)
{
if(fn && data)
{
_socket.emit(type,data,fn);
}
else if(data)
{
_socket.emit(type,data);
}
else
{
_socket.emit(type);
}
}
}
this.isEqual = function(otherPlayer){
var bret=false;
if(this.id && otherPlayer.id)
return this.id === otherPlayer.id
else
{
if(this.id || otherPlayer.id)
return false;
if(this.name && otherPlayer.name)
bret = this.name === otherPlayer.name;
else if(this.name || otherPlayer.name)
return false;
if(bret && this.email && otherPlayer.email)
bret = this.email === otherPlayer.email;
else if(this.email || otherPlayer.email)
return false;
}
return bret;
}
};
Player.prototype.join = function(game){
if(typeof(game) !== 'object') { throw new Error(i18n.__('Game must be an object'));}
game.addPlayer(this);
};
module.exports.create = function(name,email,socket) {
return new Player(name,email,socket);
};