Archived
1

/find_top_attack for selecting users by top position

This commit is contained in:
Vladimir Hodakov 2018-02-05 13:00:18 +04:00
parent 6dac398fd3
commit feb1899992
4 changed files with 54 additions and 0 deletions

View File

@ -181,6 +181,12 @@ func (r *Router) routePrivateRequest(update *tgbotapi.Update, playerRaw *dbmappi
return c.Users.FindByName(update) 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) return c.Talkers.AnyMessageUnauthorized(update)
case update.Message.Command() == "squad_add_user": case update.Message.Command() == "squad_add_user":

View File

@ -51,6 +51,28 @@ func (u *Users) FindByName(update *tgbotapi.Update) string {
return "ok" 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 // ForeignProfileMessage shows profile of another user
func (u *Users) ForeignProfileMessage(update *tgbotapi.Update) string { func (u *Users) ForeignProfileMessage(update *tgbotapi.Update) string {
userNum := strings.TrimPrefix(update.Message.Command(), "profile") userNum := strings.TrimPrefix(update.Message.Command(), "profile")

View File

@ -49,6 +49,31 @@ func (u *Users) findUserByName(pattern string) map[int]*dbmapping.PlayerProfile
return selectedUsers 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) { func (u *Users) foundUsersMessage(update *tgbotapi.Update, users map[int]*dbmapping.PlayerProfile) {
var keys []int var keys []int
for i := range users { for i := range users {

View File

@ -19,6 +19,7 @@ type UsersInterface interface {
FindByLevel(update *tgbotapi.Update) string FindByLevel(update *tgbotapi.Update) string
FindByName(update *tgbotapi.Update) string FindByName(update *tgbotapi.Update) string
FindByTopAttack(update *tgbotapi.Update) string
ForeignProfileMessage(update *tgbotapi.Update) string ForeignProfileMessage(update *tgbotapi.Update) string
FormatUsername(userName string) string FormatUsername(userName string) string
ProfileAddEffectsMessage(update *tgbotapi.Update) string ProfileAddEffectsMessage(update *tgbotapi.Update) string