Skip to content

Commit

Permalink
feat: write gohbem masterfile to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio1988 committed Jan 3, 2025
1 parent 955b8ef commit e404649
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion decoder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package decoder

import (
"context"
"encoding/json"
"fmt"
"math"
"os"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -197,7 +199,15 @@ func InitialiseOhbem() {
}

if err := o.FetchPokemonData(); err != nil {
log.Errorf("ohbem.FetchPokemonData: %s", err)
if err2 := o.LoadPokemonData("cache/master-latest-basics.json"); err2 != nil {
log.Errorf("ohbem.FetchPokemonData failed. ohbme.LoadPokemonData failed: %s", err2)
} else {
log.Warnf("ohbem.FetchPokemonData failed, loaded from cache: %s", err)
}
}

if o.PokemonData.Initialized == true {
writePokemonDataIntoCache(&o.PokemonData)
}

_ = o.WatchPokemonData()
Expand All @@ -206,6 +216,32 @@ func InitialiseOhbem() {
}
}

func writePokemonDataIntoCache(data *gohbem.PokemonData) {
// Convert to JSON
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Errorf("Error marshalling Masterfile: %s", err)
return
}
// Write to the file
file, err := os.Create("cache/master-latest-basics.json")
if err != nil {
log.Errorf("Error creating file: %s", err)
return
}
defer func() {
if err2 := file.Close(); err2 != nil {
log.Errorf("Error closing file: %s", err2)
}
}()

_, err = file.Write(jsonData)
if err != nil {
log.Errorf("Error writing to file: %s", err)
return
}
}

func ClearPokestopCache() {
pokestopCache.DeleteAll()
}
Expand Down

0 comments on commit e404649

Please sign in to comment.