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 = $request->get('user_betting'); |
184
|
|
|
$userBetting = new UserBettingEntity(); |
185
|
|
|
$form = $this->createForm(UserBettingType::class, $userBetting); |
186
|
|
|
$form->handleRequest($request); |
187
|
|
|
if (!$form->isSubmitted() && !$form->isValid()) { |
188
|
|
|
return $this->json(['status' => false]); |
189
|
|
|
} |
190
|
|
|
if (!isset($params['firstTeamResult']) || $params['firstTeamResult'] < 0) { |
191
|
|
|
return $this->json(['status' => false]); |
192
|
|
|
} |
193
|
|
|
if (!isset($params['secondTeamResult']) || $params['secondTeamResult'] < 0) { |
194
|
|
|
return $this->json(['status' => false]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
198
|
|
|
|
199
|
|
|
$userBetting = $entityManager->getRepository(UserBettingEntity::class) |
200
|
|
|
->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId()); |
201
|
|
|
if (!$userBetting instanceof UserBettingEntity) { |
202
|
|
|
$userBetting = new UserBettingEntity(); |
203
|
|
|
$game = $entityManager->getRepository(Game::class) |
204
|
|
|
->find($params['gameId']); |
205
|
|
|
$userBetting->setGame($game); |
206
|
|
|
$userBetting->setUser($this->getUser()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
$userBetting->setFirstTeamResult((int)$params['firstTeamResult']); |
211
|
|
|
$userBetting->setSecondTeamResult((int)$params['secondTeamResult']); |
212
|
|
|
|
213
|
|
|
if ($userBetting->getGame()->getDate()->getTimestamp() < time()) { |
214
|
|
|
return $this->json(['status' => false]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$entityManager->persist($userBetting); |
218
|
|
|
$entityManager->flush(); |
219
|
|
|
|
220
|
|
|
return $this->json(['status' => true]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
private function getFutureGamesFormBuilder(): array |
227
|
|
|
{ |
228
|
|
|
$userFurureGames = $this->userFutureGames->get( |
229
|
|
|
$this->getUser() |
230
|
|
|
); |
231
|
|
|
$gamesFormBuilder = []; |
232
|
|
|
foreach ($userFurureGames as $gameId => $futureGameInfo) { |
233
|
|
|
$gamesFormBuilder[$gameId] = $this->createForm( |
234
|
|
|
UserBettingType::class, null, $futureGameInfo |
235
|
|
|
)->createView(); |
236
|
|
|
} |
237
|
|
|
return $gamesFormBuilder; |
238
|
|
|
} |
239
|
|
|
} |