src/Controller/PageController.php line 44

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