From 7fcea98e44ae5133bb62000d6a46fc709efb68d7 Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Mon, 7 Feb 2022 14:00:48 +0100 Subject: [PATCH] Add mythics to set and stats fixes #6 --- .task/checksum/build | 1 + .tool-versions | 1 + Taskfile.yaml | 15 +++++++++++++ src/serra/helpers.go | 31 ++++++++++++++++++++++++++ src/serra/root.go | 53 +++++++++----------------------------------- 5 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 .task/checksum/build create mode 100644 .tool-versions create mode 100644 Taskfile.yaml diff --git a/.task/checksum/build b/.task/checksum/build new file mode 100644 index 0000000..8acc7d3 --- /dev/null +++ b/.task/checksum/build @@ -0,0 +1 @@ +91d719dba289c0336b1b419809e8f663 diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..29cc9a0 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.17.6 diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..1597b0b --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,15 @@ +# https://taskfile.dev + +version: '3' + +tasks: + build: + cmds: + - go build -v serra.go + sources: + - "src/serra/**/*.go" + - "serra.go" + generates: + - "./serra" + + diff --git a/src/serra/helpers.go b/src/serra/helpers.go index b8ed865..391cf9a 100644 --- a/src/serra/helpers.go +++ b/src/serra/helpers.go @@ -10,6 +10,10 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +type Rarities struct { + Rares, Uncommons, Commons, Mythics float64 +} + func modify_count_of_card(coll *Collection, c *Card, amount int64) error { // find already existing card @@ -131,3 +135,30 @@ func convert_mana_symbols(sym []interface{}) string { return mana } + +func convert_rarities(rar []primitive.M) Rarities { + + // this is maybe the ugliest way someone could choose to verify, if a rarity type is missing + // [ + // { _id: { rarity: 'common' }, count: 20 }, + // { _id: { rarity: 'uncommon' }, count: 2 } + // ] + // if a result like this is there, 1 rarity type "rare" is not in the array. and needs to be + // initialized with 0, otherwise we get a panic + + var ri Rarities + for _, r := range rar { + switch r["_id"] { + case "rare": + ri.Rares = r["count"].(float64) + case "uncommon": + ri.Uncommons = r["count"].(float64) + case "common": + ri.Commons = r["count"].(float64) + case "mythic": + ri.Mythics = r["count"].(float64) + } + } + return ri + +} diff --git a/src/serra/root.go b/src/serra/root.go index edb0363..d660dc0 100644 --- a/src/serra/root.go +++ b/src/serra/root.go @@ -268,33 +268,17 @@ func ShowSet(setname string) error { }}} rar, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage, sortStage}) - // this is maybe the ugliest way someone could choose to verify, if a rarity type is missing - // [ - // { _id: { rarity: 'common' }, count: 20 }, - // { _id: { rarity: 'uncommon' }, count: 2 } - // ] - // if a result like this is there, 1 rarity type "rare" is not in the array. and needs to be - // initialized with 0, otherwise we get a panic - var rares, uncommons, commons float64 - for _, r := range rar { - switch r["_id"] { - case "rare": - rares = r["count"].(float64) - case "uncommon": - uncommons = r["count"].(float64) - case "common": - commons = r["count"].(float64) - } - } + ri := convert_rarities(rar) 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 EUR", stats[0]["value"]), "normal") LogMessage(fmt.Sprintf("Released: %s", sets[0].ReleasedAt), "normal") - LogMessage(fmt.Sprintf("Rares: %.0f", rares), "normal") - LogMessage(fmt.Sprintf("Uncommons: %.0f", uncommons), "normal") - LogMessage(fmt.Sprintf("Commons: %.0f", commons), "normal") + LogMessage(fmt.Sprintf("Mythics: %.0f", ri.Mythics), "normal") + LogMessage(fmt.Sprintf("Rares: %.0f", ri.Rares), "normal") + LogMessage(fmt.Sprintf("Uncommons: %.0f", ri.Uncommons), "normal") + LogMessage(fmt.Sprintf("Commons: %.0f", ri.Commons), "normal") fmt.Printf("\n%sPrice History:%s\n", Pink, Reset) var before float64 @@ -581,34 +565,17 @@ func Stats() { {"_id", 1}, }}} rar, _ := coll.storage_aggregate(mongo.Pipeline{rarityStage, sortStage}) - - // this is maybe the ugliest way someone could choose to verify, if a rarity type is missing - // [ - // { _id: { rarity: 'common' }, count: 20 }, - // { _id: { rarity: 'uncommon' }, count: 2 } - // ] - // if a result like this is there, 1 rarity type "rare" is not in the array. and needs to be - // initialized with 0, otherwise we get a panic - var rares, uncommons, commons float64 - for _, r := range rar { - switch r["_id"] { - case "rare": - rares = r["count"].(float64) - case "uncommon": - uncommons = r["count"].(float64) - case "common": - commons = r["count"].(float64) - } - } + ri := convert_rarities(rar) fmt.Printf("%sCards %s\n", Green, Reset) fmt.Printf("Total Cards: %s%.0f%s\n", Yellow, stats[0]["count"], Reset) fmt.Printf("Unique Cards: %s%d%s\n", Purple, stats[0]["unique"], Reset) fmt.Printf("\n%sRarity%s\n", Green, Reset) - fmt.Printf("Rares: %s%.0f%s\n", Pink, rares, Reset) - fmt.Printf("Uncommons: %s%.0f%s\n", Yellow, uncommons, Reset) - fmt.Printf("Commons: %s%.0f%s\n", Purple, commons, Reset) + fmt.Printf("Mythics: %s%.0f%s\n", Pink, ri.Mythics, Reset) + fmt.Printf("Rares: %s%.0f%s\n", Pink, ri.Rares, Reset) + fmt.Printf("Uncommons: %s%.0f%s\n", Yellow, ri.Uncommons, Reset) + 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\n", Pink, stats[0]["value"], Reset)