示例#1
0
        public DeckModule()
        {
            this.RequiresAuthentication();

            //viewing public decks of a planeswalker
            Get["/mydecks"] = parameters => {
                DecksModel model = new DecksModel();
                model.ActiveMenu   = "mydecks";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Decks        = deckbuilder.GetUserDecks(model.Planeswalker.Id).ToList();
                model.Title        = "My Decks";

                return(View["Deck/MyDecks", model]);
            };

            Post["/decks/delete/{id}"] = parameters => {
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                Guid         id           = Guid.Empty;
                Deck         deck         = new Deck();
                if (Guid.TryParse((string)parameters.id, out id))
                {
                    deck = deckbuilder.GetDeck(id);

                    if (deck != null && deck.IsOwner(planeswalker))
                    {
                        deckbuilder.DeleteDeck(deck.Id);
                    }
                }

                return(Response.AsRedirect("/mydecks"));
            };


//            //for deck link sharing and viewing single deck
//            Get["/pw/{planeswalker}/decks/{name}"] = parameters => {
//                DeckModel model =       new DeckModel();
//                model.ActiveMenu =      "mydecks";
//                model.Planeswalker =    (Planeswalker)this.Context.CurrentUser;
//                model.Deck =
//                    deckbuilder.GetDeck(model.Planeswalker.Id,
//                        (string)parameters.name);
//
//                return View["Deck/Deck", model];
//            };
//
//            Get["/decks"] = parameters => {
//                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
//                Deck [] decks = deckbuilder.GetUserDecks(planeswalker.Id);
//
//                return View["Deck/Decks", decks.ToArray()];
//            };
//
//            //create a deck SPA
//            Post["/decks"] = parameters => {
//                Planeswalker planeswalker =
//                    (Planeswalker)this.Context.CurrentUser;
//
//                int [] cards = null;
//                int [] sideBar = null;
//
//                try
//                {
//                    cards = ((string)Request.Form.Cards).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                    sideBar = ((string)Request.Form.SideBar).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                }
//                catch(Exception e)
//                {
//                    throw e;
//                }
//
//                string name = (string)this.Request.Form.Name;
//                string description = (string)this.Request.Form.Description;
//
//                Deck deck = new Deck();
//                deck.Name = name;
//                deck.Description = description;
//                deck.SetCards(cards);
//                deck.SetSideBar(sideBar);
//                deck.UserId = planeswalker.Id;
//
//                deck = deckbuilder.AddDeck(deck);
//
//                return Response.AsJson(deck);
//            };

//            //update a deck SPA
//            Post["/decks/{name}"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                string deckName =               (string)parameters.name;
//
//                int [] cards = null;
//                int [] sideBar = null;
//
//                try
//                {
//                    cards = ((string)Request.Form.Cards).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                    sideBar = ((string)Request.Form.SideBar).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                }
//                catch(Exception e)
//                {
//                    throw e;
//                }
//
//                string name = (string)this.Request.Form.Name;
//                string description = (string)this.Request.Form.Description;
//
//                Deck deck = deckbuilder.GetDeck(planeswalker.Id, deckName);
//                deck.Name = name;
//                deck.Description = description;
//                deck.SetCards(cards);
//                deck.SetSideBar(sideBar);
//                deck.UserId = planeswalker.Id;
//
//                deck = deckbuilder.UpdateDeck(deck);
//
//                return Response.AsJson(deck);
//            };
//
//            //Delete a deck SPA
//            Post["/decks/delete/{name}"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck = deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                try
//                {
//                    deckbuilder.DeleteDeck(deck.Id);
//                }
//                catch(Exception e)
//                {
//                    return Response.AsJson(e.Message,
//                        HttpStatusCode.NotAcceptable);
//                }
//
//                return Response.AsJson("true", HttpStatusCode.Accepted);
//            };
//
//            //Get a deck SPA
//            Get["/decks/{name}"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck =   deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                return Response.AsJson(deck);
//            };
//
//            //Get cards for a deck SPA
//            Get["/decks/{name}/cards"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck =   deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                return Response.AsJson(deck.GetCards());
//            };
//
//            //Get cards for a deck SPA
//            Get["/decks/{name}/sidebar"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck =  deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                return Response.AsJson(deck.GetSideBarCards());
//            };

            //gets the latest version of card at the time of import
            Post["/decks/import"] = parameters => {
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                string       deckName     = (string)this.Request.Form.Name;
                string       description  = (string)this.Request.Form.Description;
                var          file         = this.Request.Files.FirstOrDefault();
                Deck         deck         = null;

                if (file != null)
                {
                    deck             = MtgFile.ImportDec(file.Value);
                    deck.Name        = deckName;
                    deck.Description = description;
                    deck.UserId      = planeswalker.Id;
                }

                return(Response.AsJson(deck));
            };

            //testing method
            Post["/col/import"] = parameters => {
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                var          file         = this.Request.Files.FirstOrDefault();

                MtgDb.Info.MtgFile.Item[] items = null;

                if (file != null)
                {
                    items = MtgFile.ImportColl2(file.Value);
                }

                return(Response.AsJson(items));
            };
        }
        public DeckViewerModule()
        {
            Get["/decks/viewer/{deckId?}"] = parameters => {
                DeckModel model = new DeckModel();
                model.ActiveMenu   = "mydecks";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Title        = "M:tgDb - Simple Deck Viewer";

                if (parameters.deckId != null)
                {
                    Guid id;

                    if (Guid.TryParse((string)parameters.deckId, out id))
                    {
                        model.Deck = deckbuilder.GetDeck(id);

                        if (model.Deck != null)
                        {
                            model.Name        = model.Deck.Name;
                            model.Description = model.Description;
                            model.DeckFile    = MtgFile.ExportDec(model.Deck);
                        }
                    }
                }

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

            Post["/decks/viewer/{deckId?}"] = parameters => {
                DeckModel model = this.Bind <DeckModel>();
                model.ActiveMenu   = "mydecks";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Title        = "M:tgDb - Simple Deck Viewer";

                var result = this.Validate(model);

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


                byte[]       byteArray = Encoding.ASCII.GetBytes(model.DeckFile);
                MemoryStream stream    = new MemoryStream(byteArray);

                try
                {
                    model.Deck = MtgFile.ImportDec(stream);

                    if (model.Deck.Cards.Count == 0)
                    {
                        model.Errors.Add("Deck file contains no valid cards.");
                        return(View["Deck/Deck", model]);
                    }
                }
                catch (Exception exc)
                {
                    model.Errors.Add(exc.Message);
                    return(View["Deck/Deck", model]);
                }

                if (parameters.deckId != null)
                {
                    Guid id;

                    if (Guid.TryParse((string)parameters.deckId, out id))
                    {
                        Deck deck = null;
                        try
                        {
                            deck = deckbuilder.GetDeck(id);
                        }
                        catch (Exception exc)
                        {
                            model.Errors.Add(exc.Message);
                        }

                        if (deck != null)
                        {
                            model.Deck.UserId    = deck.UserId;
                            model.Deck.Id        = deck.Id;
                            model.Deck.CreatedAt = deck.CreatedAt;
                        }
                    }
                }

                if (Request.Form.Save != null)
                {
                    if (model.Deck.Id != Guid.Empty &&
                        model.Planeswalker.Id == model.Deck.UserId)
                    {
                        model.Deck.UserId      = model.Planeswalker.Id;
                        model.Deck.Name        = model.Name;
                        model.Deck.Description = model.Description;
                        try
                        {
                            model.Deck = deckbuilder.UpdateDeck(model.Deck);
                        }
                        catch (Exception exc)
                        {
                            model.Errors.Add(exc.Message);
                        }
                    }
                    else
                    {
                        if (model.Planeswalker != null)
                        {
                            model.Deck.UserId = model.Planeswalker.Id;
                        }

                        model.Deck.Name        = model.Name;
                        model.Deck.Description = model.Description;
                        try
                        {
                            model.Deck = deckbuilder.AddDeck(model.Deck);
                        }
                        catch (Exception exc)
                        {
                            model.Errors.Add(exc.Message);
                        }

                        if (model.Deck.UserId == Guid.Empty && !string.IsNullOrEmpty(model.Email))
                        {
                            try
                            {
                                Email.Send(model.Email, "M:tgDb - You created an anonymous deck!",
                                           string.Format("Deck link: https://www.mtgdb.info/decks/viewer/{0}",
                                                         model.Deck.Id));
                            }
                            catch (Exception exc)
                            {
                                model.Errors.Add(exc.Message);
                            }
                        }
                    }

                    return(Response.AsRedirect(string.Format("/decks/viewer/{0}",
                                                             model.Deck.Id.ToString())));
                }

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