src/Controller/PageController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Predis\Client;
  4. use Doctrine\DBAL\Connection;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. class PageController extends BaseController
  11. {   
  12.     
  13.     public function __construct(Client $redisConnection $connection)
  14.     {
  15.         $this->redis $redis;
  16.         $this->connection $connection;
  17.     }
  18.     
  19.     private function getHistory(): array
  20.     {
  21.         try {
  22.             $historyRaw $this->redis->lRange('game_history'09);
  23.         } catch (\Throwable $exception) {
  24.             $historyRaw = [];
  25.         }
  26.         return array_map(function ($item) {
  27.             $data json_decode($itemtrue);
  28.             if (!empty($data['username'])) {
  29.                 $username $data['username'];
  30.                 $length mb_strlen($username'UTF-8');
  31.                 if ($length 6) {
  32.                     $data['username'] = mb_substr($username06'UTF-8') . '****';
  33.                 }
  34.             }
  35.             return $data;
  36.         }, $historyRaw);
  37.     }
  38.     /**
  39.      * @Route("/", name="main_page")
  40.      */
  41.     public function index(): Response
  42.     {
  43.         return $this->render('main.html.twig', [
  44.             'history' => $this->getHistory(),
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/mines", name="mines_page")
  49.      */
  50.     public function mines(): Response
  51.     {
  52.         return $this->render('mines.html.twig', [
  53.             'history' => $this->getHistory(),
  54.         ]);
  55.     }
  56.     
  57.     /**
  58.      * @Route("/dice", name="dice_page")
  59.      */
  60.     public function dice(): Response
  61.     {
  62.         return $this->render('dice.html.twig', [
  63.             'history' => $this->getHistory(),
  64.         ]);
  65.     }
  66.     
  67.     /**
  68.      * @Route("/lobby", name="lobby_page")
  69.      */
  70.     public function lobby(): Response
  71.     {
  72.         return $this->render('lobby.html.twig');
  73.     }
  74.     
  75.      /**
  76.      * @Route("/tournaments", name="tours_page")
  77.      */
  78.     public function tournaments(): Response
  79.     {
  80.         $topPlayers $this->connection->fetchAllAssociative(
  81.             'SELECT login, img, points FROM users WHERE admin = 0 and tournament = 1 ORDER BY points DESC LIMIT 10'
  82.         );
  83.         
  84.         foreach ($topPlayers as &$player) {
  85.             $player['login'] = mb_substr($player['login'], 05).'****';
  86.         }
  87.         $top3 array_slice($topPlayers03);
  88.         $rest array_slice($topPlayers3);
  89.         $prizePool 50000;
  90.         $rewards = [
  91.             => 25000,
  92.             => 15000,
  93.             => 10000,
  94.         ];
  95.         
  96.         $countRow $this->connection->fetchAssociative(
  97.             'SELECT count(*) AS cnt FROM users WHERE admin = 0 and tournament = 1'
  98.         );
  99.         $count $countRow['cnt'];
  100.         return $this->render('tournaments.html.twig', [
  101.             'top3' => $top3,
  102.             'rewards' => $rewards,
  103.             'pool' => $prizePool,
  104.             'count' => $count
  105.         ]);
  106.     }
  107.     
  108.     /**
  109.      * @Route("/bonus", name="bonus_page")
  110.      */
  111.     public function bonus(Request $request): Response
  112.     {   
  113.         $user $this->getAuthorizedUser($request);
  114.         if (!$user) {
  115.             return $this->redirectToRoute('main_page');
  116.         }
  117.         $now = new \DateTime();
  118.         $monthKey $now->format('Y-m');
  119.         $start $monthKey '-01 00:00:00';
  120.         $end $now->format('Y-m-t') . ' 23:59:59';
  121.         $deposits $this->connection->fetchOne(
  122.             "SELECT SUM(suma) FROM deposits 
  123.             WHERE user_id = :uid AND status = 1 
  124.             AND STR_TO_DATE(data, '%d-%m-%Y %H:%i:%s') BETWEEN :start AND :end",
  125.             ['uid' => $user['id'], 'start' => $start'end' => $end]
  126.         ) ?? 0;
  127.         $withdraws $this->connection->fetchOne(
  128.             "SELECT SUM(`sum`) FROM withdraws 
  129.             WHERE user_id = :uid AND status = '1' 
  130.             AND STR_TO_DATE(`date`, '%Y-%m-%d %H:%i:%s') BETWEEN :start AND :end",
  131.             ['uid' => $user['id'], 'start' => $start'end' => $end]
  132.         ) ?? 0;
  133.         $cashback 0;
  134.         if ((float)$deposits > (float)$withdraws) {
  135.             $alreadyReceived $this->connection->fetchOne(
  136.                 'SELECT 1 FROM cashback_logs WHERE user_id = :uid AND month = :month',
  137.                 ['uid' => $user['id'], 'month' => $monthKey]
  138.             );
  139.             if (!$alreadyReceived) {
  140.                 $cashback round(((float)$deposits - (float)$withdraws) * 0.082);
  141.             }
  142.         }
  143.         return $this->render('bonus.html.twig', [
  144.             'cashback' => $cashback
  145.         ]);
  146.     }
  147.     /**
  148.      * @Route("/referral", name="referral_page")
  149.      */
  150.     public function referralPage(Request $request): Response
  151.     {
  152.         $user $this->getAuthorizedUser($request);
  153.         if (!$user) {
  154.             return $this->redirectToRoute('main_page');
  155.         }
  156.         $refearn = (float) $user['refearn'];
  157.         $page    max(1, (int) $request->query->get('page'1));
  158.         $limit   5;
  159.         $offset  = ($page 1) * $limit;
  160.         $referrals $this->connection->fetchAllAssociative(
  161.             "SELECT id, login AS name, img AS avatar, created_at
  162.              FROM users
  163.              WHERE ref_id = :uid
  164.              ORDER BY created_at DESC
  165.              LIMIT :limit OFFSET :offset",
  166.             ['uid' => $user['id'], 'limit' => $limit'offset' => $offset],
  167.             ['uid' => \PDO::PARAM_INT'limit' => \PDO::PARAM_INT'offset' => \PDO::PARAM_INT]
  168.         );
  169.         $total        = (int) $this->connection->fetchOne(
  170.             "SELECT COUNT(*) FROM users WHERE ref_id = ?",
  171.             [$user['id']]
  172.         );
  173.         $hasNextPage  = ($offset $limit) < $total;
  174.         if ($request->isXmlHttpRequest()) {
  175.             $html $this->renderView('components/referral_table_rows.html.twig', [
  176.                 'referrals' => $referrals,
  177.             ]);
  178.             return new JsonResponse([
  179.                 'html'          => $html,
  180.                 'page'          => $page,
  181.                 'has_next_page' => $hasNextPage,
  182.                 'total'         => $total,
  183.             ]);
  184.         }
  185.         $levelInfo $this->getLevelInfo($refearn);
  186.         $progressPercent $levelInfo['next'] === null
  187.             100
  188.             min(100round(100 * ($refearn / ($refearn $levelInfo['next'])), 2));
  189.         return $this->render('referral.html.twig', [
  190.             'user' => $user,
  191.             'referrals' => $referrals,
  192.             'page' => $page,
  193.             'has_next_page' => $hasNextPage,
  194.             'level' => $levelInfo['level'],
  195.             'percent' => $levelInfo['percent'],
  196.             'next_level_amount' => $levelInfo['next'],
  197.             'available_to_withdraw' => $user['refbal'],
  198.             'progress_percent' => $progressPercent,
  199.             'total_referrals' => $total
  200.         ]);
  201.     }
  202.     private function getLevelInfo(float $refearn): array
  203.     {
  204.         if ($refearn >= 100000) return ['level' => 5'percent' => 9'next' => null];
  205.         if ($refearn >= 50000)  return ['level' => 4'percent' => 8'next' => 100000 $refearn];
  206.         if ($refearn >= 25000)  return ['level' => 3'percent' => 7'next' => 50000 $refearn];
  207.         if ($refearn >= 10000)  return ['level' => 2'percent' => 6'next' => 25000 $refearn];
  208.         if ($refearn >= 0)      return ['level' => 1'percent' => 5'next' => 10000 $refearn];
  209.         return ['level' => 1'percent' => 5'next' => 10000];
  210.     }
  211.  
  212.     /**
  213.      * @Route("/go/{id}", name="referral_redirect", requirements={"id"="\d+"})
  214.      */
  215.     public function referralRedirect(int $idRequest $request): RedirectResponse
  216.     {
  217.         $request->getSession()->set('ref_id'$id);
  218.         return $this->redirectToRoute('main_page');
  219.     }
  220.     
  221.     /**
  222.      * @Route("/profile", name="profile_page")
  223.      */
  224.     public function profile(Request $request): Response
  225.     {
  226.         $user $this->getAuthorizedUser($request);
  227.         if (!$user) {
  228.             return $this->redirectToRoute('main_page');
  229.         }
  230.         $sumDeposits $this->connection->fetchOne(
  231.             'SELECT COALESCE(SUM(suma), 0) FROM deposits WHERE user_id = ? AND status = 1',
  232.             [$user['id']]
  233.         );
  234.         $sumWithdraws $this->connection->fetchOne(
  235.             'SELECT COALESCE(SUM(sum), 0) FROM withdraws WHERE user_id = ? AND status = 1',
  236.             [$user['id']]
  237.         );
  238.         
  239.         $xp = (int) $sumDeposits;
  240.         $levels = [
  241.             ['title' => 'Новичок''xp' => 0'bonus' => 0'promo' => 0'dep' => 1],
  242.             ['title' => 'Любитель''xp' => 1000'bonus' => 20'promo' => 2'dep' => 3],
  243.             ['title' => 'Знаток',   'xp' => 5000'bonus' => 50'promo' => 4'dep' => 4],
  244.             ['title' => 'Мастер',   'xp' => 10000'bonus' => 100'promo' => 6'dep' => 8],
  245.             ['title' => 'Легенда',  'xp' => 25000'bonus' => 250'promo' => 14'dep' => 15],
  246.         ];
  247.         $currentLevelIndex 0;
  248.         foreach ($levels as $i => $level) {
  249.             if ($xp >= $level['xp']) {
  250.                 $currentLevelIndex $i;
  251.             } else {
  252.                 break;
  253.             }
  254.         }
  255.         $currentLevel $levels[$currentLevelIndex];
  256.         $nextLevel $levels[$currentLevelIndex 1] ?? null;
  257.         
  258.         return $this->render('profile.html.twig', [
  259.             'sumDeposits' => $sumDeposits,
  260.             'sumWithdraws' => $sumWithdraws,
  261.             'xp' => $xp,
  262.             'currentLevel' => $currentLevel,
  263.             'nextLevel' => $nextLevel,
  264.         ]);
  265.     }
  266.     
  267.     /**
  268.      * @Route("/faq", name="faq_page")
  269.      */
  270.     public function faq(): Response
  271.     {
  272.         return $this->render('faq.html.twig');
  273.     }
  274.     
  275.     /**
  276.      * @Route("/terms", name="terms_page")
  277.      */
  278.     public function terms(): Response
  279.     {
  280.         return $this->render('terms.html.twig');
  281.     }
  282.     
  283.     /**
  284.      * @Route("/support", name="support_redirect")
  285.      */
  286.     public function support(): Response
  287.     {
  288.         return $this->redirectTo('https://vk.me/club231831476');
  289.     }
  290.     
  291. }