Pokememe possibility draft
This commit is contained in:
parent
c1c37a06e1
commit
2849d16587
@ -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
|
||||
|
@ -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()
|
||||
|
28
lib/getters/exported.go
Normal file
28
lib/getters/exported.go
Normal file
@ -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...")
|
||||
}
|
10
lib/getters/gettersinterface/gettersinterface.go
Normal file
10
lib/getters/gettersinterface/gettersinterface.go
Normal file
@ -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
|
||||
}
|
77
lib/getters/possibility.go
Normal file
77
lib/getters/possibility.go
Normal file
@ -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
|
||||
}
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user