Comus Party 1.0.0
Application web de mini-jeux en ligne
Chargement...
Recherche...
Aucune correspondance
Chat.php
Aller à la documentation de ce fichier.
1<?php
10
12
16use DateMalformedStringException;
17use DateTime;
18use Exception;
19use Ratchet\ConnectionInterface;
20use Ratchet\MessageComponentInterface;
21use SplObjectStorage;
22
27class Chat implements MessageComponentInterface
28{
33 protected SplObjectStorage $clients;
34
39 protected array $games;
40
44 public function __construct()
45 {
46 $this->clients = new SplObjectStorage;
47 $this->games = [];
48 }
49
55 public function onOpen(ConnectionInterface $conn): void
56 {
57 // Récupérer le lien auquel il est connecté
58 $gameCode = explode("=", $conn->httpRequest->getUri()->getQuery())[1];
59
60 if (!isset($this->games[$gameCode])) {
61 $this->games[$gameCode] = [];
62 }
63 $this->games[$gameCode][] = $conn;
64
65 $this->clients->attach($conn);
66 }
67
75 public function onMessage(ConnectionInterface $from, $msg): void
76 {
77 $data = json_decode($msg, true);
78
79 if (!isset($data["content"]) || !isset($data["author"]) || !isset($data["game"])) {
80 return;
81 }
82
83 $content = $this->escape($data["content"]);
84 $author = $this->escape($data["author"]);
85 $game = $data["game"];
86
87 $playerManager = new PlayerDAO(Db::getInstance()->getConnection());
88 $player = $playerManager->findByUsername($author);
89
90 $penaltyManager = new PenaltyDAO(Db::getInstance()->getConnection());
91 $penalty = $penaltyManager->findLastMutedByPlayerUuid($player->getUuid());
92
93
94 if (isset($penalty)) {
95 $endDate = $penalty->getCreatedAt()->modify("+" . $penalty->getDuration() . "hour");
96 if ($endDate > new DateTime()) {
97 return;
98 }
99 }
100
101 if (!isset($this->games[$game])) {
102 $this->games[$game] = [];
103 }
104
105 if (!in_array($from, $this->games[$game])) {
106 $this->games[$game][] = $from;
107 }
108
109 foreach ($this->games[$game] as $player) {
110 $player->send(json_encode([
111 "author" => $author,
112 "content" => $content,
113 ]));
114 }
115 }
116
122 protected function escape(string $string): string
123 {
124 return htmlspecialchars($string);
125 }
126
132 public function onClose(ConnectionInterface $conn): void
133 {
134 // Retirer le joueur
135 foreach ($this->games as $gameId => &$players) {
136 $players = array_filter($players, function ($player) use ($conn) {
137 return $player !== $conn;
138 });
139
140 // Supprimer la game si vide
141 if (empty($players)) {
142 unset($this->games[$gameId]);
143 }
144 }
145
146 $this->clients->detach($conn);
147 }
148
155 public function onError(ConnectionInterface $conn, Exception $e): void
156 {
157 echo "An error has occurred: {$e->getMessage()}\n";
158
159 $conn->close();
160 }
161}
Class Db.
Definition Db.php:21
static getInstance()
Retourne l'instance du singleton de la base de données.
Definition Db.php:52
Classe Chat (Sockets)
Definition Chat.php:28
__construct()
Constructeur de la classe Chat.
Definition Chat.php:44
onClose(ConnectionInterface $conn)
Fonction appelée lors de la déconnexion d'un joueur.
Definition Chat.php:132
onMessage(ConnectionInterface $from, $msg)
Fonction appelée lors de la réception d'un message.
Definition Chat.php:75
SplObjectStorage $clients
Definition Chat.php:33
onError(ConnectionInterface $conn, Exception $e)
Fonction appelée lorsqu'une erreur survient.
Definition Chat.php:155
onOpen(ConnectionInterface $conn)
Fonction appelée lors de la connexion d'un joueur.
Definition Chat.php:55
escape(string $string)
Fonction permettant d'échapper les caractères spéciaux.
Definition Chat.php:122