Archived
1

Adding multiple cummands for listing filtered and unfiltered pokememes

This commit is contained in:
Vladimir Hodakov 2018-02-07 15:59:28 +04:00
parent 14413b3167
commit 7368c58e2d
9 changed files with 105 additions and 122 deletions

View File

@ -30,5 +30,7 @@ type DataCacheInterface interface {
DeletePokememeByID(pokememeID int) error DeletePokememeByID(pokememeID int) error
GetLeagueBySymbol(symbol string) (*dbmapping.League, error) GetLeagueBySymbol(symbol string) (*dbmapping.League, error)
GetWeaponTypeByID(weaponID int) (*dbmapping.Weapon, error)
GetWeaponTypeByName(name string) (*dbmapping.Weapon, error) GetWeaponTypeByName(name string) (*dbmapping.Weapon, error)
} }

View File

@ -92,8 +92,9 @@ func (dc *DataCache) GetOrCreatePlayerByTelegramID(telegramID int) (*dbmapping.P
// GetPlayerByID returns player from datacache by ID // GetPlayerByID returns player from datacache by ID
func (dc *DataCache) GetPlayerByID(playerID int) (*dbmapping.Player, error) { func (dc *DataCache) GetPlayerByID(playerID int) (*dbmapping.Player, error) {
c.Log.Info("DataCache: Getting player with ID=", playerID) c.Log.Info("DataCache: Getting player with ID = ", playerID)
if dc.players[playerID] != nil { if dc.players[playerID] != nil {
c.Log.Debug("DataCache: found player with ID = " + strconv.Itoa(playerID))
return dc.players[playerID], nil return dc.players[playerID], nil
} }

View File

@ -111,6 +111,7 @@ func (dc *DataCache) GetProfileByID(profileID int) (*dbmapping.Profile, error) {
// GetProfileByPlayerID returns current profile for player // GetProfileByPlayerID returns current profile for player
func (dc *DataCache) GetProfileByPlayerID(playerID int) (*dbmapping.Profile, error) { func (dc *DataCache) GetProfileByPlayerID(playerID int) (*dbmapping.Profile, error) {
if dc.currentProfiles[playerID] != nil { if dc.currentProfiles[playerID] != nil {
c.Log.Debug("DataCache: found current profile for player with ID = " + strconv.Itoa(playerID))
return dc.currentProfiles[playerID], nil return dc.currentProfiles[playerID], nil
} }

View File

@ -34,6 +34,18 @@ func (dc *DataCache) loadWeapons() {
// External functions // External functions
// GetWeaponTypeByID returns weapon type from datacache by given ID
func (dc *DataCache) GetWeaponTypeByID(weaponID int) (*dbmapping.Weapon, error) {
dc.weaponsMutex.Lock()
if dc.weapons[weaponID] != nil {
c.Log.Debug("DataCache: found weapon type with ID = " + strconv.Itoa(weaponID))
dc.weaponsMutex.Unlock()
return dc.weapons[weaponID], nil
}
dc.weaponsMutex.Unlock()
return nil, errors.New("There is no weapon type with ID = " + strconv.Itoa(weaponID))
}
// GetWeaponTypeByName returns weapon type from datacache by weapon name // GetWeaponTypeByName returns weapon type from datacache by weapon name
func (dc *DataCache) GetWeaponTypeByName(name string) (*dbmapping.Weapon, error) { func (dc *DataCache) GetWeaponTypeByName(name string) (*dbmapping.Weapon, error) {
dc.weaponsMutex.Lock() dc.weaponsMutex.Lock()

View File

@ -7,9 +7,11 @@ import (
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
"sort" "sort"
"strconv" "strconv"
"strings"
) )
func (p *Pokedexer) getBestPokememes(playerID int) ([]*dbmapping.PokememeFull, bool) { func (p *Pokedexer) getAdvicePokememes(playerID int, adviceType string) ([]*dbmapping.PokememeFull, bool) {
c.Log.Debug("Getting advice for pokememes...")
pokememesArray := make([]*dbmapping.PokememeFull, 0) pokememesArray := make([]*dbmapping.PokememeFull, 0)
playerRaw, err := c.DataCache.GetPlayerByID(playerID) playerRaw, err := c.DataCache.GetPlayerByID(playerID)
@ -27,31 +29,45 @@ func (p *Pokedexer) getBestPokememes(playerID int) ([]*dbmapping.PokememeFull, b
return pokememesArray, false return pokememesArray, false
} }
weapon, err := c.DataCache.GetWeaponTypeByID(profileRaw.WeaponID)
if err != nil {
c.Log.Debug(err.Error())
}
summPower := profileRaw.Power
if weapon != nil {
summPower = summPower + weapon.Power
}
allPokememes := c.DataCache.GetAllPokememes() allPokememes := c.DataCache.GetAllPokememes()
for i := range allPokememes { for i := range allPokememes {
// Adding only affordable pokememes... neededGrade := 0
if (allPokememes[i].Pokememe.Defence < profileRaw.Power) || allPokememes[i].Pokememe.Purchaseable { if profileRaw.LevelID < 9 {
// ...and only of needed grade (+1 until 9) neededGrade = profileRaw.LevelID + 1
neededGrade := 0 } else {
if profileRaw.LevelID < 9 { neededGrade = 9
neededGrade = profileRaw.LevelID + 1 }
if allPokememes[i].Pokememe.Grade == neededGrade {
matchLeague := false
if profileRaw.LevelID < 4 {
matchLeague = true
} else if adviceType == "best_nofilter" || adviceType == "advice_all" {
matchLeague = true
} else { } else {
neededGrade = 9 for j := range allPokememes[i].Elements {
} if allPokememes[i].Elements[j].LeagueID == playerRaw.LeagueID {
if allPokememes[i].Pokememe.Grade == neededGrade { matchLeague = true
// ...and only of our elements if our level past 4
matchLeague := false
if profileRaw.LevelID < 4 {
matchLeague = true
} else {
for j := range allPokememes[i].Elements {
if allPokememes[i].Elements[j].LeagueID == playerRaw.LeagueID {
matchLeague = true
}
} }
} }
if matchLeague { }
if matchLeague {
switch adviceType {
case "best", "advice":
if (allPokememes[i].Pokememe.Defence < summPower) || allPokememes[i].Pokememe.Purchaseable {
pokememesArray = append(pokememesArray, allPokememes[i])
}
default:
pokememesArray = append(pokememesArray, allPokememes[i]) pokememesArray = append(pokememesArray, allPokememes[i])
} }
} }
@ -62,84 +78,26 @@ func (p *Pokedexer) getBestPokememes(playerID int) ([]*dbmapping.PokememeFull, b
// As we have already filtered this array, we need to sort it and pass to view // As we have already filtered this array, we need to sort it and pass to view
sort.Slice(pokememesArray, func(i, j int) bool { sort.Slice(pokememesArray, func(i, j int) bool {
return pokememesArray[i].Pokememe.Attack > pokememesArray[j].Pokememe.Attack if strings.HasPrefix(adviceType, "best") {
}) return pokememesArray[i].Pokememe.Attack > pokememesArray[j].Pokememe.Attack
if len(pokememesArray) > 5 {
idx := 0
pokememesArrayShorted := make([]*dbmapping.PokememeFull, 0)
for i := range pokememesArray {
if idx < 5 {
pokememesArrayShorted = append(pokememesArrayShorted, pokememesArray[i])
}
idx++
} }
pokememesArray = pokememesArrayShorted
}
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 return pokememesArray[i].Pokememe.Price > pokememesArray[j].Pokememe.Price
}) })
if len(pokememesArray) > 10 { switch adviceType {
idx := 0 case "best", "advice", "best_nofilter":
if len(pokememesArray) > 5 {
pokememesArrayShorted := make([]*dbmapping.PokememeFull, 0) idx := 0
pokememesArrayShorted := make([]*dbmapping.PokememeFull, 0)
for i := range pokememesArray { for i := range pokememesArray {
if idx < 10 { if idx < 5 {
pokememesArrayShorted = append(pokememesArrayShorted, pokememesArray[i]) pokememesArrayShorted = append(pokememesArrayShorted, pokememesArray[i])
}
idx++
} }
idx++
}
pokememesArray = pokememesArrayShorted pokememesArray = pokememesArrayShorted
}
} }
return pokememesArray, true return pokememesArray, true

View File

@ -13,21 +13,10 @@ import (
// AdvicePokememesList shows list for catching // AdvicePokememesList shows list for catching
// It may be list of best or most valuable pokememes // It may be list of best or most valuable pokememes
func (p *Pokedexer) AdvicePokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string { func (p *Pokedexer) AdvicePokememesList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
pokememes := make([]*dbmapping.PokememeFull, 0) pokememes, ok := p.getAdvicePokememes(playerRaw.ID, update.Message.Command())
if update.Message.Command() == "best" { if !ok {
neededPokememes, ok := p.getBestPokememes(playerRaw.ID) c.Log.Error("Cannot get pokememes from getter!")
if !ok { return "fail"
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) profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID)
@ -37,10 +26,17 @@ func (p *Pokedexer) AdvicePokememesList(update *tgbotapi.Update, playerRaw *dbma
} }
message := "" message := ""
if update.Message.Command() == "best" { switch update.Message.Command() {
message += "*Лучшие покемемы для поимки*\n\n" case "best":
} else { message += "*Пять топовых покемемов для поимки*\n\n"
message += "*Самые дорогие покемемы для поимки*\n\n" case "advice":
message += "*Пять самых дорогих покемемов*\n\n"
case "best_all":
message += "*Все топовые покемемы для твоего уровня*\n\n"
case "advice_all":
message += "*Все самые дорогие покемемы для твоего уровня*\n\n"
case "best_nofilter":
message += "*Пять топовых покемемов для поимки без фильтра по элементам*\n\n"
} }
for i := range pokememes { for i := range pokememes {
pk := pokememes[i].Pokememe pk := pokememes[i].Pokememe
@ -68,10 +64,10 @@ func (p *Pokedexer) AdvicePokememesList(update *tgbotapi.Update, playerRaw *dbma
} else { } else {
message += "Нельзя" message += "Нельзя"
} }
if update.Message.Command() == "advice" { if update.Message.Command() == "advice" || update.Message.Command() == "advice_all" {
message += "\nСтоимость продажи: 💲" + c.Statistics.GetPrintablePoints(pk.Price) message += "\nСтоимость продажи: 💲" + c.Statistics.GetPrintablePoints(pk.Price)
} }
if len(message) > 3000 { if len(message) > 4000 {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"

View File

@ -94,6 +94,15 @@ func (r *Router) routePrivateRequest(update *tgbotapi.Update, playerRaw *dbmappi
case update.Message.Command() == "advice": case update.Message.Command() == "advice":
c.Pokedexer.AdvicePokememesList(update, playerRaw) c.Pokedexer.AdvicePokememesList(update, playerRaw)
return "ok" return "ok"
case update.Message.Command() == "best_all":
c.Pokedexer.AdvicePokememesList(update, playerRaw)
return "ok"
case update.Message.Command() == "advice_all":
c.Pokedexer.AdvicePokememesList(update, playerRaw)
return "ok"
case update.Message.Command() == "best_nofilter":
c.Pokedexer.AdvicePokememesList(update, playerRaw)
return "ok"
case update.Message.Command() == "reminders": case update.Message.Command() == "reminders":
return c.Reminder.AlarmsList(update, playerRaw) return c.Reminder.AlarmsList(update, playerRaw)

View File

@ -7,6 +7,7 @@ import (
"git.wtfteam.pro/fat0troll/i2_bot/lib/config" "git.wtfteam.pro/fat0troll/i2_bot/lib/config"
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping" "git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
"time"
) )
// AcademyMessage gives user link to Bastion // AcademyMessage gives user link to Bastion
@ -53,8 +54,13 @@ func (t *Talkers) HelpMessage(update *tgbotapi.Update, playerRaw *dbmapping.Play
message += "Текущая версия: *" + config.VERSION + "*\n\n" message += "Текущая версия: *" + config.VERSION + "*\n\n"
message += "Список команд\n\n" message += "Список команд\n\n"
message += "\\* /me посмотреть свой сохраненный профиль в боте\n" message += "\\* /me посмотреть свой сохраненный профиль в боте\n"
message += "\\* /best посмотреть лучших покемонов для поимки\n" message += "\\* /best посмотреть 5 лучших покемонов для поимки\n"
message += "\\* /advice посмотреть самых дорогих покемонов для поимки\n" message += "\\* /advice посмотреть 5 самых дорогих покемонов для поимки\n"
message += "\\* /best\\_all посмотреть всех лучших покемонов для поимки\n"
message += "\\* /advice\\_all посмотреть всех самых дорогих покемонов для поимки\n"
if playerRaw.CreatedAt.Before(time.Now().Add(30 * 24 * 60 * 60 * time.Second)) {
message += "\\* /best\\_nofilter посмотреть пять лучших покемонов для поимки _без фильтра по элементам_ (полезно для сборки максимально топовой руки на высоком уровне)\n"
}
message += "\\* /top — топ игроков лиги\n" message += "\\* /top — топ игроков лиги\n"
message += "\\* /top\\_my — топ игроков лиги твоего уровня\n" message += "\\* /top\\_my — топ игроков лиги твоего уровня\n"
message += "\\* /pokedeks получить список известных боту покемемов\n" message += "\\* /pokedeks получить список известных боту покемемов\n"

View File

@ -127,12 +127,10 @@ func (u *Users) ProfileMessage(update *tgbotapi.Update, playerRaw *dbmapping.Pla
if err != nil { if err != nil {
c.Log.Error(err) c.Log.Error(err)
} }
weapon := dbmapping.Weapon{} weapon, err := c.DataCache.GetWeaponTypeByID(profileRaw.WeaponID)
if profileRaw.WeaponID != 0 { if err != nil {
err = c.Db.Get(&weapon, c.Db.Rebind("SELECT * FROM weapons WHERE id=?"), profileRaw.WeaponID) // It's non critical
if err != nil { c.Log.Debug(err.Error())
c.Log.Error(err)
}
} }
profilePokememes := []dbmapping.ProfilePokememe{} profilePokememes := []dbmapping.ProfilePokememe{}
err = c.Db.Select(&profilePokememes, c.Db.Rebind("SELECT * FROM profiles_pokememes WHERE profile_id=?"), profileRaw.ID) err = c.Db.Select(&profilePokememes, c.Db.Rebind("SELECT * FROM profiles_pokememes WHERE profile_id=?"), profileRaw.ID)