From e5fb725c69d089da699bbb05be9dd398f72b807c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 19 Mar 2021 22:49:22 +0100 Subject: [PATCH] MAPG-203 add new table and model for multi_room --- .../structure/20210318_2136_multiplayer.sql | 9 ++ src/PersistentData/Model/MultiRoom.php | 88 +++++++++++++++++++ src/Repository/MultiRoomRepository.php | 38 ++++++++ 3 files changed, 135 insertions(+) create mode 100644 database/migrations/structure/20210318_2136_multiplayer.sql create mode 100644 src/PersistentData/Model/MultiRoom.php create mode 100644 src/Repository/MultiRoomRepository.php diff --git a/database/migrations/structure/20210318_2136_multiplayer.sql b/database/migrations/structure/20210318_2136_multiplayer.sql new file mode 100644 index 0000000..66887ad --- /dev/null +++ b/database/migrations/structure/20210318_2136_multiplayer.sql @@ -0,0 +1,9 @@ +CREATE TABLE `multi_rooms` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `room_id` varchar(6) NOT NULL, + `state` text NOT NULL, + `members` text NOT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `room_id` (`room_id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; diff --git a/src/PersistentData/Model/MultiRoom.php b/src/PersistentData/Model/MultiRoom.php new file mode 100644 index 0000000..9cc137b --- /dev/null +++ b/src/PersistentData/Model/MultiRoom.php @@ -0,0 +1,88 @@ +roomId = $roomId; + } + + public function setStateArray(array $state): void + { + $this->state = $state; + } + + public function setMembersArray(array $members): void + { + $this->members = $members; + } + + public function setState(string $state): void + { + $this->state = unserialize($state); + } + + public function setMembers(string $members): void + { + $this->members = unserialize($members); + } + + public function setUpdatedDate(DateTime $updated): void + { + $this->updated = $updated; + } + + public function setUpdated(string $updated): void + { + $this->updated = new DateTime($updated); + } + + public function getRoomId(): string + { + return $this->roomId; + } + + public function getStateArray(): array + { + return $this->state; + } + + public function getState(): string + { + return serialize($this->state); + } + + public function getMembersArray(): array + { + return $this->members; + } + + public function getMembers(): string + { + return serialize($this->members); + } + + public function getUpdatedDate(): DateTime + { + return $this->updated; + } + + public function getUpdated(): string + { + return $this->updated->format('Y-m-d H:i:s'); + } +} diff --git a/src/Repository/MultiRoomRepository.php b/src/Repository/MultiRoomRepository.php new file mode 100644 index 0000000..86c2590 --- /dev/null +++ b/src/Repository/MultiRoomRepository.php @@ -0,0 +1,38 @@ +pdm = new PersistentDataManager(); + } + + public function getById(int $id): ?MultiRoom + { + return $this->pdm->selectFromDbById($id, MultiRoom::class); + } + + public function getByRoomId(string $roomId): ?MultiRoom + { + $select = new Select(\Container::$dbConnection); + $select->where('room_id', '=', $roomId); + + return $this->pdm->selectFromDb($select, MultiRoom::class); + } + + public function getAllExpired(): Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('updated', '<', (new DateTime('-7 day'))->format('Y-m-d H:i:s')); + + yield from $this->pdm->selectMultipleFromDb($select, MultiRoom::class); + } +}