Beispiel #1
0
        public IActionResult CreateHero(Hero newHero)
        {
            Hero heroToAdd = new Hero()
            {
                name         = newHero.name,
                attribute    = newHero.attribute,
                intelligence = newHero.intelligence,
                agility      = newHero.agility,
                strength     = newHero.strength,
                attack       = newHero.attack,
                speed        = newHero.speed,
                armor        = newHero.armor,
                bio          = newHero.bio,
                attack_range = newHero.attack_range,
                attack_type  = newHero.attack_type,
                img          = newHero.img
            };

            _context.Add(heroToAdd);
            _context.SaveChanges();
            return(RedirectToAction("AddHero"));
        }
        public IActionResult VoteUp(int id, string user)
        {
            User     thisUser     = _context.Users.Single(u => u.username == user);
            Vote     existingVote = _context.Votes.SingleOrDefault(v => v.new_hero_id == id && v.user_id == thisUser.id);
            New_Hero thisHero     = _context.New_Heroes.SingleOrDefault(n => n.id == id);

            if (existingVote == null)
            {
                Vote newVote = new Vote()
                {
                    new_hero_id = id,
                    user_id     = thisUser.id,
                    value       = 1
                };
                if (thisHero.rating == null)
                {
                    thisHero.rating = (decimal)100.00;
                }
                else
                {
                    int numberOfVotes = _context.Votes.Where(v => v.new_hero_id == id).Count();
                    thisHero.rating = (thisHero.rating * numberOfVotes + 100) / (numberOfVotes + 1);
                }
                _context.Update(thisHero);
                _context.Add(newVote);
            }
            else
            {
                existingVote.value = 1;
                int numberOfVotes = _context.Votes.Where(v => v.new_hero_id == id).Count();
                thisHero.rating = (thisHero.rating * numberOfVotes + 100) / numberOfVotes;
                _context.Update(thisHero);
                _context.Update(existingVote);
            }
            _context.SaveChanges();
            return(RedirectToAction("HeroPage", new { id = id }));
        }
Beispiel #3
0
        public IActionResult Submit(New_Hero_Creator model)
        {
            List <DisplaySpell> allSpells = new List <DisplaySpell>();

            foreach (Spell s in _context.Spells)
            {
                allSpells.Add(Converter.Convert(s));
            }
            ViewBag.heroes    = _context.Heroes.ToList();
            ViewBag.regulars  = allSpells.Where(s => s.ultimate == false).ToList();
            ViewBag.ultimates = allSpells.Where(s => s.ultimate == true).ToList();
            if (model.hero_id == 0)
            {
                ModelState.AddModelError("hero_id", "You must select a base for your hero.");
            }
            if (model.spell_1_id == 0 || model.spell_2_id == 0 || model.spell_3_id == 0)
            {
                ModelState.AddModelError("spell_1_id", "You must select three regular spells.");
            }
            else if (model.spell_1_id == model.spell_2_id || model.spell_1_id == model.spell_3_id || model.spell_2_id == model.spell_3_id)
            {
                ModelState.AddModelError("spell_1_id", "Cannot use the same spell twice.");
            }
            if (model.spell_4_id == 0)
            {
                ModelState.AddModelError("spell_4_id", "Your hero needs an ultimate.");
            }
            if (_context.New_Heroes.SingleOrDefault(n => n.name == model.name) != null)
            {
                ModelState.AddModelError("name", "This name is already in use.");
            }
            if (ModelState.IsValid)
            {
                New_Hero hero = new New_Hero()
                {
                    name       = model.name,
                    hero_id    = model.hero_id,
                    spell_1_id = model.spell_1_id,
                    spell_2_id = model.spell_2_id,
                    spell_3_id = model.spell_3_id,
                    spell_4_id = model.spell_4_id
                };
                if (HttpContext.Session.GetString("username") != null)
                {
                    hero.user_id = _context.Users.SingleOrDefault(u => u.username == HttpContext.Session.GetString("username")).id;
                }
                if (model.bio != null)
                {
                    hero.bio = model.bio;
                }
                if (model.file != null)
                {
                    TransferUtility transfer = new TransferUtility(Credentials.AccessKey, Credentials.SecretKey, Amazon.RegionEndpoint.USWest2);
                    using (var stream = new MemoryStream())
                    {
                        string key = null;
                        while (_context.New_Heroes.FirstOrDefault(n => n.img == key) != null)
                        {
                            key = GenerateKey();
                        }
                        model.file.CopyTo(stream);
                        transfer.Upload(stream, "dhcimages", key);
                        hero.img = key;
                    }
                }
                _context.Add(hero);
                _context.SaveChanges();

                return(RedirectToAction("HeroPage", "Home", new { id = _context.New_Heroes.SingleOrDefault(n => n.name == hero.name).id }));
            }
            ViewBag.bio = model.bio;
            return(View("Create"));
        }