-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
307 lines (256 loc) · 8.19 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
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
305
306
307
// Express server
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
path = require('path'),
io = require('socket.io').listen(server),
nano = require('nano')('http://pi:pi@localhost:8000'),
crypto = require('crypto'),
connect = require('connect'),
parseCookie = connect.utils.parseCookie,
MemoryStore = connect.middleware.session.MemoryStore,
store,
users = 0;
server.listen(3000);
console.log("Express server listening on port 3000");
// VisibleForTesting functions
module.exports.processUser = processUser;
// Allow access to /public folder
app.configure(function () {
app.use(express.cookieParser());
app.use(express.session({
key: 'OPENRPG_ID',
secret: 'ADRG$WHSRHRWUsdfj@~€7ghzdfhgksdjñ76857hkse',
store: store = new MemoryStore(),
cookie: {
path: '/',
domain: 'uplei.com',
maxAge: 1000 * 60 * 24 // 24 hours
}
}));
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(path.join(__dirname,'/public'), {maxAge: 0}));
});
/**
* Process a list of users retrieved from the DB.
*
* @param err - error message
* @param body - the response from the DB call
* @return - json object containing { title : ... , data: ... , error: ... }
*/
function processUser(err, body) {
var response = {};
response.data = [];
if(!err) {
response.title = "Users";
body.rows.forEach(function(doc){
response.data.push(doc);
});
} else {
response.title = "Error";
response.error = err;
}
return response;
}
// List all users
app.get('/api/list', function (req, res) {
var users = nano.use('users');
users.list(function(err, body){
var response = processUser(err, body);
res.send(response);
});
});
// CouchDB Access
app.post('/api/db', function (req, res) {
// Nano!
var nano = require('nano')('http://pi:pi@localhost:8000');
if(req.body == undefined){
req.body = {user : {type : 'Unsupported'}};
}
console.log("POST to /api/db: ", req.body.type);
//TODO Improve this section
switch(req.body.type){
case 'register':
var data = req.body.user,
users = nano.use('users');
// Sha1 of password
var sha1 = crypto.createHash('sha1');
sha1.update(data.pass);
// Insert in database
users.insert({_id: data.name.toLowerCase(), pass: sha1.digest('hex')}, function(err, body) {
if (err){
if(err.status_code == 409){
res.send("El usuario ya existe!!");
}else{
res.send("Ha ocurrido un error extraño con el servidor, intentalo mas tarde");
}
//res.end();
}else{
res.redirect('/');
}
});
break;
default:
res.send(JSON.stringify('Unsupported'));
}
});
// SocketIO
io.set('log level', 2); // 0 error, 1 warnings, 2 info, 3 for debug
// Client authorisation
io.set('authorization', function (data, accept) {
// Now use data.query to handle login
// Check if user exists
var users = nano.use('users');
// Sha1 password
var sha1 = crypto.createHash('sha1');
sha1.update(data.query.pass);
users.view('lists','user-pass', {key: [data.query.user.toLowerCase(), sha1.digest('hex')]}, function (error, view) {
if(error !== null){
console.error(error);
accept(null,false);
}else{
if(view.rows.length > 0){
data.user = data.query.user;
console.log("Login ok, User="+view.rows[0].id);
accept(null, true);
}else{
accept(null,false);
}
}
});
});
// ----------------------------------
// CHAT
io.sockets.on('connection', function(socket){
users++;
// Notify everyone
io.sockets.in('server').emit('message', {room: 'server', type: 'connect', user: socket.handshake.user});
socket.on('subscribe', function(room) {
socket.join(room);
});
socket.on('unsubscribe', function(room) {
socket.leave(room);
});
socket.on('send', function(data) {
if(data.room == 'chat'){
// Chat message
data.user = socket.handshake.user;
io.sockets.in(data.room).emit('message', data);
}else if(data.room == 'position'){
// User position
socket.broadcast.to('position').emit('update', { user: socket.handshake.user, position: data.position});
}
});
socket.on('ping', function(data) {
socket.emit('ping', data);
});
socket.on('map', function(data){
// Send the requested data
socket.emit('map', {
coordinates : data.coordinates,
size: data.size,
basesheets: basesheets(data.coordinates, data.size),
resources: getResources(data.coordinates, data.size)
});
});
socket.on('disconnect', function(){
io.sockets.in('server').emit('message', {room:'server', type: 'disconnect', user: socket.handshake.user});
users--;
});
});
// Socket intervals
var usersOnlineInterval = setInterval(function(){
io.sockets.in('server').emit('usersOnline', {count:users});
},1000);
// ----------------------------------------- //
/**
* Dynamic resource generation based on the coordinates.
* it will generate the same resources for each set of coordinates
* @param {Object} Central coordinates of the requested area {x,y}
* @param {size} Size of the requested area {w,h}
*/
function getResources(coordinates, size){
// Resource definition
var resources = [
{
type : 'tree',
probability: 0.1, // Chances of it appearing. Future work might make this dependant on the area
sizes: [60,120] // min, max
}
];
// Container for the coordinates in use. To ensure that there are no duplicates.
var usedCoordinates = [],
returnResources = [];
/*
* Now iterate over the coordinates and generate resources.
* The coordinates refer to the central point in the rectangle,
* so possible coordinates for elements will be {x+-width/2, y+-height/2}
* Instead of using a random number generator I will use a sine,
* because in JS Math.random doesn't have the option to set the seed.
* This should select pseudo randomly some coordinates for each resource.
*/
// Iterate over each resource
for(var a=0; a<resources.length; a++){
// Number of resources to place
var coordNum = Math.max(50,Math.floor(size.w*size.h*resources[a].probability/3000));
// Iterate over each coordinate
for(var b=0;b<coordNum;b++){
// Generate a possible set of x,y
var possibleCoordinates = {
x: coordinates.x + Math.floor(Math.sin((coordinates.x+b+1)*15000)*Math.cos((coordinates.y+b+10)*15000)*(size.w/2)),
y: coordinates.y + Math.floor(Math.cos((coordinates.y+b+20)*6000)*Math.sin((coordinates.x+b+5)*10000)*(size.h/2)),
z: 0,
};
// Check if they are available
if(usedCoordinates.indexOf(possibleCoordinates)>0) continue;
// They are, store and add to the return array
usedCoordinates.push(possibleCoordinates);
// Generate a pseudo random size
var resourceSize = Math.floor(resources[a].sizes[1] - (resources[a].sizes[1] - resources[a].sizes[0]) * Math.abs(Math.sin(possibleCoordinates.x * possibleCoordinates.y)));
// Store in return array
returnResources.push({
type: resources[a].type,
coordinates: possibleCoordinates,
properties: {
size: Math.min(resources[a].sizes[1], Math.max(resources[a].sizes[0], resourceSize))
}
});
}
}
return returnResources;
}
/**
* Dynamically generate the basesheet color. In the future this color
* might depend on the area type (ocean, land... )
* @param {Object} Central coordinates of the requested area {x,y}
* @param {size} Size of the requested area {w,h}
*/
function basesheets(coordinates, size){
// Basesheets are always 200x200px, so I just need to iterate
// over the centers of the requested basesheets and generate a pseudo random color
// First get the top left basesheet center
var first = {
x: coordinates.x - size.w/2,
y: coordinates.y - size.h/2 + 200,
z: 0
};
var basesheets = [];
for(var col=0; col < size.w/200; col++){
for(var row=0; row < size.h/200; row++){
// Generate a pseudo random color
var color = 'rgba(' + Math.ceil(70 + Math.abs(Math.sin(first.x + col*200))*20) + ', 120,' + Math.ceil(30 + Math.abs( Math.sin( first.y + col*200))*25)+', 1)';
// Store
basesheets.push({
centerp : {
x: first.x + col*200,
y: first.y + row*200,
z: 0
},
color : color
});
}
}
return basesheets;
}