<commit_before>'use strict';

var co = require('co');
var logger = require('winston');
var config = require('../config');


var gamers = {};
var errors = [];

function lookup(discord) {
    return gamers[discord];
}

function players(game) {
    //
    var p = [];
    for(var g in gamers) {
        if(gamers[g].games.indexOf(game) !== -1) {
            p.push(gamers[g]);
        }
    }
    return p;
}


function parse(text) {

    var gamer = {}; 
    logger.debug("parsing %s", text);
    var tokens = text.split("|");
    tokens.forEach(function (t) {
        logger.debug("Token: "+t);
    });    
    // skip anything without exactly 3 tokens
    if(tokens.length != 3) return;

    // parse the gamertag
    var id = tokens[0].substring(1).trim().replace(/[@\\()]/g,"").split(" ");

    logger.debug("id count: %d", id.length);

    if(id.length === 0 || id.length > 2) return;
    gamer.discord = id[0];
    gamer.psn = (id.length === 2) ? id[1] : id[0];

    gamer.games = tokens[1].split(",").map(function (s) { return s.trim() }); 
    gamer.tz = tokens[2].trim();

    logger.debug("gamer: ", gamer);

    return gamer;

}

// parse the msg for PSN ID's 

function update(msg) {
    // relax the match so that we can try and catch more 
    // parsing errors without erroring on general text
    var m = msg.content.match(/\-.+\|.+/g);
    var gamer;

    if(!m) return;

    for (var i = 0; i < m.length; i++) {
        gamer = parse(m[i]);
        if(gamer) {
            gamers[gamer.discord] = gamer;
        } else {
            errors.push(m[i]);
        }
    }

}

// scrape the topic for PSN ID's 
function scrape(bot) {

    // clear down the errors
    errors.length = 0;

    // clear down the gamers
    for (var g in gamers) delete gamers[g];

    return co(function* () {
        for(var c = 0; c < bot.channels.length; c++) {
            if(bot.channels[c].name === config.discord.psnChannel) {
                // found a PSN channel
                var logs = yield bot.getChannelLogs(bot.channels[c]);
                // the logs appear in reverse order, so we need to 
                // process them in reverse to get them chronologically
                for(var l = logs.length - 1; l >= 0; l--) {
                    update(logs[l]);
                } 
            }
        }
    });

    
}

module.exports.lookup = lookup;
module.exports.players = players;
module.exports.scrape = scrape;
module.exports.update = update;
module.exports.errors = errors;
module.exports.gamers = gamers;<commit_msg>Added discord lookup in PSN entries<commit_after>'use strict';

var co = require('co');
var logger = require('winston');
var config = require('../config');


var gamers = {};
var errors = [];

function lookup(discord) {
    return gamers[discord];
}

function players(game) {
    //
    var p = [];
    for(var g in gamers) {
        if(gamers[g].games.indexOf(game) !== -1) {
            p.push(gamers[g]);
        }
    }
    return p;
}


function parse(bot, text) {

    var gamer = {}; 
    logger.debug("parsing %s", text);
    var tokens = text.split("|");
    tokens.forEach(function (t) {
        logger.debug("Token: "+t);
    });    
    // skip anything without exactly 3 tokens
    if(tokens.length != 3) return;

    // parse the gamertag
    var id = tokens[0].substring(1).trim().replace(/[@\\()]/g,"").split(" ");

    logger.debug("id count: %d", id.length);

    if(id.length === 0 || id.length > 2) return;
    gamer.discord = id[0];
    var discord;
    var found = false;
    if(gamer.discord.match(/^\<\d+\>$/)) {
        discord = gamer.discord.replace(/[\<\>]/g, "");
        // looks like a discord id
        for(var u = 0; u < bot.users.length; u++) {
            if(bot.users[u].id === discord ) {
                gamer.discord = bot.users[u].username;
                found = true;
                break;
            }
        }
        if(!found) return;
    }


    gamer.psn = (id.length === 2) ? id[1] : id[0];

    gamer.games = tokens[1].split(",").map(function (s) { return s.trim() }); 
    gamer.tz = tokens[2].trim();

    logger.debug("gamer: ", gamer);

    return gamer;

}

// parse the msg for PSN ID's 

function update(bot, msg) {
    // relax the match so that we can try and catch more 
    // parsing errors without erroring on general text
    var m = msg.content.match(/\-.+\|.+/g);
    var gamer;

    if(!m) return;

    for (var i = 0; i < m.length; i++) {
        gamer = parse(bot, m[i]);
        if(gamer) {
            gamers[gamer.discord] = gamer;
        } else {
            errors.push(m[i]);
        }
    }

}

// scrape the topic for PSN ID's 
function scrape(bot) {

    // clear down the errors
    errors.length = 0;

    // clear down the gamers
    for (var g in gamers) delete gamers[g];

    return co(function* () {
        for(var c = 0; c < bot.channels.length; c++) {
            if(bot.channels[c].name === config.discord.psnChannel) {
                // found a PSN channel
                var logs = yield bot.getChannelLogs(bot.channels[c]);
                // the logs appear in reverse order, so we need to 
                // process them in reverse to get them chronologically
                for(var l = logs.length - 1; l >= 0; l--) {
                    update(bot, logs[l]);
                } 
            }
        }
    });

    
}

module.exports.lookup = lookup;
module.exports.players = players;
module.exports.scrape = scrape;
module.exports.update = update;
module.exports.errors = errors;
module.exports.gamers = gamers;
{'fraction_non_alphanumeric': 0.16258552394670509, 'fraction_numerical': 0.006661865322290242, 'mean_word_length': 1.961087420042644, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, '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': '7500610', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1770, 'n_words': 468}