Search in sources :

Example 1 with GameState

use of com.faforever.server.entity.GameState in project faf-java-server by FAForever.

the class GameService method changeGameState.

private void changeGameState(Game game, GameState newState) {
    GameState oldState = game.getState();
    if (oldState != GameState.CLOSED) {
        counterService.decrement(String.format(Metrics.GAMES_STATE_FORMAT, oldState));
    }
    game.setState(newState);
    if (newState != GameState.CLOSED) {
        counterService.increment(String.format(Metrics.GAMES_STATE_FORMAT, newState));
    }
}
Also used : GameState(com.faforever.server.entity.GameState)

Example 2 with GameState

use of com.faforever.server.entity.GameState in project faf-java-server by FAForever.

the class GameService method updatePlayerOption.

/**
 * Updates an option value of a specific player. Only the host of a game is allowed to report such options and for
 * unstarted games, otherwise an exception will be thrown.
 *
 * @throws RequestException if the reporting player is not the host, or if the game is not in state {@link
 * GameState#OPEN}
 */
public void updatePlayerOption(Player reporter, int playerId, String key, Object value) {
    Game game = reporter.getCurrentGame();
    if (game == null) {
        // Since this is called repeatedly, throwing exceptions here would not be a good idea. Happens after restarts.
        log.warn("Received player option for player w/o game: {}", reporter);
        return;
    }
    Requests.verify(game.getState() == GameState.OPEN, ErrorCode.INVALID_GAME_STATE, game.getState(), GameState.OPEN);
    Requests.verify(Objects.equals(reporter.getCurrentGame().getHost(), reporter), ErrorCode.HOST_ONLY_OPTION, key);
    if (!game.getConnectedPlayers().containsKey(playerId)) {
        log.warn("Player '{}' reported option '{}' with value '{}' for unknown player '{}' in game '{}'", reporter, key, value, playerId, game);
        return;
    }
    log.trace("Updating option for player '{}' in game '{}': '{}' = '{}'", playerId, game.getId(), key, value);
    game.getPlayerOptions().computeIfAbsent(playerId, id -> new HashMap<>()).put(key, value);
    markDirty(game, DEFAULT_MIN_DELAY, DEFAULT_MAX_DELAY);
}
Also used : Arrays(java.util.Arrays) ProgrammingError(com.faforever.server.error.ProgrammingError) ModVersion(com.faforever.server.entity.ModVersion) BiFunction(java.util.function.BiFunction) ArmyResult(com.faforever.server.entity.ArmyResult) PlayerOfflineEvent(com.faforever.server.player.PlayerOfflineEvent) PlayerService(com.faforever.server.player.PlayerService) Metrics(com.faforever.server.stats.Metrics) Player(com.faforever.server.entity.Player) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Pair(org.springframework.data.util.Pair) ArmyStatistics(com.faforever.server.stats.ArmyStatistics) ClientService(com.faforever.server.client.ClientService) CounterService(org.springframework.boot.actuate.metrics.CounterService) ConnectionAware(com.faforever.server.client.ConnectionAware) RatingService(com.faforever.server.rating.RatingService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) ErrorCode(com.faforever.server.error.ErrorCode) Streams(com.google.common.collect.Streams) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) GameState(com.faforever.server.entity.GameState) 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) Stream(java.util.stream.Stream) DivisionService(com.faforever.server.ladder1v1.DivisionService) RatingType(com.faforever.server.rating.RatingType) Game(com.faforever.server.entity.Game) GlobalRating(com.faforever.server.entity.GlobalRating) Entry(java.util.Map.Entry) Optional(java.util.Optional) RequestException(com.faforever.server.error.RequestException) Joiner(com.google.common.base.Joiner) ModService(com.faforever.server.mod.ModService) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) FeaturedMod(com.faforever.server.entity.FeaturedMod) Validity(com.faforever.server.entity.Validity) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) VictoryCondition(com.faforever.server.entity.VictoryCondition) GameResponses(com.faforever.server.client.GameResponses) Service(org.springframework.stereotype.Service) ServerProperties(com.faforever.server.config.ServerProperties) Iterator(java.util.Iterator) FeaturedModFileVersion(com.faforever.server.game.GameResponse.FeaturedModFileVersion) EntityManager(javax.persistence.EntityManager) Consumer(java.util.function.Consumer) SimMod(com.faforever.server.game.GameResponse.SimMod) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ArmyStatisticsService(com.faforever.server.stats.ArmyStatisticsService) PlayerOnlineEvent(com.faforever.server.player.PlayerOnlineEvent) GamePlayerStats(com.faforever.server.entity.GamePlayerStats) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) Game(com.faforever.server.entity.Game) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 3 with GameState

