Skip to content

Commit

Permalink
Add MongoDB saving and leaderboard integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmastylo committed May 7, 2015
1 parent fe5dbf6 commit 4d523fb
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 127 deletions.
78 changes: 5 additions & 73 deletions app.js
@@ -1,9 +1,13 @@
// mongoose setup
require('./db');

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var errorhandler = require('errorhandler');

//includes for mongoDB connectivity
var mongoose = require('mongoose');
Expand All @@ -13,79 +17,6 @@ var users = require('./routes/users');

var app = express();


//DB setup
//Schema defines what goes in a collection
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
//connect to the database
mongoose.connect('mongodb://localhost:27017');
//schema for global tracking of games played
var globalSchema = new Schema({
gamesPlayed : Number,
redWins : Number,
blueWins : Number,
tags : Number,
totalCaptures : Number
});
//model sets up the schema for usage although in this case there will only be one entry ever
var globalModel = mongoose.model('globalModel',globalSchema);
// //initialize a model
var globDB = new globalModel();
//clear the db for testing purposes
// globalModel.remove({}, function(err) {
// console.log('cleared globalModel');
// });
//set the model's data
globDB.gamesPlayed = 100;
globDB.redWins = 50;
globDB.blueWins = 50;
globDB.tags = 14276;
globDB.totalCaptures = 2000;
//save the instance
//globDB.save();
globalModel.find({},function(err,docs){
for(doc in docs){
console.log(docs[doc]);
}
});

//schema for tracking of users
var userInfoSchema = new Schema({
name : String,
gamesPlayed : Number,
wins : Number,
losses : Number,
tags : Number,
captures : Number
})
//model sets up schema for usage
var uInfo = mongoose.model('uInfoModel',userInfoSchema);

// for (var i=0;i<3;i++) {
// var testUserOne = new uInfo( {name: 'Plotka', gamesPlayed: 10, wins: 10, losses: 0, tags: 100, captures: 50});
// testUserOne.save(function(err, user) {
// if (err) return console.log(err);
// });

// var testUserTwo = new uInfo( {name: 'Shirly', gamesPlayed: 5, wins: 4, losses: 1, tags: 25, captures: 3});
// testUserTwo.save(function(err, user) {
// if (err) return console.log(err);
// });

// var testUserThree = new uInfo( {name: 'Ahmed', gamesPlayed: 20, wins: 10, losses: 10, tags: 250, captures: 25});
// testUserThree.save(function(err, user) {
// if (err) return console.log(err);
// });
// };

