<commit_before>var vorpal = require('vorpal')();
var async = require('async');
var _ = require('underscore');
var Int64 = require('node-int64');
var Table = require('cli-table');
var stdin = process.openStdin();
var PokemonGO = require('./lib/poke.io.js');

var client = new PokemonGO.Pokeio();

var debug = false;
var initialized = false;

var location = {
    'type': 'name',
    'name': 'Oakland'
};

var nearbyPokemons = [];

console.log("Welcome to PokemonGO CLI!");

vorpal
    .command("init <username> <password> <provider> [initLocation]")
    .description('Initializes client.')
    .action(init);

vorpal
    .command("scan [lat_lon]")
    .description("Scan for nearby Pokemons. Catchable Pokemons will contain an index to use with the catch command.")
    .action(scan);

vorpal
    .command("catch [index]")
    .description("Tries to capture previously seen nearby Pokemon while scanning.")
    .action(capture);

vorpal
    .command("inventory <list> [sortBy]")
    .description("Displays user Pokemons or Items. You can sort the Pokemon list by utiling the 'sortBy' param and one of the followings: '#', 'cp', 'name' or 'recent'.")
    .action(showInventory);

vorpal
    .command("profile")
    .description("Displays user profile information.")
    .action(showProfile);

vorpal
    .delimiter('>')
    .show();

function init(inputArgs, done) {
    var username = inputArgs.username;
    var password = inputArgs.password;
    var provider = inputArgs.provider;

    if (inputArgs.initLocation) {
        location = {
            'type': 'name',
            'name': inputArgs.initLocation
        };
    }

    client.init(username, password, location, provider, function (err) {
        if (err) {
            console.log("[e] Error initializing.");
            console.log(JSON.stringify(err));
            return done();
        }

        initialized = true;
        console.log('[i] Current location: ' + client.playerInfo.locationName);
        console.log('[i] lat/long/alt: : ' + client.playerInfo.latitude + ' ' + client.playerInfo.longitude + ' ' + client.playerInfo.altitude);
    
        return done();
    });
}

function showProfile(inputArgs, done) {
    if (!initialized) {
        console.log("[e] Error getting profile.");
        console.log("Client not initialized.");
        return done();
    }

    client.GetProfile(function (err, profile) {
        if (err) {
            console.log("[e] Error getting profile.");
            console.log(err);
            return done();
        }

        console.log('[i] Username: ' + profile.username);
        console.log('[i] Poke Storage: ' + profile.poke_storage);
        console.log('[i] Item Storage: ' + profile.item_storage);

        var poke = 0;
        if (profile.currency[0].amount) {
            poke = profile.currency[0].amount;
        }

        console.log('[i] Pokecoin: ' + poke);
        console.log('[i] Stardust: ' + profile.currency[1].amount);

        return done();
    });
}

function scan(inputArgs, done) {
    if (!initialized) {
        console.log("[e] Error scanning.");
        console.log("Client not initialized.");
        return done();
    }

    async.waterfall([
        function (callback) {
            if (!('lat_lon' in inputArgs)) {
                return callback(null, location);;
            }

            try {
                var commaSplit = inputArgs.lat_lon.split(',');
                var latitude = commaSplit[0].trim();
                var longitude = commaSplit[1].trim();
                location = {
                    'type': 'coords',
                    'coords': {
                        'latitude': parseFloat(latitude),
                        'longitude': parseFloat(longitude),
                        'altitude': 0
                    }
                }

                return callback(null, location);
            } catch (err) {
                callback("Wrong scan location format. Try 'scan 37.7749,-122.4194'");
                return;
            }
        },
        client.SetLocation,
        function (newLocation, callback) {
            console.log('[i] Scanning lat/long/alt: : ' + client.playerInfo.latitude + ' ' + client.playerInfo.longitude + ' ' + client.playerInfo.altitude);
            callback(null);
        },
        client.Heartbeat,
        function (scan, callback) {
            for (var i = scan.cells.length - 1; i >= 0; i--) {
                if (scan.cells[i].NearbyPokemon[0]) {
                    var pokemon = client.pokemonlist[parseInt(scan.cells[i].NearbyPokemon[0].PokedexNumber)-1];
                    console.log('[+] There is a ' + pokemon.name + ' at ' + scan.cells[i].NearbyPokemon[0].DistanceMeters.toString() + ' meters');
                }
            }

            var index = 0;
            nearbyPokemons = [];
            for (i = scan.cells.length - 1; i >= 0; i--) {
                for (var j = scan.cells[i].WildPokemon.length - 1; j >= 0; j--) {
                    var currentPokemon = scan.cells[i].WildPokemon[j];
                    var pokedexInfo = client.pokemonlist[parseInt(currentPokemon.pokemon.PokemonId)-1];
                    nearbyPokemons.push({'pokemon': currentPokemon, 'pokedex': pokedexInfo});
                    console.log('[' + index + '] There is a ' + pokedexInfo.name + ' near! I can try to catch it!');
                    index++;
                }
            }

            callback(null);
        }
    ], function (err) {
        if (err) {
            console.log("[e] Error scanning.");
            console.log(err);
        }

        return done();
    });
}

