-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (73 loc) · 1.96 KB
/
index.js
File metadata and controls
85 lines (73 loc) · 1.96 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
/* eslint-disable linebreak-style */
/* eslint-disable no-tabs */
/* eslint-disable indent */
/* eslint-disable no-constant-condition */
/* eslint-disable eqeqeq */
/* eslint-disable no-console */
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const path = require('path');
const port = process.env.PORT || 7777;
server.listen(port, () => {
console.log(`listening on *${port}`);
});
app.use(express.static(path.join(__dirname, 'views')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
function random(a, b, isRound = false) {
return isRound ? Math.round(Math.random() * b + a) : Math.random() * b + a;
}
const GameRecords = [];
io.on('connection', (socket) => {
socket.on('connected', () => {
console.log(`Web hien gio da co ${io.sockets.server.engine.clientsCount} nguoi vao web`);
});
socket.on('game record', (gameRecord) => {
GameRecords.push({
playerID: socket.id,
gameRecord,
});
if (GameRecords.length > 5) {
GameRecords.splice(0, 1);
}
});
socket.on('game req pvp', () => {
const alertConfig = {
title: 'Oops!',
text: 'Server cannot find your opponent!',
icon: 'error',
_onDestroy: '$(\'#menu_button\').fadeIn(200, () => {});$("#pvp").prop(\'disabled\', false);',
};
if (GameRecords.length === 0) {
socket.emit('alert', alertConfig);
return;
}
const danhdau = {
arr: [],
obj: {},
};
let i = 0;
do {
i = random(0, GameRecords.length - 1, true);
if (!danhdau.obj[i]) {
danhdau.arr.push(i);
}
danhdau.obj[i] = true;
if (GameRecords[i].playerID != socket.id) break;
if (danhdau.arr.length == GameRecords.length) {
socket.emit('alert', alertConfig);
return;
}
}
while (1);
socket.emit('game res pvp', {
gameRecord: GameRecords[i],
});
});
});
setInterval(() => {
io.emit('game online', io.sockets.server.engine.clientsCount);
}, 1000);