Hey! I made this class the calc elo rating and i whould love so get some feed back, what i can do better and stuff.
Im using it for a copy of facemash.[URL=”http://master.aml.dk”] master.aml.dk
Code:
[code=php]
<?php
/********************************
© Andreas Myrup
2013 @ Medieskolerne
********************************/
/******************************************
Desc:
Class for calculating elo ratings
******************************************/
class elo{
/* Rating = The two rating of the players */
public $ratingA;
public $ratingB;
/* The change that player A/B win (between 0-1, 1=100% ) */
protected $chanceA;
protected $chanceB;
/* The new ratings! */
protected $newRatingA;
protected $newRatingB;
/*************************************
Desc:
Assasgin RatingA and RatingB
*************************************/
public function __construct($Ra,$Rb)
{
$this->ratingA = $Ra;
$this->ratingB = $Rb;
}
/*********************************
Desc:
Elo chance.
The change that a or b will win
exampel:
ChanceA = 0.75
ChanceB = 0.25
A have a 75% change of winning
B have a 25% change of loseing
Fromel:
1/(1+10^(ratingA – ratingB)/400)
@Parm:
$Ra = Rating A
$Ra = Rating B
*********************************/
public function chance()
{
$resultA = 1/(1+pow(10,($this->ratingA-$this->ratingB)/400));
$this->chanceA = $resultA;
$resultB = 1/(1+pow(10,($this->ratingB-$this->ratingA)/400));
$this->chanceB = $resultB;
}
/*****************************************
Desc:
The new ratings!
Formel:
New rating = Or+K(Wl-Ec)
Or = Old rating
K = K-factor(depents on the players current rating)
Wl = Win/lose, 1=Win, 0=Lose
Ec = Expected change of winning (in this class $changeA and $changeB)
@Parms:
$Wa = Win/lose, 1/0
$Wb = Win/lose, 1/0
Return:
Array
0 = new rating A
1 = new rating B
*****************************************/
public function rating($Wa,$Wb)
{
if($this->ratingA <= 2100){
$this->newRatingA = $this->ratingA + 32*($Wa – $this->chanceA);
}elseif($this->ratingA > 2100 && $Ra < 2400){
$this->newRatingA = $this->ratingA + 24*($Wa – $this->chanceA);
}elseif($this->ratingA >= 2400){
$this->newRatingA = $this->ratingA + 16*($Wa – $this->chanceA);
}
if($this->ratingB <= 2100){
$this->newRatingB = $this->ratingB + 32*($Wb – $this->chanceB);
}elseif($this->ratingB > 2100 && $this->ratingB < 2400){
$this->newRatingB = $this->ratingB + 24*($Wb – $this->chanceB);
}elseif($this->ratingB >= 2400){
$this->newRatingB = $this->ratingB + 1*($Wb – $this->chanceB);
}
$result = array(
0 => round($this->newRatingA,2),
1 => round($this->newRatingB,2)
);
return $result;
}
}