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

154 lines
4.6 KiB
Go
Raw Normal View History

// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package pokedexer
import (
2018-01-21 23:28:53 +04:00
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
"github.com/go-telegram-bot-api/telegram-bot-api"
2017-10-18 07:03:34 +04:00
"regexp"
"strconv"
"strings"
// "time"
)
// ParsePokememe parses pokememe, forwarded from PokememeBroBot, to database
func (p *Pokedexer) ParsePokememe(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
pokememeStringsArray := strings.Split(update.Message.Text, "\n")
pokememeRunesArray := make([][]rune, 0)
for i := range pokememeStringsArray {
pokememeRunesArray = append(pokememeRunesArray, []rune(pokememeStringsArray[i]))
2017-10-18 07:03:34 +04:00
}
pokememeData := make(map[string]string)
pokememeLocations := make(map[string]string)
pokememeElements := make(map[string]string)
2017-10-18 07:03:34 +04:00
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:])
2017-10-18 07:03:34 +04:00
}
if i == 1 {
pokememeData["description"] = string(pokememeRunesArray[i])
2017-10-18 07:03:34 +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
}
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
}
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
}
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
}
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
}
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
}
}
// 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
}
// 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
}
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)
2017-10-18 07:03:34 +04:00
return "fail"
}
if len(pokememeLocations) == 0 {
p.pokememeAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
2018-01-30 00:03:02 +04:00
_, err = c.DataCache.GetPokememeByName(pokememeData["name"])
if err == nil {
// There is already a pokememe with such name
p.pokememeAddDuplicateMessage(update)
return "fail"
}
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
}
p.pokememeAddSuccessMessage(update, newPokememeID)
2017-10-18 07:03:34 +04:00
return "ok"
}