2018-01-29 23:50:25 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package datacache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2018-05-02 00:47:55 +04:00
|
|
|
|
"gopkg.in/yaml.v2"
|
2018-03-31 08:34:27 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
|
2018-05-02 00:47:55 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/static"
|
2018-01-29 23:50:25 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) initPokememes() {
|
|
|
|
|
c.Log.Info("Initializing Pokememes storage...")
|
2018-05-02 00:47:55 +04:00
|
|
|
|
dc.pokememes = make(map[int]*datamapping.Pokememe)
|
|
|
|
|
dc.fullPokememes = make(map[int]*datamapping.PokememeFull)
|
|
|
|
|
dc.pokememesGradeLocation = make(map[int]map[int]int)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) loadPokememes() {
|
2018-05-02 00:47:55 +04:00
|
|
|
|
c.Log.Info("Load current Pokememes data to DataCache...")
|
|
|
|
|
|
|
|
|
|
pokememes := dc.getPokememes()
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
for i := range pokememes {
|
|
|
|
|
dc.pokememes[pokememes[i].ID] = &pokememes[i]
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
if dc.pokememesGradeLocation[pokememes[i].Grade] == nil {
|
|
|
|
|
dc.pokememesGradeLocation[pokememes[i].Grade] = make(map[int]int)
|
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
pokememeFull := datamapping.PokememeFull{}
|
|
|
|
|
pokememeFullElements := []datamapping.Element{}
|
|
|
|
|
pokememeFullLocations := []datamapping.Location{}
|
|
|
|
|
pokememeFull.Pokememe = pokememes[i]
|
|
|
|
|
for ii := range pokememes[i].Elements {
|
|
|
|
|
element, err := dc.GetElementByID(pokememes[i].Elements[ii])
|
|
|
|
|
if err != nil {
|
|
|
|
|
// This is critical
|
|
|
|
|
c.Log.Fatal(err.Error())
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
2018-05-02 00:47:55 +04:00
|
|
|
|
pokememeFullElements = append(pokememeFullElements, *element)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
2018-05-02 00:47:55 +04:00
|
|
|
|
for ii := range pokememes[i].Locations {
|
|
|
|
|
location, err := dc.getLocationByID(pokememes[i].Locations[ii])
|
|
|
|
|
if err != nil {
|
|
|
|
|
// This is critical
|
|
|
|
|
c.Log.Fatal(err.Error())
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
2018-05-02 00:47:55 +04:00
|
|
|
|
pokememeFullLocations = append(pokememeFullLocations, *location)
|
|
|
|
|
dc.pokememesGradeLocation[pokememes[i].Grade][location.ID]++
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
pokememeFull.Elements = pokememeFullElements
|
|
|
|
|
pokememeFull.Locations = pokememeFullLocations
|
|
|
|
|
dc.fullPokememes[pokememes[i].ID] = &pokememeFull
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
c.Log.Info("Loaded pokememes in DataCache: " + strconv.Itoa(len(dc.fullPokememes)))
|
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
func (dc *DataCache) getPokememes() []datamapping.Pokememe {
|
|
|
|
|
pokememes := []datamapping.Pokememe{}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
allPokememesFiles, err := static.WalkDirs("pokememes", false)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if err != nil {
|
2018-05-02 00:47:55 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
c.Log.Fatal("Can't read directory with pokememes information")
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
var pokememesData []byte
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
for i := range allPokememesFiles {
|
|
|
|
|
yamlFile, err := static.ReadFile(allPokememesFiles[i])
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2018-05-02 00:47:55 +04:00
|
|
|
|
c.Log.Fatal("Can't read pokememes data file: " + allPokememesFiles[i])
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
for ii := range yamlFile {
|
|
|
|
|
pokememesData = append(pokememesData, yamlFile[ii])
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
2018-05-02 00:47:55 +04:00
|
|
|
|
pokememesData = append(pokememesData, '\n')
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
err = yaml.Unmarshal(pokememesData, &pokememes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
c.Log.Fatal("Can't parse merged pokememes data")
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
return pokememes
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
// External functions
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// GetAllPokememes returns all pokememes
|
2018-05-02 00:47:55 +04:00
|
|
|
|
func (dc *DataCache) GetAllPokememes() map[int]*datamapping.PokememeFull {
|
|
|
|
|
pokememes := make(map[int]*datamapping.PokememeFull)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
for i := range dc.fullPokememes {
|
2018-05-02 00:47:55 +04:00
|
|
|
|
pokememes[dc.fullPokememes[i].Pokememe.ID] = dc.fullPokememes[i]
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pokememes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPokememeByID returns pokememe with additional information by ID
|
2018-05-02 00:47:55 +04:00
|
|
|
|
func (dc *DataCache) GetPokememeByID(pokememeID int) (*datamapping.PokememeFull, error) {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if dc.fullPokememes[pokememeID] != nil {
|
|
|
|
|
return dc.fullPokememes[pokememeID], nil
|
|
|
|
|
}
|
2018-05-02 00:47:55 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
return nil, errors.New("There is no pokememe with ID = " + strconv.Itoa(pokememeID))
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
// GetPokememeByName returns pokememe with additional information by name
|
|
|
|
|
func (dc *DataCache) GetPokememeByName(pokememeName string) (*datamapping.PokememeFull, error) {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
for i := range dc.fullPokememes {
|
2018-05-02 00:47:55 +04:00
|
|
|
|
if strings.Contains(dc.fullPokememes[i].Pokememe.Name, pokememeName) {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
return dc.fullPokememes[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
return nil, errors.New("There is no pokememe with name = " + pokememeName)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
2018-02-14 00:09:58 +04:00
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
// GetPokememesCountByGradeAndLocation returns pokememes count with given grade on given location
|
|
|
|
|
func (dc *DataCache) GetPokememesCountByGradeAndLocation(grade int, locationID int) int {
|
|
|
|
|
if dc.pokememesGradeLocation[grade] == nil {
|
|
|
|
|
return 0
|
2018-02-14 00:09:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 00:47:55 +04:00
|
|
|
|
return dc.pokememesGradeLocation[grade][locationID]
|
2018-02-14 00:09:58 +04:00
|
|
|
|
}
|