From c6e35c48c99c673d5aa19cc4bd3e4b75ef4e294b Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Mon, 12 Jun 2023 09:13:30 +0200 Subject: [PATCH] Use cmc as manacurve source --- src/serra/helpers.go | 46 -------------------------------------------- src/serra/stats.go | 30 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 61 deletions(-) diff --git a/src/serra/helpers.go b/src/serra/helpers.go index fe5680d..2a3612f 100644 --- a/src/serra/helpers.go +++ b/src/serra/helpers.go @@ -5,7 +5,6 @@ import ( "fmt" "math" "strconv" - "strings" "time" "unicode" @@ -223,48 +222,3 @@ func getFloat64(unknown interface{}) (float64, error) { return math.NaN(), errors.New("Non-numeric type could not be converted to float") } } - -// Splits string by multiple occurances of substring -// needed for calcManaCosts -func SplitAny(s string, seps string) []string { - splitter := func(r rune) bool { - return strings.ContainsRune(seps, r) - } - return strings.FieldsFunc(s, splitter) -} - -// Converts mana encoding to mana costs -// -// calcManaCosts("{2}{B}{B}") -> 4 -// calcManaCosts("{4}{G}{G}{G}{G}") -> 7 -// calcManaCosts("{1}{U} // {3}{U}") -> 2 (ignore transform costs) -func calcManaCosts(costs string) int { - - var count int - - for _, c := range SplitAny(costs, "{}") { - if strings.Contains(c, "//") { - break - } - - i, err := strconv.Atoi(c) - if err != nil { - count = count + 1 - } else { - count = count + i - } - } - - return count -} - -func printUniqueValue(arr []int) map[int]int { - //Create a dictionary of values for each element - dict := make(map[int]int) - for _, num := range arr { - dict[num] = dict[num] + 1 - } - - return dict - -} diff --git a/src/serra/stats.go b/src/serra/stats.go index edf30e7..b0c61c6 100644 --- a/src/serra/stats.go +++ b/src/serra/stats.go @@ -2,7 +2,6 @@ package serra import ( "fmt" - "sort" "github.com/spf13/cobra" "go.mongodb.org/mongo-driver/bson" @@ -129,22 +128,23 @@ var statsCmd = &cobra.Command{ } // Mana Curve of Collection - cards := Cards(rarity, set, sortby, name, oracle, cardType, false, foil) - var numCosts []int - for _, card := range cards { - numCosts = append(numCosts, calcManaCosts(card.ManaCost)) - } - dist := printUniqueValue(numCosts) + // Rarities + cmc, _ := coll.storageAggregate(mongo.Pipeline{ + bson.D{ + {"$group", bson.D{ + {"_id", "$cmc"}, + // {"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}}, + {"count", bson.D{{"$sum", 1}}}, + }}}, + bson.D{ + {"$sort", bson.D{ + {"_id", 1}, + }}}, + }) fmt.Printf("\n%sMana Curve%s\n", Green, Reset) - - keys := make([]int, 0, len(dist)) - for k := range dist { - keys = append(keys, k) - } - sort.Ints(keys) - for _, k := range keys { - fmt.Printf("%d: %s%d%s\n", k, Purple, dist[k], Reset) + for _, mc := range cmc { + fmt.Printf("%d: %s%d%s\n", mc["_id"], Purple, mc["count"], Reset) } // Total Value