src/Entity/Jeu.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\JeuRepository;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. #[ORM\Entity(repositoryClass: JeuRepository::class)]
  7. class Jeu
  8. {
  9. #[ORM\Id]
  10. #[ORM\GeneratedValue]
  11. #[ORM\Column(type: "integer")]
  12. public int $id;
  13. public ?Partie $partieMax = null;
  14. #[ORM\OneToMany(mappedBy: "jeu", targetEntity: Partie::class, cascade: ['persist'])]
  15. #[ORM\OrderBy(["date" => "DESC"])]
  16. public Collection|array $parties;
  17. public function __construct(
  18. #[ORM\Column(type:"string", length:255)]
  19. public ?string $nom = null,
  20. #[ORM\Column(type:"string", length:255, nullable: true)]
  21. public ?string $variante = null,
  22. #[ORM\Column(type:"integer")]
  23. public ?int $joueursMin = null,
  24. #[ORM\Column(type:"integer")]
  25. public ?int $joueursMax = null,
  26. ) {}
  27. public function getNbParties()
  28. {
  29. return count($this->parties);
  30. }
  31. public function __toString(): string
  32. {
  33. return implode(' - ', array_filter([
  34. $this->nom, $this->variante
  35. ], 'strlen'));
  36. }
  37. public function dernierePartie(): ?\DateTime
  38. {
  39. return $this->parties[0]?->date;
  40. }
  41. public function getScoreMax(): ?Partie
  42. {
  43. if ($this->partieMax == null) {
  44. $max = null;
  45. /** @var Partie $partie */
  46. foreach ($this->parties as $partie) {
  47. if ($max == null || ($partie->getScoreMax()->score > $max->getScoreMax()->score)) {
  48. $max = $partie;
  49. }
  50. }
  51. $this->partieMax = $max;
  52. }
  53. return $this->partieMax;
  54. }
  55. public function getJoueurMax()
  56. {
  57. if (count($this->parties) == 0) return null;
  58. $joueurs = [];
  59. /** @var Partie $partie */
  60. foreach ($this->parties as $partie) {
  61. /** @var Score $score */
  62. foreach ($partie->scores as $score) {
  63. $joueur = $score->joueur;
  64. if (!isset($joueurs[$joueur->id])) {
  65. $joueurs[$joueur->id] = [
  66. 'joueur' => $joueur,
  67. 'parties' => 0,
  68. 'victoires' => 0,
  69. ];
  70. }
  71. $joueurs[$joueur->id]['parties']++;
  72. }
  73. $joueur = $partie->getScoreMax()->joueur;
  74. $joueurs[$joueur->id]['victoires']++;
  75. }
  76. array_walk(
  77. $joueurs,
  78. fn(&$j) => $j['ratio'] = $j['victoires'] / $j['parties']
  79. );
  80. usort(
  81. $joueurs,
  82. fn ($a, $b) => $a['ratio'] <=> $b['ratio']
  83. );
  84. $joueurs = array_filter(
  85. $joueurs,
  86. fn($j) => $j['parties'] >= 3,
  87. );
  88. if (count($joueurs) == 0) return null;
  89. //return $joueurs[count($joueurs)-1];
  90. return end($joueurs);
  91. }
  92. }