Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Entity\Game;
  6. use App\Entity\Player;
  7. use App\Entity\Tournament;
  8. use App\Repository\GameRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12.  
  13. class DefaultController extends AbstractController
  14. {
  15. /**
  16. * @Route("/", name="default")
  17. */
  18. public function index()
  19. {
  20. $em = $this->getDoctrine()->getManager();
  21. $intro = $em->getRepository(Tournament::class)->findOneBy([], ['id' => 'desc'], 0, 1);
  22.  
  23. return $this->render('default/index.html.twig', [
  24. 'intro' => $intro,
  25. 'games' => null
  26. ]);
  27. }
  28.  
  29. /**
  30. * @Route("/game/start/{tournament}", name="game_start")
  31. */
  32. public function gameStart(GameRepository $gameRepository, $tournament): Response
  33. {
  34. $em = $this->getDoctrine()->getManager();
  35. $players = $em->getRepository(Player::class)->findAll();
  36.  
  37. $games = array();
  38.  
  39. foreach ($players as $player) {
  40. // add players to game array
  41. array_push($games, $player->getId());
  42. }
  43.  
  44. $tournament = $em->getRepository(Tournament::class)->findOneBy(['id' => $tournament]);
  45.  
  46. /*
  47. * Formula is: [participants] - (128 - [participants]) = participants round 1
  48. * [participants] - [participants round 1] is the amount of people that will instantly go to second round
  49. */
  50.  
  51. if (shuffle($games)) {
  52. if (count($games) == 128) {
  53. // if exactly 128 players
  54. for ($i = 1; $i < count($games); $i++) {
  55. $game = new Game();
  56. $game->setRound(1);
  57. $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
  58. $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
  59. $game->setTournament($tournament);
  60. $em->persist($game);
  61. $em->flush();
  62. }
  63. } elseif ((count($games) > 64) and (count($games) < 128)) {
  64. // if there's not exactly 128 players but it is between 64 and 128 then this code will activate.
  65. $round1 = count($games) - (128 - count($games));
  66. $round2 = count($games) - $round1;
  67.  
  68. for ($i = 1; $i < $round2; $i++) {
  69. // first 28 players go to round 2
  70. $game = new Game();
  71. $game->setRound(2);
  72. $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
  73. $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
  74. $game->setTournament($tournament);
  75. $em->persist($game);
  76. $em->flush();
  77. }
  78. for ($i = $round2; $i < count($games); $i++) {
  79. // the rest of the players go to round 1
  80. $game = new Game();
  81. $game->setRound(1);
  82. $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
  83. $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
  84. $game->setTournament($tournament);
  85. $em->persist($game);
  86. $em->flush();
  87. }
  88. } elseif (count($games) > 128) {
  89. // if amount of players are above 128.
  90. for ($i = 1; $i < count($games); $i++) {
  91. $game = new Game();
  92. $game->setRound(1);
  93. $game->setPlayer1($em->getRepository(Player::class)->findOneBy(['id' => $games[$i - 1]]));
  94. $game->setPlayer2($em->getRepository(Player::class)->findOneBy(['id' => $games[++$i - 1]]));
  95. $game->setTournament($tournament);
  96. $em->persist($game);
  97. $em->flush();
  98. }
  99. }
  100. } else {
  101. return $this->render('default/index.html.twig', [
  102. 'controller_name' => 'DefaultController',
  103. ]);
  104. }
  105.  
  106. return $this->redirectToRoute('game_see', [
  107. 'tournament' => $tournament->getId(),
  108. 'round' => '1',
  109. ]);
  110. }
  111.  
  112. /**
  113. * @Route("/game/set-scores/{tournament}/{round}", name="game_random_scores")
  114. */
  115. public function gameRandomScores($tournament, $round)
  116. {
  117. $em = $this->getDoctrine()->getManager();
  118.  
  119. $games = $this->getDoctrine()->getManager()
  120. ->getRepository(Game::class)
  121. ->findBy(['tournament' => $tournament, 'round' => $round]);
  122.  
  123. $numbers = range('1', '150');
  124. shuffle($numbers);
  125.  
  126. foreach ($games as $game) {
  127. $game->setScore1(array_pop($numbers));
  128. $game->setScore2(array_pop($numbers));
  129. $em->persist($game);
  130. $em->flush();
  131. }
  132.  
  133. return $this->redirectToRoute('game_set_winners', [
  134. 'tournament' => $tournament,
  135. 'round' => $round
  136. ]);
  137. }
  138.  
  139. /**
  140. * @Route("/game/set-winners/{tournament}/{round}", name="game_set_winners")
  141. */
  142. public function gameSetWinners(GameRepository $gameRepository, $tournament, $round): Response
  143. {
  144. $em = $this->getDoctrine()->getManager();
  145. $gameson = $gameRepository->findBy(['tournament' => $tournament, 'round' => $round]);
  146.  
  147. foreach ($gameson as $games) {
  148. if ($games->getScore1() > $games->getScore2()) {
  149. $games->setWinner($games->getPlayer1());
  150.  
  151. } elseif ($games->getScore1() < $games->getScore2()) {
  152. $games->setWinner($games->getPlayer2());
  153. }
  154. $em->persist($games);
  155. $em->flush();
  156. }
  157. return $this->redirectToRoute('game_end', [
  158. 'tournament' => $tournament,
  159. 'round' => $round,
  160. ]);
  161. }
  162.  
  163. /**
  164. * @Route("/game/end-round/{tournament}/{round}", name="game_end")
  165. */
  166. public function gameEnd(GameRepository $gameRepository, $tournament, $round): Response
  167. {
  168. // close round based on $round number
  169. $gamesen = $gameRepository->findBy(['tournament' => $tournament, 'round' => $round]);
  170. // get two records together and put them in a new game
  171. $counter = 1;
  172. foreach ($gamesen as $games) {
  173. // winner first game versus the winner second game
  174. if ($counter & 1) {
  175. // if $counter is odd, make a new game with a new round
  176. $ws = new Game();
  177. $ws->setTournament($games->getTournament());
  178. $ws->setRound($round + 1);
  179. }
  180. if ($counter % 2 == 0) {
  181. // if $counter is even, make a new game with a new round
  182. $ws->setPlayer1($games->getWinner());
  183. ++$counter;
  184. } else {
  185. $ws->setPlayer2($games->getWinner());
  186. ++$counter;
  187. }
  188. $entityManager = $this->getDoctrine()->getManager();
  189. $entityManager->persist($ws);
  190. $entityManager->flush();
  191. }
  192. return $this->redirectToRoute('game_see', [
  193. 'tournament' => $tournament,
  194. 'round' => $round + 1
  195. ]);
  196. }
  197.  
  198. /**
  199. * @Route("/game/see/{tournament}/{round}", name="game_see")
  200. */
  201. public function gameSee($tournament, $round)
  202. {
  203. $links = $this->getDoctrine()->getManager()
  204. ->getRepository(Game::class)
  205. ->createQueryBuilder("p")->select("p.round")
  206. ->distinct(true)
  207. ->orderBy('p.round', 'ASC')
  208. ->getQuery()
  209. ->getResult();
  210.  
  211. $games = $this->getDoctrine()->getManager()
  212. ->getRepository(Game::class)
  213. ->findBy(['tournament' => $tournament, 'round' => $round]);
  214.  
  215. if (!$games) {
  216. return $this->redirectToRoute('default');
  217. }
  218.  
  219. return $this->render('default/index.html.twig', [
  220. 'games' => $games,
  221. 'links' => $links,
  222. 'id' => $tournament
  223. ]);
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement