From d897d2c3f964d15ffe85cf378e08f5165b30b0b1 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Fri, 13 Oct 2017 03:52:04 +0400 Subject: [PATCH] Best pokememes for user listing --- lib/config/config.go | 2 +- .../gettersinterface/gettersinterface.go | 2 + lib/getters/player.go | 11 ++++ lib/getters/pokememes.go | 54 ++++++++++++++--- lib/getters/profile.go | 4 +- lib/router/router.go | 4 ++ lib/talkers/help.go | 1 + lib/talkers/profile.go | 3 +- lib/talkers/profile_add.go | 2 +- lib/talkers/suggestions.go | 58 +++++++++++++++++++ .../talkersinterface/talkersinterface.go | 1 + 11 files changed, 129 insertions(+), 13 deletions(-) create mode 100644 lib/talkers/suggestions.go diff --git a/lib/config/config.go b/lib/config/config.go index a8f1401..def04a5 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -12,7 +12,7 @@ import ( "gopkg.in/yaml.v2" ) -const VERSION = "0.28" +const VERSION = "0.29" type DatabaseConnection struct { Host string `yaml:"host"` diff --git a/lib/getters/gettersinterface/gettersinterface.go b/lib/getters/gettersinterface/gettersinterface.go index 79856f7..4b15ba5 100644 --- a/lib/getters/gettersinterface/gettersinterface.go +++ b/lib/getters/gettersinterface/gettersinterface.go @@ -12,10 +12,12 @@ type GettersInterface interface { Init() // Player GetOrCreatePlayer(telegram_id int) (dbmapping.Player, bool) + GetPlayerByID(player_id int) (dbmapping.Player, bool) // Profile GetProfile(player_id int) (dbmapping.Profile, bool) // Pokememes GetPokememes() ([]dbmapping.PokememeFull, bool) + GetBestPokememes(player_id int) ([]dbmapping.PokememeFull, bool) GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool) // Possibilities PossibilityRequiredPokeballs(location int, grade int, lvl int) (float64, int) diff --git a/lib/getters/player.go b/lib/getters/player.go index 6bbaa92..9143c8b 100644 --- a/lib/getters/player.go +++ b/lib/getters/player.go @@ -11,6 +11,17 @@ import ( "../dbmapping" ) +func (g *Getters) GetPlayerByID(player_id int) (dbmapping.Player, bool) { + player_raw := dbmapping.Player{} + err := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE id=?"), player_id) + if err != nil { + log.Println(err) + return player_raw, false + } + + return player_raw, true +} + func (g *Getters) GetOrCreatePlayer(telegram_id int) (dbmapping.Player, bool) { player_raw := dbmapping.Player{} err := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=?"), telegram_id) diff --git a/lib/getters/pokememes.go b/lib/getters/pokememes.go index 91baf35..7db23f3 100644 --- a/lib/getters/pokememes.go +++ b/lib/getters/pokememes.go @@ -11,16 +11,12 @@ import ( "../dbmapping" ) -func (g *Getters) GetPokememes() ([]dbmapping.PokememeFull, bool) { +// Internal functions + +func (g *Getters) formFullPokememes(pokememes []dbmapping.Pokememe) ([]dbmapping.PokememeFull, bool) { pokememes_full := []dbmapping.PokememeFull{} - pokememes := []dbmapping.Pokememe{} - err := c.Db.Select(&pokememes, "SELECT * FROM pokememes ORDER BY grade asc, name asc"); - if err != nil { - log.Println(err) - return pokememes_full, false - } elements := []dbmapping.Element{} - err = c.Db.Select(&elements, "SELECT * FROM elements"); + err := c.Db.Select(&elements, "SELECT * FROM elements"); if err != nil { log.Println(err) return pokememes_full, false @@ -79,6 +75,48 @@ func (g *Getters) GetPokememes() ([]dbmapping.PokememeFull, bool) { return pokememes_full, true } +// External functions + +func (g *Getters) GetPokememes() ([]dbmapping.PokememeFull, bool) { + pokememes_full := []dbmapping.PokememeFull{} + pokememes := []dbmapping.Pokememe{} + err := c.Db.Select(&pokememes, "SELECT * FROM pokememes ORDER BY grade asc, name asc"); + if err != nil { + log.Println(err) + return pokememes_full, false + } + + pokememes_full, ok := g.formFullPokememes(pokememes) + return pokememes_full, ok +} + +func (g *Getters) GetBestPokememes(player_id int) ([]dbmapping.PokememeFull, bool) { + pokememes_full := []dbmapping.PokememeFull{} + player_raw, ok := g.GetPlayerByID(player_id) + if !ok { + return pokememes_full, ok + } + profile_raw, ok := g.GetProfile(player_id) + if !ok { + return pokememes_full, ok + } + + if player_raw.League_id == 0 { + return pokememes_full, false + } + + // TODO: make it more complicated + pokememes := []dbmapping.Pokememe{} + err := c.Db.Select(&pokememes, c.Db.Rebind("SELECT p.* FROM pokememes p, pokememes_elements pe, elements e WHERE e.league_id = ? AND p.grade = ? AND pe.element_id = e.id AND pe.pokememe_id = p.id ORDER BY p.attack DESC"), player_raw.League_id, profile_raw.Level_id + 1) + if err != nil { + log.Println(err) + return pokememes_full, false + } + + pokememes_full, ok = g.formFullPokememes(pokememes) + return pokememes_full, ok +} + func (g *Getters) GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool) { pokememe_full := dbmapping.PokememeFull{} pokememe := dbmapping.Pokememe{} diff --git a/lib/getters/profile.go b/lib/getters/profile.go index 64dac80..a6c4491 100644 --- a/lib/getters/profile.go +++ b/lib/getters/profile.go @@ -10,9 +10,9 @@ import ( "../dbmapping" ) -func (g *Getters) GetProfile(profile_id int) (dbmapping.Profile, bool) { +func (g *Getters) GetProfile(player_id int) (dbmapping.Profile, bool) { profile_raw := dbmapping.Profile{} - err := c.Db.Get(&profile_raw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? ORDER BY created_at DESC LIMIT 1"), profile_id) + err := c.Db.Get(&profile_raw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? ORDER BY created_at DESC LIMIT 1"), player_id) if err != nil { log.Println(err) return profile_raw, false diff --git a/lib/router/router.go b/lib/router/router.go index 55bbb4e..13d94e9 100644 --- a/lib/router/router.go +++ b/lib/router/router.go @@ -39,6 +39,7 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { var pokedexMsg = regexp.MustCompile("/pokede(x|ks)\\d?\\z") var pokememeInfoMsg = regexp.MustCompile("/pk(\\d+)") var meMsg = regexp.MustCompile("/me\\z") + var bestMsg = regexp.MustCompile("/best\\z") // Forwards var pokememeMsg = regexp.MustCompile("(Уровень)(.+)(Опыт)(.+)\n(Элементы:)(.+)\n(.+)(💙MP)") @@ -114,6 +115,9 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { } else { c.Talkers.AnyMessageUnauthorized(update) } + // Suggestions + case bestMsg.MatchString(text): + c.Talkers.BestPokememesList(update, player_raw) // Easter eggs case huMsg.MatchString(text): c.Talkers.MatMessage(update) diff --git a/lib/talkers/help.go b/lib/talkers/help.go index d1b68aa..ec0cd46 100644 --- a/lib/talkers/help.go +++ b/lib/talkers/help.go @@ -15,6 +15,7 @@ func (t *Talkers) HelpMessage(update tgbotapi.Update) { help_message += "Текущая версия: *" + config.VERSION + "*\n\n" help_message += "Список команд:\n\n" help_message += "+ /me – посмотреть свой сохраненный профиль в боте\n" + help_message += "+ /best – посмотреть лучших покемонов для поимки\n" help_message += "+ /pokedeks – получить список известных боту покемемов\n" help_message += "+ /help – выводит данное сообщение\n" help_message += "\n\n" diff --git a/lib/talkers/profile.go b/lib/talkers/profile.go index 4520a54..a0b71af 100644 --- a/lib/talkers/profile.go +++ b/lib/talkers/profile.go @@ -95,7 +95,8 @@ func (t *Talkers) ProfileMessage(update tgbotapi.Update, player_raw dbmapping.Pl } message += "\n\n💳" + strconv.Itoa(player_raw.Telegram_id) message += "\n⏰Последнее обновление профиля: " + profile_raw.Created_at.Format("02.01.2006 15:04:05") - message += "\n\nНе забывай обновляться, это важно для получения актуальной информации." + message += "\n\nНе забывай обновляться, это важно для получения актуальной информации.\n\n" + message += "/best – посмотреть лучших покемемов для поимки" msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) msg.ParseMode = "Markdown" diff --git a/lib/talkers/profile_add.go b/lib/talkers/profile_add.go index a138854..4afc3ff 100644 --- a/lib/talkers/profile_add.go +++ b/lib/talkers/profile_add.go @@ -12,7 +12,7 @@ func (t *Talkers) ProfileAddSuccessMessage(update tgbotapi.Update) { message := "*Профиль успешно обновлен.*\n\n" message += "Функциональность бота держится на актуальности профилей. Обновляйся почаще, и да пребудет с тобой Рандом!\n" message += "Сохраненный профиль ты можешь просмотреть командой /me.\n\n" - message += "– почаще – как можно чаще, но не более 48 раз в сутки." + message += "/best – посмотреть лучших покемемов для поимки" msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) msg.ParseMode = "Markdown" diff --git a/lib/talkers/suggestions.go b/lib/talkers/suggestions.go new file mode 100644 index 0000000..d1aa179 --- /dev/null +++ b/lib/talkers/suggestions.go @@ -0,0 +1,58 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package talkers + +import ( + // stdlib + "log" + "strconv" + // 3rd party + "github.com/go-telegram-bot-api/telegram-bot-api" + // local + "../dbmapping" +) + +func (t *Talkers) BestPokememesList(update tgbotapi.Update, player_raw dbmapping.Player) string { + pokememes, ok := c.Getters.GetBestPokememes(player_raw.Id) + if !ok { + log.Printf("Cannot get pokememes from getter!") + return "fail" + } + + message := "*Лучшие покемемы для ловли*\n\n" + for i := range(pokememes) { + pk := pokememes[i].Pokememe + pk_l := pokememes[i].Locations + pk_e := pokememes[i].Elements + message += strconv.Itoa(pk.Grade) + "⃣ " + message += pk.Name + " (⚔" + message += c.Parsers.ReturnPoints(pk.Attack) + message += ", 🛡" + c.Parsers.ReturnPoints(pk.Defence) + ")" + for i := range(pk_e) { + message += pk_e[i].Symbol + } + message += " /pk" + strconv.Itoa(pk.Id) + "\n" + message += "Локации: " + for i := range(pk_l) { + message += pk_l[i].Symbol + pk_l[i].Name + if i + 1 < len(pk_l) { + message += ", " + } + } + message += "\nКупить: " + if pk.Purchaseable { + message += "💲" + c.Parsers.ReturnPoints(pk.Price * 3) + } else { + message += "Нельзя" + } + message += "\n\n" + } + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) + msg.ParseMode = "Markdown" + + c.Bot.Send(msg) + + return "ok" +} diff --git a/lib/talkers/talkersinterface/talkersinterface.go b/lib/talkers/talkersinterface/talkersinterface.go index 09c2033..806787e 100644 --- a/lib/talkers/talkersinterface/talkersinterface.go +++ b/lib/talkers/talkersinterface/talkersinterface.go @@ -18,6 +18,7 @@ type TalkersInterface interface { HelpMessage(update tgbotapi.Update) PokememesList(update tgbotapi.Update, page int) PokememeInfo(update tgbotapi.Update, player_raw dbmapping.Player) string + BestPokememesList(update tgbotapi.Update, player_raw dbmapping.Player) string // Returns PokememeAddSuccessMessage(update tgbotapi.Update)