2018-01-29 23:50:25 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package datacache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2018-02-13 22:05:32 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
2018-01-29 23:50:25 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) initLocations() {
|
|
|
|
|
c.Log.Info("Initializing Locations storage...")
|
|
|
|
|
dc.locations = make(map[int]*dbmapping.Location)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) loadLocations() {
|
|
|
|
|
c.Log.Info("Load current Locations data from database to DataCache...")
|
|
|
|
|
locations := []dbmapping.Location{}
|
|
|
|
|
err := c.Db.Select(&locations, "SELECT * FROM locations")
|
|
|
|
|
if err != nil {
|
|
|
|
|
// This is critical error and we need to stop immediately!
|
|
|
|
|
c.Log.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dc.locationsMutex.Lock()
|
|
|
|
|
for i := range locations {
|
|
|
|
|
dc.locations[locations[i].ID] = &locations[i]
|
|
|
|
|
}
|
|
|
|
|
c.Log.Info("Loaded locations in DataCache: " + strconv.Itoa(len(dc.locations)))
|
|
|
|
|
dc.locationsMutex.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) findLocationIDByName(name string) (int, error) {
|
|
|
|
|
dc.locationsMutex.Lock()
|
|
|
|
|
for i := range dc.locations {
|
|
|
|
|
if dc.locations[i].Name == name {
|
|
|
|
|
dc.locationsMutex.Unlock()
|
|
|
|
|
return i, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dc.locationsMutex.Unlock()
|
|
|
|
|
return 0, errors.New("There is no location with name = " + name)
|
|
|
|
|
}
|