forked from PPreau/roleplayit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
171 lines (132 loc) · 4.54 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
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
var express = require('express.io'),
app = require('express.io')(),
fs = require('fs'),
os = require('os'),
crypto = require('crypto'),
_ = require('underscore')
Actors = require('./server/actor');
Dice = require('./server/dice');
Users = require('./server/users');
Maps = require('./server/map');
Tilesets = require('./server/tilesets'),
Bodysets = require('./server/bodysets');
app.http().io();
// Setup your sessions, just like normal.
app.use(express.cookieParser())
app.use(express.session({secret: 'rocket propelled grenade', key: 'express.sid', cookie : {maxAge : 60 * 60 * 1000}}))
app.use(express.bodyParser());
///////////////////////////////////////////////////////////////////////////////
// Variables
store = {};
config = {
port: 7076
};
//console.log('[RoleplayIt alpha]');
///////////////////////////////////////////////////////////////////////////////
// Helpers
Array.prototype.findKey = function(key, value) {
return this.filter(function(element){ return element[key] == value;});
}
///////////////////////////////////////////////////////////////////////////////
// Configuration
/// Serialize app state
app.save = function() {
}
// Deserialize app state
app.load = function() {
Users.deserialize();
Tilesets.deserialize();
Bodysets.deserialize();
Maps.deserialize();
Actors.deserialize();
store.users = Users.getUsers();
store.actors = Actors.getActors();
store.maps = Maps.getMaps();
}
function initialize() {
// load data
app.load();
// app state
require('./server/eventhandler')(app.io);
// what else
}
///////////////////////////////////////////////////////////////////////////////
// Initialize
initialize();
//app.save();
app.get('/', function(req, res, next){
if(req.session.username) {
//res.send('welcome ' + req.session.username + ' Click to <a href="/forget">forget</a>!.');
//res.sendfile(__dirname + '/client.html');
if (req.session.accessLevel == 1)
res.sendfile(__dirname + '/client/client-dm.html');
else
res.sendfile(__dirname + '/client/client-player.html');
// TODO check accesslevel to show proper page
} else {
res.send('<form method="post">'
+ '<p>Username : <label><input type="text" name="username"/></label>'
+ '<p>Password : <label><input type="password" name="password"/></label>'
//+ '<p><label><input type="checkbox" name="remember"/> remember me</label></p> '
+ '<p><input type="submit" value="Submit"/></p></form>');
}
});
// Handle login
app.post('/', function(req, res){
var username = req.body.username;
var md5pwd = crypto.createHash('md5').update(req.body.password).digest("hex")
if (store.users[username] && store.users[username].password == md5pwd) {
req.session.username = username;
req.session.accessLevel = store.users[username].access_level;
console.log('Login: '+ req.ip +": Valid credentials for '" + username + "'");
}
else {
console.log('Login: '+ req.ip +": Access denied for '" + username + "'");
}
res.redirect('back');
});
app.get('/logout', function(req, res, next){
req.session.destroy();
res.redirect('back');
});
app.get('/style.css', function(req, res, next){
res.sendfile(__dirname + '/client/css/style.css');
})
app.use('/public', express.static(__dirname + "/public"));
app.use('/js', express.static(__dirname + "/client/js"));
app.use('/gfx', express.static(__dirname + "/client/gfx", { maxAge: 3600 }));
app.use('/images', express.static(__dirname + "/client/images", { maxAge: 3600 }));
app.use('/css', express.static(__dirname + "/client/css"));
app.listen(config.port);
function writeAddresses() {
var interfaces = os.networkInterfaces();
for (k in interfaces) {
for (k2 in interfaces[k]) {
var address = interfaces[k][k2];
//if (address.family == 'IPv4' && !address.internal) {
if (address.family == 'IPv4') {
console.log('Listening: ' + address.address + ':' + config.port)
}
}
}
}
writeAddresses();
// Shows incoming and outcoming connections
var onlineUsers = 0
app.io.sockets.on('connection', function(req) {
onlineUsers++;
var address = req.handshake.address;
console.log('Client: ' + address.address + ': Connected. [' + onlineUsers + ' Online]');
req.on('disconnect', function(req){
onlineUsers--;
console.log('Client: ' + address.address + ': Disconnected. [' + onlineUsers + ' Online]');
})
})
//actors.create();actors.create();actors.create();
//console.dir(actors.getActors());
var repl = require("repl");
repl.start({
prompt: "> ",
input: process.stdin,
output: process.stdout
});