<commit_before>var https = require('https');
var base64Encode = require('base64-stream').encode;

var github;

var config = require('../config');

var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var url = "mongodb://"+ config.mongodb.host+":"+config.mongodb.port+"/gitrunner";

var db;

MongoClient.connect(url, function(err, _db) {
    if (!err && _db) {
        db = _db;
    } else {
        throw "Could not connect to mongo";
    }
});

function generateID() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }

    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
        s4() + '-' + s4() + s4() + s4();
};

function getStartLevel(){
    return ['rpc-team', 'GitRunner'];
    //return ['Odobo', 'odobox'];
}

function getNextLevel(prevOwner, prevRepo){
    return ['vert-x3', 'vertx-lang-js'];
}

function parseGitHubStats(playerID, gameID, owner, repository, res) {
    new Promise(function(resolve, reject) {
        github.repos.get({
            user: owner,
            repo: repository
        }, function(err, data) {
            if(err) {
                return reject();
            }

            resolve({ repo: data });
        });
    })
    .then(function(prevData) {
        return new Promise(function(resolve, reject) {
            github.repos.getBranches({
                user: owner,
                repo: repository
            }, function(err, data) {
                if(err) {
                    return reject();
                }

                prevData.branches = data;
                resolve(prevData);
            });
        });
    })
    .then(function(prevData) {
        return new Promise(function(resolve, reject) {
            github.repos.getLanguages({
                user: owner,
                repo: repository
            }, function(err, data) {
                if(err) {
                    return reject();
                }

                prevData.languages = data;
                resolve(prevData);
            });
        });
    })
    .then(function(prevData) {
        return new Promise(function(resolve, reject) {
            github.repos.getReadme({
                user: owner,
                repo: repository
            }, function(err, data) {
                prevData.readme = !(!!err);
                resolve(prevData);
            });
        });
    })
    .then(function(allData) {
        var avatar_url = allData.repo.organization ? allData.repo.organization.avatar_url : allData.repo.owner.avatar_url;
        https.get(avatar_url, function(_res) {
            if(_res.statusCode === 200) {
                var imgb64 = '', contentType = _res.headers['content-type'];

                var enc = _res.pipe(base64Encode());
                enc.on('data', function(chunk){
                    imgb64 += chunk;
                });
                enc.on('finish', function(ef) {
                    var maxLangSize = 0;

                    Object.keys(allData.languages).map(function(v) { if(allData.languages[v] > maxLangSize) maxLangSize = allData.languages[v]; });

                    //function returnBestRepoSize(size){
                    //    var minSize = (Math.floor(maxLangSize / 1024) + allData.repo.subscribers_count) * 5;
                    //    var maxSize = 2500;
                    //    if(size > 2500) {
                    //        return
                    //    }
                    //}


                    var data = {
                        playerID: playerID,
                        gameID: gameID,
                        size: (Math.floor(maxLangSize / 1024) + allData.repo.subscribers_count) * 5,
                        obstacles: Math.floor(maxLangSize / 1024),
                        monsters: allData.repo.subscribers_count,
                        gaps: Object.keys(allData.branches).length,
                        fires: allData.repo.forks_count,
                        readme: allData.readme,
                        avatar: 'data:' + contentType + ';base64,' + imgb64
                    };

                    db.collection('gameplay').updateOne({ "_id" : playerID + "_" + gameID }, { $inc: { "maxScoreSize": data.size }}, function(err, result) {
                        if(!err) {
                            res.send(data);
                        } else {
                            res.status(500).send({ message: 'Failed to update gameplay into db' });
                        }
                    });
                });
            }
        });
    })
    .catch(function() {
        res.status(500).send({ message: 'Failed to fetch the required data..' });<commit_msg>Server - added owner and repo name<commit_after>var https = require('https');
var base64Encode = require('base64-stream').encode;

var github;

var config = require('../config');

var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var url = "mongodb://"+ config.mongodb.host+":"+config.mongodb.port+"/gitrunner";

var db;

MongoClient.connect(url, function(err, _db) {
    if (!err && _db) {
        db = _db;
    } else {
        throw "Could not connect to mongo";
    }
});

function generateID() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }

    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
        s4() + '-' + s4() + s4() + s4();
};

function getStartLevel(){
    return ['rpc-team', 'GitRunner'];
    //return ['Odobo', 'odobox'];
}

function getNextLevel(prevOwner, prevRepo){
    return ['vert-x3', 'vertx-lang-js'];
}

function parseGitHubStats(playerID, gameID, owner, repository, res) {
    new Promise(function(resolve, reject) {
        github.repos.get({
            user: owner,
            repo: repository
        }, function(err, data) {
            if(err) {
                return reject();
            }

            resolve({ repo: data });
        });
    })
    .then(function(prevData) {
        return new Promise(function(resolve, reject) {
            github.repos.getBranches({
                user: owner,
                repo: repository
            }, function(err, data) {
                if(err) {
                    return reject();
                }

                prevData.branches = data;
                resolve(prevData);
            });
        });
    })
    .then(function(prevData) {
        return new Promise(function(resolve, reject) {
            github.repos.getLanguages({
                user: owner,
                repo: repository
            }, function(err, data) {
                if(err) {
                    return reject();
                }

                prevData.languages = data;
                resolve(prevData);
            });
        });
    })
    .then(function(prevData) {
        return new Promise(function(resolve, reject) {
            github.repos.getReadme({
                user: owner,
                repo: repository
            }, function(err, data) {
                prevData.readme = !(!!err);
                resolve(prevData);
            });
        });
    })
    .then(function(allData) {
        var avatar_url = allData.repo.organization ? allData.repo.organization.avatar_url : allData.repo.owner.avatar_url;
        https.get(avatar_url, function(_res) {
            if(_res.statusCode === 200) {
                var imgb64 = '', contentType = _res.headers['content-type'];

                var enc = _res.pipe(base64Encode());
                enc.on('data', function(chunk){
                    imgb64 += chunk;
                });
                enc.on('finish', function(ef) {
                    var maxLangSize = 0;

                    Object.keys(allData.languages).map(function(v) { if(allData.languages[v] > maxLangSize) maxLangSize = allData.languages[v]; });

                    //function returnBestRepoSize(size){
                    //    var minSize = (Math.floor(maxLangSize / 1024) + allData.repo.subscribers_count) * 5;
                    //    var maxSize = 2500;
                    //    if(size > 2500) {
                    //        return
                    //    }
                    //}


                    var data = {
                        playerID: playerID,
                        gameID: gameID,
                        owner: owner,
                        repository: repository,
                        size: (Math.floor(maxLangSize / 1024) + allData.repo.subscribers_count) * 5,
                        obstacles: Math.floor(maxLangSize / 1024),
                        monsters: allData.repo.subscribers_count,
                        gaps: Object.keys(allData.branches).length,
                        fires: allData.repo.forks_count,
                        readme: allData.readme,
                        avatar: 'data:' + contentType + ';base64,' + imgb64
                    };

                    db.collection('gameplay').updateOne({ "_id" : playerID + "_" + gameID }, { $inc: { "maxScoreSize": data.size }}, function(err, result) {
                        if(!err) {
                            res.send(data);
                        } else {
                            res.status(500).send({ message: 'Failed to update gameplay into db' });
                        }
                    });
                });
            }
        });
    })
    .catch(function() {
        res.status(500).send({ message: 'Failed to fetch the required data..' });
{'fraction_non_alphanumeric': 0.13363191334236016, 'fraction_numerical': 0.013748567857514842, 'mean_word_length': 1.375556655121227, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 7, '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': '3477678', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2552, 'n_words': 499}