From 14413b316745c35361b510d01dbc1d3b3fd41766 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Wed, 7 Feb 2018 15:00:04 +0400 Subject: [PATCH] /advice command for searching most valuable pokememes --- lib/pokedexer/getters.go | 62 +++++++++++++++++++ .../pokedexerinterface/pokedexerinterface.go | 2 +- lib/pokedexer/responders.go | 39 ++++++++---- lib/router/private_request.go | 5 +- lib/talkers/help.go | 1 + lib/users/responders.go | 1 + lib/users/users.go | 1 + 7 files changed, 97 insertions(+), 14 deletions(-) diff --git a/lib/pokedexer/getters.go b/lib/pokedexer/getters.go index fa71d2a..13a71c6 100644 --- a/lib/pokedexer/getters.go +++ b/lib/pokedexer/getters.go @@ -82,3 +82,65 @@ func (p *Pokedexer) getBestPokememes(playerID int) ([]*dbmapping.PokememeFull, b return pokememesArray, true } + +func (p *Pokedexer) getHighPricedPokememes(playerID int) ([]*dbmapping.PokememeFull, bool) { + pokememesArray := make([]*dbmapping.PokememeFull, 0) + + playerRaw, err := c.DataCache.GetPlayerByID(playerID) + if err != nil { + c.Log.Error(err.Error()) + return pokememesArray, false + } + profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID) + if err != nil { + c.Log.Error(err.Error()) + return pokememesArray, false + } + + if playerRaw.LeagueID == 0 { + return pokememesArray, false + } + + allPokememes := c.DataCache.GetAllPokememes() + + for i := range allPokememes { + // Adding only affordable pokememes... + // Only by force: who need to buy pokememe for selling? + if allPokememes[i].Pokememe.Defence < profileRaw.Power { + // ...and only of needed grade (+1 until 9) + neededGrade := 0 + if profileRaw.LevelID < 9 { + neededGrade = profileRaw.LevelID + 1 + } else { + neededGrade = 9 + } + if allPokememes[i].Pokememe.Grade == neededGrade { + pokememesArray = append(pokememesArray, allPokememes[i]) + } + } + } + + c.Log.Debug(strconv.Itoa(len(pokememesArray)) + " pokememes passed initial /advice filtration.") + + // As we have already filtered this array, we need to sort it and pass to view + sort.Slice(pokememesArray, func(i, j int) bool { + return pokememesArray[i].Pokememe.Price > pokememesArray[j].Pokememe.Price + }) + + if len(pokememesArray) > 10 { + idx := 0 + + pokememesArrayShorted := make([]*dbmapping.PokememeFull, 0) + + for i := range pokememesArray { + if idx < 10 { + pokememesArrayShorted = append(pokememesArrayShorted, pokememesArray[i]) + } + idx++ + } + + pokememesArray = pokememesArrayShorted + } + + return pokememesArray, true +} diff --git a/lib/pokedexer/pokedexerinterface/pokedexerinterface.go b/lib/pokedexer/pokedexerinterface/pokedexerinterface.go index 6a5b3ee..9a76e34 100644 --- a/lib/pokedexer/pokedexerinterface/pokedexerinterface.go +++ b/lib/pokedexer/pokedexerinterface/pokedexerinterface.go @@ -14,7 +14,7 @@ type PokedexerInterface interface { PokememesList(update *tgbotapi.Update) PokememeInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string - BestPokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string + AdvicePokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string DeletePokememe(update *tgbotapi.Update) string } diff --git a/lib/pokedexer/responders.go b/lib/pokedexer/responders.go index 21e3475..97bbe8a 100644 --- a/lib/pokedexer/responders.go +++ b/lib/pokedexer/responders.go @@ -6,17 +6,28 @@ package pokedexer import ( "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" "github.com/go-telegram-bot-api/telegram-bot-api" - "sort" "strconv" "strings" ) -// BestPokememesList shows list for catching based on player league and grade -func (p *Pokedexer) BestPokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string { - pokememes, ok := p.getBestPokememes(playerRaw.ID) - if !ok { - c.Log.Error("Cannot get pokememes from getter!") - return "fail" +// AdvicePokememesList shows list for catching +// It may be list of best or most valuable pokememes +func (p *Pokedexer) AdvicePokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string { + pokememes := make([]*dbmapping.PokememeFull, 0) + if update.Message.Command() == "best" { + neededPokememes, ok := p.getBestPokememes(playerRaw.ID) + if !ok { + c.Log.Error("Cannot get pokememes from getter!") + return "fail" + } + pokememes = neededPokememes + } else { + neededPokememes, ok := p.getHighPricedPokememes(playerRaw.ID) + if !ok { + c.Log.Error("Cannot get pokememes from getter!") + return "fail" + } + pokememes = neededPokememes } profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID) @@ -25,11 +36,12 @@ func (p *Pokedexer) BestPokememesList(update *tgbotapi.Update, playerRaw *dbmapp return "fail" } - sort.Slice(pokememes, func(i, j int) bool { - return pokememes[i].Pokememe.Attack > pokememes[j].Pokememe.Attack - }) - - message := "*Лучшие покемемы для ловли*\n\n" + message := "" + if update.Message.Command() == "best" { + message += "*Лучшие покемемы для поимки*\n\n" + } else { + message += "*Самые дорогие покемемы для поимки*\n\n" + } for i := range pokememes { pk := pokememes[i].Pokememe pkL := pokememes[i].Locations @@ -56,6 +68,9 @@ func (p *Pokedexer) BestPokememesList(update *tgbotapi.Update, playerRaw *dbmapp } else { message += "Нельзя" } + if update.Message.Command() == "advice" { + message += "\nСтоимость продажи: 💲" + c.Statistics.GetPrintablePoints(pk.Price) + } if len(message) > 3000 { msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) msg.ParseMode = "Markdown" diff --git a/lib/router/private_request.go b/lib/router/private_request.go index 6a56f81..763b66d 100644 --- a/lib/router/private_request.go +++ b/lib/router/private_request.go @@ -89,7 +89,10 @@ func (r *Router) routePrivateRequest(update *tgbotapi.Update, playerRaw *dbmappi return c.Talkers.AnyMessageUnauthorized(update) case update.Message.Command() == "best": - c.Pokedexer.BestPokememesList(update, playerRaw) + c.Pokedexer.AdvicePokememesList(update, playerRaw) + return "ok" + case update.Message.Command() == "advice": + c.Pokedexer.AdvicePokememesList(update, playerRaw) return "ok" case update.Message.Command() == "reminders": return c.Reminder.AlarmsList(update, playerRaw) diff --git a/lib/talkers/help.go b/lib/talkers/help.go index 6320db7..8c1db0d 100644 --- a/lib/talkers/help.go +++ b/lib/talkers/help.go @@ -54,6 +54,7 @@ func (t *Talkers) HelpMessage(update *tgbotapi.Update, playerRaw *dbmapping.Play message += "Список команд\n\n" message += "\\* /me – посмотреть свой сохраненный профиль в боте\n" message += "\\* /best – посмотреть лучших покемонов для поимки\n" + message += "\\* /advice – посмотреть самых дорогих покемонов для поимки\n" message += "\\* /top — топ игроков лиги\n" message += "\\* /top\\_my — топ игроков лиги твоего уровня\n" message += "\\* /pokedeks – получить список известных боту покемемов\n" diff --git a/lib/users/responders.go b/lib/users/responders.go index fac7902..7292225 100644 --- a/lib/users/responders.go +++ b/lib/users/responders.go @@ -210,6 +210,7 @@ func (u *Users) ProfileMessage(update *tgbotapi.Update, playerRaw *dbmapping.Pla message += "\n\n⏰Последнее обновление профиля: " + profileRaw.CreatedAt.Format("02.01.2006 15:04:05") message += "\nНе забывай обновляться, это важно для получения актуальной информации.\n\n" message += "/best – посмотреть лучших покемемов для поимки\n" + message += "/advice – посмотреть самых дорогих покемемов для поимки\n" message += "/top — посмотреть лучших игроков лиги\n" message += "/top\\_my — посмотреть лучших игроков лиги твоего уровня\n" diff --git a/lib/users/users.go b/lib/users/users.go index 80b93d9..6efb65b 100644 --- a/lib/users/users.go +++ b/lib/users/users.go @@ -117,6 +117,7 @@ func (u *Users) profileAddSuccessMessage(update *tgbotapi.Update, leagueID int, message += "Функциональность бота держится на актуальности профилей. Обновляйся почаще, и да пребудет с тобой Рандом!\n" message += "Сохраненный профиль ты можешь просмотреть командой /me.\n\n" message += "/best – посмотреть лучших покемемов для поимки\n" + message += "/advice – посмотреть самых дорогих покемемов для поимки\n" message += "/top — посмотреть лучших представителей лиги\n" message += "/top\\_my — посмотреть лучших представителей лиги твоего уровня\n"