11namespace ComusParty\Models;
46 $stmt = $this->pdo->prepare(
"SELECT * FROM " .
DB_PREFIX .
"game_record WHERE game_id = :gameId");
47 $stmt->bindParam(
":gameId", $gameId);
49 $stmt->setFetchMode(PDO::FETCH_ASSOC);
50 $gameRecords = $stmt->fetchAll();
66 foreach ($rows as $row) {
67 $gameRecords[] = $this->
hydrate($row);
80 if (!is_null($row[
"hosted_by"])) {
81 $hostedBy = (
new PlayerDAO($this->
getPdo()))->findByUuid($row[
"hosted_by"]);
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,
93 $finishedAt = $row[
"finished_at"] ?
new DateTime($row[
"finished_at"]) :
null;
95 if (!is_null($players)) {
96 $players = array_map(
function ($player) {
97 $playerObject = (
new PlayerDAO($this->
getPdo()))->findByUuid($player[
"player_uuid"]);
99 "player" => $playerObject,
100 "token" => $player[
"token"]
111 $row[
"private"] === 1,
113 new DateTime($row[
"created_at"]),
114 new DateTime($row[
"updated_at"]),
144 $stmt = $this->pdo->prepare(
"SELECT player_uuid, token FROM " .
DB_PREFIX .
"played WHERE game_code = :code");
145 $stmt->bindParam(
":code", $code);
147 $stmt->setFetchMode(PDO::FETCH_ASSOC);
148 $players = $stmt->fetchAll();
164 $stmt = $this->pdo->prepare(
"SELECT * FROM " .
DB_PREFIX .
"game_record WHERE game_id = :gameId AND state = :gameState");
165 $stmt->bindParam(
":gameId", $gameId);
167 $state = match ($state) {
168 GameRecordState::WAITING =>
"waiting",
169 GameRecordState::STARTED =>
"started",
170 GameRecordState::FINISHED =>
"finished",
173 $stmt->bindParam(
":gameState", $state);
176 $stmt->setFetchMode(PDO::FETCH_ASSOC);
177 $gameRecords = $stmt->fetchAll();
193 $stmt = $this->pdo->prepare(
"SELECT * FROM " .
DB_PREFIX .
"game_record WHERE code = :code");
194 $stmt->bindParam(
":code", $code);
196 $stmt->setFetchMode(PDO::FETCH_ASSOC);
198 $gameRecord = $stmt->fetch();
202 return $this->
hydrate($gameRecord);
214 $stmt = $this->pdo->prepare(
"SELECT * FROM " .
DB_PREFIX .
"game_record WHERE hosted_by = :uuid");
215 $stmt->bindParam(
":uuid", $uuid);
217 $stmt->setFetchMode(PDO::FETCH_ASSOC);
219 $gameRecords = $stmt->fetchAll();
236 $state = match ($state) {
237 GameRecordState::WAITING =>
"waiting",
238 GameRecordState::STARTED =>
"started",
239 GameRecordState::FINISHED =>
"finished",
243 $stmt = $this->pdo->prepare(
"SELECT * FROM " .
DB_PREFIX .
"game_record WHERE state = :state");
244 $stmt->bindParam(
":state", $state);
246 $stmt->setFetchMode(PDO::FETCH_ASSOC);
248 $gameRecords = $stmt->fetchAll();
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)");
264 $code = $gameRecord->
getCode();
266 $gameId = $gameRecord->
getGame()->getId();
268 $state = match ($gameRecord->
getState()) {
269 GameRecordState::WAITING =>
"waiting",
270 GameRecordState::STARTED =>
"started",
271 GameRecordState::FINISHED =>
"finished",
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");
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);
289 return $stmt->execute();
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");
303 $state = match ($gameRecord->
getState()) {
304 GameRecordState::WAITING =>
"waiting",
305 GameRecordState::STARTED =>
"started",
306 GameRecordState::FINISHED =>
"finished",
310 $finishedAt = $gameRecord->
getFinishedAt()?->format(
"Y-m-d H:i:s");
311 $code = $gameRecord->
getCode();
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);
320 return $stmt->execute();
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()) {
349 public function delete(
string $code): bool
351 $stmt = $this->pdo->prepare(
"DELETE FROM " .
DB_PREFIX .
"game_record WHERE code = :code");
352 $stmt->bindParam(
":code", $code);
353 return $stmt->execute();
364 $stmt = $this->pdo->prepare(
"INSERT INTO " .
DB_PREFIX .
"played (game_code, player_uuid) VALUES (:gameCode, :playerUuid)");
366 $playerUuid = $player->
getUuid();
367 $gameCode = $gameRecord->
getCode();
368 $stmt->bindParam(
":gameCode", $gameCode);
369 $stmt->bindParam(
":playerUuid", $playerUuid);
370 return $stmt->execute();
379 public function removePlayer(
string $gameCode,
string $playerUuid): bool
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();
393 public function addWinner(
string $code, mixed $uuid): bool
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();
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.