hdkv
/
i2_bot
Archived
1
Fork 0
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues/pull-requests.
i2_bot/lib/datacache/weapons.go

81 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// i2_bot Instinct PokememBro Bot
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
package datacache
import (
"errors"
"strconv"
"strings"
"gopkg.in/yaml.v2"
"github.com/fat0troll/i2_bot/lib/datamapping"
"github.com/fat0troll/i2_bot/static"
)
func (dc *DataCache) initWeapons() {
c.Log.Info("Initializing Weapons storage...")
dc.weapons = make(map[int]*datamapping.Weapon)
}
func (dc *DataCache) loadWeapons() {
c.Log.Info("Load current Weapons data to DataCache...")
weapons := dc.getWeapons()
for i := range weapons {
dc.weapons[weapons[i].ID] = &weapons[i]
}
c.Log.Info("Loaded weapon types in DataCache: " + strconv.Itoa(len(dc.weapons)))
}
func (dc *DataCache) getWeapons() []datamapping.Weapon {
weapons := []datamapping.Weapon{}
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")
}
return weapons
}
// External functions
// 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))
}
// GetWeaponTypeByID returns weapon type from datacache by given ID
func (dc *DataCache) GetWeaponTypeByID(weaponID int) (*datamapping.Weapon, error) {
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))
}
// GetWeaponTypeByName returns weapon type from datacache by weapon name
func (dc *DataCache) GetWeaponTypeByName(name string) (*datamapping.Weapon, error) {
for i := range dc.weapons {
if strings.HasPrefix(dc.weapons[i].Name, name) {
return dc.weapons[i], nil
}
}
return nil, errors.New("There is no weapon type with name = " + name)
}