Browse files

Game model, joining

  • Loading branch information...
1 parent ccf1fcb commit 59f9968e05c7565013e33dbd3a97d63d08fb12e9 @ryansroberts ryansroberts committed Oct 26, 2013
Showing with 51,833 additions and 4 deletions.
  1. +51,704 −0 GB.txt
  2. +28 −0 app/controllers/game.js
  3. +14 −0 app/models/game.js
  4. +1 −0 config/express.js
  5. +6 −2 config/routes.js
  6. +75 −0 namey.js
  7. +5 −2 package.json
View
51,704 GB.txt
51,704 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
28 app/controllers/game.js
@@ -0,0 +1,28 @@
+var mongoose = require('mongoose'),
+ Game = require('../models/game'),
+ _ = require('underscore'),
+ request = require('request');
+
+exports.open = function(req, res) {
+ Game.find({}, function(error, games) {
+ res.json(games);
+ });
+};
+
+exports.join = function(req, res) {
+ console.log(req.params.id);
+ Game.findById(req.params.id, function(error, game) {
+ request("http://namey.muffinlabs.com/name.json?count=1&with_surname=true", function(error, response, body) {
+ if (_.find(game.players, function(p) {
+ return p.user == req.params.id;
+ })) return;
+ game.players.push({
+ user: req.body.id,
+ pseudonym: JSON.parse(body)[0]
+ });
+ game.save();
+ res.send(game);
+ });
+ });
+};
+
View
14 app/models/game.js
@@ -0,0 +1,14 @@
+
+var mongoose = require('mongoose'),
+ Schema = mongoose.Schema;
+
+var GameSchema = new Schema({
+ name: String,
+ geo: {type:[Number],index:'2d'},
+ distance: Number,
+ requiredUsers: Number,
+ state: {type:String,default:"open"},
+ players : [{user:Schema.Types.ObjectId,pseudonym:String}]
+});
+
+module.exports = mongoose.model('Game', GameSchema);
View
1 config/express.js
@@ -14,6 +14,7 @@ module.exports = function(app, config) {
app.use(express.bodyParser());
app.use(express.methodOverride());
restify.serve(app,mongoose.model('User'));
+ restify.serve(app,mongoose.model('Game'));
app.use(app.router);
app.use(function(req, res) {
res.status(404).render('404', { title: '404' });
View
8 config/routes.js
@@ -9,7 +9,11 @@ module.exports = function(app){
app.get('/pictures/:id', pictures.show);
var user = require('../app/controllers/user');
- app.post('/user',user.create);
- app.get('/user:id',user.id);
+ app.post('/user',user.create);
+ app.get('/user:id',user.id);
+
+ var game = require('../app/controllers/game');
+ app.get('/api/v1/opengames',game.open);
+ app.post('/api/v1/joingame/:id',game.join);
};
View
75 namey.js
@@ -0,0 +1,75 @@
+exports.namey = {
+ // jx -- http://www.openjs.com/scripts/jx/ -- V3.00.A
+ jx:{getHTTPObject:function(){var A=false;if(typeof ActiveXObject!="undefined"){try{A=new ActiveXObject("Msxml2.XMLHTTP")}catch(C){try{A=new ActiveXObject("Microsoft.XMLHTTP")}catch(B){A=false}}}else{if(window.XMLHttpRequest){try{A=new XMLHttpRequest()}catch(C){A=false}}}return A},load:function(url,callback,format){var http=this.init();if(!http||!url){return }if(http.overrideMimeType){http.overrideMimeType("text/xml")}if(!format){var format="text"}format=format.toLowerCase();var now="uid="+new Date().getTime();url+=(url.indexOf("?")+1)?"&":"?";url+=now;http.open("GET",url,true);http.onreadystatechange=function(){if(http.readyState==4){if(http.status==200){var result="";if(http.responseText){result=http.responseText}if(format.charAt(0)=="j"){result=result.replace(/[\n\r]/g,"");result=eval("("+result+")")}if(callback){callback(result)}}else{if(error){error(http.status)}}}};http.send(null)},init:function(){return this.getHTTPObject()}},
+
+ /**
+ * API for namey random name generator. There's two basic ways to use it. First, just call namey.get with a callback:
+ *
+ * namey.get(function(n) { console.log(n); }); => ["John Clark"]
+ *
+ * The call returns an array because there's an option to request more than one random name. For example:
+ *
+ * namey.get({ count: 3, callback: function(n) { console.log(n); }}); ; => ["John Cook", "Ruth Fisher", "Donna Collins"]
+ *
+ * Here's the full list of parameters:
+ *
+ * count -- how many names you would like (default: 1)
+ *
+ * type -- what sort of name you want 'female', 'male', 'surname', or leave blank if you want both genders
+ *
+ * with_surname -- true/false, if you want surnames with the first
+ * name. If false, you'll just get first names. Default is true.
+ *
+ * frequency -- 'common', 'rare', 'all' -- default is 'common'. This
+ * picks a subset of names from the database -- common names are
+ * names that occur frequently, rare is names that occur rarely.
+ *
+ * min_freq/max_freq -- specific values to get back a really
+ * specific subset of the names db. values should be between 0 and
+ * 100. You probably don't need this, but here's an example:
+ * namey.get({ count: 3, min_freq: 30, max_freq: 50, callback: function(n) { console.log(n); }});
+ * => ["Crystal Zimmerman", "Joshua Rivas", "Tina Bryan"]
+ *
+ * callback -- a function to do something with the data. The data
+ * passed in will be an array of names -- use them wisely.
+ *
+ */
+ get : function(options) {
+ var callback;
+ var tmp_params = [];
+
+ if ( typeof(options) == "function" ) {
+ callback = options;
+ }
+ else if ( typeof(options) == "object" ) {
+ callback = options.callback;
+
+ if ( typeof(options.count) == "undefined" ) {
+ options.count = 1;
+ }
+ tmp_params.push("count=" + options.count);
+
+ if ( typeof(options.type) != "undefined" && options.type != "both" ) {
+ tmp_params.push("type=" + options.type);
+ };
+
+ if ( options.type != "surname" && typeof(options.with_surname) != "undefined" ) {
+ tmp_params.push("with_surname=" + options.with_surname);
+ }
+ if ( options.min_freq ) {
+ tmp_params.push("min_freq=" + options.min_freq);
+ tmp_params.push("max_freq=" + options.max_freq);
+ }
+ else if ( typeof(options.frequency) != "undefined" ) {
+ tmp_params.push("frequency=" + options.frequency);
+ }
+ }
+
+ this.jx.load('/name.json?' + tmp_params.join("&"), function(d) {
+ var tmp = eval('(' + d + ')');
+ if ( typeof(callback) == "function" ) {
+ callback(tmp);
+ }
+ });
+ }
+}
View
7 package.json
@@ -1,6 +1,6 @@
{
"name": "appsassin",
- "version": "0.0.1-4",
+ "version": "0.0.1-9",
"private": true,
"scripts": {
"start": "app.js"
@@ -9,7 +9,10 @@
"express": "~3.3.4",
"jade": "~0.34.0",
"mongoose": "~3.6.14",
- "express-restify-mongoose": "~0.3.3"
+ "express-restify-mongoose": "~0.3.3",
+ "node-geonames": "0.0.11",
+ "underscore": "~1.5.2",
+ "request": "~2.27.0"
},
"devDependencies": {
"grunt": "~0.4.1",

0 comments on commit 59f9968

Please sign in to comment.