use of com.faforever.server.entity.GameState in project faf-java-server by FAForever.

the class GameService method restoreGameSession.

/**
 * Associates the specified player with the specified game, if this player was previously part of the game and the
 * game is still running. This is requested by the client after it lost connection to the server.
 */
public void restoreGameSession(Player player, int gameId) {
    if (player.getCurrentGame() != null) {
        log.warn("Player '{}' requested game session restoration but is still associated with game '{}'", player, player.getCurrentGame());
        return;
    }
    Optional<Game> gameOptional = getActiveGame(gameId);
    Requests.verify(gameOptional.isPresent(), ErrorCode.CANT_RESTORE_GAME_DOESNT_EXIST);
    Game game = gameOptional.get();
    GameState gameState = game.getState();
    Requests.verify(gameState == GameState.OPEN || gameState == GameState.PLAYING, ErrorCode.CANT_RESTORE_GAME_DOESNT_EXIST);
    Requests.verify(game.getState() != GameState.PLAYING || game.getPlayerStats().containsKey(player.getId()), ErrorCode.CANT_RESTORE_GAME_NOT_PARTICIPANT);
    log.debug("Reassociating player '{}' with game '{}'", player, game);
    player.setGameFuture(CompletableFuture.completedFuture(game));
    addPlayer(game, player);
    changePlayerGameState(player, PlayerGameState.INITIALIZING);
    changePlayerGameState(player, PlayerGameState.LOBBY);
    if (gameState == GameState.PLAYING) {
        changePlayerGameState(player, PlayerGameState.LAUNCHING);
    }
}
Also used : Game(com.faforever.server.entity.Game) GameState(com.faforever.server.entity.GameState)

Example 4 with GameState

use of com.faforever.server.entity.GameState in project faf-java-server by FAForever.

the class GameService method onGameEnded.

private void onGameEnded(Game game) {
    log.debug("Game ended: {}", game);
    GameState previousState = game.getState();
    game.setEndTime(Instant.now());
    try {
        changeGameState(game, GameState.ENDED);
    } catch (IllegalStateException e) {
        // Don't prevent a programming error from ending the game, but let us know about it.
        log.warn("Illegally tried to transition game '{}' from state '{}' to '{}'", game, previousState, GameState.ENDED, e);
    }
    // Games can also end before they even started, in which case we stop processing it
    if (previousState != GameState.PLAYING) {
        return;
    }
    game.getPlayerStats().values().forEach(stats -> {
        Player player = stats.getPlayer();
        armyStatisticsService.process(player, game, game.getArmyStatistics());
    });
    updateGameValidity(game);
    updateRatingsIfValid(game);
    Optional.ofNullable(game.getMapVersion()).ifPresent(mapVersion -> mapService.incrementTimesPlayed(mapVersion.getMap()));
    settlePlayerScores(game);
    updateDivisionScoresIfValid(game);
    gameRepository.save(game);
    if (game.getConnectedPlayers().isEmpty()) {
        onGameClosed(game);
    }
}
Also used : Player(com.faforever.server.entity.Player) GameState(com.faforever.server.entity.GameState)

Example 5 with GameState

use of com.faforever.server.entity.GameState in project faf-java-server by FAForever.

the class GameService method mutuallyAgreeDraw.

