-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheloCalculator.php
104 lines (90 loc) · 3.37 KB
/
eloCalculator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
// src/Service/EloCalculator.php
namespace App\Service;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
class EloCalculator
{
// Variables
private $delta; // Différence d'ELO pour laquelle un joueur A à 10x plus de chances de gagner qu'un joueur B (De base : 400)
private $factor; // Nombre de points maximum que peut gagner ou perdre un joueur après un match (De base : 32)
private $winStatus; // Situation finale de la partie, 'p1win' | 'p2win' | 'nowin'
// Builders
function __construct($delta = 400, $factor = 32, $winStatus = 'nowin') {
$this->delta = $delta;
$this->factor = $factor;
$this->winStatus = $winStatus;
}
function __destruct() {
}
// Getters & Setters
public function getDelta() {
return $this->delta;
}
public function setDelta($delta) {
if (!empty($delta)) {
$this->delta = $delta;
} else {
$this->delta = $this->getDelta();
}
}
public function getFactor() {
return $this->factor;
}
public function setFactor($factor) {
if (!empty($factor)) {
$this->factor = $factor;
} else {
$this->factor = $this->getFactor();
}
}
public function getWinStatus() {
return $this->winStatus;
}
public function setWinStatus($winStatus) {
if (empty($winStatus)) {
$this->winStatus = $winStatus;
} else {
$this->winStatus = $this->getWinStatus();
}
}
// Maths - Probabilites
public function calculatriceProbabiltesPlayerA($eloA,$eloB) {
return 1 / (1 + pow(10, ($eloB-$eloA)/$this->getDelta()));
}
// Maths - Mise à jour des scores Elo
public function calculatriceElo($eloA = 1000, $eloB = 1000, $player = "Undefined") {
$successStatus = true;
// Calcul des propabilités selon le joueur concerné
switch ($player) {
case 'A':
$probability = $this->calculatriceProbabiltesPlayerA($eloA, $eloB);
break;
case 'B':
$probability = 1.0-($this->calculatriceProbabiltesPlayerA($eloA, $eloB));
break;
default:
echo "Je ne sais pas quel joueur est examiné du côté user.";
$successStatus = false;
break;
}
// Établissement des ratings
switch ($this->getWinStatus()) {
case 'p1win':
$newEloPlayer = ($player === "A") ? $eloA + $this->getFactor()*(1-$probability) : $eloB + $this->getFactor()*(0-$probability);
break;
case 'p2win':
$newEloPlayer = ($player === "A") ? $eloA + $this->getFactor()*(0-$probability) : $eloB + $this->getFactor()*(1-$probability);
break;
case 'nowin':
$newEloPlayer = ($player === "A") ? $eloA + $this->getFactor()*(0.5-$probability) : $eloB + $this->getFactor()*(0.5-$probability);
break;
default:
echo "Je ne comprends pas l'issue du match.";
$successStatus = false;
break;
}
// Résultat du nouveau ELO
return ($successStatus) ? round($newEloPlayer,0) : null;
}
}