diff --git a/decoder/api_pokemon.go b/decoder/api_pokemon.go index 8117f052..ae66dfca 100644 --- a/decoder/api_pokemon.go +++ b/decoder/api_pokemon.go @@ -2,15 +2,17 @@ package decoder import ( "fmt" - "golbat/config" - "golbat/geo" "math" "slices" "strconv" "time" + "golbat/config" + "golbat/geo" + log "github.com/sirupsen/logrus" "github.com/tidwall/rtree" + "github.com/jellydator/ttlcache/v3" ) const earthRadiusKm = 6371 @@ -157,3 +159,42 @@ func GetOnePokemon(pokemonId uint64) *Pokemon { } return nil } + +type ApiPokemonLiveStatsResult struct { + PokemonActive int `json:"pokemon_active"` + PokemonActiveIv int `json:"pokemon_active_iv"` + PokemonActive100iv int `json:"pokemon_active_100iv"` + PokemonActiveShiny int `json:"pokemon_active_shiny"` +} + +func GetLiveStatsPokemon() *ApiPokemonLiveStatsResult { + start := time.Now() + now := time.Now().Unix() + + liveStats := &ApiPokemonLiveStatsResult{ + 0, + 0, + 0, + 0, + } + + pokemonCache.Range(func(pokemonCacheEntry *ttlcache.Item[string, Pokemon]) bool { + pokemon := pokemonCacheEntry.Value() + if int64(valueOrMinus1(pokemon.ExpireTimestamp)) > now { + liveStats.PokemonActive++ + if !pokemon.Iv.IsZero() { + liveStats.PokemonActiveIv++ + } + if bool(pokemon.Shiny.ValueOrZero()) { + liveStats.PokemonActiveShiny++ + } + if int(pokemon.Iv.ValueOrZero()) == 100 { + liveStats.PokemonActive100iv++ + } + } + return true + }) + + log.Infof("apiLiveStats - %d pokemon_active, %d pokemon_active_iv, %d pokemon_active_100iv, %d pokemon_active_shiny, total time %s", liveStats.PokemonActive, liveStats.PokemonActiveIv, liveStats.PokemonActive100iv, liveStats.PokemonActiveShiny, time.Since(start)) + return liveStats +} diff --git a/main.go b/main.go index f7b82a35..8f96b75e 100644 --- a/main.go +++ b/main.go @@ -271,6 +271,7 @@ func main() { apiGroup.POST("/pokemon/scan", PokemonScan) apiGroup.POST("/pokemon/v2/scan", PokemonScan2) apiGroup.POST("/pokemon/search", PokemonSearch) + apiGroup.GET("/pokemon/livestats", PokemonLiveStats) apiGroup.GET("/devices/all", GetDevices) diff --git a/routes.go b/routes.go index 94df24c1..e2d99c13 100644 --- a/routes.go +++ b/routes.go @@ -478,3 +478,8 @@ func GetPokestop(c *gin.Context) { func GetDevices(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"devices": GetAllDevices()}) } + +func PokemonLiveStats(c *gin.Context) { + res := decoder.GetLiveStatsPokemon() + c.JSON(http.StatusAccepted, res) +}