-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtable.js
More file actions
160 lines (135 loc) · 3.43 KB
/
Copy pathtable.js
File metadata and controls
160 lines (135 loc) · 3.43 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Game = require('./game.js');
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
function Table(tableID) {
this.id = tableID;
this.name = "";
this.key = "";
this.status = "available";
this.players = [];
this.board = null;
this.readyToPlayCounter = 0;
this.playerLimit = 2;
this.pack = [];
this.cardsOnTable = [];
this.actionCard = false;
this.requestActionCard = false;
this.penalisingActionCard = false;
this.forcedDraw = 0;
this.suiteRequest = "";
this.numberRequest = "";
this.gameObj = null;
this.firstRound = true;
};
Table.prototype.progressRound = function(player) {
if(player){ // if a player is defined in parameters, then progress round on table
for(var i = 0; i < this.players.length; i++) {
this.players[i].turnFinished = false;
if(this.players[i].id == player.id) { //when player is the same that plays, end their turn
player.turnFinished = true;
}
}
}else{ // if not; we are in suite request case, we need to block the round until a suite request is made
for(var i = 0; i < this.players.length; i++) {
this.players[i].turnFinished = true;
}
}
}
Table.prototype.setName = function(name){
this.name = name;
};
Table.prototype.getName = function(){
return this.name;
};
Table.prototype.setKey = function(key){
this.key = key;
};
Table.prototype.getKey = function(){
return this.key;
};
Table.prototype.setStatus = function(status){
this.status = status;
};
Table.prototype.isPrivate = function(){
return this.key !== "";
};
Table.prototype.isAvailable = function(){
return this.status === "available";
};
Table.prototype.isFull = function(){
return this.status === "full";
};
Table.prototype.isPlaying = function(){
return this.status === "playing";
};
Table.prototype.getRemainingSpots = function(){
return this.playerLimit - this.players.length;
};
Table.prototype.addPlayer = function(player) {
if (this.status === "available") {
var found = false;
for(var i = 0; i < this.players.length; i++) {
if(this.players[i].id == player.id){
found = true;
break;
}
}
if(!found){
this.players.push(player);
if(this.players.length == this.playerLimit){
//this.status = "playing";
for(var i = 0; i < this.players.length; i++){
this.players[i].status = "intable";
}
}
return true;
}
}
return false;
};
Table.prototype.removePlayer = function(player){
var index = -1;
for(var i = 0; i < this.players.length; i++){
if(this.players[i].id === player.id){
index = i;
break;
}
}
if(index != -1){
this.players.remove(index);
}
};
Table.prototype.assignBoard = function(board) {
if (this.board === null) {
this.board = board;
return true;
}
return false;
};
Table.prototype.isAvailable = function() {
if((this.playerLimit >= this.players.length) && (this.status === "available")) {
return true;
} else {
return false;
}
//return (this.playerLimit > this.players.length);
};
Table.prototype.hasBoard = function() {
return this.board !== null;
};
Table.prototype.createMessageObject = function() {
var table = this;
var TableMessage = function(){
this.id = table.id;
this.name = table.name;
this.status = table.status;
this.players = table.players;
this.playerLimit = table.playerLimit;
this.board = table.board;
};
return new TableMessage();
};
module.exports = Table;