Example #1
0
        public ActionResult Index(MysteryDetail mysteryDetail)
        {
            if (string.IsNullOrWhiteSpace(mysteryDetail.Name))
            {
                return(Error(mysteryDetail, "Please ensure all details are filled in."));
            }
            var details = mysteryDetail.Clues as IList <ClueDetail> ?? mysteryDetail.Clues.ToList();

            if (details.Any(clueDetail => string.IsNullOrWhiteSpace(clueDetail.Clue) || clueDetail.Latitude == null || clueDetail.Longitude == null))
            {
                return(Error(mysteryDetail, "Please ensure all details are filled in."));
            }

            var service = CreateTwitterService();

            var token       = TempData["token"];
            var tokenSecret = TempData["tokenSecret"];

            service.AuthenticateWith(token.ToString(), tokenSecret.ToString());

            var finalMystery = details.Aggregate("", (current, detail) => current + detail.Clue);

            foreach (var clueDetail in details)
            {
                var bmp    = RandomImage.GetImage();
                var colour = bmp.GetPixel(0, 0);
                bmp.MakeTransparent(colour);

                var embedded = new EmbeddedDetails
                {
                    Mystery      = mysteryDetail.Name,
                    Clue         = clueDetail.Clue,
                    Latitude     = clueDetail.Latitude.Value,
                    Longitude    = clueDetail.Longitude.Value,
                    Message      = clueDetail.Message,
                    FinalMystery = finalMystery
                };
                var json    = new JavaScriptSerializer().Serialize(embedded);
                var message = Encryption.Encrypt(json);
                var img     = Steganography.Embed(message, bmp);
                var ms      = new MemoryStream();
                img.Save(ms, ImageFormat.Png);
                ms.Seek(0, SeekOrigin.Begin);
                service.SendTweetWithMedia(new SendTweetWithMediaOptions
                {
                    Status = "#twitspionage clue",
                    Images = new Dictionary <string, Stream> {
                        { "Twitspionage clue", ms }
                    }
                });

                if (service.Response.StatusCode == HttpStatusCode.OK && service.Response.Error == null)
                {
                    continue;
                }

                return(Error(mysteryDetail, "Error tweeting clues. Please check your Twitter account and repost any that are missing."));
            }
            return(View("Success"));
        }
Example #2
0
 private ActionResult Error(MysteryDetail mysteryDetail, string errorMessage)
 {
     ViewBag.Error = errorMessage;
     return(View("Index", mysteryDetail));
 }