<commit_before>/**
 * Created by Casper on 13/02/2016.
 */

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var PORT = 8080;
var bodyParser = require('body-parser');
var path = require('path');
var Result = require('./client/app/models/resultSchema.js');

app.set('views', path.join(__dirname, '/client'));
app.set('view engine', 'ejs');

app.use(express.static(path.join(__dirname, '/client')));
//app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

mongoose.connect('mongodb://localhost/season2')
//mongoose.connect('mongodb://localhost/test0')
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
    console.log("Connected to MongoDB...");
})

app.get('/results/all', function(req, res){
    Result.find({}, function(err, results){
        if(err){
            res.json([]);
            return console.error(err);
        }else{
            console.log("app.get('/results/all') successful.")
            res.json(results);;
        }
    })
})


app.get('/results/:gameMode', function(req, res){
    var gameMode = req.params.gameMode;
    Result.find({gameMode: gameMode}, function(err, results){
        if(err){
            res.json([]);
            return console.error(err);
        }else{
            console.log("app.get('/results/:gameMode') successful.")
            res.json(results);;
        }
    })
})

app.post('/results/:gameMode', function(req, res){
    var gameMode = req.params.gameMode;
    var result = req.body.result;
    var rank = req.body.rank;
    var gameNumber = req.body.gameNumber;
    var date = new Date();
    var newResult = new Result({gameMode: gameMode, result: result, rank: rank, gameNumber: gameNumber, date: date});
    newResult.save(function(err, result){
        if(err){
            res.send(err);
            return console.error(err);
        }else{
            console.log("app.post('/results/:gameMode') save successful.")
            console.log("Saved: " + result);
            res.json(result);
        }
    });

})

app.get('/recent/:gameMode', function(req, res){
    var gameMode = req.params.gameMode;
    var RESULTS_TO_SHOW = 10;

    //Result.find({gameMode: gameMode})
    //    .sort({date: -1})
    //    .limit(20)
    //    .exec(function(err, results){
    //        if(err){
    //            res.json([]);
    //            return console.error(err);
    //        }else{
    //            res.json(results);;
    //        }
    //    });

    Result.count({gameMode: gameMode}, function(err, count){
        if(err){
            console.log("Result.count() error: " + err);
        }else{
            if(count < 20) RESULTS_TO_SHOW = count;
            Result.find({gameMode: gameMode})
                .skip(count - RESULTS_TO_SHOW)
                .exec(function(err, results){
                    if(err){
                        res.json([]);
                        return console.error(err);
                    }else{<commit_msg>Update number of results to show on table<commit_after>/**
 * Created by Casper on 13/02/2016.
 */

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var PORT = 8080;
var bodyParser = require('body-parser');
var path = require('path');
var Result = require('./client/app/models/resultSchema.js');

app.set('views', path.join(__dirname, '/client'));
app.set('view engine', 'ejs');

app.use(express.static(path.join(__dirname, '/client')));
//app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

mongoose.connect('mongodb://localhost/season2')
//mongoose.connect('mongodb://localhost/test0')
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
    console.log("Connected to MongoDB...");
})

app.get('/results/all', function(req, res){
    Result.find({}, function(err, results){
        if(err){
            res.json([]);
            return console.error(err);
        }else{
            console.log("app.get('/results/all') successful.")
            res.json(results);;
        }
    })
})


app.get('/results/:gameMode', function(req, res){
    var gameMode = req.params.gameMode;
    Result.find({gameMode: gameMode}, function(err, results){
        if(err){
            res.json([]);
            return console.error(err);
        }else{
            console.log("app.get('/results/:gameMode') successful.")
            res.json(results);;
        }
    })
})

app.post('/results/:gameMode', function(req, res){
    var gameMode = req.params.gameMode;
    var result = req.body.result;
    var rank = req.body.rank;
    var gameNumber = req.body.gameNumber;
    var date = new Date();
    var newResult = new Result({gameMode: gameMode, result: result, rank: rank, gameNumber: gameNumber, date: date});
    newResult.save(function(err, result){
        if(err){
            res.send(err);
            return console.error(err);
        }else{
            console.log("app.post('/results/:gameMode') save successful.")
            console.log("Saved: " + result);
            res.json(result);
        }
    });

})

app.get('/recent/:gameMode', function(req, res){
    var gameMode = req.params.gameMode;
    var RESULTS_TO_SHOW = 10;

    Result.count({gameMode: gameMode}, function(err, count){
        if(err){
            console.log("Result.count() error: " + err);
        }else{
            if(count < RESULTS_TO_SHOW) RESULTS_TO_SHOW = count;
            Result.find({gameMode: gameMode})
                .skip(count - RESULTS_TO_SHOW)
                .exec(function(err, results){
                    if(err){
                        res.json([]);
                        return console.error(err);
                    }else{
{'fraction_non_alphanumeric': 0.1725557461406518, 'fraction_numerical': 0.006346483704974271, 'mean_word_length': 2.3434633027522938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3049924', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1768, 'n_words': 343}