From feb18999922b2966f659606c11356f70d189eb19 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Mon, 5 Feb 2018 13:00:18 +0400 Subject: [PATCH] /find_top_attack for selecting users by top position --- lib/router/private_request.go | 6 ++++++ lib/users/responders.go | 22 +++++++++++++++++++ lib/users/users.go | 25 ++++++++++++++++++++++ lib/users/usersinterface/usersinterface.go | 1 + 4 files changed, 54 insertions(+) diff --git a/lib/router/private_request.go b/lib/router/private_request.go index af130a8..6a56f81 100644 --- a/lib/router/private_request.go +++ b/lib/router/private_request.go @@ -181,6 +181,12 @@ func (r *Router) routePrivateRequest(update *tgbotapi.Update, playerRaw *dbmappi return c.Users.FindByName(update) } + return c.Talkers.AnyMessageUnauthorized(update) + case update.Message.Command() == "find_top_attack": + if c.Users.PlayerBetterThan(playerRaw, "academic") { + return c.Users.FindByTopAttack(update) + } + return c.Talkers.AnyMessageUnauthorized(update) case update.Message.Command() == "squad_add_user": diff --git a/lib/users/responders.go b/lib/users/responders.go index 48d71d1..fac7902 100644 --- a/lib/users/responders.go +++ b/lib/users/responders.go @@ -51,6 +51,28 @@ func (u *Users) FindByName(update *tgbotapi.Update) string { return "ok" } +// 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" +} + // ForeignProfileMessage shows profile of another user func (u *Users) ForeignProfileMessage(update *tgbotapi.Update) string { userNum := strings.TrimPrefix(update.Message.Command(), "profile") diff --git a/lib/users/users.go b/lib/users/users.go index 0804edc..80b93d9 100644 --- a/lib/users/users.go +++ b/lib/users/users.go @@ -49,6 +49,31 @@ func (u *Users) findUserByName(pattern string) map[int]*dbmapping.PlayerProfile return selectedUsers } +func (u *Users) findUserByTopAttack(power int) map[int]*dbmapping.PlayerProfile { + selectedUsers := make(map[int]*dbmapping.PlayerProfile) + allPlayers := c.DataCache.GetPlayersWithCurrentProfiles() + + profiles := make([]*dbmapping.PlayerProfile, 0) + + for i := range allPlayers { + if allPlayers[i].Player.LeagueID == 1 { + profiles = append(profiles, allPlayers[i]) + } + } + + sort.Slice(profiles, func(i, j int) bool { + return profiles[i].Profile.Power > profiles[j].Profile.Power + }) + + for i := (power - 1); i < (power + 2); i++ { + if profiles[i] != nil { + selectedUsers[i] = profiles[i] + } + } + + return selectedUsers +} + func (u *Users) foundUsersMessage(update *tgbotapi.Update, users map[int]*dbmapping.PlayerProfile) { var keys []int for i := range users { diff --git a/lib/users/usersinterface/usersinterface.go b/lib/users/usersinterface/usersinterface.go index 25304cc..228641c 100644 --- a/lib/users/usersinterface/usersinterface.go +++ b/lib/users/usersinterface/usersinterface.go @@ -19,6 +19,7 @@ type UsersInterface interface { FindByLevel(update *tgbotapi.Update) string FindByName(update *tgbotapi.Update) string + FindByTopAttack(update *tgbotapi.Update) string ForeignProfileMessage(update *tgbotapi.Update) string FormatUsername(userName string) string ProfileAddEffectsMessage(update *tgbotapi.Update) string