diff --git a/cmd/init/init.go b/cmd/init/init.go index ab38ecf..079b002 100644 --- a/cmd/init/init.go +++ b/cmd/init/init.go @@ -1,8 +1,8 @@ package main import ( - "errors" "fmt" + _ "golang.org/x/image/webp" "image" _ "image/png" "io/ioutil" @@ -15,7 +15,7 @@ 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") { + if strings.Contains(fullPath, ".gif") || strings.Contains(fullPath, ".jpg") || strings.Contains(fullPath, ".svg") { err := os.Remove(fullPath) if err != nil { panic(err) @@ -63,33 +63,42 @@ } shinesEnabled := strings.Split(string(shinesEnabledBytes), "\r\n") + pokemonCount := 0 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 - } - } + shinyImageName := getFileName(fmt.Sprintf("%03d-00-shiny", i)) + imageName := getFileName(fmt.Sprintf("%03d-00", i)) + if doesFileExist(imageName) { + pokemonCount++ + fmt.Printf("INSERT INTO pokemon (id, dex_number, region, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, %d, '%s', '%s', %t, '%s', '%s'); \n \n", pokemonCount, i, "", pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyImageName, imageName) } - 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")) - } - } + shinyAlolaImageName := getFileName(fmt.Sprintf("%03d-31-shiny", i)) + alolaImageName := getFileName(fmt.Sprintf("%03d-31", i)) + if doesFileExist(alolaImageName) { + pokemonCount++ + fmt.Printf("INSERT INTO pokemon (id, dex_number, region, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, %d, '%s', '%s', %t, '%s', '%s'); \n \n", pokemonCount, i, "alola", pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyAlolaImageName, alolaImageName) } - 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) + + shinyGalarImageName := getFileName(fmt.Sprintf("%03d-61-shiny", i)) + galarImageName := getFileName(fmt.Sprintf("%03d-61", i)) + if doesFileExist(galarImageName) { + pokemonCount++ + fmt.Printf("INSERT INTO pokemon (id, dex_number, region, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, %d, '%s', '%s', %t, '%s', '%s'); \n \n", pokemonCount, i, "galar", pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyGalarImageName, galarImageName) + } + } } +func getFileName(filename string) string { + if doesFileExist(fmt.Sprintf("%s.png", filename)) { + return fmt.Sprintf("%s.png", filename) + } else if doesFileExist(fmt.Sprintf("%s.webp", filename)) { + return fmt.Sprintf("%s.webp", filename) + } + return "" +} + func doesFileExist(fileName string) bool { - if _, err := os.Stat(fmt.Sprintf("./static/images/%s", fileName)); os.IsNotExist(err) { + if _, err := os.Stat(fmt.Sprintf("./static/images/%s", fileName)); os.IsNotExist(err) || fileName == "" { return false } return true