Skip to content

Commit fa916dd

Browse files
authored
Create eloCalculator.php
1 parent 0a3377e commit fa916dd

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

eloCalculator.php

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

0 commit comments

Comments
 (0)