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 (
|
2018-01-29 23:50:25 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
"regexp"
|
2018-02-14 00:09:58 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// "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 {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
pokememeStringsArray := strings.Split(update.Message.Text, "\n")
|
2017-10-18 09:39:50 +04:00
|
|
|
|
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
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
pokememeData := make(map[string]string)
|
|
|
|
|
pokememeLocations := make(map[string]string)
|
|
|
|
|
pokememeElements := make(map[string]string)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
hitPointsRx := regexp.MustCompile("(\\d|\\.)+(K|M)?")
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
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:])
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if i == 1 {
|
|
|
|
|
pokememeData["description"] = string(pokememeRunesArray[i])
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
}
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
}
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
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]
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
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]
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
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]
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(pokememeStringsArray[i], "Купить: ") {
|
|
|
|
|
// Purchaseability
|
|
|
|
|
pokememeData["purchaseable"] = "false"
|
|
|
|
|
if strings.Contains(pokememeStringsArray[i], "Можно") {
|
|
|
|
|
pokememeData["purchaseable"] = "true"
|
|
|
|
|
}
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// Image
|
|
|
|
|
for _, entity := range *update.Message.Entities {
|
|
|
|
|
if entity.Type == "text_link" && entity.URL != "" {
|
|
|
|
|
pokememeData["image"] = entity.URL
|
|
|
|
|
}
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// Checking grade to be integer
|
|
|
|
|
_, err := strconv.Atoi(pokememeData["grade"])
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
p.pokememeAddFailureMessage(update)
|
|
|
|
|
return "fail"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
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 {
|
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
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if len(pokememeLocations) == 0 {
|
2017-11-21 06:06:32 +04:00
|
|
|
|
p.pokememeAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
|
2018-01-30 00:03:02 +04:00
|
|
|
|
_, err = c.DataCache.GetPokememeByName(pokememeData["name"])
|
|
|
|
|
if err == nil {
|
2018-02-14 00:09:58 +04:00
|
|
|
|
// There is already a pokememe with such name, updating
|
|
|
|
|
pokememeID, err := c.DataCache.UpdatePokememe(pokememeData, pokememeLocations, pokememeElements)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
p.pokememeAddFailureMessage(update)
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.pokememeAddDuplicateMessage(update, pokememeID)
|
|
|
|
|
return "ok"
|
2018-01-30 00:03:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
newPokememeID, err := c.DataCache.AddPokememe(pokememeData, pokememeLocations, pokememeElements)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
p.pokememeAddFailureMessage(update)
|
|
|
|
|
return "fail"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
p.pokememeAddSuccessMessage(update, newPokememeID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "ok"
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|