-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,531 additions
and
679 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,8 @@ | ||
// Package main provides a typing test | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
import "github.com/noqqe/serra/src/serra" | ||
|
||
"github.com/docopt/docopt-go" | ||
"github.com/noqqe/serra/src/serra" | ||
) | ||
|
||
var opts struct { | ||
Add bool `docopt:"add"` | ||
Remove bool `docopt:"remove"` | ||
Cards bool `docopt:"cards"` | ||
Card bool `docopt:"card"` | ||
Set bool `docopt:"set"` | ||
Sets bool `docopt:"sets"` | ||
Stats bool `docopt:"stats"` | ||
Missing bool `docopt:"missing"` | ||
Tops bool `docopt:"tops"` | ||
Flops bool `docopt:"flops"` | ||
Update bool `docopt:"update"` | ||
CardId []string `docopt:"<cardid>"` | ||
SetCode string `docopt:"<setcode>,--set"` | ||
Count int64 `docopt:"--count"` | ||
Sort string `docopt:"--sort"` | ||
Rarity string `docopt:"--rarity"` | ||
Limit float64 `docopt:"--limit"` | ||
} | ||
|
||
// Main Loop | ||
func main() { | ||
|
||
usage := `Serra | ||
Usage: | ||
serra add <cardid>... [--count=<number>] | ||
serra remove <cardid>... | ||
serra cards [--rarity=<rarity>] [--set=<setcode>] [--sort=<sort>] | ||
serra card <cardid>... | ||
serra tops [--limit=<limit>] | ||
serra flops [--limit=<limit>] | ||
serra missing <setcode> | ||
serra set <setcode> | ||
serra sets | ||
serra update | ||
serra stats | ||
Options: | ||
-h --help Show this screen. | ||
--count=<number> Count of card to add. [default: 1]. | ||
--version Show version. | ||
` | ||
|
||
args, _ := docopt.ParseDoc(usage) | ||
err := args.Bind(&opts) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
serra.Banner() | ||
if opts.Add { | ||
serra.Add(opts.CardId, opts.Count) | ||
} else if opts.Remove { | ||
serra.Remove(opts.CardId) | ||
} else if opts.Cards { | ||
serra.Cards(opts.Rarity, opts.SetCode, opts.Sort) | ||
} else if opts.Card { | ||
serra.ShowCard(opts.CardId) | ||
} else if opts.Sets { | ||
serra.Sets() | ||
} else if opts.Missing { | ||
serra.Missing(opts.SetCode) | ||
} else if opts.Tops { | ||
serra.Gains(opts.Limit, -1) | ||
} else if opts.Flops { | ||
serra.Gains(opts.Limit, 1) | ||
} else if opts.Set { | ||
serra.ShowSet(opts.SetCode) | ||
} else if opts.Update { | ||
serra.Update() | ||
} else if opts.Stats { | ||
serra.Stats() | ||
} | ||
|
||
serra.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package serra | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func init() { | ||
addCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to add") | ||
rootCmd.AddCommand(addCmd) | ||
} | ||
|
||
var addCmd = &cobra.Command{ | ||
Aliases: []string{"a"}, | ||
Use: "add", | ||
Short: "Add a card to your collection", | ||
Long: "Adds a card from scryfall to your collection. Amount can be modified using flags", | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, cards []string) error { | ||
|
||
client := storage_connect() | ||
coll := &Collection{client.Database("serra").Collection("cards")} | ||
defer storage_disconnect(client) | ||
|
||
// Loop over different cards | ||
for _, card := range cards { | ||
// Fetch card from scryfall | ||
c, err := fetch_card(card) | ||
if err != nil { | ||
LogMessage(fmt.Sprintf("%v", err), "red") | ||
continue | ||
} | ||
|
||
// Write card to mongodb | ||
c.SerraCount = count | ||
err = coll.storage_add(c) | ||
|
||
// If duplicate key, increase count of card | ||
if mongo.IsDuplicateKeyError(err) { | ||
modify_count_of_card(coll, c, count) | ||
continue | ||
} | ||
|
||
// If error, print error and continue | ||
if err != nil { | ||
LogMessage(fmt.Sprintf("%v", err), "red") | ||
continue | ||
} | ||
|
||
// Give feedback of successfully added card | ||
LogMessage(fmt.Sprintf("%dx \"%s\" (%.2f Eur) added to Collection.", c.SerraCount, c.Name, c.Prices.Eur), "green") | ||
} | ||
storage_disconnect(client) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package serra | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func init() { | ||
cardCmd.Flags().StringVarP(&rarity, "rarity", "r", "", "Filter by rarity of cards (mythic, rare, uncommon, common)") | ||
cardCmd.Flags().StringVarP(&set, "set", "e", "", "Filter by set code (usg/mmq/vow)") | ||
cardCmd.Flags().StringVarP(&sort, "sort", "s", "name", "How to sort cards (value/number/name)") | ||
rootCmd.AddCommand(cardCmd) | ||
} | ||
|
||
var cardCmd = &cobra.Command{ | ||
Aliases: []string{"cards"}, | ||
Use: "card [card]", | ||
Short: "Search & show cards from your collection", | ||
Long: `Search and show cards from your collection. | ||
If you directly put a card as an argument, it will be displayed | ||
otherwise you'll get a list of cards as a search result.`, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, cards []string) error { | ||
if len(cards) == 0 { | ||
Cards(rarity, set, sort) | ||
} else { | ||
ShowCard(cards) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func ShowCard(cardids []string) { | ||
|
||
client := storage_connect() | ||
coll := &Collection{client.Database("serra").Collection("cards")} | ||
defer storage_disconnect(client) | ||
|
||
for _, v := range cardids { | ||
|
||
cards, _ := coll.storage_find(bson.D{{"set", strings.Split(v, "/")[0]}, {"collectornumber", strings.Split(v, "/")[1]}}, bson.D{{"name", 1}}) | ||
|
||
for _, card := range cards { | ||
show_card_details(&card) | ||
} | ||
} | ||
} | ||
|
||
func Cards(rarity, set, sort string) { | ||
|
||
var total float64 | ||
client := storage_connect() | ||
coll := &Collection{client.Database("serra").Collection("cards")} | ||
defer storage_disconnect(client) | ||
|
||
filter := bson.D{} | ||
|
||
switch rarity { | ||
case "uncommon": | ||
filter = append(filter, bson.E{"rarity", "uncommon"}) | ||
case "common": | ||
filter = append(filter, bson.E{"rarity", "common"}) | ||
case "rare": | ||
filter = append(filter, bson.E{"rarity", "rare"}) | ||
} | ||
|
||
var sortStage bson.D | ||
switch sort { | ||
case "value": | ||
sortStage = bson.D{{"prices.eur", 1}} | ||
case "number": | ||
sortStage = bson.D{{"collectornumber", 1}} | ||
case "name": | ||
sortStage = bson.D{{"name", 1}} | ||
default: | ||
sortStage = bson.D{{"name", 1}} | ||
} | ||
|
||
if len(set) > 0 { | ||
filter = append(filter, bson.E{"set", set}) | ||
} | ||
|
||
cards, _ := coll.storage_find(filter, sortStage) | ||
|
||
for _, card := range cards { | ||
LogMessage(fmt.Sprintf("* %dx %s%s%s (%s/%s) %s%.2f EUR%s", card.SerraCount, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.Prices.Eur, Reset), "normal") | ||
total = total + card.Prices.Eur*float64(card.SerraCount) | ||
} | ||
fmt.Printf("\nTotal Value: %s%.2f EUR%s\n", Yellow, total, Reset) | ||
|
||
} |
Oops, something went wrong.