Search in sources :

Example 6 with GamePlayerStats

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

the class RatingService method teamsById.

private Map<Integer, ScoredTeam> teamsById(Collection<GamePlayerStats> playerStats, int noTeamId) {
    int highestTeamId = playerStats.stream().map(GamePlayerStats::getTeam).max(Integer::compareTo).orElse(noTeamId);
    Map<Integer, ScoredTeam> teamsById = new HashMap<>();
    for (GamePlayerStats playerStat : playerStats) {
        IPlayer player = new Player<>(playerStat.getPlayer().getId());
        jskills.Rating rating = new jskills.Rating(playerStat.getMean(), playerStat.getDeviation());
        int teamId = playerStat.getTeam();
        if (teamId == noTeamId) {
            // If a player is in the "no team" team, assign him to his own team
            teamId = playerStat.getPlayer().getId() + highestTeamId;
        }
        teamsById.computeIfAbsent(teamId, ScoredTeam::new).addPlayer(player, rating);
        teamsById.get(teamId).getScore().updateAndGet(score -> score + playerStat.getScore());
    }
    return teamsById;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IPlayer(jskills.IPlayer) Player(jskills.Player) IPlayer(jskills.IPlayer) GamePlayerStats(com.faforever.server.entity.GamePlayerStats) HashMap(java.util.HashMap) Rating(com.faforever.server.entity.Rating) Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) GlobalRating(com.faforever.server.entity.GlobalRating)

Example 7 with GamePlayerStats

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

the class RatingService method updateRatings.

/**
 * Updates the ratings of all players in {@code playerStats} as well as their global/ladder1v1 rating,
 * according to the outcome of the game <strong> without persisting the results</strong>.
 *
 * @param noTeamId ID of the "no team" team
 */
@SuppressWarnings("unchecked")
public void updateRatings(Collection<GamePlayerStats> playerStats, int noTeamId, RatingType ratingType) {
    Map<Integer, GamePlayerStats> playerStatsByPlayerId = playerStats.stream().collect(Collectors.toMap(stats -> stats.getPlayer().getId(), Function.identity()));
    Map<Integer, ScoredTeam> teamsById = teamsById(playerStats, noTeamId);
    List<ScoredTeam> teamsSortedByScore = new ArrayList<>(teamsById.values());
    teamsSortedByScore.sort(Comparator.comparingInt(o -> ((ScoredTeam) o).getScore().intValue()).reversed());
    int[] teamRanks = calculateTeamRanks(teamsSortedByScore);
    // New ArrayList is needed because the JSkill API doesn't handle subclass type parameters
    TrueSkillCalculator.calculateNewRatings(gameInfo, new ArrayList<>(teamsSortedByScore), teamRanks).entrySet().forEach(entry -> {
        Player<Integer> player = (Player<Integer>) entry.getKey();
        GamePlayerStats stats = playerStatsByPlayerId.get(player.getId());
        updatePlayerStats(entry, stats);
        updateRating(ratingType, stats);
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Player(jskills.Player) Arrays(java.util.Arrays) ProgrammingError(com.faforever.server.error.ProgrammingError) Getter(lombok.Getter) TrueSkillCalculator(jskills.TrueSkillCalculator) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) Map(java.util.Map) ServerProperties(com.faforever.server.config.ServerProperties) IPlayer(jskills.IPlayer) Rating(com.faforever.server.entity.Rating) GameInfo(jskills.GameInfo) Team(jskills.Team) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) GlobalRating(com.faforever.server.entity.GlobalRating) Entry(java.util.Map.Entry) GamePlayerStats(com.faforever.server.entity.GamePlayerStats) AllArgsConstructor(lombok.AllArgsConstructor) Comparator(java.util.Comparator) ITeam(jskills.ITeam) Assert(org.springframework.util.Assert) Player(jskills.Player) IPlayer(jskills.IPlayer) GamePlayerStats(com.faforever.server.entity.GamePlayerStats) ArrayList(java.util.ArrayList)

Example 8 with GamePlayerStats

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

the class CoopServiceTest method reportOperationComplete.

@Test
public void reportOperationComplete() throws Exception {
    Game game = new Game().setId(42).setMapName("SCMP_001");
    Map<Integer, GamePlayerStats> playerStats = game.getPlayerStats();
    playerStats.put(1, new GamePlayerStats());
    playerStats.put(2, new GamePlayerStats());
    playerStats.put(3, new GamePlayerStats());
    Player player = new Player();
    player.setCurrentGame(game);
    CoopMap mission = new CoopMap();
    when(coopMapRepository.findOneByFilenameLikeIgnoreCase("SCMP_001")).thenReturn(Optional.of(mission));
    instance.reportOperationComplete(player, false, Duration.ofMinutes(8));
    ArgumentCaptor<CoopLeaderboardEntry> captor = ArgumentCaptor.forClass(CoopLeaderboardEntry.class);
    verify(coopLeaderboardRepository).save(captor.capture());
    CoopLeaderboardEntry entry = captor.getValue();
    assertThat(entry.getGameId(), is(42L));
    assertThat(entry.getMission(), is(mission));
    assertThat(entry.getPlayerCount(), is(3));
    assertThat(entry.getTime(), is(Time.from(Instant.EPOCH.plus(Duration.ofMinutes(8)))));
}
Also used : Player(com.faforever.server.entity.Player) Game(com.faforever.server.entity.Game) CoopMap(com.faforever.server.entity.CoopMap) GamePlayerStats(com.faforever.server.entity.GamePlayerStats) CoopLeaderboardEntry(com.faforever.server.entity.CoopLeaderboardEntry) Test(org.junit.Test)

Aggregations

GamePlayerStats (com.faforever.server.entity.GamePlayerStats)8 Player (com.faforever.server.entity.Player)5 GlobalRating (com.faforever.server.entity.GlobalRating)4 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)4 HashMap (java.util.HashMap)4 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.Test)3 ServerProperties (com.faforever.server.config.ServerProperties)2 Game (com.faforever.server.entity.Game)2 Rating (com.faforever.server.entity.Rating)2 ProgrammingError (com.faforever.server.error.ProgrammingError)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ClientService (com.faforever.server.client.ClientService)1 ConnectionAware (com.faforever.server.client.ConnectionAware)1 GameResponses (com.faforever.server.client.GameResponses)1 ArmyResult (com.faforever.server.entity.ArmyResult)1 CoopLeaderboardEntry (com.faforever.server.entity.CoopLeaderboardEntry)1 CoopMap (com.faforever.server.entity.CoopMap)1