Skip to content

Commit

Permalink
Merge pull request #15 from tmck-code/compress-pokemon-data
Browse files Browse the repository at this point in the history
Compress all pokemon cowfile data
  • Loading branch information
tmck-code authored Mar 16, 2022
2 parents 594e451 + bb97e91 commit a49d97b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
13 changes: 1 addition & 12 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ func check(e error) {
}
}

type PokemonEntry struct {
Name string
Data []byte
Categories []string
}

type PokemonEntryMap struct {
Categories map[string][]PokemonEntry
Total int
}

func printSpeechBubbleLine(line string, width int) {
if len(line) > width {
fmt.Println("|", line)
Expand Down Expand Up @@ -88,7 +77,7 @@ func printPokemon(list pokedex.PokemonEntryMap) {
for _, pokemon := range list.Categories {
if idx == chosenCategory {
chosenPokemon := pokemon[randomInt(len(pokemon))]
binary.Write(os.Stdout, binary.LittleEndian, chosenPokemon.Data)
binary.Write(os.Stdout, binary.LittleEndian, pokedex.Decompress(chosenPokemon.Data))
fmt.Printf("choice: %s / categories: %s\n", chosenPokemon.Name, chosenPokemon.Categories)
}
idx += 1
Expand Down
8 changes: 4 additions & 4 deletions src/pokedex.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func findFiles(dirpath string, ext string, skip []string) pokedex.PokemonEntryMa
pokemonCategories := createCategories(fpath)

for _, c := range pokemonCategories {
p := pokedex.PokemonEntry{Name: createName(fpath), Categories: createCategories(fpath), Data: data}
p := pokedex.NewPokemonEntry(data, createName(fpath), createCategories(fpath))
if val, ok := categories.Categories[c]; ok {
val = append(val, p)
val = append(val, *p)
} else {
categories.Categories[c] = []pokedex.PokemonEntry{p}
categories.Categories[c] = []pokedex.PokemonEntry{*p}
}
categories.Categories[c] = append(categories.Categories[c], p)
categories.Categories[c] = append(categories.Categories[c], *p)
}
}
return err
Expand Down
36 changes: 36 additions & 0 deletions src/pokedex/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"encoding/gob"
"compress/gzip"
"log"
)

Expand All @@ -25,6 +26,41 @@ type PokemonEntryMap struct {
NCategories int
}

func NewPokemonEntry(data []byte, name string, categories []string) *PokemonEntry {
return &PokemonEntry{
Name: name,
Categories: categories,
Data: Compress(data),
}
}

func Compress(data []byte) []byte {
var b bytes.Buffer
gz := gzip.NewWriter(&b)

_, err := gz.Write(data)
check(err)

err = gz.Close()
check(err)

return b.Bytes()
}

func Decompress(data []byte) []byte {
buf := bytes.NewBuffer(data)

reader, err := gzip.NewReader(buf)
check(err)

var resB bytes.Buffer

_, err = resB.ReadFrom(reader)
check(err)

return resB.Bytes()
}

func WriteToFile(categories PokemonEntryMap, fpath string) {
ostream, err := os.Create(fpath)
check(err)
Expand Down

0 comments on commit a49d97b

Please sign in to comment.