2017-10-11 06:53:50 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
package users
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
|
|
|
|
import (
|
2018-01-21 23:28:53 +04:00
|
|
|
|
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
2018-01-23 20:13:16 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"strconv"
|
2017-11-24 00:16:22 +04:00
|
|
|
|
"strings"
|
2017-10-11 06:53:50 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-11-25 03:44:58 +04:00
|
|
|
|
// FormatUsername formats Telegram username for posting
|
|
|
|
|
func (u *Users) FormatUsername(userName string) string {
|
2017-12-23 17:03:26 +04:00
|
|
|
|
return strings.Replace(userName, "_", `\_`, -1)
|
2017-11-25 03:44:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 20:13:16 +04:00
|
|
|
|
// FindByLevel finds user with level and recent profile update
|
|
|
|
|
func (u *Users) FindByLevel(update *tgbotapi.Update) string {
|
2017-12-02 13:45:24 +04:00
|
|
|
|
commandArgs := update.Message.CommandArguments()
|
|
|
|
|
if commandArgs == "" {
|
|
|
|
|
c.Talkers.BotError(update)
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 20:13:16 +04:00
|
|
|
|
levelID, err := strconv.Atoi(commandArgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
users := u.findUsersByLevel(levelID)
|
2017-12-02 13:45:24 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
u.foundUsersMessage(update, users)
|
2018-01-23 20:13:16 +04:00
|
|
|
|
|
|
|
|
|
return "ok"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindByName finds user with such username or nickname
|
|
|
|
|
func (u *Users) FindByName(update *tgbotapi.Update) string {
|
|
|
|
|
commandArgs := update.Message.CommandArguments()
|
|
|
|
|
if commandArgs == "" {
|
|
|
|
|
c.Talkers.BotError(update)
|
|
|
|
|
return "fail"
|
2017-12-02 13:45:24 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
users := u.findUserByName(commandArgs)
|
2017-12-02 13:45:24 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
u.foundUsersMessage(update, users)
|
2017-12-02 13:45:24 +04:00
|
|
|
|
|
|
|
|
|
return "ok"
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 13:00:18 +04:00
|
|
|
|
// FindByTopAttack finds user by top-attack rating
|
|
|
|
|
func (u *Users) FindByTopAttack(update *tgbotapi.Update) string {
|
|
|
|
|
commandArgs := update.Message.CommandArguments()
|
|
|
|
|
if commandArgs == "" {
|
|
|
|
|
c.Talkers.BotError(update)
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attackInt, err := strconv.Atoi(commandArgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
c.Talkers.BotError(update)
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
users := u.findUserByTopAttack(attackInt)
|
|
|
|
|
|
|
|
|
|
u.foundUsersMessage(update, users)
|
|
|
|
|
|
|
|
|
|
return "ok"
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:55:18 +04:00
|
|
|
|
// ForeignProfileMessage shows profile of another user
|
|
|
|
|
func (u *Users) ForeignProfileMessage(update *tgbotapi.Update) string {
|
|
|
|
|
userNum := strings.TrimPrefix(update.Message.Command(), "profile")
|
|
|
|
|
userID, err := strconv.Atoi(userNum)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
playerRaw, err := c.DataCache.GetPlayerByID(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-12-01 10:55:18 +04:00
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
_, err = c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-12-01 10:55:18 +04:00
|
|
|
|
return c.Talkers.BotError(update)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
return u.ProfileMessage(update, playerRaw)
|
2017-12-01 10:55:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
// ProfileAddEffectsMessage shows when user tries to post profile with effects enabled
|
2018-01-23 20:50:21 +04:00
|
|
|
|
func (u *Users) ProfileAddEffectsMessage(update *tgbotapi.Update) string {
|
|
|
|
|
message := "*Наркоман, штоле?*\n\n"
|
|
|
|
|
message += "Бот не принимает профили во время активированных эффектов. Закончи свои дела и принеси чистый профиль через полчаса."
|
|
|
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
|
|
|
|
msg.ParseMode = "Markdown"
|
|
|
|
|
|
|
|
|
|
c.Bot.Send(msg)
|
|
|
|
|
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// ProfileMessage shows current player's profile
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (u *Users) ProfileMessage(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
|
2018-01-29 23:50:25 +04:00
|
|
|
|
profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-25 03:00:34 +04:00
|
|
|
|
return c.Talkers.AnyMessageUnauthorized(update)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
league := dbmapping.League{}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
err = c.Db.Get(&league, c.Db.Rebind("SELECT * FROM leagues WHERE id=?"), playerRaw.LeagueID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
level := dbmapping.Level{}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
err = c.Db.Get(&level, c.Db.Rebind("SELECT * FROM levels WHERE id=?"), profileRaw.LevelID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2018-02-07 15:59:28 +04:00
|
|
|
|
weapon, err := c.DataCache.GetWeaponTypeByID(profileRaw.WeaponID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// It's non critical
|
|
|
|
|
c.Log.Debug(err.Error())
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-18 09:39:50 +04:00
|
|
|
|
profilePokememes := []dbmapping.ProfilePokememe{}
|
|
|
|
|
err = c.Db.Select(&profilePokememes, c.Db.Rebind("SELECT * FROM profiles_pokememes WHERE profile_id=?"), profileRaw.ID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
pokememes := []dbmapping.Pokememe{}
|
|
|
|
|
err = c.Db.Select(&pokememes, c.Db.Rebind("SELECT * FROM pokememes"))
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
attackPokememes := 0
|
|
|
|
|
for i := range profilePokememes {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
for j := range pokememes {
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if profilePokememes[i].PokememeID == pokememes[j].ID {
|
2017-10-22 13:13:20 +04:00
|
|
|
|
singleAttack := profilePokememes[i].PokememeAttack
|
|
|
|
|
attackPokememes += singleAttack
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
message := "*Профиль игрока "
|
2017-11-25 03:44:58 +04:00
|
|
|
|
message += profileRaw.Nickname + "*"
|
|
|
|
|
if profileRaw.TelegramNickname != "" {
|
|
|
|
|
message += " (@" + u.FormatUsername(profileRaw.TelegramNickname) + ")"
|
|
|
|
|
}
|
2017-10-18 07:03:34 +04:00
|
|
|
|
message += "\nЛига: " + league.Symbol + league.Name
|
2017-10-18 09:39:50 +04:00
|
|
|
|
message += "\n👤 " + strconv.Itoa(profileRaw.LevelID)
|
|
|
|
|
message += " | 🎓 " + strconv.Itoa(profileRaw.Exp) + "/" + strconv.Itoa(level.MaxExp)
|
|
|
|
|
message += " | 🥚 " + strconv.Itoa(profileRaw.EggExp) + "/" + strconv.Itoa(level.MaxEgg)
|
2017-11-21 06:06:32 +04:00
|
|
|
|
message += "\n💲" + c.Statistics.GetPrintablePoints(profileRaw.Wealth)
|
2017-10-18 09:39:50 +04:00
|
|
|
|
message += " |💎" + strconv.Itoa(profileRaw.Crystalls)
|
|
|
|
|
message += " |⭕" + strconv.Itoa(profileRaw.Pokeballs)
|
2018-01-23 20:37:48 +04:00
|
|
|
|
message += "\n⚔Атака: " + c.Statistics.GetPrintablePoints(weapon.Power) + " + " + c.Statistics.GetPrintablePoints(attackPokememes) + "\n"
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if profileRaw.WeaponID != 0 {
|
2017-11-21 06:06:32 +04:00
|
|
|
|
message += "\n🔫Оружие: " + weapon.Name + " " + c.Statistics.GetPrintablePoints(weapon.Power) + "⚔"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
message += "\n🐱Покемемы:"
|
2017-10-18 09:39:50 +04:00
|
|
|
|
for i := range profilePokememes {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
for j := range pokememes {
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if profilePokememes[i].PokememeID == pokememes[j].ID {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
message += "\n" + strconv.Itoa(pokememes[j].Grade)
|
|
|
|
|
message += "⃣ " + pokememes[j].Name
|
2017-11-21 06:06:32 +04:00
|
|
|
|
message += " +" + c.Statistics.GetPrintablePoints(profilePokememes[i].PokememeAttack) + "⚔"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-21 06:06:32 +04:00
|
|
|
|
message += "\nСтоимость покемемов на руках: " + c.Statistics.GetPrintablePoints(profileRaw.PokememesWealth) + "$"
|
2017-10-18 09:39:50 +04:00
|
|
|
|
message += "\n\n💳" + strconv.Itoa(playerRaw.TelegramID)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
|
|
|
|
|
if playerRaw.Status == "owner" {
|
|
|
|
|
message += "\n\nСтатус в боте: _владелец_"
|
|
|
|
|
} else if playerRaw.Status == "admin" {
|
|
|
|
|
message += "\n\nСтатус в боте: _администратор_"
|
2017-12-01 10:55:18 +04:00
|
|
|
|
} else if playerRaw.Status == "academic" {
|
|
|
|
|
message += "\n\nСтатус в боте: _академик_"
|
2017-11-24 00:16:22 +04:00
|
|
|
|
} else {
|
|
|
|
|
message += "\n\nСтатус в боте: _игрок_"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
squadRoles, ok := c.Squader.GetUserRolesInSquads(playerRaw)
|
|
|
|
|
if ok && len(squadRoles) > 0 {
|
|
|
|
|
for i := range squadRoles {
|
|
|
|
|
if squadRoles[i].UserRole == "commander" {
|
|
|
|
|
message += "\nКомандир отряда " + squadRoles[i].Squad.Chat.Name
|
|
|
|
|
} else {
|
|
|
|
|
message += "\nУчастник отряда " + squadRoles[i].Squad.Chat.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message += "\n\n⏰Последнее обновление профиля: " + profileRaw.CreatedAt.Format("02.01.2006 15:04:05")
|
|
|
|
|
message += "\nНе забывай обновляться, это важно для получения актуальной информации.\n\n"
|
2018-02-05 12:37:46 +04:00
|
|
|
|
message += "/best – посмотреть лучших покемемов для поимки\n"
|
2018-02-07 15:00:04 +04:00
|
|
|
|
message += "/advice – посмотреть самых дорогих покемемов для поимки\n"
|
2018-02-05 12:37:46 +04:00
|
|
|
|
message += "/top — посмотреть лучших игроков лиги\n"
|
|
|
|
|
message += "/top\\_my — посмотреть лучших игроков лиги твоего уровня\n"
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-11-25 03:44:58 +04:00
|
|
|
|
c.Log.Debug(message)
|
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
|
|
|
|
msg.ParseMode = "Markdown"
|
2017-10-11 06:53:50 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Bot.Send(msg)
|
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
|
|
|
|
}
|
2017-11-24 00:16:22 +04:00
|
|
|
|
|
|
|
|
|
// UsersList lists all known users
|
|
|
|
|
func (u *Users) UsersList(update *tgbotapi.Update) string {
|
|
|
|
|
pageNumber := strings.Replace(update.Message.Text, "/users", "", 1)
|
|
|
|
|
pageNumber = strings.Replace(pageNumber, "/users", "", 1)
|
|
|
|
|
page, _ := strconv.Atoi(pageNumber)
|
|
|
|
|
if page == 0 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
2018-01-29 23:50:25 +04:00
|
|
|
|
users := c.DataCache.GetPlayersWithCurrentProfiles()
|
2017-11-29 10:36:36 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
u.usersList(update, page, users)
|
2017-11-29 10:36:36 +04:00
|
|
|
|
return "ok"
|
2017-11-24 00:16:22 +04:00
|
|
|
|
}
|