add check command

This commit is contained in:
Florian Baumann 2023-06-27 16:17:14 +02:00
parent 5eac08c40b
commit 4580748ebe
3 changed files with 73 additions and 0 deletions

BIN
imgs/check.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -69,6 +69,7 @@ Usage:
Available Commands:
add Add a card to your collection
card Search & show cards from your collection
check Check if a card is in your collection
completion Generate the autocompletion script for the specified shell
flops What cards lost most value
help Help about any command
@ -137,6 +138,12 @@ update. After all Sets are updated, the whole collection value is updated.
![](https://github.com/noqqe/serra/blob/main/imgs/update.png)
## Check
To add a card to your collection.
![](https://github.com/noqqe/serra/blob/main/imgs/check.png)
## Adding all those cards, manually?
Yes. While there are serveral OCR/Photo Scanners for mtg cards, I found they

66
src/serra/check.go Normal file
View File

@ -0,0 +1,66 @@
package serra
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/bson"
)
func init() {
checkCmd.Flags().StringVarP(&set, "set", "s", "", "Filter by set code (usg/mmq/vow)")
checkCmd.Flags().BoolVarP(&detail, "detail", "d", false, "Show details for cards (url)")
rootCmd.AddCommand(checkCmd)
}
var checkCmd = &cobra.Command{
Aliases: []string{"c"},
Use: "check",
Short: "Check if a card is in your collection",
Long: "Check if a card is in your collection. Useful for list comparsions",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, cards []string) error {
checkCards(cards, detail)
return nil
},
}
func checkCards(cards []string, detail bool) error {
client := storageConnect()
coll := &Collection{client.Database("serra").Collection("cards")}
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
collectorNumber := strings.TrimLeft(strings.Split(card, "/")[1], "0")
setName := strings.Split(card, "/")[0]
// Check if card is already in collection
co, err := coll.storageFind(bson.D{{"set", setName}, {"collectornumber", collectorNumber}}, bson.D{}, 0, 0)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
}
// If Card is in collection, print yes.
if len(co) >= 1 {
c := co[0]
LogMessage(fmt.Sprintf("Yes - %s \"%s\" (%s, %.2f%s) is in your Collection", card, c.Name, c.Rarity, c.getValue(foil), getCurrency()), "green")
continue
} else {
if detail {
// fetch card from scyrfall if --detail was given
c, _ := fetchCard(setName, collectorNumber)
LogMessage(fmt.Sprintf("No - %s \"%s\" (%s, %.2f%s) is not in your Collection.", card, c.Name, c.Rarity, c.getValue(foil), getCurrency()), "red")
} else {
// Just print, the card name was not found
LogMessage(fmt.Sprintf("No \"%s\" is not in your Collection.", card), "red")
}
}
}
storageDisconnect(client)
return nil
}