Archived
1

Move rarely changed data to datamappings, fix profile updating

As result of profile format change, introduced in game update yesterday we need to change profile regexp.

As result of some refactoring, rarely changed data removed from database and added to sources of bot.
This commit is contained in:
2018-03-31 08:34:27 +04:00
parent 8d78ef37f1
commit c9855116da
27 changed files with 460 additions and 237 deletions

View File

@@ -5,6 +5,7 @@ package datacacheinterface
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
)
@@ -51,8 +52,11 @@ type DataCacheInterface interface {
DeletePokememeByID(pokememeID int) error
UpdatePokememe(pokememeData map[string]string, pokememeLocations map[string]string, pokememeElements map[string]string) (int, error)
GetLeagueBySymbol(symbol string) (*dbmapping.League, error)
GetElementByID(elementID int) (*datamapping.Element, error)
GetWeaponTypeByID(weaponID int) (*dbmapping.Weapon, error)
GetWeaponTypeByName(name string) (*dbmapping.Weapon, error)
GetLeagueByID(leagueID int) (*datamapping.League, error)
GetLeagueBySymbol(symbol string) (*datamapping.League, error)
GetWeaponTypeByID(weaponID int) (*datamapping.Weapon, error)
GetWeaponTypeByName(name string) (*datamapping.Weapon, error)
}

View File

@@ -5,41 +5,61 @@ package datacache
import (
"errors"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"strconv"
)
func (dc *DataCache) initElements() {
c.Log.Info("Initializing Elements storage...")
dc.elements = make(map[int]*dbmapping.Element)
dc.elements = make(map[int]*datamapping.Element)
}
func (dc *DataCache) loadElements() {
c.Log.Info("Load current Elements data from database to DataCache...")
elements := []dbmapping.Element{}
err := c.Db.Select(&elements, "SELECT * FROM elements")
if err != nil {
// This is critical error and we need to stop immediately!
c.Log.Fatal(err.Error())
}
c.Log.Info("Load current Elements data to DataCache...")
elements := dc.getElements()
dc.elementsMutex.Lock()
for i := range elements {
dc.elements[elements[i].ID] = &elements[i]
}
c.Log.Info("Loaded elements in DataCache: " + strconv.Itoa(len(dc.elements)))
dc.elementsMutex.Unlock()
}
func (dc *DataCache) getElements() []datamapping.Element {
elements := []datamapping.Element{}
elements = append(elements, datamapping.Element{1, "👊", "Боевой", 1})
elements = append(elements, datamapping.Element{2, "🌀", "Летающий", 1})
elements = append(elements, datamapping.Element{3, "💀", "Ядовитый", 1})
elements = append(elements, datamapping.Element{4, "🗿", "Каменный", 1})
elements = append(elements, datamapping.Element{5, "🔥", "Огненный", 2})
elements = append(elements, datamapping.Element{6, "⚡", "Электрический", 2})
elements = append(elements, datamapping.Element{7, "💧", "Водяной", 2})
elements = append(elements, datamapping.Element{8, "🍀", "Травяной", 2})
elements = append(elements, datamapping.Element{9, "💩", "Отважный", 3})
elements = append(elements, datamapping.Element{10, "👁", "Психический", 3})
elements = append(elements, datamapping.Element{11, "👿", "Тёмный", 3})
elements = append(elements, datamapping.Element{12, "⌛", "Времени", 3})
return elements
}
func (dc *DataCache) findElementIDBySymbol(symbol string) (int, error) {
dc.elementsMutex.Lock()
for i := range dc.elements {
if dc.elements[i].Symbol == symbol {
dc.elementsMutex.Unlock()
return i, nil
}
}
dc.elementsMutex.Unlock()
return 0, errors.New("There is no element with symbol = " + symbol)
}
// External functions
// GetElementByID returns element with given ID
func (dc *DataCache) GetElementByID(elementID int) (*datamapping.Element, error) {
if dc.elements[elementID] != nil {
return dc.elements[elementID], nil
}
return nil, errors.New("There is no element with ID = " + strconv.Itoa(elementID))
}

View File

