-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
115 lines (94 loc) · 2.73 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
#!/usr/bin/env node
var http = require('http');
var io = require('socket.io');
var fs = require('fs');
var mysql = require('mysql');
var database = mysql.createConnection({
host:process.env.OPENSHIFT_MYSQL_DB_HOST || '127.0.0.1',
user:process.env.OPENSHIFT_MYSQL_DB_USER || 'root',
password:process.env.OPENSHIFT_MYSQL_DB_PASSWORD || '',
database:process.env.OPENSHIFT_MYSQL_DB_DB || 'dabase'
});
database.connect(function(err) {
if(err) console.log(err);
else console.log('Connected to the database.')
});
var server = http.createServer(function(req,res) {
var url = req.url == '/' ? 'index.html' : req.url;
if(url == '/game') {
url = 'game.html';
}
var filesplit = url.split(".");
filesplit = filesplit[filesplit.length-1];
var types = {
'js':'application/javascript',
'html':'text/html',
'png':'image/png',
'jpg':'image/jpeg',
'jpeg':'image/jpeg',
'css':'text/css',
'ico':'image/x-ico',
'text':'text/plain',
'gif':'image/gif'
};
if(url == '/db_dump') {
database.query("SELECT * FROM hacku",function(err,rows) {
res.writeHead(200);
res.end(JSON.stringify(rows));
});
} else if(url.match(/\/apis\/(.*)/gi)) {
res.writeHead(200);
var api = url.split("/apis/")[1];
http.get(api,function(response) {
var data = '';
response.on('data',function(chunk) {
data += chunk;
});
response.on('end',function() {
res.end(data);
});
});
} else {
fs.readFile(__dirname+'/'+url,function(err,data) {
if(err) {
res.writeHead(500);
return res.end('Error reading index file.');
}
res.writeHead(200,{'Content-type':(types[filesplit] || types['text'])});
res.end(data);
});
}
});
if(process.env.OPENSHIFT_NODEJS_PORT) {
server.listen(process.env.OPENSHIFT_NODEJS_PORT || 8888,process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
} else {
server.listen(8888);
}
var chars = [];
var charDir = {length:0};
var socket = io.listen(server,{log:false}).sockets.on('connection',function(client) {
client.broadcast.emit('playerData',chars);
client.emit('welcome',{status:'welcome',data:chars});
client.on('playerData',function(data) {
charDir[client.id] = data;
charDir[client.id].index = charDir.length;
charDir[client.id].pid = client.id;
charDir.length++;
console.log("added char... ");
console.log(charDir[client.id]);
chars.push(charDir[client.id]);
console.log("... "+chars.length+" total");
});
client.on('disconnect',function() {
// var id = client.id;
// var index = 0;
// console.log("disconnecting player...");
// for(var i=0;i<chars.length;i++) {
// if(chars[i].pid == id) index = i;
// }
// chars.splice(index,1);
// client.broadcast.emit('playerDisconnect',charDir[id]);
// console.log("char removed at index "+index);
// charDir.length--;
});
});