-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-compiled.js
324 lines (271 loc) · 9.92 KB
/
server-compiled.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Chris Samuel
*
* October 1, 2015
*
* FileName: server.js
*
* Description:
*
* File will be used for running node/express for our voting application.
* Chris Samuel
*
* October,
*
*
* */
//Express API Route Dependencies
'use strict';
var async = require('async');
var request = require('request');
var xml2js = require('xml2js');
//React Routes
var swig = require('swig');
var React = require('react');
var Router = require('react-router');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
//Add the mongoose module and Character.js file
var mongoose = require('mongoose');
var Character = require('./models/character');
var express = require('express');
var routes = require('./app/routes');
var config = require('./config');
//Underscore.js
var _ = require('underscore');
mongoose.connect(config.database);
mongoose.connection.on('error', function () {
console.info('Error: Could not connect to MongoDB. Did you forget to run `mongod`?');
});
var juice = express();
juice.set('port', process.env.PORT || 9876);
juice.use(logger('dev'));
juice.use(bodyParser.json());
juice.use(express['static'](path.join(__dirname, 'public')));
/**
* GET /api/characters
* Returns 2 random characters of the same gender that have not been voted yet.
*/
juice.get('/api/characters', function (req, res, next) {
var choices = ['Female', 'Male'];
var randomGender = _.sample(choices);
Character.find({ random: { $near: [Math.random(), 0] } }).where('voted', false).where('gender', randomGender).limit(2).exec(function (err, characters) {
if (err) return next(err);
if (characters.length === 2) {
return res.send(characters);
}
var oppositeGender = _.first(_.without(choices, randomGender));
Character.find({ random: { $near: [Math.random(), 0] } }).where('voted', false).where('gender', oppositeGender).limit(2).exec(function (err, characters) {
if (err) return next(err);
if (characters.length === 2) {
return res.send(characters);
}
Character.update({}, { $set: { voted: false } }, { multi: true }, function (err) {
if (err) return next(err);
res.send([]);
});
});
});
});
/*
* POST /api/characters
* Adds new characters to the database
*
* */
juice.post('/api/characters', function (req, res, next) {
var gender = req.body.gender;
var characterName = req.body.name; //get a Character ID from a Character Name
var characterIdLookupUrl = "https://api.eveonline.com/eve/CharacterID.xml.aspx?names=" + characterName;
// create a XML parser instance with xml2js
var parser = new xml2js.Parser();
//waterfall(tasks, [callback])
//use the name and the url to find if the name is in the database already
async.waterfall([function (callback) {
request.get(characterIdLookupUrl, function (err, request, xml) {
if (err) return next(err);
//parse the XML reponse
parser.parseString(xml, function (err, parsedXml) {
if (err) return next(err);
try {
var characterId = parsedXml.eveapi.result[0].row[0].$.characterID;
Character.findOne({ characterId: character }, function (err, character) {
if (err) return next(err);
if (character) {
return res.status(409).send({ message: character.name + 'is already in database' });
}
callback(err, characterId);
});
} catch (e) {
return res.status(400).send({ message: 'XML Parse Error' });
}
});
});
}, //pass the CharacterID to the next function in the async .waterfall stage
function (characterId) {
var characterInfoUrl = 'https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID=' + characterId;
//get basic character information from a character ID
request.get({ url: characterInfoUrl }, function (err, request, xml) {
if (err) return next(err);
parser.parseString(xml, function (err, parsedXml) {
if (err) return res.send(err);
try {
var name = parsedXml.eveapi.result[0].characterName[0];
var race = parsedXml.eveapi.result[0].race[0];
var bloodline = parsedXml.eveapi.result[0].bloodline[0];
var character = new Character({
characterId: characterId,
name: name,
race: race,
bloodline: bloodline,
gender: gender,
random: [Math.random(), 0]
}); //Add a new character to the database
character.save(function (err) {
if (err) return next(err);
res.send({ message: characterName + 'has been added successfully' });
});
} catch (e) {
res.status(404).send({ message: characterName + 'is not a registered citzen of Faces of Space' });
}
});
});
}]);
});
/**
* PUT /api/characters
* Update winning and losing count for both characters.
*/
juice.put('/api/characters', function (req, res, next) {
var winner = req.body.winner;
var loser = req.body.loser;
if (!winner || !loser) {
return res.status(400).send({ message: 'Voting requires two characters.' });
}
if (winner === loser) {
return res.status(400).send({ message: 'Cannot vote for and against the same character.' });
}
async.parallel([function (callback) {
Character.findOne({ characterId: winner }, function (err, winner) {
callback(err, winner);
});
}, function (callback) {
Character.findOne({ characterId: loser }, function (err, loser) {
callback(err, loser);
});
}], function (err, results) {
if (err) return next(err);
var winner = results[0];
var loser = results[1];
if (!winner || !loser) {
return res.status(404).send({ message: 'One of the characters no longer exists.' });
}
if (winner.voted || loser.voted) {
return res.status(200).end();
}
//We are using Async parallel to make two database queries
// simultaneously, since one query does not depend on another
// however, because we have two separate MongoDB documents,
// that's two independent asynchronous operations, hence
// another async.parallel. Basically, we respond with a success only when
// both characters have finished updating and there were no errors
async.parallel([function (callback) {
winner.wins++;
winner.voted = true;
winner.random = [Math.random(), 0];
winner.save(function (err) {
callback(err);
});
}, function (callback) {
loser.losses++;
loser.voted = true;
loser.random = [Math.random(), 0];
loser.save(function (err) {
callback(err);
});
}], function (err) {
if (err) return next(err);
res.status(200).end();
});
});
});
/**
* GET /api/characters/count
* Returns the total number of characters.
*/
juice.get('/api/characters/count', function (req, res, next) {
Character.count({}, function (err, count) {
if (err) return next(err);
res.send({ count: count });
});
});
/**
*
* GET /api/characters/top
* return top 50 Highest ranked characters. Filter by gender, race and bloodline.
*
*
*
*/
juice.get('/api/characters/top', function (req, res, next) {
var params = req.query;
var conditions = {};
_.each(params, function (value, key) {
conditions[key] = new RegExp('^' + value + '$', 'i');
});
Character.find(conditions).sort('-wins') // Sort in descending order (highest wins on top)
.limit(50).exec(function (err, characters) {
if (err) return next(err);
// Sort by winning percentage
characters.sort(function (a, b) {
if (a.wins / (a.wins + a.losses) < b.wins / (b.wins + b.losses)) {
return 1;
}
if (a.wins / (a.wins + a.losses) > b.wins / (b.wins + b.losses)) {
return -1;
}
return 0;
});
res.send(characters);
});
});
/**
* GET /api/characters/shame
* Returns 100 lowest ranked characters.
*/
juice.get('/api/characters/shame', function (req, res, next) {
Character.find().sort('-losses').limit(100).exec(function (err, characters) {
if (err) return next(err);
res.send(characters);
});
});
// Express middleware components
// WILL BE EXECUTED ON EVERY REQUEST TO THE SERVER.
juice.use(function (req, res) {
Router.run(routes, req.path, function (Handler) {
var html = React.renderToString(React.createElement(Handler));
var page = swig.renderFile('views/index.html', { html: html });
res.send(page);
});
});
/**
* Socket.io stuff.
*/
var server = require('http').createServer(juice);
var io = require('socket.io')(server);
var onlineUsers = 0;
io.sockets.on('connection', function (socket) {
onlineUsers++;
console.log(onlineUsers);
io.sockets.emit('onlineUsers', { onlineUsers: onlineUsers });
socket.on('disconnect', function () {
onlineUsers--;
io.sockets.emit('onlineUsers', { onlineUsers: onlineUsers });
});
});
server.listen(juice.get('port'), function () {
console.log('Express server listening on port ' + juice.get('port'));
});
//# sourceMappingURL=server-compiled.js.map