Search in sources :

Example 41 with Game

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

the class GameService method getPlayerTeamId.

private Optional<Integer> getPlayerTeamId(Player player) {
    Optional<Game> gameOptional = Optional.ofNullable(player.getCurrentGame());
    if (!gameOptional.isPresent()) {
        return Optional.empty();
    }
    Game game = gameOptional.get();
    return Optional.ofNullable((Integer) game.getPlayerOptions().get(player.getId()).get(OPTION_TEAM));
}
Also used : Game(com.faforever.server.entity.Game)

Example 42 with Game

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

the class GameService method endGameIfArmyResultsComplete.

/**
 * Checks whether a game has ended and, if so, calls {@link #onGameEnded(Game)}. As the game doesn't yet send a clear
 * "game ended" command yet, a game is considered as ended when the following conditions are met: <ol><li>Every
 * connected player reported an army outcome for all armies</li> <li>Every survivor reported a score for all armies
 * (defeated players do not send scores)</li></ol>(human and AI).
 */
// TODO simplify once https://github.com/FAForever/fa/issues/2378 is implemented
private void endGameIfArmyResultsComplete(Game game) {
    List<Integer> armies = Streams.concat(game.getPlayerOptions().values().stream(), game.getAiOptions().values().stream()).filter(options -> options.containsKey(OPTION_ARMY)).map(options -> (Integer) options.get(OPTION_ARMY)).collect(Collectors.toList());
    Map<Integer, Player> connectedPlayers = game.getConnectedPlayers();
    Map<Integer, Map<Integer, ArmyResult>> reportedArmyOutcomes = game.getReportedArmyResults();
    for (Integer playerId : connectedPlayers.keySet()) {
        Map<Integer, ArmyResult> reportedOutcomes = reportedArmyOutcomes.get(playerId);
        if (reportedOutcomes == null) {
            return;
        }
        if (!areArmyOutcomesComplete(armies, playerId, reportedOutcomes)) {
            return;
        }
    }
    onGameEnded(game);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Player(com.faforever.server.entity.Player) ArmyResult(com.faforever.server.entity.ArmyResult) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 43 with Game

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

the class GameService method disconnectPlayerFromGame.

/**
 * Tells all peers of the player with the specified ID to drop their connections to him/her.
 */
public void disconnectPlayerFromGame(Player requester, int playerId) {
    Optional<Player> optional = playerService.getOnlinePlayer(playerId);
    if (!optional.isPresent()) {
        log.warn("User '{}' tried to disconnect unknown player '{}' from game", requester, playerId);
        return;
    }
    Player player = optional.get();
    Game game = player.getCurrentGame();
    if (game == null) {
        log.warn("User '{}' tried to disconnect player '{}' from game, but no game is associated", requester, player);
        return;
    }
    Collection<? extends ConnectionAware> receivers = game.getConnectedPlayers().values().stream().filter(item -> !Objects.equals(item.getId(), playerId)).collect(Collectors.toList());
    clientService.disconnectPlayerFromGame(playerId, receivers);
    log.info("User '{}' disconnected player '{}' from game '{}'", requester, player, game);
}
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) Player(com.faforever.server.entity.Player) Game(com.faforever.server.entity.Game)

Example 44 with Game

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

the class GameService method updatePlayerGameState.

/**
 * Updates the game state of a player's game.
 */
@Transactional
public void updatePlayerGameState(PlayerGameState newState, Player player) {
    Game game = player.getCurrentGame();
    Requests.verify(game != null, ErrorCode.NOT_IN_A_GAME);
    PlayerGameState oldState = player.getGameState();
    log.debug("Player '{}' updated his game state from '{}' to '{}' (game: '{}')", player, oldState, newState, game);
    Requests.verify(PlayerGameState.canTransition(oldState, newState), ErrorCode.INVALID_PLAYER_GAME_STATE_TRANSITION, oldState, newState);
    changePlayerGameState(player, newState);
    switch(newState) {
        case LOBBY:
            onLobbyEntered(player, game);
            break;
        case LAUNCHING:
            onGameLaunching(player, game);
            break;
        case ENDED:
            onPlayerGameEnded(player, game);
            break;
        case CLOSED:
            onPlayerGameClosed(player, game);
            break;
        case IDLE:
            log.warn("Ignoring state '{}' from player '{}' for game '{}' (should be handled by the client)", newState, player, game);
            break;
        default:
            throw new ProgrammingError("Uncovered state: " + newState);
    }
}
Also used : Game(com.faforever.server.entity.Game) ProgrammingError(com.faforever.server.error.ProgrammingError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 45 with Game

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

the class GameService method updateGameOption.

/**
 * Updates an option value of the game that is currently being hosted by the specified host. If the specified player
 * is not currently hosting a game, this method does nothing.
 */
public void updateGameOption(Player reporter, 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
        log.debug("Received game option for player w/o game: {}", reporter);
        return;
    }
    Requests.verify(Objects.equals(reporter.getCurrentGame().getHost(), reporter), ErrorCode.HOST_ONLY_OPTION, key);
    log.trace("Updating option for game '{}': '{}' = '{}'", game, key, value);
    game.getOptions().put(key, value);
    if (VictoryCondition.GAME_OPTION_NAME.equals(key)) {
        game.setVictoryCondition(VictoryCondition.fromString((String) value));
    } else if (OPTION_SLOTS.equals(key)) {
        game.setMaxPlayers((int) value);
    } else if (OPTION_SCENARIO_FILE.equals(value)) {
        game.setMapName(((String) value).replace("//", "/").replace("\\", "/").split("/")[2]);
    } else if (OPTION_TITLE.equals(value)) {
        game.setTitle((String) value);
    }
    markDirty(game, DEFAULT_MIN_DELAY, DEFAULT_MAX_DELAY);
}
Also used : Game(com.faforever.server.entity.Game)

Aggregations

Game (com.faforever.server.entity.Game)96 Test (org.junit.Test)71 Player (com.faforever.server.entity.Player)26 FeaturedMod (com.faforever.server.entity.FeaturedMod)15 ModVersion (com.faforever.server.entity.ModVersion)12 ConnectionAware (com.faforever.server.client.ConnectionAware)11 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)11 List (java.util.List)11 ClientService (com.faforever.server.client.ClientService)10 ServerProperties (com.faforever.server.config.ServerProperties)10 RequestException (com.faforever.server.error.RequestException)10 ModService (com.faforever.server.mod.ModService)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)10 Duration (java.time.Duration)10 Map (java.util.Map)10 Optional (java.util.Optional)10 Slf4j (lombok.extern.slf4j.Slf4j)10 Service (org.springframework.stereotype.Service)10 ArmyResult (com.faforever.server.entity.ArmyResult)9 GamePlayerStats (com.faforever.server.entity.GamePlayerStats)9