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
|
/*
* kyle's nodejs server
*
* ToDo: redis for active users
*
*/
var express = require('express');
var RedisStore = require('connect-redis')(express);
var dburl = 'localhost/nodejs1';
var collections = ['users'];
var db = require('mongojs').connect(dburl, collections);
var util = require('util');
var driver = require('./router/driver.js');
/* var activeusers = require('./activeusers'); */
var app = express.createServer();
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade'); /* no need to specify .jade extension */
app.set('view options', { pretty: true }); /* avoid html source all in on line or so */
app.use(express.logger('dev'));
app.use(express.favicon());
app.use(express.compress()); /* gzip */
app.use(express.bodyParser()); /* creates req.body which req.param() uses */
app.use(express.cookieParser()); /* req.session can be populated with user defined vars */
app.use(express.session({ secret: "keyboard cat", store: new RedisStore() }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.post('/create', function(req, res) {
db.users.save({tag: req.param('tag'), id: req.param('id'), status: "offline",
vehicle: {make: req.param('make'), model: req.param('model'), year: req.param('year'),
desc: req.param('desc')}, userinfo: {sig: req.param('sig')}, location: {loc: req.param('loc')},
stats: {matches: 0, won: 0, lost: 0}},
function(err, thing) {
if (err || !thing)
util.log('[create] error saving');
else
util.log('[create] successfully saved');
});
res.redirect('/');
});
app.post('/login', function(req, res) {
db.users.find({tag: req.param('tag')}, function(err, thing) {
if (err || !thing || thing.length == 0) {
util.log('[login] user does not exist');
res.send('user does not exist\n', 403);
}
else {
/* util.log('[login] retrived user: ' + util.inspect(thing)); */
if (req.param('id') === thing[0].id) { /* insert md5 hashing here */
util.log('[login] ' + thing[0].tag + ' authenticated');
db.users.update({tag: req.param('tag')}, {$set: {status: 'online'}}, function(err, updated) {
if (err || !updated)
util.log('[login] failed to set status to online');
});
res.send('successfully logged in\n', 200);
}
else {
util.log('[login] could not authenticate');
res.send('could not authenticate\n', 401);
}
}
});
});
/* /sys/do?get=activelist [list of active users]
* /sys/do?get=entermatch&master=foo&slave=bar [enter master's match]
* /sys/do?post=creatematch&master=foo&slave=bar [create match]
*
*/
app.get('/sys/:id([a-z]+)', function(req, res, next) {
/* id contains routing token, req.query is a block/struct, key-val data structure containing GET params
* id should be the main verb, action we want to do */
util.log('[sys] route id aka action: ' + req.params.id);
if (req.params.id === 'do') {
var data = '';
if (req.query.get != undefined) {
util.log('[sys] get value: ' + req.query.get);
if (req.query.get === 'activelist') {
db.users.find({status: 'online'}, function(err, result) {
if (err || !result)
util.log('[sys] do?get=activelist failed or empty');
else {
for (var i = 0; i < result.length; i++) {
data += result[i].tag + '\n';
}
res.send(data);
}
});
}
else if (req.query.get === 'entermatch' && req.query.master != undefined && req.query.master.length > 0 &&
req.query.slave != undefined && req.query.slave.length > 0) {
util.log('[sys] get: ' + req.query.slave + ' entered ' + req.query.master + '\'s match');
res.send('entered match\n');
}
else
next();
}
else if (req.query.post != undefined) {
util.log('[sys] post value: ' + req.query.post);
if (req.query.post === 'creatematch' && req.query.master != undefined && req.query.master.length > 0 &&
req.query.slave != undefined && req.query.slave.length > 0) { /* master creates a match */
/* how the fuck do I get a hold of slave? */
// invite slave, wait for reply, if accepted, keep that state!
// respond to master
util.log('[sys] post: ' + req.query.slave + ' accepted match');
res.send('match accepted\n');
}
else
next();
}
else next();
}
else
next();
});
app.get('/', function(req, res) {
db.users.find(function(err, items) {
if (err || !items || items.length == 0)
util.log('[index pn] nothing in db or error retrieving');
res.render('index', {
locals: {
title: 'Challenger 2.0',
users: items
}
});
});
});
/* routing to handlers that can driver the server's functionality */
app.get('/newuser', driver.newuser);
app.listen(8081, function() {
console.log("listening on port %d in %s mode", this.address().port, this.settings.env);
})
.on('error', function(e) {
console.log('failed creating server, errno: ' + e.errno);
});
|