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/users/parsers.go

274 lines
7.7 KiB
Go
Raw Normal View History

2017-10-11 06:53:50 +04:00
// i2_bot Instinct PokememBro Bot
2018-05-02 07:25:39 +04:00
// Copyright (c) 2017-2018 Vladimir "fat0troll" Hodakov
2017-10-11 06:53:50 +04:00
package users
2017-10-11 06:53:50 +04:00
import (
"fmt"
2017-10-18 07:03:34 +04:00
"strconv"
"strings"
"time"
"github.com/go-telegram-bot-api/telegram-bot-api"
2018-05-19 12:14:25 +04:00
"github.com/fat0troll/i2_bot/lib/dbmapping"
2017-10-11 06:53:50 +04:00
)
// Internal functions
func (u *Users) fillProfilePokememe(profileID int, pokememeID int, attack string, rarity string) {
spkRaw, err := c.DataCache.GetPokememeByID(pokememeID)
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-10-18 07:03:34 +04:00
} else {
attackInt := c.Statistics.GetPoints(attack)
2017-10-18 07:03:34 +04:00
ppk := dbmapping.ProfilePokememe{}
ppk.ProfileID = profileID
ppk.PokememeID = spkRaw.Pokememe.ID
ppk.PokememeAttack = attackInt
ppk.PokememeRarity = rarity
ppk.CreatedAt = time.Now().UTC()
_, 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 {
2017-11-14 03:44:21 +04:00
c.Log.Error(err2.Error())
2017-10-18 07:03:34 +04:00
}
}
2017-10-11 06:53:50 +04:00
}
// External functions
// ParseProfile parses user profile, forwarded from PokememBroBot, to database
func (u *Users) ParseProfile(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
2017-10-18 07:03:34 +04:00
text := update.Message.Text
2017-11-14 03:44:21 +04:00
c.Log.Info(text)
2017-10-11 06:53:50 +04:00
profileStringsArray := strings.Split(text, "\n")
2017-10-11 06:53:50 +04:00
telegramNickname := update.Message.From.UserName
rawProfileData := make(map[string]string)
rawCurrentHandData := make(map[string]string)
currentHand := 0
profilePower := 0
2017-10-11 06:53:50 +04:00
// Not using range here, because range is picking elements in random order
for i := 0; i < len(profileStringsArray); i++ {
infoString := profileStringsArray[i]
c.Log.Debug("Processing string: " + infoString)
if strings.HasPrefix(infoString, "CurrentHand ") {
currentHandNumber := strings.Split(infoString, " ")[1]
currentHandInt, err := strconv.Atoi(currentHandNumber)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
currentHand = currentHandInt
} else if strings.HasPrefix(infoString, ("Hand" + strconv.Itoa(currentHand))) {
if strings.HasPrefix(infoString, ("Hand"+strconv.Itoa(currentHand))+" Attack") {
rawProfileData["currentHandAttack"] = strings.Join(strings.Split(infoString, " ")[2:], " ")
} else {
rawCurrentHandData[strconv.Itoa(len(rawCurrentHandData))] = strings.Join(strings.Split(infoString, " ")[1:], " ")
2017-10-18 07:03:34 +04:00
}
} else {
rawProfileData[strings.Split(infoString, " ")[0]] = strings.Join(strings.Split(infoString, " ")[1:], " ")
2017-10-18 07:03:34 +04:00
}
}
2017-10-11 06:53:50 +04:00
fmt.Println(rawProfileData)
2017-10-11 06:53:50 +04:00
nickname := rawProfileData["Name"]
league, err := c.DataCache.GetLeagueByName(rawProfileData["Team"])
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
level := rawProfileData["Lvl"]
levelInt, err := strconv.Atoi(level)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
exp := rawProfileData["Exp"]
expInt, err := strconv.Atoi(exp)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
eggexp := strings.Split(rawProfileData["Eggs"], "/")[0]
eggexpInt, err := strconv.Atoi(eggexp)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
pokeballs := rawProfileData["BallsMax"]
pokeballsInt, err := strconv.Atoi(pokeballs)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
wealth := rawProfileData["Money"]
wealthInt, err := strconv.Atoi(wealth)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
crystals := rawProfileData["Crystalls"]
crystalsInt, err := strconv.Atoi(crystals)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
2017-10-18 07:03:34 +04:00
}
weapon := rawProfileData["Weapon"]
weaponAttack, err := strconv.Atoi(weapon)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
currentHandAttack := rawProfileData["currentHandAttack"]
currentHandAttackInt, err := strconv.Atoi(currentHandAttack)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
return "fail"
}
profilePower = weaponAttack + currentHandAttackInt
2017-10-11 06:53:50 +04:00
2017-11-14 03:44:21 +04:00
c.Log.Debug("Telegram nickname: " + telegramNickname)
c.Log.Debug("Nickname: " + nickname)
c.Log.Debug("League: " + league.Name)
c.Log.Debug("Level: " + level)
c.Log.Debugln(levelInt)
c.Log.Debug("Exp: " + exp)
c.Log.Debugln(expInt)
c.Log.Debug("Egg exp: " + eggexp)
c.Log.Debugln(eggexpInt)
c.Log.Debug("Pokeballs: " + pokeballs)
c.Log.Debugln(pokeballsInt)
c.Log.Debug("Wealth: " + wealth)
c.Log.Debugln(wealthInt)
c.Log.Debug("Crystals: " + crystals)
2018-05-02 07:25:39 +04:00
c.Log.Debugln(crystalsInt)
c.Log.Debug("Weapon attack: " + weapon)
c.Log.Debugln(weaponAttack)
c.Log.Debug("Current hand attack: " + currentHandAttack)
c.Log.Debugln(currentHandAttackInt)
if len(rawCurrentHandData) > 0 {
for i := range rawCurrentHandData {
c.Log.Debug(rawCurrentHandData[i])
2017-10-18 07:03:34 +04:00
}
} else {
2017-11-14 03:44:21 +04:00
c.Log.Debug("Hand is empty.")
2017-10-18 07:03:34 +04:00
}
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!
weaponRaw, err := c.DataCache.GetWeaponTypeByAttack(weaponAttack)
if err != nil {
c.Log.Error(err.Error())
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
if weaponRaw != nil {
c.Log.Debug("Got weapon: " + weaponRaw.Name)
} else {
c.Log.Debug("This profile contains no weapon")
}
if playerRaw.LeagueID == 0 {
2017-10-18 07:03:34 +04:00
// Updating player with league
playerRaw.LeagueID = league.ID
if playerRaw.Status == "nobody" {
playerRaw.Status = "common"
2017-10-18 07:03:34 +04:00
}
_, err = c.DataCache.UpdatePlayerFields(playerRaw)
if err != nil {
u.profileAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
} else if playerRaw.LeagueID != league.ID {
// User changed league, beware!
playerRaw.LeagueID = league.ID
playerRaw.Status = "league_changed"
_, err = c.DataCache.UpdatePlayerFields(playerRaw)
if err != nil {
u.profileAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
}
2017-10-11 06:53: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
// TODO: count pokememes wealth
profileRaw.PokememesWealth = 0
profileRaw.Exp = expInt
profileRaw.EggExp = eggexpInt
profileRaw.Power = profilePower
2018-01-30 18:41:23 +04:00
if weaponRaw != nil {
profileRaw.WeaponID = weaponRaw.ID
} else {
profileRaw.WeaponID = 0
}
2018-05-02 07:25:39 +04:00
profileRaw.Crystals = crystalsInt
profileRaw.CreatedAt = time.Now().UTC()
2017-10-11 06:53:50 +04:00
newProfileID, err := c.DataCache.AddProfile(&profileRaw)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
2017-10-11 06:53:50 +04:00
_, err = c.DataCache.GetProfileByID(newProfileID)
if err != nil {
c.Log.Error(err.Error())
u.profileAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
2017-10-11 06:53:50 +04:00
err = c.DataCache.UpdatePlayerTimestamp(playerRaw.ID)
if err != nil {
u.profileAddFailureMessage(update)
2017-10-18 07:03:34 +04:00
return "fail"
}
2017-10-11 06:53:50 +04:00
for i := range rawCurrentHandData {
pokememeInfoArray := strings.Split(rawCurrentHandData[i], " ")
pokememePokedexNumber := pokememeInfoArray[1]
pokememePokedexID, err := strconv.Atoi(pokememePokedexNumber)
if err != nil {
c.Log.Error(err.Error())
2017-12-28 05:57:51 +04:00
}
pokememeRarityName := "common"
pokememeRarity := pokememeInfoArray[3]
pokememeAttack := pokememeInfoArray[7]
switch pokememeRarity {
case "1":
pokememeRarityName = "rare"
case "2":
pokememeRarityName = "super rare"
case "3":
pokememeRarityName = "liber"
case "4":
pokememeRarityName = "super liber"
case "7":
pokememeRarityName = "new year"
case "8":
pokememeRarityName = "valentine"
}
u.fillProfilePokememe(newProfileID, pokememePokedexID, pokememeAttack, pokememeRarityName)
2017-10-18 07:03:34 +04:00
}
2017-10-11 06:53:50 +04:00
u.profileAddSuccessMessage(update, league.ID, profileRaw.LevelID)
2017-10-18 07:03:34 +04:00
return "ok"
2017-10-11 06:53:50 +04:00
}