Pokememes possibilities, some refactoring
This commit is contained in:
@@ -3,8 +3,20 @@
|
||||
|
||||
package gettersinterface
|
||||
|
||||
import (
|
||||
// local
|
||||
"../../dbmapping"
|
||||
)
|
||||
|
||||
type GettersInterface interface {
|
||||
Init()
|
||||
// Player
|
||||
GetOrCreatePlayer(telegram_id int) (dbmapping.Player, bool)
|
||||
// Profile
|
||||
GetProfile(player_id int) (dbmapping.Profile, bool)
|
||||
// Pokememes
|
||||
GetPokememes() ([]dbmapping.PokememeFull, bool)
|
||||
GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool)
|
||||
// Possibilities
|
||||
PossibilityRequiredPokeballs(location int, grade int, lvl int) int
|
||||
PossibilityRequiredPokeballs(location int, grade int, lvl int) (float64, int)
|
||||
}
|
||||
|
38
lib/getters/player.go
Normal file
38
lib/getters/player.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package getters
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
"time"
|
||||
// local
|
||||
"../dbmapping"
|
||||
)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
log.Printf("Message user not found in database.")
|
||||
log.Printf(err.Error())
|
||||
|
||||
// Create "nobody" user
|
||||
player_raw.Telegram_id = telegram_id
|
||||
player_raw.League_id = 0
|
||||
player_raw.Squad_id = 0
|
||||
player_raw.Status = "nobody"
|
||||
player_raw.Created_at = time.Now().UTC()
|
||||
player_raw.Updated_at = time.Now().UTC()
|
||||
_, err = c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)", &player_raw)
|
||||
if err != nil {
|
||||
log.Printf(err.Error())
|
||||
return player_raw, false
|
||||
}
|
||||
} else {
|
||||
log.Printf("Message user found in database.")
|
||||
}
|
||||
|
||||
return player_raw, true
|
||||
}
|
143
lib/getters/pokememes.go
Normal file
143
lib/getters/pokememes.go
Normal file
@@ -0,0 +1,143 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package getters
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
"strconv"
|
||||
// local
|
||||
"../dbmapping"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
elements := []dbmapping.Element{}
|
||||
err = c.Db.Select(&elements, "SELECT * FROM elements");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
}
|
||||
locations := []dbmapping.Location{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
}
|
||||
pokememes_elements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememes_elements, "SELECT * FROM pokememes_elements");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
}
|
||||
pokememes_locations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememes_locations, "SELECT * FROM pokememes_locations");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
}
|
||||
|
||||
for i := range(pokememes) {
|
||||
full_pokememe := dbmapping.PokememeFull{}
|
||||
elements_listed := []dbmapping.Element{}
|
||||
locations_listed := []dbmapping.Location{}
|
||||
|
||||
for j := range(pokememes_locations) {
|
||||
if pokememes_locations[j].Pokememe_id == pokememes[i].Id {
|
||||
for l := range(locations) {
|
||||
if pokememes_locations[j].Location_id == locations[l].Id {
|
||||
locations_listed = append(locations_listed, locations[l])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range(pokememes_elements) {
|
||||
if pokememes_elements[k].Pokememe_id == pokememes[i].Id {
|
||||
for e := range(elements) {
|
||||
if pokememes_elements[k].Element_id == elements[e].Id {
|
||||
elements_listed = append(elements_listed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
full_pokememe.Pokememe = pokememes[i]
|
||||
full_pokememe.Elements = elements_listed
|
||||
full_pokememe.Locations = locations_listed
|
||||
|
||||
pokememes_full = append(pokememes_full, full_pokememe)
|
||||
}
|
||||
|
||||
return pokememes_full, true
|
||||
}
|
||||
|
||||
func (g *Getters) GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool) {
|
||||
pokememe_full := dbmapping.PokememeFull{}
|
||||
pokememe := dbmapping.Pokememe{}
|
||||
err := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE id=?"), pokememe_id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
}
|
||||
elements := []dbmapping.Element{}
|
||||
err = c.Db.Select(&elements, "SELECT * FROM elements");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
}
|
||||
locations := []dbmapping.Location{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
}
|
||||
pokememes_elements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememes_elements, "SELECT * FROM pokememes_elements WHERE pokememe_id='" + strconv.Itoa(pokememe.Id) + "'");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
}
|
||||
pokememes_locations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememes_locations, "SELECT * FROM pokememes_locations WHERE pokememe_id='" + strconv.Itoa(pokememe.Id) + "'");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
}
|
||||
|
||||
elements_listed := []dbmapping.Element{}
|
||||
locations_listed := []dbmapping.Location{}
|
||||
|
||||
for j := range(pokememes_locations) {
|
||||
if pokememes_locations[j].Pokememe_id == pokememe.Id {
|
||||
for l := range(locations) {
|
||||
if pokememes_locations[j].Location_id == locations[l].Id {
|
||||
locations_listed = append(locations_listed, locations[l])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range(pokememes_elements) {
|
||||
if pokememes_elements[k].Pokememe_id == pokememe.Id {
|
||||
for e := range(elements) {
|
||||
if pokememes_elements[k].Element_id == elements[e].Id {
|
||||
elements_listed = append(elements_listed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pokememe_full.Pokememe = pokememe
|
||||
pokememe_full.Elements = elements_listed
|
||||
pokememe_full.Locations = locations_listed
|
||||
|
||||
return pokememe_full, true
|
||||
}
|
@@ -8,9 +8,10 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func (g *Getters) PossibilityRequiredPokeballs(location int, grade int, lvl int) int {
|
||||
func (g *Getters) PossibilityRequiredPokeballs(location int, grade int, lvl int) (float64, int) {
|
||||
var base_possibility float64 = 0.00
|
||||
var required_pokeballs int = 0
|
||||
var percentile = 0.00
|
||||
|
||||
if lvl > 3 {
|
||||
switch {
|
||||
@@ -70,8 +71,9 @@ func (g *Getters) PossibilityRequiredPokeballs(location int, grade int, lvl int)
|
||||
}
|
||||
|
||||
if base_possibility != 0 && number_of_pokememes != 0 {
|
||||
required_pokeballs = int(1.0 / (base_possibility / float64(number_of_pokememes)))
|
||||
percentile = base_possibility * 100.0 / float64(number_of_pokememes)
|
||||
required_pokeballs = int(100.0 / percentile)
|
||||
}
|
||||
|
||||
return required_pokeballs
|
||||
return percentile, required_pokeballs
|
||||
}
|
||||
|
22
lib/getters/profile.go
Normal file
22
lib/getters/profile.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package getters
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
// local
|
||||
"../dbmapping"
|
||||
)
|
||||
|
||||
func (g *Getters) GetProfile(profile_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)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return profile_raw, false
|
||||
}
|
||||
|
||||
return profile_raw, true
|
||||
}
|
Reference in New Issue
Block a user