PHP code example of elijahcruz12 / glicko2

1. Go to this page and download the library: Download elijahcruz12/glicko2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elijahcruz12 / glicko2 example snippets


use Elijahcruz12\Glicko2\Rating;

// New player with defaults
$player = new Rating;

// Existing player with known values
$player = new Rating(
    rating: 1500,
    rd:     200,
    sigma:  0.06,
    tau:    0.5,
);

use Elijahcruz12\Glicko2\Glicko2;
use Elijahcruz12\Glicko2\MatchResult;
use Elijahcruz12\Glicko2\Outcome;
use Elijahcruz12\Glicko2\Rating;

$alice   = new Rating(rating: 1500, rd: 200, sigma: 0.06, tau: 0.5);
$bob     = new Rating(rating: 1400, rd: 30,  sigma: 0.06, tau: 0.5);
$charlie = new Rating(rating: 1550, rd: 100, sigma: 0.06, tau: 0.5);
$david   = new Rating(rating: 1700, rd: 300, sigma: 0.06, tau: 0.5);

$calc = new Glicko2;

// Alice beat Bob, lost to Charlie, lost to David
$newAlice = $calc->calculate($alice, [
    new MatchResult($bob,     Outcome::Win),
    new MatchResult($charlie, Outcome::Loss),
    new MatchResult($david,   Outcome::Loss),
]);

echo $newAlice->rating; // 1464.06
echo $newAlice->rd;     // 151.52
echo $newAlice->sigma;  // 0.05999

$newDavid = $calc->calculate($david, []);

Outcome::Win
Outcome::Loss
Outcome::Draw

// Serialize
$json = json_encode($rating);
// {"rating":1500,"rd":200,"sigma":0.06,"tau":0.5}

// Restore from JSON string
$rating = Rating::fromJson($json);

// Restore from array (e.g. from a decoded JSON column)
$rating = Rating::fromArray($data);

$serialized = serialize($rating);
$rating     = unserialize($serialized);

// In your migration
$table->json('rating_data')->nullable();

// In your model
use Elijahcruz12\Glicko2\Rating;

protected function rating(): Attribute
{
    return Attribute::make(
        get: fn (?string $value) => $value ? Rating::fromJson($value) : new Rating,
        set: fn (Rating $value)  => json_encode($value),
    );
}

class UpdatePlayerRating implements ShouldQueue
{
    public function __construct(
        public readonly Rating $currentRating,
    ) {}
}