From 6a57d8581c5cc08f3f51ecf73847767782006356 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Tue, 23 Jan 2018 20:13:16 +0400 Subject: [PATCH] Command to find users on level --- lib/router/private_request.go | 8 +++- lib/users/responders.go | 45 +++++++++++++--------- lib/users/users.go | 35 ++++++++++++++++- lib/users/usersinterface/usersinterface.go | 3 +- 4 files changed, 69 insertions(+), 22 deletions(-) diff --git a/lib/router/private_request.go b/lib/router/private_request.go index bd9f86f..dcfa7f9 100644 --- a/lib/router/private_request.go +++ b/lib/router/private_request.go @@ -4,8 +4,8 @@ package router import ( - "github.com/go-telegram-bot-api/telegram-bot-api" "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" + "github.com/go-telegram-bot-api/telegram-bot-api" "regexp" ) @@ -159,6 +159,12 @@ func (r *Router) routePrivateRequest(update *tgbotapi.Update, playerRaw *dbmappi return c.Talkers.AnyMessageUnauthorized(update) + case update.Message.Command() == "find_level": + if c.Users.PlayerBetterThan(playerRaw, "academic") { + return c.Users.FindByLevel(update) + } + + return c.Talkers.AnyMessageUnauthorized(update) case update.Message.Command() == "find_user": if c.Users.PlayerBetterThan(playerRaw, "academic") { return c.Users.FindByName(update) diff --git a/lib/users/responders.go b/lib/users/responders.go index be32e26..d2f24a5 100644 --- a/lib/users/responders.go +++ b/lib/users/responders.go @@ -4,8 +4,8 @@ package users import ( - "github.com/go-telegram-bot-api/telegram-bot-api" "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" + "github.com/go-telegram-bot-api/telegram-bot-api" "strconv" "strings" ) @@ -15,6 +15,30 @@ func (u *Users) FormatUsername(userName string) string { return strings.Replace(userName, "_", `\_`, -1) } +// FindByLevel finds user with level and recent profile update +func (u *Users) FindByLevel(update *tgbotapi.Update) string { + commandArgs := update.Message.CommandArguments() + if commandArgs == "" { + c.Talkers.BotError(update) + return "fail" + } + + levelID, err := strconv.Atoi(commandArgs) + if err != nil { + c.Log.Error(err.Error()) + return "fail" + } + + usersArray, ok := u.findUsersByLevel(levelID) + if !ok { + return "fail" + } + + u.foundUsersMessage(update, usersArray) + + return "ok" +} + // FindByName finds user with such username or nickname func (u *Users) FindByName(update *tgbotapi.Update) string { commandArgs := update.Message.CommandArguments() @@ -28,24 +52,7 @@ func (u *Users) FindByName(update *tgbotapi.Update) string { return "fail" } - message := "*Найденные игроки:*\n" - - for i := range usersArray { - message += "#" + strconv.Itoa(usersArray[i].PlayerID) - message += " " + usersArray[i].LeagueSymbol - message += " " + usersArray[i].Nickname - if usersArray[i].TelegramNickname != "" { - message += " (@" + u.FormatUsername(usersArray[i].TelegramNickname) + ")" - } - message += " /profile" + strconv.Itoa(usersArray[i].PlayerID) + "\n" - message += "Telegram ID: " + strconv.Itoa(usersArray[i].TelegramID) + "\n" - message += "Последнее обновление: " + usersArray[i].CreatedAt.Format("02.01.2006 15:04:05") + "\n" - } - - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) - msg.ParseMode = "Markdown" - - c.Bot.Send(msg) + u.foundUsersMessage(update, usersArray) return "ok" } diff --git a/lib/users/users.go b/lib/users/users.go index 74053c4..bb4648b 100644 --- a/lib/users/users.go +++ b/lib/users/users.go @@ -4,8 +4,8 @@ package users import ( - "github.com/go-telegram-bot-api/telegram-bot-api" "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" + "github.com/go-telegram-bot-api/telegram-bot-api" "strconv" ) @@ -47,6 +47,18 @@ func (u *Users) getUsersWithProfiles() ([]dbmapping.PlayerProfile, bool) { return usersArray, true } +func (u *Users) findUsersByLevel(levelID int) ([]dbmapping.ProfileWithAddons, bool) { + selectedUsers := []dbmapping.ProfileWithAddons{} + + err := c.Db.Select(&selectedUsers, c.Db.Rebind("SELECT p.*, l.symbol AS league_symbol, l.id AS league_id, pl.telegram_id FROM players pl, profiles p, leagues l WHERE p.created_at > NOW() - INTERVAL 72 HOUR AND p.level_id = ? GROUP BY player_id"), levelID) + if err != nil { + c.Log.Error(err.Error()) + return selectedUsers, false + } + + return selectedUsers, true +} + func (u *Users) findUserByName(pattern string) ([]dbmapping.ProfileWithAddons, bool) { selectedUsers := []dbmapping.ProfileWithAddons{} @@ -59,6 +71,27 @@ func (u *Users) findUserByName(pattern string) ([]dbmapping.ProfileWithAddons, b return selectedUsers, true } +func (u *Users) foundUsersMessage(update *tgbotapi.Update, usersArray []dbmapping.ProfileWithAddons) { + message := "*Найденные игроки:*\n" + + for i := range usersArray { + message += "#" + strconv.Itoa(usersArray[i].PlayerID) + message += " " + usersArray[i].LeagueSymbol + message += " " + usersArray[i].Nickname + if usersArray[i].TelegramNickname != "" { + message += " (@" + u.FormatUsername(usersArray[i].TelegramNickname) + ")" + } + message += " /profile" + strconv.Itoa(usersArray[i].PlayerID) + "\n" + message += "Telegram ID: " + strconv.Itoa(usersArray[i].TelegramID) + "\n" + message += "Последнее обновление: " + usersArray[i].CreatedAt.Format("02.01.2006 15:04:05") + "\n" + } + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) + msg.ParseMode = "Markdown" + + c.Bot.Send(msg) +} + func (u *Users) profileAddSuccessMessage(update *tgbotapi.Update, leagueID int, level int) { message := "*Профиль успешно обновлен.*\n\n" message += "Функциональность бота держится на актуальности профилей. Обновляйся почаще, и да пребудет с тобой Рандом!\n" diff --git a/lib/users/usersinterface/usersinterface.go b/lib/users/usersinterface/usersinterface.go index cac958c..1d6a1b5 100644 --- a/lib/users/usersinterface/usersinterface.go +++ b/lib/users/usersinterface/usersinterface.go @@ -4,8 +4,8 @@ package usersinterface import ( - "github.com/go-telegram-bot-api/telegram-bot-api" "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" + "github.com/go-telegram-bot-api/telegram-bot-api" ) // UsersInterface implements Users for importing via appcontex @@ -20,6 +20,7 @@ type UsersInterface interface { GetPlayerByID(playerID int) (dbmapping.Player, bool) PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool + FindByLevel(update *tgbotapi.Update) string FindByName(update *tgbotapi.Update) string ForeignProfileMessage(update *tgbotapi.Update) string FormatUsername(userName string) string