Search in sources :

Example 1 with PlayerDivisionInfo

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);
}
Also used : PlayerDivisionInfo(com.faforever.server.entity.PlayerDivisionInfo) Division(com.faforever.server.entity.Division) TestWith(com.googlecode.zohhak.api.TestWith)

Example 2 with PlayerDivisionInfo

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))));
}
Also used : Getter(lombok.Getter) Transactional(javax.transaction.Transactional) PlayerDivisionInfo(com.faforever.server.entity.PlayerDivisionInfo) MessageFormat(java.text.MessageFormat) Player(com.faforever.server.entity.Player) Inject(javax.inject.Inject) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) Slf4j(lombok.extern.slf4j.Slf4j) ImmutableList(com.google.common.collect.ImmutableList) Service(org.springframework.stereotype.Service) ServerProperties(com.faforever.server.config.ServerProperties) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Division(com.faforever.server.entity.Division) NotNull(org.jetbrains.annotations.NotNull) PlayerDivisionInfo(com.faforever.server.entity.PlayerDivisionInfo)

Example 3 with PlayerDivisionInfo

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));
}
Also used : PlayerDivisionInfo(com.faforever.server.entity.PlayerDivisionInfo) Transactional(javax.transaction.Transactional)

Example 4 with PlayerDivisionInfo

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;
}
Also used : PlayerDivisionInfo(com.faforever.server.entity.PlayerDivisionInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with 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();
}
Also used : PlayerDivisionInfo(com.faforever.server.entity.PlayerDivisionInfo) ServerProperties(com.faforever.server.config.ServerProperties) Division(com.faforever.server.entity.Division) Before(org.junit.Before)

Aggregations

PlayerDivisionInfo (com.faforever.server.entity.PlayerDivisionInfo)6 Division (com.faforever.server.entity.Division)3 ServerProperties (com.faforever.server.config.ServerProperties)2 Transactional (javax.transaction.Transactional)2 NotNull (org.jetbrains.annotations.NotNull)2 Player (com.faforever.server.entity.Player)1 ImmutableList (com.google.common.collect.ImmutableList)1 TestWith (com.googlecode.zohhak.api.TestWith)1 MessageFormat (java.text.MessageFormat)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 PostConstruct (javax.annotation.PostConstruct)1 Inject (javax.inject.Inject)1 Getter (lombok.Getter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Nullable (org.jetbrains.annotations.Nullable)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Service (org.springframework.stereotype.Service)1