예제 #1
0
        public ActionResult Details(int id, GameTypeVariant model)
        {
            var variant = GameTypeService.GetVariant(id);

            variant.Title            = model.Title;
            variant.ShortDescription = model.ShortDescription;
            variant.Description      = model.Description;
            variant.IsStaffPick      = model.IsStaffPick;
            if (ModelState.IsValid)
            {
                string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/GameType/"), variant.File.FileName);
                using (FileStream stream = System.IO.File.Open(path, FileMode.Open))
                {
                    VariantLib.GameVariant game = new VariantLib.GameVariant(stream);

                    game.VariantDescription = variant.ShortDescription;
                    game.VariantName        = variant.Title;
                    game.Save();
                }

                GameTypeService.Save();

                this.SetAlert(string.Format("The variant '{0}' has been updated.", variant.Title), AlertType.Success);
                return(RedirectToAction("Index"));
            }
            return(View(variant));
        }
예제 #2
0
        public void GameType(int id, string token)
        {
            if (!TokenService.ValidateDateTimeToken(token))
            {
                Response.StatusCode = 403;
                Response.End();
                return;
            }

            var variant = GameTypeService.GetVariant(id);

            variant.File.Downloads.Add(new FileDownload()
            {
                DownloadedOn = DateTime.UtcNow,
                UserId       = Request.IsAuthenticated ? (int?)User.Identity.GetUserId <int>() : null,
                UserIP       = Request.UserHostAddress
            });
            variant.File.DownloadCount += 1;
            GameTypeService.Save();

            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + variant.DownloadName);
            Response.TransmitFile(Path.Combine(Server.MapPath("~/Content/Files/GameType"), variant.File.FileName));
            Response.End();
        }
예제 #3
0
        public ActionResult Delete(int id, GameTypeVariant model)
        {
            var variant = GameTypeService.GetVariant(id);

            variant.IsDeleted = !variant.IsDeleted;
            GameTypeService.Save();

            this.SetAlert(string.Format("The variant '{0}' has been deleted.", variant.Title), AlertType.Success);
            return(RedirectToAction("Index"));
        }
예제 #4
0
        public ActionResult Delete(int id, GameTypeVariant model)
        {
            var variant = GameTypeService.GetVariant(id);

            if (!(variant.AuthorId == User.Identity.GetUserId <int>() || User.IsInRole("Mod")))
            {
                return(new HttpUnauthorizedResult());
            }

            variant.IsDeleted = true;
            GameTypeService.Save();

            SetAlert(string.Format("The game variant {0} is deleted.", variant.Title), AlertType.Success);
            return(RedirectToAction("Index"));
        }
예제 #5
0
        public ActionResult Edit(int id, Models.GameTypeVariant model)
        {
            if (ModelState.IsValid)
            {
                var variant = GameTypeService.GetVariant(id);

                if (!(variant.AuthorId == User.Identity.GetUserId <int>() || User.IsInRole("Mod")))
                {
                    return(new HttpUnauthorizedResult());
                }

                variant.Title            = model.Title;
                variant.Description      = model.Description;
                variant.ShortDescription = model.ShortDescription;

                string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/GameType/"), variant.File.FileName);
                using (FileStream stream = System.IO.File.Open(path, FileMode.Open))
                {
                    VariantLib.GameVariant type = new VariantLib.GameVariant(stream);

                    type.VariantDescription = variant.ShortDescription;
                    type.VariantName        = variant.Title;
                    type.Save();
                }

                if (User.IsInRole("Mod"))
                {
                    variant.IsStaffPick = model.IsStaffPick;
                }

                GameTypeService.Save();

                SetAlert(string.Format("The game variant is saved.", variant.Title), AlertType.Success);
                return(RedirectToAction("Details", new { slug = variant.Slug }));
            }
            return(View(model));
        }
예제 #6
0
        public ActionResult GameType(ViewModels.UploadViewModel model)
        {
            Response.TrySkipIisCustomErrors = true;
            if (ModelState.IsValid)
            {
                var currentUser = UserManager.FindById(User.Identity.GetUserId <int>());

                Models.GameTypeVariant variant = new Models.GameTypeVariant();
                variant.Title            = model.Title.Replace("\0", "");
                variant.ShortDescription = model.Description;
                variant.Description      = model.Content;
                variant.CreatedOn        = DateTime.UtcNow;
                variant.AuthorId         = User.Identity.GetUserId <int>();
                variant.File             = new Models.File()
                {
                    FileSize   = model.File.ContentLength,
                    FileName   = Guid.NewGuid().ToString() + ".gtv",
                    UploadedOn = variant.CreatedOn,
                    MD5        = model.File.InputStream.ToMD5()
                };

                var validateGame = GameTypeService.ValidateHash(variant.File.MD5);

                if (validateGame != null)
                {
                    Response.StatusCode = 400;
                    return(Content(string.Format(
                                       "<b>Keep it Clean!</b> The game variant has already been uploaded: <a target=\"_blank\" href=\"{0}\">{1}</a>.",
                                       Url.Action("Details", "GameType", new { slug = validateGame.Slug }),
                                       validateGame.Title)));
                }

                /* Read the map type from the uploaded file.
                 * This is also a validation message to make sure
                 * that the file uploaded is an actual map.
                 */

                var detectType = VariantDetector.Detect(model.File.InputStream);
                if (detectType == VariantType.Invalid)
                {
                    // The given map doesn't exist.
                    model.File.InputStream.Close();

                    Response.StatusCode = 400;
                    return(Content("<b>Keep it Clean!</b> The file uploaded is invalid. Please make sure it's a valid game variant."));
                }
                else if (detectType == VariantType.ForgeVariant)
                {
                    // The given map doesn't exist.
                    model.File.InputStream.Close();

                    Response.StatusCode = 400;
                    return(Content("<strong>PARDON OUR DUST!</strong> Can't upload forge variant as game variant."));
                }

                string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/GameType/"), variant.File.FileName);
                using (var stream = new System.IO.MemoryStream())
                {
                    model.File.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                    model.File.InputStream.CopyTo(stream);

                    GameVariant variantItem = new GameVariant(stream);

                    var type = GameTypeService.GetByInternalId(variantItem.TypeId);

                    if (type != null)
                    {
                        variant.GameTypeId = type.Id;

                        variantItem.VariantName = model.Title;
                        //variantItem.VariantAuthor = currentUser.UploaderName;
                        variantItem.VariantDescription = variant.ShortDescription;
                        variantItem.Save();


                        // Save the file.
                        using (var fileStream = System.IO.File.Create(path))
                        {
                            stream.Seek(0, System.IO.SeekOrigin.Begin);
                            stream.CopyTo(fileStream);
                        }
                    }
                    else
                    {
                        Response.StatusCode = 400;
                        return(Content("<strong>PARDON OUR DUST!</strong> We currently do not support the uploaded gametype."));
                    }
                }

                GameTypeService.AddVariant(variant);
                GameTypeService.Save();

                return(Content(Url.Action("Details", "GameType", new { slug = variant.Slug })));
            }

            Response.StatusCode = 400;
            return(View("~/Views/Shared/_ModelState.cshtml"));
        }