diff --git a/pkg/serra/add.go b/pkg/serra/add.go index 97dc63d..87ccc5c 100644 --- a/pkg/serra/add.go +++ b/pkg/serra/add.go @@ -156,9 +156,11 @@ func addCards(cards []string, unique bool, count int64) error { var total int64 = 0 if foil { c.SerraCountFoil = count + c.SerraCountFoilDeck = 0 total = c.SerraCountFoil } else { c.SerraCount = count + c.SerraCountDeck = 0 total = c.SerraCount } err = coll.storageAdd(c) diff --git a/pkg/serra/card.go b/pkg/serra/card.go index d15d7ea..2ee9191 100644 --- a/pkg/serra/card.go +++ b/pkg/serra/card.go @@ -30,6 +30,7 @@ func init() { cardCmd.Flags().BoolVarP(&reserved, "reserved", "w", false, "If card is on reserved list") cardCmd.Flags().BoolVarP(&foil, "foil", "f", false, "If card is foil list") cardCmd.Flags().BoolVarP(&drawImg, "image", "g", false, "Draw card image using kitty format") + cardCmd.Flags().BoolVarP(&omitInDeck, "omit-in-deck", "q", false, "Omit cards that are in decks") rootCmd.AddCommand(cardCmd) } @@ -174,16 +175,28 @@ func showCardList(cards []Card, detail bool) { var total float64 if drawImg { for _, card := range cards { + if shouldOmit(&card) { + continue + } drawImage(&card) } } else if detail { for _, card := range cards { + if shouldOmit(&card) { + continue + } fmt.Printf("* %dx %s%s%s (%s/%s) %s%.2f%s %s %s %s\n", card.SerraCount+card.SerraCountFoil+card.SerraCountEtched, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(false), getCurrency(), Background, strings.Replace(card.ScryfallURI, "?utm_source=api", "", 1), Reset) total = total + card.getValue(false)*float64(card.SerraCount) + card.getValue(true)*float64(card.SerraCountFoil) } } else { for _, card := range cards { - fmt.Printf("* %dx %s%s%s (%s/%s) %s%.2f%s%s\n", card.SerraCount+card.SerraCountFoil+card.SerraCountEtched, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(false), getCurrency(), Reset) + if shouldOmit(&card) { + continue + } + fmt.Printf("* %dx (%dx) %s%s%s (%s/%s) %s%.2f%s%s\n", + card.SerraCount+card.SerraCountFoil+card.SerraCountEtched-card.SerraCountDeck-card.SerraCountFoilDeck-card.SerraCountEtchedDeck, + card.SerraCount+card.SerraCountFoil+card.SerraCountEtched, + Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(false), getCurrency(), Reset) total = total + card.getValue(false)*float64(card.SerraCount) + card.getValue(true)*float64(card.SerraCountFoil) } } @@ -198,6 +211,7 @@ func showCardDetails(card *Card) error { drawImage(card) } else { fmt.Printf("%s%s%s (%s/%s)\n", Purple, card.Name, Reset, card.Set, card.CollectorNumber) + fmt.Printf("Available: %d / %d\n", card.SerraCount + card.SerraCountFoil - card.SerraCountDeck - card.SerraCountFoilDeck, card.SerraCount + card.SerraCountFoil) fmt.Printf("Added: %s\n", stringToTime(card.SerraCreated)) fmt.Printf("Rarity: %s\n", card.Rarity) fmt.Printf("Scryfall: %s\n", strings.Replace(card.ScryfallURI, "?utm_source=api", "", 1)) @@ -216,6 +230,10 @@ func showCardDetails(card *Card) error { return nil } +func shouldOmit(card *Card) bool { + return omitInDeck && card.SerraCount == card.SerraCountDeck && card.SerraCountFoil == card.SerraCountFoilDeck +} + func drawImage(card *Card) { fmt.Printf("%s - %s (%s/%s)\n", card.Name, card.SetName, card.Set, card.CollectorNumber) data, err := base64.StdEncoding.DecodeString(card.SerraImage64) diff --git a/pkg/serra/deck.go b/pkg/serra/deck.go new file mode 100644 index 0000000..34ad642 --- /dev/null +++ b/pkg/serra/deck.go @@ -0,0 +1,67 @@ +package serra + +import ( + "strings" + + "github.com/spf13/cobra" + "go.mongodb.org/mongo-driver/bson" +) + +func init() { + deckCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to add") + deckCmd.Flags().BoolVarP(&foil, "foil", "f", false, "Add foil variant of card") + rootCmd.AddCommand(deckCmd) +} + +var deckCmd = &cobra.Command{ + Aliases: []string{"d"}, + Use: "deck", + Short: "Mark a card as in a deck", + Long: "Mark a card as in a deck", + SilenceErrors: true, + RunE: func(cmd *cobra.Command, cards []string) error { + deckCards(cards, count) + return nil + }, +} + +func deckCards(cards []string, count int64) error { + client := storageConnect() + coll := &Collection{client.Database("serra").Collection("cards")} + l := Logger() + defer storageDisconnect(client) + + // Loop over different cards + for _, card := range cards { + // Extract collector number and set name from card input & trim any leading 0 from collector number + + if !strings.Contains(card, "/") { + l.Errorf("Invalid card format %s. Needs to be set/collector number i.e. \"usg/13\"", card) + continue + } + + setName := strings.ToLower(strings.Split(card, "/")[0]) + collectorNumber := strings.TrimLeft(strings.Split(card, "/")[1], "0") + + if collectorNumber == "" { + l.Errorf("Invalid card format %s. Needs to be set/collector number i.e. \"usg/13\"", card) + continue + } + + // Check if card is already in collection + co, err := coll.storageFind(bson.D{{"set", setName}, {"collectornumber", collectorNumber}}, bson.D{}, 0, 0) + if err != nil { + l.Error(err) + continue + } + + if len(co) >= 1 { + modifyCardDeckCount(coll, &co[0], count, foil) + } else { + l.Errorf("Card not in collection: %s", card) + continue + } + } + storageDisconnect(client) + return nil +} diff --git a/pkg/serra/helpers.go b/pkg/serra/helpers.go index f977231..385fab1 100644 --- a/pkg/serra/helpers.go +++ b/pkg/serra/helpers.go @@ -41,17 +41,25 @@ func Logger() *log.Logger { return l } -func modifyCardCount(coll *Collection, c *Card, amount int64, foil bool) error { - +func getStoredCard(coll *Collection, c *Card) (Card, error) { // find already existing card sort := bson.D{{"_id", 1}} searchFilter := bson.D{{"_id", c.ID}} - l := Logger() storedCards, err := coll.storageFind(searchFilter, sort, 0, 0) + if err != nil { + return Card{}, err + } + + return storedCards[0], nil +} + +func modifyCardCount(coll *Collection, c *Card, amount int64, foil bool) error { + l := Logger() + + storedCard, err := getStoredCard(coll, c) if err != nil { return err } - storedCard := storedCards[0] // update card amount var update bson.M @@ -87,6 +95,35 @@ func modifyCardCount(coll *Collection, c *Card, amount int64, foil bool) error { return nil } +func modifyCardDeckCount(coll *Collection, c *Card, amount int64, foil bool) error { + l := Logger() + + storedCard, err := getStoredCard(coll, c) + if err != nil { + return err + } + + // update card amount + var update bson.M + if foil { + newAmount := min(max(storedCard.SerraCountFoilDeck + amount, 0), storedCard.SerraCountFoil) + l.Infof("%d / %d available", storedCard.SerraCountFoil - newAmount, storedCard.SerraCountFoil) + update = bson.M{ + "$set": bson.M{"serra_count_foil_deck": newAmount}, + } + } else { + newAmount := min(max(storedCard.SerraCountDeck + amount, 0), storedCard.SerraCount) + l.Infof("%d / %d available", storedCard.SerraCount - newAmount, storedCard.SerraCount) + update = bson.M{ + "$set": bson.M{"serra_count_deck": newAmount}, + } + } + + coll.storageUpdate(bson.M{"_id": bson.M{"$eq": c.ID}}, update) + + return nil +} + func findCardByCollectorNumber(coll *Collection, setCode string, collectorNumber string) (*Card, error) { sort := bson.D{{"_id", 1}} searchFilter := bson.D{{"set", setCode}, {"collectornumber", collectorNumber}} diff --git a/pkg/serra/root.go b/pkg/serra/root.go index 94ebf48..e2ab1ed 100644 --- a/pkg/serra/root.go +++ b/pkg/serra/root.go @@ -23,6 +23,7 @@ var ( interactive bool limit float64 name string + omitInDeck bool oracle string port uint64 rarity string diff --git a/pkg/serra/scryfall.go b/pkg/serra/scryfall.go index 3983a32..28e9cfb 100644 --- a/pkg/serra/scryfall.go +++ b/pkg/serra/scryfall.go @@ -17,13 +17,16 @@ import ( type Card struct { // Added by Serra - SerraCount int64 `bson:"serra_count"` - SerraCountFoil int64 `bson:"serra_count_foil"` - SerraCountEtched int64 `bson:"serra_count_etched"` - SerraPrices []PriceEntry `bson:"serra_prices"` - SerraCreated primitive.DateTime `bson:"serra_created"` - SerraUpdated primitive.DateTime `bson:"serra_updated"` - SerraImage64 string `bson:"serra_image"` + SerraCount int64 `bson:"serra_count"` + SerraCountFoil int64 `bson:"serra_count_foil"` + SerraCountEtched int64 `bson:"serra_count_etched"` + SerraCountDeck int64 `bson:"serra_count_deck"` + SerraCountFoilDeck int64 `bson:"serra_count_foil_deck"` + SerraCountEtchedDeck int64 `bson:"serra_count_etched_deck"` + SerraPrices []PriceEntry `bson:"serra_prices"` + SerraCreated primitive.DateTime `bson:"serra_created"` + SerraUpdated primitive.DateTime `bson:"serra_updated"` + SerraImage64 string `bson:"serra_image"` Artist string `json:"artist"` ArtistIds []string `json:"artist_ids"` diff --git a/pkg/serra/undeck.go b/pkg/serra/undeck.go new file mode 100644 index 0000000..9d739bb --- /dev/null +++ b/pkg/serra/undeck.go @@ -0,0 +1,23 @@ +package serra + +import ( + "github.com/spf13/cobra" +) + +func init() { + undeckCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to add") + undeckCmd.Flags().BoolVarP(&foil, "foil", "f", false, "Add foil variant of card") + rootCmd.AddCommand(undeckCmd) +} + +var undeckCmd = &cobra.Command{ + Aliases: []string{"u"}, + Use: "undeck", + Short: "Unmark a card as in a deck", + Long: "Unmark a card as in a deck", + SilenceErrors: true, + RunE: func(cmd *cobra.Command, cards []string) error { + deckCards(cards, -count) + return nil + }, +}