function capture(inputArgs, done) {
    if (!initialized) {
        console.log("[e] Error catching pokemon.");
        console.log("Client not initialized.");
        return done();
    }

    var index = inputArgs[1];
    if (index < 0 || index >= nearbyPokemons.length) {
        console.log("[e] Error catching pokemon.");
        console.log("Invalid pokemon index.");<commit_msg>Fixing bug in Vorpal refactor, catch argument wrongly parsed<commit_after>var vorpal = require('vorpal')();
var async = require('async');
var _ = require('underscore');
var Int64 = require('node-int64');
var Table = require('cli-table');
var stdin = process.openStdin();
var PokemonGO = require('./lib/poke.io.js');

var client = new PokemonGO.Pokeio();

var debug = false;
var initialized = false;

var location = {
    'type': 'name',
    'name': 'Oakland'
};

var nearbyPokemons = [];

console.log("Welcome to PokemonGO CLI!");

vorpal
    .command("init <username> <password> <provider> [initLocation]")
    .description('Initializes client.')
    .action(init);

vorpal
    .command("scan [lat_lon]")
    .description("Scan for nearby Pokemons. Catchable Pokemons will contain an index to use with the catch command.")
    .action(scan);

vorpal
    .command("catch [index]")
    .description("Tries to capture previously seen nearby Pokemon while scanning.")
    .action(capture);

vorpal
    .command("inventory <list> [sortBy]")
    .description("Displays user Pokemons or Items. You can sort the Pokemon list by utiling the 'sortBy' param and one of the followings: '#', 'cp', 'name' or 'recent'.")
    .action(showInventory);

vorpal
    .command("profile")
    .description("Displays user profile information.")
    .action(showProfile);

vorpal
    .delimiter('>')
    .show();

function init(inputArgs, done) {
    var username = inputArgs.username;
    var password = inputArgs.password;
    var provider = inputArgs.provider;

    if (inputArgs.initLocation) {
        location = {
            'type': 'name',
            'name': inputArgs.initLocation
        };
    }

    client.init(username, password, location, provider, function (err) {
        if (err) {
            console.log("[e] Error initializing.");
            console.log(JSON.stringify(err));
            return done();
        }

        initialized = true;
        console.log('[i] Current location: ' + client.playerInfo.locationName);
        console.log('[i] lat/long/alt: : ' + client.playerInfo.latitude + ' ' + client.playerInfo.longitude + ' ' + client.playerInfo.altitude);
    
        return done();
    });
}

function showProfile(inputArgs, done) {
    if (!initialized) {
        console.log("[e] Error getting profile.");
        console.log("Client not initialized.");
        return done();
    }

    client.GetProfile(function (err, profile) {
        if (err) {
            console.log("[e] Error getting profile.");
            console.log(err);
            return done();
        }

        console.log('[i] Username: ' + profile.username);
        console.log('[i] Poke Storage: ' + profile.poke_storage);
        console.log('[i] Item Storage: ' + profile.item_storage);

        var poke = 0;
        if (profile.currency[0].amount) {
            poke = profile.currency[0].amount;
        }

        console.log('[i] Pokecoin: ' + poke);
        console.log('[i] Stardust: ' + profile.currency[1].amount);

        return done();
    });
}

function scan(inputArgs, done) {
    if (!initialized) {
        console.log("[e] Error scanning.");
        console.log("Client not initialized.");
        return done();
    }

    async.waterfall([
        function (callback) {
            if (!('lat_lon' in inputArgs)) {
                return callback(null, location);;
            }

            try {
                var commaSplit = inputArgs.lat_lon.split(',');
                var latitude = commaSplit[0].trim();
                var longitude = commaSplit[1].trim();
                location = {
                    'type': 'coords',
                    'coords': {
                        'latitude': parseFloat(latitude),
                        'longitude': parseFloat(longitude),
                        'altitude': 0
                    }
                }

                return callback(null, location);
            } catch (err) {
                callback("Wrong scan location format. Try 'scan 37.7749,-122.4194'");
                return;
            }
        },
        client.SetLocation,
        function (newLocation, callback) {
            console.log('[i] Scanning lat/long/alt: : ' + client.playerInfo.latitude + ' ' + client.playerInfo.longitude + ' ' + client.playerInfo.altitude);
            callback(null);
        },
        client.Heartbeat,
        function (scan, callback) {
            for (var i = scan.cells.length - 1; i >= 0; i--) {
                if (scan.cells[i].NearbyPokemon[0]) {
                    var pokemon = client.pokemonlist[parseInt(scan.cells[i].NearbyPokemon[0].PokedexNumber)-1];
                    console.log('[+] There is a ' + pokemon.name + ' at ' + scan.cells[i].NearbyPokemon[0].DistanceMeters.toString() + ' meters');
                }
            }

            var index = 0;
            nearbyPokemons = [];
            for (i = scan.cells.length - 1; i >= 0; i--) {
                for (var j = scan.cells[i].WildPokemon.length - 1; j >= 0; j--) {
                    var currentPokemon = scan.cells[i].WildPokemon[j];
                    var pokedexInfo = client.pokemonlist[parseInt(currentPokemon.pokemon.PokemonId)-1];
                    nearbyPokemons.push({'pokemon': currentPokemon, 'pokedex': pokedexInfo});
                    console.log('[' + index + '] There is a ' + pokedexInfo.name + ' near! I can try to catch it!');
                    index++;
                }
            }

            callback(null);
        }
    ], function (err) {
        if (err) {
            console.log("[e] Error scanning.");
            console.log(err);
        }

        return done();
    });
}

function capture(inputArgs, done) {
    if (!initialized) {
        console.log("[e] Error catching pokemon.");
        console.log("Client not initialized.");
        return done();
    }

    var index = inputArgs.index;
    if (index < 0 || index >= nearbyPokemons.length) {
        console.log("[e] Error catching pokemon.");
        console.log("Invalid pokemon index.");
{'fraction_non_alphanumeric': 0.14375725418670204, 'fraction_numerical': 0.006217874316033825, 'mean_word_length': 2.0082294264339153, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '686848', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3452, 'n_words': 801}