private static bool SetMatches(CardSet set, string name, SearchTypes searchTypes, bool caseSensitive = false)
    {
      var setName = caseSensitive
        ? set.Name
        : set.Name.ToLower();

      var promptName = caseSensitive
        ? name
        : name.ToLower();

      switch (searchTypes)
      {
        case SearchTypes.StartsWith:
          return setName.StartsWith(promptName);

        case SearchTypes.EndsWith:
          return setName.EndsWith(promptName);

        case SearchTypes.Equals:
          return setName == promptName;

        case SearchTypes.Contains:
        default:
          return setName.Contains(promptName);
      }
    }
예제 #2
0
        public Guid AddCardSetChangeRequest(SetChange change)
        {
            MongoCollection <SetChange> collection =
                database.GetCollection <SetChange> ("set_changes");

            List <SetChange> changes = new List <SetChange>();
            CardSet          set     = magicdb.GetSet(change.SetId);
            var query        = Query.And(Query <SetChange> .EQ(e => e.SetId, change.SetId));
            var mongoResults = collection.Find(query);

            foreach (SetChange c in mongoResults)
            {
                changes.Add(c);
            }

            if (changes.Count == 0)
            {
                SetChange original = SetChange.MapSet(set);
                original.Comment   = "Original set info";
                original.Id        = Guid.NewGuid();
                original.Version   = 0;
                original.CreatedAt = DateTime.Now;

                collection.Save(original);
            }

            change.Id            = Guid.NewGuid();
            change.Version       = changes.Count == 0 ? 1 : changes.Count;
            change.CreatedAt     = DateTime.Now;
            change.ModifiedAt    = DateTime.Now;
            change.FieldsUpdated = SetChange.FieldsChanged(set, change);
            change.Status        = "Pending";

            if (change.FieldsUpdated == null ||
                change.FieldsUpdated.Length == 0)
            {
                throw new ArgumentException("No fields have been updated.");
            }

            try
            {
                collection.Save(change);
            }
            catch (Exception e)
            {
                change.Id = Guid.Empty;
                throw e;
            }

            return(change.Id);
        }
예제 #3
0
        public static SetChange MapSet(CardSet set)
        {
            SetChange change = new SetChange();

            change.SetId       = set.Id;
            change.Name        = set.Name;
            change.Type        = set.Type;
            change.Block       = set.Block;
            change.Description = set.Description;
            change.ReleasedAt  = set.ReleasedAt.ToString(DateFormat);
            change.Common      = set.Common;
            change.Uncommon    = set.Uncommon;
            change.Rare        = set.Rare;
            change.MythicRare  = set.MythicRare;

            return(change);
        }
