Hackerss.com

hackerss
hackerss

Posted on

Php Function for elo ranking


function elo_rating($player_one, $player_two, $player_one_score, $player_two_score) {
  //calculate the expected score for each player
  $expected_score_player_one = 1 / (1 + pow(10, (($player_two['rating'] - $player_one['rating']) / 400)));
  $expected_score_player_two = 1 / (1 + pow(10, (($player_one['rating'] - $player_two['rating']) / 400)));

  //calculate the new rating for each player
  $new_rating_player_one = $player_one['rating'] + (32 * ($player_one_score - $expected_score_player_one));
  $new_rating_player_two = $player_two['rating'] + (32 * ($player_two_score - $expected_score_player_two));

  //return the new rating for each player
  return array(
    'playerOne' => $new_rating_player_one,
    'playerTwo' => $new_rating_player_two
  );
}

//example
$result = elo_rating(array('rating' => 1500), array('rating' => 1200), 1, 0);

//output
echo $result['playerOne']; //1501
echo $result['playerTwo']; //1199
Enter fullscreen mode Exit fullscreen mode

Top comments (0)