Ejemplo n.º 1
0
        public async Task <IActionResult> CancelWager(int id)
        {
            string?userId   = User.GetId();
            string?userName = User.GetName();
            Wager  wager    = await _context.Wagers.Where(x => x.Id == id).Include(x => x.Hosts).FirstOrDefaultAsync();

            if (wager == null)
            {
                ModelState.AddModelError(string.Empty, _errorMessages.NotFound);
                return(BadRequest(ModelState));
            }
            if (wager.Status != (byte)Status.Created)
            {
                ModelState.AddModelError(string.Empty, "Wager is not in the created.");
                return(BadRequest(ModelState));
            }
            if (!wager.Hosts.Any(x => x.ProfileId == userId))
            {
                ModelState.AddModelError(string.Empty, "You are not a host of this wager.");
                return(BadRequest(ModelState));
            }
            wager.Status = (byte)Status.Canceled;
            PersonalNotification notification = new PersonalNotification
            {
                Date      = DateTime.Now,
                Message   = $"{userName} has canceled the wager.",
                Data      = wager.Id.ToString(),
                DataModel = (byte)DataModel.Wager
            };
            List <PersonalNotification> notifications = NotificationHandler.AddNotificationToUsers(_context, wager.HostIds(), notification);
            await SignalRHandler.SendNotificationsAsync(_context, _hubContext, wager.HostIds(), notifications);

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AcceptBid(int id)
        {
            string?userId   = User.GetId();
            string?userName = User.GetName();

            var bid = await _context.WagerHostBids.Where(x => x.Id == id).Include(x => x.Wager).ThenInclude(x => x.Hosts).FirstOrDefaultAsync();

            if (bid == null)
            {
                ModelState.AddModelError(string.Empty, _errorMessages.NotFound);
                return(BadRequest(ModelState));
            }
            if (bid.Wager.Status != (byte)Status.Created)
            {
                ModelState.AddModelError(string.Empty, "Wager is not in the created state.");
                return(BadRequest(ModelState));
            }
            if (bid.ProfileId != userId)
            {
                ModelState.AddModelError(string.Empty, _errorMessages.NotCorresponding);
                return(BadRequest(ModelState));
            }
            if (bid.Approved != null)
            {
                ModelState.AddModelError(string.Empty, _errorMessages.AlreadySent);
                return(BadRequest(ModelState));
            }

            bid.Approved = true;
            if (bid.Wager != null)
            {
                PersonalNotification notification = new PersonalNotification
                {
                    Date      = DateTime.Now,
                    Data      = bid.Wager.Id.ToString(),
                    DataModel = (byte)DataModel.Wager
                };
                if (bid.Wager.IsApproved())
                {
                    bid.Wager.Status     = (byte)Status.Confirmed;
                    notification.Message = $"{userName} has accepted and confirmed the wager.";
                }
                else
                {
                    notification.Message = $"{userName} has accepted the wager.";
                }
                List <PersonalNotification> notifications = NotificationHandler.AddNotificationToUsers(_context, bid.Wager.HostIds(), notification);
                await SignalRHandler.SendNotificationsAsync(_context, _hubContext, bid.Wager.HostIds(), notifications);
            }
            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PostWager(Wager wagerData)
        {
            string?userId   = User.GetId();
            string?userName = User.GetName();
            string?userKey  = User.GetKey();

            if (userKey == null)
            {
                ModelState.AddModelError(string.Empty, "User does not have a public key registered.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            WagerHostBid caller = wagerData.Hosts.FirstOrDefault(x => x.ProfileId == userId);

            if (wagerData.Hosts.Sum(x => x.ReceivablePt) != 100)
            {
                ModelState.AddModelError(string.Empty, "The hosts receivable percentages do not add up to 100.");
            }
            if (wagerData.Hosts.Sum(x => x.PayablePt) != 100)
            {
                ModelState.AddModelError(string.Empty, "The hosts payable percentages do not add up to 100.");
            }
            if (caller == null)
            {
                ModelState.AddModelError(string.Empty, "Caller must be a host.");
            }
            else if (!caller.IsOwner)
            {
                ModelState.AddModelError(string.Empty, "Caller must be the owner.");
            }
            if (!wagerData.HostIds().IsUnique())
            {
                ModelState.AddModelError(string.Empty, "The hosts are not unique.");
            }
            try
            {
                if (wagerData.Hosts.Single(x => x.IsOwner) == null)
                {
                    throw new Exception("Only 1 owner should be specified.");
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            DateTime date  = DateTime.Now;
            Wager    wager = new Wager //prevents overposting
            {
                GameId         = wagerData.GameId,
                Date           = date,
                Description    = wagerData.Description,
                MinimumWager   = wagerData.MinimumWager,
                MaximumWager   = wagerData.MaximumWager,
                IsPrivate      = wagerData.IsPrivate,
                Status         = 0,
                Hosts          = new List <WagerHostBid>(),
                ChallengeCount = 0
            };

            foreach (WagerHostBid host in wagerData.Hosts)
            {
                WagerHostBid bid = new WagerHostBid
                {
                    Approved     = null,
                    IsOwner      = false,
                    ReceivablePt = host.ReceivablePt,
                    PayablePt    = host.PayablePt,
                    ProfileId    = host.ProfileId
                };
                if (host.IsOwner)
                {
                    bid.Approved = true;
                    bid.IsOwner  = true;
                }
                wager.Hosts.Add(bid);
            }

            if (wager.IsApproved())
            {
                wager.Status = (byte)Status.Confirmed;
            }

            _context.Wagers.Add(wager);
            _context.SaveChanges();
            PersonalNotification notification = new PersonalNotification
            {
                Date      = date,
                Message   = $"{userName} created a wager with you.",
                Data      = wager.Id.ToString(),
                DataModel = (byte)DataModel.Wager
            };
            IEnumerable <string>        others        = wager.HostIds().Where(x => x != userId);
            List <PersonalNotification> notifications = NotificationHandler.AddNotificationToUsers(_context, others, notification);
            await SignalRHandler.SendNotificationsAsync(_context, _hubContext, others, notifications);

            return(Ok(new { id = wager.Id, status = wager.Status }));
        }