55 public function onOpen(ConnectionInterface $conn): void
58 $gameCode = explode(
"=", $conn->httpRequest->getUri()->getQuery())[1];
60 if (!isset($this->games[$gameCode])) {
61 $this->games[$gameCode] = [];
63 $this->games[$gameCode][] = $conn;
65 $this->clients->attach($conn);
75 public function onMessage(ConnectionInterface $from, $msg): void
77 $data = json_decode($msg,
true);
79 if (!isset($data[
"content"]) || !isset($data[
"author"]) || !isset($data[
"game"])) {
83 $content = $this->
escape($data[
"content"]);
84 $author = $this->
escape($data[
"author"]);
85 $game = $data[
"game"];
88 $player = $playerManager->findByUsername($author);
91 $penalty = $penaltyManager->findLastMutedByPlayerUuid($player->getUuid());
94 if (isset($penalty)) {
95 $endDate = $penalty->getCreatedAt()->modify(
"+" . $penalty->getDuration() .
"hour");
96 if ($endDate >
new DateTime()) {
101 if (!isset($this->games[$game])) {
102 $this->games[$game] = [];
105 if (!in_array($from, $this->games[$game])) {
106 $this->games[$game][] = $from;
109 foreach ($this->games[$game] as $player) {
110 $player->send(json_encode([
112 "content" => $content,
132 public function onClose(ConnectionInterface $conn): void
135 foreach ($this->games as $gameId => &$players) {
136 $players = array_filter($players,
function ($player) use ($conn) {
137 return $player !== $conn;
141 if (empty($players)) {
142 unset($this->games[$gameId]);
146 $this->clients->detach($conn);