2017-10-11 06:53:50 +04:00
// i2_bot – Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package parsers
import (
2017-10-18 07:03:34 +04:00
// stdlib
"log"
"regexp"
"strconv"
"strings"
"time"
// 3rd party
2017-10-11 06:53:50 +04:00
"github.com/go-telegram-bot-api/telegram-bot-api"
2017-10-18 07:03:34 +04:00
// local
"../dbmapping"
2017-10-11 06:53:50 +04:00
)
// Internal functions
2017-10-18 09:39:50 +04:00
func ( p * Parsers ) fillProfilePokememe ( profileID int , meme string , attack string , rarity string ) {
spkRaw := dbmapping . Pokememe { }
err := c . Db . Get ( & spkRaw , c . Db . Rebind ( "SELECT * FROM pokememes WHERE name='" + meme + "';" ) )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
} else {
2017-10-18 09:39:50 +04:00
attackInt := p . getPoints ( attack )
2017-10-18 07:03:34 +04:00
ppk := dbmapping . ProfilePokememe { }
2017-10-18 09:39:50 +04:00
ppk . ProfileID = profileID
ppk . PokememeID = spkRaw . ID
2017-10-22 13:13:20 +04:00
ppk . PokememeAttack = attackInt
2017-10-18 09:39:50 +04:00
ppk . PokememeRarity = rarity
ppk . CreatedAt = time . Now ( ) . UTC ( )
2017-10-22 13:13:20 +04:00
_ , err2 := c . Db . NamedExec ( "INSERT INTO `profiles_pokememes` VALUES(NULL, :profile_id, :pokememe_id, :pokememe_attack, :pokememe_rarity, :created_at)" , & ppk )
2017-10-18 07:03:34 +04:00
if err2 != nil {
log . Println ( err2 )
}
}
2017-10-11 06:53:50 +04:00
}
// External functions
2017-10-18 09:39:50 +04:00
// ParseProfile parses user profile, forwarded from PokememBroBot, to database
func ( p * Parsers ) ParseProfile ( update tgbotapi . Update , playerRaw dbmapping . Player ) string {
2017-10-18 07:03:34 +04:00
text := update . Message . Text
log . Println ( text )
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
profileStringsArray := strings . Split ( text , "\n" )
profileRunesArray := make ( [ ] [ ] rune , 0 )
for i := range profileStringsArray {
profileRunesArray = append ( profileRunesArray , [ ] rune ( profileStringsArray [ i ] ) )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 07:03:34 +04:00
league := dbmapping . League { }
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
telegramNickname := update . Message . From . UserName
2017-10-18 07:03:34 +04:00
nickname := ""
level := ""
2017-10-18 09:39:50 +04:00
levelInt := 0
2017-10-18 07:03:34 +04:00
exp := ""
2017-10-18 09:39:50 +04:00
expInt := 0
eggexp := ""
eggexpInt := 0
2017-10-18 07:03:34 +04:00
pokeballs := ""
2017-10-18 09:39:50 +04:00
pokeballsInt := 0
2017-10-18 07:03:34 +04:00
wealth := ""
2017-10-18 09:39:50 +04:00
wealthInt := 0
2017-10-22 13:13:20 +04:00
pokememesWealth := ""
pokememesWealthInt := 0
2017-10-18 07:03:34 +04:00
crystalls := ""
2017-10-18 09:39:50 +04:00
crystallsInt := 0
2017-10-18 07:03:34 +04:00
weapon := ""
pokememes := make ( map [ string ] string )
2017-10-18 09:39:50 +04:00
powerInt := 1
2017-10-11 06:53:50 +04:00
2017-10-18 07:03:34 +04:00
// Filling information
// We don't know how many strings we got, so we iterating each other
2017-10-18 09:39:50 +04:00
for i := range profileRunesArray {
currentString := string ( profileRunesArray [ i ] )
currentRunes := profileRunesArray [ i ]
if strings . HasPrefix ( currentString , "🈸" ) || strings . HasPrefix ( currentString , "🈳 " ) || strings . HasPrefix ( currentString , "🈵" ) {
err1 := c . Db . Get ( & league , c . Db . Rebind ( "SELECT * FROM leagues WHERE symbol='" + string ( currentRunes [ 0 ] ) + "'" ) )
2017-10-18 07:03:34 +04:00
if err1 != nil {
log . Println ( err1 )
return "fail"
}
2017-10-18 09:39:50 +04:00
for j := range currentRunes {
2017-10-18 07:03:34 +04:00
if j > 1 {
2017-10-18 09:39:50 +04:00
nickname += string ( currentRunes [ j ] )
2017-10-18 07:03:34 +04:00
}
}
}
2017-10-18 09:39:50 +04:00
if strings . HasPrefix ( currentString , "👤Уровень:" ) {
2017-10-18 07:03:34 +04:00
levelRx := regexp . MustCompile ( "\\d+" )
2017-10-18 09:39:50 +04:00
levelArray := levelRx . FindAllString ( currentString , - 1 )
if len ( levelArray ) < 1 {
2017-10-18 07:03:34 +04:00
log . Println ( "Level string broken" )
return "fail"
}
2017-10-18 09:39:50 +04:00
level = levelArray [ 0 ]
levelInt , _ = strconv . Atoi ( level )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
if strings . HasPrefix ( currentString , "🎓Опыт" ) {
2017-10-18 07:03:34 +04:00
expRx := regexp . MustCompile ( "\\d+" )
2017-10-18 09:39:50 +04:00
expArray := expRx . FindAllString ( currentString , - 1 )
if len ( expArray ) < 4 {
2017-10-18 07:03:34 +04:00
log . Println ( "Exp string broken" )
return "fail"
}
2017-10-18 09:39:50 +04:00
exp = expArray [ 0 ]
expInt , _ = strconv . Atoi ( exp )
eggexp = expArray [ 2 ]
eggexpInt , _ = strconv . Atoi ( eggexp )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
if strings . HasPrefix ( currentString , "⭕Покеболы" ) {
2017-10-18 07:03:34 +04:00
pkbRx := regexp . MustCompile ( "\\d+" )
2017-10-18 09:39:50 +04:00
pkbArray := pkbRx . FindAllString ( currentString , - 1 )
if len ( pkbArray ) < 2 {
2017-10-18 07:03:34 +04:00
log . Println ( "Pokeballs string broken" )
return "fail"
}
2017-10-18 09:39:50 +04:00
pokeballs = pkbArray [ 1 ]
pokeballsInt , _ = strconv . Atoi ( pokeballs )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
if strings . HasPrefix ( currentString , "💲" ) {
2017-10-18 07:03:34 +04:00
wealthRx := regexp . MustCompile ( "(\\d|\\.|K|M)+" )
2017-10-18 09:39:50 +04:00
wealthArray := wealthRx . FindAllString ( currentString , - 1 )
if len ( wealthArray ) < 2 {
2017-10-18 07:03:34 +04:00
log . Println ( "Wealth string broken" )
return "fail"
}
2017-10-18 09:39:50 +04:00
wealth = wealthArray [ 0 ]
wealthInt = p . getPoints ( wealth )
crystalls = wealthArray [ 1 ]
crystallsInt = p . getPoints ( crystalls )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
if strings . HasPrefix ( currentString , "🔫" ) {
2017-10-18 07:03:34 +04:00
// We need NEXT string!
2017-10-18 09:39:50 +04:00
weaponType := strings . Replace ( currentString , "🔫 " , "" , 1 )
2017-10-18 07:03:34 +04:00
wnRx := regexp . MustCompile ( "(.+)(ита)" )
2017-10-18 09:39:50 +04:00
weapon = wnRx . FindString ( weaponType )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
if strings . HasPrefix ( currentString , "🐱Покемемы: " ) {
2017-10-22 13:13:20 +04:00
pkmnumRx := regexp . MustCompile ( ` (\d+)(\d|K|M|) ` )
2017-10-18 09:39:50 +04:00
pkNumArray := pkmnumRx . FindAllString ( currentString , - 1 )
2017-10-22 13:13:20 +04:00
if len ( pkNumArray ) < 3 {
2017-10-18 07:03:34 +04:00
log . Println ( "Pokememes count broken" )
return "fail"
}
2017-10-18 09:39:50 +04:00
pokememesCount , _ := strconv . Atoi ( pkNumArray [ 0 ] )
2017-10-22 13:13:20 +04:00
pokememesWealth = pkNumArray [ 2 ]
pokememesWealthInt = p . getPoints ( pokememesWealth )
2017-10-18 09:39:50 +04:00
if pokememesCount > 0 {
for pi := 0 ; pi < pokememesCount ; pi ++ {
pokememeString := string ( profileRunesArray [ i + 1 + pi ] )
2017-10-18 07:03:34 +04:00
attackRx := regexp . MustCompile ( "(\\d|\\.|K|M)+" )
2017-10-18 09:39:50 +04:00
pkPointsArray := attackRx . FindAllString ( pokememeString , - 1 )
pkAttack := pkPointsArray [ 1 ]
pkName := strings . Split ( pokememeString , "+" ) [ 0 ]
pkName = strings . Replace ( pkName , " ⭐" , "" , 1 )
pkName = strings . TrimSuffix ( pkName , " " )
pkName = strings . Split ( pkName , "⃣ " ) [ 1 ]
pokememes [ pkName ] = pkAttack
powerInt += p . getPoints ( pkAttack )
2017-10-18 07:03:34 +04:00
}
}
}
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
log . Printf ( "Telegram nickname: " + telegramNickname )
2017-10-18 07:03:34 +04:00
log . Printf ( "Nickname: " + nickname )
log . Printf ( "League: " + league . Name )
log . Printf ( "Level: " + level )
2017-10-18 09:39:50 +04:00
log . Println ( levelInt )
2017-10-18 07:03:34 +04:00
log . Printf ( "Exp: " + exp )
2017-10-18 09:39:50 +04:00
log . Println ( expInt )
log . Printf ( "Egg exp: " + eggexp )
log . Println ( eggexpInt )
2017-10-18 07:03:34 +04:00
log . Printf ( "Pokeballs: " + pokeballs )
2017-10-18 09:39:50 +04:00
log . Println ( pokeballsInt )
2017-10-18 07:03:34 +04:00
log . Printf ( "Wealth: " + wealth )
2017-10-18 09:39:50 +04:00
log . Println ( wealthInt )
2017-10-18 07:03:34 +04:00
log . Printf ( "Crystalls: " + crystalls )
2017-10-18 09:39:50 +04:00
log . Println ( crystallsInt )
2017-10-18 07:03:34 +04:00
log . Printf ( "Weapon: " + weapon )
if len ( pokememes ) > 0 {
2017-10-22 13:13:20 +04:00
log . Printf ( "Hand cost: " + pokememesWealth )
log . Println ( pokememesWealthInt )
2017-10-18 07:03:34 +04:00
for meme , attack := range pokememes {
log . Printf ( meme + ": " + attack )
}
} else {
log . Printf ( "Hand is empty." )
}
2017-10-11 06:53:50 +04:00
2017-10-18 07:03:34 +04:00
// Information is gathered, let's create profile in database!
2017-10-18 09:39:50 +04:00
weaponRaw := dbmapping . Weapon { }
err2 := c . Db . Get ( & weaponRaw , c . Db . Rebind ( "SELECT * FROM weapons WHERE name='" + weapon + "'" ) )
2017-10-18 07:03:34 +04:00
if err2 != nil {
log . Println ( err2 )
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
if playerRaw . LeagueID == 0 {
2017-10-18 07:03:34 +04:00
// Updating player with league
2017-10-18 09:39:50 +04:00
playerRaw . LeagueID = league . ID
if playerRaw . Status == "nobody" {
playerRaw . Status = "common"
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
_ , err4 := c . Db . NamedExec ( "UPDATE `players` SET league_id=:league_id, status=:status WHERE id=:id" , & playerRaw )
2017-10-18 07:03:34 +04:00
if err4 != nil {
log . Println ( err4 )
return "fail"
}
2017-10-18 09:39:50 +04:00
} else if playerRaw . LeagueID != league . ID {
2017-10-18 07:03:34 +04:00
// Duplicate profile: user changed league, beware!
2017-10-18 09:39:50 +04:00
playerRaw . LeagueID = league . ID
playerRaw . SquadID = 0
playerRaw . Status = "league_changed"
playerRaw . CreatedAt = time . Now ( ) . UTC ( )
_ , err5 := c . Db . NamedExec ( "INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)" , & playerRaw )
2017-10-18 07:03:34 +04:00
if err5 != nil {
log . Println ( err5 )
return "fail"
}
2017-10-18 09:39:50 +04:00
err6 := c . Db . Get ( & playerRaw , c . Db . Rebind ( "SELECT * FROM players WHERE telegram_id='" + strconv . Itoa ( playerRaw . TelegramID ) + "' AND league_id='" + strconv . Itoa ( league . ID ) + "';" ) )
2017-10-18 07:03:34 +04:00
if err6 != nil {
log . Println ( err6 )
return "fail"
}
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
profileRaw := dbmapping . Profile { }
profileRaw . PlayerID = playerRaw . ID
profileRaw . Nickname = nickname
profileRaw . TelegramNickname = telegramNickname
profileRaw . LevelID = levelInt
profileRaw . Pokeballs = pokeballsInt
profileRaw . Wealth = wealthInt
2017-10-22 13:13:20 +04:00
profileRaw . PokememesWealth = pokememesWealthInt
2017-10-18 09:39:50 +04:00
profileRaw . Exp = expInt
profileRaw . EggExp = eggexpInt
profileRaw . Power = powerInt
profileRaw . WeaponID = weaponRaw . ID
profileRaw . Crystalls = crystallsInt
profileRaw . CreatedAt = time . Now ( ) . UTC ( )
2017-10-11 06:53:50 +04:00
2017-10-22 13:13:20 +04:00
_ , err3 := c . Db . NamedExec ( "INSERT INTO `profiles` VALUES(NULL, :player_id, :nickname, :telegram_nickname, :level_id, :pokeballs, :wealth, :pokememes_wealth, :exp, :egg_exp, :power, :weapon_id, :crystalls, :created_at)" , & profileRaw )
2017-10-18 07:03:34 +04:00
if err3 != nil {
log . Println ( err3 )
return "fail"
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
err8 := c . Db . Get ( & profileRaw , c . Db . Rebind ( "SELECT * FROM profiles WHERE player_id=? AND created_at=?" ) , profileRaw . PlayerID , profileRaw . CreatedAt )
2017-10-18 07:03:34 +04:00
if err8 != nil {
log . Println ( err8 )
log . Printf ( "Profile isn't added!" )
return "fail"
}
2017-10-11 06:53:50 +04:00
2017-10-18 09:39:50 +04:00
playerRaw . UpdatedAt = time . Now ( ) . UTC ( )
_ , err7 := c . Db . NamedExec ( "UPDATE `players` SET updated_at=:updated_at WHERE id=:id" , & playerRaw )
2017-10-18 07:03:34 +04:00
if err7 != nil {
log . Println ( err7 )
return "fail"
}
2017-10-11 06:53:50 +04:00
2017-10-18 07:03:34 +04:00
for meme , attack := range pokememes {
rarity := "common"
if strings . HasPrefix ( meme , "🔸" ) {
rarity = "rare"
meme = strings . Replace ( meme , "🔸" , "" , 1 )
}
2017-10-22 13:13:20 +04:00
if strings . HasPrefix ( meme , "🔶" ) {
rarity = "super rare"
meme = strings . Replace ( meme , "🔶" , "" , 1 )
}
if strings . HasPrefix ( meme , "🔹" ) {
rarity = "liber"
meme = strings . Replace ( meme , "🔹" , "" , 1 )
}
if strings . HasPrefix ( meme , "🔷" ) {
rarity = "super liber"
meme = strings . Replace ( meme , "🔷" , "" , 1 )
}
2017-10-18 09:39:50 +04:00
p . fillProfilePokememe ( profileRaw . ID , meme , attack , rarity )
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
2017-10-18 07:03:34 +04:00
return "ok"
2017-10-11 06:53:50 +04:00
}