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
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
package users
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
|
|
|
|
import (
|
2018-05-02 08:48:22 +04:00
|
|
|
|
"fmt"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2018-03-31 23:07:05 +04:00
|
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
2018-05-02 08:48:22 +04:00
|
|
|
|
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 {
|
2017-11-21 06:06:32 +04:00
|
|
|
|
attackInt := c.Statistics.GetPoints(attack)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
ppk := dbmapping.ProfilePokememe{}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
ppk.ProfileID = profileID
|
2018-01-29 23:50:25 +04:00
|
|
|
|
ppk.PokememeID = spkRaw.Pokememe.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 {
|
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
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// ParseProfile parses user profile, forwarded from PokememBroBot, to database
|
2017-11-21 06:06:32 +04:00
|
|
|
|
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
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
profileStringsArray := strings.Split(text, "\n")
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
telegramNickname := update.Message.From.UserName
|
2018-05-02 08:48:22 +04:00
|
|
|
|
rawProfileData := make(map[string]string)
|
|
|
|
|
rawCurrentHandData := make(map[string]string)
|
|
|
|
|
currentHand := 0
|
|
|
|
|
profilePower := 0
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-05-02 08:48:22 +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)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
u.profileAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2018-05-02 08:48:22 +04:00
|
|
|
|
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
|
|
|
|
}
|
2018-05-02 08:48:22 +04:00
|
|
|
|
} else {
|
|
|
|
|
rawProfileData[strings.Split(infoString, " ")[0]] = strings.Join(strings.Split(infoString, " ")[1:], " ")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-05-02 08:48:22 +04:00
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-05-02 08:48:22 +04:00
|
|
|
|
fmt.Println(rawProfileData)
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-05-02 08:48:22 +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
|
|
|
|
}
|
2018-05-02 08:48:22 +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)
|
2018-05-02 08:48:22 +04:00
|
|
|
|
c.Log.Debug("Crystals: " + crystals)
|
2018-05-02 07:25:39 +04:00
|
|
|
|
c.Log.Debugln(crystalsInt)
|
2018-05-02 08:48:22 +04:00
|
|
|
|
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!
|
2018-05-02 08:48:22 +04:00
|
|
|
|
weaponRaw, err := c.DataCache.GetWeaponTypeByAttack(weaponAttack)
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-05-02 08:48:22 +04:00
|
|
|
|
if weaponRaw != nil {
|
|
|
|
|
c.Log.Debug("Got weapon: " + weaponRaw.Name)
|
|
|
|
|
} else {
|
|
|
|
|
c.Log.Debug("This profile contains no weapon")
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
_, err = c.DataCache.UpdatePlayerFields(playerRaw)
|
|
|
|
|
if err != nil {
|
2017-11-21 06:06:32 +04:00
|
|
|
|
u.profileAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
} else if playerRaw.LeagueID != league.ID {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// User changed league, beware!
|
2017-10-18 09:39:50 +04:00
|
|
|
|
playerRaw.LeagueID = league.ID
|
|
|
|
|
playerRaw.Status = "league_changed"
|
2018-01-29 23:50:25 +04:00
|
|
|
|
_, err = c.DataCache.UpdatePlayerFields(playerRaw)
|
|
|
|
|
if err != nil {
|
2017-11-21 06:06:32 +04:00
|
|
|
|
u.profileAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
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
|
2018-05-02 08:48:22 +04:00
|
|
|
|
// TODO: count pokememes wealth
|
|
|
|
|
profileRaw.PokememesWealth = 0
|
2017-10-18 09:39:50 +04:00
|
|
|
|
profileRaw.Exp = expInt
|
|
|
|
|
profileRaw.EggExp = eggexpInt
|
2018-05-02 08:48:22 +04:00
|
|
|
|
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
|
2017-10-18 09:39:50 +04:00
|
|
|
|
profileRaw.CreatedAt = time.Now().UTC()
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
newProfileID, err := c.DataCache.AddProfile(&profileRaw)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
u.profileAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
_, err = c.DataCache.GetProfileByID(newProfileID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-21 06:06:32 +04:00
|
|
|
|
u.profileAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
err = c.DataCache.UpdatePlayerTimestamp(playerRaw.ID)
|
|
|
|
|
if err != nil {
|
2017-11-21 06:06:32 +04:00
|
|
|
|
u.profileAddFailureMessage(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2018-05-02 08:48:22 +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
|
|
|
|
}
|
2018-05-02 08:48:22 +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"
|
2018-02-13 22:05:32 +04:00
|
|
|
|
}
|
2018-05-02 08:48:22 +04:00
|
|
|
|
u.fillProfilePokememe(newProfileID, pokememePokedexID, pokememeAttack, pokememeRarityName)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-11-29 10:36:36 +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
|
|
|
|
}
|