DataCache and changes for game update
Recent game update changed pokememes view in pokedeks, so we need to reflect it by updating parser. Introducing DataCache - a silver bullet for eliminating lags linked to database queries. Less queries, more in RAM, faster work. Needs testing in production environment.
This commit is contained in:
@@ -15,32 +15,12 @@ func (p *Pokedexer) DeletePokememe(update *tgbotapi.Update) string {
|
||||
return "fail"
|
||||
}
|
||||
|
||||
pokememe, ok := p.GetPokememeByID(strconv.Itoa(pokememeNum))
|
||||
if !ok {
|
||||
return "fail"
|
||||
}
|
||||
|
||||
_, err := c.Db.NamedExec("DELETE FROM pokememes WHERE id=:id", &pokememe.Pokememe)
|
||||
err := c.DataCache.DeletePokememeByID(pokememeNum)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return "fail"
|
||||
}
|
||||
|
||||
_, err = c.Db.NamedExec("DELETE FROM pokememes_elements WHERE pokememe_id=:id", &pokememe.Pokememe)
|
||||
if err != nil {
|
||||
c.Log.Debug(err.Error())
|
||||
}
|
||||
|
||||
_, err = c.Db.NamedExec("DELETE FROM pokememes_locations WHERE pokememe_id=:id", &pokememe.Pokememe)
|
||||
if err != nil {
|
||||
c.Log.Debug(err.Error())
|
||||
}
|
||||
|
||||
_, err = c.Db.NamedExec("DELETE FROM profiles_pokememes WHERE pokememe_id=:id", &pokememe.Pokememe)
|
||||
if err != nil {
|
||||
c.Log.Debug(err.Error())
|
||||
}
|
||||
|
||||
message := "Покемем удалён."
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
|
@@ -5,184 +5,50 @@ package pokedexer
|
||||
|
||||
import (
|
||||
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Internal functions
|
||||
|
||||
func (p *Pokedexer) formFullPokememes(pokememes []dbmapping.Pokememe) ([]dbmapping.PokememeFull, bool) {
|
||||
pokememesArray := []dbmapping.PokememeFull{}
|
||||
elements := []dbmapping.Element{}
|
||||
err := c.Db.Select(&elements, "SELECT * FROM elements")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return pokememesArray, false
|
||||
}
|
||||
locations := []dbmapping.Location{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return pokememesArray, false
|
||||
}
|
||||
pokememesElements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememesElements, "SELECT * FROM pokememes_elements")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return pokememesArray, false
|
||||
}
|
||||
pokememesLocations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememesLocations, "SELECT * FROM pokememes_locations")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
for i := range pokememes {
|
||||
fullPokememe := dbmapping.PokememeFull{}
|
||||
elementsListed := []dbmapping.Element{}
|
||||
locationsListed := []dbmapping.Location{}
|
||||
|
||||
for j := range pokememesLocations {
|
||||
if pokememesLocations[j].PokememeID == pokememes[i].ID {
|
||||
for l := range locations {
|
||||
if pokememesLocations[j].LocationID == locations[l].ID {
|
||||
locationsListed = append(locationsListed, locations[l])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range pokememesElements {
|
||||
if pokememesElements[k].PokememeID == pokememes[i].ID {
|
||||
for e := range elements {
|
||||
if pokememesElements[k].ElementID == elements[e].ID {
|
||||
elementsListed = append(elementsListed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fullPokememe.Pokememe = pokememes[i]
|
||||
fullPokememe.Elements = elementsListed
|
||||
fullPokememe.Locations = locationsListed
|
||||
|
||||
pokememesArray = append(pokememesArray, fullPokememe)
|
||||
}
|
||||
|
||||
return pokememesArray, true
|
||||
}
|
||||
|
||||
// External functions
|
||||
|
||||
// GetPokememes returns all existing pokememes, known by bot
|
||||
func (p *Pokedexer) GetPokememes() ([]dbmapping.PokememeFull, bool) {
|
||||
pokememesArray := []dbmapping.PokememeFull{}
|
||||
pokememes := []dbmapping.Pokememe{}
|
||||
err := c.Db.Select(&pokememes, "SELECT * FROM pokememes ORDER BY grade asc, name asc")
|
||||
func (p *Pokedexer) getBestPokememes(playerID int) (map[int]*dbmapping.PokememeFull, bool) {
|
||||
pokememesArray := make(map[int]*dbmapping.PokememeFull)
|
||||
|
||||
playerRaw, err := c.DataCache.GetPlayerByID(playerID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
c.Log.Error(err.Error())
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
pokememesArray, ok := p.formFullPokememes(pokememes)
|
||||
return pokememesArray, ok
|
||||
}
|
||||
|
||||
func (p *Pokedexer) getBestPokememes(playerID int) ([]dbmapping.PokememeFull, bool) {
|
||||
pokememesArray := []dbmapping.PokememeFull{}
|
||||
playerRaw, ok := c.Users.GetPlayerByID(playerID)
|
||||
if !ok {
|
||||
return pokememesArray, ok
|
||||
}
|
||||
profileRaw, ok := c.Users.GetProfile(playerID)
|
||||
if !ok {
|
||||
return pokememesArray, ok
|
||||
profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
if playerRaw.LeagueID == 0 {
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
// TODO: make it more complicated
|
||||
pokememes := []dbmapping.Pokememe{}
|
||||
allPokememes := c.DataCache.GetAllPokememes()
|
||||
if profileRaw.LevelID < 4 {
|
||||
err := c.Db.Select(&pokememes, c.Db.Rebind("SELECT * FROM pokememes WHERE grade = ? ORDER BY attack DESC"), profileRaw.LevelID+1)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return pokememesArray, false
|
||||
for i := range allPokememes {
|
||||
if allPokememes[i].Pokememe.Grade == profileRaw.LevelID+1 {
|
||||
pokememesArray[allPokememes[i].Pokememe.Attack] = allPokememes[i]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err := c.Db.Select(&pokememes, c.Db.Rebind("SELECT p.* FROM pokememes p, pokememes_elements pe, elements e WHERE e.league_id = ? AND p.grade = ? AND pe.element_id = e.id AND pe.pokememe_id = p.id ORDER BY p.attack DESC"), playerRaw.LeagueID, profileRaw.LevelID+1)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return pokememesArray, false
|
||||
}
|
||||
}
|
||||
|
||||
pokememesArray, ok = p.formFullPokememes(pokememes)
|
||||
return pokememesArray, ok
|
||||
}
|
||||
|
||||
// GetPokememeByID returns single pokememe based on internal ID in database
|
||||
func (p *Pokedexer) GetPokememeByID(pokememeID string) (dbmapping.PokememeFull, bool) {
|
||||
fullPokememe := dbmapping.PokememeFull{}
|
||||
pokememe := dbmapping.Pokememe{}
|
||||
err := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE id=?"), pokememeID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return fullPokememe, false
|
||||
}
|
||||
elements := []dbmapping.Element{}
|
||||
err = c.Db.Select(&elements, "SELECT * FROM elements")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return fullPokememe, false
|
||||
}
|
||||
locations := []dbmapping.Location{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return fullPokememe, false
|
||||
}
|
||||
pokememesElements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememesElements, "SELECT * FROM pokememes_elements WHERE pokememe_id='"+strconv.Itoa(pokememe.ID)+"'")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return fullPokememe, false
|
||||
}
|
||||
pokememesLocations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememesLocations, "SELECT * FROM pokememes_locations WHERE pokememe_id='"+strconv.Itoa(pokememe.ID)+"'")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return fullPokememe, false
|
||||
}
|
||||
|
||||
elementsListed := []dbmapping.Element{}
|
||||
locationsListed := []dbmapping.Location{}
|
||||
|
||||
for j := range pokememesLocations {
|
||||
if pokememesLocations[j].PokememeID == pokememe.ID {
|
||||
for l := range locations {
|
||||
if pokememesLocations[j].LocationID == locations[l].ID {
|
||||
locationsListed = append(locationsListed, locations[l])
|
||||
for i := range allPokememes {
|
||||
if allPokememes[i].Pokememe.Grade == profileRaw.LevelID+1 {
|
||||
matchLeague := false
|
||||
for j := range allPokememes[i].Elements {
|
||||
if allPokememes[i].Elements[j].LeagueID == playerRaw.LeagueID {
|
||||
matchLeague = true
|
||||
}
|
||||
}
|
||||
if matchLeague {
|
||||
pokememesArray[allPokememes[i].Pokememe.Attack] = allPokememes[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range pokememesElements {
|
||||
if pokememesElements[k].PokememeID == pokememe.ID {
|
||||
for e := range elements {
|
||||
if pokememesElements[k].ElementID == elements[e].ID {
|
||||
elementsListed = append(elementsListed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fullPokememe.Pokememe = pokememe
|
||||
fullPokememe.Elements = elementsListed
|
||||
fullPokememe.Locations = locationsListed
|
||||
|
||||
return fullPokememe, true
|
||||
return pokememesArray, true
|
||||
}
|
||||
|
@@ -4,248 +4,143 @@
|
||||
package pokedexer
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
// "time"
|
||||
)
|
||||
|
||||
// ParsePokememe parses pokememe, forwarded from PokememeBroBot, to database
|
||||
func (p *Pokedexer) ParsePokememe(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
|
||||
text := update.Message.Text
|
||||
var defendablePokememe = false
|
||||
pokememeStringsArray := strings.Split(text, "\n")
|
||||
pokememeStringsArray := strings.Split(update.Message.Text, "\n")
|
||||
pokememeRunesArray := make([][]rune, 0)
|
||||
for i := range pokememeStringsArray {
|
||||
pokememeRunesArray = append(pokememeRunesArray, []rune(pokememeStringsArray[i]))
|
||||
}
|
||||
|
||||
if len(pokememeRunesArray) == 13 {
|
||||
defendablePokememe = true
|
||||
pokememeData := make(map[string]string)
|
||||
pokememeLocations := make(map[string]string)
|
||||
pokememeElements := make(map[string]string)
|
||||
|
||||
hitPointsRx := regexp.MustCompile("(\\d|\\.)+(K|M)?")
|
||||
|
||||
for i := range pokememeStringsArray {
|
||||
c.Log.Debug("Processing string: " + pokememeStringsArray[i])
|
||||
if strings.Contains(pokememeStringsArray[i], "⃣") {
|
||||
// Strings with name and grade
|
||||
pokememeData["grade"] = string(pokememeRunesArray[i][0])
|
||||
pokememeData["name"] = string(pokememeRunesArray[i][3:])
|
||||
}
|
||||
|
||||
if i == 1 {
|
||||
pokememeData["description"] = string(pokememeRunesArray[i])
|
||||
}
|
||||
|
||||
if strings.HasPrefix(pokememeStringsArray[i], "Обитает: ") {
|
||||
// Elements
|
||||
locationsString := strings.TrimPrefix(pokememeStringsArray[i], "Обитает: ")
|
||||
locationsArray := strings.Split(locationsString, ", ")
|
||||
for i := range locationsArray {
|
||||
pokememeLocations[strconv.Itoa(i)] = locationsArray[i]
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(pokememeStringsArray[i], "Элементы: ") {
|
||||
// Elements
|
||||
elementsString := strings.TrimPrefix(pokememeStringsArray[i], "Элементы: ")
|
||||
elementsArray := strings.Split(elementsString, " ")
|
||||
for i := range elementsArray {
|
||||
pokememeElements[strconv.Itoa(i)] = elementsArray[i]
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(pokememeStringsArray[i], "⚔Атака: ") {
|
||||
// Attack, HP, MP
|
||||
hitPoints := hitPointsRx.FindAllString(string(pokememeRunesArray[i]), -1)
|
||||
if len(hitPoints) != 3 {
|
||||
c.Log.Error("Can't parse hitpoints!")
|
||||
c.Log.Debug("Points string was: " + string(pokememeRunesArray[i]))
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
pokememeData["attack"] = hitPoints[0]
|
||||
pokememeData["hp"] = hitPoints[1]
|
||||
pokememeData["mp"] = hitPoints[2]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(pokememeStringsArray[i], "🛡Защита ") {
|
||||
// Defence for top-level pokememes
|
||||
defence := hitPointsRx.FindAllString(string(pokememeRunesArray[i]), -1)
|
||||
if len(defence) != 1 {
|
||||
c.Log.Error("Can't parse defence!")
|
||||
c.Log.Debug("Defence string was: " + string(pokememeRunesArray[i]))
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
pokememeData["defence"] = defence[0]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(pokememeStringsArray[i], "Стоимость :") {
|
||||
// Price
|
||||
price := hitPointsRx.FindAllString(string(pokememeRunesArray[i]), -1)
|
||||
if len(price) != 1 {
|
||||
c.Log.Error("Can't parse price!")
|
||||
c.Log.Debug("Price string was: " + string(pokememeRunesArray[i]))
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
pokememeData["price"] = price[0]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(pokememeStringsArray[i], "Купить: ") {
|
||||
// Purchaseability
|
||||
pokememeData["purchaseable"] = "false"
|
||||
if strings.Contains(pokememeStringsArray[i], "Можно") {
|
||||
pokememeData["purchaseable"] = "true"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Getting elements
|
||||
elements := []dbmapping.Element{}
|
||||
elementEmojis := make([]string, 0)
|
||||
elementEmojis = append(elementEmojis, string(pokememeRunesArray[4][11]))
|
||||
if len(pokememeRunesArray[4]) > 12 {
|
||||
elementEmojis = append(elementEmojis, string(pokememeRunesArray[4][13]))
|
||||
}
|
||||
if len(pokememeRunesArray[4]) > 14 {
|
||||
elementEmojis = append(elementEmojis, string(pokememeRunesArray[4][15]))
|
||||
// Image
|
||||
for _, entity := range *update.Message.Entities {
|
||||
if entity.Type == "text_link" && entity.URL != "" {
|
||||
pokememeData["image"] = entity.URL
|
||||
}
|
||||
}
|
||||
|
||||
err := c.Db.Select(&elements, "SELECT * FROM elements WHERE symbol IN ('"+strings.Join(elementEmojis, "', '")+"')")
|
||||
// Checking grade to be integer
|
||||
_, err := strconv.Atoi(pokememeData["grade"])
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
|
||||
// Getting hit-points
|
||||
hitPointsRx := regexp.MustCompile("(\\d|\\.)+(K|M)?")
|
||||
hitPoints := hitPointsRx.FindAllString(string(pokememeRunesArray[5]), -1)
|
||||
if len(hitPoints) != 3 {
|
||||
c.Log.Error("Can't parse hitpoints!")
|
||||
c.Log.Debug(pokememeRunesArray[5])
|
||||
pokememeData["creator_id"] = strconv.Itoa(playerRaw.ID)
|
||||
|
||||
c.Log.Debugln("Pokememe data: ", pokememeData)
|
||||
c.Log.Debugln("Elements: ", pokememeElements)
|
||||
c.Log.Debugln("Locations: ", pokememeLocations)
|
||||
|
||||
if len(pokememeElements) == 0 {
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
|
||||
defence := "0"
|
||||
price := "0"
|
||||
|
||||
locations := []dbmapping.Location{}
|
||||
|
||||
purchaseable := false
|
||||
image := ""
|
||||
|
||||
if defendablePokememe {
|
||||
// Actions for high-grade pokememes
|
||||
defenceMatch := hitPointsRx.FindAllString(string(pokememeRunesArray[6]), -1)
|
||||
if len(defenceMatch) < 1 {
|
||||
c.Log.Error("Can't parse defence!")
|
||||
c.Log.Debug(pokememeRunesArray[6])
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
defence = defenceMatch[0]
|
||||
priceMatch := hitPointsRx.FindAllString(string(pokememeRunesArray[7]), -1)
|
||||
if len(priceMatch) < 1 {
|
||||
c.Log.Error("Can't parse price!")
|
||||
c.Log.Debug(pokememeRunesArray[7])
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
price = priceMatch[0]
|
||||
locationsPrepare := strings.Split(string(pokememeRunesArray[8]), ": ")
|
||||
if len(locationsPrepare) < 2 {
|
||||
c.Log.Error("Can't parse locations!")
|
||||
c.Log.Debug(pokememeRunesArray[8])
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
locationsNames := strings.Split(locationsPrepare[1], ", ")
|
||||
if len(locationsNames) < 1 {
|
||||
c.Log.Error("Can't parse locations!")
|
||||
c.Log.Debug(locationsPrepare)
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
|
||||
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('"+strings.Join(locationsNames, "', '")+"')")
|
||||
if err2 != nil {
|
||||
c.Log.Error(err2.Error())
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
if strings.HasSuffix(string(pokememeRunesArray[9]), "Можно") {
|
||||
purchaseable = true
|
||||
}
|
||||
image = strings.Replace(string(pokememeRunesArray[12]), " ", "", -1)
|
||||
} else {
|
||||
// Actions for low-grade pokememes
|
||||
defence = hitPoints[0]
|
||||
priceMatch := hitPointsRx.FindAllString(string(pokememeRunesArray[6]), -1)
|
||||
if len(priceMatch) < 1 {
|
||||
c.Log.Error("Can't parse price!")
|
||||
c.Log.Debug(pokememeRunesArray[6])
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
price = priceMatch[0]
|
||||
locationsPrepare := strings.Split(string(pokememeRunesArray[7]), ": ")
|
||||
if len(locationsPrepare) < 2 {
|
||||
c.Log.Error("Can't parse locations!")
|
||||
c.Log.Debug(pokememeRunesArray[7])
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
locationsNames := strings.Split(locationsPrepare[1], ", ")
|
||||
if len(locationsNames) < 1 {
|
||||
c.Log.Error("Can't parse locations!")
|
||||
c.Log.Debug(locationsPrepare)
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
|
||||
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('"+strings.Join(locationsNames, "', '")+"')")
|
||||
if err2 != nil {
|
||||
c.Log.Error(err2.Error())
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
if strings.HasSuffix(string(pokememeRunesArray[8]), "Можно") {
|
||||
purchaseable = true
|
||||
}
|
||||
image = strings.Replace(string(pokememeRunesArray[11]), " ", "", -1)
|
||||
}
|
||||
|
||||
grade := string(pokememeRunesArray[0][0])
|
||||
name := string(pokememeRunesArray[0][3:])
|
||||
description := string(pokememeRunesArray[1])
|
||||
c.Log.Debug("Pokememe grade: " + grade)
|
||||
c.Log.Debug("Pokememe name: " + name)
|
||||
c.Log.Debug("Pokememe description: " + description)
|
||||
c.Log.Debug("Elements:")
|
||||
for i := range elements {
|
||||
c.Log.Debug(elements[i].Symbol + " " + elements[i].Name)
|
||||
}
|
||||
c.Log.Debug("Attack: " + hitPoints[0])
|
||||
c.Log.Debug("HP: " + hitPoints[1])
|
||||
c.Log.Debug("MP: " + hitPoints[2])
|
||||
c.Log.Debug("Defence: " + defence)
|
||||
c.Log.Debug("Price: " + price)
|
||||
c.Log.Debug("Locations:")
|
||||
for i := range locations {
|
||||
c.Log.Debug(locations[i].Symbol + " " + locations[i].Name)
|
||||
}
|
||||
if purchaseable {
|
||||
c.Log.Debug("Purchaseable")
|
||||
} else {
|
||||
c.Log.Debug("Non-purchaseable")
|
||||
}
|
||||
c.Log.Debug("Image: " + image)
|
||||
|
||||
// Building pokememe
|
||||
pokememe := dbmapping.Pokememe{}
|
||||
// Checking if pokememe exists in database
|
||||
err3 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='"+grade+"' AND name='"+name+"';"))
|
||||
if err3 != nil {
|
||||
c.Log.Debug("Adding new pokememe...")
|
||||
} else {
|
||||
c.Log.Info("This pokememe already exist. Return specific error.")
|
||||
p.pokememeAddDuplicateMessage(update)
|
||||
return "dup"
|
||||
}
|
||||
|
||||
gradeInt, _ := strconv.Atoi(grade)
|
||||
attackInt := c.Statistics.GetPoints(hitPoints[0])
|
||||
hpInt := c.Statistics.GetPoints(hitPoints[1])
|
||||
mpInt := c.Statistics.GetPoints(hitPoints[2])
|
||||
defenceInt := c.Statistics.GetPoints(defence)
|
||||
priceInt := c.Statistics.GetPoints(price)
|
||||
|
||||
pokememe.Grade = gradeInt
|
||||
pokememe.Name = name
|
||||
pokememe.Description = description
|
||||
pokememe.Attack = attackInt
|
||||
pokememe.HP = hpInt
|
||||
pokememe.MP = mpInt
|
||||
pokememe.Defence = defenceInt
|
||||
pokememe.Price = priceInt
|
||||
if purchaseable {
|
||||
pokememe.Purchaseable = true
|
||||
} else {
|
||||
pokememe.Purchaseable = false
|
||||
}
|
||||
pokememe.ImageURL = image
|
||||
pokememe.PlayerID = playerRaw.ID
|
||||
pokememe.CreatedAt = time.Now().UTC()
|
||||
|
||||
_, err4 := c.Db.NamedExec("INSERT INTO pokememes VALUES(NULL, :grade, :name, :description, :attack, :hp, :mp, :defence, :price, :purchaseable, :image_url, :player_id, :created_at)", &pokememe)
|
||||
if err4 != nil {
|
||||
c.Log.Error(err4.Error())
|
||||
if len(pokememeLocations) == 0 {
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
|
||||
// Getting new pokememe
|
||||
err5 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='"+grade+"' AND name='"+name+"';"))
|
||||
if err5 != nil {
|
||||
c.Log.Error("Pokememe isn't added!")
|
||||
newPokememeID, err := c.DataCache.AddPokememe(pokememeData, pokememeLocations, pokememeElements)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
for i := range elements {
|
||||
link := dbmapping.PokememeElement{}
|
||||
link.PokememeID = pokememe.ID
|
||||
link.ElementID = elements[i].ID
|
||||
link.CreatedAt = time.Now().UTC()
|
||||
|
||||
_, err6 := c.Db.NamedExec("INSERT INTO pokememes_elements VALUES(NULL, :pokememe_id, :element_id, :created_at)", &link)
|
||||
if err6 != nil {
|
||||
c.Log.Error(err6.Error())
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
}
|
||||
for i := range locations {
|
||||
link := dbmapping.PokememeLocation{}
|
||||
link.PokememeID = pokememe.ID
|
||||
link.LocationID = locations[i].ID
|
||||
link.CreatedAt = time.Now().UTC()
|
||||
|
||||
_, err7 := c.Db.NamedExec("INSERT INTO pokememes_locations VALUES(NULL, :pokememe_id, :location_id, :created_at)", &link)
|
||||
if err7 != nil {
|
||||
c.Log.Error(err7.Error())
|
||||
p.pokememeAddFailureMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
}
|
||||
|
||||
p.pokememeAddSuccessMessage(update)
|
||||
p.pokememeAddSuccessMessage(update, newPokememeID)
|
||||
return "ok"
|
||||
}
|
||||
|
@@ -4,15 +4,16 @@
|
||||
package pokedexer
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (p *Pokedexer) pokememesListing(update *tgbotapi.Update, page int, pokememesArray []dbmapping.PokememeFull) {
|
||||
func (p *Pokedexer) pokememesListing(update *tgbotapi.Update, page int, pokememesArray map[int]*dbmapping.PokememeFull) {
|
||||
message := "*Известные боту покемемы*\n"
|
||||
message += "Список отсортирован по грейду и алфавиту.\n"
|
||||
message += "Покедекс: " + strconv.Itoa(len(pokememesArray)) + " / 249\n"
|
||||
message += "Покедекс: " + strconv.Itoa(len(pokememesArray)) + " / 274\n"
|
||||
message += "Отображаем покемемов с " + strconv.Itoa(((page-1)*50)+1) + " по " + strconv.Itoa(page*50) + "\n"
|
||||
if len(pokememesArray) > page*50 {
|
||||
message += "Переход на следующую страницу: /pokedeks" + strconv.Itoa(page+1)
|
||||
@@ -22,7 +23,13 @@ func (p *Pokedexer) pokememesListing(update *tgbotapi.Update, page int, pokememe
|
||||
}
|
||||
message += "\n\n"
|
||||
|
||||
var keys []int
|
||||
for i := range pokememesArray {
|
||||
keys = append(keys, i)
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
for _, i := range keys {
|
||||
if (i+1 > 50*(page-1)) && (i+1 < (50*page)+1) {
|
||||
pk := pokememesArray[i].Pokememe
|
||||
pkE := pokememesArray[i].Elements
|
||||
@@ -52,9 +59,10 @@ func (p *Pokedexer) pokememesListing(update *tgbotapi.Update, page int, pokememe
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
||||
func (p *Pokedexer) pokememeAddSuccessMessage(update *tgbotapi.Update) {
|
||||
func (p *Pokedexer) pokememeAddSuccessMessage(update *tgbotapi.Update, newPokememeID int) {
|
||||
message := "*Покемем успешно добавлен.*\n\n"
|
||||
message += "Посмотреть всех известных боту покемемов можно командой /pokedeks"
|
||||
message += "Посмотреть всех известных боту покемемов можно командой /pokedeks\n"
|
||||
message += "Посмотреть свежедобавленного покемема можно командой /pk" + strconv.Itoa(newPokememeID)
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
@@ -4,8 +4,8 @@
|
||||
package pokedexerinterface
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
)
|
||||
|
||||
// PokedexerInterface implements Pokedexer for importing via appcontext.
|
||||
@@ -16,8 +16,5 @@ type PokedexerInterface interface {
|
||||
PokememeInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
|
||||
BestPokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
|
||||
|
||||
GetPokememes() ([]dbmapping.PokememeFull, bool)
|
||||
GetPokememeByID(pokememeID string) (dbmapping.PokememeFull, bool)
|
||||
|
||||
DeletePokememe(update *tgbotapi.Update) string
|
||||
}
|
||||
|
@@ -4,8 +4,9 @@
|
||||
package pokedexer
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -18,8 +19,14 @@ func (p *Pokedexer) BestPokememesList(update *tgbotapi.Update, playerRaw *dbmapp
|
||||
return "fail"
|
||||
}
|
||||
|
||||
message := "*Лучшие покемемы для ловли*\n\n"
|
||||
var attacks []int
|
||||
for i := range pokememes {
|
||||
attacks = append(attacks, i)
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(attacks)))
|
||||
|
||||
message := "*Лучшие покемемы для ловли*\n\n"
|
||||
for _, i := range attacks {
|
||||
pk := pokememes[i].Pokememe
|
||||
pkL := pokememes[i].Locations
|
||||
pkE := pokememes[i].Elements
|
||||
@@ -63,25 +70,24 @@ func (p *Pokedexer) PokememesList(update *tgbotapi.Update) {
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
pokememesArray, ok := p.GetPokememes()
|
||||
if !ok {
|
||||
c.Talkers.BotError(update)
|
||||
} else {
|
||||
p.pokememesListing(update, page, pokememesArray)
|
||||
}
|
||||
pokememesArray := c.DataCache.GetAllPokememes()
|
||||
p.pokememesListing(update, page, pokememesArray)
|
||||
}
|
||||
|
||||
// PokememeInfo shows information about single pokememe based on internal ID
|
||||
func (p *Pokedexer) PokememeInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
|
||||
pokememeNumber := strings.Replace(update.Message.Text, "/pk", "", 1)
|
||||
var calculatePossibilites = true
|
||||
profileRaw, ok := c.Users.GetProfile(playerRaw.ID)
|
||||
if !ok {
|
||||
profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
calculatePossibilites = false
|
||||
}
|
||||
|
||||
pokememe, ok := p.GetPokememeByID(pokememeNumber)
|
||||
if !ok {
|
||||
pokememeID, _ := strconv.Atoi(pokememeNumber)
|
||||
pokememe, err := c.DataCache.GetPokememeByID(pokememeID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return "fail"
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user