From 073843191abd7a1b5b15557e1042615792468d2b Mon Sep 17 00:00:00 2001 From: Jesper Beisner Date: Mon, 29 Aug 2022 08:45:38 +0200 Subject: [PATCH] add race and profession changes to profile --- .../Factory/ProfileControllerFactory.php | 10 +++ src/Controller/ProfileController.php | 8 ++ .../PlayerProfessionHistoryRepository.php | 50 +++++++++++ .../PlayerRaceHistoryRepository.php | 55 +++++++++++- views/profile/profile.phtml | 84 +++++++++++++++++++ 5 files changed, 205 insertions(+), 2 deletions(-) diff --git a/src/Controller/Factory/ProfileControllerFactory.php b/src/Controller/Factory/ProfileControllerFactory.php index f297375..f812741 100644 --- a/src/Controller/Factory/ProfileControllerFactory.php +++ b/src/Controller/Factory/ProfileControllerFactory.php @@ -6,6 +6,8 @@ use Jesperbeisner\Fwstats\Controller\ProfileController; use Jesperbeisner\Fwstats\Repository\PlayerNameHistoryRepository; +use Jesperbeisner\Fwstats\Repository\PlayerProfessionHistoryRepository; +use Jesperbeisner\Fwstats\Repository\PlayerRaceHistoryRepository; use Jesperbeisner\Fwstats\Repository\PlayerRepository; use Jesperbeisner\Fwstats\Service\PlaytimeService; use Jesperbeisner\Fwstats\Stdlib\Interface\FactoryInterface; @@ -28,11 +30,19 @@ public function __invoke(ContainerInterface $serviceContainer, string $serviceNa /** @var PlayerNameHistoryRepository $playerNameHistoryRepository */ $playerNameHistoryRepository = $serviceContainer->get(PlayerNameHistoryRepository::class); + /** @var PlayerRaceHistoryRepository $playerRaceHistoryRepository */ + $playerRaceHistoryRepository = $serviceContainer->get(PlayerRaceHistoryRepository::class); + + /** @var PlayerProfessionHistoryRepository $playerProfessionHistoryRepository */ + $playerProfessionHistoryRepository = $serviceContainer->get(PlayerProfessionHistoryRepository::class); + return new ProfileController( $request, $playerRepository, $playtimeService, $playerNameHistoryRepository, + $playerRaceHistoryRepository, + $playerProfessionHistoryRepository, ); } } diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index 5b27467..226e3c4 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -8,6 +8,8 @@ use Jesperbeisner\Fwstats\Enum\WorldEnum; use Jesperbeisner\Fwstats\Model\Player; use Jesperbeisner\Fwstats\Repository\PlayerNameHistoryRepository; +use Jesperbeisner\Fwstats\Repository\PlayerProfessionHistoryRepository; +use Jesperbeisner\Fwstats\Repository\PlayerRaceHistoryRepository; use Jesperbeisner\Fwstats\Repository\PlayerRepository; use Jesperbeisner\Fwstats\Service\PlaytimeService; use Jesperbeisner\Fwstats\Stdlib\Exception\NotFoundException; @@ -22,6 +24,8 @@ public function __construct( private readonly PlayerRepository $playerRepository, private readonly PlaytimeService $playtimeService, private readonly PlayerNameHistoryRepository $playerNameHistoryRepository, + private readonly PlayerRaceHistoryRepository $playerRaceHistoryRepository, + private readonly PlayerProfessionHistoryRepository $playerProfessionHistoryRepository, ) { } @@ -47,6 +51,8 @@ public function profile(): ResponseInterface [$totalPlaytime, $averagePlaytime] = $this->getTotalAndAveragePlaytime($player, $weeklyPlaytimes); $nameChanges = $this->playerNameHistoryRepository->getNameChangesForPlayer($player); + $raceChanges = $this->playerRaceHistoryRepository->getRaceChangesForPlayer($player); + $professionChanges = $this->playerProfessionHistoryRepository->getProfessionChangesForPlayer($player); return new HtmlResponse('profile/profile.phtml', [ 'player' => $player, @@ -54,6 +60,8 @@ public function profile(): ResponseInterface 'totalPlaytime' => $totalPlaytime, 'averagePlaytime' => $averagePlaytime, 'nameChanges' => $nameChanges, + 'raceChanges' => $raceChanges, + 'professionChanges' => $professionChanges, ]); } diff --git a/src/Repository/PlayerProfessionHistoryRepository.php b/src/Repository/PlayerProfessionHistoryRepository.php index 8d333e0..fee9547 100644 --- a/src/Repository/PlayerProfessionHistoryRepository.php +++ b/src/Repository/PlayerProfessionHistoryRepository.php @@ -4,7 +4,10 @@ namespace Jesperbeisner\Fwstats\Repository; +use DateTimeImmutable; +use Jesperbeisner\Fwstats\Enum\WorldEnum; use Jesperbeisner\Fwstats\Model\PlayerProfessionHistory; +use Jesperbeisner\Fwstats\Stdlib\Interface\PlayerInterface; final class PlayerProfessionHistoryRepository extends AbstractRepository { @@ -26,6 +29,33 @@ public function insert(PlayerProfessionHistory $playerProfessionHistory): void ]); } + /** + * @return PlayerProfessionHistory[] + */ + public function getProfessionChangesForPlayer(PlayerInterface $player): array + { + $sql = "SELECT * FROM $this->table WHERE world = :world AND player_id = :playerId ORDER BY created DESC"; + + $stmt = $this->pdo->prepare($sql); + $stmt->execute(['world' => $player->getWorld()->value, 'playerId' => $player->getPlayerId()]); + + $playerProfessionHistories = []; + while (false !== $row = $stmt->fetch()) { + /** + * @var array{ + * world: string, + * player_id: int, + * old_profession: string|null, + * new_profession: string|null, + * created: string + * } $row + */ + $playerProfessionHistories[] = $this->hydratePlayerProfessionHistory($row); + } + + return $playerProfessionHistories; + } + public function deleteAll(): void { $sql = "DELETE FROM $this->table"; @@ -33,4 +63,24 @@ public function deleteAll(): void $stmt = $this->pdo->prepare($sql); $stmt->execute(); } + + /** + * @param array{ + * world: string, + * player_id: int, + * old_profession: string|null, + * new_profession: string|null, + * created: string + * } $row + */ + private function hydratePlayerProfessionHistory(array $row): PlayerProfessionHistory + { + return new PlayerProfessionHistory( + world: WorldEnum::from($row['world']), + playerId: $row['player_id'], + oldProfession: $row['old_profession'], + newProfession: $row['new_profession'], + created: new DateTimeImmutable($row['created']), + ); + } } diff --git a/src/Repository/PlayerRaceHistoryRepository.php b/src/Repository/PlayerRaceHistoryRepository.php index ab3fd88..f7b5614 100644 --- a/src/Repository/PlayerRaceHistoryRepository.php +++ b/src/Repository/PlayerRaceHistoryRepository.php @@ -4,7 +4,10 @@ namespace Jesperbeisner\Fwstats\Repository; +use DateTimeImmutable; +use Jesperbeisner\Fwstats\Enum\WorldEnum; use Jesperbeisner\Fwstats\Model\PlayerRaceHistory; +use Jesperbeisner\Fwstats\Stdlib\Interface\PlayerInterface; final class PlayerRaceHistoryRepository extends AbstractRepository { @@ -13,8 +16,8 @@ final class PlayerRaceHistoryRepository extends AbstractRepository public function insert(PlayerRaceHistory $playerRaceHistory): void { $sql = <<table} (world, player_id, old_race, new_race) - VALUES (:world, :playerId, :oldRace, :newRace) + INSERT INTO {$this->table} (world, player_id, old_race, new_race, created) + VALUES (:world, :playerId, :oldRace, :newRace, :created) SQL; $this->pdo->prepare($sql)->execute([ @@ -22,9 +25,37 @@ public function insert(PlayerRaceHistory $playerRaceHistory): void 'playerId' => $playerRaceHistory->playerId, 'oldRace' => $playerRaceHistory->oldRace, 'newRace' => $playerRaceHistory->newRace, + 'created' => $playerRaceHistory->created->format('Y-m-d H:i:s') ]); } + /** + * @return PlayerRaceHistory[] + */ + public function getRaceChangesForPlayer(PlayerInterface $player): array + { + $sql = "SELECT * FROM $this->table WHERE world = :world AND player_id = :playerId ORDER BY created DESC"; + + $stmt = $this->pdo->prepare($sql); + $stmt->execute(['world' => $player->getWorld()->value, 'playerId' => $player->getPlayerId()]); + + $playerRaceHistories = []; + while (false !== $row = $stmt->fetch()) { + /** + * @var array{ + * world: string, + * player_id: int, + * old_race: string, + * new_race: string, + * created: string + * } $row + */ + $playerRaceHistories[] = $this->hydratePlayerRaceHistory($row); + } + + return $playerRaceHistories; + } + public function deleteAll(): void { $sql = "DELETE FROM $this->table"; @@ -32,4 +63,24 @@ public function deleteAll(): void $stmt = $this->pdo->prepare($sql); $stmt->execute(); } + + /** + * @param array{ + * world: string, + * player_id: int, + * old_race: string, + * new_race: string, + * created: string + * } $row + */ + private function hydratePlayerRaceHistory(array $row): PlayerRaceHistory + { + return new PlayerRaceHistory( + world: WorldEnum::from($row['world']), + playerId: $row['player_id'], + oldRace: $row['old_race'], + newRace: $row['new_race'], + created: new DateTimeImmutable($row['created']), + ); + } } diff --git a/views/profile/profile.phtml b/views/profile/profile.phtml index c709382..fd3a99c 100644 --- a/views/profile/profile.phtml +++ b/views/profile/profile.phtml @@ -6,6 +6,8 @@ use Jesperbeisner\Fwstats\DTO\Playtime; use Jesperbeisner\Fwstats\Helper\Html; use Jesperbeisner\Fwstats\Model\Player; use Jesperbeisner\Fwstats\Model\PlayerNameHistory; +use Jesperbeisner\Fwstats\Model\PlayerProfessionHistory; +use Jesperbeisner\Fwstats\Model\PlayerRaceHistory; use Jesperbeisner\Fwstats\Service\ViewRenderService; /** @var ViewRenderService $this */ @@ -25,6 +27,12 @@ $averagePlaytime = $this->vars['averagePlaytime']; /** @var PlayerNameHistory[] $nameChanges */ $nameChanges = $this->vars['nameChanges']; +/** @var PlayerRaceHistory[] $raceChanges */ +$raceChanges = $this->vars['raceChanges']; + +/** @var PlayerProfessionHistory[] $professionChanges */ +$professionChanges = $this->vars['professionChanges']; + $this->setTitle($player->name . ' - ' . $player->world->worldString()); ?> @@ -115,4 +123,80 @@ $this->setTitle($player->name . ' - ' . $player->world->worldString()); + +
+
+

+ Rassenänderungen +

+
+
+
+ 0): ?> +
+ + + + + + + + + + + + + + + + + +
Alte RasseNeue RasseDatum
oldRace ?>newRace ?>created->format('d.m.Y') ?>
+
+ +

+ Keine Rassenänderungen vorhanden. +

+ +
+
+
+ +
+
+

+ Berufsänderungen +

+
+
+
+ 0): ?> +
+ + + + + + + + + + + + + + + + + +
Alter BerufNeuer BerufDatum
oldProfession ?? '-' ?>newProfession ?? '-' ?>created->format('d.m.Y') ?>
+
+ +

+ Keine Berufsänderungen vorhanden. +

+ +
+
+