From d1fd624ba8ddc967b5ff21ad1df7e641da442e62 Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Tue, 9 May 2023 11:26:12 +0200 Subject: [PATCH] Add mana curve into statistics --- src/serra/helpers.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/serra/stats.go | 20 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/serra/helpers.go b/src/serra/helpers.go index 1222c8b..74eea62 100644 --- a/src/serra/helpers.go +++ b/src/serra/helpers.go @@ -5,6 +5,7 @@ import ( "fmt" "math" "strconv" + "strings" "time" "unicode" @@ -225,3 +226,48 @@ 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 a618965..40c3038 100644 --- a/src/serra/stats.go +++ b/src/serra/stats.go @@ -2,6 +2,7 @@ package serra import ( "fmt" + "sort" "github.com/spf13/cobra" "go.mongodb.org/mongo-driver/bson" @@ -127,6 +128,25 @@ var statsCmd = &cobra.Command{ fmt.Printf("%s: %s%d%s\n", artist["_id"].(string), Purple, artist["count"], Reset) } + // 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) + 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) + } + // Total Value fmt.Printf("\n%sTotal Value%s\n", Green, Reset) nf_value, err := getFloat64(stats[0]["value"])