Skip to content

Instantly share code, notes, and snippets.

@gallexme
Last active July 24, 2016 18:43
Show Gist options
  • Save gallexme/43512b54fa6b4dfdd10236d25899585d to your computer and use it in GitHub Desktop.
Save gallexme/43512b54fa6b4dfdd10236d25899585d to your computer and use it in GitHub Desktop.
import PokeAPI from 'pokemongo-api';
const Poke = new PokeAPI();
function lerp(a, b, t) {
var len = a.length;
if (b.length != len) return;
var x = [];
for (var i = 0; i < len; i++)
x.push(a[i] + t * (b[i] - a[i]));
return x;
}
function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms));
}
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1 / 180
var radlat2 = Math.PI * lat2 / 180
var theta = lon1 - lon2
var radtheta = Math.PI * theta / 180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180 / Math.PI
dist = dist * 60 * 1.1515
if (unit == "K") {
dist = dist * 1.609344
}
if (unit == "N") {
dist = dist * 0.8684
}
return dist
}
async function walkTo(lat, long, Poke,api) {
let movementSpeed = 4 //14 km/h 4m /s
let player = api.player;
let playerCoords = player.coords;
let startPlayerCoords = playerCoords;
console.log(playerCoords);
console.log(lat,long);
let calculatedDistance = distance(startPlayerCoords[0], startPlayerCoords[1], lat, long, "K") * 1000; //TO M
console.log("GOT TO WALK: "+calculatedDistance+" M");
let startTime = new Date().getTime();
console.log("Starting Time"+startTime);
if (calculatedDistance > 40) {
let currentStep = 0;
let stepCount = Math.round(calculatedDistance / 100);
while (currentStep < stepCount) {
console.log("WALKING STEP: "+currentStep/stepCount);
_ = await Poke.GetMapObjects();
currentStep += 1;
let newCoords = lerp(startPlayerCoords, [lat, long], calculatedDistance, currentStep / stepCount);
let location = {
type: 'coords',
coords: {
latitude: newCoords[0],
longitude: newCoords[1],
},
};
await api.player.setLocation(location);
playerCoords = api.player.coords;
console.log("waiting: "+((calculatedDistance / movementSpeed) / stepCount)*1000+" MS");
await sleep(((calculatedDistance / movementSpeed) / stepCount)*1000);
}
}
let endTime = new Date().getTime();
console.log("ENDTIME"+endTime,"REQUIRED TIME"+(endTime-startTime));
}
async function init() {
//yep, we do need to login..
const api = await Poke.login(username, password, location, provider)
// just update the profile...
async function loop() {
let player = await Poke.GetPlayer();
// get map objects..
let cells = await Poke.GetMapObjects()
await Poke.player.walkAround()
for (let cell of cells) {
if (cell.catchable_pokemons.length > 0) {
//we have wild pokemons
console.log({
catchable_pokemons: cell.catchable_pokemons
})
}
//wild pokemons
if (cell.wild_pokemons.length > 0) {
console.log({
wild_pokemons: cell.wild_pokemons
})
}
//forts
if (cell.forts.length > 0) {
for (fort of cell.forts) {
switch (fort.type) {
case 1: //POKESTOP
console.log("WALKING TO POKESTOP");
let _ = await walkTo(fort.latitude, fort.longitude, Poke,api);
console.log("WALKED TO POKESTOP");
console.log("SEARCHING TO POKESTOP");
let fortResult = await Poke.FortSearch({
fort_id: fort.id,
fort_latitude: fort.latitude,
fort_longitude: fort.longitude
});
console.log("SEARCHED POKESTOP");
console.log(fortResult);
break;
case 0: //GYM
break;
}
}
}
if (cell.catchable_pokemons.length > 0) {
for (var pokemon of cell.catchable_pokemons) {
try {
let encounterResult = await Poke.EncounterPokemon(pokemon);
let catchPokemonResult = await Poke.CatchPokemon(pokemon);
console.log({
encounterResult: encounterResult,
catchPokemonResult: catchPokemonResult
});
} catch (error) {
console.log(error);
}
}
console.log({
catchable_pokemons: cell.catchable_pokemons
})
}
//Done...
//TODO: We need to move.. like a human..!
}
}
while(true) {
await sleep(2000);
await loop();
}
}
init().catch(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment