Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller;
- use App\Entity\Game;
- use App\Entity\Player;
- use App\Entity\Tournament;
- use App\Repository\GameRepository;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Annotation\Route;
- class DefaultController extends AbstractController
- {
- /**
- * @Route("/", name="default")
- */
- public function index()
- {
- $em = $this->getDoctrine()->getManager();
- $intro = $em->getRepository(Tournament::class)->findOneBy([], ['id' => 'desc'], 0, 1);
- return $this->render('default/index.html.twig', [
- 'intro' => $intro,
- 'games' => null
- ]);
- }
- /**
- * @Route("/game/start/{tournament}", name="game_start")
- */
- public function gameStart(GameRepository $gameRepository, $tournament): Response
- {
- $em = $this->getDoctrine()->getManager();
- $players = $em->getRepository(Player::class)->findAll();
- $games = array();
- foreach ($players as $player) {
- // add players to game array
- array_push($games, $player->getId());
- }
- $tournament = $em->getRepository(Tournament::class)->findOneBy(['id' => $tournament]);
- /*
- * Formula is: [participants] - (128 - [participants]) = participants round 1
- * [participants] - [participants round 1] is the amount of people that will instantly go to second round
- */
- if (shuffle($games)) {
- if (count($games) == 128) {
- // if exactly 128 players
- for ($i = 1; $i < count($games); $i++) {
- $game = new Game();
- $game->setRound(1);
- $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
- $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
- $game->setTournament($tournament);
- $em->persist($game);
- $em->flush();
- }
- } elseif ((count($games) > 64) and (count($games) < 128)) {
- // if there's not exactly 128 players but it is between 64 and 128 then this code will activate.
- $round1 = count($games) - (128 - count($games));
- $round2 = count($games) - $round1;
- for ($i = 1; $i < $round2; $i++) {
- // first 28 players go to round 2
- $game = new Game();
- $game->setRound(2);
- $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
- $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
- $game->setTournament($tournament);
- $em->persist($game);
- $em->flush();
- }
- for ($i = $round2; $i < count($games); $i++) {
- // the rest of the players go to round 1
- $game = new Game();
- $game->setRound(1);
- $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
- $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
- $game->setTournament($tournament);
- $em->persist($game);
- $em->flush();
- }
- } elseif (count($games) > 128) {
- // if amount of players are above 128.
- for ($i = 1; $i < count($games); $i++) {
- $game = new Game();
- $game->setRound(1);
- $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
- $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
- $game->setTournament($tournament);
- $em->persist($game);
- $em->flush();
- }
- }
- } else {
- return $this->render('default/index.html.twig', [
- 'controller_name' => 'DefaultController',
- ]);
- }
- return $this->redirectToRoute('game_see', [
- 'tournament' => $tournament->getId(),
- 'round' => '1',
- ]);
- }
- /**
- * @Route("/game/set-scores/{tournament}/{round}", name="game_random_scores")
- */
- public function gameRandomScores($tournament, $round)
- {
- $em = $this->getDoctrine()->getManager();
- $games = $this->getDoctrine()->getManager()
- ->getRepository(Game::class)
- ->findBy(['tournament' => $tournament, 'round' => $round]);
- $numbers = range('1', '150');
- shuffle($numbers);
- foreach ($games as $game) {
- $game->setScore1(array_pop($numbers));
- $game->setScore2(array_pop($numbers));
- $em->persist($game);
- $em->flush();
- }
- return $this->redirectToRoute('game_set_winners', [
- 'tournament' => $tournament,
- 'round' => $round
- ]);
- }
- /**
- * @Route("/game/set-winners/{tournament}/{round}", name="game_set_winners")
- */
- public function gameSetWinners(GameRepository $gameRepository, $tournament, $round): Response
- {
- $em = $this->getDoctrine()->getManager();
- $gameson = $gameRepository->findBy(['tournament' => $tournament, 'round' => $round]);
- foreach ($gameson as $games) {
- if ($games->getScore1() > $games->getScore2()) {
- $games->setWinner($games->getPlayer1());
- } elseif ($games->getScore1() < $games->getScore2()) {
- $games->setWinner($games->getPlayer2());
- }
- $em->persist($games);
- $em->flush();
- }
- return $this->redirectToRoute('game_end', [
- 'tournament' => $tournament,
- 'round' => $round,
- ]);
- }
- /**
- * @Route("/game/end-round/{tournament}/{round}", name="game_end")
- */
- public function gameEnd(GameRepository $gameRepository, $tournament, $round): Response
- {
- // close round based on $round number
- $gamesen = $gameRepository->findBy(['tournament' => $tournament, 'round' => $round]);
- // get two records together and put them in a new game
- $counter = 1;
- foreach ($gamesen as $games) {
- // winner first game versus the winner second game
- if ($counter & 1) {
- // if $counter is odd, make a new game with a new round
- $ws = new Game();
- $ws->setTournament($games->getTournament());
- $ws->setRound($round + 1);
- }
- if ($counter % 2 == 0) {
- // if $counter is even, make a new game with a new round
- $ws->setPlayer1($games->getWinner());
- ++$counter;
- } else {
- $ws->setPlayer2($games->getWinner());
- ++$counter;
- }
- $entityManager = $this->getDoctrine()->getManager();
- $entityManager->persist($ws);
- $entityManager->flush();
- }
- return $this->redirectToRoute('game_see', [
- 'tournament' => $tournament,
- 'round' => $round + 1
- ]);
- }
- /**
- * @Route("/game/see/{tournament}/{round}", name="game_see")
- */
- public function gameSee($tournament, $round)
- {
- $links = $this->getDoctrine()->getManager()
- ->getRepository(Game::class)
- ->createQueryBuilder("p")->select("p.round")
- ->distinct(true)
- ->orderBy('p.round', 'ASC')
- ->getQuery()
- ->getResult();
- $games = $this->getDoctrine()->getManager()
- ->getRepository(Game::class)
- ->findBy(['tournament' => $tournament, 'round' => $round]);
- if (!$games) {
- return $this->redirectToRoute('default');
- }
- return $this->render('default/index.html.twig', [
- 'games' => $games,
- 'links' => $links,
- 'id' => $tournament
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement