-
Notifications
You must be signed in to change notification settings - Fork 0
/
elo.js
77 lines (70 loc) · 2.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const Elo = require('arpad');
class EpongeElo {
constructor() {
this.elo = new Elo(
{
default: 32,
2100: 24,
2400: 16,
},
100,
10000
);
}
getKMultiplier(scoreDelta) {
return 1.5 * (1 + 1 / (1 + Math.exp(5 - scoreDelta / 3)));
}
/**
* The calculated new rating based on the expected outcone, actual outcome, and previous score
* with an incorporation of a k ratio multiplier based on game score.
*
* @param {Number} expected_score The expected score, e.g. 0.25
* @param {Number} actual_score The actual score, e.g. 1
* @param {Number} previous_rating The previous rating of the player, e.g. 1200
* @param {Float} kMultiplier The ratio calculated based on score delta
* @return {Number} The new rating of the player, e.g. 1256
*/
newRating(expectedScore, actualScore, previousRating, kMultiplier) {
const difference = actualScore - expectedScore;
const updateKFactor = kMultiplier * this.elo.getKFactor(previousRating);
const rating = Math.round(previousRating + updateKFactor * difference);
if (rating < this.elo.minimum) {
rating = this.elo.minimum;
} else if (rating > this.elo.maximum) {
rating = this.elo.maximum;
}
return rating;
}
gameResultingElo(scoreDelta, winnerElo, loserElo) {
const [
winnerExpectedScore,
loserExpectedScore,
] = this.elo.bothExpectedScores(winnerElo, loserElo);
const kMultiplier = this.getKMultiplier(scoreDelta, winnerElo, loserElo);
console.log(kMultiplier);
const winnerNewRating = this.newRating(
winnerExpectedScore,
1,
winnerElo,
kMultiplier
);
console.log(
'winner base new rating',
this.elo.newRating(winnerExpectedScore, 1, winnerElo)
);
console.log('winner new rating', winnerNewRating);
const loserNewRating = this.newRating(
loserExpectedScore,
0,
loserElo,
kMultiplier
);
console.log(
'loser base new rating',
this.elo.newRating(loserExpectedScore, 0, loserElo)
);
console.log('loser new rating', loserNewRating);
return [winnerNewRating, loserNewRating];
}
}
module.exports = EpongeElo;