-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.js
78 lines (62 loc) · 2.04 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var proxy = require('./jsutil.js').proxy;
var Crypto = require('./jsutil.js').Crypto;
var log = console.log;
this.Player = function() {
// Private class properties and functions
var numPlayersCreated = 0;
return function() {
var id = 0; // needs to be private so players can't pretend to be each other
this.publicId = 0;
this.score = 0;
this.numSets = 0;
this.numFalseSets = 0;
this.name = null;
this.color = 0;
this.isRequestingMoreCards = false;
this.isRequestingGameRestart = false;
this.isRequestingGameEnd = false;
this.lastSeen = 0;
var _game = null;
function init() {
this.name = 'Player' + numPlayersCreated;
this.publicId = numPlayersCreated;
this.color = this.publicId % 15;
numPlayersCreated += 1;
id = Crypto.getRandomKey();
log('Creating Player with id: ' + id);
// TODO: create unregister player function to unregister these handlers so that we can delete the player
}
this.getId = function() {
return id;
};
this.joinGame = function(game) {
if (game.addPlayer(this)) {
_game = game;
return true;
}
return false;
};
this.selectCards = function(cards) {
log('Player:selectCards(' + JSON.stringify(cards) + ')');
if (!_game) {
return false;
}
if (!_game.areCardsInPlay(cards)) {
return false;
}
if (_game.processSet(cards)) {
this.score += 1;
this.numSets += 1;
return true;
} else {
this.score -= 1;
this.numFalseSets += 1;
return false;
}
};
init.apply(this, arguments);
};
}();
this.Player.equals = function(playerA, playerB) {
return playerA.getId() === playerB.getId();
};