Skip to content

Instantly share code, notes, and snippets.

@filharvey
Last active July 26, 2016 15:15
Show Gist options
  • Save filharvey/05c178772857a977dd1f8d8844f5f44b to your computer and use it in GitHub Desktop.
Save filharvey/05c178772857a977dd1f8d8844f5f44b to your computer and use it in GitHub Desktop.
'use strict';
var uri = '*';
var PokemonGO = require('./Pokemon-GO-node-api/poke.io.js');
var redis = require('./redis')();
var geo = require('georedis').initialize(redis);
// using var so you can login with multiple users
var a = new PokemonGO.Pokeio();
//http://www.whatsmygps.com/
//Set environment variables or replace placeholder text
var location = {
// type: 'name',
// name: process.env.PGO_LOCATION || 'Times Square'
type: 'coords',
coords: {
latitude: 33.9708705270028,
longitude: -118.37950852699578,
altitude: 10
}
};
var username = process.env.PGO_USERNAME || '*';
var password = process.env.PGO_PASSWORD || '*';
var provider = process.env.PGO_PROVIDER || 'google';
var target = null;
var moving = false;
var isPokemon = false;
var capturing = false;
redis.get("pb:" + username, function (err, reply)
{
if (err)
{
return;
}
if (reply)
{
var userData = JSON.parse (reply);
console.log (reply.toString ());
location.coords.latitude = userData[0];
location.coords.longitude = userData[1];
}
else
{
console.log ("no locale");
}
start ();
});
function start()
{
a.init(username, password, location, provider, function(err) {
if (err) throw err;
console.log('1[i] Current location: ' + a.playerInfo.locationName);
console.log('1[i] lat/long/alt: : ' + a.playerInfo.latitude + ' ' + a.playerInfo.longitude + ' ' + a.playerInfo.altitude);
a.GetProfile(function(err, profile) {
if (err) throw err;
console.log('1[i] Username: ' + profile.username);
console.log('1[i] Poke Storage: ' + profile.poke_storage);
console.log('1[i] Item Storage: ' + profile.item_storage);
var poke = 0;
if (profile.currency[0].amount) {
poke = profile.currency[0].amount;
}
console.log('1[i] Pokecoin: ' + poke);
console.log('1[i] Stardust: ' + profile.currency[1].amount);
a.GetInventory(function (err, inventory)
{
if(err) {
console.log(err);
}
if (inventory && inventory.inventory_delta)
{
for (var i = 0; i < inventory.inventory_delta.inventory_items.length; i++)
{
var item = inventory.inventory_delta.inventory_items[i];
var inventory_item_data = item.inventory_item_data;
var item_count = inventory_item_data.item;
// console.log (inventory_item_data);
// pokemon
if (inventory_item_data.pokemon)
{
var pokemon = inventory_item_data.pokemon;
// console.log (pokemon);
if (pokemon.pokemon_id > 0)
{
/* a.TransferPokemon (pokemon.pokemon_id, function (err, transfer) {
if (err)
{
}
console.log (transfer);
});
*/ }
else
{
console.log ("egg: " + pokemon.egg_km_walked_target + "km");
// eggd
}
}
if (inventory_item_data.player_stats)
{
var player_stats = inventory_item_data.player_stats;
if (player_stats.experience != null && player_stats.next_level_xp != null)
console.log (player_stats.experience.low + "/" + player_stats.next_level_xp.low);
}
if (inventory_item_data.pokedex_entry)
{
var pokedex_entry = inventory_item_data.pokedex_entry;
// console.log (pokedex_entry);
var pokemon = a.pokemonlist[parseInt(pokedex_entry.pokedex_entry_number)-1];
if (pokedex_entry.times_captured == null)
console.log ("pokemon: " + pokemon.name + ", seen: " + pokedex_entry.times_encountered);
else
console.log ("pokemon: " + pokemon.name + ", seen: " + pokedex_entry.times_encountered + " - captured: " + pokedex_entry.times_captured);
}
if (inventory_item_data.player_currency)
{
var player_currency = inventory_item_data.player_currency;
}
if (inventory_item_data.egg_incubators)
{
var egg_incubators = inventory_item_data.egg_incubators;
// console.log (egg_incubators);
}
if (inventory_item_data.pokemon_family)
{
var pokemon_family = inventory_item_data.pokemon_family;
// console.log (pokemon_family);
var pokemon = a.pokemonlist[parseInt(pokemon_family.family_id)-1];
console.log ("pokemon: " + pokemon.name + ", candy: " + pokemon_family.candy);
}
if (inventory_item_data.item)
{
var item = inventory_item_data.item;
// console.log (item);
var count = item.count;
var itemId = item.item;
var unseen = item.unseen;
console.log ("item: " + itemId + ", count: " + count);
}
}
}
});
setInterval(function(){
if (moving)
{
return;
}
console.log('1[i] Current location: ' + a.playerInfo.locationName);
console.log('1[i] lat/long/alt: : ' + a.playerInfo.latitude + ' ' + a.playerInfo.longitude + ' ' + a.playerInfo.altitude);
a.Heartbeat(function(err,hb)
{
if(err)
{
console.log(err);
}
for (var i = hb.cells.length - 1; i >= 0; i--)
{
if(hb.cells[i].NearbyPokemon[0])
{
var nearbyPokemon = hb.cells[i].NearbyPokemon[0];
var pokemon = a.pokemonlist[parseInt(nearbyPokemon.PokedexNumber)-1];
var encountedId = nearbyPokemon.EncounterId;
// console.log(nearbyPokemon)
console.log('1[+] There is a ' + pokemon.name + ' at ' + nearbyPokemon.DistanceMeters.toString() + ' meters');
}
if(hb.cells[i].MapPokemon[0])
{
var mapPokemon = hb.cells[i].MapPokemon[0];
var pokemon = a.pokemonlist[parseInt(mapPokemon.PokedexTypeId)-1];
var encountedId = mapPokemon.EncounterId;
var time = mapPokemon.ExpirtationTimeMs;
// console.log(mapPokemon)
var dist = getDistanceFromLatLonInKm (a.playerInfo.latitude, a.playerInfo.longitude, mapPokemon.Latitude, mapPokemon.Longitude) * 1000;
var date = new Date();
if (mapPokemon.ExpirationTimeMs < date.getTime ())
console.log('1[+] There is a ' + pokemon.name + ' at ' + mapPokemon.Latitude.toString() + ", " + mapPokemon.Longitude.toString() + " - Gone");
else
console.log('1[+] There is a ' + pokemon.name + ' at ' + mapPokemon.Latitude.toString() + ", " + mapPokemon.Longitude.toString() + " - " + dist);
if (moving == false && capturing == false && mapPokemon.ExpirationTimeMs >= date.getTime ())
{
if (dist > 10)
{
if (isPokemon == false)
target = [mapPokemon.Latitude, mapPokemon.Longitude, dist];
else if (target == null)
target = [mapPokemon.Latitude, mapPokemon.Longitude, dist];
else if (dist < target[2])
target = [mapPokemon.Latitude, mapPokemon.Longitude, dist];
isPokemon = true;
}
else
{
capturing = true;
console.log('1[+] Encountering ' + pokemon.name + ' at ' + mapPokemon.Latitude.toString() + ", " + mapPokemon.Longitude.toString() + " - " + dist);
a.EncounterPokemon(mapPokemon, function(err, encounterResponse)
{
if(err)
{
console.log(err);
}
// console.log (encounterResponse);
if (encounterResponse.EncounterStatus == 1)
{
var wildPokemon = encounterResponse.WildPokemon;
var pokemon = wildPokemon.pokemon;
var pokemonInfo = a.pokemonlist[parseInt(pokemon.PokemonId)-1];
var dist = getDistanceFromLatLonInKm (a.playerInfo.latitude, a.playerInfo.longitude, wildPokemon.Latitude, wildPokemon.Longitude) * 1000;
console.log('1[+] capturing ' + pokemonInfo.name + ':' + pokemonInfo.cp + 'cp at ' + wildPokemon.Latitude.toString() + ", " + wildPokemon.Longitude.toString() + " - " + dist);
a.CatchPokemon(wildPokemon, 1, 1.95, 0.85, 1, function(err, catchResponse)
{
if(err) {
console.log(err);
}
if (catchResponse != null)
{
console.log (catchResponse);
if (catchResponse.Status == 1)
{
}
else if (catchResponse.Status == 2) // Escape
{
}
else if (catchResponse.Status == 3) // flee
{
}
else if (catchResponse.Status == 4) // missed
{
}
}
capturing = false;
});
}
});
}
}
}
if(hb.cells[i].WildPokemon[0])
{
var wildPokemon = hb.cells[i].WildPokemon[0];
console.log (wildPokemon);
}
if (hb.cells[i].Fort[0])
{
var fort = hb.cells[i].Fort[0];
// 1 = PokeStop
// 0 = GYM
if(fort.FortType == 1)
{
var dist = getDistanceFromLatLonInKm (a.playerInfo.latitude, a.playerInfo.longitude, fort.Latitude, fort.Longitude) * 1000;
console.log('1[+] There is a PokeStop (' + fort.FortId + ') at ' + fort.Latitude.toString() + ", " + fort.Longitude.toString() + " - " + dist + ", " + fort.CooldownCompleteMs);
if (fort.Enabled)
{
var date = new Date();
if ((fort.CooldownCompleteMs == null || fort.CooldownCompleteMs < date.getTime ()) && moving == false && isPokemon == false)
{
var dist = getDistanceFromLatLonInKm (a.playerInfo.latitude, a.playerInfo.longitude, fort.Latitude, fort.Longitude) * 1000;
if (dist > 10)
{
if (target == null)
target = [fort.Latitude, fort.Longitude, dist];
else if (dist < target[2])
target = [fort.Latitude, fort.Longitude, dist];
}
else
{
a.GetFort(fort.FortId, fort.Latitude, fort.Longitude, function(err, fortresponse)
{
if(err)
{
console.log(err);
}
if(fortresponse.result == 1)
{
var items = fortresponse.items_awarded;
var eggs = fortresponse.pokemon_data_egg;
var gems = fortresponse.gems_awarded;
var xp = fortresponse.experience_awarded;
for (var i = 0; i < items.length; i++)
{
var item = items[i];
console.log (item);
}
// 1 = success
// 2 = out of range ..
console.log(fort.FortId + " used!!");
}
});
}
}
}
}
else
{
if (fort.Enabled)
{
/* a.GetGymDetails(fort.FortId, fort.Latitude, fort.Longitude, function(err, fortresponse)
{
if(err) {
console.log(err);
}
console.log (fortresponse);
});
*/ }
console.log('1[+] There is a Gym (' + fort.FortId + ') at ' + fort.Latitude.toString() + ", " + fort.Longitude.toString() + ' owned by ' + fort.Team);
}
}
}
if (target != null)
{
moving = true;
console.log ("moving to: " + target[0] + ", " + target[1] + " - dist: " + target[2]);
}
});
}, 1000);
setInterval(function()
{
// are we walking
if (target != null && moving && capturing == false)
{
var dist = getDistanceFromLatLonInKm (a.playerInfo.latitude, a.playerInfo.longitude, target[0], target[1]) * 1000;
// move towards if greater then 40 meters
if (dist > 8)
{
var moveDistance = 10;
var nextWaypointBearing = DegreeBearing(a.playerInfo.latitude, a.playerInfo.longitude, target[0], target[1]);
var pos = CreateWaypoint(a.playerInfo.latitude, a.playerInfo.longitude, moveDistance, nextWaypointBearing);
var location = {
type: 'coords',
coords: {
latitude: pos[0],
longitude: pos[1],
altitude: 10
}
};
a.SetLocation (location, function (err, loc)
{
if (err)
{
}
});
var pos = [a.playerInfo.latitude, a.playerInfo.longitude];
redis.set('pb:' + username, JSON.stringify(pos));
console.log('1[i] Current location: ' + pos + ", dist " + dist);
}
else
{
moving = false;
target = null;
isPokemon = false;
}
}
}, 1000);
});
});
}
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
function CreateWaypoint(slat, slng, distanceInMeters, bearingDegrees)
{
var distanceKm = distanceInMeters/1000.0;
var distanceRadians = distanceKm/6371; //6371 = Earth's radius in km
var bearingRadians = ToRad(bearingDegrees);
var sourceLatitudeRadians = ToRad(slat);
var sourceLongitudeRadians = ToRad(slng);
var targetLatitudeRadians = Math.asin(Math.sin(sourceLatitudeRadians)*Math.cos(distanceRadians)
+
Math.cos(sourceLatitudeRadians)*Math.sin(distanceRadians)*
Math.cos(bearingRadians));
var targetLongitudeRadians = sourceLongitudeRadians + Math.atan2(Math.sin(bearingRadians)
*Math.sin(distanceRadians)*
Math.cos(sourceLatitudeRadians),
Math.cos(distanceRadians)
- Math.sin(sourceLatitudeRadians)*Math.sin(targetLatitudeRadians));
// adjust toLonRadians to be in the range -180 to +180...
targetLongitudeRadians = (targetLongitudeRadians + 3*Math.PI)%(2*Math.PI) - Math.PI;
var ret = [ToDegrees(targetLatitudeRadians), ToDegrees(targetLongitudeRadians)];
return ret;
}
function DegreeBearing(slat, slng, tlat, tlng)
{
var dLon = ToRad(tlng - slng);
var dPhi = Math.log(
Math.tan(ToRad(tlat)/2 + Math.PI/4)/
Math.tan(ToRad(slat)/2 + Math.PI/4));
if (Math.abs(dLon) > Math.PI)
dLon = dLon > 0 ? -(2*Math.PI - dLon) : 2*Math.PI + dLon;
return ToBearing(Math.atan2(dLon, dPhi));
}
function ToBearing(radians)
{
// convert radians to degrees (as bearing: 0...360)
return (ToDegrees(radians) + 360)%360;
}
function ToDegrees(radians)
{
return radians*180/Math.PI;
}
function ToRad(degrees)
{
return degrees*(Math.PI/180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment