use of com.faforever.server.entity.PlayerDivisionInfo in project faf-java-server by FAForever.
the class DivisionServiceTest method testGetCurrentPlayerDivision.
@TestWith({ "1, 0.0f, 1", "1, 1.0f, 1", "1, 10.0f, 1", "1, 10.1f, 2", "1, 30.0f, 2", "2, 10.0f, 4", "2, 99.9f, 6", "3, 999.0f, 9" })
public void testGetCurrentPlayerDivision(int league, float score, int expectedDivisionId) throws Exception {
PlayerDivisionInfo info = new PlayerDivisionInfo();
info.setPlayer(player1);
info.setLeague(league);
info.setScore(score);
when(playerDivisionInfoRepository.findByPlayerAndSeason(player1, serverProperties.getLadder1v1().getSeason())).thenReturn(info);
Division result = instance.getCurrentPlayerDivision(player1).get();
assertEquals(result.getId(), expectedDivisionId);
}
use of com.faforever.server.entity.PlayerDivisionInfo in project faf-java-server by FAForever.
the class DivisionService method getCurrentPlayerDivision.
public Optional<Division> getCurrentPlayerDivision(Player player) {
int season = properties.getLadder1v1().getSeason();
PlayerDivisionInfo info = playerDivisionInfoRepository.findByPlayerAndSeason(player, season);
if (info == null) {
return Optional.empty();
}
return Optional.of(divisions.stream().filter(division -> Objects.equals(division.getLeague(), info.getLeague())).filter(division -> info.getScore() <= division.getThreshold()).findFirst().orElseThrow(() -> new IllegalStateException(MessageFormat.format("Could not determine division for PlayerDivisionInfo {0}", info))));
}
use of com.faforever.server.entity.PlayerDivisionInfo in project faf-java-server by FAForever.
the class DivisionService method postResult.
@Transactional
public void postResult(@NotNull Player playerOne, @NotNull Player playerTwo, @Nullable Player winner) {
PlayerDivisionInfo playerOneInfo = getPlayerDivisionInfo(playerOne, properties.getLadder1v1().getSeason());
PlayerDivisionInfo playerTwoInfo = getPlayerDivisionInfo(playerTwo, properties.getLadder1v1().getSeason());
playerOneInfo.setGames(playerOneInfo.getGames() + 1);
playerTwoInfo.setGames(playerTwoInfo.getGames() + 1);
if (winner == null) {
log.debug("Game ended in a draw, no changes in score");
return;
}
PlayerDivisionInfo winnerInfo;
PlayerDivisionInfo loserInfo;
if (winner.equals(playerOne)) {
winnerInfo = playerOneInfo;
loserInfo = playerTwoInfo;
} else if (winner.equals(playerTwo)) {
winnerInfo = playerTwoInfo;
loserInfo = playerOneInfo;
} else {
throw new IllegalArgumentException(MessageFormat.format("Winner does not match any of the players (player one: '{0}', player two: '{1}', winner: '{2}'", playerOne, playerTwo, winner));
}
float gain = properties.getLadder1v1().getRegularGain();
float loss = properties.getLadder1v1().getRegularLoss();
if (winnerInfo.isInInferiorLeague(loserInfo)) {
gain = properties.getLadder1v1().getIncreasedGain();
loss = properties.getLadder1v1().getIncreasedLoss();
} else if (winnerInfo.isInSuperiorLeague(loserInfo)) {
gain = properties.getLadder1v1().getReducedGain();
loss = properties.getLadder1v1().getReducedLoss();
}
log.debug("Player '{}' won against player '{}', gain: '{}', loss: '{}'", winner, loserInfo.getPlayer(), gain, loss);
if (winnerInfo.getScore() + gain > getMaxLeagueThreshold(winnerInfo.getLeague())) {
int newLeague = winnerInfo.getLeague() + 1;
log.debug("Winner was promoted to league '{}'", newLeague);
winnerInfo.setScore(0f);
winnerInfo.setLeague(newLeague);
} else {
winnerInfo.setScore(winnerInfo.getScore() + gain);
}
loserInfo.setScore(Float.max(0f, loserInfo.getScore() - loss));
}
use of com.faforever.server.entity.PlayerDivisionInfo in project faf-java-server by FAForever.
the class DivisionService method addPlayerToSeason.
@NotNull
private PlayerDivisionInfo addPlayerToSeason(@NotNull Player player, int season) {
log.debug("Adding player '{}' to season '{}'", player, season);
PlayerDivisionInfo playerDivisionInfo = new PlayerDivisionInfo();
playerDivisionInfo.setSeason(season);
playerDivisionInfo.setPlayer(player);
playerDivisionInfo.setGames(0);
playerDivisionInfo.setLeague(1);
playerDivisionInfo.setScore(0f);
playerDivisionInfoRepository.save(playerDivisionInfo);
return playerDivisionInfo;
}
use of com.faforever.server.entity.PlayerDivisionInfo in project faf-java-server by FAForever.
the class DivisionServiceTest method setUp.
@Before
public void setUp() throws Exception {
serverProperties = new ServerProperties();
serverProperties.getLadder1v1().setSeason(1);
info1 = new PlayerDivisionInfo();
info1.setPlayer(player1);
info1.setSeason(serverProperties.getLadder1v1().getSeason());
info2 = new PlayerDivisionInfo();
info2.setPlayer(player2);
info2.setSeason(serverProperties.getLadder1v1().getSeason());
Division[] divisions = new Division[] { createDivision(1, "League 1 - Division A", 1, 10), createDivision(2, "League 1 - Division B", 1, 30), createDivision(3, "League 1 - Division C", 1, 50), createDivision(4, "League 2 - Division D", 2, 20), createDivision(5, "League 2 - Division E", 2, 60), createDivision(6, "League 2 - Division F", 2, 100), createDivision(7, "League 3 - Division G", 3, 100), createDivision(8, "League 3 - Division H", 3, 200), createDivision(9, "League 3 - Division I", 3, 9999) };
when(divisionRepository.findAllByOrderByLeagueAscThresholdAsc()).thenReturn(Arrays.asList(divisions));
instance = new DivisionService(serverProperties, divisionRepository, playerDivisionInfoRepository);
instance.init();
}
Aggregations