Esempio n. 1
0
        public async Task <IActionResult> AddWeapon(AdminAddWeaponViewModel model)
        {
            if (ModelState.IsValid)
            {
                var weapon = new CraftableWeapon
                {
                    Id               = Guid.NewGuid().ToString(),
                    Name             = model.Name,
                    Type             = model.Type,
                    MaterialRequired = model.MaterialRequired,
                    MaterialCount    = model.MaterialCount,
                    Damage           = model.Damage,
                    CraftCost        = model.Cost
                };

                await _db.CraftableWeapons.AddAsync(weapon);

                await _db.SaveChangesAsync();

                return(this.RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Invalid armor entry.");
            return(this.View(model));
        }
Esempio n. 2
0
        public async Task SeedCraftableWeapons(ApplicationDbContext context)
        {
            var weapon1 = new CraftableWeapon()
            {
                Id               = "Id1",
                Name             = "Test weapon 1",
                Type             = WeaponTypes.Axe,
                MaterialRequired = Materials.Copper,
                MaterialCount    = 5,
                Damage           = 10,
                CraftCost        = 50,
            };

            var weapon2 = new CraftableWeapon()
            {
                Id               = "Id2",
                Name             = "Test weapon 2",
                Type             = WeaponTypes.Staff,
                MaterialRequired = Materials.Mithril,
                MaterialCount    = 10,
                Damage           = 20,
                CraftCost        = 100,
            };

            await context.CraftableWeapons.AddAsync(weapon1);

            await context.CraftableWeapons.AddAsync(weapon2);

            await context.SaveChangesAsync();
        }
        public ActionResult CraftWeapon(int WeaponId, string returnUrl)
        {
            Character character = db.Characters.Where(x => x.ApplicationUserID == User.Identity.Name).FirstOrDefault();

            CraftableWeapon craftableWeapon = db.CraftableWeapons.Find(WeaponId);


            List <CraftableWeaponMaterial> allCraftableWeaponMaterials = db.CraftableWeaponMaterials.Where(x => x.WeaponID == WeaponId).ToList();

            List <CraftableWeaponMaterial> craftableWeaponMaterials = db.CraftableWeaponMaterials.Where(x => x.WeaponID == WeaponId).ToList();

            Building building = new Building();

            int counter = 0;

            foreach (var mat in allCraftableWeaponMaterials)
            {
                foreach (var b in db.Buildings)
                {
                    if (b.ItemID == mat.MaterialID)
                    {
                        craftableWeaponMaterials.Remove(mat);
                    }
                }
            }



            foreach (var invMat in character.Inventory)
            {
                foreach (var weapMat in craftableWeaponMaterials)
                {
                    if (invMat.ItemID == weapMat.MaterialID)
                    {
                        if (invMat.ItemPieces >= weapMat.MaterialPieces)
                        {
                            counter++;
                        }
                    }
                }
            }


            if (counter == craftableWeaponMaterials.Count())
            {
                foreach (var invMat in character.Inventory)
                {
                    foreach (var weapMat in craftableWeaponMaterials)
                    {
                        if (invMat.ItemID == weapMat.MaterialID && invMat.ItemID != building.ItemID)
                        {
                            invMat.ItemPieces -= weapMat.MaterialPieces;
                        }
                    }
                }



                if (character.Inventory.Where(x => x.ItemID == WeaponId).FirstOrDefault() != null)
                {
                    Inventory inventory = character.Inventory.Where(x => x.ItemID == WeaponId).FirstOrDefault();
                    inventory.ItemPieces++;
                }
                else
                {
                    Inventory inventory = new Inventory {
                        ItemID = WeaponId, CharacterID = character.CharacterID, ItemPieces = 1, ItemCurrentDurability = craftableWeapon.ItemMaxDurability, ItemMaxDurability = craftableWeapon.ItemMaxDurability
                    };
                    db.Inventories.Add(inventory);
                }


                db.SaveChanges();
            }
            return(Redirect(returnUrl));
        }