-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_server.js
364 lines (302 loc) · 11.2 KB
/
node_server.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cardid = 1;
var num_players = 0;
var Cards;
var NUM_DEAL = 5;
var PlayersInfo = new Array();
var Players = new Array();
var PlayerInfo = function(id,name,numberOfStacks){
var self = this;
self.p_name = name;
self.p_id = id;
self.numberOfStacks = numberOfStacks;
return self;
}
var indexOf =function(rank)
{
if(1< rank && rank <=10)
return rank;
console.log("Rank is greater than 10 :"+rank);
if(rank=="A")
return 1;
if(rank=="J")
return 11;
if(rank=="Q")
return 12;
if(rank=="K")
return 13;
}
var Player = function(id,name,cards)
{
var self = this;
self.p_id = id;
self.p_name = name;
self.p_cards = cards;
self.stackCount = new Array(14);
for(i=1;i<14;i++)
self.stackCount[i] = 0;
self.numberOfStacks = 0;
self.turn = false;
return self;
}
var Card = function(rank,suit)
{
var self = this;
self.id = cardid++;
self.rank = rank;
self.suit = suit;
self.name = self.rank + self.suit;
return self;
}
var removePlayer=function(player)
{
for(i=0;i<Players.length;i++)
{
if(Players[i].p_id == player.p_id)
{
Players.splice(i,1);
i--;
return;
}
}
}
function stackShuffle() {
var i, j, k;
var temp;
// Shuffle the stack 'n' times.
for (j = 0; j < Cards.length; j++) {
k = Math.floor(Math.random() * Cards.length);
temp = Cards[j];
Cards[j] =Cards[k];
Cards[k] = temp;
}
}
function stackDeal() {
if (Cards.length > 0)
return Cards.shift();
else
return null;
}
var removeRank = function(p_cards,rank)
{
console.log("+++++++++++++++++++++++"+p_cards.length+"Rank:"+rank);
for(i=0;i<p_cards.length;i++)
{
if(p_cards[i].rank == rank)
{
p_cards.splice(i,1);
i--;
}
}
console.log("-------------------------"+p_cards.length+"Rank:"+rank);
return p_cards;
}
var pickRandom = function()
{
return stackDeal();
}
var removeCards = function(p_id,num){
var askedPlayer;
for (i = 0; i < Players.length; i++) {
if (Players[i].p_id == p_id) {
askedPlayer = Players[i];
for (j = 0; j < askedPlayer.p_cards.length; j++) {
if (askedPlayer.p_cards[j].rank == num) {
askedPlayer.p_cards.splice(j,1);
j--;
}
}
}
}
return askedPlayer;
}
var displayStack = function(stack)
{
for(i=1;i<14;i++)
{
console.log(i+":"+stack[i]);
}
}
var findCards = function (num, p_id) {
var resultAsk = new Array();
for (i = 0; i < Players.length; i++) {
if (Players[i].p_id == p_id) {
for (j = 0; j < Players[i].p_cards.length; j++) {
if (Players[i].p_cards[j].rank == num) {
resultAsk.push(Players[i].p_cards[j]);
}
}
}
}
return resultAsk;
}
var gameEnd = function(){
var max =-1;
var winner;
for(i=0;i<Players.length;i++)
{
if(max < Players[i].numberOfStacks)
{
max = Players[i].numberOfStacks;
winner = Players[i];
}
}
console.log("Game end 1");
io.sockets.emit('gameOver',winner);
console.log("Game end 2");
}
var createDeck = function() {
var n =1;
var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K");
var suits = new Array("C", "D", "H", "S");
var i, j, k;
var m;
l =0;
m = ranks.length * suits.length;
// Set array of cards.
Cards = new Array(m);
// Fill the array with 'n' packs of cards.
// for (i = 0; i < n; i++)
for (j = 0; j < suits.length; j++)
for (k = 0; k < ranks.length; k++)
Cards[l++] =
new Card(ranks[k], suits[j]);
}
app.get('/', function(req, res){
res.sendFile('index.html', { root: __dirname });
});
io.on('connection', function(socket){
var newPlayer;
socket.on('requestCards',function(name){
num_players++;
if(num_players==1)
{
createDeck();
}
stackShuffle();
var player_cards = new Array(NUM_DEAL);
for(i = 0; i < NUM_DEAL; i++) {
var cardReturned = stackDeal();
console.log("Card Removed"+cardReturned.name);
player_cards[i] = cardReturned;
}
newPlayer = new Player(socket.id,name,player_cards);
for(i =0;i< NUM_DEAL;i++)
{
//console.log(indexOf(player_cards[i].rank));
newPlayer.stackCount[indexOf(player_cards[i].rank)]++;
if(newPlayer.stackCount[indexOf(player_cards[i].rank)] == 4)
{
newPlayer.p_cards = removeRank(newPlayer.p_cards,card.rank);
newPlayer.stackCount[indexOf(player_cards[i].rank)] = 0;
newPlayer.numberOfStacks++;
var playerInfo = new PlayerInfo(newPlayer.p_id,newPlayer.p_name,newPlayer.numberOfStacks);
io.sockets.emit('updatePlayerStackCount',playerInfo);
}
//console.log(newPlayer.stackCount[indexOf(player_cards[i].rank)]);
}
displayStack(newPlayer.stackCount);
Players.push(newPlayer);
socket.emit('sendCards',newPlayer);
if(num_players == 1)
{
console.log("Emiting turn for 1st player");
socket.emit('turn',newPlayer.p_id);
}
});
socket.on('myName' , function(name){
console.log('new player with name: ' + name);
var newPlayerInfo = new PlayerInfo(socket.id,name,0);
PlayersInfo.push(newPlayerInfo);
io.sockets.emit('playersInfoObject',PlayersInfo);
});
socket.on('ask',function(number,p_id){
console.log("Cards length"+Cards.length);
var currPid = p_id;
console.log("Number:"+number+"+Pid :"+p_id);
var askedCards = findCards(number,p_id);
console.log("Asked Cards :"+askedCards.length);
if(askedCards.length > 0)
{
if(Cards.length!=0)
{
newPlayer.stackCount[indexOf(askedCards[0].rank)]+=askedCards.length;
for(var i=0;i<askedCards.length;i++)
{
newPlayer.p_cards.push(askedCards[i]);
}
if(newPlayer.stackCount[indexOf(askedCards[0].rank)] == 4)
{
newPlayer.p_cards = removeRank(newPlayer.p_cards,askedCards[0].rank);
newPlayer.stackCount[indexOf(askedCards[0].rank)] = 0;
newPlayer.numberOfStacks++;
var playerInfo = new PlayerInfo(newPlayer.p_id,newPlayer.p_name,newPlayer.numberOfStacks);
io.sockets.emit('updatePlayerStackCount',playerInfo);
}
var askedPlayer = removeCards(p_id,askedCards[0].rank);
askedPlayer.stackCount[indexOf(askedCards[0].rank)]-=askedCards.length;
removePlayer(askedPlayer);
console.log("Asked Player Removed:"+askedPlayer.p_id);
Players.push(askedPlayer);
if (io.sockets.connected[p_id]) {
io.sockets.connected[p_id].emit('sendCards',askedPlayer);
//console.log(askedPlayer.p_cards.length);
}
removePlayer(newPlayer);
// console.log("Player Removed:"+newPlayer.p_id);
Players.push(newPlayer);
// console.log("Player Pushed:"+newPlayer.p_id);
socket.emit("resultAsk",newPlayer,1);
console.log("Adding "+askedCards[0].rank);
displayStack(newPlayer.stackCount);
if(Cards.length==0)
gameEnd();
}
}
else// Go fish case
{
if(Cards.length != 0)
{
var card = pickRandom();
newPlayer.p_cards.push(card);
newPlayer.stackCount[indexOf(card.rank)]++;
if(newPlayer.stackCount[indexOf(card.rank)] == 4)
{
newPlayer.p_cards = removeRank(newPlayer.p_cards,card.rank);
newPlayer.stackCount[indexOf(card.rank)] = 0;
newPlayer.numberOfStacks++;
var playerInfo = new PlayerInfo(newPlayer.p_id,newPlayer.p_name,newPlayer.numberOfStacks);
io.sockets.emit('updatePlayerStackCount',playerInfo);
}
removePlayer(newPlayer);
// console.log("Player Removed:"+newPlayer.p_id);
Players.push(newPlayer);
// console.log("Player Pushed:"+newPlayer.p_id);
newPlayer.turn = false;
for(var i=0;i<Players.length;i++)
{
if(Players[i].p_id == newPlayer.p_id)
{
console.log("i previous::: " +i);
console.log("players length::: "+ Players.length);
i = (i+1)%Players.length;
console.log("i later::: "+i);
io.sockets.emit('turn',Players[i].p_id);
break;
}
}
socket.emit("resultAsk",newPlayer,0);
console.log("Adding Go Fish "+card.rank);
displayStack(newPlayer.stackCount);
if(Cards.length==0)
gameEnd();
}
}
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});