new: usr: add new profile charts and stats - & add new logo to the tech stack #5
This commit is contained in:
@@ -112,16 +112,22 @@ class ProfileController extends AbstractController
|
||||
|
||||
$months = array_column(array_values($monthlyData), 'label');
|
||||
|
||||
$bonus = $this->repo->findBonusStatsForUser($user);
|
||||
|
||||
return $this->render('Security/profile.html.twig', [
|
||||
'stats' => [
|
||||
'total' => $total,
|
||||
'wins' => $wins,
|
||||
'losses' => $losses,
|
||||
'draws' => $draws,
|
||||
'bombs' => $this->repo->countBombsForUser($user),
|
||||
'winRate' => $total > 0 ? (int)round($wins / $total * 100) : 0,
|
||||
'avgScore' => $this->repo->findAvgScoreForUser($user),
|
||||
'bestScore' => $this->repo->findBestScoreForUser($user),
|
||||
'total' => $total,
|
||||
'wins' => $wins,
|
||||
'losses' => $losses,
|
||||
'draws' => $draws,
|
||||
'minesHit' => $this->repo->findTotalMinesForUser($user),
|
||||
'winRate' => $total > 0 ? (int)round($wins / $total * 100) : 0,
|
||||
'avgScore' => $this->repo->findAvgScoreForUser($user),
|
||||
'bonusPoints' => $bonus['totalBonusPoints'],
|
||||
'avgBonus' => $bonus['avgBonusPoints'],
|
||||
'bestChain' => $bonus['bestChain'],
|
||||
'blindHits' => $bonus['totalBlindHits'],
|
||||
'edgeMines' => $bonus['totalEdgeMines'],
|
||||
],
|
||||
'recent' => ($recent = $this->repo->findRecentFinishedForUser($user)),
|
||||
'gamesData' => array_map(function (PlayedGame $game) use ($userId, $cacheManager): array {
|
||||
@@ -170,17 +176,41 @@ class ProfileController extends AbstractController
|
||||
];
|
||||
}, $recent),
|
||||
'chartData' => [
|
||||
'months' => $months,
|
||||
'wins' => array_column(array_values($monthlyData), 'wins'),
|
||||
'losses' => array_column(array_values($monthlyData), 'losses'),
|
||||
'draws' => array_column(array_values($monthlyData), 'draws'),
|
||||
'pieWins' => $wins,
|
||||
'pieLosses' => $losses,
|
||||
'pieDraws' => $draws,
|
||||
'months' => $months,
|
||||
'wins' => array_column(array_values($monthlyData), 'wins'),
|
||||
'losses' => array_column(array_values($monthlyData), 'losses'),
|
||||
'draws' => array_column(array_values($monthlyData), 'draws'),
|
||||
'pieWins' => $wins,
|
||||
'pieLosses' => $losses,
|
||||
'pieDraws' => $draws,
|
||||
'recentGames' => $this->buildRecentGamesSeries($user, $userId),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build per-game data for the last 15 finished games, oldest → newest.
|
||||
*
|
||||
* @return array{labels:string[],mines:int[],bonus:float[]}
|
||||
*/
|
||||
private function buildRecentGamesSeries(User $user, int $userId): array
|
||||
{
|
||||
$recent = $this->repo->findRecentFinishedForUser($user, 15);
|
||||
$recent = array_reverse($recent);
|
||||
|
||||
$labels = [];
|
||||
$mines = [];
|
||||
$bonus = [];
|
||||
foreach ($recent as $i => $game) {
|
||||
$isRed = $game->getRed()?->getId() === $userId;
|
||||
$labels[] = '#' . ($i + 1);
|
||||
$mines[] = (int) ($isRed ? $game->getRedPoints() : $game->getBluePoints());
|
||||
$bonus[] = (float) ($isRed ? $game->getRedBonusPoints() : $game->getBlueBonusPoints()) ?: 0;
|
||||
}
|
||||
|
||||
return ['labels' => $labels, 'mines' => $mines, 'bonus' => $bonus];
|
||||
}
|
||||
|
||||
#[Route(
|
||||
'/battle/{uuid}',
|
||||
name: 'MineSeekerBundle_battle_share',
|
||||
|
||||
@@ -260,6 +260,21 @@ class PlayedGameRepository extends ServiceEntityRepository
|
||||
}
|
||||
}
|
||||
|
||||
public function findTotalMinesForUser(User $user): int
|
||||
{
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
|
||||
$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->getId()],
|
||||
)->fetchAssociative();
|
||||
|
||||
return (int) ($result['total_pts'] ?? 0);
|
||||
}
|
||||
|
||||
public function findAvgScoreForUser(User $user): int
|
||||
{
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
@@ -284,6 +299,49 @@ class PlayedGameRepository extends ServiceEntityRepository
|
||||
return (int) round((float) $result['total_pts'] / (int) $result['total_games']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$userId = $user->getId();
|
||||
$qb = $this->createQueryBuilder('g');
|
||||
$qb->where($qb->expr()->orX(
|
||||
$qb->expr()->eq('g.red', ':u'),
|
||||
$qb->expr()->eq('g.blue', ':u'),
|
||||
))->setParameter('u', $user);
|
||||
|
||||
/** @var PlayedGame[] $games */
|
||||
$games = $qb->getQuery()->getResult();
|
||||
|
||||
$totalBonusPoints = 0.0;
|
||||
$bestChain = 0;
|
||||
$totalBlindHits = 0;
|
||||
$totalEdgeMines = 0;
|
||||
$gameCount = 0;
|
||||
|
||||
foreach ($games as $game) {
|
||||
$isRed = $game->getRed()?->getId() === $userId;
|
||||
$totalBonusPoints += (float) (($isRed ? $game->getRedBonusPoints() : $game->getBlueBonusPoints()) ?? 0.0);
|
||||
|
||||
$stats = ($isRed ? $game->getRedBonusStats() : $game->getBlueBonusStats()) ?? [];
|
||||
$bestChain = max($bestChain, (int) ($stats['chainBest'] ?? 0));
|
||||
$totalBlindHits += (int) ($stats['blindHits'] ?? 0);
|
||||
$totalEdgeMines += (int) ($stats['edgeMines'] ?? 0);
|
||||
$gameCount++;
|
||||
}
|
||||
|
||||
return [
|
||||
'totalBonusPoints' => round($totalBonusPoints, 1),
|
||||
'avgBonusPoints' => 0 < $gameCount ? round($totalBonusPoints / $gameCount, 1) : 0.0,
|
||||
'bestChain' => $bestChain,
|
||||
'totalBlindHits' => $totalBlindHits,
|
||||
'totalEdgeMines' => $totalEdgeMines,
|
||||
];
|
||||
}
|
||||
|
||||
public function findBestScoreForUser(User $user): int
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user