diff --git a/i2_bot.go b/i2_bot.go index 078bcc8..a675de6 100644 --- a/i2_bot.go +++ b/i2_bot.go @@ -10,6 +10,7 @@ import ( "github.com/go-telegram-bot-api/telegram-bot-api" // local "./lib/appcontext" + "./lib/getters" "./lib/migrations" "./lib/parsers" "./lib/router" @@ -28,6 +29,7 @@ func main() { c.RunDatabaseMigrations() parsers.New(c) talkers.New(c) + getters.New(c) u := tgbotapi.NewUpdate(0) u.Timeout = 60 diff --git a/lib/appcontext/appcontext.go b/lib/appcontext/appcontext.go index 13e4a7f..3472b41 100644 --- a/lib/appcontext/appcontext.go +++ b/lib/appcontext/appcontext.go @@ -11,6 +11,7 @@ import ( "../config" "../connections" // interfaces + "../getters/gettersinterface" "../migrations/migrationsinterface" "../parsers/parsersinterface" "../router/routerinterface" @@ -25,6 +26,7 @@ type Context struct { Parsers parsersinterface.ParsersInterface Db *sqlx.DB Talkers talkersinterface.TalkersInterface + Getters gettersinterface.GettersInterface } func (c *Context) Init() { @@ -52,6 +54,10 @@ func (c *Context) RegisterTalkersInterface(ti talkersinterface.TalkersInterface) c.Talkers = ti } +func (c *Context) RegisterGettersInterface(gi gettersinterface.GettersInterface) { + c.Getters = gi +} + func (c *Context) RunDatabaseMigrations() { c.Migrations.SetDialect("mysql") c.Migrations.Migrate() diff --git a/lib/getters/exported.go b/lib/getters/exported.go new file mode 100644 index 0000000..148da19 --- /dev/null +++ b/lib/getters/exported.go @@ -0,0 +1,28 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package getters + +import ( + // stdlib + "log" + // local + "../appcontext" + "../getters/gettersinterface" +) + +var ( + c *appcontext.Context +) + +type Getters struct {} + +func New(ac *appcontext.Context) { + c = ac + g := &Getters{} + c.RegisterGettersInterface(gettersinterface.GettersInterface(g)) +} + +func (g *Getters) Init() { + log.Printf("Initializing getters...") +} diff --git a/lib/getters/gettersinterface/gettersinterface.go b/lib/getters/gettersinterface/gettersinterface.go new file mode 100644 index 0000000..d032fec --- /dev/null +++ b/lib/getters/gettersinterface/gettersinterface.go @@ -0,0 +1,10 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package gettersinterface + +type GettersInterface interface { + Init() + // Possibilities + PossibilityRequiredPokeballs(location int, grade int, lvl int) int +} diff --git a/lib/getters/possibility.go b/lib/getters/possibility.go new file mode 100644 index 0000000..9ca179a --- /dev/null +++ b/lib/getters/possibility.go @@ -0,0 +1,77 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package getters + +import ( + // stdlib + "log" +) + +func (g *Getters) PossibilityRequiredPokeballs(location int, grade int, lvl int) int { + var base_possibility float64 = 0.00 + var required_pokeballs int = 0 + + if lvl > 3 { + switch { + case grade == (lvl + 1): + base_possibility = 0.05 + case grade == lvl: + base_possibility = 0.5 + case grade == (lvl - 1): + base_possibility = 0.3 + case grade == (lvl - 2): + base_possibility = 0.1 + case grade == (lvl - 3): + base_possibility = 0.05 + default: + base_possibility = 0.00 + } + } else if lvl == 3 { + switch grade { + case 4: + base_possibility = 0.05 + case 3: + base_possibility = 0.5 + case 2: + base_possibility = 0.3 + case 1: + base_possibility = 0.15 + default: + base_possibility = 0.00 + } + } else if lvl == 2 { + switch grade { + case 3: + base_possibility = 0.05 + case 2: + base_possibility = 0.70 + case 1: + base_possibility = 0.25 + default: + base_possibility = 0.00 + } + } else if lvl == 1 { + switch grade { + case 2: + base_possibility = 0.80 + case 1: + base_possibility = 0.20 + default: + base_possibility = 0.00 + } + } + + var number_of_pokememes int = 0 + + err := c.Db.Get(&number_of_pokememes, c.Db.Rebind("SELECT count(*) FROM pokememes p, pokememes_locations pl WHERE p.grade = ? AND pl.location_id = ? AND pl.pokememe_id = p.id;"), grade, location) + if err != nil { + log.Println(err) + } + + if base_possibility != 0 && number_of_pokememes != 0 { + required_pokeballs = int(1.0 / (base_possibility / float64(number_of_pokememes))) + } + + return required_pokeballs +} diff --git a/lib/router/router.go b/lib/router/router.go index e91d21f..8547b1f 100644 --- a/lib/router/router.go +++ b/lib/router/router.go @@ -129,7 +129,7 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { c.Talkers.PokememesList(update, 1) } case pokememeInfoMsg.MatchString(text): - c.Talkers.PokememeInfo(update) + c.Talkers.PokememeInfo(update, player_raw) // Profile info case meMsg.MatchString(text): if player_raw.Id != 0 { diff --git a/lib/talkers/pokedex.go b/lib/talkers/pokedex.go index 9796b84..80c5875 100644 --- a/lib/talkers/pokedex.go +++ b/lib/talkers/pokedex.go @@ -20,13 +20,21 @@ type PokememeFull struct { Locations []dbmappings.Locations } -func (t *Talkers) PokememeInfo(update tgbotapi.Update) string { +func (t *Talkers) PokememeInfo(update tgbotapi.Update, player_raw dbmappings.Players) string { pokememe_number := strings.Replace(update.Message.Text, "/pk", "", 1) + var calculate_possibilites bool = false + profile_raw := dbmappings.Profiles{} + err := c.Db.Get(&profile_raw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? ORDER BY created_at DESC LIMIT 1"), player_raw.Id) + if err != nil { + log.Println(err) + } else { + calculate_possibilites = true + } // 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 + "'")) + err = c.Db.Get(&pk, c.Db.Rebind("SELECT * FROM pokememes WHERE id='" + pokememe_number + "'")) if err != nil { log.Println(err) return "fail" @@ -91,6 +99,17 @@ func (t *Talkers) PokememeInfo(update tgbotapi.Update) string { } } + if calculate_possibilites { + message += "\nВероятность поимки:" + for i := range(pokememes_locations) { + for j := range(locations) { + if pokememes_locations[i].Location_id == locations[j].Id { + message += "\n" + locations[j].Name + " – " + strconv.Itoa(c.Getters.PossibilityRequiredPokeballs(locations[j].Id, pk.Grade, profile_raw.Level_id)) + "⭕" + } + } + } + } + message += "\n" + pk.Image_url msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) diff --git a/lib/talkers/talkersinterface/talkersinterface.go b/lib/talkers/talkersinterface/talkersinterface.go index 77c99cb..3870ebe 100644 --- a/lib/talkers/talkersinterface/talkersinterface.go +++ b/lib/talkers/talkersinterface/talkersinterface.go @@ -17,7 +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 + PokememeInfo(update tgbotapi.Update, player_raw dbmappings.Players) string // Returns PokememeAddSuccessMessage(update tgbotapi.Update)