Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/getters/pokememes.go

185 lines
5.2 KiB
Go
Raw Normal View History

// 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:52:04 +04:00
// Internal functions
func (g *Getters) formFullPokememes(pokememes []dbmapping.Pokememe) ([]dbmapping.PokememeFull, bool) {
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)
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)
return pokememesArray, false
2017-10-18 07:03:34 +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)
return pokememesArray, false
2017-10-18 07:03:34 +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)
return pokememesArray, false
2017-10-18 07:03:34 +04:00
}
for i := range pokememes {
fullPokememe := dbmapping.PokememeFull{}
elementsListed := []dbmapping.Element{}
locationsListed := []dbmapping.Location{}
2017-10-18 07:03:34 +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 {
if pokememesLocations[j].LocationID == locations[l].ID {
locationsListed = append(locationsListed, locations[l])
2017-10-18 07:03:34 +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 {
if pokememesElements[k].ElementID == elements[e].ID {
elementsListed = append(elementsListed, elements[e])
2017-10-18 07:03:34 +04:00
}
}
}
}
fullPokememe.Pokememe = pokememes[i]
fullPokememe.Elements = elementsListed
fullPokememe.Locations = locationsListed
2017-10-18 07:03:34 +04:00
pokememesArray = append(pokememesArray, fullPokememe)
2017-10-18 07:03:34 +04:00
}
return pokememesArray, true
}
2017-10-13 03:52:04 +04:00
// External functions
// GetPokememes returns all existing pokememes, known by bot
2017-10-13 03:52:04 +04:00
func (g *Getters) GetPokememes() ([]dbmapping.PokememeFull, bool) {
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)
return pokememesArray, false
2017-10-18 07:03:34 +04:00
}
pokememesArray, ok := g.formFullPokememes(pokememes)
return pokememesArray, ok
2017-10-13 03:52:04 +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 {
return pokememesArray, ok
2017-10-18 07:03:34 +04:00
}
profileRaw, ok := g.GetProfile(playerID)
2017-10-18 07:03:34 +04:00
if !ok {
return pokememesArray, ok
2017-10-18 07:03:34 +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{}
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)
return pokememesArray, false
2017-10-18 07:03:34 +04:00
}
pokememesArray, ok = g.formFullPokememes(pokememes)
return pokememesArray, ok
2017-10-13 03:52:04 +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{}
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)
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)
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)
return fullPokememe, false
2017-10-18 07:03:34 +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)
return fullPokememe, false
2017-10-18 07:03:34 +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)
return fullPokememe, false
2017-10-18 07:03:34 +04:00
}
elementsListed := []dbmapping.Element{}
locationsListed := []dbmapping.Location{}
2017-10-18 07:03:34 +04:00
for j := range pokememesLocations {
if pokememesLocations[j].PokememeID == pokememe.ID {
2017-10-18 07:03:34 +04:00
for l := range locations {
if pokememesLocations[j].LocationID == locations[l].ID {
locationsListed = append(locationsListed, locations[l])
2017-10-18 07:03:34 +04:00
}
}
}
}
for k := range pokememesElements {
if pokememesElements[k].PokememeID == pokememe.ID {
2017-10-18 07:03:34 +04:00
for e := range elements {
if pokememesElements[k].ElementID == elements[e].ID {
elementsListed = append(elementsListed, elements[e])
2017-10-18 07:03:34 +04:00
}
}
}
}
fullPokememe.Pokememe = pokememe
fullPokememe.Elements = elementsListed
fullPokememe.Locations = locationsListed
2017-10-18 07:03:34 +04:00
return fullPokememe, true
}