Home

pokego @main - refs - log -
-
https://git.jolheiser.com/pokego.git
pokego hard-fork
pokego / main.go
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main

import (
	"embed"
	"encoding/json"
	"flag"
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
)

// Pokemon struct represents the data structure for a Pokémon
type Pokemon struct {
	Name  string   `json:"name"`
	Forms []string `json:"forms"`
}

// Embed assets directory
//
//go:embed assets/*
var assets embed.FS

const (
	rootDir         = "assets"
	shinyRate       = 1.0 / 128.0
	colorscriptsDir = "colorscripts"
	regularSubdir   = "regular"
	shinySubdir     = "shiny"
)

// Generation ranges for Pokémon
var generations = map[string][2]int{
	"1": {1, 151},
	"2": {152, 251},
	"3": {252, 386},
	"4": {387, 493},
	"5": {494, 649},
	"6": {650, 721},
	"7": {722, 809},
	"8": {810, 898},
}

// printFile prints the content of the specified file
func printFile(filepath string) {
	content, err := assets.ReadFile(filepath)
	if err != nil {
		fmt.Println("Error reading file:", err)
		return
	}
	fmt.Print(string(content))
}

// readPokemonJSON reads the pokemon.json file from the embedded assets
func readPokemonJSON() []Pokemon {
	file, err := assets.ReadFile(filepath.Join(rootDir, "pokemon.json"))
	if err != nil {
		panic(err)
	}

	var pokemon []Pokemon
	if err := json.Unmarshal(file, &pokemon); err != nil {
		panic(err)
	}
	return pokemon
}

// listPokemonNames lists the names of all Pokémon
func listPokemonNames() {
	pokemon := readPokemonJSON()
	for _, p := range pokemon {
		fmt.Println(p.Name)
	}
}

// showPokemonByName displays Pokémon information based on its name
func showPokemonByName(name string, showTitle, shiny bool, form string) {
	colorSubdir := regularSubdir
	if shiny {
		colorSubdir = shinySubdir
	}

	pokemon := readPokemonJSON()
	pokemonNames := make(map[string]struct{})

	for _, p := range pokemon {
		pokemonNames[p.Name] = struct{}{}
	}

	if _, exists := pokemonNames[name]; !exists {
		fmt.Printf("invalid pokemon %s\n", name)
		os.Exit(1)
	}

	if form != "" {
		var alternateForms []string
		for _, p := range pokemon {
			if p.Name == name {
				alternateForms = p.Forms
				break
			}
		}
		if !contains(alternateForms, form) {
			fmt.Printf("invalid form '%s' for pokemon %s\n", form, name)
			fmt.Println("available alternate forms are:")
			for _, f := range alternateForms {
				fmt.Printf("- %s\n", f)
			}
			os.Exit(1)
		}
		name += "-" + form
	}

	pokemonFile := filepath.Join(rootDir, colorscriptsDir, colorSubdir, name)
	if showTitle {
		if shiny {
			fmt.Printf("%s (shiny)\n", name)
		} else {
			fmt.Println(name)
		}
	}
	printFile(pokemonFile)
}

// showRandomPokemon displays a random Pokémon based on specified generations
func showRandomPokemon(generationsStr string, showTitle, shiny bool) {
	var startGen, endGen string
	genList := strings.Split(generationsStr, ",")

	if len(genList) > 1 {
		startGen = genList[rand.Intn(len(genList))]
		endGen = startGen
	} else if strings.Contains(generationsStr, "-") {
		parts := strings.Split(generationsStr, "-")
		startGen, endGen = parts[0], parts[1]
	} else {
		startGen = generationsStr
		endGen = startGen
	}

	pokemon := readPokemonJSON()
	startIdx, ok := generations[startGen]
	if !ok {
		fmt.Printf("invalid generation '%s'\n", generationsStr)
		os.Exit(1)
	}

	endIdx, ok := generations[endGen]
	if !ok {
		fmt.Printf("invalid generation '%s'\n", generationsStr)
		os.Exit(1)
	}

	randomIdx := rand.Intn(endIdx[1]-startIdx[0]+1) + startIdx[0]
	randomPokemon := pokemon[randomIdx-1].Name

	if !shiny && rand.Float64() <= shinyRate {
		shiny = true
	}
	showPokemonByName(randomPokemon, showTitle, shiny, "")
}

// contains checks if a slice contains a specific item
func contains(slice []string, item string) bool {
	for _, v := range slice {
		if v == item {
			return true
		}
	}
	return false
}

// main function to handle command-line flags and execute appropriate actions
func main() {
	fs := flag.NewFlagSet("pokego", flag.ExitOnError)
	listFlag := fs.Bool("list", false, "Print list of all pokemon")
	fs.BoolVar(listFlag, "l", *listFlag, "--list")
	nameFlag := fs.String("name", "", "Select pokemon by name")
	fs.StringVar(nameFlag, "n", *nameFlag, "--name")
	formFlag := fs.String("form", "", "Show an alternate form for a pokemon")
	fs.StringVar(formFlag, "f", *formFlag, "--form")
	noTitleFlag := fs.Bool("no-title", false, "Do not display pokemon name")
	shinyFlag := fs.Bool("shiny", false, "Show the shiny version of a pokemon instead")
	fs.BoolVar(shinyFlag, "s", *shinyFlag, "--shiny")
	generationFlag := fs.String("generation", "", "Generation number or range filter")
	fs.StringVar(generationFlag, "g", *generationFlag, "--generation")
	if err := fs.Parse(os.Args[1:]); err != nil {
		fmt.Println(err)
		return
	}

	if *listFlag {
		listPokemonNames()
	} else if *nameFlag != "" {
		showPokemonByName(*nameFlag, !*noTitleFlag, *shinyFlag, *formFlag)
	} else {
		gen := "1-8"
		if *generationFlag != "" {
			gen = *generationFlag
		}
		showRandomPokemon(gen, !*noTitleFlag, *shinyFlag)
	}
}