use of com.faforever.server.entity.Player in project faf-java-server by FAForever.
the class GameServiceActivatorsTest method setUp.
@Before
public void setUp() throws Exception {
clientConnection = new ClientConnection("1", Protocol.V1_LEGACY_UTF_16, mock(InetAddress.class));
player = new Player();
User user = (User) new User().setPlayer(player).setPassword("password").setLogin("JUnit");
clientConnection.setAuthentication(new TestingAuthenticationToken(new FafUserDetails(user), null));
instance = new GameServiceActivators(gameService);
}
use of com.faforever.server.entity.Player 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);
}
use of com.faforever.server.entity.Player 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);
}
use of com.faforever.server.entity.Player 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);
}
use of com.faforever.server.entity.Player 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);
}
}
Aggregations