elijahcruz12/glicko2 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elijahcruz12/glicko2

Composer 安装命令:

composer require elijahcruz12/glicko2

包简介

A modern PHP 8.4+ implementation of the Glicko-2 rating system, and the only one on Glicko.net

README 文档

README

A modern PHP 8.4 implementation of Glicko-2, a rating system for competitive game ladders. Glicko-2 is a refinement of the well-known Elo rating system that adds the concepts of rating deviation, volatility, and rating decay.

This is a clean, ground-up rewrite targeting PHP 8.4, with full type safety, immutable value objects, and a stateless calculator. It implements the algorithm as described in the official Glicko-2 paper by Professor Mark E. Glickman (revised March 22, 2022), including the corrected Illinois algorithm for volatility convergence.

This library is linked to on the official Glicko-2 page as a reference implementation.

Requirements

  • PHP 8.4+

Installation

composer require elijahcruz12/glicko2

Usage

Creating a player rating

For new players, use the default values. The tau system constant should be between 0.3 and 1.2 depending on the application, and must be determined by estimation or experimentation. Smaller values prevent large swings in volatility.

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,
);

Calculating updated ratings

Ratings are calculated at the end of a rating period — a collection of games treated as having occurred simultaneously. The Glicko2 calculator is stateless and returns a new Rating instance without mutating the original.

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

Players who did not compete

Pass an empty results array. The rating and volatility remain unchanged, but the RD increases to reflect growing uncertainty.

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

Outcomes

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

Serialization

Rating implements JsonSerializable and supports PHP's native serialize()/unserialize(), making it suitable for storage in a database or use with Laravel Queues.

JSON

// 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);

The derived internal properties mu and phi are excluded from serialization — they are always recomputed from rating and rd on construction, so round-tripping through JSON is lossless.

Native PHP serialization

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

Laravel

For storing a player's rating in a database column, a simple JSON cast works well:

// 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),
    );
}

For Laravel Queues, Rating can be passed directly as a job property — PHP's native serialization handles it automatically.

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

Concepts

Rating (r) — The player's skill estimate. Defaults to 1500 for new players.

Rating Deviation (RD) — Uncertainty in the rating. High for new or inactive players, lower for active players with consistent results. Defaults to 350.

Volatility (σ) — How erratic the player's performance is expected to be. Defaults to 0.06.

System Constant (τ) — Constrains how much volatility can change per rating period. Recommended range is 0.31.2. Defaults to 0.75.

Rating periods

Glicko-2 works best when each rating period contains a moderate number of games — ideally at least 10–15 per player. All games within a period are treated as simultaneous. The length of a rating period is left to the administrator's discretion.

Testing

Tests are written with PestPHP and cover the full algorithm against the numerical example from the official Glicko-2 paper, including intermediate values for v, Δ, and σ′, as well as serialization round-trips.

composer require --dev pestphp/pest
./vendor/bin/pest

License

MIT

elijahcruz12/glicko2 适用场景与选型建议

elijahcruz12/glicko2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 elijahcruz12/glicko2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 elijahcruz12/glicko2 我们能提供哪些服务?
定制开发 / 二次开发

基于 elijahcruz12/glicko2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-05