diff --git a/dependencies.sh b/dependencies.sh index 7585e20..97c964c 100755 --- a/dependencies.sh +++ b/dependencies.sh @@ -3,5 +3,5 @@ go get github.com/go-sql-driver/mysql go get github.com/jmoiron/sqlx go get github.com/pressly/goose -go get gopkg.in/telegram-bot-api.v4 +go get github.com/go-telegram-bot-api/telegram-bot-api go get gopkg.in/yaml.v2 diff --git a/i2_bot.go b/i2_bot.go index a77ca86..078bcc8 100644 --- a/i2_bot.go +++ b/i2_bot.go @@ -7,7 +7,7 @@ import ( // stdlib "time" // 3rd-party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "./lib/appcontext" "./lib/migrations" diff --git a/lib/appcontext/appcontext.go b/lib/appcontext/appcontext.go index 9d92e2a..13e4a7f 100644 --- a/lib/appcontext/appcontext.go +++ b/lib/appcontext/appcontext.go @@ -6,7 +6,7 @@ package appcontext import ( // 3rd-party "github.com/jmoiron/sqlx" - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../config" "../connections" diff --git a/lib/config/config.go b/lib/config/config.go index f64437d..5d672e4 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -12,7 +12,7 @@ import ( "gopkg.in/yaml.v2" ) -const VERSION = "0.1" +const VERSION = "0.15" type DatabaseConnection struct { Host string `yaml:"host"` diff --git a/lib/connections/connections.go b/lib/connections/connections.go index 715770d..d4f4206 100644 --- a/lib/connections/connections.go +++ b/lib/connections/connections.go @@ -7,7 +7,7 @@ import ( // stdlib "log" // 3rd-party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jmoiron/sqlx" _ "github.com/go-sql-driver/mysql" // local diff --git a/lib/migrations/9_update_locations.go b/lib/migrations/9_update_locations.go new file mode 100644 index 0000000..947fa43 --- /dev/null +++ b/lib/migrations/9_update_locations.go @@ -0,0 +1,43 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package migrations + +import ( + // stdlib + "database/sql" +) + +func UpdateLocationsUp(tx *sql.Tx) error { + _, err := tx.Exec("UPDATE `locations` SET symbol='⛪' WHERE symbol=':church:';") + if err != nil { + return err + } + _, err = tx.Exec("UPDATE `locations` SET symbol='🌲' WHERE symbol=':evergreen_tree:';") + if err != nil { + return err + } + _, err = tx.Exec("UPDATE `locations` SET symbol='🚣' WHERE symbol=':rowboat:';") + if err != nil { + return err + } + + return nil +} + +func UpdateLocationsDown(tx *sql.Tx) error { + _, err := tx.Exec("UPDATE `locations` SET symbol=':church:' WHERE symbol='⛪'';") + if err != nil { + return err + } + _, err = tx.Exec("UPDATE `locations` SET symbol=':evergreen_tree:' WHERE symbol='🌲';") + if err != nil { + return err + } + _, err = tx.Exec("UPDATE `locations` SET symbol=':rowboat:' WHERE symbol='🚣';") + if err != nil { + return err + } + + return nil +} diff --git a/lib/migrations/migrations.go b/lib/migrations/migrations.go index 757a53d..18b7c3d 100644 --- a/lib/migrations/migrations.go +++ b/lib/migrations/migrations.go @@ -23,6 +23,7 @@ func (m *Migrations) Init() { goose.AddNamedMigration("6_create_elements.go", CreateElementsUp, CreateElementsDown) goose.AddNamedMigration("7_create_leagues.go", CreateLeaguesUp, CreateLeaguesDown) goose.AddNamedMigration("8_create_relations.go", CreateRelationsUp, CreateRelationsDown) + goose.AddNamedMigration("9_update_locations.go", UpdateLocationsUp, UpdateLocationsDown) } func (m *Migrations) Migrate() error { diff --git a/lib/router/handler.go b/lib/router/handler.go index f18c73d..a41e135 100644 --- a/lib/router/handler.go +++ b/lib/router/handler.go @@ -5,7 +5,7 @@ package router import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" ) type RouterHandler struct {} diff --git a/lib/router/router.go b/lib/router/router.go index 9e8f58f..a50f2b0 100644 --- a/lib/router/router.go +++ b/lib/router/router.go @@ -11,7 +11,7 @@ import ( "strings" "time" // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../dbmappings" ) @@ -60,6 +60,7 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { var helpMsg = regexp.MustCompile("/help\\z") var helloMsg = regexp.MustCompile("/start\\z") var pokedexMsg = regexp.MustCompile("/pokede(x|ks)\\d?\\z") + var pokememeInfoMsg = regexp.MustCompile("/pk(\\d+)") // Forwards var pokememeMsg = regexp.MustCompile("(Уровень)(.+)(Опыт)(.+)\n(Элементы:)(.+)\n(.+)(💙MP)") @@ -115,6 +116,8 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { } else { c.Talkers.PokememesList(update, 1) } + case pokememeInfoMsg.MatchString(text): + c.Talkers.PokememeInfo(update) // Easter eggs case huMsg.MatchString(text): c.Talkers.MatMessage(update) diff --git a/lib/router/routerinterface/routerinterface.go b/lib/router/routerinterface/routerinterface.go index 5683212..262fc77 100644 --- a/lib/router/routerinterface/routerinterface.go +++ b/lib/router/routerinterface/routerinterface.go @@ -5,7 +5,7 @@ package routerinterface import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" ) type RouterInterface interface { diff --git a/lib/talkers/easter.go b/lib/talkers/easter.go index 1a59d6f..eb9352c 100644 --- a/lib/talkers/easter.go +++ b/lib/talkers/easter.go @@ -9,7 +9,7 @@ import ( "math/rand" "time" // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" ) func (t *Talkers) DurakMessage(update tgbotapi.Update) { diff --git a/lib/talkers/errors.go b/lib/talkers/errors.go index e78a1a6..3662fa7 100644 --- a/lib/talkers/errors.go +++ b/lib/talkers/errors.go @@ -5,7 +5,7 @@ package talkers import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" ) func (t *Talkers) AnyMessageUnauthorized(update tgbotapi.Update) { diff --git a/lib/talkers/hello.go b/lib/talkers/hello.go index 706d5d5..2a14f5e 100644 --- a/lib/talkers/hello.go +++ b/lib/talkers/hello.go @@ -5,7 +5,7 @@ package talkers import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../dbmappings" ) diff --git a/lib/talkers/help.go b/lib/talkers/help.go index f00b65d..2954079 100644 --- a/lib/talkers/help.go +++ b/lib/talkers/help.go @@ -5,7 +5,7 @@ package talkers import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../config" ) @@ -14,6 +14,7 @@ func (t *Talkers) HelpMessage(update tgbotapi.Update) { help_message := "*Бот Инстинкта Enchanched.*\n\n" help_message += "Текущая версия: *" + config.VERSION + "*\n\n" help_message += "Список команд:\n\n" + help_message += "+ /pokedex – получить список известных боту покемемов\n" help_message += "+ /help – выводит данное сообщение\n" help_message += "\n\n" help_message += "Связаться с автором: @fat0troll\n" diff --git a/lib/talkers/pokedex.go b/lib/talkers/pokedex.go index 3f1d956..9796b84 100644 --- a/lib/talkers/pokedex.go +++ b/lib/talkers/pokedex.go @@ -6,9 +6,10 @@ package talkers import ( // stdlib "log" + "strings" "strconv" // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../dbmappings" ) @@ -19,6 +20,100 @@ type PokememeFull struct { Locations []dbmappings.Locations } +func (t *Talkers) PokememeInfo(update tgbotapi.Update) string { + pokememe_number := strings.Replace(update.Message.Text, "/pk", "", 1) + + // Building pokememe + pk := dbmappings.Pokememes{} + // Checking if pokememe exists in database + err := c.Db.Get(&pk, c.Db.Rebind("SELECT * FROM pokememes WHERE id='" + pokememe_number + "'")) + if err != nil { + log.Println(err) + return "fail" + } + + elements := []dbmappings.Elements{} + err = c.Db.Select(&elements, "SELECT * FROM elements"); + if err != nil { + log.Println(err) + } + locations := []dbmappings.Locations{} + err = c.Db.Select(&locations, "SELECT * FROM locations"); + if err != nil { + log.Println(err) + } + + pokememes_elements := []dbmappings.PokememesElements{} + err = c.Db.Select(&pokememes_elements, "SELECT * FROM pokememes_elements WHERE pokememe_id='" + pokememe_number + "'"); + if err != nil { + log.Println(err) + } + pokememes_locations := []dbmappings.PokememesLocations{} + err = c.Db.Select(&pokememes_locations, "SELECT * FROM pokememes_locations WHERE pokememe_id='" + pokememe_number + "'"); + if err != nil { + log.Println(err) + } + + message := strconv.Itoa(pk.Grade) + "⃣ *" + pk.Name + "*\n" + message += pk.Description + "\n\n" + message += "Элементы:" + for i := range(pokememes_elements) { + for j := range(elements) { + if pokememes_elements[i].Element_id == elements[j].Id { + message += " " + elements[j].Symbol + } + } + } + message += "\n⚔ Атака: *" + c.Parsers.ReturnPoints(pk.Attack) + message += "*\n❤️ HP: *" + c.Parsers.ReturnPoints(pk.HP) + message += "*\n💙 MP: *" + c.Parsers.ReturnPoints(pk.MP) + if (pk.Defence != pk.Attack) { + message += "*\n🛡Защита: *" + c.Parsers.ReturnPoints(pk.Defence) + "* _(сопротивляемость покемема к поимке)_" + } else { + message += "*" + } + message += "\nСтоимость: *" + c.Parsers.ReturnPoints(pk.Price) + message += "*\nКупить: *" + if pk.Purchaseable { + message += "Можно" + } else { + message += "Нельзя" + } + message += "*\nОбитает:" + for i := range(pokememes_locations) { + for j := range(locations) { + if pokememes_locations[i].Location_id == locations[j].Id { + message += " *" + locations[j].Name + "*" + if (i + 1) < len(pokememes_locations) { + message += "," + } + } + } + } + + message += "\n" + pk.Image_url + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) + keyboard := tgbotapi.InlineKeyboardMarkup{} + for i := range(pokememes_locations) { + for j := range(locations) { + if pokememes_locations[i].Location_id == locations[j].Id { + var row []tgbotapi.InlineKeyboardButton + btn := tgbotapi.NewInlineKeyboardButtonSwitch(locations[j].Symbol + locations[j].Name, locations[j].Symbol + locations[j].Name) + row = append(row, btn) + keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, row) + } + } + } + + msg.ReplyMarkup = keyboard + msg.ParseMode = "Markdown" + + c.Bot.Send(msg) + + return "ok" +} + func (t *Talkers) PokememesList(update tgbotapi.Update, page int) { pokememes := []dbmappings.Pokememes{} err := c.Db.Select(&pokememes, "SELECT * FROM pokememes"); diff --git a/lib/talkers/pokememe_add.go b/lib/talkers/pokememe_add.go index be1370d..8188931 100644 --- a/lib/talkers/pokememe_add.go +++ b/lib/talkers/pokememe_add.go @@ -5,7 +5,7 @@ package talkers import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" ) func (t *Talkers) PokememeAddSuccessMessage(update tgbotapi.Update) { diff --git a/lib/talkers/talkersinterface/talkersinterface.go b/lib/talkers/talkersinterface/talkersinterface.go index f1a78b9..c7f2b29 100644 --- a/lib/talkers/talkersinterface/talkersinterface.go +++ b/lib/talkers/talkersinterface/talkersinterface.go @@ -5,7 +5,7 @@ package talkersinterface import ( // 3rd party - "gopkg.in/telegram-bot-api.v4" + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../../dbmappings" ) @@ -17,6 +17,7 @@ type TalkersInterface interface { HelloMessageAuthorized(update tgbotapi.Update, player_raw dbmappings.Players) HelpMessage(update tgbotapi.Update) PokememesList(update tgbotapi.Update, page int) + PokememeInfo(update tgbotapi.Update) string // Returns PokememeAddSuccessMessage(update tgbotapi.Update)