Add mana curve into statistics

This commit is contained in:
Florian Baumann 2023-05-09 11:26:12 +02:00
parent 71f451bbd1
commit d1fd624ba8
2 changed files with 66 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"math" "math"
"strconv" "strconv"
"strings"
"time" "time"
"unicode" "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") 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
}

View File

@ -2,6 +2,7 @@ package serra
import ( import (
"fmt" "fmt"
"sort"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/bson" "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) 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 // Total Value
fmt.Printf("\n%sTotal Value%s\n", Green, Reset) fmt.Printf("\n%sTotal Value%s\n", Green, Reset)
nf_value, err := getFloat64(stats[0]["value"]) nf_value, err := getFloat64(stats[0]["value"])