-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·140 lines (123 loc) · 3.66 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
var blobs = [];
var bullets = [];
const RANGE = 20;
function Blob(id, x, y, r, kills) {
this.id = id;
this.x = x;
this.y = y;
this.r = r;
this.kills = kills
}
function Bullet(id, x, y, c, velx, vely, d, parent, travelled) {
this.id = id;
this.x = x;
this.y = y;
this.c = c;
this.velx = velx;
this.vely = vely;
this.d = d;
this.parent = parent;
this.travelled = travelled;
}
// Using express: http://expressjs.com/
var express = require('express');
// Create the app
var app = express();
// Set up the server
// process.env.PORT is related to deploying on heroku
var server = app.listen(process.env.PORT || 3000, listen);
// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
app.use(express.static('public'));
// WebSocket Portionbullet
// WebSockets work with the HTTP server
var io = require('socket.io')(server);
setInterval(heartbeat, 33);
setInterval(bulletHeartbeat, 33);
function heartbeat() {
io.sockets.emit('heartbeat', blobs);
}
function bulletHeartbeat() {
io.sockets.emit('bulletheartbeat', bullets);
}
// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
io.sockets.on(
'connection',
// We are given a websocket object in our function
function(socket) {
console.log('We have a new client: ' + socket.id);
socket.on('start', function(data) {
console.log(socket.id + ' ' + data.x + ' ' + data.y + ' ' + data.r);
var blob = new Blob(socket.id, data.x, data.y, data.r, data.kills);
blobs.push(blob);
});
socket.on('addbullets', function(data) {
var bullet = new Bullet(data.id, data.x, data.y, data.c, data.velx, data.vely, data.d, data.parent, data.travelled);
bullets.push(bullet);
});
socket.on('updatebullets', function(data) {
var bullet;
// console.log(bullets);
for (var i = 0; i < bullets.length; i++) {
// console.log(bullets[i].travelled);
if (data.id == bullets[i].id) {
bullet = bullets[i];
bullet.id = data.id
bullet.x = data.x;
bullet.y = data.y;
bullet.travelled = data.travelled;
if(bullets[i].travelled >= RANGE) {
// console.log("bullet died");
bullets.splice(i,1);
}
break;
}
}
// var bullet = new Bullet(data.id, data.x, data.y, data.c, data.velx, data.vely, data.d);
// bullets.push(bullet);
// console.log(bullets);
});
//
// socket.on('DEAD-Bullet', function(data) {
// bullets.pop(data);
// });
//
socket.on('DEAD-Blob', function(data) {
// console.log("blob dead")
for (var i = 0; i < blobs.length; i++) {
if (blobs[i].id == data.blob) {
blobs.pop(blobs[i]);
} if (blobs[i].id == data.bullet) {
blobs[i].kills++;
}
}
});
socket.on('update', function(data) {
//console.log(socket.id + " " + data.x + " " + data.y + " " + data.r);
var blob;
for (var i = 0; i < blobs.length; i++) {
if (socket.id == blobs[i].id) {
blob = blobs[i];
blob.x = data.x;
blob.y = data.y;
blob.r = data.r;
blobs.kills = data.kills;
}
}
});
socket.on('disconnect', function() {
console.log('Client has disconnected');
for (var i = blobs.length-1; i >= 0; i--) {
if (blobs[i].id == socket.id) {
console.log(blobs[i].id);
blobs.splice(i, 1);
}
}
});
}
);