-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
368 lines (339 loc) · 10 KB
/
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
365
366
367
368
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var util = require('util');
const sleep = require("http");
/* Prevent Sleep in Heroku Server */
setInterval(function () {
sleep.get("http://yachoo.herokuapp.com");
}, 600000); // every 10 minutes
app.get('/',function(req, res){
res.sendFile(__dirname + '/client.html');
});
var count = 100;
var rooms = 30;
var djj = 0;
// [Ctrl + f] delete cheat
var isCheat = 0;
var visitors = new Array(rooms);
for (var i = 0; i <= rooms; i++) {
visitors[i] = 0;
}
var fakechk = new Array(rooms);
for (var i = 0; i < rooms; i++) {
fakechk[i] = 0;
}
var fakecount = 0;
var fakenum = 0;
var fakeusers = 4;
setInterval(function () {
if ((rooms * 2) - 20 > djj) {
if (fakeusers <= 2) {
fakeusers = 3;
} else if (fakeusers >= 7) {
fakeusers = 6;
} else {
fakeusers += Math.floor(Math.random() * 3) - 1;
}
while (fakecount > fakeusers) {
for (var i = 0; i < rooms; i++) {
if (fakechk[i] && Math.floor(Math.random() * fakecount) == 0) {
fakechk[i] = 0;
fakecount--;
visitors[i] = 0;
}
}
}
while (fakecount < fakeusers) {
fakenum = Math.floor(Math.random() * rooms);
if (fakechk[fakenum] == 0 && visitors[fakenum] == 0) {
fakechk[fakenum] = 1;
fakecount++;
visitors[fakenum] = 2;
}
}
io.emit('room list', rooms, visitors);
}
}, 180000); // every 3 minutes
io.on('connection', function(socket) {
var isJoin = 0;
var leaveRoll = 3;
var dices = [0, 0, 0, 0, 0];
var score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var tmp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var turnchk = 0;
var sunhoo;
var ipip = "YET";
djj++;
socket.on('set ip', function(tmpip){
ipip = tmpip;
});
socket.join(isJoin);
var name = "user" + count++;
console.log(`[${ipip}] ` + `(${djj})` + 'user connected: ', name);
io.to(socket.id).emit('change name',name);
io.to(socket.id).emit('room list', rooms, visitors);
io.to(isJoin).emit('receive message', `[server] join ${name}`);
socket.on('disconnect', function(){
djj--;
console.log(`[${ipip}] ` + `(${djj})` + 'user disconnected: ', name);
if (isJoin) {
visitors[isJoin - 1]--;
io.emit('room list', rooms, visitors);
io.to(isJoin).emit('receive message', `[room ${isJoin}] ${name}이(가) 게임을 떠났습니다. 게임이 종료됩니다.`);
io.to(isJoin).emit('disconnected room user client');
io.to(isJoin).emit('draw table', 0);
io.to(isJoin).emit('receive message', `!@#$exit show!@#$`);
}
});
socket.on('disconnected room user server', function(){
score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
tmp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
turnchk = 1;
sunhoo = 1;
});
// delete cheat
function chkVaildNum(num) {
for(var i = 0; i < 5; i++) {
if (1 > parseInt(num[i]) || 6 < parseInt(num[i])) {
return false;
}
}
return true;
}
socket.on('send message', function(name,text){
if (text.substring(0, 8) == "!diceset" || text.substring(0, 7) == "diceset"){
if (text.length == 13 && text.substring(0, 8) == "!diceset" && chkVaildNum(text.substring(8, 13))) {
console.log(`[${ipip}] ` + name + " use cheat " + text);
isCheat = 1;
for(var i = 0; i < 5; i++) {
dices[i] = parseInt(text[i + 8]);
}
}
} else if (text == "!show connect users") {
io.to(socket.id).emit('receive message', djj);
}
else {
if (text.trim()) {
text = text.replace(/</g,"[");
text = text.replace(/>/g,"]");
var msg = name + ' : ' + text;
console.log(`[${ipip}] ` + msg);
io.to(isJoin).emit('receive message', msg);
}
}
});
/* delete cheat
socket.on('send message', function(name,text){
var msg = name + ' : ' + text;
util.log(msg);
io.to(isJoin).emit('receive message', msg);
});
*/
socket.on('join room', function(roomNumber) {
if (isJoin) {
socket.emit('receive message', `[system] You already joined room ${isJoin}`);
}
else if (visitors[roomNumber - 1] < 2) {
visitors[roomNumber - 1]++;
isJoin = roomNumber;
console.log(`[${ipip}] ` + name + " join room" + roomNumber);
socket.leave(0);
socket.join(isJoin);
socket.emit('receive message', `[system] room ${isJoin}에 접속했습니다.`);
io.to(isJoin).emit('receive message', `[room ${isJoin}] ${name}이(가) 참가했습니다.`);
socket.emit('joined room', isJoin);
if (visitors[roomNumber - 1] == 1) {
socket.emit('receive message', `[room ${isJoin}] 다른 플레이어를 기다리는 중..`);
socket.emit('receive message', `[room ${isJoin}] 플레이어가 2명이 되면 자동으로 게임이 시작됩니다.`);
turnchk++;
sunhoo = 1;
}
else if (visitors[roomNumber - 1] == 2) {
io.to(isJoin).emit('draw table', 1);
io.to(isJoin).emit('test cli', socket.id, 0, name);
io.to(isJoin).emit('receive message', `[room ${isJoin}] 게임을 시작합니다.`);
io.to(isJoin).emit('receive message', "!@#$exit hidden!@#$");
sunhoo = 2;
}
}
else {
socket.emit('receive message', `[system] room ${roomNumber} is full`);
}
io.emit('room list', rooms, visitors);
});
socket.on('test serv', function(turn) {
leaveRoll = 3;
io.to(isJoin).emit('rolled dice', leaveRoll, score);
if (parseInt(turn / 2) < 13)
io.to(isJoin).emit('test cli', socket.id, turn, name);
else {
io.to(isJoin).emit('receive message', `[room ${isJoin}] !!!!!! game over !!!!!`);
io.to(isJoin).emit('receive message', "!@#$exit show!@#$");
io.to(isJoin).emit('game over');
}
});
socket.on('check score', function() {
io.to(isJoin).emit('receive message', `[room ${isJoin}] ${name}'s Total score : ${score[13]}`);
console.log(`[${ipip}] ` + `[room ${isJoin}] ${name}'s Total score : ${score[13]}`);
});
socket.on('append in room', function(target, content, option, target2, content2) {
io.to(isJoin).emit('append me', target, content, option, target2, content2);
});
socket.on('highlight in room', function(target, basic) {
io.to(isJoin).emit('highlight me', target, basic);
});
socket.on('css in room', function(elem, attr, value) {
io.to(isJoin).emit('css me', elem, attr, value);
});
function calcScore() {
// Aces ~ Sixes
for (var i = 0; i < 6; i++) {
tmp[i] = 0;
for (var j = 0; j < 5; j++) {
if (dices[j] == i + 1)
tmp[i] += i + 1;
}
}
//tmp[6]
// Choice
tmp[7] = 0;
for (var i = 0; i < 5; i++) {
tmp[7] += dices[i];
}
// 4 of a kind
tmp[8] = 0;
var fourcardchk = [0, 0, 0, 0, 0, 0];
for (var i = 0; i < 5; i++) {
fourcardchk[dices[i] - 1]++;
}
for (var i = 0; i < 6; i++) {
if (fourcardchk[i] >= 4) {
for (var j = 0; j < 5; j++) {
tmp[8] += dices[j];
}
}
}
// Full House
tmp[9] = 0;
var fullhousechk = [0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 5; i++) {
fullhousechk[dices[i] - 1]++;
}
for (var i = 0; i < 6; i++) {
if (fullhousechk[i] == 2)
fullhousechk[6] = 1;
if (fullhousechk[i] == 3)
fullhousechk[7] = 1;
}
if (fullhousechk[6] == 1 && fullhousechk[7] == 1) {
for (var i = 0; i < 5; i++)
tmp[9] += dices[i];
}
// S. Straight
tmp[10] = 0;
var SSchk = [0, 0, 0, 0, 0, 0];
for (var i = 0; i < 5; i++) {
SSchk[dices[i] - 1]++;
}
if ((SSchk[0] > 0 && SSchk[1] > 0 && SSchk[2] > 0 && SSchk[3] > 0) ||
(SSchk[1] > 0 && SSchk[2] > 0 && SSchk[3] > 0 && SSchk[4] > 0) ||
(SSchk[2] > 0 && SSchk[3] > 0 && SSchk[4] > 0 && SSchk[5] > 0))
tmp[10] = 15;
// L. Straight
tmp[11] = 0;
var LSchk = [0, 0, 0, 0, 0, 0];
for (var i = 0; i < 5; i++) {
LSchk[dices[i] - 1]++;
}
if ((SSchk[0] > 0 && SSchk[1] > 0 && SSchk[2] > 0 && SSchk[3] > 0 && SSchk[4] > 0) ||
(SSchk[1] > 0 && SSchk[2] > 0 && SSchk[3] > 0 && SSchk[4] > 0 && SSchk[5] > 0))
tmp[11] = 30;
// Yachoo
tmp[12] = 0;
if (dices[0] == dices[1] && dices[1] == dices[2] && dices[2] == dices[3] && dices[3] == dices[4])
tmp[12] = 50;
}
socket.on('roll dice', function(keepDice) {
// console.log(`[${ipip}] ` + leaveRoll, turnchk, keepDice);
if (turnchk % 2) {
leaveRoll--;
if (leaveRoll >= 0) {
if (!isCheat) {
if (leaveRoll == 2) {
for (var i = 0; i < 5; i++) {
dices[i] = Math.floor(Math.random() * 6) + 1;
}
}
else {
for (var i = 0; i < 5; i++) {
if (keepDice[i] == 0)
dices[i] = Math.floor(Math.random() * 6) + 1;
}
}
}
isCheat = 0;
calcScore();
io.to(isJoin).emit('rolled dice', leaveRoll);
// console.log(`[${ipip}] ` + tmp);
io.to(isJoin).emit('score update', tmp, sunhoo);
io.to(isJoin).emit('dice update', dices);
}
}
});
/* delete cheat
socket.on('roll dice', function(keepDice) {
// console.log(`[${ipip}] ` + leaveRoll, turnchk, keepDice);
if (turnchk % 2) {
leaveRoll--;
if (leaveRoll >= 0) {
if (leaveRoll == 2) {
for (var i = 0; i < 5; i++) {
dices[i] = Math.floor(Math.random() * 6) + 1;
}
}
else {
for (var i = 0; i < 5; i++) {
if (keepDice[i] == 0)
dices[i] = Math.floor(Math.random() * 6) + 1;
}
}
calcScore();
io.to(isJoin).emit('rolled dice', leaveRoll);
// console.log(`[${ipip}] ` + tmp);
io.to(isJoin).emit('score update', tmp, sunhoo);
io.to(isJoin).emit('dice update', dices);
}
}
});
*/
socket.on('pick score', function(index, id) {
score[index - 1] = tmp[index - 1];
io.to(isJoin).emit('append me', `#${id}`, `<b>${score[index - 1]}</b>`);
score[6] = 0;
for (var i = 0; i < 6; i++) {
score[6] += score[i];
}
score[13] = score[6];
for(var i = 7; i < 13; i++) {
score[13] += score[i];
}
if (score[6] >= 63) {
score[13] += 35;
}
io.to(isJoin).emit('append me', `#bonus-${sunhoo}`, `<b>${score[6]}/63</b>`);
io.to(isJoin).emit('append me', `#total-${sunhoo}`, `<b>${score[13]}</b>`);
tmp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
});
socket.on('client to room client', function() {
io.to(isJoin).emit('server to room client');
});
socket.on('turn over', function() {
turnchk++;
});
});
const { PORT=3000, LOCAL_ADDRESS='0.0.0.0' } = process.env
http.listen(PORT, LOCAL_ADDRESS, () => {
const address = http.address();
});