Skip to content

Commit

Permalink
Merge branch 'UnownHash:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ReuschelCGN authored Dec 23, 2024
2 parents 92364e1 + b876ecb commit bea04ef
Show file tree
Hide file tree
Showing 14 changed files with 1,042 additions and 439 deletions.
35 changes: 18 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import (
)

type configDefinition struct {
Port int `koanf:"port"`
GrpcPort int `koanf:"grpc_port"`
Webhooks []Webhook `koanf:"webhooks"`
Database database `koanf:"database"`
Logging logging `koanf:"logging"`
Sentry sentry `koanf:"sentry"`
Pyroscope pyroscope `koanf:"pyroscope"`
Prometheus Prometheus `koanf:"prometheus"`
PokemonMemoryOnly bool `koanf:"pokemon_memory_only"`
TestFortInMemory bool `koanf:"test_fort_in_memory"`
Cleanup cleanup `koanf:"cleanup"`
RawBearer string `koanf:"raw_bearer"`
ApiSecret string `koanf:"api_secret"`
Pvp pvp `koanf:"pvp"`
Koji koji `koanf:"koji"`
Tuning tuning `koanf:"tuning"`
ScanRules []scanRule `koanf:"scan_rules"`
Port int `koanf:"port"`
GrpcPort int `koanf:"grpc_port"`
Webhooks []Webhook `koanf:"webhooks"`
Database database `koanf:"database"`
Logging logging `koanf:"logging"`
Sentry sentry `koanf:"sentry"`
Pyroscope pyroscope `koanf:"pyroscope"`
Prometheus Prometheus `koanf:"prometheus"`
PokemonMemoryOnly bool `koanf:"pokemon_memory_only"`
PokemonInternalToDb bool `koanf:"pokemon_internal_to_db"`
TestFortInMemory bool `koanf:"test_fort_in_memory"`
Cleanup cleanup `koanf:"cleanup"`
RawBearer string `koanf:"raw_bearer"`
ApiSecret string `koanf:"api_secret"`
Pvp pvp `koanf:"pvp"`
Koji koji `koanf:"koji"`
Tuning tuning `koanf:"tuning"`
ScanRules []scanRule `koanf:"scan_rules"`
}