예제 #4
0
        public CardModule()
        {
            this.RequiresAuthentication();

            Post ["/cards/{id}/amount/{count}"] = parameters => {
                int  multiverseId = (int)parameters.id;
                int  count        = (int)parameters.count;
                Guid walkerId     = ((Planeswalker)this.Context.CurrentUser).Id;

                repository.AddUserCard(walkerId, multiverseId, count);

                return(count.ToString());
            };

            //TODO: Refactor this, to long and confusing
            Get ["/mycards/{setId?}", true] = async(parameters, ct) => {
                PlaneswalkerModel model = new PlaneswalkerModel();
                model.ActiveMenu = "mycards";
                string setId = (string)parameters.setId;
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                if (setId == null)
                {
                    //try to get an setId if no value was provided in the url
                    setId = repository.GetSetCardCounts(model.Planeswalker.Id)
                            .Select(x => x.Key)
                            .OrderBy(x => x).FirstOrDefault();

                    //if still no setId then user has no cards
                    if (setId == null)
                    {
                        PageModel pm = new PageModel();
                        pm.ActiveMenu   = "mycards";
                        pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                        pm.Errors.Add("You have no cards in this set or no cards in your collection");
                        return(View["Page", pm]);
                    }
                }

                CardSet current = magicdb.GetSet(setId);
                //force type into block if block is null
                model.Block = current.Block ?? current.Type;

                try
                {
                    model.Counts = repository.GetSetCardCounts(model.Planeswalker.Id);
                    //if no counts then no cards again
                    if (model.Counts == null ||
                        model.Counts.Count == 0)
                    {
                        PageModel pm = new PageModel();
                        pm.ActiveMenu   = "mycards";
                        pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                        pm.Errors.Add("You have no cards in this set or no cards in your collection");
                        return(View["Page", pm]);
                    }

                    model.TotalCards  = model.Counts.Sum(x => x.Value);
                    model.TotalAmount = repository.GetUserCards(model.Planeswalker.Id).Sum(x => x.Amount);

                    List <string> blocks;

                    if (model.Counts.Count > 0)
                    {
                        model.Sets = magicdb.GetSets(model.Counts
                                                     .AsEnumerable().Select(x => x.Key)
                                                     .ToArray());

                        //keep a list of all sets user has cards in
                        //this is to get the default set for each block or type
                        model.AllSets = model.Sets;

                        //get all blocks
                        blocks = model.Sets
                                 .Select(x => x.Block)
                                 .Where(x => x != null)
                                 .Distinct()
                                 .OrderBy(x => x).ToList();
                        //force types into blocks
                        blocks.AddRange(model.Sets
                                        .Where(x => x.Block == null)
                                        .Select(x => x.Type)
                                        .Distinct()
                                        .OrderBy(x => x).ToList());

                        //macke dure no dupes
                        blocks = blocks.Distinct().ToList();

                        foreach (string block in blocks)
                        {
                            List <CardSet> sets = new List <CardSet>();
                            sets = model.Sets.Where(x => x.Block == block).ToList();
                            sets.AddRange(model.Sets.Where(x => x.Type == block && x.Block == null).ToList());

                            int total = 0;
                            foreach (CardSet set in sets)
                            {
                                total += model.Counts[set.Id];
                            }
                            if (block != null)
                            {
                                model.Blocks.Add(block, total);
                            }
                        }

                        //filter sets for current block or type
                        model.Sets = model.Sets
                                     .Where(x => x.Block == model.Block || x.Type == model.Block)
                                     .OrderBy(x => x.Name).ToArray();

                        //nothing in the set
                        if (model.Sets == null || model.Sets.Length == 0)
                        {
                            PageModel pm = new PageModel();
                            pm.ActiveMenu   = "mycards";
                            pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                            pm.Errors.Add("You have no cards in this set or no cards in your collection");
                            return(View["Page", pm]);
                        }

                        if (setId == null)
                        {
                            setId = model.Sets.FirstOrDefault().Id;
                        }

                        model.SetId     = setId;
                        model.UserCards = repository.GetUserCards(model.Planeswalker.Id, setId);

                        //man same here no cards
                        if (model.UserCards == null ||
                            model.UserCards.Length == 0)
                        {
                            PageModel pm = new PageModel();
                            pm.ActiveMenu   = "mycards";
                            pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                            pm.Errors.Add("You have no cards in this set or no cards in your collection");
                            return(View["Page", pm]);
                        }

                        Card[] dbcards = null;
                        if (Request.Query.Show != null)
                        {
                            dbcards    = magicdb.GetSetCards(model.SetId);
                            model.Show = true;
                        }
                        else
                        {
                            dbcards = magicdb.GetCards(model.UserCards
                                                       .AsEnumerable()
                                                       .Where(x => x.Amount > 0)
                                                       .Select(x => x.MultiverseId)
                                                       .ToArray());

                            model.Show = false;
                        }

                        //same here no cards, this is getting old
                        if (dbcards == null ||
                            dbcards.Length == 0)
                        {
                            PageModel pm = new PageModel();
                            pm.ActiveMenu   = "mycards";
                            pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                            pm.Errors.Add("You have no cards in this set or no cards in your collection");
                            return(View["Page", pm]);
                        }

                        List <CardInfo> cards = new List <CardInfo>();

                        CardInfo card = null;
                        foreach (Card c in dbcards)
                        {
                            card        = new CardInfo();
                            card.Amount = model.UserCards
                                          .AsEnumerable()
                                          .Where(x => x.MultiverseId == c.Id)
                                          .Select(x => x.Amount)
                                          .FirstOrDefault();

                            card.Card = c;

                            cards.Add(card);
                        }

                        model.Cards = cards.OrderBy(x => x.Card.SetNumber).ToArray();
                    }
                    else
                    {
                        model.Messages.Add("You have no cards in your library.");
                    }
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["MyCards", model]);
            };

            //can use this for public profile stuff
            Get ["/pw/{planeswalker}/cards"] = parameters => {
                PageModel model = new PageModel();
                model.ActiveMenu   = "mycards";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Page", model]);
            };
        }
