From de868f58b1ff742274d705fcc3aabd92419aed67 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Sat, 18 Mar 2023 21:51:11 -0400 Subject: [PATCH] working version of adding/removing foils and adding to collection value --- src/serra/add.go | 25 ++++++++++++++++++----- src/serra/card.go | 7 ++++--- src/serra/helpers.go | 46 +++++++++++++++++++++++++++++++++++++++---- src/serra/missing.go | 2 +- src/serra/remove.go | 19 ++++++++++++++---- src/serra/root.go | 1 + src/serra/scryfall.go | 8 +++++++- src/serra/set.go | 22 +++++++++++++++++---- src/serra/stats.go | 16 +++++++++++++-- src/serra/storage.go | 11 ++++++++++- src/serra/update.go | 2 +- 11 files changed, 133 insertions(+), 26 deletions(-) diff --git a/src/serra/add.go b/src/serra/add.go index a722cee..2b3cd0c 100644 --- a/src/serra/add.go +++ b/src/serra/add.go @@ -15,6 +15,7 @@ func init() { addCmd.Flags().BoolVarP(&unique, "unique", "u", false, "Only add card if not existent yet") addCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Spin up interactive terminal") addCmd.Flags().StringVarP(&set, "set", "s", "", "Filter by set code (usg/mmq/vow)") + addCmd.Flags().BoolVarP(&foil, "foil", "f", false, "Add foil variant of card") rootCmd.AddCommand(addCmd) } @@ -78,13 +79,20 @@ func addCards(cards []string, unique bool, count int64) error { c := co[0] if unique { - LogMessage(fmt.Sprintf("Not adding \"%s\" (%s, %.2f %s) to Collection because it already exists.", c.Name, c.Rarity, c.getValue(), getCurrency()), "red") + LogMessage(fmt.Sprintf("Not adding \"%s\" (%s, %.2f %s) to Collection because it already exists.", c.Name, c.Rarity, c.getValue(foil), getCurrency()), "red") continue } - modify_count_of_card(coll, &c, count) + modify_count_of_card(coll, &c, count, foil) + + var total int64 = 0 + if foil { + total = c.SerraCountFoil + count + } else { + total = c.SerraCount + count + } // Give feedback of successfully added card - LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f %s) added to Collection.", c.SerraCount, c.Name, c.Rarity, c.getValue(), getCurrency()), "green") + LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f %s) added to Collection.", total, c.Name, c.Rarity, c.getValue(foil), getCurrency()), "green") // If card is not already in collection, fetching from scyfall } else { @@ -97,7 +105,14 @@ func addCards(cards []string, unique bool, count int64) error { } // Write card to mongodb - c.SerraCount = count + var total int64 = 0 + if foil { + c.SerraCountFoil = count + total = c.SerraCountFoil + } else { + c.SerraCount = count + total = c.SerraCount + } err = coll.storage_add(c) if err != nil { LogMessage(fmt.Sprintf("%v", err), "red") @@ -105,7 +120,7 @@ func addCards(cards []string, unique bool, count int64) error { } // Give feedback of successfully added card - LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f %s) added to Collection.", c.SerraCount, c.Name, c.Rarity, c.getValue(), getCurrency()), "green") + LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f %s) added to Collection.", total, c.Name, c.Rarity, c.getValue(foil), getCurrency()), "green") } } storage_disconnect(client) diff --git a/src/serra/card.go b/src/serra/card.go index 47f26d3..f473651 100644 --- a/src/serra/card.go +++ b/src/serra/card.go @@ -118,8 +118,9 @@ func show_card_list(cards []Card) { var total float64 for _, card := range cards { - LogMessage(fmt.Sprintf("* %dx %s%s%s (%s/%s) %s%.2f %s%s", card.SerraCount, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(), getCurrency(), Reset), "normal") - total = total + card.getValue()*float64(card.SerraCount) + LogMessage(fmt.Sprintf("* %dx Non-Foil %s%s%s (%s/%s) %s%.2f %s%s", card.SerraCount, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(false), getCurrency(), Reset), "normal") + LogMessage(fmt.Sprintf("* %dx Foil %s%s%s (%s/%s) %s%.2f %s%s", card.SerraCountFoil, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(true), getCurrency(), Reset), "normal") + total = total + card.getValue(false)*float64(card.SerraCount) + card.getValue(true)*float64(card.SerraCountFoil) } fmt.Printf("\nTotal Value: %s%.2f %s%s\n", Yellow, total, getCurrency(), Reset) @@ -131,7 +132,7 @@ func show_card_details(card *Card) error { fmt.Printf("Count: %dx\n", card.SerraCount) fmt.Printf("Rarity: %s\n", card.Rarity) fmt.Printf("Scryfall: %s\n", strings.Replace(card.ScryfallURI, "?utm_source=api", "", 1)) - fmt.Printf("Current Value: %s%.2f %s%s\n", Yellow, card.getValue(), getCurrency(), Reset) + fmt.Printf("Current Value: Non-Foil %s%.2f %s%s Foil %s%.2f %s%s\n", Yellow, card.getValue(false), getCurrency(), Reset, Yellow, card.getValue(true), getCurrency(), Reset) fmt.Printf("\n%sHistory%s\n", Green, Reset) print_price_history(card.SerraPrices, "* ") diff --git a/src/serra/helpers.go b/src/serra/helpers.go index 835a3c6..4dac9eb 100644 --- a/src/serra/helpers.go +++ b/src/serra/helpers.go @@ -3,6 +3,7 @@ package serra import ( "errors" "fmt" + "math" "time" "go.mongodb.org/mongo-driver/bson" @@ -13,7 +14,7 @@ type Rarities struct { Rares, Uncommons, Commons, Mythics float64 } -func modify_count_of_card(coll *Collection, c *Card, amount int64) error { +func modify_count_of_card(coll *Collection, c *Card, amount int64, foil bool) error { // find already existing card sort := bson.D{{"_id", 1}} @@ -26,12 +27,26 @@ func modify_count_of_card(coll *Collection, c *Card, amount int64) error { // update card amount update_filter := bson.M{"_id": bson.M{"$eq": c.ID}} - update := bson.M{ - "$set": bson.M{"serra_count": stored_card.SerraCount + amount}, + var update bson.M + if foil { + update = bson.M{ + "$set": bson.M{"serra_count_foil": stored_card.SerraCountFoil + amount}, + } + } else { + update = bson.M{ + "$set": bson.M{"serra_count": stored_card.SerraCount + amount}, + } } + coll.storage_update(update_filter, update) - LogMessage(fmt.Sprintf("Updating Card \"%s\" amount to %d", stored_card.Name, stored_card.SerraCount+amount), "purple") + var total int64 + if foil { + total = stored_card.SerraCountFoil + amount + } else { + total = stored_card.SerraCount + amount + } + LogMessage(fmt.Sprintf("Updating Card \"%s\" amount to %d", stored_card.Name, total), "purple") return nil } @@ -167,3 +182,26 @@ func print_price_history(prices []PriceEntry, prefix string) { before = value } } + +func getFloat64(unknown interface{}) (float64, error) { + switch i := unknown.(type) { + case float64: + return i, nil + case float32: + return float64(i), nil + case int64: + return float64(i), nil + case int32: + return float64(i), nil + case int: + return float64(i), nil + case uint64: + return float64(i), nil + case uint32: + return float64(i), nil + case uint: + return float64(i), nil + default: + return math.NaN(), errors.New("Non-numeric type could not be converted to float") + } +} diff --git a/src/serra/missing.go b/src/serra/missing.go index 7799a70..1dd54ec 100644 --- a/src/serra/missing.go +++ b/src/serra/missing.go @@ -58,7 +58,7 @@ cards you dont own (yet) :)`, if err != nil { continue } - fmt.Printf("%.02f %s\t%s (%s)\n", ncard.getValue(), getCurrency(), ncard.Name, ncard.SetName) + fmt.Printf("%.02f %s\t%s (%s)\n", ncard.getValue(false), getCurrency(), ncard.Name, ncard.SetName) } return nil }, diff --git a/src/serra/remove.go b/src/serra/remove.go index b208538..128a17c 100644 --- a/src/serra/remove.go +++ b/src/serra/remove.go @@ -14,6 +14,7 @@ func init() { removeCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to remove") removeCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Spin up interactive terminal") removeCmd.Flags().StringVarP(&set, "set", "s", "", "Filter by set code (usg/mmq/vow)") + removeCmd.Flags().BoolVarP(&foil, "foil", "f", false, "Remove foil variant of card") rootCmd.AddCommand(removeCmd) } @@ -77,11 +78,21 @@ func removeCards(cards []string, count int64) error { continue } - if c.SerraCount > 1 { - modify_count_of_card(coll, c, -1) - } else { + if foil && c.SerraCountFoil < 1 { + LogMessage(fmt.Sprintf("Error: No Foil \"%s\" in the Collection.", c.Name), "red") + continue + } + + if !foil && c.SerraCount < 1 { + LogMessage(fmt.Sprintf("Error: No Non-Foil \"%s\" in the Collection.", c.Name), "red") + continue + } + + if foil && c.SerraCountFoil == 1 && c.SerraCount == 0 || !foil && c.SerraCount == 1 && c.SerraCountFoil == 0 { coll.storage_remove(bson.M{"_id": c.ID}) - LogMessage(fmt.Sprintf("\"%s\" (%.2f %s) removed from the Collection.", c.Name, c.getValue(), getCurrency()), "green") + LogMessage(fmt.Sprintf("\"%s\" (%.2f %s) removed from the Collection.", c.Name, c.getValue(foil), getCurrency()), "green") + } else { + modify_count_of_card(coll, c, -1, foil) } } diff --git a/src/serra/root.go b/src/serra/root.go index d3ab100..859caf4 100644 --- a/src/serra/root.go +++ b/src/serra/root.go @@ -20,6 +20,7 @@ var since string var sortby string var cardType string var unique bool +var foil bool var rootCmd = &cobra.Command{ Version: Version, diff --git a/src/serra/scryfall.go b/src/serra/scryfall.go index 80beb27..48758c6 100644 --- a/src/serra/scryfall.go +++ b/src/serra/scryfall.go @@ -120,10 +120,16 @@ type Card struct { } // Getter for currency specific value -func (c Card) getValue() float64 { +func (c Card) getValue(foil bool) float64 { if getCurrency() == "EUR" { + if foil { + return c.Prices.EurFoil + } return c.Prices.Eur } + if foil { + return c.Prices.UsdFoil + } return c.Prices.Usd } diff --git a/src/serra/set.go b/src/serra/set.go index ec8e0b9..50e889b 100644 --- a/src/serra/set.go +++ b/src/serra/set.go @@ -42,7 +42,8 @@ func Sets(sort string) []primitive.M { groupStage := bson.D{ {"$group", bson.D{ {"_id", "$setname"}, - {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(), "$serra_count"}}}}}}, + {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(false), "$serra_count"}}}}}}, + {"value_foil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(true), "$serra_count_foil"}}}}}}, {"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}}, {"unique", bson.D{{"$sum", 1}}}, {"code", bson.D{{"$last", "$set"}}}, @@ -113,7 +114,8 @@ func ShowSet(setname string) error { groupStage := bson.D{ {"$group", bson.D{ {"_id", "$setname"}, - {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(), "$serra_count"}}}}}}, + {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(false), "$serra_count"}}}}}}, + {"value_foil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(true), "$serra_count_foil"}}}}}}, {"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}}, }}, } @@ -142,7 +144,18 @@ func ShowSet(setname string) error { LogMessage(fmt.Sprintf("%s", sets[0].Name), "green") LogMessage(fmt.Sprintf("Set Cards: %d/%d", len(cards), sets[0].CardCount), "normal") LogMessage(fmt.Sprintf("Total Cards: %.0f", stats[0]["count"]), "normal") - LogMessage(fmt.Sprintf("Total Value: %.2f %s", stats[0]["value"], getCurrency()), "normal") + nf_value, err := getFloat64(stats[0]["value"]) + if err != nil { + LogMessage(fmt.Sprintf("Error: %v", err), "red") + nf_value = 0 + } + foil_value, err := getFloat64(stats[0]["value_foil"]) + if err != nil { + LogMessage(fmt.Sprintf("Error: %v", err), "red") + foil_value = 0 + } + total_value := nf_value + foil_value + LogMessage(fmt.Sprintf("Total Value: %.2f %s", total_value, getCurrency()), "normal") LogMessage(fmt.Sprintf("Released: %s", sets[0].ReleasedAt), "normal") LogMessage(fmt.Sprintf("Mythics: %.0f", ri.Mythics), "normal") LogMessage(fmt.Sprintf("Rares: %.0f", ri.Rares), "normal") @@ -163,7 +176,8 @@ func ShowSet(setname string) error { for i := 0; i < ccards; i++ { card := cards[i] - fmt.Printf("* %dx %s%s%s (%s/%s) %s%.2f %s%s\n", card.SerraCount, Purple, card.Name, Reset, sets[0].Code, card.CollectorNumber, Yellow, card.getValue(), getCurrency(), Reset) + fmt.Printf("* %dx %s%s%s (%s/%s) %s%.2f %s%s\n", card.SerraCount, Purple, card.Name, Reset, sets[0].Code, card.CollectorNumber, Yellow, card.getValue(false), getCurrency(), Reset) + fmt.Printf("* %dx %s%s%s (%s/%s) %s%.2f %s%s\n", card.SerraCountFoil, Purple, card.Name, Reset, sets[0].Code, card.CollectorNumber, Yellow, card.getValue(true), getCurrency(), Reset) } return nil diff --git a/src/serra/stats.go b/src/serra/stats.go index b744b50..d29e517 100644 --- a/src/serra/stats.go +++ b/src/serra/stats.go @@ -50,7 +50,8 @@ var statsCmd = &cobra.Command{ bson.D{ {"$group", bson.D{ {"_id", nil}, - {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(), "$serra_count"}}}}}}, + {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(false), "$serra_count"}}}}}}, + {"value_foil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(true), "$serra_count_foil"}}}}}}, {"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}}, {"count_foil", bson.D{{"$sum", "$serra_count_foil"}}}, {"count_etched", bson.D{{"$sum", "$serra_count_etched"}}}, @@ -96,7 +97,18 @@ var statsCmd = &cobra.Command{ fmt.Printf("Commons: %s%.0f%s\n", Purple, ri.Commons, Reset) fmt.Printf("\n%sTotal Value%s\n", Green, Reset) - fmt.Printf("Current: %s%.2f %s%s\n", Pink, stats[0]["value"], getCurrency(), Reset) + nf_value, err := getFloat64(stats[0]["value"]) + if err != nil { + LogMessage(fmt.Sprintf("Error: %v", err), "red") + nf_value = 0 + } + foil_value, err := getFloat64(stats[0]["value_foil"]) + if err != nil { + LogMessage(fmt.Sprintf("Error: %v", err), "red") + foil_value = 0 + } + total_value := nf_value + foil_value + fmt.Printf("Current: %s%.2f %s%s\n", Pink, total_value, getCurrency(), Reset) total, _ := totalcoll.storage_find_total() fmt.Printf("History: \n") diff --git a/src/serra/storage.go b/src/serra/storage.go index 259694a..9abfbc4 100644 --- a/src/serra/storage.go +++ b/src/serra/storage.go @@ -25,13 +25,22 @@ type Collection struct { // Returns configured human readable name for // the configured currency of the user -func getCurrencyField() string { +func getCurrencyField(foil bool) string { switch os.Getenv("SERRA_CURRENCY") { case "EUR": + if foil { + return "$prices.eur_foil" + } return "$prices.eur" case "USD": + if foil { + return "$prices.usd_foil" + } return "$prices.usd" default: + if foil { + return "$prices.usd_foil" + } return "$prices.usd" } } diff --git a/src/serra/update.go b/src/serra/update.go index fb887ca..09dfa36 100644 --- a/src/serra/update.go +++ b/src/serra/update.go @@ -129,7 +129,7 @@ var updateCmd = &cobra.Command{ tmpCard := Card{} tmpCard.Prices = t - fmt.Printf("\n%sUpdating total value of collection to: %s%.02f %s%s\n", Green, Yellow, tmpCard.getValue(), getCurrency(), Reset) + fmt.Printf("\n%sUpdating total value of collection to: %s%.02f %s%s\n", Green, Yellow, tmpCard.getValue(false), getCurrency(), Reset) totalcoll.storage_add_total(t) return nil