/    Sign up×
Community /Pin to ProfileBookmark

Php elo class, please rate.

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[/URL]
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;
}
}[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @dreaz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...