public ActionResult OpenContainer(int id) { if (id == 0) { return(RedirectToAction("PageNotFound", "Home")); } UserDTO user = _clientService.GetUserDTO(User.Identity.Name); ContainerToCountOpensDTO cont = user.Client.Containers.Where(x => x.Container.Id == id).FirstOrDefault(); ContainerOpensVM model = new ContainerOpensVM(); if (cont.CountOpens > 0) { UpCountOrders(id); ContainerOpens result = _clientService.OpenContainer(id, User.Identity.Name); model.Container = _mappers.ToProductDM.Map <ProductDTO, ProductDM>(result.Container); model.ContainerLeft = _mappers.ToProductDM.Map <ProductDTO, ProductDM>(result.ContainerLeft); model.ContainerRight = _mappers.ToProductDM.Map <ProductDTO, ProductDM>(result.ContainerRight); return(PartialView(model)); } model.Message = "У вас закончились кейсы"; return(PartialView(model)); }
public ContainerOpens OpenContainer(int id, string userName) { User user = Database.ClientManager.FindUser(x => x.UserName == userName); Product container = Database.Products.Get(id); Container cont = Database.Containers.Get(container.FromTableId); if (user == null || cont == null) { return(null); } int resultNumber = -1; int chanceForCommon = 100 - cont.ChanseForLegendary - cont.ChanseForRare; int chanceForRare = chanceForCommon + cont.ChanseForRare; ContainerOpens result = new ContainerOpens(); switch (cont.TypeOfHard) { case "Easy": resultNumber = random.Next(1, 100); result.Container = GetRandomProductFromContainer(chanceForCommon, chanceForRare, resultNumber, cont); resultNumber -= 10; result.ContainerLeft = GetRandomProductFromContainer(chanceForCommon, chanceForRare, resultNumber, cont); resultNumber += 20; result.ContainerRight = GetRandomProductFromContainer(chanceForCommon, chanceForRare, resultNumber, cont); break; case "Medium": resultNumber = random.Next(1, 1000); result.Container = GetRandomProductFromContainer(chanceForCommon * 10, chanceForRare * 10, resultNumber, cont); resultNumber -= 100; result.ContainerLeft = GetRandomProductFromContainer(chanceForCommon * 10, chanceForRare * 10, resultNumber, cont); resultNumber += 200; result.ContainerRight = GetRandomProductFromContainer(chanceForCommon * 10, chanceForRare * 10, resultNumber, cont); break; case "Hard": resultNumber = random.Next(1, 10000); result.Container = GetRandomProductFromContainer(chanceForCommon * 100, chanceForRare * 100, resultNumber, cont); resultNumber -= 1000; result.ContainerLeft = GetRandomProductFromContainer(chanceForCommon * 100, chanceForRare * 100, resultNumber, cont); resultNumber += 2000; result.ContainerRight = GetRandomProductFromContainer(chanceForCommon * 100, chanceForRare * 100, resultNumber, cont); break; case "Very hard": resultNumber = random.Next(1, 100000); result.Container = GetRandomProductFromContainer(chanceForCommon * 1000, chanceForRare * 1000, resultNumber, cont); resultNumber -= 10000; result.ContainerLeft = GetRandomProductFromContainer(chanceForCommon * 1000, chanceForRare * 1000, resultNumber, cont); resultNumber += 20000; result.ContainerRight = GetRandomProductFromContainer(chanceForCommon * 1000, chanceForRare * 1000, resultNumber, cont); break; case "Ultra hard": resultNumber = random.Next(1, 1000000); result.Container = GetRandomProductFromContainer(chanceForCommon * 10000, chanceForRare * 10000, resultNumber, cont); resultNumber -= 100000; result.ContainerLeft = GetRandomProductFromContainer(chanceForCommon * 10000, chanceForRare * 10000, resultNumber, cont); resultNumber += 200000; result.ContainerRight = GetRandomProductFromContainer(chanceForCommon * 10000, chanceForRare * 10000, resultNumber, cont); break; } if (user.Client.Products == null) { user.Client.Products = new List <ContainerToCountOpens>(); } bool flag = true; foreach (var i in user.Client.Products) { if (i.Container.Id == result.Container.Id) { i.CountOpens++; flag = false; } } if (flag) { Product prod = Database.Products.Get(result.Container.Id); user.Client.Products.Add(new ContainerToCountOpens() { Container = prod, CountOpens = 1 }); } if (user.Client.ContainerToCountOpens == null) { user.Client.ContainerToCountOpens = new List <ContainerToCountOpens>(); user.Client.ContainerToCountOpens.Add(new ContainerToCountOpens() { Container = container, CountOpens = 1 }); } else { flag = true; foreach (var i in user.Client.ContainerToCountOpens) { if (i.Container.Id == container.Id) { i.CountOpens++; flag = false; } } if (flag) { user.Client.ContainerToCountOpens.Add(new ContainerToCountOpens() { Container = container, CountOpens = 1 }); } } foreach (var i in user.Client.Containers) { if (i.Container.Id == container.Id) { i.CountOpens--; } } Database.ClientManager.Update(user); Database.Save(); return(result); }