Search in sources :

Example 6 with RequestException

use of com.faforever.server.error.RequestException in project faf-java-server by FAForever.

the class MatchMakerService method createMatch.

/**
 * <p>Creates a new match with the specified options and participants. All participants must be online and available
 * for matchmaking. A player can be unavailable for matchmaking if, for instance, he's currently playing a game or
 * offline. In this case, a {@link ErrorCode#PLAYER_NOT_AVAILABLE_FOR_MATCHMAKING} is thrown.</p>
 *
 * @throws RequestException if a player is not available for matchmaking or the map to be played is unknown by the
 * server.
 */
public void createMatch(ConnectionAware requester, UUID requestId, String title, String featuredMod, List<MatchParticipant> participants, int mapVersionId) {
    log.debug("Creating match '{}' with '{}' participants on map '{}'", title, participants.size(), mapVersionId);
    Requests.verify(participants.size() > 1, ErrorCode.INSUFFICIENT_MATCH_PARTICIPANTS, participants.size(), 2);
    List<Player> players = participants.stream().map(matchParticipant -> playerService.getOnlinePlayer(matchParticipant.getId()).orElseThrow(() -> new RequestException(requestId, ErrorCode.PLAYER_NOT_AVAILABLE_FOR_MATCHMAKING, matchParticipant.getId()))).peek(player -> Requests.verify(player.getCurrentGame() == null, requestId, ErrorCode.PLAYER_NOT_AVAILABLE_FOR_MATCHMAKING, player)).peek(this::removePlayer).collect(Collectors.toList());
    String mapFileName = mapService.findMap(mapVersionId).map(MapVersion::getFilename).orElseThrow(() -> new RequestException(requestId, ErrorCode.UNKNOWN_MAP, mapVersionId));
    Player host = players.get(0);
    List<Player> guests = players.subList(1, players.size());
    gameService.createGame(title, featuredMod, mapFileName, null, GameVisibility.PRIVATE, null, null, host).handle((game, throwable) -> {
        if (throwable != null) {
            log.debug("The host of match '{}' failed to start his game", title, throwable);
            throw new RequestException(requestId, ErrorCode.HOST_FAILED_TO_START_GAME, title, host);
        }
        AtomicInteger counter = new AtomicInteger();
        Integer hostId = host.getId();
        setPlayerOptionsForMatchParticipant(participants, host, counter, hostId);
        log.trace("Host '{}' for match '{}' is ready", host, title);
        clientService.sendMatchCreatedNotification(requestId, game.getId(), requester);
        List<CompletableFuture<Game>> guestGameFutures = guests.stream().peek(player -> log.trace("Telling player '{}' to start the game process for match '{}'", player, title)).map(player -> gameService.joinGame(game.getId(), null, player).thenApply(gameStartedFuture -> {
            setPlayerOptionsForMatchParticipant(participants, host, counter, player.getId());
            return gameStartedFuture;
        })).collect(Collectors.toList());
        return CompletableFuture.allOf(guestGameFutures.toArray(new CompletableFuture[guests.size()])).thenAccept(aVoid -> log.debug("All players launched their game for match '{}'", title)).exceptionally(throwable1 -> {
            log.debug("At least one player failed to launch their game for match '{}'", title, throwable1);
            return null;
        });
    });
}
Also used : ModService(com.faforever.server.mod.ModService) PlayerService(com.faforever.server.player.PlayerService) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) GameVisibility(com.faforever.server.game.GameVisibility) Scheduled(org.springframework.scheduling.annotation.Scheduled) FeaturedMod(com.faforever.server.entity.FeaturedMod) Value(lombok.Value) Player(com.faforever.server.entity.Player) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) Duration(java.time.Duration) Map(java.util.Map) ServerProperties(com.faforever.server.config.ServerProperties) ClientService(com.faforever.server.client.ClientService) ConnectionAware(com.faforever.server.client.ConnectionAware) RatingService(com.faforever.server.rating.RatingService) Set(java.util.Set) ErrorCode(com.faforever.server.error.ErrorCode) Faction(com.faforever.server.game.Faction) GameService(com.faforever.server.game.GameService) UUID(java.util.UUID) EqualsAndHashCode(lombok.EqualsAndHashCode) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) MapService(com.faforever.server.map.MapService) Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Requests(com.faforever.server.error.Requests) Game(com.faforever.server.entity.Game) MapVersion(com.faforever.server.entity.MapVersion) Optional(java.util.Optional) RequestException(com.faforever.server.error.RequestException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AllArgsConstructor(lombok.AllArgsConstructor) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Player(com.faforever.server.entity.Player) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestException(com.faforever.server.error.RequestException)

Example 7 with RequestException

use of com.faforever.server.error.RequestException in project faf-java-server by FAForever.

the class LegacyServicesActivators method loginRequest.

@ServiceActivator(inputChannel = ChannelNames.LEGACY_LOGIN_REQUEST)
@Transactional
public void loginRequest(LegacyLoginRequest loginRequest, @Header(CLIENT_CONNECTION) ClientConnection clientConnection) {
    // TODO this method shouldn't do anything but call a service
    Requests.verify(!playerService.isPlayerOnline(loginRequest.getLogin()), ErrorCode.USER_ALREADY_CONNECTED, loginRequest.getLogin());
    log.debug("Processing login request from user: {}", loginRequest.getLogin());
    try {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(loginRequest.getLogin(), loginRequest.getPassword());
        Authentication authentication = authenticationManager.authenticate(token);
        FafUserDetails userDetails = (FafUserDetails) authentication.getPrincipal();
        clientConnection.setAuthentication(authentication);
        Player player = userDetails.getPlayer();
        player.setClientConnection(clientConnection);
        geoIpService.lookupCountryCode(clientConnection.getClientAddress()).ifPresent(player::setCountry);
        uniqueIdService.verify(player, loginRequest.getUniqueId());
        chatService.updateIrcPassword(userDetails.getUsername(), loginRequest.getPassword());
        playerService.setPlayerOnline(player);
    } catch (BadCredentialsException e) {
        throw new RequestException(e, ErrorCode.INVALID_LOGIN);
    }
}
Also used : Player(com.faforever.server.entity.Player) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) FafUserDetails(com.faforever.server.security.FafUserDetails) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) RequestException(com.faforever.server.error.RequestException) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with RequestException

