All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wakingrufus.elo.PlayerUtil.kt Maven / Gradle / Ivy

Go to download

A library which performs ELO calculations for a game league (such as chess)

There is a newer version: 0.4.0
Show newest version
package com.github.wakingrufus.elo


fun calculateTeamStartingRating(players: List): Int {
    var teamStartingRating = 0
    players.forEach { teamStartingRating += it.currentRating }
    return teamStartingRating / players.size
}

fun calculateTeamAverageGamesPlayed(players: List): Int {
    var teamGamesPlayed = 0
    players.forEach { teamGamesPlayed += it.gamesPlayed }
    return teamGamesPlayed / players.size
}

fun buildTeam(allPlayers: Map, teamIds: List): List {
    val teamPlayers = ArrayList()
    teamIds.forEach { teamPlayers += allPlayers[it]!! }
    return teamPlayers
}

fun addNewPlayers(existingPlayers: Map, game: Game, startingRating: Int): Map {
    var newPlayerMap = existingPlayers
    val allPlayerIds = game.team1PlayerIds + game.team2PlayerIds
    allPlayerIds.forEach { playerId ->
        if (!newPlayerMap.containsKey(playerId)) {
            newPlayerMap += Pair(playerId, Player(id = playerId,
                    gamesPlayed = 0,
                    wins = 0,
                    losses = 0,
                    currentRating = startingRating))
        }
    }
    return newPlayerMap
}

fun updatePlayer(player: Player, ratingAdjustment: Int, games: Int, wins: Int, losses: Int): Player {
    return player.copy(
            currentRating = player.currentRating + ratingAdjustment,
            gamesPlayed = player.gamesPlayed + games,
            wins = player.wins + wins,
            losses = player.losses + losses)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy