summaryrefslogtreecommitdiffstats
path: root/protoype/router
diff options
context:
space:
mode:
Diffstat (limited to 'protoype/router')
-rw-r--r--protoype/router/driver.js6
-rw-r--r--protoype/router/index.js25
-rw-r--r--protoype/router/myplatform.js77
-rw-r--r--protoype/router/user.js69
4 files changed, 174 insertions, 3 deletions
diff --git a/protoype/router/driver.js b/protoype/router/driver.js
index 05e1039..5e79856 100644
--- a/protoype/router/driver.js
+++ b/protoype/router/driver.js
@@ -1,8 +1,8 @@
-/* handlers user to drive
+/* handlers used to drive
*
*/
-function newuser(req, res) {
+function create_get(req, res) {
res.send('<!DOCTYPE html>\n' +
'<html>\n' +
'<body>\n' +
@@ -21,5 +21,5 @@ function newuser(req, res) {
'</html>\n');
}
-exports.newuser = newuser;
+exports.create_get = create_get;
diff --git a/protoype/router/index.js b/protoype/router/index.js
new file mode 100644
index 0000000..d7da788
--- /dev/null
+++ b/protoype/router/index.js
@@ -0,0 +1,25 @@
+/*
+ * index.js
+ *
+ * main route
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+function root(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', {
+ title: 'Challenger 2.0', /* might use app.locals */
+ users: items
+ });
+ });
+}
+
+/* we're exporting object of type module, so exports is enough */
+exports.root = root;
+
diff --git a/protoype/router/myplatform.js b/protoype/router/myplatform.js
new file mode 100644
index 0000000..ef189aa
--- /dev/null
+++ b/protoype/router/myplatform.js
@@ -0,0 +1,77 @@
+/*
+ * myplatform.js
+ *
+ * main inerface used for the server platform
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+/* /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]
+ *
+ */
+function system(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);
+ }
+ });
+ }
+ /* slave enters a match here */
+ 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) /* post here refers to a param client would assign */
+ {
+ util.log('[sys] client 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 {
+ util.logger('[sys] unrecognized client data post');
+ next();
+ }
+ }
+ else
+ {
+ util.log('[sys] undefined GET params');
+ next();
+ }
+ }
+ else
+ {
+ /* unknown action */
+ next();
+ }
+}
+
+exports.system = system;
+
diff --git a/protoype/router/user.js b/protoype/router/user.js
new file mode 100644
index 0000000..4215054
--- /dev/null
+++ b/protoype/router/user.js
@@ -0,0 +1,69 @@
+/*
+ * user.js
+ *
+ * routing handlers for a user
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+var connected_clients = {};
+
+function create_get(req, res, next) {
+}
+
+function create_post(req, res, next) {
+ 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('/');
+}
+
+function login_get(req, res, next) {
+}
+
+/*
+ * test drive: curl -d 'tag=unclescotty&id=brotato' localhost:8081/login
+ *
+ */
+function login_post(req, res, next) {
+ 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');
+ });
+ /* real deal? */
+ connected_clients[thing[0].tag] = {ip: res.connection.myip, port: res.connection.myport};
+ res.send('successfully logged in\n', 200);
+ }
+ else {
+ util.log('[login] could not authenticate');
+ res.send('could not authenticate\n', 401);
+ }
+ }
+ });
+}
+
+module.exports = {
+ create_get: create_get,
+ create_post: create_post,
+ login_get: login_get,
+ login_post: login_post
+};
+