From 3e2efa7660aec32b527164c0de099f7b5909cdbe Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Tue, 11 Jan 2022 08:40:54 +0100 Subject: [PATCH] Handle rarity missing for ShowSet --- src/serra/root.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/serra/root.go b/src/serra/root.go index 7a5c9e5..60281bc 100644 --- a/src/serra/root.go +++ b/src/serra/root.go @@ -261,14 +261,33 @@ 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) + } + } + 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", rar[1]["count"]), "normal") - LogMessage(fmt.Sprintf("Uncommons: %.0f", rar[2]["count"]), "normal") - LogMessage(fmt.Sprintf("Commons: %.0f", rar[0]["count"]), "normal") + LogMessage(fmt.Sprintf("Rares: %.0f", rares), "normal") + LogMessage(fmt.Sprintf("Uncommons: %.0f", uncommons), "normal") + LogMessage(fmt.Sprintf("Commons: %.0f", commons), "normal") fmt.Printf("\n%sPrice History:%s\n", Pink, Reset) for _, e := range sets[0].SerraPrices { fmt.Printf("* %s %.2f EUR\n", stringToTime(e.Date), e.Value)