コード例 #1
0
ファイル: WagerController.cs プロジェクト: ismellike/Wagering
        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());
        }
コード例 #2
0
ファイル: SignalRHandler.cs プロジェクト: ismellike/Wagering
        public static async Task SendNotificationsAsync(ApplicationDbContext _context, IHubContext <GroupHub> _hubContext, IEnumerable <string> ids, List <PersonalNotification> notifications)
        {
            List <Connection> connections = await _context.Connections.AsNoTracking().Where(x => x.Connected).Where(x => ids.Contains(x.ProfileId)).ToListAsync();

            foreach (Connection connection in connections)
            {
                PersonalNotification notification = notifications.FirstOrDefault(x => x.ProfileId == connection.ProfileId);
                if (notification != null)
                {
                    await _hubContext.Clients.Client(connection.ConnectionId).SendAsync("ReceiveNotification", notification);
                }
            }
        }
コード例 #3
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());
        }
コード例 #4
0
        public async Task <IActionResult> DeleteNotification(int id)
        {
            string?userId = User.GetId();
            PersonalNotification notification = await _context.Notifications.Where(x => x.Id == id).FirstOrDefaultAsync();

            if (notification == null)
            {
                ModelState.AddModelError(string.Empty, _errorMessages.NotFound);
                return(BadRequest(ModelState));
            }
            if (notification.ProfileId != userId)
            {
                ModelState.AddModelError(string.Empty, _errorMessages.NotCorresponding);
                return(BadRequest(ModelState));
            }
            _context.Remove(notification);
            _context.SaveChanges();
            return(Ok());
        }
コード例 #5
0
        public static List <PersonalNotification> AddNotificationToUsers(ApplicationDbContext _context, IEnumerable <string> userIds, PersonalNotification notification)
        {
            List <PersonalNotification> notifications = new List <PersonalNotification>();

            foreach (string id in userIds)
            {
                PersonalNotification personalNotification = new PersonalNotification
                {
                    Date      = notification.Date,
                    DataModel = notification.DataModel,
                    Data      = notification.Data,
                    Message   = notification.Message,
                    ProfileId = id
                };
                notifications.Add(personalNotification);
            }
            if (notifications.Count > 0)
            {
                _context.Notifications.AddRange(notifications);
                _context.SaveChanges();
            }
            return(notifications);
        }
コード例 #6
0
ファイル: WagerController.cs プロジェクト: ismellike/Wagering
        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 }));
        }