Skip to content

Instantly share code, notes, and snippets.

@BadBastion
Last active August 21, 2020 06:21
Show Gist options
  • Save BadBastion/b9ca9f9ea5c4c9b72746779c59fa9268 to your computer and use it in GitHub Desktop.
Save BadBastion/b9ca9f9ea5c4c9b72746779c59fa9268 to your computer and use it in GitHub Desktop.
Basic ELO sheets function
const STARTING_ELO = 100
const K = 2
function normalized_win_probability(player_elo, total_game_elo, player_count) {
const avg_opponent_elo = (total_game_elo - player_elo) / (player_count - 1)
// Standard ELO algorithm with opponent ELO avged because we have more than one opponent
const win_probability = 1 / (1 + Math.pow(10, (avg_opponent_elo - player_elo) / STARTING_ELO))
// Normally ELO is calculated for 1 vs 1 games. We need to normalize the probability with the number of players.
const normalized_win_probability = 2 * win_probability / player_count
return normalized_win_probability
}
function update_elo(players, winner, previous) {
const elos = previous || {}
// Make sure any new players start with the default elo
players.forEach(player => {
elos[player] = elos[player] || STARTING_ELO
})
// We need the total game ELO and player count to calculate opponent avg elo
let total_game_elo = 0
let player_count = 0
players.forEach(player => {
total_game_elo += elos[player]
player_count++
})
// Update all our player ELOs
players.forEach(player => {
const win_actual = player == winner ? 1 : 0
const win_prob = normalized_win_probability(elos[player], total_game_elo, player_count)
elos[player] += K * (win_actual - win_prob)
})
return elos
}
function player_elo(player, matchs, match_winners) {
let elos = {}
match_winners = match_winners.flat()
for(let i = 0; i < matchs.length; i++) {
const match = matchs[i]
const winner = match_winners[i] // We assume match_winners only have one column
elos = update_elo(match, winner, elos)
}
return elos[player]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment