Search in sources :

Example 1 with Rating

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

the class RatingServiceTest method calculateQuality.

@Test
public void calculateQuality() throws Exception {
    Rating left = new GlobalRating().setMean(1600d).setDeviation(30d);
    Rating right = new GlobalRating().setMean(900d).setDeviation(160d);
    double quality = instance.calculateQuality(left, right);
    assertThat(quality, is(0.16000885216755253));
}
Also used : Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) GlobalRating(com.faforever.server.entity.GlobalRating) Rating(com.faforever.server.entity.Rating) GlobalRating(com.faforever.server.entity.GlobalRating) Test(org.junit.Test)

Example 2 with Rating

use of com.faforever.server.entity.Rating 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 3 with Rating

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

the class RatingService method calculateQuality.

public double calculateQuality(Rating left, Rating right) {
    jskills.Rating leftRating = ofNullable(left).map(rating -> new jskills.Rating(left.getMean(), left.getDeviation())).orElse(gameInfo.getDefaultRating());
    jskills.Rating rightRating = ofNullable(left).map(rating -> new jskills.Rating(right.getMean(), right.getDeviation())).orElse(gameInfo.getDefaultRating());
    Collection<ITeam> teams = Arrays.asList(new Team(new Player<>(1), leftRating), new Team(new Player<>(2), rightRating));
    return TrueSkillCalculator.calculateMatchQuality(gameInfo, teams);
}
Also used : 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) Rating(com.faforever.server.entity.Rating) Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) GlobalRating(com.faforever.server.entity.GlobalRating) ITeam(jskills.ITeam) Team(jskills.Team) ITeam(jskills.ITeam)

Example 4 with Rating

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

the class RatingService method updateRating.

private void updateRating(RatingType ratingType, GamePlayerStats stats) {
    Rating rating;
    switch(ratingType) {
        case GLOBAL:
            rating = ofNullable(stats.getPlayer().getGlobalRating()).orElseGet(() -> {
                GlobalRating initial = new GlobalRating();
                stats.getPlayer().setGlobalRating(initial);
                return initial;
            });
            break;
        case LADDER_1V1:
            rating = ofNullable(stats.getPlayer().getLadder1v1Rating()).orElseGet(() -> {
                Ladder1v1Rating initial = new Ladder1v1Rating();
                stats.getPlayer().setLadder1v1Rating(initial);
                return initial;
            });
            break;
        default:
            throw new ProgrammingError("Uncovered rating type: " + ratingType);
    }
    rating.setMean(stats.getAfterMean());
    rating.setDeviation(stats.getAfterDeviation());
    log.debug("New '{}' rating for player '{}' is: {}", ratingType, stats.getPlayer(), rating);
}
Also used : Rating(com.faforever.server.entity.Rating) Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) GlobalRating(com.faforever.server.entity.GlobalRating) GlobalRating(com.faforever.server.entity.GlobalRating) Ladder1v1Rating(com.faforever.server.entity.Ladder1v1Rating) ProgrammingError(com.faforever.server.error.ProgrammingError)

Example 5 with Rating

use of com.faforever.server.entity.Rating 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)

Aggregations

GlobalRating (com.faforever.server.entity.GlobalRating)5 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)5 Rating (com.faforever.server.entity.Rating)5 GamePlayerStats (com.faforever.server.entity.GamePlayerStats)3 ProgrammingError (com.faforever.server.error.ProgrammingError)3 HashMap (java.util.HashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IPlayer (jskills.IPlayer)3 Player (jskills.Player)3 ServerProperties (com.faforever.server.config.ServerProperties)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2