Skip to content
Permalink
Browse files

Created a simple Tic Tac Toe test game with a history of games played…

… to test saving and loading. Also started doing a working prototype of saving/loading based on myfavoritebeer.org.
  • Loading branch information...
Jesse Silver
Jesse Silver committed Oct 25, 2011
1 parent e325649 commit bfec907b513c4dcbd31ef29f0c55f9979b3c8f2c
@@ -0,0 +1,6 @@
MONGODIR=~/mongodb/bin

db:
$(MONGODIR)/mongod
host:
node server/main.js
@@ -10,52 +10,45 @@ url = require('url'),
mongodb = require('mongodb');

var collections = {
dev: undefined,
beta: undefined,
prod: undefined
Games: undefined
};

exports.connect = function(cb) {
if (!process.env.MONGOLAB_URI) {
/*if (!process.env.MONGOLAB_URI) {
cb("no MONGOLAB_URI env var!");
return;
}
}*/

var bits = url.parse(process.env.MONGOLAB_URI);
var user = 'test';
var password = 'test';
var uri = 'http://' + user + ':' + password + '@localhost:27017';
var dbName = 'Gladius';
var bits = url.parse(uri);
var server = new mongodb.Server(bits.hostname, bits.port, {});
new mongodb.Db(bits.pathname.substr(1), server, {}).open(function (err, cli) {
new mongodb.Db(dbName, server, {}).open(function (err, cli) {
if (err) return cb(err);
collections.dev = new mongodb.Collection(cli, 'devbeers');
collections.local = collections.dev;
collections.beta = new mongodb.Collection(cli, 'betabeers');
collections.prod = new mongodb.Collection(cli, 'prodbeers');

// now authenticate
collections.Games = new mongodb.Collection(cli, 'Games');
//now authenticate
var auth = bits.auth.split(':');
cli.authenticate(auth[0], auth[1], function(err) {
cb(err);
});
});
};

exports.get = function(collection, email, cb) {
var c = collections[collection].find({ email: email }, { beer: 1 });
exports.get = function(collection, email, title, cb) {
var c = collections[collection].find({ email: email, title: title });
c.toArray(function(err, docs) {
if (err) return cb(err);
if (docs.length != 1) return cb("consistency error! more than one doc returned!");
cb(undefined, docs[0].beer);
console.log(docs[0]);
cb(undefined, docs[0]);
});
};

exports.set = function(collection, email, beer, cb) {
exports.set = function(collection, email, title, data, cb) {
collections[collection].update(
{ email: email },
{
email: email,
beer: beer
},
{
safe:true,
upsert: true
}, cb);
{ email: email, title: title },
{ email: email, title: title, data: JSON.parse(data) },
{ safe: true, upsert: true }, cb);
};
48 server/main.js 100755 → 100644
@@ -18,7 +18,8 @@ const COOKIE_SECRET = process.env.SEKRET || 'you love, i love, we all love beer!
const IP_ADDRESS = process.env.IP_ADDRESS || '127.0.0.1';

// The port to listen to.
const PORT = process.env.PORT || 0;
//const PORT = process.env.PORT || 0;
const PORT = 54321;

// localHostname is the address to which we bind. It will be used
// as our external address ('audience' to which assertions will be set)
@@ -76,9 +77,9 @@ function determineBrowserIDURL(req) {

return ({
prod: 'https://browserid.org',
beta: 'https://diresworb.org',
dev: 'https://dev.diresworb.org',
local: 'https://dev.diresworb.org'
beta: 'https://browserid.org',
dev: 'https://browserid.org',
local: 'https://browserid.org'
})[determineEnvironment(req)];
}

@@ -92,7 +93,6 @@ app.use(postprocess.middleware(function(req, body) {
return body.toString().replace(new RegExp("https://browserid.org", 'g'), browseridURL);
}));


// /api/whoami is an API that returns the authentication status of the current session.
// it returns a JSON encoded string containing the currently authenticated user's email
// if someone is logged in, otherwise it returns null.
@@ -101,7 +101,6 @@ app.get("/api/whoami", function (req, res) {
return res.json(null);
});


// /api/login is an API which authenticates the current session. The client includes
// an assertion in the post body (returned by browserid's navigator.id.getVerifiedEmail()).
// if the assertion is valid an (encrypted) cookie is set to start the user's session.
@@ -168,7 +167,7 @@ app.get("/api/get", function (req, res) {

if (!email) {
res.writeHead(400, {"Content-Type": "text/plain"});
res.write("Bad Request: you must be authenticated to get your beer");
res.write("Bad Request: you must be authenticated to get your game data");
res.end();
return;
}
@@ -178,13 +177,13 @@ app.get("/api/get", function (req, res) {
return res.json("no database");
}

db.get(determineEnvironment(req), email, function(err, beer) {
db.get('Games', email, req.query['gameTitle'], function(err, data) {
if (err) {
console.log("error getting beer for", email);
res.writeHead(500);
res.end();
res.writeHead(500);
res.end();
} else {
res.json(beer);
console.log(data);
res.json(data);
}
});
});
@@ -201,28 +200,31 @@ app.post("/api/set", function (req, res) {
return;
}

var beer = req.body.beer;
var data = req.body.data;

if (!beer) {
if (!data) {
res.writeHead(400, {"Content-Type": "text/plain"});
res.write("Bad Request: a 'beer' parameter is required to set your favorite beer");
res.write("Bad Request: Invalid parameters");
res.end();
return;
} else {
console.log('Title: ' + req.body.gameTitle);
console.log('Email: ' + email);
console.log(JSON.stringify(data));
}

if (!havePersistence) {
console.log("WARNING: set is a no-op! we have no database configured");
return res.json(true);
}

db.set(determineEnvironment(req), email, beer, function(err) {
if (err) {
console.log("setting beer for", email, "to", beer);
res.writeHead(500);
res.end();
} else {
res.json(true);
}
db.set('Games', email, req.body.gameTitle, data, function(err) {
if (err) {
res.writeHead(500);
res.end();
} else {
res.json(true);
}
});
});

@@ -0,0 +1,85 @@
var BrowserID = function(gameTitle, callback) {
var that = this, loggedIn = false, email = '';
callback = callback || {};
gameTitle = gameTitle || 'Unknown Title';
this.__defineGetter__('loggedIn', function() { return loggedIn; });
this.__defineGetter__('email', function() { return email; });

var fail = function(res, errback) { loggedIn = false; if (errback) errback(res); };
var success = function(res, callback) { loggedIn = true; if (callback) callback(res); };

this.setSessions = function(val) {
if (navigator.id) {
navigator.id.sessions = val ? val : [ ];
}
}

this.verify = function() {
navigator.id.getVerifiedEmail(function(assertion, callback, errback) {
var audience = document.domain || 'null';
if (assertion) {
$.ajax({
type: 'POST',
url: '/api/login',
data: { assertion: assertion, audience: audience },
success: function(res, status, xhr) {
alert(res);
if (res) {
email = res;
success(res, callback);
} else
fail(res, errback);
},
error: function(res, status, xhr) {
fail(res, errback);
}
});
} else
fail();
});
return loggedIn;
};

this.send = function(data, callback, errback) {
$.ajax({
type: 'POST',
url: '/api/set',
data: {
data: JSON.stringify(data),
gameTitle: gameTitle
},
dataType: 'json',
success: function(res, status, xhr) {
if (callback) callback(res);
},
error: function(res) {
if (errback) errback(res);
}
});
};

this.receive = function(callback, errback) {
$.ajax({
type: 'GET',
url: '/api/get',
data: { gameTitle: gameTitle },
dataType: 'json',
success: function(res, status, xhr) {
if (callback) callback(res);
},
error: function(res) {
if (errback) errback(res);
}
});
};

(function() {
$.get('/api/whoami', function (res) {
if (res) {
loggedIn = true;
email = res;
if (callback) callback(res);
}
}, 'json');
})();
};

0 comments on commit bfec907

Please sign in to comment.
You can’t perform that action at this time.