-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
202 lines (177 loc) · 4.9 KB
/
index.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
const gameloop = require('node-gameloop');
const generateName = require('sillyname');
const shuffleArray = require("knuth-shuffle").knuthShuffle;
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
global.io = require('socket.io')(http);
var Room = require('./room.js');
app.use(express.static(path.join(__dirname, 'public')));
var rooms = {};
global.MIN_PLAYERS = 3;
global.MAX_PLAYERS = 6;
global.GAME_COUNTDOWN = 30;
global.FIRST_TURN_COUNTDOWN = 5;
global.TURN_DURATION = 30;
global.ROOM_NAME_LENGTH = 5;
global.START_CARDS_PER_PLAYER = 3;
global.MAX_CARDS_PER_PLAYER = 4;
global.PILE_SIZE = 7;
global.MAX_ROOMS = 200;
io.on('connection', function (socket) {
console.log('client with id ' + socket.id + ' connected from ' + socket.conn.remoteAddress);
socket.on('requestRoom', function (data) {
if(socket.room) {
return;
}
console.log(socket.conn.remoteAddress + " requests room '" + data.roomId + "' and player name '" + data.playerName + "'");
if (!data.playerName) {
data.playerName = generateName().split(" ")[0];
}
var room = findPlayableRoom(data.roomId);
room.joinPlayer(socket, data.playerName);
});
socket.on("drawCard", function() {
if(socket.room && socket.room.isTurnHolder(socket)) {
socket.room.drawCard();
}
});
socket.on("placeCard", function(data) {
if(socket.room && socket.room.isTurnHolder(socket) && data.card && data.pileHolderId) {
socket.room.placeCard(data.card, data.pileHolderId);
}
})
socket.on("endTurn", function() {
if(socket.room && socket.room.isTurnHolder(socket)) {
socket.room.endTurn();
}
})
socket.on('readyForStart', function () {
if (socket.room) {
socket.room.playerReadyForStart(socket);
}
});
socket.on('disconnect', function () {
console.log("client with id " + socket.id + " disconnected");
if (socket.room) {
socket.room.leavePlayer(socket);
}
});
});
function resetRoom(roomId) {
var room = rooms[roomId];
rooms[roomId] = new Room(roomId);
if (room) {
room.emit("endGame", room.rankList());
}
delete room;
}
function findPlayableRoom(roomId) {
if (roomId) {
roomId = roomId.substring(0, ROOM_NAME_LENGTH);
var room = rooms[roomId];
if (room && room.isJoinable) {
return room;
} else {
var room = new Room();
return new Room();
}
} else {
var roomId, bestRoom;
for (roomId in rooms) {
var room = rooms[roomId];
if(room.playing) {
continue;
}
if (room.amountSockets().length == MAX_PLAYERS - 1) {
return room;
}
if (!bestRoom || (room.amountPlayers().length > bestRoom.amountPlayers() && room.players().length < MAX_PLAYERS)) {
bestRoom = room;
}
}
return bestRoom || createNewRoom();
}
}
function createNewRoom(roomId) {
if(amountRooms() >= MAX_ROOMS) {
return;
}
if (!roomId) {
do {
roomId = (Math.random().toString(36) + '00000000000000000').slice(2, ROOM_NAME_LENGTH + 2);
} while (rooms[roomId]);
}
var room = new Room(roomId);
rooms[roomId] = room;
return room;
}
global.amountRooms = function() {
var i = 0, roomId;
for(roomId in rooms) {
i++;
}
return i;
}
global.randomDeck = function() {
var cards = [];
var i;
for (i = 0; i < 54; i++) {
cards[i] = i;
}
shuffleArray(cards);
return cards;
}
global.cardsMatch = function(card1, card2) {
if (card1 > 53 || card2 > 53) {
return false;
}
var card1mod = card1 % 13;
var card2mod = card2 % 13;
// cards of the same symbol or same number or
return card1mod === card2mod || Math.floor(card1 / 4) === Math.floor(card2 / 4) || card1 >= 52 || card2 >= 52;
}
global.isCardBlack = function(card) {
var type = Math.floor(card / 4);
return type == 0 || type == 3
}
const loop = gameloop.setGameLoop(function (delta) {
var roomId;
for (roomId in rooms) {
var room = rooms[roomId];
if (!room.playing) {
if (room.amountSockets() > MIN_PLAYERS && --room.count <= 0) {
room.startGame();
}
} else if (--room.count <= 0) {
room.nextTurn();
}
}
}, 1000);
http.listen(3000, function () {
console.log("listening on *:3000");
});
/*
Card order:
clubs A
diamonds A
hearts A
spades A
clubs 2
diamonds 2
hearts 2
spades 2
clubs 3
diamonds 3
hearts 3
spades 3
...
clubs jack
diamonds jack
hearts jack
spades jack
...
black joker
red joker
*/