|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\GameBetting\Communication\Controller; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use App\GameBetting\Business\Form\UserBettingType; |
|
7
|
|
|
use App\GameBetting\Business\Games\UserFutureGamesInterface; |
|
8
|
|
|
use App\GameBetting\Business\Games\UserPastGamesInterface; |
|
9
|
|
|
use App\GameBetting\Persistence\Entity\UserBetting as UserBettingEntity; |
|
10
|
|
|
use App\GameCore\Persistence\Entity\Game; |
|
11
|
|
|
use App\GameCore\Persistence\Repository\GameRepository; |
|
12
|
|
|
use App\User\Persistence\Entity\User; |
|
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
17
|
|
|
|
|
18
|
|
|
class UserBetting extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var UserFutureGamesInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $userFutureGames; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var UserPastGamesInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $userPastGames; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param UserFutureGamesInterface $userFutureGames |
|
32
|
|
|
* @param UserPastGamesInterface $userPastGames |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(UserFutureGamesInterface $userFutureGames, UserPastGamesInterface $userPastGames) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->userFutureGames = $userFutureGames; |
|
37
|
|
|
$this->userPastGames = $userPastGames; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @TODO template struktur überarbeiten und eigene actions machen |
|
42
|
|
|
* @Route("/gamebet/", name="game_bet_list") |
|
43
|
|
|
*/ |
|
44
|
|
|
public function list() |
|
45
|
|
|
{ |
|
46
|
|
|
$futureGamesFormBuilder = $this->getFutureGamesFormBuilder(); |
|
47
|
|
|
$pastGamesForm = $this->userPastGames->get($this->getUser()); |
|
48
|
|
|
|
|
49
|
|
|
return $this->render( |
|
50
|
|
|
'gamebetting/betting/betting.html.twig', |
|
51
|
|
|
[ |
|
52
|
|
|
'futureGamesForm' => $futureGamesFormBuilder, |
|
53
|
|
|
'pastGamesForm' => \array_reverse(\array_slice($pastGamesForm, -6)) |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Route("/active-games/", name="active-games") |
|
60
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getActiveGames() |
|
63
|
|
|
{ |
|
64
|
|
|
$activeGames = $this->userPastGames->getActiveGames($this->getUser()); |
|
65
|
|
|
|
|
66
|
|
|
return $this->render( |
|
67
|
|
|
'gamebetting/betting/active_games.html.twig', |
|
68
|
|
|
[ |
|
69
|
|
|
'activeGames' => $activeGames |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @Route("/all-upcomming-games", name="all_upcomming_games") |
|
76
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
77
|
|
|
*/ |
|
78
|
|
|
public function allUpcommingGames() |
|
79
|
|
|
{ |
|
80
|
|
|
$futureGamesFormBuilder = $this->getFutureGamesFormBuilder(); |
|
81
|
|
|
|
|
82
|
|
|
return $this->render( |
|
83
|
|
|
'gamebetting/betting/all_games.html.twig', |
|
84
|
|
|
[ |
|
85
|
|
|
'futureGamesForm' => $futureGamesFormBuilder |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @Route("/all-past-games", name="all_past_games") |
|
92
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
93
|
|
|
*/ |
|
94
|
|
|
public function allPastGames() |
|
95
|
|
|
{ |
|
96
|
|
|
$pastGamesForm = $this->userPastGames->get($this->getUser()); |
|
97
|
|
|
|
|
98
|
|
|
return $this->render( |
|
99
|
|
|
'gamebetting/betting/past_games.html.twig', |
|
100
|
|
|
[ |
|
101
|
|
|
'pastGamesForm' => $pastGamesForm, |
|
102
|
|
|
'username' => false |
|
103
|
|
|
] |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* toDo Refactor this |
|
109
|
|
|
* @Route("/past-game-detail/{gameId}", name="past_game_detail") |
|
110
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
111
|
|
|
*/ |
|
112
|
|
|
public function pastGameDetail(int $gameId) |
|
113
|
|
|
{ |
|
114
|
|
|
$users = $this->getDoctrine()->getRepository(User::class) |
|
115
|
|
|
->findBy([], ['username' => 'ASC']); |
|
116
|
|
|
|
|
117
|
|
|
$teamFirstName = ''; |
|
118
|
|
|
$teamSecondName = ''; |
|
119
|
|
|
$gameInfo2users = []; |
|
120
|
|
|
foreach ($users as $user) { |
|
121
|
|
|
$pastGamesForm = $this->userPastGames->getOnePastGame($user, $gameId); |
|
122
|
|
|
if (!empty($pastGamesForm)) { |
|
123
|
|
|
$teamFirstName = current($pastGamesForm)->getFirstTeamName(); |
|
124
|
|
|
$teamSecondName = current($pastGamesForm)->getSecondTeamName(); |
|
125
|
|
|
|
|
126
|
|
|
$gameInfo2users[$user->getUsername()] = current($pastGamesForm); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->render( |
|
132
|
|
|
'gamebetting/betting/past_game_detail.html.twig', |
|
133
|
|
|
[ |
|
134
|
|
|
'gameInfo2users' => $gameInfo2users, |
|
135
|
|
|
'teamFirstName' => $teamFirstName, |
|
136
|
|
|
'teamSecondName' => $teamSecondName, |
|
137
|
|
|
] |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @Route("/all-past-games-by-user/{userId}", name="game_past_games_by_users") |
|
143
|
|
|
*/ |
|
144
|
|
|
public function allPastGameByUserId(int $userId) |
|
145
|
|
|
{ |
|
146
|
|
|
$user = $this->getDoctrine()->getRepository(User::class) |
|
147
|
|
|
->findOneBy(['id' => $userId]); |
|
148
|
|
|
$pastGamesForm = []; |
|
149
|
|
|
$username = ''; |
|
150
|
|
|
if ($user instanceof User) { |
|
151
|
|
|
$pastGamesForm = $this->userPastGames->get($user); |
|
152
|
|
|
$username = $user->getUsername(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $this->render( |
|
156
|
|
|
'gamebetting/betting/past_games.html.twig', |
|
157
|
|
|
[ |
|
158
|
|
|
'pastGamesForm' => $pastGamesForm, |
|
159
|
|
|
'username' => $username |
|
160
|
|
|
] |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function getNextGames(int $numberOfGames) |
|
166
|
|
|
{ |
|
167
|
|
|
$gameBetResult = \array_slice($this->userFutureGames->get($this->getUser()), 0, $numberOfGames); |
|
168
|
|
|
|
|
169
|
|
|
return $this->render( |
|
170
|
|
|
'dashboard/next_games.html.twig', |
|
171
|
|
|
[ |
|
172
|
|
|
'gameBetResult' => $gameBetResult |
|
173
|
|
|
] |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @Route("/savebet", name="save_bet") |
|
179
|
|
|
* @Method({"POST"}) |
|
180
|
|
|
*/ |
|
181
|
|
|
public function saveBet(Request $request) |
|
182
|
|
|
{ |
|
183
|
|
|
$params = (array)$request->get('user_betting'); |
|
184
|
|
|
$userBetting = new UserBettingEntity(); |
|
185
|
|
|
$form = $this->createForm(UserBettingType::class, $userBetting); |
|
186
|
|
|
$form->handleRequest($request); |
|
187
|
|
|
|
|
188
|
|
|
if ($this->isSaveFormInValid($form, $params) === true) { |
|
189
|
|
|
return $this->json(['status' => false]); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
193
|
|
|
$userBetting = $entityManager->getRepository(UserBettingEntity::class) |
|
194
|
|
|
->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId()); |
|
195
|
|
|
if (!$userBetting instanceof UserBettingEntity) { |
|
196
|
|
|
$userBetting = new UserBettingEntity(); |
|
197
|
|
|
$game = $entityManager->getRepository(Game::class) |
|
198
|
|
|
->find($params['gameId']); |
|
199
|
|
|
$userBetting->setGame($game); |
|
200
|
|
|
$userBetting->setUser($this->getUser()); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
$userBetting->setFirstTeamResult((int)$params['firstTeamResult']); |
|
205
|
|
|
$userBetting->setSecondTeamResult((int)$params['secondTeamResult']); |
|
206
|
|
|
|
|
207
|
|
|
if ($userBetting->getGame()->getDate()->getTimestamp() < time()) { |
|
208
|
|
|
return $this->json(['status' => false]); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$entityManager->persist($userBetting); |
|
212
|
|
|
$entityManager->flush(); |
|
213
|
|
|
|
|
214
|
|
|
return $this->json(['status' => true]); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param $form |
|
219
|
|
|
* @param array $params |
|
220
|
|
|
* @return bool |
|
221
|
|
|
*/ |
|
222
|
|
|
private function isSaveFormInValid($form, array $params) : bool |
|
223
|
|
|
{ |
|
224
|
|
|
$isFromInValid = false; |
|
225
|
|
|
if (!$form->isSubmitted() && !$form->isValid()) { |
|
226
|
|
|
$isFromInValid = true; |
|
227
|
|
|
} else if (!isset($params['firstTeamResult']) || $params['firstTeamResult'] < 0) { |
|
228
|
|
|
$isFromInValid = true; |
|
229
|
|
|
} else if (!isset($params['secondTeamResult']) || $params['secondTeamResult'] < 0) { |
|
230
|
|
|
$isFromInValid = true; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $isFromInValid; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
|
|
private function getFutureGamesFormBuilder(): array |
|
240
|
|
|
{ |
|
241
|
|
|
$userFurureGames = $this->userFutureGames->get( |
|
242
|
|
|
$this->getUser() |
|
243
|
|
|
); |
|
244
|
|
|
$gamesFormBuilder = []; |
|
245
|
|
|
foreach ($userFurureGames as $gameId => $futureGameInfo) { |
|
246
|
|
|
$gamesFormBuilder[$gameId] = $this->createForm( |
|
247
|
|
|
UserBettingType::class, null, $futureGameInfo |
|
248
|
|
|
)->createView(); |
|
249
|
|
|
} |
|
250
|
|
|
return $gamesFormBuilder; |
|
251
|
|
|
} |
|
252
|
|
|
} |