Improve performance, fix issues with confirmation, allow specifying multiple phrases for oracle text
This commit is contained in:
parent
cea9a47de4
commit
1ffd466dfc
@ -156,7 +156,7 @@ func addCards(cards []string, unique bool, count int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write card to mongodb
|
// Write card to mongodb
|
||||||
var total int64 = 0
|
var total int64
|
||||||
if foil {
|
if foil {
|
||||||
c.SerraCountFoil = count
|
c.SerraCountFoil = count
|
||||||
c.SerraCountFoilDeck = 0
|
c.SerraCountFoilDeck = 0
|
||||||
|
|||||||
@ -23,7 +23,7 @@ func init() {
|
|||||||
cardCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the card (regex compatible)")
|
cardCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the card (regex compatible)")
|
||||||
cardCmd.Flags().Int64VarP(&cmc, "cmc", "m", -1, "Cumulative mana cost of card")
|
cardCmd.Flags().Int64VarP(&cmc, "cmc", "m", -1, "Cumulative mana cost of card")
|
||||||
cardCmd.Flags().StringVarP(&color, "color", "i", "", "Color identity of card (w,u,b,r,g)")
|
cardCmd.Flags().StringVarP(&color, "color", "i", "", "Color identity of card (w,u,b,r,g)")
|
||||||
cardCmd.Flags().StringVarP(&oracle, "oracle", "o", "", "Contains string in card text")
|
cardCmd.Flags().StringArrayVarP(&oracle, "oracle", "o", []string{}, "Contains string in card text")
|
||||||
cardCmd.Flags().StringVarP(&cardType, "type", "t", "", "Contains string in card type line")
|
cardCmd.Flags().StringVarP(&cardType, "type", "t", "", "Contains string in card type line")
|
||||||
cardCmd.Flags().Int64VarP(&count, "min-count", "c", 0, "Occource more than X in your collection")
|
cardCmd.Flags().Int64VarP(&count, "min-count", "c", 0, "Occource more than X in your collection")
|
||||||
cardCmd.Flags().BoolVarP(&detail, "detail", "d", false, "Show details for cards (url)")
|
cardCmd.Flags().BoolVarP(&detail, "detail", "d", false, "Show details for cards (url)")
|
||||||
@ -73,7 +73,7 @@ func ShowCard(cardids []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bool, skip, limit int64, omitInDeck bool) []Card {
|
func Cards(rarity, set, sortby, name string, oracle []string, cardType string, reserved, foil bool, skip, limit int64, omitInDeck bool) []Card {
|
||||||
client := storageConnect()
|
client := storageConnect()
|
||||||
coll := &Collection{client.Database("serra").Collection("cards")}
|
coll := &Collection{client.Database("serra").Collection("cards")}
|
||||||
defer storageDisconnect(client)
|
defer storageDisconnect(client)
|
||||||
@ -125,8 +125,10 @@ func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bo
|
|||||||
filter = append(filter, bson.E{"cmc", cmc})
|
filter = append(filter, bson.E{"cmc", cmc})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(oracle) > 0 {
|
for _, o:= range oracle {
|
||||||
filter = append(filter, bson.E{"oracletext", bson.D{{"$regex", ".*" + oracle + ".*"}, {"$options", "i"}}})
|
if len(o) > 0 {
|
||||||
|
filter = append(filter, bson.E{"oracletext", bson.D{{"$regex", ".*" + o + ".*"}, {"$options", "i"}}})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cardType) > 0 {
|
if len(cardType) > 0 {
|
||||||
|
|||||||
@ -312,12 +312,11 @@ func coloredValue(value float64) string {
|
|||||||
func askConfirmation(card *Card) bool {
|
func askConfirmation(card *Card) bool {
|
||||||
drawImage(card)
|
drawImage(card)
|
||||||
|
|
||||||
|
response := "some invalid response"
|
||||||
|
for response != "y" && response != "n" {
|
||||||
fmt.Println("Is this correct (y/n)?")
|
fmt.Println("Is this correct (y/n)?")
|
||||||
|
fmt.Scanln(&response)
|
||||||
var char = 'x'
|
|
||||||
for char != 'y' && char != 'n' {
|
|
||||||
fmt.Scanf("%c\n", &char)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return char == 'y'
|
return response == "y"
|
||||||
}
|
}
|
||||||
|
|||||||
56
pkg/serra/initialize.go
Normal file
56
pkg/serra/initialize.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package serra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(initializeCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var initializeCmd = &cobra.Command{
|
||||||
|
Use: "init",
|
||||||
|
Short: "Initialize database",
|
||||||
|
Long: "Create database optimization structures",
|
||||||
|
SilenceErrors: true,
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
return initializeDatabase()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func initializeDatabase() error {
|
||||||
|
client := storageConnect()
|
||||||
|
l := Logger()
|
||||||
|
defer storageDisconnect(client)
|
||||||
|
|
||||||
|
// create sets collection
|
||||||
|
client.Database("serra").Collection("sets")
|
||||||
|
|
||||||
|
// create total collection and insert entry if it doesn't exist
|
||||||
|
totalcoll := &Collection{client.Database("serra").Collection("total")}
|
||||||
|
totalcoll.InsertOne(context.TODO(), Total{ID: "1", Value: []PriceEntry{}})
|
||||||
|
|
||||||
|
cardscoll := &Collection{client.Database("serra").Collection("cards")}
|
||||||
|
|
||||||
|
fieldsToIndex := []string{ "name", "prices.eur", "prices.usd", "collectornumber", "serra_created" }
|
||||||
|
for _, fieldName := range fieldsToIndex {
|
||||||
|
if createIndex(cardscoll, fieldName) != nil {
|
||||||
|
l.Warnf("Failed to create index on field '%s'", fieldName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createIndex(coll *Collection, fieldName string) error {
|
||||||
|
indexModel := mongo.IndexModel{
|
||||||
|
Keys: bson.D{{fieldName, 1}},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@ -24,7 +24,7 @@ var (
|
|||||||
limit float64
|
limit float64
|
||||||
name string
|
name string
|
||||||
omitInDeck bool
|
omitInDeck bool
|
||||||
oracle string
|
oracle []string
|
||||||
port uint64
|
port uint64
|
||||||
rarity string
|
rarity string
|
||||||
reserved bool
|
reserved bool
|
||||||
@ -44,7 +44,6 @@ var rootCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
|
|
||||||
l := Logger()
|
l := Logger()
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
l.Fatal(err)
|
l.Fatal(err)
|
||||||
|
|||||||
@ -71,7 +71,7 @@ func landingPage(c *gin.Context) {
|
|||||||
sets := Sets("release")
|
sets := Sets("release")
|
||||||
|
|
||||||
// Fetch all results based on filter criteria
|
// Fetch all results based on filter criteria
|
||||||
cards := Cards("", query.Set, query.Sort, query.Name, "", "", false, false, query.Page*int64(limit), limit, false)
|
cards := Cards("", query.Set, query.Sort, query.Name, []string{}, "", false, false, query.Page*int64(limit), limit, false)
|
||||||
|
|
||||||
// Construct quick way for counting results
|
// Construct quick way for counting results
|
||||||
filter := bson.D{}
|
filter := bson.D{}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user