Skip to content

Instantly share code, notes, and snippets.

@binarykore
Last active May 16, 2022 21:15
Show Gist options
  • Select an option

  • Save binarykore/1c4826781e4fa70689cb449dc00ba2c1 to your computer and use it in GitHub Desktop.

Select an option

Save binarykore/1c4826781e4fa70689cb449dc00ba2c1 to your computer and use it in GitHub Desktop.
ELO Rating using PHP as base Backend Language..
<?php
class elo{
//K Factor = 32 (below 2100)
//K Factor = 24 (between 2100 and 2400)
//K Factor = 16 (above 2400)
const KFACTOR = 32;
//WIN = 1 Points
//DRAW = 0.5 Points
//LOSE = 0 Points
const WIN = 1;
const DRAW = 0.5;
const LOSE = 0;
//
protected static $_expectedScore = [];
protected static $_newRating = [];
protected $_rating = [];
public static function powMod($_base,$_exponent){
$_result = 1;
for($_g = 0;$_g < $_exponent;$_g++){
$_result *= $_base;
}
return($_result);
}
public function rating($_rA,$_rB){
$this->_rating["rate"] = self::getExpectedScores($_rA,$_rB);
return($this->_rating);
}
public static function getNewRatings($_rA,$_rB,$_sA,$_sB,$_eA,$_eB){
self::$_newRating["a"] = $_rA + (self::KFACTOR * ($_sA - $_eA));
self::$_newRating["b"] = $_rB + (self::KFACTOR * ($_sB - $_eB));
return(self::$_newRating);
}
public static function getExpectedScores($_rA,$_rB){
$_sA = self::WIN;
$_sB = self::LOSE;
self::$_expectedScore["a"] = 1 / (1 + (self::powMod(10,($_rA - $_rB) / 400)));
self::$_expectedScore["b"] = 1 / (1 + (self::powMod(10,($_rB - $_rA) / 400)));
$_eA = self::$_expectedScore["a"];
$_eB = self::$_expectedScore["b"];
self::$_expectedScore["c"] = self::getNewRatings($_rA,$_rB,$_sA,$_sB,$_eA,$_eB);
return(self::$_expectedScore);
}
}
$_rA = 1200;
$_rB = 1200;
$_c = new elo();
print_r($_c -> rating($_rA,$_rB));
//print_r(elo::getExpectedScores($_rA,$_rB));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment