|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Card\DeckOfCards; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
12
|
|
|
|
|
13
|
|
|
class CardGameController extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
#[Route("/card", name: "card_index")] |
|
16
|
|
|
public function index(SessionInterface $session): Response |
|
17
|
|
|
{ |
|
18
|
|
|
$session->set('currentDeck', $session->get('currentDeck', new DeckOfCards())); |
|
19
|
|
|
|
|
20
|
|
|
return $this->render('card/index.html.twig'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
#[Route("/card/deck", name: "show_ordered")] |
|
24
|
|
|
public function showOrdered(SessionInterface $session): Response |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var DeckOfCards $deck */ |
|
27
|
|
|
$deck = new DeckOfCards(); |
|
28
|
|
|
|
|
29
|
|
|
$session->set('currentDeck', $deck); |
|
30
|
|
|
|
|
31
|
|
|
$data = [ |
|
32
|
|
|
"deck" => $deck->getString(), |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
return $this->render('card/test/deck.html.twig', $data); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
#[Route("/card/deck/shuffle", name: "show_shuffled")] |
|
39
|
|
|
public function showShuffled(SessionInterface $session): Response |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var DeckOfCards $deck */ |
|
42
|
|
|
$deck = new DeckOfCards(); |
|
43
|
|
|
|
|
44
|
|
|
$deck->shuffleCards(); |
|
45
|
|
|
|
|
46
|
|
|
$session->set('currentDeck', $deck); // Save shuffled deck |
|
47
|
|
|
|
|
48
|
|
|
$data = [ |
|
49
|
|
|
"deck" => $deck->getString(), |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
return $this->render('card/test/deck.html.twig', $data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
#[Route("/card/deck/draw/{number<\d+>}", name: "card_draw_many")] |
|
56
|
|
|
public function cardDrawMany(SessionInterface $session, int $number): Response |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var DeckOfCards $deck */ |
|
59
|
|
|
$deck = $session->get('currentDeck', new DeckOfCards()); |
|
60
|
|
|
|
|
61
|
|
|
$draw = array_map('strval', $deck->draw($number)); |
|
62
|
|
|
|
|
63
|
|
|
$session->set('currentDeck', $deck); // Save updated deck |
|
64
|
|
|
|
|
65
|
|
|
$data = [ |
|
66
|
|
|
"draw" => $draw, |
|
67
|
|
|
"count" => $deck->getCount(), |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
return $this->render('card/test/draw.html.twig', $data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
#[Route("/card/deck/draw", name: "card_draw")] |
|
74
|
|
|
public function cardDraw(SessionInterface $session): Response |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var DeckOfCards $deck */ |
|
77
|
|
|
$deck = $session->get('currentDeck', new DeckOfCards()); |
|
78
|
|
|
|
|
79
|
|
|
$draw = array_map('strval', $deck->draw(1)); |
|
80
|
|
|
|
|
81
|
|
|
$session->set('currentDeck', $deck); // Save updated deck |
|
82
|
|
|
|
|
83
|
|
|
$data = [ |
|
84
|
|
|
"draw" => $draw, |
|
85
|
|
|
"count" => $deck->getCount(), |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
return $this->render('card/test/draw.html.twig', $data); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
#[Route("card/deck/deal/{players<\d+>}/{cards<\d+>}", name: "card_deal")] |
|
92
|
|
|
public function cardDeal(SessionInterface $session, int $players, int $cards): Response |
|
93
|
|
|
{ |
|
94
|
|
|
/** @var DeckOfCards $deck */ |
|
95
|
|
|
$deck = $session->get('currentDeck', new DeckOfCards()); |
|
96
|
|
|
|
|
97
|
|
|
$deal = []; |
|
98
|
|
|
|
|
99
|
|
|
for ($i = 0; $i < $players; $i++) { |
|
100
|
|
|
$deal[] = array_map('strval', $deck->draw($cards)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$session->set('currentDeck', $deck); // Save updated deck |
|
104
|
|
|
|
|
105
|
|
|
$data = [ |
|
106
|
|
|
"deal" => $deal, |
|
107
|
|
|
"count" => $deck->getCount(), |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
return $this->render('card/test/deal.html.twig', $data); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
#[Route("/api/card", name: "json_card_index")] |
|
114
|
|
|
public function jsonIndex(): Response |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->render('card/json_api/json_index.html.twig'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|