<?php
namespace App\Controller;
use App\Entity\Vote;
use App\Repository\BattleRepository;
use App\Repository\ClientRepository;
use App\Repository\VoteRepository;
use App\Service\BattleWinnerService;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
class BattleController extends AbstractController
{
private BattleRepository $battleRepository;
private ClientRepository $clientRepository;
private VoteRepository $voteRepository;
private UserDataController $userDataController;
private BattleWinnerService $battleWinnerService;
public function __construct(
BattleRepository $battleRepository,
ClientRepository $clientRepository,
VoteRepository $voteRepository,
UserDataController $userDataController,
BattleWinnerService $battleWinnerService
)
{
$this->battleRepository = $battleRepository;
$this->clientRepository = $clientRepository;
$this->voteRepository = $voteRepository;
$this->userDataController = $userDataController;
$this->battleWinnerService = $battleWinnerService;
}
#[Route(path: [
'cs' => '/battly',
'en' => '/battles'
], name: 'battles')]
public function frontendBattlesAction(Security $security): Response
{
$user = $this->getUser();
$allBattles = $this->battleRepository->findAll();
$this->checkForWinners($allBattles);
if ($user==null){
return $this->render('pages/battles.html.twig',[
'allBattles' => $allBattles
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/battles.html.twig',[
'userData'=>$this->userDataController->getUserData(),
'allBattles' => $allBattles
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'email'=>$client->getEmail(),
'user'=>$client
]);
}
}
}
#[Route(path: [
'en' => '/battle/{id}',
'cs' => '/bitva/{id}'
], name: 'battleDetail')]
public function frontendBattleDetailAction($id): Response
{
$user = $this->getUser();
$battle = $this->battleRepository->findOneBy(['id'=>$id]);
if ($user==null){
return $this->render('pages/battle-detail.html.twig',[
'battle'=>$battle
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/battle-detail.html.twig',[
'userData'=>$this->userDataController->getUserData(),
'battle'=>$battle
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'email'=>$client->getEmail(),
'userData'=>$this->userDataController->getUserData(),
'user'=>$client
]);
}
}
}
#[Route(path: '/vote-challenger/{id}', name: 'voteChallenger')]
public function frontendVoteChallengerAction($id): JsonResponse
{
$battle = $this->battleRepository->findOneBy(['id'=>$id]);
$votingClient = $this->clientRepository->findOneBy(["email"=>$this->getUser()->getUserIdentifier()]);
$voterVote = $this->voteRepository->findOneBy(["voter"=>$votingClient]);
if($voterVote!=null){
if($voterVote->getVoteValue()===1){
$this->voteRepository->remove($voterVote,true);
return new JsonResponse("Removed",200);
}else{
$voterVote->setVoteValue(1);
$savedVote = $this->voteRepository->save($voterVote,true);
if($savedVote->getId()!=0){
return new JsonResponse($savedVote->getId(),201);
}
}
}else{
$vote = new Vote();
$vote
->setBattle($battle)
->setVoter($votingClient)
->setVoteValue(1)
;
$savedVote = $this->voteRepository->save($vote,true);
if($savedVote->getId()!=0){
return new JsonResponse($savedVote->getId(),201);
}
}
return new JsonResponse("Err",400);
}
#[Route(path: '/vote-oponnent/{id}', name: 'voteOponnent')]
public function frontendBattleVoteOponnentAction($id): JsonResponse
{
$battle = $this->battleRepository->findOneBy(['id'=>$id]);
$votingClient = $this->clientRepository->findOneBy(["email"=>$this->getUser()->getUserIdentifier()]);
$voterVote = $this->voteRepository->findOneBy(["voter"=>$votingClient]);
if($voterVote!=null){
if($voterVote->getVoteValue()===2){
$this->voteRepository->remove($voterVote,true);
return new JsonResponse("Removed",200);
}else{
$voterVote->setVoteValue(2);
$savedVote = $this->voteRepository->save($voterVote,true);
if($savedVote->getId()!=0){
return new JsonResponse($savedVote->getId(),201);
}
}
}else{
$vote = new Vote();
$vote
->setBattle($battle)
->setVoter($votingClient)
->setVoteValue(2)
;
$savedVote = $this->voteRepository->save($vote,true);
if($savedVote->getId()!=0){
return new JsonResponse($savedVote->getId(),201);
}
}
return new JsonResponse("Err",400);
}
#[Route("/get-battles/", name: "getBattlesApi")]
public function getBattlesUrlAction(): JsonResponse
{
$battles=$this->battleRepository->findAll();
$this->checkForWinners($battles);
$data = [];
foreach ($battles as $battle){
$data[] = [
'id' => $battle->getId(),
'battleFileName' => $battle->getBattleFileName(),
'challenger' => $battle->getChallenger()->__toArray(),
'oponnent' => $battle->getOponnent()->__toArray(),
'allVotes'=>$battle->getAllVotes(),
];
}
$jsonResponse = [
"battles" => $data
];
return new JsonResponse($jsonResponse,200);
}
public function checkForWinners(array $battles):void
{
foreach ($battles as $battle)
{
if($battle->isFinished() && $battle->getWinner()==null){
$potentialWinner = $this->battleWinnerService->getWinner($battle);
if($potentialWinner!=null){
$battle
->setWinner($potentialWinner)
->setUpdatedAt(new DateTime('now'))
;
$savedBattle = $this->battleRepository->save($battle,true);
dump("potentialWinnerNotNull: ".$savedBattle->getId());
}else{
dump("potentialWinnerNull");
}
}
}
}
}