public async Task SendGameInvite(GameInvite inviteRequest)
        {
            if (inviteRequest.hostId == inviteRequest.invitedId)
            {
                throw new SendToYourselfException();
            }

            Player player = await _playerRepository.GetByUsername(inviteRequest.invitedId);

            if (player == null)
            {
                throw new EntityNotFoundException();
            }

            PlayerFriend foundFriend = new PlayerFriend(inviteRequest.hostId, inviteRequest.invitedId);

            foundFriend = await _friendListRepository.GetByUsernames(foundFriend.playerId, foundFriend.friendId);

            if (foundFriend == null)
            {
                throw new FriendNotFoundException();
            }

            GameInvite foundInvite = await _gameInviteRepository.GetByRoomAndInvited(inviteRequest.invitedId, inviteRequest.roomId);

            if (foundInvite != null)
            {
                throw new AlreadyExistsGameInvite();
            }
            await _gameInviteRepository.Create(inviteRequest);
        }
        public async Task DeleteGameInvite(string invitedId, string roomId)
        {
            GameInvite foundInvite = await _gameInviteRepository.GetByRoomAndInvited(invitedId, roomId);

            if (foundInvite == null)
            {
                throw new GameInviteNotFoundException();
            }
            await _gameInviteRepository.Delete(invitedId, roomId);
        }
Ejemplo n.º 3
0
 public async Task Create(GameInvite request)
 {
     _context.GameInvites.Add(request);
     await _context.SaveChangesAsync();
 }
Ejemplo n.º 4
0
        public async Task <HttpResponseMessage> InviteToGame([FromUri] string gameId, [FromUri] string inviteUserId)
        {
            if (gameId == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Game id can not be null!"));
            }
            if (inviteUserId == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invite user can not be null!"));
            }

            var currentUserId = User.Identity.GetUserId();
            var inviteUser    = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == inviteUserId);

            if (inviteUser == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invite user not found!"));
            }

            var game = await _dbContext.Games
                       .Include(b => b.Owner)
                       .Include(b => b.GameApplication)
                       .Include(b => b.Players)
                       .FirstOrDefaultAsync(x => x.Id == gameId);

            if (game?.Owner == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Game not found!"));
            }
            if (game.Owner.Id != currentUserId)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You do not own this game!"));
            }
            if (game.Players.Any(x => x.Id == inviteUserId))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                   "Can not invite user to game when they are already a player!"));
            }

            var currentUser = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == currentUserId);

            var invite = new GameInvite
            {
                CreateDate = DateTime.Now,
                Expiry     = DateTime.Now.AddDays(14),
                Game       = game,
                Id         = Guid.NewGuid().ToString(),
                Invitee    = inviteUser,
                Inviter    = currentUser
            };

            _dbContext.GameInvites.Add(invite);
            await _dbContext.SaveChangesAsync();

            LobbyHub.NotifyUsers(new UserNotify
            {
                Event = new UserNotifyEvent
                {
                    Event = new
                    {
                        CreateInvite = invite
                    }
                }
            }, currentUserId, inviteUserId);

            return(Request.CreateResponse(HttpStatusCode.OK, invite));
        }