Refactoring: now we respect gofmt and have some comments
This commit is contained in:
@@ -15,14 +15,17 @@ var (
|
||||
c *appcontext.Context
|
||||
)
|
||||
|
||||
// Getters is a function-handling struct for package getters.
|
||||
type Getters struct{}
|
||||
|
||||
// New is an initialization function for appcontext
|
||||
func New(ac *appcontext.Context) {
|
||||
c = ac
|
||||
g := &Getters{}
|
||||
c.RegisterGettersInterface(gettersinterface.GettersInterface(g))
|
||||
}
|
||||
|
||||
// Init is a initialization function for package
|
||||
func (g *Getters) Init() {
|
||||
log.Printf("Initializing getters...")
|
||||
}
|
||||
|
@@ -8,17 +8,14 @@ import (
|
||||
"../../dbmapping"
|
||||
)
|
||||
|
||||
// GettersInterface implements Getters for importing via appcontext.
|
||||
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)
|
||||
}
|
||||
|
@@ -11,39 +11,42 @@ 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)
|
||||
// GetPlayerByID returns dbmapping.Player instance with given ID.
|
||||
func (g *Getters) GetPlayerByID(playerID int) (dbmapping.Player, bool) {
|
||||
playerRaw := dbmapping.Player{}
|
||||
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE id=?"), playerID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return player_raw, false
|
||||
return playerRaw, false
|
||||
}
|
||||
|
||||
return player_raw, true
|
||||
return playerRaw, 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)
|
||||
// GetOrCreatePlayer seeks for player in database via Telegram ID.
|
||||
// In case, when there is no player with such ID, new player will be created.
|
||||
func (g *Getters) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) {
|
||||
playerRaw := dbmapping.Player{}
|
||||
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=?"), telegramID)
|
||||
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)
|
||||
playerRaw.TelegramID = telegramID
|
||||
playerRaw.LeagueID = 0
|
||||
playerRaw.SquadID = 0
|
||||
playerRaw.Status = "nobody"
|
||||
playerRaw.CreatedAt = time.Now().UTC()
|
||||
playerRaw.UpdatedAt = time.Now().UTC()
|
||||
_, err = c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)", &playerRaw)
|
||||
if err != nil {
|
||||
log.Printf(err.Error())
|
||||
return player_raw, false
|
||||
return playerRaw, false
|
||||
}
|
||||
} else {
|
||||
log.Printf("Message user found in database.")
|
||||
}
|
||||
|
||||
return player_raw, true
|
||||
return playerRaw, true
|
||||
}
|
||||
|
@@ -14,168 +14,171 @@ import (
|
||||
// Internal functions
|
||||
|
||||
func (g *Getters) formFullPokememes(pokememes []dbmapping.Pokememe) ([]dbmapping.PokememeFull, bool) {
|
||||
pokememes_full := []dbmapping.PokememeFull{}
|
||||
pokememesArray := []dbmapping.PokememeFull{}
|
||||
elements := []dbmapping.Element{}
|
||||
err := c.Db.Select(&elements, "SELECT * FROM elements")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
return pokememesArray, false
|
||||
}
|
||||
locations := []dbmapping.Location{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
return pokememesArray, false
|
||||
}
|
||||
pokememes_elements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememes_elements, "SELECT * FROM pokememes_elements")
|
||||
pokememesElements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememesElements, "SELECT * FROM pokememes_elements")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
return pokememesArray, false
|
||||
}
|
||||
pokememes_locations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememes_locations, "SELECT * FROM pokememes_locations")
|
||||
pokememesLocations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememesLocations, "SELECT * FROM pokememes_locations")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
for i := range pokememes {
|
||||
full_pokememe := dbmapping.PokememeFull{}
|
||||
elements_listed := []dbmapping.Element{}
|
||||
locations_listed := []dbmapping.Location{}
|
||||
fullPokememe := dbmapping.PokememeFull{}
|
||||
elementsListed := []dbmapping.Element{}
|
||||
locationsListed := []dbmapping.Location{}
|
||||
|
||||
for j := range pokememes_locations {
|
||||
if pokememes_locations[j].Pokememe_id == pokememes[i].Id {
|
||||
for j := range pokememesLocations {
|
||||
if pokememesLocations[j].PokememeID == pokememes[i].ID {
|
||||
for l := range locations {
|
||||
if pokememes_locations[j].Location_id == locations[l].Id {
|
||||
locations_listed = append(locations_listed, locations[l])
|
||||
if pokememesLocations[j].LocationID == locations[l].ID {
|
||||
locationsListed = append(locationsListed, locations[l])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range pokememes_elements {
|
||||
if pokememes_elements[k].Pokememe_id == pokememes[i].Id {
|
||||
for k := range pokememesElements {
|
||||
if pokememesElements[k].PokememeID == pokememes[i].ID {
|
||||
for e := range elements {
|
||||
if pokememes_elements[k].Element_id == elements[e].Id {
|
||||
elements_listed = append(elements_listed, elements[e])
|
||||
if pokememesElements[k].ElementID == elements[e].ID {
|
||||
elementsListed = append(elementsListed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
full_pokememe.Pokememe = pokememes[i]
|
||||
full_pokememe.Elements = elements_listed
|
||||
full_pokememe.Locations = locations_listed
|
||||
fullPokememe.Pokememe = pokememes[i]
|
||||
fullPokememe.Elements = elementsListed
|
||||
fullPokememe.Locations = locationsListed
|
||||
|
||||
pokememes_full = append(pokememes_full, full_pokememe)
|
||||
pokememesArray = append(pokememesArray, fullPokememe)
|
||||
}
|
||||
|
||||
return pokememes_full, true
|
||||
return pokememesArray, true
|
||||
}
|
||||
|
||||
// External functions
|
||||
|
||||
// GetPokememes returns all existing pokememes, known by bot
|
||||
func (g *Getters) GetPokememes() ([]dbmapping.PokememeFull, bool) {
|
||||
pokememes_full := []dbmapping.PokememeFull{}
|
||||
pokememesArray := []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
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
pokememes_full, ok := g.formFullPokememes(pokememes)
|
||||
return pokememes_full, ok
|
||||
pokememesArray, ok := g.formFullPokememes(pokememes)
|
||||
return pokememesArray, ok
|
||||
}
|
||||
|
||||
func (g *Getters) GetBestPokememes(player_id int) ([]dbmapping.PokememeFull, bool) {
|
||||
pokememes_full := []dbmapping.PokememeFull{}
|
||||
player_raw, ok := g.GetPlayerByID(player_id)
|
||||
// GetBestPokememes returns all pokememes, which will be good for player to catch
|
||||
func (g *Getters) GetBestPokememes(playerID int) ([]dbmapping.PokememeFull, bool) {
|
||||
pokememesArray := []dbmapping.PokememeFull{}
|
||||
playerRaw, ok := g.GetPlayerByID(playerID)
|
||||
if !ok {
|
||||
return pokememes_full, ok
|
||||
return pokememesArray, ok
|
||||
}
|
||||
profile_raw, ok := g.GetProfile(player_id)
|
||||
profileRaw, ok := g.GetProfile(playerID)
|
||||
if !ok {
|
||||
return pokememes_full, ok
|
||||
return pokememesArray, ok
|
||||
}
|
||||
|
||||
if player_raw.League_id == 0 {
|
||||
return pokememes_full, false
|
||||
if playerRaw.LeagueID == 0 {
|
||||
return pokememesArray, 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)
|
||||
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"), playerRaw.LeagueID, profileRaw.LevelID+1)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememes_full, false
|
||||
return pokememesArray, false
|
||||
}
|
||||
|
||||
pokememes_full, ok = g.formFullPokememes(pokememes)
|
||||
return pokememes_full, ok
|
||||
pokememesArray, ok = g.formFullPokememes(pokememes)
|
||||
return pokememesArray, ok
|
||||
}
|
||||
|
||||
func (g *Getters) GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool) {
|
||||
pokememe_full := dbmapping.PokememeFull{}
|
||||
// GetPokememeByUD returns single pokememe based on internal ID in database
|
||||
func (g *Getters) GetPokememeByID(pokememeID string) (dbmapping.PokememeFull, bool) {
|
||||
fullPokememe := dbmapping.PokememeFull{}
|
||||
pokememe := dbmapping.Pokememe{}
|
||||
err := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE id=?"), pokememe_id)
|
||||
err := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE id=?"), pokememeID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
return fullPokememe, false
|
||||
}
|
||||
elements := []dbmapping.Element{}
|
||||
err = c.Db.Select(&elements, "SELECT * FROM elements")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
return fullPokememe, false
|
||||
}
|
||||
locations := []dbmapping.Location{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
return fullPokememe, false
|
||||
}
|
||||
pokememes_elements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememes_elements, "SELECT * FROM pokememes_elements WHERE pokememe_id='"+strconv.Itoa(pokememe.Id)+"'")
|
||||
pokememesElements := []dbmapping.PokememeElement{}
|
||||
err = c.Db.Select(&pokememesElements, "SELECT * FROM pokememes_elements WHERE pokememe_id='"+strconv.Itoa(pokememe.ID)+"'")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
return fullPokememe, false
|
||||
}
|
||||
pokememes_locations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememes_locations, "SELECT * FROM pokememes_locations WHERE pokememe_id='"+strconv.Itoa(pokememe.Id)+"'")
|
||||
pokememesLocations := []dbmapping.PokememeLocation{}
|
||||
err = c.Db.Select(&pokememesLocations, "SELECT * FROM pokememes_locations WHERE pokememe_id='"+strconv.Itoa(pokememe.ID)+"'")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return pokememe_full, false
|
||||
return fullPokememe, false
|
||||
}
|
||||
|
||||
elements_listed := []dbmapping.Element{}
|
||||
locations_listed := []dbmapping.Location{}
|
||||
elementsListed := []dbmapping.Element{}
|
||||
locationsListed := []dbmapping.Location{}
|
||||
|
||||
for j := range pokememes_locations {
|
||||
if pokememes_locations[j].Pokememe_id == pokememe.Id {
|
||||
for j := range pokememesLocations {
|
||||
if pokememesLocations[j].PokememeID == pokememe.ID {
|
||||
for l := range locations {
|
||||
if pokememes_locations[j].Location_id == locations[l].Id {
|
||||
locations_listed = append(locations_listed, locations[l])
|
||||
if pokememesLocations[j].LocationID == locations[l].ID {
|
||||
locationsListed = append(locationsListed, locations[l])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range pokememes_elements {
|
||||
if pokememes_elements[k].Pokememe_id == pokememe.Id {
|
||||
for k := range pokememesElements {
|
||||
if pokememesElements[k].PokememeID == pokememe.ID {
|
||||
for e := range elements {
|
||||
if pokememes_elements[k].Element_id == elements[e].Id {
|
||||
elements_listed = append(elements_listed, elements[e])
|
||||
if pokememesElements[k].ElementID == elements[e].ID {
|
||||
elementsListed = append(elementsListed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pokememe_full.Pokememe = pokememe
|
||||
pokememe_full.Elements = elements_listed
|
||||
pokememe_full.Locations = locations_listed
|
||||
fullPokememe.Pokememe = pokememe
|
||||
fullPokememe.Elements = elementsListed
|
||||
fullPokememe.Locations = locationsListed
|
||||
|
||||
return pokememe_full, true
|
||||
return fullPokememe, true
|
||||
}
|
||||
|
@@ -8,72 +8,74 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
// PossibilityRequiredPokeballs returns possibility of catching pokememe
|
||||
// It's based on location, grade of pokememe and current level of player
|
||||
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
|
||||
var basePossibility float64
|
||||
var requiredPokeballs int
|
||||
var percentile float64
|
||||
|
||||
if lvl > 3 {
|
||||
switch {
|
||||
case grade == (lvl + 1):
|
||||
base_possibility = 0.05
|
||||
basePossibility = 0.05
|
||||
case grade == lvl:
|
||||
base_possibility = 0.5
|
||||
basePossibility = 0.5
|
||||
case grade == (lvl - 1):
|
||||
base_possibility = 0.3
|
||||
basePossibility = 0.3
|
||||
case grade == (lvl - 2):
|
||||
base_possibility = 0.1
|
||||
basePossibility = 0.1
|
||||
case grade == (lvl - 3):
|
||||
base_possibility = 0.05
|
||||
basePossibility = 0.05
|
||||
default:
|
||||
base_possibility = 0.00
|
||||
basePossibility = 0.00
|
||||
}
|
||||
} else if lvl == 3 {
|
||||
switch grade {
|
||||
case 4:
|
||||
base_possibility = 0.05
|
||||
basePossibility = 0.05
|
||||
case 3:
|
||||
base_possibility = 0.5
|
||||
basePossibility = 0.5
|
||||
case 2:
|
||||
base_possibility = 0.3
|
||||
basePossibility = 0.3
|
||||
case 1:
|
||||
base_possibility = 0.15
|
||||
basePossibility = 0.15
|
||||
default:
|
||||
base_possibility = 0.00
|
||||
basePossibility = 0.00
|
||||
}
|
||||
} else if lvl == 2 {
|
||||
switch grade {
|
||||
case 3:
|
||||
base_possibility = 0.05
|
||||
basePossibility = 0.05
|
||||
case 2:
|
||||
base_possibility = 0.70
|
||||
basePossibility = 0.70
|
||||
case 1:
|
||||
base_possibility = 0.25
|
||||
basePossibility = 0.25
|
||||
default:
|
||||
base_possibility = 0.00
|
||||
basePossibility = 0.00
|
||||
}
|
||||
} else if lvl == 1 {
|
||||
switch grade {
|
||||
case 2:
|
||||
base_possibility = 0.80
|
||||
basePossibility = 0.80
|
||||
case 1:
|
||||
base_possibility = 0.20
|
||||
basePossibility = 0.20
|
||||
default:
|
||||
base_possibility = 0.00
|
||||
basePossibility = 0.00
|
||||
}
|
||||
}
|
||||
|
||||
var number_of_pokememes int = 0
|
||||
var pokememesCount int
|
||||
|
||||
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)
|
||||
err := c.Db.Get(&pokememesCount, 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 {
|
||||
percentile = base_possibility * 100.0 / float64(number_of_pokememes)
|
||||
required_pokeballs = int(100.0 / percentile)
|
||||
if basePossibility != 0 && pokememesCount != 0 {
|
||||
percentile = basePossibility * 100.0 / float64(pokememesCount)
|
||||
requiredPokeballs = int(100.0 / percentile)
|
||||
}
|
||||
|
||||
return percentile, required_pokeballs
|
||||
return percentile, requiredPokeballs
|
||||
}
|
||||
|
@@ -10,13 +10,14 @@ import (
|
||||
"../dbmapping"
|
||||
)
|
||||
|
||||
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"), player_id)
|
||||
// GetProfile returns last saved profile of player
|
||||
func (g *Getters) GetProfile(playerID int) (dbmapping.Profile, bool) {
|
||||
profileRaw := dbmapping.Profile{}
|
||||
err := c.Db.Get(&profileRaw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? ORDER BY created_at DESC LIMIT 1"), playerID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return profile_raw, false
|
||||
return profileRaw, false
|
||||
}
|
||||
|
||||
return profile_raw, true
|
||||
return profileRaw, true
|
||||
}
|
||||
|
Reference in New Issue
Block a user