예제 #1
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);
        }
예제 #2
0
        public async Task DisconnectPlayerFromLobby(string username)
        {
            var currentLobby = lobbyStorage.GetLobbyOfUser(username);

            if (currentLobby != null)
            {
                currentLobby.DisconnectPlayer(username);
                if (currentLobby.Empty)
                {
                    lobbyStorage.RemoveLobby(currentLobby.Id);
                    await notificationService.NotifyAllAsync(client => client.LobbyDeleted(currentLobby.Id));

                    var invitedUsers = await context.Users.Where(x => currentLobby.InvitedPlayers.Contains(x.UserName))
                                       .ToListAsync();

                    await notificationPersistenceService.RemoveNotificationsOf(invitedUsers.Select(x => x.Id),
                                                                               x => x.Type == NotificationType.InviteReceived && x.Data == currentLobby.Id);
                }
                else
                {
                    await notificationService.NotifyAllAsync(client =>
                                                             client.LobbyChanged(mapper.Map <LobbyDataWrapper>(currentLobby)));
                }
            }
            else
            {
                throw new InvalidOperationException("You are not in a lobby.");
            }
        }