Skip to content

Commit

Permalink
MTG: properly count card with the same name but from different sets
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandeaual committed Oct 10, 2020
1 parent 8d691c6 commit 0a214ef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
43 changes: 30 additions & 13 deletions plugins/mtg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,44 @@ func (c *CardNames) Insert(name string, set *string) {

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

// Count return the number of cards for a given name and set (optional).
func (c *CardNames) Count(name string, set *string) int {
idx := name
if set != nil {
idx += *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]
count := c.Count(cardInfo.Name, cardInfo.Set)
sb.WriteString(strconv.Itoa(count))
sb.WriteString(" ")
sb.WriteString(cardInfo.Name)
if cardInfo.Set != nil {
sb.WriteString(" ")
sb.WriteString(*cardInfo.Set)
}
sb.WriteString("\n")
}

Expand Down Expand Up @@ -345,7 +362,7 @@ func cardNamesToDeck(cards *CardNames, name string, options map[string]interface
}

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

opts := scryfall.GetCardByNameOptions{}
if cardInfo.Set != nil {
Expand Down Expand Up @@ -624,11 +641,11 @@ func parseDeckLine(
// That means we found an empty line beforehand,
// assuming this would be the sideboard separator
main.Names = append(main.Names, side.Names...)
for name, count := range side.Counts {
if originalCount, found := main.Counts[name]; found {
main.Counts[name] = originalCount + count
for idx, count := range side.Counts {
if originalCount, found := main.Counts[idx]; found {
main.Counts[idx] = originalCount + count
} else {
main.Counts[name] = count
main.Counts[idx] = count
}
}
side = nil
Expand Down Expand Up @@ -762,11 +779,11 @@ func parseDeckFile(file io.Reader) (*CardNames, *CardNames, *CardNames, error) {
// Multiple empty lines with no line starting with "SB:", that means
// there was no sideboard
main.Names = append(main.Names, side.Names...)
for name, count := range side.Counts {
if originalCount, found := main.Counts[name]; found {
main.Counts[name] = originalCount + count
for idx, count := range side.Counts {
if originalCount, found := main.Counts[idx]; found {
main.Counts[idx] = originalCount + count
} else {
main.Counts[name] = count
main.Counts[idx] = count
}
}
side = nil
Expand Down
18 changes: 9 additions & 9 deletions plugins/mtg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ func TestParseDeckFile(t *testing.T) {
},
},
Counts: map[string]int{
"Blood Crypt": 2,
"Carnival // Carnage": 3,
"Demon of Catastrophes": 3,
"Demonlord Belzenlok": 1,
"Diregraf Ghoul": 4,
"Doom Whisperer": 1,
"Dragonskull Summit": 4,
"Graf Rats": 2,
"Midnight Scavengers": 2,
"Blood CryptRNA": 2,
"Carnival // CarnageRNA": 3,
"Demon of CatastrophesM19": 3,
"Demonlord BelzenlokDOM": 1,
"Diregraf GhoulM19": 4,
"Doom WhispererGRN": 1,
"Dragonskull SummitXLN": 4,
"Graf RatsEMN": 2,
"Midnight ScavengersEMN": 2,
},
}

Expand Down

0 comments on commit 0a214ef

Please sign in to comment.