Newer
Older
pokemon-go-trade / internal / pokemon / pokemon.go
package pokemon

import (
	"fmt"
	"git.ssns.se/git/frozendragon/pokemon-go-trade/internal/db"
)

type Package struct {
	Db db.Interface
}

type Interface interface {
	GetAvailablePokemons() (*Pokemons, error)
}

type Pokemons struct {
	Pokemons []Pokemon `json:"pokemons"`
}

type Pokemon struct {
	ID         int       `json:"id"`
	DexNumber  int       `json:"dex_number"`
	Url        string    `json:"url"`
	Variations []Pokemon `json:"variations, omitempty"`
}

func (p *Package) GetAvailablePokemons() (*Pokemons, error) {
	dbPokemons, err := p.Db.GetPokemons()
	if err != nil {
		return nil, err
	}
	var pokemons Pokemons
	for _, pokemon := range dbPokemons {
		dexFound := false
		if pokemon.Region != "" {
			for i, p := range pokemons.Pokemons {
				if p.DexNumber == pokemon.DexNumber {
					pokemons.Pokemons[i].Variations = append(pokemons.Pokemons[i].Variations, Pokemon{
						ID:        pokemon.ID,
						DexNumber: pokemon.DexNumber,
						Url:       fmt.Sprintf("image/%s", pokemon.ShinyImageName),
					})
					pokemons.Pokemons[i].Variations = append(pokemons.Pokemons[i].Variations, Pokemon{
						ID:        pokemon.ID,
						DexNumber: pokemon.DexNumber,
						Url:       fmt.Sprintf("image/%s", pokemon.ImageName),
					})
					dexFound = true
					break
				}
			}
		}
		if dexFound {
			continue
		}
		if pokemon.ShinyEnabled {
			pokemons.Pokemons = append(pokemons.Pokemons, Pokemon{
				ID:        pokemon.ID,
				DexNumber: pokemon.DexNumber,
				Url:       fmt.Sprintf("image/%s", pokemon.ShinyImageName),
				Variations: []Pokemon{
					{
						ID:  pokemon.ID,
						Url: fmt.Sprintf("image/%s", pokemon.ImageName),
					},
				},
			})
		} else {
			pokemons.Pokemons = append(pokemons.Pokemons, Pokemon{
				ID:         pokemon.ID,
				DexNumber:  pokemon.DexNumber,
				Url:        fmt.Sprintf("image/%s", pokemon.ImageName),
				Variations: []Pokemon{},
			})
		}

	}
	return &pokemons, nil
}