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 07:25:39 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2018-04-30 19:46:28 +04:00
|
|
|
|
"gopkg.in/yaml.v2"
|
2018-05-19 12:14:25 +04:00
|
|
|
|
"github.com/fat0troll/i2_bot/lib/datamapping"
|
|
|
|
|
"github.com/fat0troll/i2_bot/static"
|
2018-01-29 23:50:25 +04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) initWeapons() {
|
|
|
|
|
c.Log.Info("Initializing Weapons storage...")
|
2018-03-31 08:34:27 +04:00
|
|
|
|
dc.weapons = make(map[int]*datamapping.Weapon)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) loadWeapons() {
|
2018-03-31 08:34:27 +04:00
|
|
|
|
c.Log.Info("Load current Weapons data to DataCache...")
|
|
|
|
|
weapons := dc.getWeapons()
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
for i := range weapons {
|
|
|
|
|
dc.weapons[weapons[i].ID] = &weapons[i]
|
|
|
|
|
}
|
|
|
|
|
c.Log.Info("Loaded weapon types in DataCache: " + strconv.Itoa(len(dc.weapons)))
|
2018-03-31 08:34:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *DataCache) getWeapons() []datamapping.Weapon {
|
|
|
|
|
weapons := []datamapping.Weapon{}
|
|
|
|
|
|
2018-04-30 19:46:28 +04:00
|
|
|
|
yamlFile, err := static.ReadFile("weapons.yml")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
c.Log.Fatal("Can't read weapons data file")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(yamlFile, &weapons)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
c.Log.Fatal("Can't parse weapons data file")
|
|
|
|
|
}
|
2018-03-31 08:34:27 +04:00
|
|
|
|
|
|
|
|
|
return weapons
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// External functions
|
|
|
|
|
|
2018-05-02 08:48:22 +04:00
|
|
|
|
// GetWeaponTypeByAttack returns weapon type from datacache by given attack
|
|
|
|
|
func (dc *DataCache) GetWeaponTypeByAttack(attack int) (*datamapping.Weapon, error) {
|
|
|
|
|
for i := range dc.weapons {
|
|
|
|
|
if dc.weapons[i].Power == attack {
|
|
|
|
|
return dc.weapons[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("There is no weapon type with attack = " + strconv.Itoa(attack))
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 15:59:28 +04:00
|
|
|
|
// GetWeaponTypeByID returns weapon type from datacache by given ID
|
2018-03-31 08:34:27 +04:00
|
|
|
|
func (dc *DataCache) GetWeaponTypeByID(weaponID int) (*datamapping.Weapon, error) {
|
2018-02-07 15:59:28 +04:00
|
|
|
|
if dc.weapons[weaponID] != nil {
|
|
|
|
|
c.Log.Debug("DataCache: found weapon type with ID = " + strconv.Itoa(weaponID))
|
|
|
|
|
return dc.weapons[weaponID], nil
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New("There is no weapon type with ID = " + strconv.Itoa(weaponID))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// GetWeaponTypeByName returns weapon type from datacache by weapon name
|
2018-03-31 08:34:27 +04:00
|
|
|
|
func (dc *DataCache) GetWeaponTypeByName(name string) (*datamapping.Weapon, error) {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
for i := range dc.weapons {
|
2018-01-30 18:41:23 +04:00
|
|
|
|
if strings.HasPrefix(dc.weapons[i].Name, name) {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
return dc.weapons[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("There is no weapon type with name = " + name)
|
|
|
|
|
}
|