forked from yyf0101/BnBonline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (97 loc) · 3.29 KB
/
app.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
var express = require('express'),
bodyParser = require('body-parser');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var swig = require('swig');
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/templates');
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
var rooms = {};
app.get('/', function (req, res) {
res.render('index');
});
io.on('connection', function (socket) {
var clientIp = socket.request.connection.remoteAddress;
console.log('New connection from ' + clientIp);
socket.on('joinRoom', function (roomname) {
var room = rooms[roomname];
if (!room) {
socket.emit('joinRoom', {ret: 0, err: 'no such room'});
} else {
socket.roomname = roomname;
socket.role = 'challenger';
room.challenger = socket;
var RandomSeed = Math.random();
room.master.emit("start", {role: "master", seed: RandomSeed});
room.challenger.emit("start", {role: "challenger", seed: RandomSeed} )
}
});
socket.on('getRooms', function(data) {
var msg = {'ret': 1, 'data': Object.keys(rooms)};
socket.emit('getRooms', msg);
});
socket.on('newRoom', function(data) {
var roomname = data['name'];
var msg;
if (roomname in rooms) {
msg = {'ret': 0, 'err': 'room already existed'}
} else {
rooms[roomname] = {master: socket, challenger: null, winner: null};
msg = {'ret': 1};
socket.roomname = roomname;
socket.role = 'master';
}
socket.emit('newRooms', msg);
});
socket.on('KeyUp', function (data) {
var room = rooms[socket.roomname];
if(room){
if (socket.role === 'master') {
room.challenger.emit("KU", data);
} else {
room.master.emit("KU", data);
}
}
});
socket.on('KeyDown', function (data) {
var room = rooms[socket.roomname];
if (room) {
if (socket.role === 'master') {
room.challenger.emit("KD", data);
} else {
room.master.emit("KD", data);
}
}
});
socket.on('end', function (data) {
var room = rooms[socket.roomname];
var winner = data;
if (room.winner == null ) {
room.winner = winner;
} else if (room.winner != winner) {
socket.emit('end', {ret: 0, err: "result don't match"})
} else {
room.master.emit('end', {ret: 1, data: winner});
room.challenger.emit('end', {ret: 1, data: winner});
delete rooms[socket.roomname];
}
});
socket.on('disconnect', function(){
var room = rooms[socket.roomname];
if (room) {
var other = (room.challenger == socket)?room.master:room.challenger;
other.emit('err', "Other Player Disconnected!");
}
})
});
server.listen(4000, function(){
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});