Esempio n. 1
0
        public async Task <LobbyDataWrapper> JoinToLobbyAsync(Guid lobbyId)
        {
            var currentUser = identityService.GetCurrentUserName();
            var userLobby   = lobbyStorage.GetLobbyOfUser(currentUser);

            if (userLobby != null || soloQueueService.IsQueuing(currentUser))
            {
                throw new InvalidOperationException("You can only join a lobby if you are not queuing and not in an other lobby.");
            }

            var lobby = lobbyStorage.GetLobby(lobbyId);

            var friends = await context.Friendships
                          .Where(f => f.User1.UserName == lobby.Host || f.User2.UserName == lobby.Host)
                          .Select(f => f.User1.UserName == lobby.Host ? f.User2.UserName : f.User1.UserName)
                          .ToListAsync();

            var wrapper = mapper.Map <LobbyDataWrapper>(lobby);

            lobby.JoinGuest(currentUser, friends);
            await notificationService.NotifyAllExceptAsync(currentUser,
                                                           client => client.LobbyChanged(wrapper));

            return(wrapper);
        }
Esempio n. 2
0
        public async Task <MatchStatus> CreateMatchAsync(Guid lobbyId)
        {
            var currentUser = identityService.GetCurrentUserName();
            var lobby       = lobbyStorage.GetLobby(lobbyId);

            if (lobby.Host != currentUser)
            {
                throw new UnauthorizedAccessException("Only the host can create a match.");
            }

            if (!lobby.Validate())
            {
                throw new InvalidOperationException("The lobby is not in a valid state to start a match.");
            }

            var service = serviceContainer.FindBoardCreator(lobby);
            var board   = service.CreateBoard(lobby);

            var statuses = await CreateMatchWithBoardAsync(lobby.Guests.Append(lobby.Host), board, false);

            lobbyStorage.RemoveLobby(lobbyId);

            await notificationService.NotifyEachAsync(statuses
                                                      .Where(s => s.Key != currentUser)
                                                      .Select(x => new KeyValuePair <string, Func <ICzeumClient, Task> >(x.Key, client => client.MatchCreated(x.Value))));

            return(statuses.Single(s => s.Key == currentUser).Value);
        }
Esempio n. 3
0
        public async Task <Message> SendToLobbyAsync(Guid lobbyId, string message)
        {
            var sender = identityService.GetCurrentUserName();
            var lobby  = lobbyStorage.GetLobby(lobbyId);

            if (lobby.Host != sender && !lobby.Guests.Contains(sender))
            {
                throw new UnauthorizedAccessException("Not authorized to send message to this lobby.");
            }

            var msg = new Message
            {
                Id        = Guid.NewGuid(),
                Sender    = sender,
                Text      = message,
                Timestamp = DateTime.UtcNow
            };

            lobbyStorage.AddMessage(lobbyId, msg);
            await notificationService.NotifyAsync(lobby.Others(sender),
                                                  client => client.ReceiveLobbyMessage(lobbyId, msg));

            return(msg);
        }