Skip to content

Commit

Permalink
support filtering by name AND category
Browse files Browse the repository at this point in the history
woohoo one of the final bits of functionality!

- after selecting a pokemon by name, optionally filter entries by desired
  category
- then, randomly select from all matching entries
  - if no category match is found, fall back to a random selection
  - if no category is specified, select a random entry like normal
  • Loading branch information
tmck-code committed Feb 22, 2024
1 parent 91dea62 commit 73a8628
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
20 changes: 19 additions & 1 deletion pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func runPrintByName(args pokesay.Args) {
names := pokedex.ReadStructFromBytes[map[string][]int](GOBAllNames)
t.Mark("read name struct")

metadata, final := pokesay.ChooseByName(names, args.NameToken, GOBCowNames, MetadataRoot)
metadata, final := pokesay.ChooseByName(names, args.NameToken, GOBCowNames, MetadataRoot, "")
t.Mark("find and read metadata")

pokesay.Print(args, final.EntryIndex, GenerateNames(metadata, args), final.Categories, GOBCowData)
Expand All @@ -150,6 +150,22 @@ func runPrintByCategory(args pokesay.Args) {
t.PrintJson()
}

func runPrintByNameAndCategory(args pokesay.Args) {
t := timer.NewTimer("runPrintByNameAndCategory", true)

names := pokedex.ReadStructFromBytes[map[string][]int](GOBAllNames)
t.Mark("read name struct")

metadata, final := pokesay.ChooseByName(names, args.NameToken, GOBCowNames, MetadataRoot, args.Category)
t.Mark("find and read metadata")

pokesay.Print(args, final.EntryIndex, GenerateNames(metadata, args), final.Categories, GOBCowData)
t.Mark("print")

t.Stop()
t.PrintJson()
}

func runPrintRandom(args pokesay.Args) {
t := timer.NewTimer("runPrintRandom", true)
choice := pokesay.RandomInt(pokedex.ReadIntFromBytes(GOBTotal))
Expand All @@ -175,6 +191,8 @@ func main() {
runListCategories()
} else if args.ListNames {
runListNames()
} else if args.NameToken != "" && args.Category != "" {
runPrintByNameAndCategory(args)
} else if args.NameToken != "" {
runPrintByName(args)
} else if args.Category != "" {
Expand Down
27 changes: 24 additions & 3 deletions src/pokesay/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ListNames(names map[string][]int) []string {
return pokedex.GatherMapKeys(names)
}

func ChooseByName(names map[string][]int, nameToken string, metadataFiles embed.FS, metadataRootDir string) (pokedex.PokemonMetadata, pokedex.PokemonEntryMapping) {
func ChooseByName(names map[string][]int, nameToken string, metadataFiles embed.FS, metadataRootDir string, category string) (pokedex.PokemonMetadata, pokedex.PokemonEntryMapping) {
match := names[nameToken]
if len(match) == 0 {
log.Fatalf("cannot find pokemon by name '%s'", nameToken)
Expand All @@ -64,8 +64,29 @@ func ChooseByName(names map[string][]int, nameToken string, metadataFiles embed.
metadataFiles,
pokedex.MetadataFpath(metadataRootDir, nameChoice),
)
choice := RandomInt(len(metadata.Entries))
return metadata, metadata.Entries[choice]

// pick a random entry
if category == "" {
choice := RandomInt(len(metadata.Entries))
return metadata, metadata.Entries[choice]
// try to filter by desired category
} else {
matching := make([]pokedex.PokemonEntryMapping, 0)
for _, entry := range metadata.Entries {
log.Printf("entry: %v", entry)
for _, entryCategory := range entry.Categories {
if entryCategory == category {
matching = append(matching, entry)
}
}
}
// if the category is not found for this pokemon, return a random entry
if len(matching) == 0 {
return metadata, metadata.Entries[RandomInt(len(metadata.Entries))]
} else {
return metadata, matching[RandomInt(len(matching))]
}
}
}

func ChooseByRandomIndex(totalInBytes []byte) (int, int) {
Expand Down

0 comments on commit 73a8628

Please sign in to comment.