Example #1
0
        public async Task <IActionResult> Create([Bind("ItemId,Name,Category,Extra")] Items items)
        {
            if (ModelState.IsValid)
            {
                _context.Add(items);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(items));
        }
Example #2
0
        public async Task <IActionResult> SignOn(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var quests = await _context.Quests
                         .FirstOrDefaultAsync(m => m.QuestId == id);

            if (quests == null)
            {
                return(NotFound());
            }

            var userId = _context.Users
                         .Where(i => i.CredentialsId == User.FindFirstValue(ClaimTypes.NameIdentifier))
                         .Select(uid => uid.UserId)
                         .Single();

            ViewData["QuestTitle"] = quests.Title;
            if (_context.UsersQuests.Any(o => o.QuestId == quests.QuestId && o.UserId == userId))
            {
                ViewData["Exists"] = true;
                return(View());
            }

            UsersQuests usersQuests = new UsersQuests
            {
                UserId  = userId,
                QuestId = quests.QuestId,
                Status  = "In progress"
            };

            if (ModelState.IsValid)
            {
                _context.Add(usersQuests);
                await _context.SaveChangesAsync();
            }
            return(View());
        }
Example #3
0
        public async Task <IActionResult> Create([Bind("GroupId,Name,NumberOfPpl")] Groups groups)
        {
            if (ModelState.IsValid)
            {
                _context.Add(groups);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(groups));
        }
Example #4
0
        public async Task <IActionResult> BuyItem(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var itemToBuy = await _context.Items
                            .FirstOrDefaultAsync(m => m.ItemId == id);

            if (itemToBuy == null)
            {
                return(NotFound());
            }

            TempData["ItemInInventory"] = false;
            TempData["YouArePoor"]      = false;

            var loggedUser = _context.Users
                             .Single(i => i.CredentialsId == User.FindFirstValue(ClaimTypes.NameIdentifier));
            var userId  = loggedUser.UserId;
            var groupId = loggedUser.GroupId;

            var itemExists = false;

            if (itemToBuy.Category == "Magic")
            {
                if (_context.GroupsInventory.Any(o => o.ItemId == itemToBuy.ItemId && o.GroupId == groupId))
                {
                    if (!_context.GroupsInventory.Single(i => i.ItemId == id).ItemUsed)
                    {
                        TempData["ItemInInventory"] = true;
                        return(RedirectToAction("Index"));
                    }
                    itemExists = true;
                }
            }
            else
            {
                if (_context.UserInventory.Any(o => o.ItemId == itemToBuy.ItemId && o.UserId == userId))
                {
                    if (!_context.UserInventory.Single(i => i.ItemId == id).ItemUsed)
                    {
                        TempData["ItemInInventory"] = true;
                        return(RedirectToAction("Index"));
                    }
                    itemExists = true;
                }
            }

            var userBalance = _context.Wallet
                              .Single(i => i.UserId == userId);
            var group = _context.Groups
                        .Single(i => i.GroupId == groupId);
            var groupBalance = group.GroupBank;
            var itemInStore  = _context.Store
                               .Single(i => i.ItemId == itemToBuy.ItemId);

            GroupsInventory groupsInventory = new GroupsInventory();
            UserInventory   userInventory   = new UserInventory();

            if (itemToBuy.Category == "Magic")
            {
                if (groupBalance < itemInStore.Price)
                {
                    TempData["YouArePoor"] = true;
                    return(RedirectToAction("Index"));
                }
                groupsInventory.GroupId = groupId;
                groupsInventory.ItemId  = itemToBuy.ItemId;
            }
            else
            {
                if (userBalance.Balance < itemInStore.Price)
                {
                    TempData["YouArePoor"] = true;
                    return(RedirectToAction("Index"));
                }
                userInventory.UserId = userId;
                userInventory.ItemId = itemToBuy.ItemId;
            }

            if (ModelState.IsValid)
            {
                if (itemToBuy.Category == "Magic")
                {
                    var updatedBalance = group.GroupBank - itemToBuy.Store.Price;
                    group.GroupBank = updatedBalance;
                    if (!itemExists)
                    {
                        _context.Add(groupsInventory);
                    }
                    else
                    {
                        _context.GroupsInventory
                        .Single(i => i.ItemId == itemToBuy.ItemId && i.GroupId == groupId).ItemUsed = false;
                    }
                }
                else
                {
                    var updatedBalance = userBalance.Balance - itemToBuy.Store.Price;
                    userBalance.Balance = updatedBalance;
                    if (!itemExists)
                    {
                        _context.Add(userInventory);
                    }
                    else
                    {
                        _context.UserInventory
                        .Single(i => i.ItemId == itemToBuy.ItemId && i.UserId == userId).ItemUsed = false;
                    }
                }
                itemInStore.NumberAvailable -= 1;
                await _context.SaveChangesAsync();
            }
            TempData["ItemAdded"] = true;
            return(RedirectToAction("Index"));
        }