Search in sources :

Example 1 with ErrorCode

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

the class V2ServerMessageMapper method map.

default ErrorServerMessage map(ErrorResponse source) {
    ErrorCode errorCode = source.getErrorCode();
    Object[] args = source.getArgs();
    return new ErrorServerMessage(errorCode.getCode(), MessageFormat.format(errorCode.getTitle(), args), MessageFormat.format(errorCode.getDetail(), args), Optional.ofNullable(source.getRequestId()).map(UUID::toString).orElse(null), args);
}
Also used : ErrorCode(com.faforever.server.error.ErrorCode) UUID(java.util.UUID)

Example 2 with ErrorCode

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

the class ErrorResponseTransformerTest method transform.

@Test
public void transform() throws Exception {
    ErrorCode errorCode = ErrorCode.UNSUPPORTED_REQUEST;
    ErrorResponse errorResponse = new ErrorResponse(errorCode, UUID.randomUUID(), new Object[] { "foobar" });
    Map<String, Serializable> result = ErrorResponseTransformer.INSTANCE.transform(errorResponse);
    assertThat(result.get("command"), is("notice"));
    assertThat(result.get("style"), is("error"));
    assertThat(result.get("code"), is(errorCode.getCode()));
    assertThat(result.get("text"), is(MessageFormat.format(errorCode.getTitle() + ": " + errorCode.getDetail(), "foobar")));
}
Also used : Serializable(java.io.Serializable) ErrorCode(com.faforever.server.error.ErrorCode) ErrorResponse(com.faforever.server.error.ErrorResponse) Test(org.junit.Test)

Example 3 with ErrorCode

use of com.faforever.server.error.ErrorCode 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)

Aggregations

ErrorCode (com.faforever.server.error.ErrorCode)3 UUID (java.util.UUID)2 ClientService (com.faforever.server.client.ClientService)1 ConnectionAware (com.faforever.server.client.ConnectionAware)1 ServerProperties (com.faforever.server.config.ServerProperties)1 FeaturedMod (com.faforever.server.entity.FeaturedMod)1 Game (com.faforever.server.entity.Game)1 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)1 MapVersion (com.faforever.server.entity.MapVersion)1 Player (com.faforever.server.entity.Player)1 ErrorResponse (com.faforever.server.error.ErrorResponse)1 RequestException (com.faforever.server.error.RequestException)1 Requests (com.faforever.server.error.Requests)1 Faction (com.faforever.server.game.Faction)1 GameService (com.faforever.server.game.GameService)1 GameVisibility (com.faforever.server.game.GameVisibility)1 MapService (com.faforever.server.map.MapService)1 ModService (com.faforever.server.mod.ModService)1 PlayerService (com.faforever.server.player.PlayerService)1 RatingService (com.faforever.server.rating.RatingService)1