コード例 #1
0
        public IActionResult SellEquipment(int ninjaId, int equipmentId)
        {
            if (ninjaId == 0 || equipmentId == 0)
            {
                return(RedirectToAction("Index"));
            }

            var ninja     = _repo.GetOne(ninjaId);
            var equipment = _repo.GetEquipmentsFromNinja(ninja).FirstOrDefault(e => e.Id == equipmentId);

            if (ninja == null || equipment == null)
            {
                return(NotFound());
            }

            ninja.Gold += equipment.Cost;

            var newEquipments = _repo.GetEquipmentsFromNinja(ninja).Select(e => e.Id).ToList();

            newEquipments.Remove(equipment.Id);

            _repo.UpdateEquipments(ninja, newEquipments);

            _repo.Update(ninja);

            return(RedirectToAction("Details", new { id = ninjaId }));
        }
コード例 #2
0
        public IActionResult Buy(int id, int ninjaId)
        {
            if (id == 0 || ninjaId == 0)
            {
                return(RedirectToAction("Index"));
            }

            var equipment = _repo.GetOne(id);
            var ninja     = _ninjaRepo.GetOne(ninjaId);

            if (equipment == null || ninja == null)
            {
                return(NotFound());
            }

            if (ninja.Gold < equipment.Cost)
            {
                return(RedirectToAction("Index"));
            }

            if (_ninjaRepo.GetEquipmentsFromNinja(ninja).ToList().Any(e => e.Category == equipment.Category))
            {
                return(RedirectToAction("Index"));
            }

            ninja.Gold -= equipment.Cost;

            var newEquipments = _ninjaRepo.GetEquipmentsFromNinja(ninja).Select(e => e.Id).ToList();

            newEquipments.Add(equipment.Id);

            _ninjaRepo.UpdateEquipments(ninja, newEquipments);

            _ninjaRepo.Update(ninja);

            return(RedirectToAction("Details", "Ninja", new { id = ninjaId }));
        }