Esempio n. 1
0
        public CharacterViewModel GetCharacterViewModel(Character character, List<ItemViewModel> items)
        {
            CharacterViewModel characterViewModel = new CharacterViewModel();

            characterViewModel.Class = character.Class;
            characterViewModel.Name = character.Name;

            characterViewModel.Experience = character.Experience;
            characterViewModel.ExperienceToLevel = (characterViewModel.Level + 1) * 100;
            characterViewModel.Level = characterViewModel.Experience / 100 + 1;

            if (characterViewModel.Class == CharacterClass.Warrior)
            {
                characterViewModel.Strength += 3 * characterViewModel.Level;
                characterViewModel.Dexterity += 1 * characterViewModel.Level;
                characterViewModel.Intelligence += 1 * characterViewModel.Level;
            }
            else if (characterViewModel.Class == CharacterClass.Archer)
            {
                characterViewModel.Strength += 1 * characterViewModel.Level;
                characterViewModel.Dexterity += 3 * characterViewModel.Level;
                characterViewModel.Intelligence += 1 * characterViewModel.Level;
            }
            else if (characterViewModel.Class == CharacterClass.Mage)
            {
                characterViewModel.Strength += 1 * characterViewModel.Level;
                characterViewModel.Dexterity += 1 * characterViewModel.Level;
                characterViewModel.Intelligence += 3 * characterViewModel.Level;
            }

            characterViewModel.Vitality += 2 * characterViewModel.Level;

            ItemViewModel weapon = items.FirstOrDefault(i => i.Item.Type == ItemType.Weapon);
            List<ItemViewModel> armorsAndShields = items.FindAll(i => i.Item.Type == ItemType.Shield || i.Item.Type == ItemType.Armor);

            characterViewModel.Strength += items.Sum(gi => gi.GeneratedItem.Strength);
            characterViewModel.Dexterity += items.Sum(gi => gi.GeneratedItem.Dexterity);
            characterViewModel.Intelligence += items.Sum(gi => gi.GeneratedItem.Intelligence);
            characterViewModel.Vitality += items.Sum(gi => gi.GeneratedItem.Vitality);

            characterViewModel.MinDamage = (weapon != null ? weapon.GeneratedItem.PrimaryMinValue : 1);
            characterViewModel.MaxDamage = (weapon != null ? weapon.GeneratedItem.PrimaryMaxValue : 2);
            characterViewModel.Armor = armorsAndShields.Sum(aas => aas.GeneratedItem.PrimaryMinValue);

            characterViewModel.Life = characterViewModel.Vitality * 10 * characterViewModel.Level;
            characterViewModel.ChanceToHit = (weapon != null ? weapon.Item.ChanceToHit : 1) * characterViewModel.Dexterity / characterViewModel.Level;
            characterViewModel.AttackSpeed = characterViewModel.Dexterity / 100 + (weapon != null ? weapon.Item.AttackSpeed : 1);

            if (characterViewModel.ChanceToHit > 75)
            {
                characterViewModel.ChanceToHit = 75;
            }

            return characterViewModel;
        }
Esempio n. 2
0
        public ActionResult Create(Character character)
        {
            if (!Enum.IsDefined(typeof(CharacterClass), character.Class))
            {
                return HttpNotFound();
            }

            User user = this.HttpContext.Items["User"] as User;
            var query = from c in db.Characters
                        where c.UserId == user.Id
                        select c;

            if (query.Any())
            {
                return RedirectToAction("Index");
            }

            var query2 = from c in db.Characters
                         where c.Name == character.Name
                         select c;

            if (query2.Any())
            {
                ModelState.AddModelError("Name", "Nazwa postaci jest już zajęta.");
            }

            if (!ModelState.IsValid)
            {
                return View(character);
            }

            character.UserId = user.Id;
            character.Experience = 0;
            character.Gold = 0;
            character.RenewalTime = DateTime.Now;
            character.AvailableMoves = 0;

            try
            {
                db.Characters.Add(character);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch(Exception)
            {
                FlashMessageHelper.SetMessage(this, FlashMessageType.Warning, "Wystąpił nieoczekiwany błąd.");
            }

            return View(character);
        }