-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·79 lines (64 loc) · 1.97 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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var port = process.env.PORT || 2000;
var online = 0;
var roomList = [0];
var update = setInterval(function(){
for (var i = 0; i < roomList.length; i++){
for (var j = 0; j < roomList.length; j++){
io.sockets.in(i).emit("update", roomList[i][j]);
}
}
},10);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/assets/:Image', function (req, res){
res.sendFile(__dirname + '/assets/' + req.params.Image);
});
app.get('/raycasting.js', function (req, res){
res.sendFile(__dirname + '/raycasting.js');
});
app.get('/maps.js', function (req, res){
res.sendFile(__dirname + '/maps.js');
});
io.on('connection', function(socket){
socket.on('newconnection', function(name){
console.log('player has connected');
socket.pID = online;
socket.username = name;
socket.emit('setup', online, 0);
socket.join(0);
socket.room = 0;
online += 1;
roomList[0] += 1;
io.sockets.emit('updateOnline', online);
//console.log(roomList[0]);
});
socket.on('disconnect', function(){
console.log('player has disconnected');
online -= 1;
roomList[socket.room] -= 1;
io.sockets.emit('updateOnline', online);
io.sockets.in(socket.room).emit('disconnection', socket.pID);
//console.log(roomList[0]);
});
socket.on('fire', function(room, playerID, x, y, rot, d){
io.sockets.in(room).emit('newBullet', playerID, x, y, rot, d);
});
socket.on('move', function(room, id, name, hp, kills, activity, x, y, rot){
io.sockets.in(room).emit("update", id, name, hp, kills, activity, x, y, rot);
});
socket.on('kill', function(room, id, killerID){
console.log("player killed " + id);
io.sockets.in(room).emit("death", killerID);
});
socket.on('log', function(msg){
console.log(msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});