-
Notifications
You must be signed in to change notification settings - Fork 0
/
elo.js
48 lines (35 loc) · 1.76 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
var RANKING_WEIGHT = 200;
var GAME_WEIGHT = 20;
var BATTLE_POINTS_WEIGHT = 1000;
function calculateElo(player1, player2){
if(player1.battlePoints == player2.battlePoints){
throw new Error("Cannot determin winner.");
}
var winner = player1.battlePoints > player2.battlePoints ? player1:player2;
var loser = player1.battlePoints > player2.battlePoints ? player2:player1;
var gameWeightFactor = calculateGameWeightFactor(winner.numberOfGames);
var battlePointsWeightFactor = 2+0.5*(calculateBattlePointsWeightFactor(winner.battlePoints, loser.battlePoints));
var rankingWeightFactor = calculaterankingWeightFactor(loser.eloRanking, winner.eloRanking);
var winnerElo = winner.eloRanking+2*(gameWeightFactor*(battlePointsWeightFactor-rankingWeightFactor));
gameWeightFactor = calculateGameWeightFactor(loser.numberOfGames);
battlePointsWeightFactor = -1-0.5*(calculateBattlePointsWeightFactor(winner.battlePoints, loser.battlePoints));
rankingWeightFactor = calculaterankingWeightFactor(winner.eloRanking, loser.eloRanking);
var loserElo = loser.eloRanking+2*(gameWeightFactor*(battlePointsWeightFactor-rankingWeightFactor));
winner.eloRanking = Math.round(winnerElo);
loser.eloRanking = Math.round(loserElo);
}
function calculateGameWeightFactor(numberOfGames){
return 1 + (GAME_WEIGHT/(10+numberOfGames));
}
function calculateBattlePointsWeightFactor(points1, points2){
return (points1-points2)/BATTLE_POINTS_WEIGHT;
}
function calculaterankingWeightFactor(eloRanking1, eloRanking2){
return 2*(1/(1+Math.pow(10,(eloRanking1-eloRanking2)/RANKING_WEIGHT)));
}
var obbElo = {
};
obbElo.package = require('./package.json');
obbElo.calculateElo = calculateElo;
obbElo.calculaterankingWeightFactor = calculaterankingWeightFactor;
module.exports = obbElo;