Skip to content

Commit

Permalink
Use a Count method in all plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandeaual committed Oct 10, 2020
1 parent 0a214ef commit 6bb21af
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
9 changes: 7 additions & 2 deletions plugins/custom/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ func (c *CardFiles) InsertCount(path string, name *string, count int) {
}
}

// Count return the number of cards for a given path.
func (c *CardFiles) Count(path string) int {
return c.Counts[path]
}

// String representation of a CardFiles struct.
func (c *CardFiles) String() string {
var sb strings.Builder

for _, cardInfo := range c.Cards {
count := c.Counts[cardInfo.Path]
count := c.Count(cardInfo.Path)
sb.WriteString(strconv.Itoa(count))
sb.WriteString(" ")
sb.WriteString(cardInfo.Path)
Expand All @@ -86,7 +91,7 @@ func cardFilesToDeck(cards *CardFiles, name string, options map[string]interface
for _, cardInfo := range cards.Cards {
card := plugins.CardInfo{
ImageURL: cardInfo.Path,
Count: cards.Counts[cardInfo.Path],
Count: cards.Count(cardInfo.Path),
}
if cardInfo.Name != nil {
card.Name = *cardInfo.Name
Expand Down
17 changes: 12 additions & 5 deletions plugins/pkm/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,32 @@ func (c *CardNames) Insert(name string, set string, number string) {

// InsertCount inserts several new cards in a CardNames struct.
func (c *CardNames) InsertCount(name string, set string, number string, count int) {
_, found := c.Counts[name+set]
idx := name + set
_, found := c.Counts[idx]
if !found {
c.Names = append(c.Names, CardInfo{
Name: name,
Set: set,
Number: number,
})
c.Counts[name+set] = count
c.Counts[idx] = count
} else {
c.Counts[name+set] = c.Counts[name] + count
c.Counts[idx] = c.Counts[idx] + count
}
}

// Count return the number of cards for a given name and set.
func (c *CardNames) Count(name string, set string) int {
idx := name + set
return c.Counts[idx]
}

// String representation of a CardNames struct.
func (c *CardNames) String() string {
var sb strings.Builder

for _, cardInfo := range c.Names {
count := c.Counts[cardInfo.Name+cardInfo.Set]
count := c.Count(cardInfo.Name, cardInfo.Set)
sb.WriteString(strconv.Itoa(count))
sb.WriteString(" ")
sb.WriteString(cardInfo.Name)
Expand All @@ -96,7 +103,7 @@ func cardNamesToDeck(cards *CardNames, name string, options map[string]interface
}

for _, cardInfo := range cards.Names {
count := cards.Counts[cardInfo.Name+cardInfo.Set]
count := cards.Count(cardInfo.Name, cardInfo.Set)

set, found := getSetCode(cardInfo.Set)
if !found {
Expand Down
9 changes: 7 additions & 2 deletions plugins/vanguard/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ func (c *CardNames) InsertCount(name string, count int) {
}
}

// Count return the number of cards for a given name.
func (c *CardNames) Count(name string) int {
return c.Counts[name]
}

// String representation of a CardNames struct.
func (c *CardNames) String() string {
var sb strings.Builder

for _, name := range c.Names {
count := c.Counts[name]
count := c.Count(name)
sb.WriteString(strconv.Itoa(count))
sb.WriteString(" ")
sb.WriteString(name)
Expand Down Expand Up @@ -96,7 +101,7 @@ func cardNamesToDeck(cards *CardNames, name string, options map[string]interface
}

for _, cardName := range cards.Names {
count := cards.Counts[cardName]
count := cards.Count(cardName)

log.Debugf("Querying card %s (prefer premium: %v)", cardName, preferPremium)

Expand Down
18 changes: 14 additions & 4 deletions plugins/ygo/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ func (c *CardIDs) InsertCount(id int64, count int) {
}
}

// Count return the number of cards for a given ID.
func (c *CardIDs) Count(id int64) int {
return c.Counts[id]
}

// String representation of a CardIDs struct.
func (c *CardIDs) String() string {
var sb strings.Builder

for _, id := range c.IDs {
count := c.Counts[id]
count := c.Count(id)
sb.WriteString(strconv.Itoa(count))
sb.WriteString("x ")
sb.WriteString(strconv.FormatInt(id, 10))
Expand Down Expand Up @@ -127,12 +132,17 @@ func (c *CardNames) InsertCount(name string, count int) {
}
}

// Count return the number of cards for a given name.
func (c *CardNames) Count(name string) int {
return c.Counts[name]
}

// String representation of a CardNames struct.
func (c *CardNames) String() string {
var sb strings.Builder

for _, name := range c.Names {
count := c.Counts[name]
count := c.Count(name)
sb.WriteString(strconv.Itoa(count))
sb.WriteString(" ")
sb.WriteString(name)
Expand All @@ -152,7 +162,7 @@ func cardIDsToDeck(cards *CardIDs, deckName string, format api.Format) (*plugins
var tokens []plugins.CardInfo

for _, id := range cards.IDs {
count := cards.Counts[id]
count := cards.Count(id)

log.Debugf("Querying card ID %d", id)

Expand Down Expand Up @@ -207,7 +217,7 @@ func cardNamesToDeck(cards *CardNames, deckName string, format api.Format) (*plu
var tokens []plugins.CardInfo

for _, name := range cards.Names {
count := cards.Counts[name]
count := cards.Count(name)

log.Debugf("Querying card name %s", name)

Expand Down

0 comments on commit 6bb21af

Please sign in to comment.