use of com.faforever.server.error.RequestException in project faf-java-server by FAForever.

the class V2ClientMessageTransformer method transform.

@Override
@SneakyThrows
public ClientMessage transform(String source) {
    try {
        V2ClientMessageWrapper wrapper = objectMapper.readValue(source, V2ClientMessageWrapper.class);
        V2ClientMessage message = wrapper.getData();
        Requests.verify(message != null, ErrorCode.UNSUPPORTED_REQUEST, source);
        Method mappingMethod = mapperMethods.get(message.getClass());
        Requests.verify(mappingMethod != null, ErrorCode.UNSUPPORTED_REQUEST, source);
        return (ClientMessage) mappingMethod.invoke(v2ClientMessageMapper, message);
    } catch (JsonMappingException e) {
        throw new RequestException(e, ErrorCode.UNSUPPORTED_REQUEST, source, e.getMessage());
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Method(java.lang.reflect.Method) ClientMessage(com.faforever.server.common.ClientMessage) RequestException(com.faforever.server.error.RequestException) SneakyThrows(lombok.SneakyThrows)

Aggregations

RequestException (com.faforever.server.error.RequestException)8 Game (com.faforever.server.entity.Game)4 Player (com.faforever.server.entity.Player)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Transactional (org.springframework.transaction.annotation.Transactional)4 ClientService (com.faforever.server.client.ClientService)3 ConnectionAware (com.faforever.server.client.ConnectionAware)3 ServerProperties (com.faforever.server.config.ServerProperties)3 FeaturedMod (com.faforever.server.entity.FeaturedMod)3 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)3 ErrorCode (com.faforever.server.error.ErrorCode)3 ProgrammingError (com.faforever.server.error.ProgrammingError)3 Requests (com.faforever.server.error.Requests)3 MapService (com.faforever.server.map.MapService)3 ModService (com.faforever.server.mod.ModService)3 PlayerService (com.faforever.server.player.PlayerService)3 RatingService (com.faforever.server.rating.RatingService)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Duration (java.time.Duration)3 Instant (java.time.Instant)3