-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelo.js
35 lines (31 loc) · 1.17 KB
/
elo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const { pow, round } = Math
/**
* The constant. Maximum value to be won/lost.
*/
const K = 32
/**
* Calculates the expected match outcome.
* @param {number} playerRating Target player rating.
* @param {number} rivalRating Rival rating.
* @returns {number}
*/
export const expected = (playerRating, rivalRating) =>
parseFloat((1 / (1 + pow(10, (rivalRating - playerRating) / 400))).toFixed(2))
/**
* Gets the new rating of the target player.
* @param {number} playerRating Target player rating.
* @param {number} rivalRating Rival rating.
* @param {number} score Result of the match. 0 = lost, 0.5 = drawn, 1 = won.
* @returns {number}
*/
export const newRating = (playerRating, rivalRating, score) =>
round(playerRating + K * (score - expected(playerRating, rivalRating)))
/**
* Gets the rating difference of the target player.
* @param {number} playerRating Target player rating.
* @param {number} rivalRating Rival rating.
* @param {number} score Result of the match. 0 = lost, 0.5 = drawn, 1 = won.
* @returns {number}
*/
export const ratingDifference = (playerRating, rivalRating, score) =>
newRating(playerRating, rivalRating, score) - playerRating