2017-10-13 03:05:26 +04:00
// i2_bot – Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package getters
import (
2017-10-18 07:03:34 +04:00
// stdlib
"log"
"strconv"
// local
"../dbmapping"
2017-10-13 03:05:26 +04:00
)
2017-10-13 03:52:04 +04:00
// Internal functions
func ( g * Getters ) formFullPokememes ( pokememes [ ] dbmapping . Pokememe ) ( [ ] dbmapping . PokememeFull , bool ) {
2017-10-18 09:39:50 +04:00
pokememesArray := [ ] dbmapping . PokememeFull { }
2017-10-18 07:03:34 +04:00
elements := [ ] dbmapping . Element { }
err := c . Db . Select ( & elements , "SELECT * FROM elements" )
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
locations := [ ] dbmapping . Location { }
err = c . Db . Select ( & locations , "SELECT * FROM locations" )
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
pokememesElements := [ ] dbmapping . PokememeElement { }
err = c . Db . Select ( & pokememesElements , "SELECT * FROM pokememes_elements" )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
pokememesLocations := [ ] dbmapping . PokememeLocation { }
err = c . Db . Select ( & pokememesLocations , "SELECT * FROM pokememes_locations" )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
for i := range pokememes {
2017-10-18 09:39:50 +04:00
fullPokememe := dbmapping . PokememeFull { }
elementsListed := [ ] dbmapping . Element { }
locationsListed := [ ] dbmapping . Location { }
2017-10-18 07:03:34 +04:00
2017-10-18 09:39:50 +04:00
for j := range pokememesLocations {
if pokememesLocations [ j ] . PokememeID == pokememes [ i ] . ID {
2017-10-18 07:03:34 +04:00
for l := range locations {
2017-10-18 09:39:50 +04:00
if pokememesLocations [ j ] . LocationID == locations [ l ] . ID {
locationsListed = append ( locationsListed , locations [ l ] )
2017-10-18 07:03:34 +04:00
}
}
}
}
2017-10-18 09:39:50 +04:00
for k := range pokememesElements {
if pokememesElements [ k ] . PokememeID == pokememes [ i ] . ID {
2017-10-18 07:03:34 +04:00
for e := range elements {
2017-10-18 09:39:50 +04:00
if pokememesElements [ k ] . ElementID == elements [ e ] . ID {
elementsListed = append ( elementsListed , elements [ e ] )
2017-10-18 07:03:34 +04:00
}
}
}
}
2017-10-18 09:39:50 +04:00
fullPokememe . Pokememe = pokememes [ i ]
fullPokememe . Elements = elementsListed
fullPokememe . Locations = locationsListed
2017-10-18 07:03:34 +04:00
2017-10-18 09:39:50 +04:00
pokememesArray = append ( pokememesArray , fullPokememe )
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
return pokememesArray , true
2017-10-13 03:05:26 +04:00
}
2017-10-13 03:52:04 +04:00
// External functions
2017-10-18 09:39:50 +04:00
// GetPokememes returns all existing pokememes, known by bot
2017-10-13 03:52:04 +04:00
func ( g * Getters ) GetPokememes ( ) ( [ ] dbmapping . PokememeFull , bool ) {
2017-10-18 09:39:50 +04:00
pokememesArray := [ ] dbmapping . PokememeFull { }
2017-10-18 07:03:34 +04:00
pokememes := [ ] dbmapping . Pokememe { }
err := c . Db . Select ( & pokememes , "SELECT * FROM pokememes ORDER BY grade asc, name asc" )
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
pokememesArray , ok := g . formFullPokememes ( pokememes )
return pokememesArray , ok
2017-10-13 03:52:04 +04:00
}
2017-10-18 09:39:50 +04:00
// 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 )
2017-10-18 07:03:34 +04:00
if ! ok {
2017-10-18 09:39:50 +04:00
return pokememesArray , ok
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
profileRaw , ok := g . GetProfile ( playerID )
2017-10-18 07:03:34 +04:00
if ! ok {
2017-10-18 09:39:50 +04:00
return pokememesArray , ok
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
if playerRaw . LeagueID == 0 {
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
// TODO: make it more complicated
pokememes := [ ] dbmapping . Pokememe { }
2017-10-18 09:39:50 +04:00
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 )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return pokememesArray , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
pokememesArray , ok = g . formFullPokememes ( pokememes )
return pokememesArray , ok
2017-10-13 03:52:04 +04:00
}
2017-10-18 09:39:50 +04:00
// GetPokememeByUD returns single pokememe based on internal ID in database
func ( g * Getters ) GetPokememeByID ( pokememeID string ) ( dbmapping . PokememeFull , bool ) {
fullPokememe := dbmapping . PokememeFull { }
2017-10-18 07:03:34 +04:00
pokememe := dbmapping . Pokememe { }
2017-10-18 09:39:50 +04:00
err := c . Db . Get ( & pokememe , c . Db . Rebind ( "SELECT * FROM pokememes WHERE id=?" ) , pokememeID )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return fullPokememe , false
2017-10-18 07:03:34 +04:00
}
elements := [ ] dbmapping . Element { }
err = c . Db . Select ( & elements , "SELECT * FROM elements" )
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return fullPokememe , false
2017-10-18 07:03:34 +04:00
}
locations := [ ] dbmapping . Location { }
err = c . Db . Select ( & locations , "SELECT * FROM locations" )
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return fullPokememe , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
pokememesElements := [ ] dbmapping . PokememeElement { }
err = c . Db . Select ( & pokememesElements , "SELECT * FROM pokememes_elements WHERE pokememe_id='" + strconv . Itoa ( pokememe . ID ) + "'" )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return fullPokememe , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
pokememesLocations := [ ] dbmapping . PokememeLocation { }
err = c . Db . Select ( & pokememesLocations , "SELECT * FROM pokememes_locations WHERE pokememe_id='" + strconv . Itoa ( pokememe . ID ) + "'" )
2017-10-18 07:03:34 +04:00
if err != nil {
log . Println ( err )
2017-10-18 09:39:50 +04:00
return fullPokememe , false
2017-10-18 07:03:34 +04:00
}
2017-10-18 09:39:50 +04:00
elementsListed := [ ] dbmapping . Element { }
locationsListed := [ ] dbmapping . Location { }
2017-10-18 07:03:34 +04:00
2017-10-18 09:39:50 +04:00
for j := range pokememesLocations {
if pokememesLocations [ j ] . PokememeID == pokememe . ID {
2017-10-18 07:03:34 +04:00
for l := range locations {
2017-10-18 09:39:50 +04:00
if pokememesLocations [ j ] . LocationID == locations [ l ] . ID {
locationsListed = append ( locationsListed , locations [ l ] )
2017-10-18 07:03:34 +04:00
}
}
}
}
2017-10-18 09:39:50 +04:00
for k := range pokememesElements {
if pokememesElements [ k ] . PokememeID == pokememe . ID {
2017-10-18 07:03:34 +04:00
for e := range elements {
2017-10-18 09:39:50 +04:00
if pokememesElements [ k ] . ElementID == elements [ e ] . ID {
elementsListed = append ( elementsListed , elements [ e ] )
2017-10-18 07:03:34 +04:00
}
}
}
}
2017-10-18 09:39:50 +04:00
fullPokememe . Pokememe = pokememe
fullPokememe . Elements = elementsListed
fullPokememe . Locations = locationsListed
2017-10-18 07:03:34 +04:00
2017-10-18 09:39:50 +04:00
return fullPokememe , true
2017-10-13 03:05:26 +04:00
}