func (configDefinition configDefinition) GetWebhookInterval() time.Duration {
Expand Down
38 changes: 18 additions & 20 deletions decoder/api_pokemon_scan_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,29 +275,27 @@ func GetPokemonInArea(retrieveParameters ApiPokemonScan) []*ApiPokemonResult {
pokemon := pokemonCacheEntry.Value()

apiPokemon := ApiPokemonResult{
Id: pokemon.Id,
PokestopId: pokemon.PokestopId,
SpawnId: pokemon.SpawnId,
Lat: pokemon.Lat,
Lon: pokemon.Lon,
Weight: pokemon.Weight,
Size: pokemon.Size,
Height: pokemon.Height,
ExpireTimestamp: pokemon.ExpireTimestamp,
Updated: pokemon.Updated,
PokemonId: pokemon.PokemonId,
Move1: pokemon.Move1,
Move2: pokemon.Move2,
Gender: pokemon.Gender,
Cp: pokemon.Cp,
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
//not IvInactive
Id: pokemon.Id,
PokestopId: pokemon.PokestopId,
SpawnId: pokemon.SpawnId,
Lat: pokemon.Lat,
Lon: pokemon.Lon,
Weight: pokemon.Weight,
Size: pokemon.Size,
Height: pokemon.Height,
ExpireTimestamp: pokemon.ExpireTimestamp,
Updated: pokemon.Updated,
PokemonId: pokemon.PokemonId,
Move1: pokemon.Move1,
Move2: pokemon.Move2,
Gender: pokemon.Gender,
Cp: pokemon.Cp,
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
Iv: pokemon.Iv,
Form: pokemon.Form,
Level: pokemon.Level,
EncounterWeather: pokemon.EncounterWeather, //? perhaps do not include
Weather: pokemon.Weather,
Costume: pokemon.Costume,
FirstSeenTimestamp: pokemon.FirstSeenTimestamp,
Expand Down
40 changes: 18 additions & 22 deletions decoder/api_pokemon_scan_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,27 @@ func GetPokemonInArea2(retrieveParameters ApiPokemonScan2) []*ApiPokemonResult {
}

apiPokemon := ApiPokemonResult{
Id: pokemon.Id,
PokestopId: pokemon.PokestopId,
SpawnId: pokemon.SpawnId,
Lat: pokemon.Lat,
Lon: pokemon.Lon,
Weight: pokemon.Weight,
Size: pokemon.Size,
Height: pokemon.Height,
ExpireTimestamp: pokemon.ExpireTimestamp,
Updated: pokemon.Updated,
PokemonId: pokemon.PokemonId,
Move1: pokemon.Move1,
Move2: pokemon.Move2,
Gender: pokemon.Gender,
Cp: pokemon.Cp,
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
//not IvInactive
Id: pokemon.Id,
PokestopId: pokemon.PokestopId,
SpawnId: pokemon.SpawnId,
Lat: pokemon.Lat,
Lon: pokemon.Lon,
Weight: pokemon.Weight,
Size: pokemon.Size,
Height: pokemon.Height,
ExpireTimestamp: pokemon.ExpireTimestamp,
Updated: pokemon.Updated,
PokemonId: pokemon.PokemonId,
Move1: pokemon.Move1,
Move2: pokemon.Move2,
Gender: pokemon.Gender,
Cp: pokemon.Cp,
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
Iv: pokemon.Iv,
Form: pokemon.Form,
Level: pokemon.Level,
EncounterWeather: pokemon.EncounterWeather, //? perhaps do not include
Weather: pokemon.Weather,
Costume: pokemon.Costume,
FirstSeenTimestamp: pokemon.FirstSeenTimestamp,
Expand Down Expand Up @@ -394,11 +392,9 @@ func GrpcGetPokemonInArea2(retrieveParameters *pb.PokemonScanRequest) []*pb.Poke
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
//not IvInactive
Iv: pokemon.Iv,
Form: pokemon.Form,
Level: pokemon.Level,
EncounterWeather: pokemon.EncounterWeather, //? perhaps do not include
Weather: pokemon.Weather,
Costume: pokemon.Costume,
FirstSeenTimestamp: pokemon.FirstSeenTimestamp,
Expand Down
59 changes: 31 additions & 28 deletions decoder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
)

type RawFortData struct {
Cell uint64
Data *pogo.PokemonFortProto
Cell uint64
Data *pogo.PokemonFortProto
Timestamp int64
}

type RawStationData struct {
Expand All @@ -35,22 +36,19 @@ type RawStationData struct {
type RawWildPokemonData struct {
Cell uint64
Data *pogo.WildPokemonProto
Timestamp uint64
Timestamp int64
}

type RawNearbyPokemonData struct {
Cell uint64
Data *pogo.NearbyPokemonProto
Cell uint64
Data *pogo.NearbyPokemonProto
Timestamp int64
}

type RawMapPokemonData struct {
Cell uint64
Data *pogo.MapPokemonProto
}

type RawClientWeatherData struct {
Cell int64
Data *pogo.ClientWeatherProto
Cell uint64
Data *pogo.MapPokemonProto
Timestamp int64
}

type webhooksSenderInterface interface {
Expand Down Expand Up @@ -255,7 +253,7 @@ func UpdateFortBatch(ctx context.Context, db db.DbDetails, scanParameters ScanPa
if pokestop == nil {
pokestop = &Pokestop{}
}
pokestop.updatePokestopFromFort(fort.Data, fort.Cell)
pokestop.updatePokestopFromFort(fort.Data, fort.Cell, fort.Timestamp/1000)
savePokestopRecord(ctx, db, pokestop)

incidents := fort.Data.PokestopDisplays
Expand Down Expand Up @@ -330,20 +328,25 @@ func UpdateStationBatch(ctx context.Context, db db.DbDetails, scanParameters Sca
}
}

func UpdatePokemonBatch(ctx context.Context, db db.DbDetails, scanParameters ScanParameters, wildPokemonList []RawWildPokemonData, nearbyPokemonList []RawNearbyPokemonData, mapPokemonList []RawMapPokemonData, username string) {
func UpdatePokemonBatch(ctx context.Context, db db.DbDetails, scanParameters ScanParameters, wildPokemonList []RawWildPokemonData, nearbyPokemonList []RawNearbyPokemonData, mapPokemonList []RawMapPokemonData, weather []*pogo.ClientWeatherProto, username string) {
weatherLookup := make(map[int64]pogo.GameplayWeatherProto_WeatherCondition)
for _, weatherProto := range weather {
weatherLookup[weatherProto.S2CellId] = weatherProto.GameplayWeather.GameplayCondition
}

for _, wild := range wildPokemonList {
encounterId := strconv.FormatUint(wild.Data.EncounterId, 10)
pokemonMutex, _ := pokemonStripedMutex.GetLock(encounterId)
pokemonMutex.Lock()

spawnpointUpdateFromWild(ctx, db, wild.Data, int64(wild.Timestamp))
spawnpointUpdateFromWild(ctx, db, wild.Data, wild.Timestamp)

if scanParameters.ProcessWild {
pokemon, err := getOrCreatePokemonRecord(ctx, db, encounterId)
if err != nil {
log.Errorf("getOrCreatePokemonRecord: %s", err)
} else {
updateTime := int64(wild.Timestamp / 1000)
updateTime := wild.Timestamp / 1000
if pokemon.isNewRecord() || pokemon.wildSignificantUpdate(wild.Data, updateTime) {
go func(wildPokemon *pogo.WildPokemonProto, cellId int64, timestampMs int64) {
time.Sleep(15 * time.Second)
Expand All @@ -361,11 +364,11 @@ func UpdatePokemonBatch(ctx context.Context, db db.DbDetails, scanParameters Sca
if pokemon.wildSignificantUpdate(wildPokemon, updateTime) && pokemon.Updated.ValueOrZero() < updateTime {
log.Debugf("DELAYED UPDATE: Updating pokemon %s from wild", encounterId)

pokemon.updateFromWild(ctx, db, wildPokemon, cellId, timestampMs, username)
savePokemonRecordAsAtTime(ctx, db, pokemon, updateTime)
pokemon.updateFromWild(ctx, db, wildPokemon, cellId, weatherLookup, timestampMs, username)
savePokemonRecordAsAtTime(ctx, db, pokemon, false, updateTime)
}
}
}(wild.Data, int64(wild.Cell), int64(wild.Timestamp))
}(wild.Data, int64(wild.Cell), wild.Timestamp)
}
}
}
Expand All @@ -382,8 +385,8 @@ func UpdatePokemonBatch(ctx context.Context, db db.DbDetails, scanParameters Sca
if err != nil {
log.Printf("getOrCreatePokemonRecord: %s", err)
} else {
pokemon.updateFromNearby(ctx, db, nearby.Data, int64(nearby.Cell), username)
savePokemonRecord(ctx, db, pokemon)
pokemon.updateFromNearby(ctx, db, nearby.Data, int64(nearby.Cell), weatherLookup, nearby.Timestamp, username)
savePokemonRecordAsAtTime(ctx, db, pokemon, false, nearby.Timestamp/1000)
}

pokemonMutex.Unlock()
Expand All @@ -399,33 +402,33 @@ func UpdatePokemonBatch(ctx context.Context, db db.DbDetails, scanParameters Sca
if err != nil {
log.Printf("getOrCreatePokemonRecord: %s", err)
} else {
pokemon.updateFromMap(ctx, db, mapPokemon.Data, int64(mapPokemon.Cell), username)
pokemon.updateFromMap(ctx, db, mapPokemon.Data, int64(mapPokemon.Cell), weatherLookup, mapPokemon.Timestamp, username)
storedDiskEncounter := diskEncounterCache.Get(encounterId)
if storedDiskEncounter != nil {
diskEncounter := storedDiskEncounter.Value()
diskEncounterCache.Delete(encounterId)
pokemon.updatePokemonFromDiskEncounterProto(ctx, db, diskEncounter, username)
log.Infof("Processed stored disk encounter")
//log.Infof("Processed stored disk encounter")
}
savePokemonRecord(ctx, db, pokemon)
savePokemonRecordAsAtTime(ctx, db, pokemon, false, mapPokemon.Timestamp/1000)
}
pokemonMutex.Unlock()
}
}

func UpdateClientWeatherBatch(ctx context.Context, db db.DbDetails, p []RawClientWeatherData) {
func UpdateClientWeatherBatch(ctx context.Context, db db.DbDetails, p []*pogo.ClientWeatherProto) {
for _, weatherProto := range p {
weatherId := strconv.FormatInt(weatherProto.Data.S2CellId, 10)
weatherId := strconv.FormatInt(weatherProto.S2CellId, 10)
weatherMutex, _ := weatherStripedMutex.GetLock(weatherId)
weatherMutex.Lock()
weather, err := getWeatherRecord(ctx, db, weatherProto.Cell)
weather, err := getWeatherRecord(ctx, db, weatherProto.S2CellId)
if err != nil {
log.Printf("getWeatherRecord: %s", err)
} else {
if weather == nil {
weather = &Weather{}
}
weather.updateWeatherFromClientWeatherProto(weatherProto.Data)
weather.updateWeatherFromClientWeatherProto(weatherProto)
saveWeatherRecord(ctx, db, weather)
}
weatherMutex.Unlock()
Expand Down
Loading

0 comments on commit bea04ef

Please sign in to comment.