Exemplo n.º 1
0
        public async Task <ActionResult> DressResult(DressModelViewModel model)
        {
            var scryerDragon       = FRHelpers.ParseUrlForDragon(model.ScryerUrl);
            var dressingRoomDragon = FRHelpers.ParseUrlForDragon(model.DressingRoomUrl);

            if (scryerDragon.Age == Data.Age.Hatchling)
            {
                AddErrorNotification($"You can only dress an adult dragon!");
                return(RedirectToRoute("ScryerDresser"));
            }

            if (scryerDragon.DragonType != dressingRoomDragon.DragonType)
            {
                AddErrorNotification($"The breeds of the two images do not match. Scryer image is a <b>{scryerDragon.DragonType}</b> while the dressing room is a <b>{dressingRoomDragon.DragonType}</b>!");
                return(RedirectToRoute("ScryerDresser"));
            }

            if (scryerDragon.Gender != dressingRoomDragon.Gender)
            {
                AddErrorNotification($"The genders of the two images do not match. Scryer image is a <b>{scryerDragon.Gender}</b> while the dressing room is a <b>{dressingRoomDragon.Gender}</b>!");
                return(RedirectToRoute("ScryerDresser"));
            }

            var azureImageService = new AzureImageService();

            var azureImagePreviewPath = $@"previews\dresser\{scryerDragon.ToString().Trim('_')}\{dressingRoomDragon.Apparel?.Replace(',', '-').ToString()}.png";
            var previewUrl            = await azureImageService.Exists(azureImagePreviewPath);

            if (previewUrl == null)
            {
                var invisibleDragon = await FRHelpers.GetInvisibleDressingRoomDragon(dressingRoomDragon);

                var baseDragon = await FRHelpers.GetDragonBaseImage(scryerDragon);

                using (var graphics = Graphics.FromImage(baseDragon))
                {
                    graphics.DrawImage(invisibleDragon, new Rectangle(0, 0, 350, 350));
                }

                using (var memStream = new MemoryStream())
                {
                    baseDragon.Save(memStream, ImageFormat.Png);
                    memStream.Position = 0;

                    previewUrl = await azureImageService.WriteImage(azureImagePreviewPath, memStream);
                }
            }

            return(View(new DressModelResultViewModel {
                PreviewUrl = previewUrl
            }));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Upload(UploadModelPost model)
        {
            var azureImageService = new AzureImageService();

            var    randomizedId = CodeHelpers.GenerateId(5, DataContext.Skins.Select(x => x.GeneratedId).ToList());
            var    secretKey    = CodeHelpers.GenerateId(7);
            Bitmap skinImage    = null;

            try
            {
                skinImage = (Bitmap)Image.FromStream(model.Skin.InputStream);
                if (skinImage.Width != 350 || skinImage.Height != 350)
                {
                    AddErrorNotification("Image needs to be 350px x 350px. Just like FR.");
                    return(View());
                }
            }
            catch
            {
                AddErrorNotification("Upload is not a valid png image");
                return(View());
            }
            try
            {
                var fixPixelFormat = SkinTester.FixPixelFormat(skinImage);
                if (fixPixelFormat != null)
                {
                    skinImage = fixPixelFormat;
                }

                model.Skin.InputStream.Position = 0;
                var url = await azureImageService.WriteImage($@"skins\{randomizedId}.png", model.Skin.InputStream);

                Bitmap dragonImage = null;
                using (var client = new WebClient())
                {
                    var dwagonImageBytes = client.DownloadDataTaskAsync(string.Format(FRHelpers.DressingRoomDummyUrl, (int)model.DragonType, (int)model.Gender));
                    try
                    {
                        using (var memStream = new MemoryStream(await dwagonImageBytes, false))
                            dragonImage = (Bitmap)Image.FromStream(memStream);
                    }
                    catch
                    {
                    }
                }

                var skin = new Skin
                {
                    GeneratedId = randomizedId,
                    SecretKey   = secretKey,
                    Title       = model.Title,
                    Description = model.Description,
                    DragonType  = (int)model.DragonType,
                    GenderType  = (int)model.Gender,
                    Coverage    = GetCoveragePercentage(skinImage, dragonImage)
                };
                skinImage.Dispose();

                if (Request.IsAuthenticated)
                {
                    var userId = HttpContext.GetOwinContext().Authentication.User.Identity.GetUserId <int>();
                    skin.Creator    = DataContext.Users.FirstOrDefault(x => x.Id == userId);
                    skin.Visibility = skin.Creator.ProfileSettings.DefaultSkinsArePublic ? skin.Creator.ProfileSettings.DefaultShowSkinsInBrowse ? SkinVisiblity.Visible : SkinVisiblity.HideFromBrowse : SkinVisiblity.HideEverywhere;
                }

                DataContext.Skins.Add(skin);
                await DataContext.SaveChangesAsync();

                return(View("UploadResult", new UploadModelPostViewModel
                {
                    SkinId = randomizedId,
                    SecretKey = secretKey,
                    PreviewImageUrl = (await SkinTester.GenerateOrFetchDummyPreview(randomizedId, skin.Version)).Urls[0]
                }));
            }
            catch
            {
                AddErrorNotification("Something went wrong uploading");
                return(View());
            }
        }