Skip to content

Commit

Permalink
add race and profession changes to profile
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperbeisner committed Aug 29, 2022
1 parent 69db7ce commit 0738431
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Controller/Factory/ProfileControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
);
}
}
8 changes: 8 additions & 0 deletions src/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

Expand All @@ -47,13 +51,17 @@ 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,
'weeklyPlaytimes' => $weeklyPlaytimes,
'totalPlaytime' => $totalPlaytime,
'averagePlaytime' => $averagePlaytime,
'nameChanges' => $nameChanges,
'raceChanges' => $raceChanges,
'professionChanges' => $professionChanges,
]);
}

Expand Down
50 changes: 50 additions & 0 deletions src/Repository/PlayerProfessionHistoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -26,11 +29,58 @@ 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";

$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']),
);
}
}
55 changes: 53 additions & 2 deletions src/Repository/PlayerRaceHistoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -13,23 +16,71 @@ final class PlayerRaceHistoryRepository extends AbstractRepository
public function insert(PlayerRaceHistory $playerRaceHistory): void
{
$sql = <<<SQL
INSERT INTO {$this->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([
'world' => $playerRaceHistory->world->value,
'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";

$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']),
);
}
}
84 changes: 84 additions & 0 deletions views/profile/profile.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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());

?>
Expand Down Expand Up @@ -115,4 +123,80 @@ $this->setTitle($player->name . ' - ' . $player->world->worldString());
</div>
</div>
</div>

<div class="card mt-5">
<div class="card-header">
<p class="card-header-title">
Rassenänderungen
</p>
</div>
<div class="card-content">
<div class="content">
<?php if (count($raceChanges) > 0): ?>
<div class="table-container">
<table class="table is-hoverable is-fullwidth">
<thead>
<tr>
<td>Alte Rasse</td>
<td>Neue Rasse</td>
<td>Datum</td>
</tr>
</thead>
<tbody>
<?php foreach ($raceChanges as $raceChange): ?>
<tr>
<td><?= $raceChange->oldRace ?></td>
<td><?= $raceChange->newRace ?></td>
<td><?= $raceChange->created->format('d.m.Y') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p class="has-text-centered">
Keine Rassenänderungen vorhanden.
</p>
<?php endif; ?>
</div>
</div>
</div>

<div class="card mt-5 mb-5">
<div class="card-header">
<p class="card-header-title">
Berufsänderungen
</p>
</div>
<div class="card-content">
<div class="content">
<?php if (count($professionChanges) > 0): ?>
<div class="table-container">
<table class="table is-hoverable is-fullwidth">
<thead>
<tr>
<td>Alter Beruf</td>
<td>Neuer Beruf</td>
<td>Datum</td>
</tr>
</thead>
<tbody>
<?php foreach ($professionChanges as $professionChange): ?>
<tr>
<td><?= $professionChange->oldProfession ?? '-' ?></td>
<td><?= $professionChange->newProfession ?? '-' ?></td>
<td><?= $professionChange->created->format('d.m.Y') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p class="has-text-centered">
Keine Berufsänderungen vorhanden.
</p>
<?php endif; ?>
</div>
</div>
</div>
</div>

0 comments on commit 0738431

Please sign in to comment.