-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
executable file
·74 lines (59 loc) · 1.81 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
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server, { log: false });
var fs = require('fs');
server.listen('9000');
app.use("/css", express.static(__dirname + '/css'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/img", express.static(__dirname + '/img'));
//Main page of the server
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
//Player List
var players = {};
io.sockets.on('connection', function(socket){
//Detect socket when you connect
socket.on('connect', function(data){
//Save player on the Array
players[data.nickname] = new Player(data.nickname, data.y, data.x, socket.id);
//Send broadcast of connection
socket.broadcast.emit('user_connect', {'nickname' : data.nickname, 'y' : data.y, 'x' : data.x});
log('Connecting: ' + data.nickname);
})
//Detect player movement and update the player
socket.on('player_move', function (data){
if( players[data.nickname] ){
players[data.nickname].y = data.y;
players[data.nickname].x = data.x;
}else{
players[data.nickname] = new Player(data.nickname, data.y, data.x, socket.id);
}
});
//Detect the Disconnection
socket.on('disconnect', function(){
for (key in players){
if( players[key].socketId == socket.id){
log('Disconnecting: ' + key);
//Send broadcast of disconnection
socket.broadcast.emit('user_disconnect', {'nickname' : players[key].nickname});
delete players[key];
}
}
})
//Game Loop
setInterval(function(){
socket.emit('players_position', players);
}, 30);
});
//Player Class
function Player (nickname, y, x, socketId){
this.nickname = nickname;
this.y = y;
this.x = x;
this.socketId = socketId;
}
function log(logMessage){
console.log(logMessage);
}