Skip to content

Instantly share code, notes, and snippets.

@bigbeno37
Last active September 29, 2018 10:30
Show Gist options
  • Save bigbeno37/c1b52beec15a751cf8b9439e3102d092 to your computer and use it in GitHub Desktop.
Save bigbeno37/c1b52beec15a751cf8b9439e3102d092 to your computer and use it in GitHub Desktop.
ELO Points
// Calculates the log base 'base' of 'value' e.g. logBase(2, 4) is log_2 (4) = 2
function logBase(base, value) {
return log(value)/log(base);
}
/*
* Calculates the points earned after winning a match based on the following:
* Min and max points: The minimum and maximum amount of points to be applied
* Min and max delta: At what rank difference the min or max points should be applied (e.g. -500 and 500 ranking point difference)
* Delta: How much ranking point difference there is between the two players (winningPlayer - losingPlayer)
*
* This is an exponential function, so the following apply: minPoints, maxDelta - minDelta
* and minPoints - minDelta must not be zero, otherwise the universe implodes. Simple, right?
*
* When calculating how many points are won when player wins, an example is as follows:
* winningPlayerPoints += calculatePoints(1, 100, -500, 500, 200);
*
* When calculating how many points are lost when player loses, flip function along y-axis i.e. 1/calculatePoints
* e.g. losingPlayerPoints -= 1/calculatePoints(1, 100, -500, 500, -100);
*/
function calculatePoints(minPoints, maxPoints, minDelta, maxDelta, delta) {
// If the difference in ranking is less than the minimum specified delta, return the minPoints
if (delta < minDelta) {
return minPoints;
}
// If the difference in ranking is greater than the maximum specified delta, return the maxPoints
if (delta > maxDelta) {
return maxPoints;
}
// Delta is in between min and max delta, calculate amount of points won
let a = logBase(maxDelta-minDelta, maxPoints/minPoints);
let b = logBase(a, minPoints - minDelta);
// Return result of exponential function
return Math.pow(a, delta+b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment