Comus Party 1.0.0
Application web de mini-jeux en ligne
Chargement...
Recherche...
Aucune correspondance
gameRecord.dao.php
Aller à la documentation de ce fichier.
1<?php
10
11namespace ComusParty\Models;
12
13use DateTime;
14use Exception;
15use PDO;
16
22{
27 private ?PDO $pdo;
28
33 public function __construct(?PDO $pdo)
34 {
35 $this->pdo = $pdo;
36 }
37
44 public function findByGameId(int $gameId): ?array
45 {
46 $stmt = $this->pdo->prepare("SELECT * FROM " . DB_PREFIX . "game_record WHERE game_id = :gameId");
47 $stmt->bindParam(":gameId", $gameId);
48 $stmt->execute();
49 $stmt->setFetchMode(PDO::FETCH_ASSOC);
50 $gameRecords = $stmt->fetchAll();
51 if (!$gameRecords) {
52 return null;
53 }
54 return $this->hydrateMany($gameRecords);
55 }
56
63 private function hydrateMany(array $rows): array
64 {
65 $gameRecords = [];
66 foreach ($rows as $row) {
67 $gameRecords[] = $this->hydrate($row);
68 }
69 return $gameRecords;
70 }
71
78 private function hydrate(array $row): GameRecord
79 {
80 if (!is_null($row["hosted_by"])) {
81 $hostedBy = (new PlayerDAO($this->getPdo()))->findByUuid($row["hosted_by"]);
82 } else {
83 $hostedBy = null;
84 }
85
86 $game = (new GameDAO($this->getPdo()))->findById($row["game_id"]);
87 $gameRecordState = match ($row["state"]) {
88 "waiting" => GameRecordState::WAITING,
89 "started" => GameRecordState::STARTED,
90 "finished" => GameRecordState::FINISHED,
91 default => GameRecordState::UNKNOWN,
92 };
93 $finishedAt = $row["finished_at"] ? new DateTime($row["finished_at"]) : null;
94 $players = $this->findPlayersByGameRecordCode($row["code"]);
95 if (!is_null($players)) {
96 $players = array_map(function ($player) {
97 $playerObject = (new PlayerDAO($this->getPdo()))->findByUuid($player["player_uuid"]);
98 return [
99 "player" => $playerObject,
100 "token" => $player["token"]
101 ];
102 }, $players);
103 }
104
105 return new GameRecord(
106 $row["code"],
107 $game,
108 $hostedBy,
109 $players,
110 $gameRecordState,
111 $row["private"] === 1,
112 $row["token"],
113 new DateTime($row["created_at"]),
114 new DateTime($row["updated_at"]),
115 $finishedAt
116 );
117 }
118
123 public function getPdo(): ?PDO
124 {
125 return $this->pdo;
126 }
127
132 public function setPdo(?PDO $pdo): void
133 {
134 $this->pdo = $pdo;
135 }
136
142 private function findPlayersByGameRecordCode(string $code): ?array
143 {
144 $stmt = $this->pdo->prepare("SELECT player_uuid, token FROM " . DB_PREFIX . "played WHERE game_code = :code");
145 $stmt->bindParam(":code", $code);
146 $stmt->execute();
147 $stmt->setFetchMode(PDO::FETCH_ASSOC);
148 $players = $stmt->fetchAll();
149 if (!$players) {
150 return null;
151 }
152 return $players;
153 }
154
162 public function findByGameIdAndState(int $gameId, GameRecordState $state): ?array
163 {
164 $stmt = $this->pdo->prepare("SELECT * FROM " . DB_PREFIX . "game_record WHERE game_id = :gameId AND state = :gameState");
165 $stmt->bindParam(":gameId", $gameId);
166
167 $state = match ($state) {
168 GameRecordState::WAITING => "waiting",
169 GameRecordState::STARTED => "started",
170 GameRecordState::FINISHED => "finished",
172 };
173 $stmt->bindParam(":gameState", $state);
174
175 $stmt->execute();
176 $stmt->setFetchMode(PDO::FETCH_ASSOC);
177 $gameRecords = $stmt->fetchAll();
178 if (!$gameRecords) {
179 return null;
180 }
181 return $this->hydrateMany($gameRecords);
182 }
183
191 public function findByCode(string $code): ?GameRecord
192 {
193 $stmt = $this->pdo->prepare("SELECT * FROM " . DB_PREFIX . "game_record WHERE code = :code");
194 $stmt->bindParam(":code", $code);
195 $stmt->execute();
196 $stmt->setFetchMode(PDO::FETCH_ASSOC);
197
198 $gameRecord = $stmt->fetch();
199 if (!$gameRecord) {
200 return null;
201 }
202 return $this->hydrate($gameRecord);
203 }
204
212 public function findByHostUuid(string $uuid): ?array
213 {
214 $stmt = $this->pdo->prepare("SELECT * FROM " . DB_PREFIX . "game_record WHERE hosted_by = :uuid");
215 $stmt->bindParam(":uuid", $uuid);
216 $stmt->execute();
217 $stmt->setFetchMode(PDO::FETCH_ASSOC);
218
219 $gameRecords = $stmt->fetchAll();
220 if (!$gameRecords) {
221 return null;
222 }
223 return $this->hydrateMany($gameRecords);
224 }
225
226
234 public function findByState(GameRecordState $state): ?array
235 {
236 $state = match ($state) {
237 GameRecordState::WAITING => "waiting",
238 GameRecordState::STARTED => "started",
239 GameRecordState::FINISHED => "finished",
241 };
242
243 $stmt = $this->pdo->prepare("SELECT * FROM " . DB_PREFIX . "game_record WHERE state = :state");
244 $stmt->bindParam(":state", $state);
245 $stmt->execute();
246 $stmt->setFetchMode(PDO::FETCH_ASSOC);
247
248 $gameRecords = $stmt->fetchAll();
249 if (!$gameRecords) {
250 return null;
251 }
252 return $this->hydrateMany($gameRecords);
253 }
254
260 public function insert(GameRecord $gameRecord): bool
261 {
262 $stmt = $this->pdo->prepare("INSERT INTO " . DB_PREFIX . "game_record (code, token, game_id, hosted_by, state, private, created_at, updated_at, finished_at) VALUES (:code, :token, :gameId, :hostedBy, :state, :isPrivate, :createdAt, :updatedAt, :finishedAt)");
263
264 $code = $gameRecord->getCode();
265 $token = $gameRecord->getToken();
266 $gameId = $gameRecord->getGame()->getId();
267 $hostUuid = $gameRecord->getHostedBy()->getUuid();
268 $state = match ($gameRecord->getState()) {
269 GameRecordState::WAITING => "waiting",
270 GameRecordState::STARTED => "started",
271 GameRecordState::FINISHED => "finished",
273 };
274 $isPrivate = $gameRecord->isPrivate();
275 $createdAt = $gameRecord->getCreatedAt()->format("Y-m-d H:i:s");
276 $updatedAt = $gameRecord->getUpdatedAt()?->format("Y-m-d H:i:s");
277 $finishedAt = $gameRecord->getFinishedAt()?->format("Y-m-d H:i:s");
278
279 $stmt->bindParam(":code", $code);
280 $stmt->bindParam(":token", $token);
281 $stmt->bindParam(":gameId", $gameId);
282 $stmt->bindParam(":hostedBy", $hostUuid);
283 $stmt->bindParam(":state", $state);
284 $stmt->bindParam(":isPrivate", $isPrivate, PDO::PARAM_BOOL);
285 $stmt->bindParam(":createdAt", $createdAt);
286 $stmt->bindParam(":updatedAt", $updatedAt);
287 $stmt->bindParam(":finishedAt", $finishedAt);
288
289 return $stmt->execute();
290 }
291
297 public function update(GameRecord $gameRecord): bool
298 {
299 $stmt = $this->pdo->prepare("UPDATE " . DB_PREFIX . "game_record SET token = :token, hosted_by = :hosted_by, state = :state, private = :private, finished_at = :finishedAt WHERE code = :code");
300
301 $token = $gameRecord->getToken();
302 $hostUuid = $gameRecord->getHostedBy()->getUuid();
303 $state = match ($gameRecord->getState()) {
304 GameRecordState::WAITING => "waiting",
305 GameRecordState::STARTED => "started",
306 GameRecordState::FINISHED => "finished",
308 };
309 $private = $gameRecord->isPrivate();
310 $finishedAt = $gameRecord->getFinishedAt()?->format("Y-m-d H:i:s");
311 $code = $gameRecord->getCode();
312
313 $stmt->bindParam(":token", $token);
314 $stmt->bindParam(":hosted_by", $hostUuid);
315 $stmt->bindParam(":state", $state);
316 $stmt->bindParam(":private", $private, PDO::PARAM_BOOL);
317 $stmt->bindParam(":finishedAt", $finishedAt);
318 $stmt->bindParam(":code", $code);
319
320 return $stmt->execute();
321 }
322
329 public function updatePlayers(string $gameCode, array $players): bool
330 {
331 $stmt = $this->pdo->prepare("UPDATE " . DB_PREFIX . "played SET token = :token WHERE game_code = :gameCode AND player_uuid = :playerUuid");
332 foreach ($players as $player) {
333 $stmt->bindParam(":token", $player["token"]);
334 $stmt->bindParam(":gameCode", $gameCode);
335 $uuid = $player["player"]->getUuid();
336 $stmt->bindParam(":playerUuid", $uuid);
337 if (!$stmt->execute()) {
338 return false;
339 }
340 }
341 return true;
342 }
343
349 public function delete(string $code): bool
350 {
351 $stmt = $this->pdo->prepare("DELETE FROM " . DB_PREFIX . "game_record WHERE code = :code");
352 $stmt->bindParam(":code", $code);
353 return $stmt->execute();
354 }
355
362 public function addPlayer(GameRecord $gameRecord, Player $player): bool
363 {
364 $stmt = $this->pdo->prepare("INSERT INTO " . DB_PREFIX . "played (game_code, player_uuid) VALUES (:gameCode, :playerUuid)");
365 $gameRecord->addPlayer($player);
366 $playerUuid = $player->getUuid();
367 $gameCode = $gameRecord->getCode();
368 $stmt->bindParam(":gameCode", $gameCode);
369 $stmt->bindParam(":playerUuid", $playerUuid);
370 return $stmt->execute();
371 }
372
379 public function removePlayer(string $gameCode, string $playerUuid): bool
380 {
381 $stmt = $this->pdo->prepare("DELETE FROM " . DB_PREFIX . "played WHERE game_code = :gameCode AND player_uuid = :playerUuid");
382 $stmt->bindParam(":gameCode", $gameCode);
383 $stmt->bindParam(":playerUuid", $playerUuid);
384 return $stmt->execute();
385 }
386
393 public function addWinner(string $code, mixed $uuid): bool
394 {
395 $stmt = $this->pdo->prepare("INSERT INTO " . DB_PREFIX . "won (game_code, player_uuid) VALUES (:gameCode, :playerUuid)");
396 $stmt->bindParam(":gameCode", $code);
397 $stmt->bindParam(":playerUuid", $uuid);
398 return $stmt->execute();
399 }
400}
addPlayer(GameRecord $gameRecord, Player $player)
Ajoute un joueur à une partie en base de données.
update(GameRecord $gameRecord)
Met à jour un enregistrement de partie en base de données.
hydrateMany(array $rows)
Retourne la liste des parties hydratées.
getPdo()
Retourne la connexion à la base de données.
findByCode(string $code)
Retourne un objet GameRecord (ou null) à partir du code passé en paramètre.
findByGameId(int $gameId)
Retourne la liste des parties grâce à l'ID du jeu.
insert(GameRecord $gameRecord)
Insère un enregistrement de partie en base de données.
removePlayer(string $gameCode, string $playerUuid)
Supprime un joueur d'une partie.
updatePlayers(string $gameCode, array $players)
Met à jour les joueurs d'une partie en base de données.
findPlayersByGameRecordCode(string $code)
Retourne la liste des joueurs dans une partie grâce au code de celle-ci.
findByHostUuid(string $uuid)
Retourne un tableau d'objets GameRecord (ou null) à partir de l'UUID passé en paramètre correspondant...
addWinner(string $code, mixed $uuid)
Enregistre un gagnant d'une partie dans la table cp_won.
hydrate(array $row)
Retourne un enregistrement de partie hydraté
findByState(GameRecordState $state)
Retourne un tableau d'objets GameRecord (ou null) à partir de l'état passé en paramètre.
setPdo(?PDO $pdo)
Modifie la connexion à la base de données.
findByGameIdAndState(int $gameId, GameRecordState $state)
Retourne la liste des parties grâce à l'ID du jeu.
__construct(?PDO $pdo)
Constructeur de la classe GameRecordDAO.
getCreatedAt()
Getter de l'attribut createdAt.
getToken()
Getter de l'attribut token.
isPrivate()
Getter de l'attribut isPrivate.
getUpdatedAt()
Getter de l'attribut updatedAt.
getFinishedAt()
Getter de l'attribut finishedAt.
getGame()
Getter de l'attribut game.
getCode()
Getter de l'attribut uuid.
getHostedBy()
Getter de l'attribut hostedBy.
addPlayer(Player $player)
Ajoute un joueur à la partie.
getState()
Getter de l'attribut state.
getUuid()
Retourne l'UUID du joueur.
GameRecordState
Enumération des états d'une partie.
@ UNKNOWN
L'état WAITING indique que la partie est en attente de joueurs pour démarrer.
const DB_PREFIX
Préfixe des tables de la base de données.
Definition db.php:52