|
1
|
|
|
<?php |
|
2
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
5
|
|
|
|
|
6
|
|
|
class MemoryController extends GameController |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var gameService |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $gameService; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(ServiceLocatorInterface $locator) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($locator); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function playAction() |
|
19
|
|
|
{ |
|
20
|
|
|
$playError = null; |
|
21
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user, $playError); |
|
22
|
|
View Code Duplication |
if (!$entry) { |
|
|
|
|
|
|
23
|
|
|
$reason = ""; |
|
|
|
|
|
|
24
|
|
|
if ($playError === -1) { |
|
25
|
|
|
// the user has already taken part to this game and the participation limit has been reached |
|
26
|
|
|
$this->flashMessenger()->addMessage('Vous avez déjà participé'); |
|
|
|
|
|
|
27
|
|
|
$reason = '?playLimitReached=1'; |
|
28
|
|
|
$noEntryRedirect = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
29
|
|
|
$this->game->getClassType().'/result', |
|
30
|
|
|
array( |
|
31
|
|
|
'id' => $this->game->getIdentifier(), |
|
32
|
|
|
) |
|
33
|
|
|
) .$reason; |
|
34
|
|
|
} elseif ($playError === -2) { |
|
35
|
|
|
// the user has not accepted the mandatory rules of the game |
|
36
|
|
|
$this->flashMessenger()->addMessage('Vous devez accepter le réglement'); |
|
|
|
|
|
|
37
|
|
|
$reason = '?NoOptin=1'; |
|
38
|
|
|
$noEntryRedirect = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
39
|
|
|
$this->game->getClassType(), |
|
40
|
|
|
array( |
|
41
|
|
|
'id' => $this->game->getIdentifier(), |
|
42
|
|
|
) |
|
43
|
|
|
) .$reason; |
|
44
|
|
|
} elseif ($playError === -3) { |
|
45
|
|
|
// the user has enough points to buy an entry to this game |
|
46
|
|
|
$this->flashMessenger()->addMessage("Vous ne pouvez pas acheter la partie"); |
|
|
|
|
|
|
47
|
|
|
$reason = '?NotPaid=1'; |
|
48
|
|
|
$noEntryRedirect = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
49
|
|
|
$this->game->getClassType(), |
|
50
|
|
|
array( |
|
51
|
|
|
'id' => $this->game->getIdentifier(), |
|
52
|
|
|
) |
|
53
|
|
|
) .$reason; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->redirect()->toUrl($noEntryRedirect); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
60
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
|
|
61
|
|
|
$attempts = filter_var($data['attempts'], FILTER_SANITIZE_NUMBER_INT); |
|
62
|
|
|
$mistakes = filter_var($data['mistakes'], FILTER_SANITIZE_NUMBER_INT); |
|
63
|
|
|
$duration = filter_var($data['duration'], FILTER_SANITIZE_NUMBER_INT); |
|
64
|
|
|
$dataSanitized = ['attempts' => $attempts, 'mistakes' => $mistakes, 'duration' => $duration]; |
|
65
|
|
|
$this->getGameService()->memoryScore($this->game, $this->user, $dataSanitized); |
|
66
|
|
|
|
|
67
|
|
|
return $this->redirect()->toUrl( |
|
68
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
69
|
|
|
'memory/result', |
|
70
|
|
|
array('id' => $this->game->getIdentifier()) |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$viewModel = $this->buildView($this->game); |
|
76
|
|
|
|
|
77
|
|
|
return $viewModel; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function resultAction() |
|
81
|
|
|
{ |
|
82
|
|
|
$statusMail = null; |
|
83
|
|
|
$playLimitReached = false; |
|
84
|
|
|
if ($this->getRequest()->getQuery()->get('playLimitReached')) { |
|
|
|
|
|
|
85
|
|
|
$playLimitReached = true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
|
89
|
|
View Code Duplication |
if (!$lastEntry) { |
|
|
|
|
|
|
90
|
|
|
return $this->redirect()->toUrl( |
|
91
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
92
|
|
|
'memory', |
|
93
|
|
|
array('id' => $this->game->getIdentifier()), |
|
94
|
|
|
array('force_canonical' => true) |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Je recherche le score associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
|
100
|
|
|
$score = $this->getGameService()->getMemoryScoreMapper()->findOneBy(array('entry' => $lastEntry)); |
|
101
|
|
|
|
|
102
|
|
|
if (! $score) { |
|
103
|
|
|
return $this->redirect()->toUrl( |
|
104
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
105
|
|
|
'memory', |
|
106
|
|
|
array('id' => $this->game->getIdentifier()) |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
|
112
|
|
|
$form->setAttribute('method', 'post'); |
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
115
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
|
|
116
|
|
|
$form->setData($data); |
|
117
|
|
|
if ($form->isValid()) { |
|
118
|
|
|
$result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
|
119
|
|
|
if ($result) { |
|
120
|
|
|
$statusMail = true; |
|
121
|
|
|
$this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// buildView must be before sendMail because it adds the game template path to the templateStack |
|
127
|
|
|
$viewModel = $this->buildView($this->game); |
|
128
|
|
|
|
|
129
|
|
|
$viewModel->setVariables( |
|
|
|
|
|
|
130
|
|
|
array( |
|
131
|
|
|
'statusMail' => $statusMail, |
|
132
|
|
|
'form' => $form, |
|
133
|
|
|
'playLimitReached' => $playLimitReached, |
|
134
|
|
|
'entry' => $lastEntry, |
|
135
|
|
|
'score' => $score, |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
return $viewModel; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
View Code Duplication |
public function fbshareAction() |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
$result = parent::fbshareAction(); |
|
145
|
|
|
$bonusEntry = false; |
|
146
|
|
|
|
|
147
|
|
|
if ($result->getVariable('success')) { |
|
148
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$response = $this->getResponse(); |
|
152
|
|
|
$response->setContent( |
|
153
|
|
|
\Zend\Json\Json::encode( |
|
154
|
|
|
array( |
|
155
|
|
|
'success' => $result, |
|
156
|
|
|
'playBonus' => $bonusEntry |
|
157
|
|
|
) |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
return $response; |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
View Code Duplication |
public function fbrequestAction() |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$result = parent::fbrequestAction(); |
|
167
|
|
|
$bonusEntry = false; |
|
168
|
|
|
|
|
169
|
|
|
if ($result->getVariable('success')) { |
|
170
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$response = $this->getResponse(); |
|
174
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
|
175
|
|
|
'success' => $result, |
|
176
|
|
|
'playBonus' => $bonusEntry |
|
177
|
|
|
))); |
|
178
|
|
|
|
|
179
|
|
|
return $response; |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
View Code Duplication |
public function tweetAction() |
|
|
|
|
|
|
183
|
|
|
{ |
|
184
|
|
|
$result = parent::tweetAction(); |
|
185
|
|
|
$bonusEntry = false; |
|
186
|
|
|
|
|
187
|
|
|
if ($result->getVariable('success')) { |
|
188
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$response = $this->getResponse(); |
|
192
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
|
193
|
|
|
'success' => $result, |
|
194
|
|
|
'playBonus' => $bonusEntry |
|
195
|
|
|
))); |
|
196
|
|
|
|
|
197
|
|
|
return $response; |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
View Code Duplication |
public function googleAction() |
|
|
|
|
|
|
201
|
|
|
{ |
|
202
|
|
|
$result = parent::googleAction(); |
|
203
|
|
|
$bonusEntry = false; |
|
204
|
|
|
|
|
205
|
|
|
if ($result->getVariable('success')) { |
|
206
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$response = $this->getResponse(); |
|
210
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
|
211
|
|
|
'success' => $result, |
|
212
|
|
|
'playBonus' => $bonusEntry |
|
213
|
|
|
))); |
|
214
|
|
|
|
|
215
|
|
|
return $response; |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function getGameService() |
|
219
|
|
|
{ |
|
220
|
|
|
if (!$this->gameService) { |
|
221
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_memory_service'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $this->gameService; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.