Created
September 26, 2021 15:41
-
-
Save jupegarnica/11659932d422b152829dc1d85728d5a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const elo = ([...ratings], kFactor = 32, selfRating) => { | |
const [a, b] = ratings; | |
const expectedScore = (self, opponent) => | |
1 / (1 + 10 ** ((opponent - self) / 400)); | |
const newRating = (rating, i) => | |
(selfRating || rating) + | |
kFactor * (i - expectedScore(i ? a : b, i ? b : a)); | |
if (ratings.length === 2) | |
return [newRating(a, 1), newRating(b, 0)]; | |
for (let i = 0, len = ratings.length; i < len; i++) { | |
let j = i; | |
while (j < len - 1) { | |
j++; | |
[ratings[i], ratings[j]] = elo( | |
[ratings[i], ratings[j]], | |
kFactor, | |
); | |
} | |
} | |
return ratings; | |
}; | |
// Standard 1v1s | |
elo([1200, 1200]); // [1216, 1184] | |
elo([1200, 1200], 64); // [1232, 1168] | |
// 4 player FFA, all same rank | |
elo([1200, 1200, 1200, 1200]).map(Math.round); // [1246, 1215, 1185, 1154] | |
/* | |
For teams, each rating can adjusted based on own team's average rating vs. | |
average rating of opposing team, with the score being added to their | |
own individual rating by supplying it as the third argument. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment