-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (100 loc) · 3.09 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
//step 1) require the modules we need
var
http = require('http'),
path = require('path'),
fs = require('fs'),
//these are the only file types we will support for now
extensions = {
".html" : "text/html",
".css" : "text/css",
".js" : "application/javascript",
".png" : "image/png",
".gif" : "image/gif",
".jpg" : "image/jpeg"
};
// comment
//helper function handles file verification
function getFile(filePath,res,page404,mimeType){
//does the requested file exist?
fs.exists(filePath,function(exists){
//if it does...
if(exists){
//read the fiule, run the anonymous function
fs.readFile(filePath,function(err,contents){
if(!err){
//if there was no error
//send the contents with the default 200/ok header
res.writeHead(200,{
"Content-type" : mimeType,
"Content-Length" : contents.length
});
res.end(contents);
} else {
//for our own troubleshooting
console.dir(err);
};
});
} else {
//if the requested file was not found
//serve-up our custom 404 page
fs.readFile(page404,function(err,contents){
//if there was no error
if(!err){
//send the contents with a 404/not found header
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(contents);
} else {
//for our own troubleshooting
console.dir(err);
};
});
};
});
};
//a helper function to handle HTTP requests
function requestHandler(req, res) {
var
fileName = path.basename(req.url) || 'index.html',
ext = path.extname(fileName),
localFolder = __dirname + '/public/',
page404 = localFolder + '404.html';
//do we support the requested file type?
if(!extensions[ext]){
//for now just send a 404 and a short message
res.writeHead(404, {'Content-Type': 'text/html'});
res.end("<html><head></head><body>The requested file type is not supported</body></html>");
};
//call our helper function
//pass in the path to the file we want,
//the response object, and the 404 page path
//in case the requestd file is not found
getFile((localFolder + fileName),res,page404,extensions[ext]);
};
var server = require('http').createServer(requestHandler);
var io = require('socket.io')(server);
var gameInfo = require('./gameInfo.js');
io.on('connection', function(socket){
io.emit('gameInfo', gameInfo.sendGame());
io.emit('enemies', gameInfo.getCurrentEnemies());
socket.on('attack', function(mouseX, mouseY){
gameInfo.attack(mouseX, mouseY);
});
socket.on('move', function(dir){
gameInfo.move(dir);
});
});
server.listen(3000);
fps30 = 1000 / 0.0333;
var player = gameInfo.player;
var enemies = gameInfo.enemies;
var arrows = gameInfo.arrows;
function loop()
{
io.emit('enemiesPos', enemies);
io.emit('arrows', arrows);
io.emit('playerPos', player);
io.emit('render', 'now');
gameInfo.update();
setTimeout(loop, 33,33);
}
loop();