public void mutuallyAgreeDraw(Player player) {
    Requests.verify(player.getCurrentGame() != null, ErrorCode.NOT_IN_A_GAME);
    Game game = player.getCurrentGame();
    GameState gameState = game.getState();
    Requests.verify(gameState == GameState.PLAYING, ErrorCode.INVALID_GAME_STATE, GameState.PLAYING);
    getPlayerTeamId(player).filter(teamId -> OBSERVERS_TEAM_ID != teamId).ifPresent(teamId -> {
        log.debug("Adding player '{}' to mutually accepted draw list in game '{}'", player, game);
        game.getMutuallyAcceptedDrawPlayerIds().add(player.getId());
        final boolean allConnectedNonObserverPlayersAgreedOnMutualDraw = game.getConnectedPlayers().values().stream().filter(connectedPlayer -> !getPlayerTeamId(connectedPlayer).equals(Optional.of(OBSERVERS_TEAM_ID)) && !getPlayerTeamId(connectedPlayer).equals(Optional.empty())).allMatch(connectedPlayer -> game.getMutuallyAcceptedDrawPlayerIds().contains(connectedPlayer.getId()));
        if (allConnectedNonObserverPlayersAgreedOnMutualDraw) {
            log.debug("All in-game players agreed on mutual draw. Setting mutually agreed draw state in game '{}'", game);
            game.setMutuallyAgreedDraw(true);
        }
    });
}
Also used : Arrays(java.util.Arrays) ProgrammingError(com.faforever.server.error.ProgrammingError) ModVersion(com.faforever.server.entity.ModVersion) BiFunction(java.util.function.BiFunction) ArmyResult(com.faforever.server.entity.ArmyResult) PlayerOfflineEvent(com.faforever.server.player.PlayerOfflineEvent) PlayerService(com.faforever.server.player.PlayerService) Metrics(com.faforever.server.stats.Metrics) Player(com.faforever.server.entity.Player) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Pair(org.springframework.data.util.Pair) ArmyStatistics(com.faforever.server.stats.ArmyStatistics) ClientService(com.faforever.server.client.ClientService) CounterService(org.springframework.boot.actuate.metrics.CounterService) ConnectionAware(com.faforever.server.client.ConnectionAware) RatingService(com.faforever.server.rating.RatingService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) ErrorCode(com.faforever.server.error.ErrorCode) Streams(com.google.common.collect.Streams) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) GameState(com.faforever.server.entity.GameState) 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) Stream(java.util.stream.Stream) DivisionService(com.faforever.server.ladder1v1.DivisionService) RatingType(com.faforever.server.rating.RatingType) Game(com.faforever.server.entity.Game) GlobalRating(com.faforever.server.entity.GlobalRating) Entry(java.util.Map.Entry) Optional(java.util.Optional) RequestException(com.faforever.server.error.RequestException) Joiner(com.google.common.base.Joiner) ModService(com.faforever.server.mod.ModService) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) FeaturedMod(com.faforever.server.entity.FeaturedMod) Validity(com.faforever.server.entity.Validity) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) VictoryCondition(com.faforever.server.entity.VictoryCondition) GameResponses(com.faforever.server.client.GameResponses) Service(org.springframework.stereotype.Service) ServerProperties(com.faforever.server.config.ServerProperties) Iterator(java.util.Iterator) FeaturedModFileVersion(com.faforever.server.game.GameResponse.FeaturedModFileVersion) EntityManager(javax.persistence.EntityManager) Consumer(java.util.function.Consumer) SimMod(com.faforever.server.game.GameResponse.SimMod) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ArmyStatisticsService(com.faforever.server.stats.ArmyStatisticsService) PlayerOnlineEvent(com.faforever.server.player.PlayerOnlineEvent) GamePlayerStats(com.faforever.server.entity.GamePlayerStats) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) Game(com.faforever.server.entity.Game) GameState(com.faforever.server.entity.GameState)

Aggregations

GameState (com.faforever.server.entity.GameState)5 Game (com.faforever.server.entity.Game)3 Player (com.faforever.server.entity.Player)3 ClientService (com.faforever.server.client.ClientService)2 ConnectionAware (com.faforever.server.client.ConnectionAware)2 GameResponses (com.faforever.server.client.GameResponses)2 ServerProperties (com.faforever.server.config.ServerProperties)2 ArmyResult (com.faforever.server.entity.ArmyResult)2 FeaturedMod (com.faforever.server.entity.FeaturedMod)2 GamePlayerStats (com.faforever.server.entity.GamePlayerStats)2 GlobalRating (com.faforever.server.entity.GlobalRating)2 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)2 ModVersion (com.faforever.server.entity.ModVersion)2 Validity (com.faforever.server.entity.Validity)2 VictoryCondition (com.faforever.server.entity.VictoryCondition)2 ErrorCode (com.faforever.server.error.ErrorCode)2 ProgrammingError (com.faforever.server.error.ProgrammingError)2 RequestException (com.faforever.server.error.RequestException)2 Requests (com.faforever.server.error.Requests)2 FeaturedModFileVersion (com.faforever.server.game.GameResponse.FeaturedModFileVersion)2