-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplayer.js
117 lines (90 loc) · 2.69 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var Player = function(id, nickname, socketid){
this._id = id;
this._nickname = nickname;
this._hand = [];
// Keep track if the player has pressed the ready button in lobby
this._ready = false;
this._socketid = socketid;
this._inGame = false;
// Check if a player has placed al his cards
this._done = false;
};
Player.prototype.getNickname = function(){
return this._nickname;
}
Player.prototype.getId = function(){
return this._id;
}
Player.prototype.give = function(cards) {
console.log(cards);
if( cards == null || typeof cards.length == "undefined")
return false;
for(var i = 0; i < cards.length; i++) {
console.log('given card to player '+ this._nickname);
this._hand.push(cards[i]);
}
}
Player.prototype.take = function(card){
for(var i = 0; i < this._hand.length; i++){
var handCard = this._hand[i];
if( handCard._value != card._value){
continue;
}
else if( handCard._suit != card._suit){
continue;
}
this._hand.splice(i, 1);
}
}
Player.prototype.ready = function () {
console.log( this._nickname + " is ready");
this._ready = true;
}
Player.prototype.unReady = function () {
console.log( this._nickname + " is not ready");
this._ready = false;
}
Player.prototype.isReady = function(){
return this._ready;
}
Player.prototype.getHand = function(){
return this._hand;
}
Player.prototype.hasCard = function(card){
// We receive the input from the client, so it's unsure if
// we get the right data to check the card in hand
console.log('check: ', card._suit, card._value);
if( typeof card._value == "undefined" ) {
console.log('NO VALUE');
return false;
}
// Added to make the joker card compatible
if( card._value != 0 && typeof card._suit == "undefined") {
console.log('No suit found with value of ' +card._value);
return false;
}
// Assume we have all the necessary data here
for(var i = 0; i < this._hand.length; i++){
var handCard = this._hand[i];
if( handCard._value != card._value){
console.log('Not same value', card._value, handCard._value);
continue;
}
else if( typeof handCard._suit != "undefined" && handCard._suit != card._suit){
console.log('Not same suit', card._suit, handCard._suit);
continue;
}
return true;
}
}
Player.prototype.checkDone = function(){
if( this._hand.length == 0){
this._done = true;
return true;
}
return false;
}
Player.prototype.removeCards = function(){
this._hand = [];
}
module.exports = Player;