-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (75 loc) · 2.21 KB
/
app.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
require('./Database');
require('./Entity');
require('./client/Inventory');
var express = require('express');
var app = express();
var serv = require('http').Server(app);
app.get('/',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(process.env.PORT || 2000);
console.log("Server started.");
var SOCKET_LIST = {};
var DEBUG = true;
var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
socket.id = Math.random();
SOCKET_LIST[socket.id] = socket;
socket.on('signIn',function(data){ //{username,password}
Database.isValidPassword(data,function(res){
if(!res)
return socket.emit('signInResponse',{success:false});
Database.getPlayerProgress(data.username,function(progress){
Player.onConnect(socket,data.username,progress);
socket.emit('signInResponse',{success:true});
})
});
});
socket.on('signUp',function(data){
Database.isUsernameTaken(data,function(res){
if(res){
socket.emit('signUpResponse',{success:false});
} else {
Database.addUser(data,function(){
socket.emit('signUpResponse',{success:true});
});
}
});
});
socket.on('disconnect',function(){
delete SOCKET_LIST[socket.id];
Player.onDisconnect(socket);
});
socket.on('evalServer',function(data){
if(!DEBUG)
return;
var res = eval(data);
socket.emit('evalAnswer',res);
});
});
setInterval(function(){
var packs = Entity.getFrameUpdateData();
for(var i in SOCKET_LIST){
var socket = SOCKET_LIST[i];
socket.emit('init',packs.initPack);
socket.emit('update',packs.updatePack);
socket.emit('remove',packs.removePack);
}
},1000/25);
/*
var profiler = require('v8-profiler');
var fs = require('fs');
var startProfiling = function(duration){
profiler.startProfiling('1', true);
setTimeout(function(){
var profile1 = profiler.stopProfiling('1');
profile1.export(function(error, result) {
fs.writeFile('./profile.cpuprofile', result);
profile1.delete();
console.log("Profile saved.");
});
},duration);
}
startProfiling(10000);
*/