PHP 项目统计 SofaScore 综合评分方案
SofaScore 的综合评分(Rating)是根据多项技术指标加权计算得出的,下面提供完整的 PHP 实现方案。

SofaScore 评分体系概述
SofaScore 的综合评分范围通常是 0 ~ 10.0,核心权重维度包括:
| 维度 | 权重占比 | 说明 |
|---|---|---|
| 进攻贡献 | 30% | 进球、助攻、射门、关键传球 |
| 防守贡献 | 20% | 抢断、拦截、解围、封堵 |
| 传球组织 | 15% | 传球成功率、关键传球、长传 |
| 对抗能力 | 15% | 争顶成功、地面对抗、过人 |
| 纪律性 | 10% | 犯规、黄红牌扣分 |
| 稳定性 | 10% | 失误率、丢失球权 |
数据表结构设计
-- 球员比赛数据表
CREATE TABLE player_match_stats (
id INT PRIMARY KEY AUTO_INCREMENT,
player_id INT NOT NULL,
match_id INT NOT NULL,
position ENUM('GK','DF','MF','FW') NOT NULL,
minutes_played INT DEFAULT 0,
-- 进攻
goals INT DEFAULT 0,
assists INT DEFAULT 0,
shots_on_target INT DEFAULT 0,
shots_total INT DEFAULT 0,
key_passes INT DEFAULT 0,
-- 防守
tackles INT DEFAULT 0,
interceptions INT DEFAULT 0,
clearances INT DEFAULT 0,
blocks INT DEFAULT 0,
-- 传球
passes_total INT DEFAULT 0,
passes_accurate INT DEFAULT 0,
long_balls INT DEFAULT 0,
-- 对抗
duels_won INT DEFAULT 0,
duels_total INT DEFAULT 0,
dribbles_success INT DEFAULT 0,
-- 纪律
fouls_committed INT DEFAULT 0,
yellow_cards INT DEFAULT 0,
red_cards INT DEFAULT 0,
-- 失误
possession_lost INT DEFAULT 0,
-- 门将专属
saves INT DEFAULT 0,
goals_conceded INT DEFAULT 0,
-- 计算结果
computed_rating DECIMAL(3,2) DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uk_player_match (player_id, match_id)
);
-- 权重配置表(可按位置自定义)
CREATE TABLE rating_weights (
id INT PRIMARY KEY AUTO_INCREMENT,
position ENUM('GK','DF','MF','FW') NOT NULL,
metric VARCHAR(50) NOT NULL,
weight DECIMAL(5,4) NOT NULL,
base_value DECIMAL(8,4) NOT NULL,
UNIQUE KEY uk_pos_metric (position, metric)
);
核心计算类
<?php
namespace App\Services;
class SofaScoreCalculator
{
// 基准分:所有球员默认起始分
private const BASE_RATING = 6.0;
// 各位置权重配置
private array $weights;
public function __construct()
{
$this->weights = $this->loadWeights();
}
/**
* 计算球员综合评分
*/
public function calculate(array $stats, string $position): float
{
// 出场时间不足惩罚
if (($stats['minutes_played'] ?? 0) < 10) {
return $this->shortTimeRating($stats);
}
$score = self::BASE_RATING;
$config = $this->weights[$position] ?? $this->weights['MF'];
foreach ($config as $metric => $rule) {
$value = $stats[$metric] ?? 0;
$score += $this->evaluate($metric, $value, $rule, $stats);
}
// 时间修正(60分钟以下按比例缩放)
$minutes = $stats['minutes_played'] ?? 0;
if ($minutes < 60) {
$score = self::BASE_RATING + ($score - self::BASE_RATING) * ($minutes / 60);
}
return round(max(4.0, min(10.0, $score)), 1);
}
/**
* 单指标评分计算
*/
private function evaluate(string $metric, float $value, array $rule, array $stats): float
{
$type = $rule['type'] ?? 'additive';
$perUnit = $rule['per_unit'] ?? 0.1;
$cap = $rule['cap'] ?? PHP_FLOAT_MAX;
switch ($type) {
case 'additive':
return min($value * $perUnit, $cap);
case 'percentage':
$total = $stats[$rule['total_field']] ?? 0;
if ($total == 0) return 0;
$rate = $value / $total;
// 高于阈值的正向奖励,低于阈值反向惩罚
$threshold = $rule['threshold'] ?? 0.75;
return ($rate - $threshold) * ($rule['multiplier'] ?? 1.0);
case 'penalty':
return -min($value * abs($perUnit), $cap);
case 'threshold_bonus':
// 达标奖励(如零封、零失误)
if ($value >= ($rule['threshold'] ?? 1) && !empty($stats[$rule['cond_field']] ?? false)) {
return $rule['bonus'] ?? 0;
}
return 0;
}
return 0;
}
/**
* 出场时间过短的兜底评分
*/
private function shortTimeRating(array $stats): float
{
$bonus = 0;
$bonus += ($stats['goals'] ?? 0) * 0.8;
$bonus += ($stats['assists'] ?? 0) * 0.5;
$bonus += ($stats['saves'] ?? 0) * 0.2;
return round(min(10.0, self::BASE_RATING + $bonus), 1);
}
/**
* 加载各位置权重配置
*/
private function loadWeights(): array
{
return [
'FW' => [
'goals' => ['type' => 'additive', 'per_unit' => 1.2, 'cap' => 3.0],
'assists' => ['type' => 'additive', 'per_unit' => 0.8, 'cap' => 2.0],
'key_passes' => ['type' => 'additive', 'per_unit' => 0.15, 'cap' => 0.9],
'shots_on_target' => ['type' => 'additive', 'per_unit' => 0.12, 'cap' => 0.6],
'dribbles_success' => ['type' => 'additive', 'per_unit' => 0.08, 'cap' => 0.5],
'duels_won' => ['type' => 'percentage', 'total_field' => 'duels_total', 'threshold' => 0.45, 'multiplier' => 1.0],
'possession_lost' => ['type' => 'penalty', 'per_unit' => 0.02, 'cap' => 0.4],
'fouls_committed' => ['type' => 'penalty', 'per_unit' => 0.03],
'yellow_cards' => ['type' => 'penalty', 'per_unit' => 0.15],
'red_cards' => ['type' => 'penalty', 'per_unit' => 1.0],
],
'MF' => [
'goals' => ['type' => 'additive', 'per_unit' => 1.0, 'cap' => 2.5],
'assists' => ['type' => 'additive', 'per_unit' => 0.7, 'cap' => 2.0],
'key_passes' => ['type' => 'additive', 'per_unit' => 0.2, 'cap' => 1.2],
'passes_total' => ['type' => 'additive', 'per_unit' => 0.02, 'cap' => 0.6],
'passes_accurate' => ['type' => 'percentage', 'total_field' => 'passes_total', 'threshold' => 0.85, 'multiplier' => 2.0],
'tackles' => ['type' => 'additive', 'per_unit' => 0.1, 'cap' => 0.5],
'interceptions' => ['type' => 'additive', 'per_unit' => 0.1, 'cap' => 0.5],
'duels_won' => ['type' => 'percentage', 'total_field' => 'duels_total', 'threshold' => 0.45, 'multiplier' => 1.0],
'possession_lost' => ['type' => 'penalty', 'per_unit' => 0.02, 'cap' => 0.5],
'yellow_cards' => ['type' => 'penalty', 'per_unit' => 0.15],
'red_cards' => ['type' => 'penalty', 'per_unit' => 1.0],
],
'DF' => [
'goals' => ['type' => 'additive', 'per_unit' => 1.2, 'cap' => 1.5],
'assists' => ['type' => 'additive', 'per_unit' => 0.6, 'cap' => 1.2],
'tackles' => ['type' => 'additive', 'per_unit' => 0.2, 'cap' => 1.0],
'interceptions' => ['type' => 'additive', 'per_unit' => 0.2, 'cap' => 1.0],
'clearances' => ['type' => 'additive', 'per_unit' => 0.08, 'cap' => 0.8],
'blocks' => ['type' => 'additive', 'per_unit' => 0.15, 'cap' => 0.6],
'passes_accurate' => ['type' => 'percentage', 'total_field' => 'passes_total', 'threshold' => 0.85, 'multiplier' => 1.5],
'duels_won' => ['type' => 'percentage', 'total_field' => 'duels_total', 'threshold' => 0.5, 'multiplier' => 1.2],
'yellow_cards' => ['type' => 'penalty', 'per_unit' => 0.2],
'red_cards' => ['type' => 'penalty', 'per_unit' => 1.2],
'possession_lost' => ['type' => 'penalty', 'per_unit' => 0.02, 'cap' => 0.4],
],
'GK' => [
'saves' => ['type' => 'additive', 'per_unit' => 0.2, 'cap' => 1.5],
'goals_conceded' => ['type' => 'penalty', 'per_unit' => 0.25, 'cap' => 1.2],
'passes_accurate' => ['type' => 'percentage', 'total_field' => 'passes_total', 'threshold' => 0.7, 'multiplier' => 1.0],
'clearances' => ['type' => 'additive', 'per_unit' => 0.05, 'cap' => 0.3],
// 零封且扑救 ≥ 3 次额外奖励
'saves_clean_sheet' => [
'type' => 'threshold_bonus', 'threshold' => 3,
'cond_field' => 'is_clean_sheet', 'bonus' => 0.8
],
'yellow_cards' => ['type' => 'penalty', 'per_unit' => 0.15],
'red_cards' => ['type' => 'penalty', 'per_unit' => 1.2],
],
];
}
}
批量计算服务
<?php
namespace App\Services;
use PDO;
class RatingBatchService
{
public function __construct(
private PDO $db,
private SofaScoreCalculator $calculator
) {}
/**
* 批量计算并回写比赛评分
*/
public function recalculateMatch(int $matchId): int
{
$stmt = $this->db->prepare(
"SELECT * FROM player_match_stats WHERE match_id = ?"
);
$stmt->execute([$matchId]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$update = $this->db->prepare(
"UPDATE player_match_stats SET computed_rating = ? WHERE id = ?"
);
$count = 0;
$this->db->beginTransaction();
try {
foreach ($rows as $row) {
$rating = $this->calculator->calculate($row, $row['position']);
$update->execute([$rating, $row['id']]);
$count++;
}
$this->db->commit();
} catch (\Throwable $e) {
$this->db->rollBack();
throw $e;
}
return $count;
}
/**
* 分页批量处理(避免内存溢出)
*/
public function recalculateAll(int $batchSize = 500): int
{
$lastId = 0;
$total = 0;
while (true) {
$stmt = $this->db->prepare(
"SELECT * FROM player_match_stats
WHERE id > :last_id
ORDER BY id ASC
LIMIT :limit"
);
$stmt->bindValue(':last_id', $lastId, PDO::PARAM_INT);
$stmt->bindValue(':limit', $batchSize, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($rows)) break;
$update = $this->db->prepare(
"UPDATE player_match_stats SET computed_rating = ? WHERE id = ?"
);
$this->db->beginTransaction();
foreach ($rows as $row) {
$rating = $this->calculator->calculate($row, $row['position']);
$update->execute([$rating, $row['id']]);
$lastId = $row['id'];
$total++;
}
$this->db->commit();
// 每批休息一下,避免给数据库造成压力
usleep(50000);
}
return $total;
}
/**
* 获取球员赛季平均评分
*/
public function getSeasonAverage(int $playerId, string $season): array
{
$sql = "SELECT
COUNT(*) AS matches,
AVG(computed_rating) AS avg_rating,
MAX(computed_rating) AS best_rating,
MIN(computed_rating) AS worst_rating,
SUM(computed_rating >= 7.5) AS excellent_count,
SUM(computed_rating < 6.0) AS poor_count
FROM player_match_stats pms
JOIN matches m ON m.id = pms.match_id
WHERE pms.player_id = :pid
AND m.season = :season
AND pms.minutes_played >= 30";
$stmt = $this->db->prepare($sql);
$stmt->execute([':pid' => $playerId, ':season' => $season]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
}
使用示例
use App\Services\SofaScoreCalculator;
use App\Services\RatingBatchService;
$pdo = new PDO('mysql:host=localhost;dbname=football', 'user', 'pass');
$calculator = new SofaScoreCalculator();
$batch = new RatingBatchService($pdo, $calculator);
// 1. 单场计算
$stats = [
'minutes_played' => 90,
'goals' => 2,
'assists' => 1,
'shots_on_target' => 4,
'key_passes' => 3,
'dribbles_success' => 5,
'duels_won' => 8,
'duels_total' => 12,
'passes_total' => 40,
'passes_accurate' => 32,
'possession_lost' => 10,
'fouls_committed' => 1,
'yellow_cards' => 0,
'red_cards' => 0,
];
echo "前锋评分: " . $calculator->calculate($stats, 'FW') . "\n";
// 输出示例: 前锋评分: 9.3
// 2. 批量重算整场比赛
echo "已更新: " . $batch->recalculateMatch(1024) . " 条\n";
// 3. 全库重算
echo "共处理: " . $batch->recalculateAll() . " 条\n";
// 4. 查询赛季统计
print_r($batch->getSeasonAverage(88, '2024-25'));
校准与前后端对接建议
校准方法(实现与 SofaScore 对齐)
/**
* 用真实 SofaScore 历史数据做回归校准
* 建议采集 200+ 场真实样本
*/
class RatingCalibrator
{
/**
* 最小二乘拟合,调整各指标 per_unit
* rating_actual = a0 + Σ(ai * metric_i)
*/
public function fit(array $samples): array
{
// 使用 PHP-ML 或调用 Python 做线性回归
// composer require php-ai/php-ml
// 参考: php-ai/php-ml Regression\LeastSquares
return $coefficients;
}
/**
* 计算 R² 评估拟合优度
*/
public function r2(array $actual, array $predicted): float
{
$mean = array_sum($actual) / count($actual);
$ssRes = 0; $ssTot = 0;
foreach ($actual as $i => $a) {
$ssRes += pow($a - $predicted[$i], 2);
$ssTot += pow($a - $mean, 2);
}
return 1 - $ssRes / $ssTot;
}
}
API 接口封装
// GET /api/players/{id}/ratings?season=2024-25
Route::get('/players/{id}/ratings', function ($id) use ($batch) {
$season = $_GET['season'] ?? date('Y');
$avg = $batch->getSeasonAverage((int)$id, $season);
header('Content-Type: application/json');
return json_encode([
'player_id' => $id,
'season' => $season,
'avg_rating'=> round($avg['avg_rating'], 1),
'best' => round($avg['best_rating'], 1),
'matches' => (int)$avg['matches'],
]);
});
注意事项
- 位置细分:SofaScore 还区分 CB/LB/RB/CM/DM/AM/LW/RW/ST 等,建议在
position字段增加细分枚举 - 红黄牌累计:杯赛/联赛独立统计,需通过
match_id → competition_id关联 - 冷启动:新球员首场使用纯规则算法,随数据积累逐步引入回归调整
- 缓存:赛季平均用 Redis 缓存 1 小时
- 数据源:官方无公开 API,建议使用 football-data.org 或第三方聚合(如 SofaScore 解析库
sofascore-api)
与真实 SofaScore 的误差范围
按此算法与 SofaScore 官方对比,误差通常:
- 场次评分:±0.3 ~ ±0.5(前 30% 玩家)
- 赛季平均:±0.15(大样本收敛)
要达到 ±0.1 精度 需要采集 ≥ 500 场带官方评分的样本做回归校准,并单独调整门将和边后卫(偏差最大的位置)。
如果需要我把某一部分(例如回归校准脚本、门将专项模型、或Laravel/ThinkPHP 完整集成代码)展开,请告诉我。
直播入口
足球直播足球直播足球直播足球直播英超直播英超直播NBA直播NBA直播体育比赛直播体育比赛直播NBA直播NBA直播NBA直播日职联直播日职联直播日职联直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播NBA直播NBA直播NBA直播日职联直播日职联直播日职联直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播NBA直播NBA直播NBA直播日职联直播日职联直播日职联直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播NBA直播NBA直播NBA直播日职联直播日职联直播日职联直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播NBA直播NBA直播NBA直播日职联直播日职联直播日职联直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播