chg: dev: refactor the code - there was unnecessary codes and wrongly formatted or designed code that are related to Repositories #7

This commit is contained in:
Lang
2026-04-20 11:10:00 +02:00
parent cd93a26c2c
commit f493f94368
7 changed files with 122 additions and 143 deletions
+65 -88
View File
@@ -14,6 +14,7 @@ use App\Entity\PlayedGame;
use App\Entity\User;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
@@ -34,6 +35,7 @@ use RuntimeException;
* @method PlayedGame|null findOneBy(array $criteria, array $orderBy = null)
* @method PlayedGame[] findAll()
* @method PlayedGame[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
* @method PlayedGame|null findOneByGameAssoc(mixed $gameAssoc)
*/
class PlayedGameRepository extends ServiceEntityRepository
{
@@ -42,32 +44,12 @@ class PlayedGameRepository extends ServiceEntityRepository
parent::__construct($registry, PlayedGame::class);
}
public function findOneByGameAssoc(string $gameAssoc): ?PlayedGame
{
$qb = $this->createQueryBuilder('p');
try {
return $qb
->where($qb->expr()->eq('p.gameAssoc', ':gameAssoc'))
->setParameter('gameAssoc', $gameAssoc)
->getQuery()
->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
$this->logger->error($e->getMessage());
throw new RuntimeException(
"Unexpectedly multiple results found when looking up gameAssoc: $gameAssoc",
0,
$e,
);
}
}
public function countFinishedForUser(User $user): int
{
$qb = $this->createQueryBuilder('g');
try {
return (int) $qb
return (int)$qb
->select('COUNT(g.id)')
->where($qb->expr()->andX(
$qb->expr()->orX(
@@ -104,7 +86,7 @@ class PlayedGameRepository extends ServiceEntityRepository
$qb = $this->createQueryBuilder('g');
try {
return (int) $qb
return (int)$qb
->select('COUNT(g.id)')
->where($qb->expr()->orX(
$qb->expr()->andX(
@@ -153,7 +135,7 @@ class PlayedGameRepository extends ServiceEntityRepository
$qb = $this->createQueryBuilder('g');
try {
return (int) $qb
return (int)$qb
->select('COUNT(g.id)')
->where($qb->expr()->orX(
$qb->expr()->andX(
@@ -202,18 +184,19 @@ class PlayedGameRepository extends ServiceEntityRepository
$qb = $this->createQueryBuilder('g');
try {
return (int) $qb
return (int)$qb
->select('COUNT(g.id)')
->where($qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->eq('g.red', ':u'),
$qb->expr()->eq('g.redExplodedBomb', 'true'),
$qb->expr()->eq('g.redExplodedBomb', ':true'),
),
$qb->expr()->andX(
$qb->expr()->eq('g.blue', ':u'),
$qb->expr()->eq('g.blueExplodedBomb', 'true'),
$qb->expr()->eq('g.blueExplodedBomb', ':true'),
),
))
->setParameter('true', true, Types::BOOLEAN)
->setParameter('u', $user)
->getQuery()
->getSingleScalarResult();
@@ -238,71 +221,69 @@ class PlayedGameRepository extends ServiceEntityRepository
{
$qb = $this->createQueryBuilder('g');
try {
return (int) $qb
->select('COUNT(g.id)')
->where($qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->eq('g.red', ':u'),
$qb->expr()->eq('g.blue', ':u'),
),
$qb->expr()->isNotNull('g.redPoints'),
$qb->expr()->isNotNull('g.bluePoints'),
$qb->expr()->isNull('g.resign'),
'g.redPoints = g.bluePoints',
))
->setParameter('u', $user)
->getQuery()
->getSingleScalarResult();
} catch (NoResultException | NonUniqueResultException $e) {
$this->logger->error($e->getMessage());
return 0;
}
return (int)$qb
->select('COUNT(g.id)')
->where($qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->eq('g.red', ':u'),
$qb->expr()->eq('g.blue', ':u'),
),
$qb->expr()->isNotNull('g.redPoints'),
$qb->expr()->isNotNull('g.bluePoints'),
$qb->expr()->isNull('g.resign'),
'g.redPoints = g.bluePoints',
))
->setParameter('u', $user)
->getQuery()
->getSingleScalarResult();
}
public function findTotalMinesForUser(User $user): int
{
$conn = $this->getEntityManager()->getConnection();
$qb = $this->createQueryBuilder('g');
$result = $conn->executeQuery(
'SELECT
COALESCE(SUM(CASE WHEN g.red_id = :uid THEN g.red_points ELSE g.blue_points END), 0) AS total_pts
FROM played_game g
WHERE (g.red_id = :uid OR g.blue_id = :uid)',
['uid' => $user->id],
)->fetchAssociative();
return (int) ($result['total_pts'] ?? 0);
return (int)$qb
->select('COALESCE(SUM(CASE WHEN g.red = :u THEN g.redPoints ELSE g.bluePoints END), 0)')
->where($qb->expr()->orX(
$qb->expr()->eq('g.red', ':u'),
$qb->expr()->eq('g.blue', ':u'),
))
->setParameter('u', $user)
->getQuery()
->getSingleScalarResult();
}
public function findAvgScoreForUser(User $user): int
{
$conn = $this->getEntityManager()->getConnection();
$qb = $this->createQueryBuilder('g');
$result = $conn->executeQuery(
'SELECT
SUM(CASE WHEN g.red_id = :uid THEN g.red_points ELSE g.blue_points END) AS total_pts,
COUNT(g.id) AS total_games
FROM played_game g
WHERE (g.red_id = :uid OR g.blue_id = :uid)
AND (
(g.red_id = :uid AND g.red_points IS NOT NULL)
OR (g.blue_id = :uid AND g.blue_points IS NOT NULL)
)',
['uid' => $user->id],
)->fetchAssociative();
/** @var array{totalPts: int|string|null, totalGames: int|string} $row */
$row = $qb
->select('SUM(CASE WHEN g.red = :u THEN g.redPoints ELSE g.bluePoints END) AS totalPts')
->addSelect('COUNT(g.id) AS totalGames')
->where($qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->eq('g.red', ':u'),
$qb->expr()->isNotNull('g.redPoints'),
),
$qb->expr()->andX(
$qb->expr()->eq('g.blue', ':u'),
$qb->expr()->isNotNull('g.bluePoints'),
),
))
->setParameter('u', $user)
->getQuery()
->getSingleResult();
if (!$result || (int) $result['total_games'] === 0) {
if ((int)$row['totalGames'] === 0) {
return 0;
}
return (int) round((float) $result['total_pts'] / (int) $result['total_games']);
return (int)round((float)$row['totalPts'] / (int)$row['totalGames']);
}
/**
* Aggregates bonus points and bonus stats across all finished games for a user.
*
* @return array{totalBonusPoints:float,avgBonusPoints:float,bestChain:int,totalBlindHits:int,totalEdgeMines:int}
*/
public function findBonusStatsForUser(User $user): array
{
@@ -324,12 +305,12 @@ class PlayedGameRepository extends ServiceEntityRepository
foreach ($games as $game) {
$isRed = $game->red?->id === $userId;
$totalBonusPoints += (float) (($isRed ? $game->redBonusPoints : $game->blueBonusPoints) ?? 0.0);
$totalBonusPoints += (float)(($isRed ? $game->redBonusPoints : $game->blueBonusPoints) ?? 0.0);
$stats = ($isRed ? $game->redBonusStats : $game->blueBonusStats) ?? [];
$bestChain = max($bestChain, (int) ($stats['chainBest'] ?? 0));
$totalBlindHits += (int) ($stats['blindHits'] ?? 0);
$totalEdgeMines += (int) ($stats['edgeMines'] ?? 0);
$bestChain = max($bestChain, (int)($stats['chainBest'] ?? 0));
$totalBlindHits += (int)($stats['blindHits'] ?? 0);
$totalEdgeMines += (int)($stats['edgeMines'] ?? 0);
$gameCount++;
}
@@ -346,7 +327,7 @@ class PlayedGameRepository extends ServiceEntityRepository
{
try {
$qbRed = $this->createQueryBuilder('g');
$maxRed = (int) $qbRed
$maxRed = (int)$qbRed
->select('MAX(g.redPoints)')
->where($qbRed->expr()->eq('g.red', ':u'))
->setParameter('u', $user)
@@ -354,7 +335,7 @@ class PlayedGameRepository extends ServiceEntityRepository
->getSingleScalarResult();
$qbBlue = $this->createQueryBuilder('g');
$maxBlue = (int) $qbBlue
$maxBlue = (int)$qbBlue
->select('MAX(g.bluePoints)')
->where($qbBlue->expr()->eq('g.blue', ':u'))
->setParameter('u', $user)
@@ -362,15 +343,12 @@ class PlayedGameRepository extends ServiceEntityRepository
->getSingleScalarResult();
return max($maxRed, $maxBlue);
} catch (NoResultException | NonUniqueResultException $e) {
} catch (NoResultException|NonUniqueResultException $e) {
$this->logger->error($e->getMessage());
return 0;
}
}
/**
* @return PlayedGame[]
*/
public function findFinishedForUserSince(User $user, DateTime $since): array
{
$qb = $this->createQueryBuilder('g');
@@ -394,9 +372,6 @@ class PlayedGameRepository extends ServiceEntityRepository
->getResult();
}
/**
* @return PlayedGame[]
*/
public function findRecentFinishedForUser(User $user, int $limit = 10): array
{
$qb = $this->createQueryBuilder('g');
@@ -418,10 +393,12 @@ class PlayedGameRepository extends ServiceEntityRepository
->getResult();
}
/**
* Any legitimately waiting game was updated within the last 10 minutes.
* Abandoned games are stamped with updated = 2000-01-01, so they fail this filter.
*/
public function findWaitingGames(int $limit = 20): array
{
// Any legitimately waiting game was updated within the last 10 minutes.
// Abandoned games are stamped with updated = 2000-01-01, so they fail this filter.
$qb = $this->createQueryBuilder('p');
return $qb