2017-10-06 02:56:06 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
package pokedexer
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
|
|
|
|
import (
|
2017-11-21 06:06:32 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2017-10-06 02:56:06 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// ParsePokememe parses pokememe, forwarded from PokememeBroBot, to database
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (p *Pokedexer) ParsePokememe(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
|
|
|
|
|
text := update.Message.Text
|
2017-10-18 09:39:50 +04:00
|
|
|
|
var defendablePokememe = false
|
|
|
|
|
pokememeStringsArray := strings.Split(text, "\n")
|
|
|
|
|
pokememeRunesArray := make([][]rune, 0)
|
|
|
|
|
for i := range pokememeStringsArray {
|
|
|
|
|
pokememeRunesArray = append(pokememeRunesArray, []rune(pokememeStringsArray[i]))
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if len(pokememeRunesArray) == 13 {
|
|
|
|
|
defendablePokememe = true
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Getting elements
|
|
|
|
|
elements := []dbmapping.Element{}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
elementEmojis := make([]string, 0)
|
|
|
|
|
elementEmojis = append(elementEmojis, string(pokememeRunesArray[4][11]))
|
|
|
|
|
if len(pokememeRunesArray[4]) > 12 {
|
|
|
|
|
elementEmojis = append(elementEmojis, string(pokememeRunesArray[4][13]))
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if len(pokememeRunesArray[4]) > 14 {
|
|
|
|
|
elementEmojis = append(elementEmojis, string(pokememeRunesArray[4][15]))
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
err := c.Db.Select(&elements, "SELECT * FROM elements WHERE symbol IN ('"+strings.Join(elementEmojis, "', '")+"')")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Getting hit-points
|
|
|
|
|
hitPointsRx := regexp.MustCompile("(\\d|\\.)+(K|M)?")
|
2017-10-18 09:39:50 +04:00
|
|
|
|
hitPoints := hitPointsRx.FindAllString(string(pokememeRunesArray[5]), -1)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if len(hitPoints) != 3 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse hitpoints!")
|
|
|
|
|
c.Log.Debug(pokememeRunesArray[5])
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
defence := "0"
|
|
|
|
|
price := "0"
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
locations := []dbmapping.Location{}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
purchaseable := false
|
|
|
|
|
image := ""
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if defendablePokememe {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Actions for high-grade pokememes
|
2017-10-18 09:39:50 +04:00
|
|
|
|
defenceMatch := hitPointsRx.FindAllString(string(pokememeRunesArray[6]), -1)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if len(defenceMatch) < 1 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse defence!")
|
|
|
|
|
c.Log.Debug(pokememeRunesArray[6])
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
defence = defenceMatch[0]
|
2017-10-18 09:39:50 +04:00
|
|
|
|
priceMatch := hitPointsRx.FindAllString(string(pokememeRunesArray[7]), -1)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if len(priceMatch) < 1 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse price!")
|
|
|
|
|
c.Log.Debug(pokememeRunesArray[7])
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
price = priceMatch[0]
|
2017-10-18 09:39:50 +04:00
|
|
|
|
locationsPrepare := strings.Split(string(pokememeRunesArray[8]), ": ")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if len(locationsPrepare) < 2 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse locations!")
|
|
|
|
|
c.Log.Debug(pokememeRunesArray[8])
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
locationsNames := strings.Split(locationsPrepare[1], ", ")
|
|
|
|
|
if len(locationsNames) < 1 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse locations!")
|
|
|
|
|
c.Log.Debug(locationsPrepare)
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('"+strings.Join(locationsNames, "', '")+"')")
|
|
|
|
|
if err2 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err2.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if strings.HasSuffix(string(pokememeRunesArray[9]), "Можно") {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
purchaseable = true
|
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
image = strings.Replace(string(pokememeRunesArray[12]), " ", "", -1)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
} else {
|
|
|
|
|
// Actions for low-grade pokememes
|
|
|
|
|
defence = hitPoints[0]
|
2017-10-18 09:39:50 +04:00
|
|
|
|
priceMatch := hitPointsRx.FindAllString(string(pokememeRunesArray[6]), -1)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if len(priceMatch) < 1 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse price!")
|
|
|
|
|
c.Log.Debug(pokememeRunesArray[6])
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
price = priceMatch[0]
|
2017-10-18 09:39:50 +04:00
|
|
|
|
locationsPrepare := strings.Split(string(pokememeRunesArray[7]), ": ")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if len(locationsPrepare) < 2 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse locations!")
|
|
|
|
|
c.Log.Debug(pokememeRunesArray[7])
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
locationsNames := strings.Split(locationsPrepare[1], ", ")
|
|
|
|
|
if len(locationsNames) < 1 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Can't parse locations!")
|
|
|
|
|
c.Log.Debug(locationsPrepare)
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('"+strings.Join(locationsNames, "', '")+"')")
|
|
|
|
|
if err2 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err2.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if strings.HasSuffix(string(pokememeRunesArray[8]), "Можно") {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
purchaseable = true
|
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
image = strings.Replace(string(pokememeRunesArray[11]), " ", "", -1)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
grade := string(pokememeRunesArray[0][0])
|
|
|
|
|
name := string(pokememeRunesArray[0][3:])
|
|
|
|
|
description := string(pokememeRunesArray[1])
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("Pokememe grade: " + grade)
|
|
|
|
|
c.Log.Debug("Pokememe name: " + name)
|
|
|
|
|
c.Log.Debug("Pokememe description: " + description)
|
|
|
|
|
c.Log.Debug("Elements:")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
for i := range elements {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug(elements[i].Symbol + " " + elements[i].Name)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-11-14 03:44:21 +04:00
|
|
|
|
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:")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
for i := range locations {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug(locations[i].Symbol + " " + locations[i].Name)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
if purchaseable {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("Purchaseable")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
} else {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("Non-purchaseable")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("Image: " + image)
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// 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 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("Adding new pokememe...")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
} else {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Info("This pokememe already exist. Return specific error.")
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddDuplicateMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "dup"
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
gradeInt, _ := strconv.Atoi(grade)
|
2017-11-21 06:06:32 +04:00
|
|
|
|
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)
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
pokememe.Grade = gradeInt
|
2017-10-18 07:03:34 +04:00
|
|
|
|
pokememe.Name = name
|
|
|
|
|
pokememe.Description = description
|
2017-10-18 09:39:50 +04:00
|
|
|
|
pokememe.Attack = attackInt
|
|
|
|
|
pokememe.HP = hpInt
|
|
|
|
|
pokememe.MP = mpInt
|
|
|
|
|
pokememe.Defence = defenceInt
|
|
|
|
|
pokememe.Price = priceInt
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if purchaseable {
|
|
|
|
|
pokememe.Purchaseable = true
|
|
|
|
|
} else {
|
|
|
|
|
pokememe.Purchaseable = false
|
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
pokememe.ImageURL = image
|
|
|
|
|
pokememe.PlayerID = playerRaw.ID
|
|
|
|
|
pokememe.CreatedAt = time.Now().UTC()
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
_, 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 {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err4.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Getting new pokememe
|
|
|
|
|
err5 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='"+grade+"' AND name='"+name+"';"))
|
|
|
|
|
if err5 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Pokememe isn't added!")
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
for i := range elements {
|
|
|
|
|
link := dbmapping.PokememeElement{}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
link.PokememeID = pokememe.ID
|
|
|
|
|
link.ElementID = elements[i].ID
|
|
|
|
|
link.CreatedAt = time.Now().UTC()
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
_, err6 := c.Db.NamedExec("INSERT INTO pokememes_elements VALUES(NULL, :pokememe_id, :element_id, :created_at)", &link)
|
|
|
|
|
if err6 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err6.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for i := range locations {
|
|
|
|
|
link := dbmapping.PokememeLocation{}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
link.PokememeID = pokememe.ID
|
|
|
|
|
link.LocationID = locations[i].ID
|
|
|
|
|
link.CreatedAt = time.Now().UTC()
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
_, err7 := c.Db.NamedExec("INSERT INTO pokememes_locations VALUES(NULL, :pokememe_id, :location_id, :created_at)", &link)
|
|
|
|
|
if err7 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err7.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddSuccessMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "ok"
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|