Comus Party 1.0.0
Application web de mini-jeux en ligne
Chargement...
Recherche...
Aucune correspondance
Game.php
Aller à la documentation de ce fichier.
1<?php
10
11namespace ComusParty\App\Sockets;
12
13use ComusParty\App\Db;
14use ComusParty\Models\GameRecordDAO;
15use ComusParty\Models\GameRecordState;
16use Exception;
17use Ratchet\ConnectionInterface;
18use Ratchet\MessageComponentInterface;
19use SplObjectStorage;
20
25class Game implements MessageComponentInterface
26{
31 protected SplObjectStorage $clients;
32
37 protected array $games;
38
42 public function __construct()
43 {
44 $this->clients = new SplObjectStorage;
45 $this->games = [];
46 }
47
53 function onOpen(ConnectionInterface $conn): void
54 {
55 $conn->send(json_encode(['message' => 'Connection established']));
56 }
57
65 function onMessage(ConnectionInterface $from, $msg): void
66 {
67 $data = json_decode($msg, true);
68
69 if (!isset($data['uuid']) || !isset($data['command']) || !isset($data['game'])) {
70 $from->send(json_encode(['error' => 'Message invalide']));
71 return;
72 }
73
74 $uuid = $data['uuid'];
75 $game = $data['game'];
76 $command = $data['command'];
77
78 if (!isset($this->games[$game])) {
79 $this->games[$game] = [];
80 }
81
82 if (!in_array($from, $this->games[$game])) {
83 $this->games[$game][] = $from;
84 }
85
86 switch ($command) {
87 case 'quitGame':
88 case 'joinGame':
89 $this->updatePlayer($game);
90 break;
91 case 'startGame':
92 $this->redirectUserToGame($game, $uuid);
93 break;
94 default:
95 break;
96 }
97 }
98
105 private function updatePlayer(string $game): void
106 {
107 $gameRecord = (new GameRecordDAO(Db::getInstance()->getConnection()))->findByCode($game);
108
109 if (!isset($gameRecord)) {
110 return;
111 }
112
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(),
119 ], $players);
120
121 $this->sendToGame($game, "updatePlayers", json_encode($jsonPlayer));
122 }
123
130 private function sendToGame(string $game, string $command, string $content): void
131 {
132 foreach ($this->games[$game] as $client) {
133 $client->send(json_encode(['command' => $command, 'content' => $content]));
134 }
135 }
136
144 private function redirectUserToGame(string $game, string $uuid): void
145 {
146 $gameRecord = (new GameRecordDAO(Db::getInstance()->getConnection()))->findByCode($game);
147
148 if ($gameRecord->getState() == GameRecordState::STARTED &&
149 $gameRecord->getHostedBy()->getUuid() == $uuid) {
150 $this->sendToGame($game, "gameStarted", json_encode(["message" => "Game started!"]));
151 }
152 }
153
160 public function onClose(ConnectionInterface $conn): void
161 {
162 foreach ($this->games as $game => $clients) {
163 $key = array_search($conn, $clients);
164 if ($key !== false) {
165 $this->updatePlayer($game);
166 unset($this->games[$game][$key]);
167 }
168 }
169
170 $this->clients->detach($conn);
171 }
172
178 public function onError(ConnectionInterface $conn, Exception $e): void
179 {
180 echo "Erreur: {$e->getMessage()}\n";
181 $conn->close();
182 }
183
189 protected function escape(string $string): string
190 {
191 return htmlspecialchars($string);
192 }
193}
static getInstance()
Retourne l'instance du singleton de la base de données.
Definition Db.php:52
Classe Game (Sockets)
Definition Game.php:26
__construct()
Constructeur de la classe Game.
Definition Game.php:42
onClose(ConnectionInterface $conn)
Ferme la connexion d'un client.
Definition Game.php:160
redirectUserToGame(string $game, string $uuid)
Redirige un joueur vers la partie si elle a commencé
Definition Game.php:144
onMessage(ConnectionInterface $from, $msg)
Fonction appelée lors de la réception d'un message.
Definition Game.php:65
SplObjectStorage $clients
Definition Game.php:31
onError(ConnectionInterface $conn, Exception $e)
Fonction appelée lors d'une erreur.
Definition Game.php:178
onOpen(ConnectionInterface $conn)
Fonction appelée lors de la connexion d'un joueur.
Definition Game.php:53
sendToGame(string $game, string $command, string $content)
Fonction permettant d'envoyer un message à une partie.
Definition Game.php:130
updatePlayer(string $game)
Envoie un signal à tous les clients avec la nouvelle liste des joueurs.
Definition Game.php:105
escape(string $string)
Fonction permettant d'échapper les caractères spéciaux.
Definition Game.php:189