From 03a15d998ee69a5200110cb0bdb656d64de4790b Mon Sep 17 00:00:00 2001 From: Corentin Barreau Date: Mon, 24 Apr 2023 15:54:26 +0200 Subject: [PATCH] Show the card's ID when printing missing cards --- src/serra/add.go | 3 +-- src/serra/card.go | 3 --- src/serra/missing.go | 22 +++++++++++++++++++--- src/serra/scryfall.go | 27 +++++++++++++++------------ src/serra/update.go | 2 +- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/serra/add.go b/src/serra/add.go index a9cc084..61ea69f 100644 --- a/src/serra/add.go +++ b/src/serra/add.go @@ -112,9 +112,8 @@ func addCards(cards []string, unique bool, count int64) error { // If card is not already in collection, fetching from scyfall } else { - // Fetch card from scryfall - c, err := fetch_card(card) + c, err := fetchCard(card) if err != nil { LogMessage(fmt.Sprintf("%v", err), "red") continue diff --git a/src/serra/card.go b/src/serra/card.go index 52c1420..a6c593b 100644 --- a/src/serra/card.go +++ b/src/serra/card.go @@ -40,13 +40,11 @@ otherwise you'll get a list of cards as a search result.`, } func ShowCard(cardids []string) { - client := storage_connect() coll := &Collection{client.Database("serra").Collection("cards")} defer storage_disconnect(client) for _, v := range cardids { - if len(strings.Split(v, "/")) < 2 || strings.Split(v, "/")[1] == "" { LogMessage(fmt.Sprintf("Invalid card %s", v), "red") continue @@ -61,7 +59,6 @@ func ShowCard(cardids []string) { } func Cards(rarity, set, sortby, name, oracle, cardType string) []Card { - client := storage_connect() coll := &Collection{client.Database("serra").Collection("cards")} defer storage_disconnect(client) diff --git a/src/serra/missing.go b/src/serra/missing.go index 1dd54ec..7f059fa 100644 --- a/src/serra/missing.go +++ b/src/serra/missing.go @@ -2,6 +2,7 @@ package serra import ( "fmt" + "sort" "strconv" "github.com/spf13/cobra" @@ -20,7 +21,6 @@ var missingCmd = &cobra.Command{ cards you dont own (yet) :)`, SilenceErrors: true, RunE: func(cmd *cobra.Command, setname []string) error { - client := storage_connect() coll := &Collection{client.Database("serra").Collection("cards")} defer storage_disconnect(client) @@ -53,13 +53,29 @@ cards you dont own (yet) :)`, } misses := missing(in_collection, complete_set) + + // Fetch all missing cards + missingCards := []*Card{} for _, m := range misses { - ncard, err := fetch_card(fmt.Sprintf("%s/%s", setname[0], m)) + card, err := fetchCard(fmt.Sprintf("%s/%s", setname[0], m)) if err != nil { continue } - fmt.Printf("%.02f %s\t%s (%s)\n", ncard.getValue(false), getCurrency(), ncard.Name, ncard.SetName) + + missingCards = append(missingCards, card) } + + // Sort the missing cards by ID + sort.Slice(missingCards, func(i, j int) bool { + id1, _ := strconv.Atoi(missingCards[i].CollectorNumber) + id2, _ := strconv.Atoi(missingCards[j].CollectorNumber) + return id1 < id2 + }) + + for _, card := range missingCards { + fmt.Printf("[%s] %.02f %s\t%s (%s)\n", card.CollectorNumber, card.getValue(false), getCurrency(), card.Name, card.SetName) + } + return nil }, } diff --git a/src/serra/scryfall.go b/src/serra/scryfall.go index 48758c6..4140aa5 100644 --- a/src/serra/scryfall.go +++ b/src/serra/scryfall.go @@ -3,7 +3,6 @@ package serra import ( "bytes" "encoding/json" - "errors" "fmt" "io/ioutil" "log" @@ -28,7 +27,7 @@ type Card struct { Booster bool `json:"booster"` BorderColor string `json:"border_color"` CardBackID string `json:"card_back_id"` - Cmc int64 `json:"cmc"` + Cmc float64 `json:"cmc"` CollectorNumber string `json:"collector_number"` ColorIdentity []string `json:"color_identity"` Colors []string `json:"colors"` @@ -170,11 +169,9 @@ type Set struct { URI string `json:"uri"` } -func fetch_card(path string) (*Card, error) { - +func fetchCard(path string) (*Card, error) { if !strings.Contains(path, "/") { - err := errors.New(fmt.Sprintf("Card must follow format /, for example: ath/15")) - return &Card{}, err + return &Card{}, fmt.Errorf("Card must follow format /, for example: ath/15") } // TODO better URL Building... @@ -185,21 +182,24 @@ func fetch_card(path string) (*Card, error) { } if resp.StatusCode != 200 { - err := errors.New(fmt.Sprintf("Error: %s not found", path)) - return &Card{}, err + return &Card{}, fmt.Errorf("Card %s not found", path) } //We Read the response body on the line below. body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Fatalln(err) + log.Fatalf("%s", err) return &Card{}, err } r := bytes.NewReader(body) decoder := json.NewDecoder(r) val := &Card{} + err = decoder.Decode(val) + if err != nil { + log.Fatalf("%s", err) + } // Set created Time val.SerraCreated = primitive.NewDateTimeFromTime(time.Now()) @@ -213,15 +213,14 @@ func fetch_card(path string) (*Card, error) { func fetch_sets() (*SetList, error) { // TODO better URL Building... - resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets")) + resp, err := http.Get("https://api.scryfall.com/sets") if err != nil { log.Fatalln(err) return &SetList{}, err } if resp.StatusCode != 200 { - err := errors.New(fmt.Sprintf("Error: /sets not found")) - return &SetList{}, err + return &SetList{}, fmt.Errorf("/sets not found") } //We Read the response body on the line below. @@ -234,7 +233,11 @@ func fetch_sets() (*SetList, error) { r := bytes.NewReader(body) decoder := json.NewDecoder(r) val := &SetList{} + err = decoder.Decode(val) + if err != nil { + log.Fatalln(err) + } return val, nil } diff --git a/src/serra/update.go b/src/serra/update.go index 6c04726..cfda762 100644 --- a/src/serra/update.go +++ b/src/serra/update.go @@ -79,7 +79,7 @@ var updateCmd = &cobra.Command{ ) for _, card := range cards { bar.Add(1) - updated_card, err := fetch_card(fmt.Sprintf("%s/%s", card.Set, card.CollectorNumber)) + updated_card, err := fetchCard(fmt.Sprintf("%s/%s", card.Set, card.CollectorNumber)) if err != nil { LogMessage(fmt.Sprintf("%v", err), "red") continue