<?php
namespace App\Controller;
use Predis\Client;
use Doctrine\DBAL\Connection;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
class PageController extends BaseController
{
public function __construct(Client $redis, Connection $connection)
{
$this->redis = $redis;
$this->connection = $connection;
}
private function getHistory(): array
{
$historyRaw = $this->redis->lRange('game_history', 0, 9);
return array_map(function ($item) {
$data = json_decode($item, true);
if (!empty($data['username'])) {
$username = $data['username'];
$length = mb_strlen($username, 'UTF-8');
if ($length > 6) {
$data['username'] = mb_substr($username, 0, 6, 'UTF-8') . '****';
}
}
return $data;
}, $historyRaw);
}
/**
* @Route("/", name="main_page")
*/
public function index(): Response
{
return $this->render('main.html.twig', [
'history' => $this->getHistory(),
]);
}
/**
* @Route("/mines", name="mines_page")
*/
public function mines(): Response
{
return $this->render('mines.html.twig', [
'history' => $this->getHistory(),
]);
}
/**
* @Route("/dice", name="dice_page")
*/
public function dice(): Response
{
return $this->render('dice.html.twig', [
'history' => $this->getHistory(),
]);
}
/**
* @Route("/lobby", name="lobby_page")
*/
public function lobby(): Response
{
return $this->render('lobby.html.twig');
}
/**
* @Route("/tournaments", name="tours_page")
*/
public function tournaments(): Response
{
$topPlayers = $this->connection->fetchAllAssociative(
'SELECT login, img, points FROM users WHERE admin = 0 and tournament = 1 ORDER BY points DESC LIMIT 10'
);
foreach ($topPlayers as &$player) {
$player['login'] = mb_substr($player['login'], 0, 5).'****';
}
$top3 = array_slice($topPlayers, 0, 3);
$rest = array_slice($topPlayers, 3);
$prizePool = 25000;
$rewards = [
1 => 12500,
2 => 10000,
3 => 2500,
];
$countRow = $this->connection->fetchAssociative(
'SELECT count(*) AS cnt FROM users WHERE admin = 0 and tournament = 1'
);
$count = $countRow['cnt'];
return $this->render('tournaments.html.twig', [
'top3' => $top3,
'rewards' => $rewards,
'pool' => $prizePool,
'count' => $count
]);
}
/**
* @Route("/bonus", name="bonus_page")
*/
public function bonus(Request $request): Response
{
$user = $this->getAuthorizedUser($request);
if (!$user) {
return $this->redirectToRoute('main_page');
}
$now = new \DateTime();
$monthKey = $now->format('Y-m');
$start = $monthKey . '-01 00:00:00';
$end = $now->format('Y-m-t') . ' 23:59:59';
$deposits = $this->connection->fetchOne(
"SELECT SUM(suma) FROM deposits
WHERE user_id = :uid AND status = 1
AND STR_TO_DATE(data, '%d-%m-%Y %H:%i:%s') BETWEEN :start AND :end",
['uid' => $user['id'], 'start' => $start, 'end' => $end]
) ?? 0;
$withdraws = $this->connection->fetchOne(
"SELECT SUM(`sum`) FROM withdraws
WHERE user_id = :uid AND status = '1'
AND STR_TO_DATE(`date`, '%Y-%m-%d %H:%i:%s') BETWEEN :start AND :end",
['uid' => $user['id'], 'start' => $start, 'end' => $end]
) ?? 0;
$cashback = 0;
if ((float)$deposits > (float)$withdraws) {
$alreadyReceived = $this->connection->fetchOne(
'SELECT 1 FROM cashback_logs WHERE user_id = :uid AND month = :month',
['uid' => $user['id'], 'month' => $monthKey]
);
if (!$alreadyReceived) {
$cashback = round(((float)$deposits - (float)$withdraws) * 0.08, 2);
}
}
return $this->render('bonus.html.twig', [
'cashback' => $cashback
]);
}
/**
* @Route("/referral", name="referral_page")
*/
public function referralPage(Request $request): Response
{
$user = $this->getAuthorizedUser($request);
if (!$user) {
return $this->redirectToRoute('main_page');
}
$refearn = (float) $user['refearn'];
$page = max(1, (int) $request->query->get('page', 1));
$limit = 5;
$offset = ($page - 1) * $limit;
$referrals = $this->connection->fetchAllAssociative(
"SELECT id, login AS name, img AS avatar, created_at
FROM users
WHERE ref_id = :uid
ORDER BY created_at DESC
LIMIT :limit OFFSET :offset",
['uid' => $user['id'], 'limit' => $limit, 'offset' => $offset],
['uid' => \PDO::PARAM_INT, 'limit' => \PDO::PARAM_INT, 'offset' => \PDO::PARAM_INT]
);
$total = (int) $this->connection->fetchOne(
"SELECT COUNT(*) FROM users WHERE ref_id = ?",
[$user['id']]
);
$hasNextPage = ($offset + $limit) < $total;
if ($request->isXmlHttpRequest()) {
$html = $this->renderView('components/referral_table_rows.html.twig', [
'referrals' => $referrals,
]);
return new JsonResponse([
'html' => $html,
'page' => $page,
'has_next_page' => $hasNextPage,
'total' => $total,
]);
}
$levelInfo = $this->getLevelInfo($refearn);
$progressPercent = $levelInfo['next'] === null
? 100
: min(100, round(100 * ($refearn / ($refearn + $levelInfo['next'])), 2));
return $this->render('referral.html.twig', [
'user' => $user,
'referrals' => $referrals,
'page' => $page,
'has_next_page' => $hasNextPage,
'level' => $levelInfo['level'],
'percent' => $levelInfo['percent'],
'next_level_amount' => $levelInfo['next'],
'available_to_withdraw' => $user['refbal'],
'progress_percent' => $progressPercent
]);
}
private function getLevelInfo(float $refearn): array
{
if ($refearn >= 100000) return ['level' => 5, 'percent' => 9, 'next' => null];
if ($refearn >= 50000) return ['level' => 4, 'percent' => 8, 'next' => 100000 - $refearn];
if ($refearn >= 25000) return ['level' => 3, 'percent' => 7, 'next' => 50000 - $refearn];
if ($refearn >= 10000) return ['level' => 2, 'percent' => 6, 'next' => 25000 - $refearn];
if ($refearn >= 0) return ['level' => 1, 'percent' => 5, 'next' => 10000 - $refearn];
return ['level' => 1, 'percent' => 5, 'next' => 10000];
}
/**
* @Route("/go/{id}", name="referral_redirect", requirements={"id"="\d+"})
*/
public function referralRedirect(int $id, Request $request): RedirectResponse
{
$request->getSession()->set('ref_id', $id);
return $this->redirectToRoute('main_page');
}
/**
* @Route("/profile", name="profile_page")
*/
public function profile(Request $request): Response
{
$user = $this->getAuthorizedUser($request);
if (!$user) {
return $this->redirectToRoute('main_page');
}
$sumDeposits = $this->connection->fetchOne(
'SELECT COALESCE(SUM(suma), 0) FROM deposits WHERE user_id = ? AND status = 1',
[$user['id']]
);
$sumWithdraws = $this->connection->fetchOne(
'SELECT COALESCE(SUM(sum), 0) FROM withdraws WHERE user_id = ? AND status = 1',
[$user['id']]
);
$xp = (int) $sumDeposits;
$levels = [
['title' => 'Новичок', 'xp' => 0, 'bonus' => 0, 'promo' => 0, 'dep' => 1],
['title' => 'Любитель', 'xp' => 1000, 'bonus' => 20, 'promo' => 2, 'dep' => 3],
['title' => 'Знаток', 'xp' => 5000, 'bonus' => 50, 'promo' => 4, 'dep' => 4],
['title' => 'Мастер', 'xp' => 10000, 'bonus' => 100, 'promo' => 6, 'dep' => 8],
['title' => 'Легенда', 'xp' => 25000, 'bonus' => 250, 'promo' => 14, 'dep' => 15],
];
$currentLevelIndex = 0;
foreach ($levels as $i => $level) {
if ($xp >= $level['xp']) {
$currentLevelIndex = $i;
} else {
break;
}
}
$currentLevel = $levels[$currentLevelIndex];
$nextLevel = $levels[$currentLevelIndex + 1] ?? null;
return $this->render('profile.html.twig', [
'sumDeposits' => $sumDeposits,
'sumWithdraws' => $sumWithdraws,
'xp' => $xp,
'currentLevel' => $currentLevel,
'nextLevel' => $nextLevel,
]);
}
/**
* @Route("/faq", name="faq_page")
*/
public function faq(): Response
{
return $this->render('faq.html.twig');
}
/**
* @Route("/terms", name="terms_page")
*/
public function terms(): Response
{
return $this->render('terms.html.twig');
}
/**
* @Route("/support", name="support_redirect")
*/
public function support(): Response
{
return $this->redirectTo('https://t.me/justfolked');
}
}