Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Commit

Permalink
Use correct altitude for given GPS coordinates (#1155)
Browse files Browse the repository at this point in the history
* Use correct altitude

* Rebase (#1)

* Updated explanation in several files (#1152)

* Updated some of the readme files (API/FAQ/USAGE and ISSUE TEMPLATE)

* Additional information

* Fixed some words with feedback from jabbink

* Lowercase "ptc", to be sure ^-^

* Some more clarification

* Fix #1147 (#1157)

* Ensure template correctness (#1154)

* fixed typo in template file

* wrote tests for json-template

* fixed json-template with help from the new TestSettings

* Separated json credential testing from properties testing

* Use correct altitude(and cache it)

* Rebase (#2)

* Updated explanation in several files (#1152)

* Updated some of the readme files (API/FAQ/USAGE and ISSUE TEMPLATE)

* Additional information

* Fixed some words with feedback from jabbink

* Lowercase "ptc", to be sure ^-^

* Some more clarification

* Fix #1147 (#1157)

* Ensure template correctness (#1154)

* fixed typo in template file

* wrote tests for json-template

* fixed json-template with help from the new TestSettings

* Separated json credential testing from properties testing

* Cleaner string concatenation

* Use Google API first(without API key), when it hits the rate limit, use mapzen

* More try block coverage

* Added logging if requests fail, removed unused import

* More detailed log message
  • Loading branch information
KyleBoyer authored and Sieberkev committed Aug 19, 2016
1 parent a1c4eb0 commit a4acaf8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
2 changes: 2 additions & 0 deletions authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
@shilch

@Sieberkev

@KyleBoyer
17 changes: 12 additions & 5 deletions src/main/kotlin/ink/abb/pogo/scraper/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import kotlin.concurrent.thread
import com.fasterxml.jackson.databind.ObjectMapper

class Bot(val api: PokemonGo, val settings: Settings) {

private var runningLatch = CountDownLatch(0)
var prepareWalkBack = AtomicBoolean(false)
var walkBackLock = AtomicBoolean(true)

var altitudeCache: MutableMap<String, Double> =
try {
ObjectMapper().readValue(File("altitude_cache.json").readText(), MutableMap::class.java) as MutableMap<String, Double>
} catch (ex: Exception){
mutableMapOf()
}
lateinit private var phaser: Phaser

var ctx = Context(
api,
api.playerProfile,
Expand All @@ -54,7 +59,8 @@ class Bot(val api: PokemonGo, val settings: Settings) {
mutableSetOf(),
SocketServer(),
Pair(AtomicBoolean(settings.catchPokemon), AtomicBoolean(false)),
settings.restApiPassword
settings.restApiPassword,
altitudeCache
)

@Synchronized
Expand Down Expand Up @@ -226,11 +232,12 @@ class Bot(val api: PokemonGo, val settings: Settings) {
if (!isRunning()) return

if(settings.saveLocationOnShutdown) {
Log.normal("Saving last location ...")
Log.normal("Saving last location...")
settings.longitude = ctx.lng.get()
settings.latitude = ctx.lat.get()
}

Log.normal("Saving cache file...")
ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File("altitude_cache.json"), ctx.s2Cache)
val socketServerStopLatch = CountDownLatch(1)
thread {
Log.red("Stopping SocketServer...")
Expand Down
40 changes: 39 additions & 1 deletion src/main/kotlin/ink/abb/pogo/scraper/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

package ink.abb.pogo.scraper

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.util.concurrent.AtomicDouble
import com.pokegoapi.api.PokemonGo
import com.pokegoapi.api.player.PlayerProfile
import ink.abb.pogo.scraper.gui.SocketServer
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import java.time.LocalDateTime
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import com.pokegoapi.google.common.geometry.S2LatLng
import com.pokegoapi.google.common.geometry.S2CellId
import ink.abb.pogo.scraper.util.Log

data class Context(
val api: PokemonGo,
Expand All @@ -35,10 +41,42 @@ data class Context(
val pokemonInventoryFullStatus: Pair<AtomicBoolean, AtomicBoolean>,

var restApiPassword: String,
var s2Cache: MutableMap<String, Double>,
var restApiToken: String = "",

val walking: AtomicBoolean = AtomicBoolean(false),

val pauseWalking: AtomicBoolean = AtomicBoolean(false)

)
) {
fun getAltitude(latitude: Double, longitude: Double): Double {
val rand = (Math.random() * 3) + 1
val cellId = S2CellId.fromLatLng(S2LatLng.fromDegrees(latitude, longitude)).parent(15).id().toString()
if (this.s2Cache.containsKey(cellId) && this.s2Cache[cellId] != null) {
return this.s2Cache[cellId]!! + rand
}
var elevation = 10.0
try {
val url = HttpUrl.parse("https://maps.googleapis.com/maps/api/elevation/json?locations=$latitude,$longitude&sensor=true").newBuilder().build()
val request = okhttp3.Request.Builder().url(url).build()
val result: Map<*, *>
result = ObjectMapper().readValue(OkHttpClient().newCall(request).execute().body().string(), Map::class.java)
val results = result["results"] as List<*>
val firstResult = results[0] as Map<*, *>
elevation = firstResult["elevation"].toString().toDouble()
this.s2Cache[cellId] = elevation
} catch(ex: Exception) {
val url = HttpUrl.parse("https://elevation.mapzen.com/height?json={\"shape\":[{\"lat\":$latitude,\"lon\":$longitude}]}").newBuilder().build()
val request = okhttp3.Request.Builder().url(url).build()
try {
val result: Map<*, *>
result = ObjectMapper().readValue(OkHttpClient().newCall(request).execute().body().string(), Map::class.java)
elevation = result["height"].toString().replace("[^\\d\\-]".toRegex(), "").toDouble()
this.s2Cache[cellId] = elevation
} catch (exi: Exception) {
Log.red("Can't get elevation, using ${elevation + rand}...")
}
}
return elevation + rand
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class BotController {
ctx.lat.set(latitude)
ctx.lng.set(longitude)

ctx.api.setLocation(latitude, longitude, 10.0)
ctx.api.setLocation(latitude, longitude, ctx.getAltitude(latitude, longitude))

ctx.pauseWalking.set(false)

Expand All @@ -310,13 +310,13 @@ class BotController {
@RequestMapping(value = "/bot/{name}/pokedex", method = arrayOf(RequestMethod.GET))
fun getPokedex(@PathVariable name: String): List<PokedexEntry> {

var pokedex = mutableListOf<PokedexEntry>()
val pokedex = mutableListOf<PokedexEntry>()
val api = service.getBotContext(name).api
var i: Int = 1

while (i < 151) {
i++
var entry: PokedexEntryOuterClass.PokedexEntry? = api.inventories.pokedex.getPokedexEntry(PokemonIdOuterClass.PokemonId.forNumber(i))
val entry: PokedexEntryOuterClass.PokedexEntry? = api.inventories.pokedex.getPokedexEntry(PokemonIdOuterClass.PokemonId.forNumber(i))
entry ?: continue

pokedex.add(PokedexEntry().buildFromEntry(entry))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CatchOneNearbyPokemon : Task {
return
}
Log.green("Found pokemon ${catchablePokemon.pokemonId}")
ctx.api.setLocation(ctx.lat.get(), ctx.lng.get(), 0.0)
ctx.api.setLocation(ctx.lat.get(), ctx.lng.get(), ctx.getAltitude(ctx.lat.get(), ctx.lng.get()))

val encounterResult = catchablePokemon.encounterPokemon()
val wasFromLure = encounterResult is DiskEncounterResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GetMapRandomDirection : Task {
val lng = ctx.lng.get() + randomLatLng()

if (settings.displayKeepalive) Log.normal("Getting map of ($lat, $lng)")
ctx.api.setLocation(lat, lng, 0.0)
ctx.api.setLocation(lat, lng, ctx.getAltitude(lat, lng))
}

fun randomLatLng(): Double {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LootOneNearbyPokestop(val sortedPokestops: List<Pokestop>, val lootTimeout
override fun run(bot: Bot, ctx: Context, settings: Settings) {
// STOP WALKING! until loot is done
ctx.pauseWalking.set(true)
ctx.api.setLocation(ctx.lat.get(), ctx.lng.get(), 0.0)
ctx.api.setLocation(ctx.lat.get(), ctx.lng.get(), ctx.getAltitude(ctx.lat.get(), ctx.lng.get()))
val nearbyPokestops = sortedPokestops.filter {
it.canLoot(lootTimeouts = lootTimeouts, api = ctx.api)
}
Expand Down

0 comments on commit a4acaf8

Please sign in to comment.