Exemple #1
0
        public async Task <LobbyDataWrapper> InvitePlayerToLobby(Guid lobbyId, string player)
        {
            var lobby = lobbyStorage.GetLobby(lobbyId);

            if (lobby.Host != identityService.GetCurrentUserName())
            {
                throw new UnauthorizedAccessException("Not authorized to invite to this lobby.");
            }

            if (lobby.InvitedPlayers.Contains(player))
            {
                throw new InvalidOperationException("This player has already been invited.");
            }

            var invitedUser = await context.Users.CustomSingleAsync(x => x.UserName == player, "No such player found.");

            lobby.InvitedPlayers.Add(player);
            lobby.LastModified = DateTime.UtcNow;

            var wrapper = mapper.Map <LobbyDataWrapper>(lobby);
            await notificationService.NotifyAsync(player,
                                                  client => client.ReceiveLobbyInvite(wrapper));

            await notificationService.NotifyAllExceptAsync(identityService.GetCurrentUserName(),
                                                           client => client.LobbyChanged(wrapper));

            await notificationPersistenceService.PersistNotificationAsync(NotificationType.InviteReceived,
                                                                          invitedUser.Id,
                                                                          identityService.GetCurrentUserId(),
                                                                          lobby.Id);

            return(wrapper);
        }
Exemple #2
0
        public async Task <FriendDto> AcceptRequestAsync(Guid requestId)
        {
            var currentUser = identityService.GetCurrentUserName();
            var request     = await context.Requests.Include(r => r.Sender)
                              .Include(r => r.Receiver)
                              .CustomSingleAsync(r => r.Id == requestId, "No friend request found with the given id.");

            if (request.Receiver.UserName != currentUser)
            {
                throw new UnauthorizedAccessException("You can not accept someone else's friend request.");
            }

            var friendship = new Friendship
            {
                User1 = request.Sender,
                User2 = request.Receiver
            };

            context.Friendships.Add(friendship);
            context.Requests.Remove(request);
            await context.SaveChangesAsync();

            await notificationService.NotifyAsync(friendship.User1.UserName,
                                                  client => client.FriendAdded(new FriendDto
            {
                FriendshipId = friendship.Id,
                IsOnline     = onlineUserTracker.IsOnline(friendship.User2.UserName),
                Username     = friendship.User2.UserName
            }));

            await notificationPersistenceService.PersistNotificationAsync(NotificationType.FriendRequestAccepted,
                                                                          request.Sender.Id,
                                                                          request.Receiver.Id);

            return(new FriendDto
            {
                FriendshipId = friendship.Id,
                IsOnline = onlineUserTracker.IsOnline(friendship.User1.UserName),
                Username = friendship.User1.UserName
            });
        }