-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatic_Server.js
304 lines (251 loc) · 12.9 KB
/
Static_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//CODE WRITTEN BY IBRAHIM HELMY AND ALEX PATEL
// These are like import statements in java (equal to var in order to import)
var http = require('http'); // http library: module related to servers
var url = require('url'); // URL library: for parces the URL
var fs = require('fs'); // fs Library: that Opens and reads files
var io = require("socket.io")();
var ROOT_DIR = 'data';// addresses to the folders where the data is contained
var canvasWidth = 1600;
var canvasHeight = 800;
var person = [];
person.push({id: "", name: "Unused", x: canvasWidth/2 - 100, y: 70, sX: canvasWidth/2 - 100, sY: 70, size: 20, speed: 5, color: "red"});
person.push({id: "", name: "Unused", x: canvasWidth/2, y: 70, sX: canvasWidth/2, sY: 70, size: 20, speed: 5, color: "green"});
person.push({id: "", name: "Unused", x: canvasWidth/2 + 100, y: 70, sX: canvasWidth/2 + 100, sY: 70, size: 20, speed: 5, color: "blue"});
/*person.push({id: "", name: "Unused", x: canvasWidth/4, y: canvasHeight/4, sX: canvasWidth/2 - 100, sY: 100, size: 20, speed: 5, color: "red"});
person.push({id: "", name: "Unused", x: canvasWidth - canvasWidth/4, y: canvasHeight/4, sX: canvasWidth/2, sY: 100, size: 20, speed: 5, color: "green"});
person.push({id: "", name: "Unused", x: canvasWidth - canvasWidth/4, y: canvasHeight - canvasHeight/4, sX: canvasWidth/2 + 100, sY: 100, size: 20, speed: 5, color: "blue"});*/
var bullet = [];
var blocks = [];
blocks.push({x: canvasWidth/2 - 50, y: 200, width: 100, height: canvasHeight - 400});
blocks.push({x: 200, y: canvasHeight/2 - 50, width: canvasWidth - 400, height: 100});
blocks.push({x: canvasWidth/2 - 400, y: 0, width: 800, height: 100});
blocks.push({x: 100, y: canvasHeight - 200, width: 250, height: 100});
blocks.push({x: canvasWidth/2 + 200, y: canvasHeight/2 + 150, width: 50, height: 225});
blocks.push({x: canvasWidth - 200, y: 150, width: 50, height: 50});
blocks.push({x: canvasWidth - 200, y: canvasHeight - 200, width: 50, height: 50});
//blocks.push({x: canvasWidth - 400, y: canvasHeight - 100, width: 50, height: 50});
//blocks.push({x: canvasWidth - 200, y: canvasHeight - 100, width: 50, height: 50});
var EXT_TYPES = { // small array containing the many extension types we use.
'css': 'text/css',
'gif': 'image/gif',
'htm': 'text/html',
'html': 'text/html',
'ico': 'image/x-icon',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'js': 'application/javascript',
'json': 'application/json',
'png': 'image/png',
'txt': 'text/text'
}
var get_ext = function(filename){ // gets the extensions from above
for (var ext in EXT_TYPES){ // For every extension in the array
var type = EXT_TYPES[ext]; // it sets the return type to the type specified to the extension at that spesific index
if (filename.indexOf(ext, filename.length - ext.length) !== -1) return type; // if it finds the extension in the filename it will return that extension
} // find ext in, filename - extention and check to see if the extension of the file matches the extension type of the index in the array
return EXT_TYPE['txt']; // returns txt if it did not find it
}
var server = http.createServer(function(request, response){ // Craetes the server
var path = url.parse(request.url).pathname; // it gives it useful properties such as:
console.log('Path: ' + path); // The pathname of the file the user is looking for
console.log('Method: ' + request.method); // Which method the request came from (get, post, etc.)
if(request.method == 'GET'){ // If the request type was of type GET
fs.readFile(ROOT_DIR + path, function(err, data){ // it reads the file if found
if (err){ // otherwise an error is produced
console.log('Get Error: ' + err);
response.writeHead(404);
response.end(JSON.stringify(err));
} else {
response.writeHead(200, {'Content-Type': get_ext(path)});
response.end(data);
}
});
}
}); // listens for user requests at port 3000
var port = Number(process.env.PORT || 3000);
server.listen(port);
//io.serveClient(true);
io.attach(server);
io.on("connection", function(socket){
console.log("User " + socket.id + " connected");
socket.emit("syncData", JSON.stringify({arr: person, arrTwo: bullet, arrThree: blocks}));
socket.on("requestControl", function(data){
var dataObj = JSON.parse(data);
console.log("User " + socket.id + ", requestControl: " + dataObj.controlName);
for (var i in person){
if (dataObj.controlName == person[i].color){
if (person[i].id == ""){
for (var j in person){
if (socket.id == person[j].id){
person[j].id = "";
person[j].name = "Unused";
person[j].x = person[j].sX;
person[j].y = person[j].sY;
}
}
person[i].id = socket.id;
person[i].name = dataObj.name;
person[i].x = canvasWidth/4;
person[i].y = canvasHeight/4;
}
}
}
/*if (dataObj.controlName == "red"){
if (person[0].id == ""){
for (var i in person){
if (socket.id == person[i].id){
person[i].id = "";
person[i].name = "Unused";
}
}
person[0].id = socket.id;
person[0].name = dataObj.name;
}
} else if (dataObj.controlName == "green"){
if (person[1].id == ""){
for (var i in person){
if (socket.id == person[i].id){
person[i].id = "";
person[i].name = "Unused";
}
}
person[1].id = socket.id;
person[1].name = dataObj.name;
}
} else if (dataObj.controlName == "blue"){
if (person[2].id == ""){
for (var i in person){
if (socket.id == person[i].id){
person[i].id = "";
person[i].name = "Unused";
}
}
person[2].id = socket.id;
person[2].name = dataObj.name;
}
}*/
io.emit("updateControl", JSON.stringify({arr: person}));
});
socket.on("requestPositionUpdate", function(data){
var dataObj = JSON.parse(data);
for (var i in person){
if (socket.id == person[i].id){
if (dataObj.up){
if (person[i].y - person[i].speed - person[i].size > 0){
person[i].y -= person[i].speed;
}
}
if (dataObj.down){
if (person[i].y + person[i].speed + person[i].size < canvasHeight){
person[i].y += person[i].speed;
}
}
if (dataObj.left){
if (person[i].x - person[i].speed - person[i].size > 0){
person[i].x -= person[i].speed;
}
}
if (dataObj.right){
if (person[i].x + person[i].speed + person[i].size < canvasWidth){
person[i].x += person[i].speed;
}
}
for (var j in blocks){
if ((person[i].x - person[i].size > blocks[j].x && person[i].x - person[i].size < blocks[j].x + blocks[j].width && person[i].y - person[i].size > blocks[j].y && person[i].y - person[i].size < blocks[j].y + blocks[j].height) || (person[i].x + person[i].size > blocks[j].x && person[i].x + person[i].size < blocks[j].x + blocks[j].width && person[i].y - person[i].size > blocks[j].y && person[i].y - person[i].size < blocks[j].y + blocks[j].height) || (person[i].x - person[i].size > blocks[j].x && person[i].x - person[i].size < blocks[j].x + blocks[j].width && person[i].y + person[i].size > blocks[j].y && person[i].y + person[i].size < blocks[j].y + blocks[j].height) || (person[i].x + person[i].size > blocks[j].x && person[i].x + person[i].size < blocks[j].x + blocks[j].width && person[i].y + person[i].size > blocks[j].y && person[i].y + person[i].size < blocks[j].y + blocks[j].height)){
if (dataObj.up){
person[i].y += person[i].speed;
}
if (dataObj.down){
person[i].y -= person[i].speed;
}
if (dataObj.left){
person[i].x += person[i].speed;
}
if (dataObj.right){
person[i].x -= person[i].speed;
}
}
}
io.emit("updatePosition", JSON.stringify({arr: person}));
}
}
});
socket.on("requestBulletPositionUpdate", function(data){
var dataObj = JSON.parse(data);
bullet.push(dataObj);
});
socket.on("disconnect", function(){
for (var i in person){
if (socket.id == person[i].id){
person[i].id = "";
person[i].name = "Unused";
person[i].x = person[i].sX;
person[i].y = person[i].sY;
}
}
io.emit("updateControl", JSON.stringify({arr: person}));
console.log("User " + socket.id + " disconnected");
});
});
lastTime = new Date().getTime();
msPerCalc = 1000 / 60;
calcs = 0;
frames = 0;
lastTimer = new Date().getTime();
delta = 0;
setInterval(function (){
now = new Date().getTime();
delta += (now - lastTime) / msPerCalc;
lastTime = now;
while (delta >= 1)
{
calcs++;
//Program's Logic
update();
delta -= 1;
}
if ((new Date().getTime() - lastTimer) > 1000)
{
lastTimer += 1000;
//console.log("FPS: " + calcs);
frames = 0;
calcs = 0;
}
}, 2);
function update(){
for (var i in bullet){
bullet[i].x += bullet[i].speedX;
bullet[i].y += bullet[i].speedY;
if (bullet[i].x - bullet[i].size < 0 || bullet[i].x + bullet[i].size > canvasWidth){
bullet[i].speedX = -(bullet[i].speedX);
}
if (bullet[i].y - bullet[i].size < 0 || bullet[i].y + bullet[i].size > canvasHeight){
bullet[i].speedY = -(bullet[i].speedY);
}
for (var j in blocks){
if ((bullet[i].x - bullet[i].size > blocks[j].x && bullet[i].x - bullet[i].size < blocks[j].x + blocks[j].width && bullet[i].y - bullet[i].size > blocks[j].y && bullet[i].y - bullet[i].size < blocks[j].y + blocks[j].height) || (bullet[i].x + bullet[i].size > blocks[j].x && bullet[i].x + bullet[i].size < blocks[j].x + blocks[j].width && bullet[i].y - bullet[i].size > blocks[j].y && bullet[i].y - bullet[i].size < blocks[j].y + blocks[j].height) || (bullet[i].x - bullet[i].size > blocks[j].x && bullet[i].x - bullet[i].size < blocks[j].x + blocks[j].width && bullet[i].y + bullet[i].size > blocks[j].y && bullet[i].y + bullet[i].size < blocks[j].y + blocks[j].height) || (bullet[i].x + bullet[i].size > blocks[j].x && bullet[i].x + bullet[i].size < blocks[j].x + blocks[j].width && bullet[i].y + bullet[i].size > blocks[j].y && bullet[i].y + bullet[i].size < blocks[j].y + blocks[j].height)){
if (Math.abs(bullet[i].x - blocks[j].x) <= Math.abs(bullet[i].speedX) + bullet[i].size || Math.abs(bullet[i].x - (blocks[j].x + blocks[j].width)) <= Math.abs(bullet[i].speedX) + bullet[i].size){
bullet[i].speedX = -(bullet[i].speedX);
}
if (Math.abs(bullet[i].y - blocks[j].y) <= Math.abs(bullet[i].speedY) + bullet[i].size || Math.abs(bullet[i].y - (blocks[j].y + blocks[j].height)) <= Math.abs(bullet[i].speedY) + bullet[i].size){
bullet[i].speedY = -(bullet[i].speedY);
}
}
}
}
for (var i in person){
for (var j in bullet){
if (Math.sqrt(Math.pow(bullet[j].x - person[i].x, 2) + Math.pow(bullet[j].y - person[i].y, 2)) < person[i].size + bullet[j].size){
console.log("Person Hit");
bullet.splice(j, 1);
person[i].id = "";
person[i].name = "Unused";
person[i].x = person[i].sX;
person[i].y = person[i].sY;
io.emit("updateControl", JSON.stringify({arr: person}));
}
}
}
io.emit("updateBulletPosition", JSON.stringify({arr: bullet}));
}
console.log('Sever has been created at Port 3000. Press CRL-C to exit.');