Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions decoder/api_pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}