com.github.wakingrufus.elo.PlayerUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lib-elo Show documentation
Show all versions of lib-elo Show documentation
A library which performs ELO calculations for a game league (such as chess)
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)
}