@@ -6,6 +6,7 @@ package datacache
import (
"source.wtfteam.pro/i2_bot/i2_bot/lib/appcontext"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datacache/datacacheinterface"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
"sync"
)
@@ -44,18 +45,15 @@ type DataCache struct {
squadPlayers map[int]map[int]*dbmapping.SquadPlayerFull
squadsMutex sync.Mutex
// Non-database data
// Elements
elements map[int]*dbmapping.Element
elementsMutex sync.Mutex
elements map[int]*datamapping.Element
// Leagues
leagues map[int]*dbmapping.League
leaguesMutex sync.Mutex
leagues map[int]*datamapping.League
// Locations
locations map[int]*dbmapping.Location
locationsMutex sync.Mutex
locations map[int]*datamapping.Location
// Weapons
weapons map[int]*dbmapping.Weapon
weaponsMutex sync.Mutex
weapons map[int]*datamapping.Weapon
}
// New is an initialization function for appcontext

View File

@@ -5,44 +5,53 @@ package datacache
import (
"errors"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"strconv"
)
func (dc *DataCache) initLeagues() {
c.Log.Info("Initializing Leagues storage...")
dc.leagues = make(map[int]*dbmapping.League)
dc.leagues = make(map[int]*datamapping.League)
}
func (dc *DataCache) loadLeagues() {
c.Log.Info("Load current Leagues data from database to DataCache...")
leagues := []dbmapping.League{}
err := c.Db.Select(&leagues, "SELECT * FROM leagues")
if err != nil {
// This is critical error and we need to stop immediately!
c.Log.Fatal(err.Error())
}
c.Log.Info("Load current Leagues data to DataCache...")
leagues := dc.getLeagues()
dc.leaguesMutex.Lock()
for i := range leagues {
dc.leagues[leagues[i].ID] = &leagues[i]
}
c.Log.Info("Loaded leagues in DataCache: " + strconv.Itoa(len(dc.leagues)))
dc.leaguesMutex.Unlock()
}
func (dc *DataCache) getLeagues() []datamapping.League {
leagues := []datamapping.League{}
leagues = append(leagues, datamapping.League{1, "🈸", "ИНСТИНКТ"})
leagues = append(leagues, datamapping.League{2, "🈳 ", "МИСТИКА"})
leagues = append(leagues, datamapping.League{3, "🈵", "ОТВАГА"})
return leagues
}
// External functions
// GetLeagueByID returns league from datacache by ID
func (dc *DataCache) GetLeagueByID(leagueID int) (*datamapping.League, error) {
if dc.leagues[leagueID] != nil {
return dc.leagues[leagueID], nil
}
return nil, errors.New("There is no league with ID = " + strconv.Itoa(leagueID))
}
// GetLeagueBySymbol returns league from datacache by emoji
func (dc *DataCache) GetLeagueBySymbol(symbol string) (*dbmapping.League, error) {
dc.leaguesMutex.Lock()
func (dc *DataCache) GetLeagueBySymbol(symbol string) (*datamapping.League, error) {
for i := range dc.leagues {
if dc.leagues[i].Symbol == symbol {
dc.leaguesMutex.Unlock()
return dc.leagues[i], nil
}
}
dc.leaguesMutex.Unlock()
return nil, errors.New("There is no league with symbol = " + symbol)
}

View File

@@ -5,41 +5,44 @@ package datacache
import (
"errors"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"strconv"
)
func (dc *DataCache) initLocations() {
c.Log.Info("Initializing Locations storage...")
dc.locations = make(map[int]*dbmapping.Location)
dc.locations = make(map[int]*datamapping.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())
}
c.Log.Info("Load current Locations data to DataCache...")
locations := dc.getLocations()
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) getLocations() []datamapping.Location {
locations := []datamapping.Location{}
locations = append(locations, datamapping.Location{1, "🌲", "Лес"})
locations = append(locations, datamapping.Location{2, "⛰", "Горы"})
locations = append(locations, datamapping.Location{3, "🚣", "Озеро"})
locations = append(locations, datamapping.Location{4, "🏙", "Город"})
locations = append(locations, datamapping.Location{5, "🏛", "Катакомбы"})
locations = append(locations, datamapping.Location{6, "⛪️", "Кладбище"})
return locations
}
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)
}

View File

@@ -6,6 +6,7 @@ package datacache
import (
"errors"
"sort"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
"strconv"
"strings"
@@ -46,8 +47,8 @@ func (dc *DataCache) loadPokememes() {
// Filling fullPokememes
fullPokememe := dbmapping.PokememeFull{}
elementsListed := []dbmapping.Element{}
locationsListed := []dbmapping.Location{}
elementsListed := []datamapping.Element{}
locationsListed := []datamapping.Location{}
for j := range pokememesLocations {
if pokememesLocations[j].PokememeID == pokememes[i].ID {
@@ -122,8 +123,8 @@ func (dc *DataCache) AddPokememe(pokememeData map[string]string, pokememeLocatio
pokememe.PlayerID = creatorID
pokememe.CreatedAt = time.Now().UTC()
locations := []dbmapping.Location{}
elements := []dbmapping.Element{}
locations := []datamapping.Location{}
elements := []datamapping.Element{}
for i := range pokememeLocations {
locationID, err := dc.findLocationIDByName(pokememeLocations[i])
@@ -343,8 +344,8 @@ func (dc *DataCache) UpdatePokememe(pokememeData map[string]string, pokememeLoca
pokememe.PlayerID = creatorID
pokememe.CreatedAt = time.Now().UTC()
locations := []dbmapping.Location{}
elements := []dbmapping.Element{}
locations := []datamapping.Location{}
elements := []datamapping.Element{}
for i := range pokememeLocations {
locationID, err := dc.findLocationIDByName(pokememeLocations[i])

View File

@@ -5,57 +5,58 @@ package datacache
import (
"errors"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
"source.wtfteam.pro/i2_bot/i2_bot/lib/datamapping"
"strconv"
"strings"
)
func (dc *DataCache) initWeapons() {
c.Log.Info("Initializing Weapons storage...")
dc.weapons = make(map[int]*dbmapping.Weapon)
dc.weapons = make(map[int]*datamapping.Weapon)
}
func (dc *DataCache) loadWeapons() {
c.Log.Info("Load current Weapons data from database to DataCache...")
weapons := []dbmapping.Weapon{}
err := c.Db.Select(&weapons, "SELECT * FROM weapons")
if err != nil {
// This is critical error and we need to stop immediately!
c.Log.Fatal(err.Error())
}
c.Log.Info("Load current Weapons data to DataCache...")
weapons := dc.getWeapons()
dc.weaponsMutex.Lock()
for i := range weapons {
dc.weapons[weapons[i].ID] = &weapons[i]
}
c.Log.Info("Loaded weapon types in DataCache: " + strconv.Itoa(len(dc.weapons)))
dc.weaponsMutex.Unlock()
}
func (dc *DataCache) getWeapons() []datamapping.Weapon {
weapons := []datamapping.Weapon{}
weapons = append(weapons, datamapping.Weapon{1, "Бита", 2, 5})
weapons = append(weapons, datamapping.Weapon{2, "Стальная бита", 10, 40})
weapons = append(weapons, datamapping.Weapon{3, "Чугунная бита", 200, 500})
weapons = append(weapons, datamapping.Weapon{4, "Титановая бита", 2000, 10000})
weapons = append(weapons, datamapping.Weapon{5, "Алмазная бита", 10000, 100000})
weapons = append(weapons, datamapping.Weapon{6, "Криптонитовая бита", 100000, 500000})
weapons = append(weapons, datamapping.Weapon{5, "Буханка из пятёры", 1000000, 5000000})
return weapons
}
// External functions
// GetWeaponTypeByID returns weapon type from datacache by given ID
func (dc *DataCache) GetWeaponTypeByID(weaponID int) (*dbmapping.Weapon, error) {
dc.weaponsMutex.Lock()
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))
dc.weaponsMutex.Unlock()
return dc.weapons[weaponID], nil
}
dc.weaponsMutex.Unlock()
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) (*dbmapping.Weapon, error) {
dc.weaponsMutex.Lock()
func (dc *DataCache) GetWeaponTypeByName(name string) (*datamapping.Weapon, error) {
for i := range dc.weapons {
if strings.HasPrefix(dc.weapons[i].Name, name) {
dc.weaponsMutex.Unlock()
return dc.weapons[i], nil
}
}
dc.weaponsMutex.Unlock()
return nil, errors.New("There is no weapon type with name = " + name)
}