Newer
Older
pokemon-go-trade / cmd / init / init.go
package main

import (
	"errors"
	"fmt"
	"image"
	_ "image/png"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	files, _ := ioutil.ReadDir("./static/images")
	for _, imgFile := range files {
		fullPath := filepath.Join("./static/images", imgFile.Name())
		if strings.Contains(fullPath, ".webp") || strings.Contains(fullPath, ".gif") || strings.Contains(fullPath, ".jpg") {
			err := os.Remove(fullPath)
			if err != nil {
				panic(err)
			}
			continue
		}
		if reader, err := os.Open(fullPath); err == nil {
			im, _, err := image.DecodeConfig(reader)
			if err != nil {
				panic(err)
			}
			err = reader.Close()
			if err != nil {
				panic(err)
			}
			if im.Height < 256 || im.Width < 256 {
				err := os.Remove(fullPath)
				if err != nil {
					panic(err)
				}
				continue
			}
			if strings.Contains(imgFile.Name(), "(") {
				newName := imgFile.Name()[:strings.Index(imgFile.Name(), "(")-1] + imgFile.Name()[strings.Index(imgFile.Name(), ")")+1:]
				err := os.Rename(fullPath, filepath.Join("./static/images", newName))
				if err != nil {
					panic(err)
				}
			}
		} else {
			panic(err)
		}

	}

	pokemonListBytes, err := ioutil.ReadFile("./cmd/init/pokemon-list.txt")
	if err != nil {
		panic(err)
	}
	pokemonList := strings.Split(string(pokemonListBytes), "\r\n")

	shinesEnabledBytes, err := ioutil.ReadFile("./cmd/init/shiny-enabled.txt")
	if err != nil {
		panic(err)
	}
	shinesEnabled := strings.Split(string(shinesEnabledBytes), "\r\n")

	for i := 1; i < 867; i++ {
		shinyImageName := fmt.Sprintf("%03d-00-shiny.png", i)
		if !doesFileExist(shinyImageName) {
			shinyImageName := fmt.Sprintf("%03d-31-shiny.png", i)
			if !doesFileExist(shinyImageName) {
				shinyImageName := fmt.Sprintf("%03d-61-shiny.png", i)
				if !doesFileExist(shinyImageName) {
					continue
				}
			}
		}
		imageName := fmt.Sprintf("%03d-00.png", i)
		if !doesFileExist(shinyImageName) {
			shinyImageName = fmt.Sprintf("%03d-31.png", i)
			if !doesFileExist(shinyImageName) {
				shinyImageName = fmt.Sprintf("%03d-61.png", i)
				if !doesFileExist(shinyImageName) {
					panic(errors.New("non-shiny not found"))
				}
			}
		}
		fmt.Printf("INSERT INTO pokemon (id, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, '%s', %t, '%s', '%s'); \n \n", i, pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyImageName, imageName)
	}
}

func doesFileExist(fileName string) bool {
	if _, err := os.Stat(fmt.Sprintf("./static/images/%s", fileName)); os.IsNotExist(err) {
		return false
	}
	return true
}

func isShinyEnabled(shinesEnabled []string, pokemon string) bool {
	for _, shinyEnabled := range shinesEnabled {
		if pokemon == shinyEnabled {
			return true
		}
	}
	return false
}