-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
58 lines (48 loc) · 1.25 KB
/
encode.go
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
package goterra
import (
"bytes"
"encoding/base32"
)
func encodeBase32(data []byte) string {
encode := base32.StdEncoding.EncodeToString(data)
return encode
}
func encodeCardBlock(numOfCards uint8, dst *bytes.Buffer, deck *Deck) {
tmpDeck := *NewDeck()
for _, cg := range deck.CardGroups {
if cg.Count == numOfCards {
tmpDeck.CardGroups = append(tmpDeck.CardGroups, cg)
}
}
dm := make(map[string]*Deck)
for _, cg := range tmpDeck.CardGroups {
groupId := cg.Card.getGroupId()
if dt, ok := dm[groupId]; ok {
dt.CardGroups = append(dt.CardGroups, cg)
} else {
dm[groupId] = NewDeck()
dm[groupId].CardGroups = append(dm[groupId].CardGroups, cg)
}
}
dst.WriteByte(byte(len(dm)))
for _, d := range dm {
dst.WriteByte(byte(len(d.CardGroups)))
dst.WriteByte(d.CardGroups[0].Card.Set)
dst.WriteByte(d.CardGroups[0].Card.Faction)
for _, c := range d.CardGroups {
dst.WriteByte(c.Card.Id)
}
}
}
func encodeDeckCardsFromBuffer(dest *bytes.Buffer, deck *Deck) {
for n := 3; n > 0; n-- {
encodeCardBlock(uint8(n), dest, deck)
}
}
func (d *Deck) Encode() string {
buf := bytes.NewBuffer([]byte{})
buf.WriteByte((d.Format << 4) + d.Version)
encodeDeckCardsFromBuffer(buf, d)
s := buf.String()
return encodeBase32([]byte(s))
}