new: usr: add more stats and a dialog for the recent battle that can be shareable #4
This commit is contained in:
@@ -234,6 +234,108 @@ class PlayedGameRepository extends ServiceEntityRepository
|
||||
}
|
||||
}
|
||||
|
||||
public function countDrawsForUser(User $user): int
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
public function findAvgScoreForUser(User $user): int
|
||||
{
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
|
||||
$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->getId()],
|
||||
)->fetchAssociative();
|
||||
|
||||
if (!$result || (int) $result['total_games'] === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) round((float) $result['total_pts'] / (int) $result['total_games']);
|
||||
}
|
||||
|
||||
public function findBestScoreForUser(User $user): int
|
||||
{
|
||||
try {
|
||||
$qbRed = $this->createQueryBuilder('g');
|
||||
$maxRed = (int) $qbRed
|
||||
->select('MAX(g.redPoints)')
|
||||
->where($qbRed->expr()->eq('g.red', ':u'))
|
||||
->setParameter('u', $user)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
|
||||
$qbBlue = $this->createQueryBuilder('g');
|
||||
$maxBlue = (int) $qbBlue
|
||||
->select('MAX(g.bluePoints)')
|
||||
->where($qbBlue->expr()->eq('g.blue', ':u'))
|
||||
->setParameter('u', $user)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
|
||||
return max($maxRed, $maxBlue);
|
||||
} 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');
|
||||
|
||||
return $qb
|
||||
->where($qb->expr()->andX(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->eq('g.red', ':u'),
|
||||
$qb->expr()->eq('g.blue', ':u'),
|
||||
),
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isNotNull('g.redPoints'),
|
||||
$qb->expr()->isNotNull('g.resign'),
|
||||
),
|
||||
$qb->expr()->gte('g.updated', ':since'),
|
||||
))
|
||||
->setParameter('u', $user)
|
||||
->setParameter('since', $since)
|
||||
->orderBy('g.updated', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PlayedGame[]
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user