//DB acessors
app.get('userInfo',function(res,req){
uInfo.find({},function(err,docs){
return res.json(docs);
});
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
Expand All @@ -109,6 +40,7 @@ app.use(function(req, res, next) {
});

// error handlers
app.use(errorhandler({ showStack: true, dumpExceptions: true }));

// development error handler
// will print stacktrace
Expand Down
Binary file modified data/local.0
Binary file not shown.
Binary file modified data/local.ns
Binary file not shown.
6 changes: 6 additions & 0 deletions db.js
@@ -0,0 +1,6 @@
var mongoose = require('mongoose');
var GlobalModel = require('./models/globalModel.js');
var UserInfo = require('./models/userInfo.js');

// Connect to MongoDB
mongoose.connect('mongodb://localhost/ctf');
13 changes: 13 additions & 0 deletions models/globalModel.js
@@ -0,0 +1,13 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// schema for global tracking of games played
var GlobalModel = new Schema({
gamesPlayed : Number,
redWins : Number,
blueWins : Number,
tags : Number,
totalCaptures : Number
});

module.exports = GlobalModel = mongoose.model('GlobalModel', GlobalModel);
14 changes: 14 additions & 0 deletions models/userInfo.js
@@ -0,0 +1,14 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// schema for tracking of users
var UserInfoSchema = new Schema({
name : String,
gamesPlayed : Number,
wins : Number,
losses : Number,
tags : Number,
captures : Number
})

module.exports = UserInfo = mongoose.model('UserInfo', UserInfoSchema);
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -11,10 +11,13 @@
"cookie-parser": "~1.3.3",
"debug": "~2.1.1",
"ejs": "~2.2.3",
"errorhandler": "^1.3.5",
"express": "~4.11.1",
"express-partials": "^0.3.0",
"jade": "^1.9.2",
"mongoose": "^4.0.2",
"morgan": "~1.5.1",
"request": "^2.55.0",
"serve-favicon": "~2.2.0",
"socket.io": "^1.3.4"
}
Expand Down
13 changes: 12 additions & 1 deletion public/game/server.js
@@ -1,3 +1,5 @@
var request = require('request');

var Server = IgeClass.extend({
classId: 'Server',
Server: true,
Expand Down Expand Up @@ -271,14 +273,23 @@ var Server = IgeClass.extend({

gameEnd: function() {
self = this;
console.log("Ganed ending!");
console.log("Game ending!");

// stop game things happening
this.game_active = false;

this.resetGame();

// print (or save to db) game results and reset
console.log("Game Ended!");

var params = { red_score: self.red_score, blue_score: self.blue_score };
request.post('game_results', params, function(err, res, body) {
if (!err && res.statusCode === 200) {
console.log(res, body);
}
});

console.log("Red score");
console.log(self.red_score);
self.red_score = 0;
Expand Down
19 changes: 11 additions & 8 deletions public/javascripts/leaderboard.js
@@ -1,8 +1,11 @@
var app = angular.module('myApp', []);
function leaderboard($scope, $http){

$http.get("/userInfo").success(function (response) {
console.dir(response);
$scope.names = response.records;
});
};
var app = angular.module('ctf', []);

function leaderboard($scope, $http) {
$scope.leaderboardInit = function() {
$http.get("/userInfo").success(function (response) {
console.log(response);
$scope.stats = response.stats;
$scope.users = response.users;
});
};
}
49 changes: 47 additions & 2 deletions routes/index.js
@@ -1,13 +1,58 @@
var express = require('express');
var router = express.Router();

// MongoDB dependencies
var mongoose = require('mongoose');
var GlobalModel = mongoose.model('GlobalModel');
var UserInfo = mongoose.model('UserInfo');

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'CTF' });
});

router.get('/leaderboard',function(req,res,next){
res.render('leaderboard',{title:'CTF'});
router.get('/leaderboard', function(req, res, next){
res.render('leaderboard', { title:'CTF Leaderboard' });
});

router.get('/userInfo', function(req, res, next) {
// clear the db for testing purposes
// response.stats = [{
// gamesPlayed : 100,
// redWins : 50,
// blueWins : 50,
// tags : 14276,
// totalCaptures : 2000
// }];

// newGlobal = new GlobalModel(response.stats);
// newGlobal.save({});

// response.users = [
// { name: 'Plotka', gamesPlayed: 10, wins: 10, losses: 0, tags: 100, captures: 50 },
// { name: 'Shirly', gamesPlayed: 5, wins: 4, losses: 1, tags: 25, captures: 3 },
// { name: 'Ahmed', gamesPlayed: 20, wins: 10, losses: 10, tags: 250, captures: 25 }
// ]

// UserInfo.create(response.users, function(err, _) {
// res.json(response);
// });

UserInfo.find({}, function(err, users) {
GlobalModel.find({}, function(err, stats) {
res.json({ users: users, stats: stats });
});
});
});

router.post('game_results', function(req, res, next) {
// Expects { gamesPlayed, redWins, blueWins, tags, totalCaptures }
var stats = req.body.stats;
console.log("yo" + req.body.stats);

GlobalModel.update({}, stats, function() {
res.json({ result: true });
});
});

module.exports = router;
Binary file modified views/.DS_Store
Binary file not shown.
12 changes: 7 additions & 5 deletions views/error.jade
@@ -1,6 +1,8 @@
extends layout
html
head
title Capture The Flag

block content
h1= message
h2= error.status
pre #{error.stack}
body
h1= message
h2= error.status
pre #{error.stack}
73 changes: 35 additions & 38 deletions views/leaderboard.jade
@@ -1,13 +1,14 @@
html(ng-controller='leaderboard')
html(ng-app="ctf")
head
title Capture The Flag
link(rel='stylesheet', href='css/lstyle.css')
link(rel="stylesheet", href="css/foundation.css")
link(rel="stylesheet", href="css/font-awesome.min.css")
script(src='http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js')
link(rel="shortcut icon", href="favicon.ico", type="image/x-icon")
body
// Nav Bar

script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js')
script(src='javascripts/leaderboard.js')
body(ng-controller="leaderboard")
nav.icon-bar.four-up
a.item(href='')
i.fa.fa-home
Expand All @@ -18,8 +19,6 @@ html(ng-controller='leaderboard')
a.item(href='https://github.com/dmastylo/itws-4200-group')
i.fa.fa-user


// Leaderboard
section#first.main
header
.container
Expand All @@ -28,39 +27,37 @@ html(ng-controller='leaderboard')
tr
th Position
th Username
th Points
tr
td.positions 1
td Shirley
td $64,000
tr
td.positions 2
td Plotka
td 4070
tr
td.positions 3
td Barb
td 1200
tr
td.positions 4
td Moorthy
td 10
th Games Played
th Wins
th Losses
th Tags
th Captures

tr(ng-repeat='user in users')
td.positions {{ $index }}
td {{ user.name }}
td {{ user.gamesPlayed }}
td {{ user.wins }}
td {{ user.losses }}
td {{ user.tags }}
td {{ user.captures }}

h2 Global Game Stats
table(style='width:80%' data-ng-init="leaderboardInit()")
tr
td.positions 3
td rpi
td 2
div
table
tr(ng-repeat='x in names')
td {{ $index + 1 }}
td {{ x.name }}
td {{ x.gamesPlayed }}
td {{ x.wins }}
td {{ x.losses }}
td {{ x.tags}}
td {{ x.captures}}
script(src='javascripts/leaderboard.js')
// Footer
th Total Games Played
th Red Wins
th Blue Wins
th Tags
th Total Captures

tr(ng-repeat='stat in stats')
td {{ stat.gamesPlayed }}
td {{ stat.redWins }}
td {{ stat.blueWins }}
td {{ stat.tags }}
td {{ stat.totalCaptures }}

footer
p
| CTF was created by Damian, Max, Nathan, Peter, Ylonka, and Zach for Web Science Systems Development, Spring 2015.
Expand Down

0 comments on commit 4d523fb

Please sign in to comment.