コード例 #1
0
ファイル: MessageService.cs プロジェクト: koppa96/czeum
        public async Task <Message> SendToMatchAsync(Guid matchId, string message)
        {
            var senderId = identityService.GetCurrentUserId();
            var match    = await context.Matches.Include(m => m.Users)
                           .ThenInclude(um => um.User)
                           .CustomSingleAsync(m => m.Id == matchId);

            if (match.Users.All(um => um.UserId != senderId))
            {
                throw new UnauthorizedAccessException("Not authorized to send message to this match.");
            }

            var senderUser = await context.Users.FindAsync(senderId);

            var storedMessage = new StoredMessage
            {
                Id        = Guid.NewGuid(),
                Sender    = senderUser,
                Match     = match,
                Text      = message,
                Timestamp = DateTime.UtcNow
            };

            context.MatchMessages.Add(storedMessage);
            await context.SaveChangesAsync();

            var sentMessage = mapper.Map <Message>(storedMessage);
            await notificationService.NotifyAsync(match.Others(identityService.GetCurrentUserName()),
                                                  client => client.ReceiveMatchMessage(match.Id, sentMessage));

            return(sentMessage);
        }
コード例 #2
0
        private async Task <Dictionary <string, MatchStatus> > CreateMatchWithBoardAsync(IEnumerable <string> players, SerializedBoard board, bool isQuickMatch)
        {
            var users = await context.Users.Where(u => players.Any(p => p == u.UserName))
                        .ToListAsync();

            var match = new Match
            {
                Board = board,
                CurrentPlayerIndex = 0,
                IsQuickMatch       = isQuickMatch,
                CreateTime         = DateTime.UtcNow,
                LastMove           = DateTime.UtcNow
            };

            match.Users = Enumerable.Range(0, users.Count)
                          .Select(x => new UserMatch {
                User = users[x], Match = match, PlayerIndex = x
            })
                          .ToList();

            context.Matches.Add(match);
            context.Boards.Add(board);
            await context.SaveChangesAsync();

            return(match.Users.Select(um => new { Player = um.User.UserName, Status = matchConverter.ConvertFor(match, um.User.UserName) })
                   .ToDictionary(x => x.Player, x => x.Status));
        }
コード例 #3
0
ファイル: UserService.cs プロジェクト: koppa96/czeum
        public async Task UpdateLastDisconnectDate(string username)
        {
            var user = await context.Users.SingleAsync(x => x.UserName == username);

            user.LastDisconnected = DateTime.UtcNow;
            await context.SaveChangesAsync();
        }
コード例 #4
0
        public async Task DeleteNotificationAsync(Guid notificationId)
        {
            var notification = await context.Notifications.FindAsync(notificationId);

            context.Notifications.Remove(notification);
            await context.SaveChangesAsync();
        }
コード例 #5
0
        public async Task <AchivementDto> StarAchivementAsync(Guid userAchivementId)
        {
            var currentUserId  = identityService.GetCurrentUserId();
            var userAchivement = await context.UserAchivements
                                 .Include(x => x.Achivement)
                                 .CustomSingleAsync(x => x.Id == userAchivementId, "No such achivement exists.");

            if (userAchivement.UserId != currentUserId)
            {
                throw new UnauthorizedAccessException("Can not star other user's achivements.");
            }

            userAchivement.IsStarred = true;
            await context.SaveChangesAsync();

            return(mapper.Map <AchivementDto>(userAchivement));
        }
コード例 #6
0
ファイル: FriendService.cs プロジェクト: koppa96/czeum
        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
            });
        }
コード例 #7
0
        public async Task PersistNotificationAsync(NotificationType notificationType, Guid receiverId, Guid?senderId = null, Guid?data = null)
        {
            var receiver = await context.Users.FindAsync(receiverId);

            var notification = new Notification
            {
                Type           = notificationType,
                ReceiverUserId = receiverId,
                SenderUserId   = senderId,
                Data           = data
            };

            context.Notifications.Add(notification);
            await context.SaveChangesAsync();

            await notificationService.NotifyAsync(receiver.UserName, async client => await client.NotificationReceived(new NotificationDto
            {
                Id             = notification.Id,
                Data           = notification.Data,
                SenderUserName = senderId != null ? (await context.Users.FindAsync(senderId)).UserName : null,
                Type           = notificationType
            }));
        }