예제 #5
0
        public OpenDbModule()
        {
            this.RequiresAuthentication();

            Get["/cr/{status?}"] = parameters => {
                ChangeRequestModel model  = new ChangeRequestModel();
                string             status = (string)parameters.status;
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Title        = "M:tgDb.Info Admin";

                if (status == null)
                {
                    model.Changes    = repository.GetChangeRequests().ToList();
                    model.NewCards   = repository.GetNewCards().ToList();
                    model.NewSets    = repository.GetNewSets().ToList();
                    model.SetChanges = repository.GetSetChangeRequests().ToList();
                }
                else
                {
                    model.Changes    = repository.GetChangeRequests(status).ToList();
                    model.NewCards   = repository.GetNewCards(status).ToList();
                    model.NewSets    = repository.GetNewSets(status).ToList();
                    model.SetChanges = repository.GetSetChangeRequests(status).ToList();
                }

                return(View["Change/ChangeRequests", model]);
            };

            Get["/sets/new", true] = async(parameters, ct) => {
                NewSet model = new NewSet();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Change/NewSet", model]);
            };

            Post["/sets/new", true] = async(parameters, ct) => {
                NewSet model = this.Bind <NewSet>();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/NewSet", model]);
                }

                repository.AddSet(model);

                return(Response.AsRedirect(string.Format("/sets/new/{0}",
                                                         model.Id)));
            };

            Get["/sets/new/{id}", true] = async(parameters, ct) => {
                NewSet model = repository.GetSet((Guid)parameters.id);

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Change/NewSetRead", model]);
            };

            Post["/sets/new/{id}", true] = async(parameters, ct) => {
                NewSet model = repository.GetSet((Guid)parameters.id);
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                Admin admin = new Admin(ConfigurationManager.AppSettings.Get("api"));

                if (!model.Planeswalker.InRole("admin"))
                {
                    return(HttpStatusCode.Unauthorized);
                }

                MtgDbAdminDriver.CardSet newSet =
                    new MtgDbAdminDriver.CardSet()
                {
                    Id          = model.SetId,
                    Name        = model.Name,
                    Description = model.Description,
                    ReleasedAt  = model.ReleasedAt,
                    Type        = model.Type,
                    Block       = model.Block,
                    Common      = model.Common,
                    Uncommon    = model.Uncommon,
                    Rare        = model.Rare,
                    MythicRare  = model.MythicRare,
                    BasicLand   = model.BasicLand
                };

                admin.AddSet(model.Planeswalker.AuthToken, newSet);
                model = repository.UpdateNewSetStatus(model.Id, "Accepted");

                model.Messages.Add("Set has been sucessfully added.");

                return(View["Change/NewSetRead", model]);
            };

            Get["/cards/new/{id}", true] = async(parameters, ct) => {
                NewCard model = repository.GetCard((Guid)parameters.id);
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Change/NewCardsRead", model]);
            };

            Post["/cards/new/{id}", true] = async(parameters, ct) => {
                NewCard model = repository.GetCard((Guid)parameters.id);
                Admin   admin = new Admin(ConfigurationManager.AppSettings.Get("api"));
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                if (!model.Planeswalker.InRole("admin"))
                {
                    return(HttpStatusCode.Unauthorized);
                }


                string [] colors = null;

                if (model.Colors == null ||
                    model.Colors.Length == 0)
                {
                    colors    = new string[1];
                    colors[0] = "None";
                }
                else
                {
                    colors = model.Colors.Split(',');
                }

                MtgDbAdminDriver.Card newCard =
                    new MtgDbAdminDriver.Card()
                {
                    Id                = model.Mvid,
                    Name              = model.Name,
                    Description       = model.Description,
                    Flavor            = model.Flavor,
                    Power             = model.Power,
                    Toughness         = model.Toughness,
                    Loyalty           = model.Loyalty,
                    Artist            = model.Artist,
                    Type              = model.Type,
                    SubType           = model.SubType,
                    Token             = model.Token,
                    Colors            = colors,
                    CardSetId         = model.CardSetId,
                    ManaCost          = model.ManaCost,
                    ConvertedManaCost = model.ConvertedManaCost,
                    RelatedCardId     = model.RelatedCardId,
                    Rarity            = model.Rarity,
                    SetNumber         = model.SetNumber
                };

                try
                {
                    admin.AddCard(model.Planeswalker.AuthToken, newCard);
                    repository.UpdateNewCardStatus(model.Id, "Accepted");
                    model = repository.GetCard((Guid)parameters.id);
                    model.Messages.Add("Card has been sucessfully added.");
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Change/NewCardsRead", model]);
            };

            Get["/cards/new", true] = async(parameters, ct) => {
                NewCard model = new NewCard();

                if (Request.Query.SetId != null)
                {
                    model.CardSetId = (string)Request.Query.SetId;
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Types        = magicdb.GetCardTypes();
                model.SubTypes     = magicdb.GetCardSubTypes();
                model.Rarities     = magicdb.GetCardRarityTypes();

                return(View["Change/NewCard", model]);
            };

            Post["/cards/new", true] = async(parameters, ct) => {
                NewCard model = this.Bind <NewCard>();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;

                model.Types    = magicdb.GetCardTypes();
                model.SubTypes = magicdb.GetCardSubTypes();
                model.Rarities = magicdb.GetCardRarityTypes();

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/NewCard", model]);
                }

                Guid id = repository.AddCard(model);
                model = repository.GetCard(id);

                return(Response.AsRedirect(string.Format("/cards/new/{0}",
                                                         model.Id)));
            };


            Get["/cards/{id}/logs"] = parameters => {
                CardLogsModel model = new CardLogsModel();
                model.ActiveMenu   = "sets";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Mvid         = (int)parameters.id;

                if (Request.Query.v != null)
                {
                    int version = 0;
                    if (int.TryParse((string)Request.Query.v, out version))
                    {
                        model.NewVersion = version;
                    }
                }

                try
                {
                    model.Changes = repository.GetCardChangeRequests((int)parameters.id)
                                    .OrderByDescending(x => x.Version)
                                    .ToList();
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Change/CardLogs", model]);
            };

            Get["/cards/{id}/logs/{logid}"] = parameters => {
                CardChange model = new CardChange();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model = repository.GetCardChangeRequest((Guid)parameters.logid);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                model.ActiveMenu   = "sets";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Mvid         = (int)parameters.id;


                return(View["Change/CardChange", model]);
            };

            Post["/change/{id}/field/{field}"] = parameters => {
                Admin        admin        = new Admin(ConfigurationManager.AppSettings.Get("api"));
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                Guid         changeId     = Guid.Parse((string)parameters.id);
                string       field        = (string)parameters.field;
                CardChange   change       = null;

                if (!planeswalker.InRole("admin"))
                {
                    return(HttpStatusCode.Unauthorized);
                }

                try
                {
                    change = repository.GetCardChangeRequest(changeId);

                    if (field == "close")
                    {
                        repository.UpdateCardChangeStatus(change.Id, "Closed");

                        return(Response.AsRedirect(string.Format("/cards/{0}/logs/{1}",
                                                                 change.Mvid, change.Id)));
                    }

                    if (field == "open")
                    {
                        repository.UpdateCardChangeStatus(change.Id, "Pending");

                        return(Response.AsRedirect(string.Format("/cards/{0}/logs/{1}",
                                                                 change.Mvid, change.Id)));
                    }


                    if (field == "formats")
                    {
                        admin.UpdateCardFormats(planeswalker.AuthToken,
                                                change.Mvid, change.Formats);
                    }
                    else if (field == "rulings")
                    {
                        admin.UpdateCardRulings(planeswalker.AuthToken,
                                                change.Mvid, change.Rulings);
                    }
                    else
                    {
                        string value = change.GetFieldValue(field);
                        admin.UpdateCardField(planeswalker.AuthToken,
                                              change.Mvid, field, (string)value);
                    }

                    repository.UpdateCardChangeStatus(change.Id, "Accepted", field);
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(Response.AsRedirect(string.Format("/cards/{0}/logs/{1}",
                                                         change.Mvid, change.Id)));
            };

            Post["/setchange/{id}/field/{field}"] = parameters => {
                Admin        admin        = new Admin(ConfigurationManager.AppSettings.Get("api"));
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                Guid         changeId     = Guid.Parse((string)parameters.id);
                string       field        = (string)parameters.field;
                SetChange    change       = null;

                if (!planeswalker.InRole("admin"))
                {
                    return(HttpStatusCode.Unauthorized);
                }

                try
                {
                    change = repository.GetCardSetChangeRequest(changeId);

                    if (field == "close")
                    {
                        repository.UpdateCardSetChangeStatus(change.Id, "Closed");

                        return(Response.AsRedirect(string.Format("/sets/{0}/logs/{1}",
                                                                 change.SetId, change.Id)));
                    }

                    if (field == "open")
                    {
                        repository.UpdateCardSetChangeStatus(change.Id, "Pending");

                        return(Response.AsRedirect(string.Format("/sets/{0}/logs/{1}",
                                                                 change.SetId, change.Id)));
                    }

                    string value = change.GetFieldValue(field);
                    admin.UpdateCardSetField(planeswalker.AuthToken,
                                             change.SetId, field, (string)value);

                    repository.UpdateCardSetChangeStatus(change.Id, "Accepted", field);
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(Response.AsRedirect(string.Format("/sets/{0}/logs/{1}",
                                                         change.SetId, change.Id)));
            };

            Get ["/cards/{id}/change"] = parameters => {
                CardChange model = new CardChange();
                Card       card;

                try
                {
                    card  = magicdb.GetCard((int)parameters.id);
                    model = CardChange.MapCard(card);
                    model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(View["Change/Card", model]);
            };

            Post ["/cards/{id}/change"] = parameters => {
                CardChange model   = this.Bind <CardChange>();
                Card       current = magicdb.GetCard((int)parameters.id);
                model.CardSetId    = current.CardSetId;
                model.CardSetName  = current.CardSetName;
                model.Name         = current.Name;
                model.Mvid         = (int)parameters.id;
                model.Rulings      = Bind.Rulings(Request);
                model.Formats      = Bind.Formats(Request);
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;
                CardChange card = null;

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/Card", model]);
                }

                try
                {
                    card = repository.GetCardChangeRequest(
                        repository.AddCardChangeRequest(model)
                        );
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                    return(View["Change/Card", model]);
                }

                return(Response.AsRedirect(string.Format("/cards/{0}/logs?v={1}",
                                                         card.Mvid, card.Version)));

                //return model.Description;
                //return View["Change/Card", model];
            };

            Get ["/sets/{id}/change"] = parameters => {
                SetChange model = new SetChange();
                CardSet   set;

                try
                {
                    set   = magicdb.GetSet((string)parameters.id);
                    model = SetChange.MapSet(set);
                    model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(View["Change/CardSet", model]);
            };

            Post ["/sets/{id}/change"] = parameters => {
                SetChange model   = this.Bind <SetChange>();
                CardSet   current = magicdb.GetSet((string)parameters.id);
                model.SetId = current.Id;

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;
                SetChange set = null;

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/CardSet", model]);
                }

                try
                {
                    set = repository.GetCardSetChangeRequest(
                        repository.AddCardSetChangeRequest(model)
                        );
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                    return(View["Change/CardSet", model]);
                }

                return(Response.AsRedirect(string.Format("/sets/{0}/logs?v={1}",
                                                         set.SetId, set.Version)));

                //return model.Description;
                //return View["Change/Card", model];
            };

            Get["/sets/{id}/logs"] = parameters => {
                SetLogsModel model = new SetLogsModel();
                model.ActiveMenu   = "sets";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.SetId        = (string)parameters.id;

                if (Request.Query.v != null)
                {
                    int version = 0;
                    if (int.TryParse((string)Request.Query.v, out version))
                    {
                        model.NewVersion = version;
                    }
                }

                try
                {
                    model.Changes = repository.GetCardSetChangeRequests((string)parameters.id)
                                    .OrderByDescending(x => x.Version)
                                    .ToList();
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Change/SetLogs", model]);
            };

            Get["/sets/{id}/logs/{logid}"] = parameters => {
                SetChange model = new SetChange();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model = repository.GetCardSetChangeRequest((Guid)parameters.logid);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                model.ActiveMenu   = "sets";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.SetId        = (string)parameters.id;


                return(View["Change/SetChange", model]);
            };
        }