11namespace ComusParty\App\Sockets;
14use ComusParty\Models\GameRecordDAO;
15use ComusParty\Models\GameRecordState;
17use Ratchet\ConnectionInterface;
18use Ratchet\MessageComponentInterface;
25class Game implements MessageComponentInterface
44 $this->clients =
new SplObjectStorage;
53 function onOpen(ConnectionInterface $conn): void
55 $conn->send(json_encode([
'message' =>
'Connection established']));
65 function onMessage(ConnectionInterface $from, $msg): void
67 $data = json_decode($msg,
true);
69 if (!isset($data[
'uuid']) || !isset($data[
'command']) || !isset($data[
'game'])) {
70 $from->send(json_encode([
'error' =>
'Message invalide']));
74 $uuid = $data[
'uuid'];
75 $game = $data[
'game'];
76 $command = $data[
'command'];
78 if (!isset($this->games[$game])) {
79 $this->games[$game] = [];
82 if (!in_array($from, $this->games[$game])) {
83 $this->games[$game][] = $from;
109 if (!isset($gameRecord)) {
113 $players = $gameRecord->getPlayers();
114 $jsonPlayer = array_map(fn($player) => [
115 "uuid" => $player[
'player']->getUuid(),
116 "username" => $player[
'player']->getUsername(),
117 "pfp" => $player[
'player']->getActivePfp(),
118 "isHost" => $player[
'player']->getUuid() == $gameRecord->getHostedBy()->getUuid(),
121 $this->
sendToGame($game,
"updatePlayers", json_encode($jsonPlayer));
130 private function sendToGame(
string $game,
string $command,
string $content): void
132 foreach ($this->games[$game] as $client) {
133 $client->send(json_encode([
'command' => $command,
'content' => $content]));
148 if ($gameRecord->getState() == GameRecordState::STARTED &&
149 $gameRecord->getHostedBy()->getUuid() == $uuid) {
150 $this->
sendToGame($game,
"gameStarted", json_encode([
"message" =>
"Game started!"]));
160 public function onClose(ConnectionInterface $conn): void
162 foreach ($this->games as $game =>
$clients) {
163 $key = array_search($conn,
$clients);
164 if ($key !==
false) {
166 unset($this->games[$game][$key]);
170 $this->clients->detach($conn);
178 public function onError(ConnectionInterface $conn, Exception $e): void
180 echo
"Erreur: {$e->getMessage()}\n";
189 protected function escape(
string $string): string
191 return htmlspecialchars($string);
static getInstance()
Retourne l'instance du singleton de la base de données.
__construct()
Constructeur de la classe Game.
onClose(ConnectionInterface $conn)
Ferme la connexion d'un client.
redirectUserToGame(string $game, string $uuid)
Redirige un joueur vers la partie si elle a commencé
onMessage(ConnectionInterface $from, $msg)
Fonction appelée lors de la réception d'un message.
SplObjectStorage $clients
onError(ConnectionInterface $conn, Exception $e)
Fonction appelée lors d'une erreur.
onOpen(ConnectionInterface $conn)
Fonction appelée lors de la connexion d'un joueur.
sendToGame(string $game, string $command, string $content)
Fonction permettant d'envoyer un message à une partie.
updatePlayer(string $game)
Envoie un signal à tous les clients avec la nouvelle liste des joueurs.
escape(string $string)
Fonction permettant d'échapper les caractères spéciaux.