1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Controller\Frontend\GameController; |
6
|
|
|
use Zend\Session\Container; |
7
|
|
|
use PlaygroundGame\Service\GameService; |
8
|
|
|
|
9
|
|
|
class MissionController extends GameController |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var gameService |
14
|
|
|
*/ |
15
|
|
|
protected $gameService; |
16
|
|
|
protected $mission; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Homepage of the game |
20
|
|
|
*/ |
21
|
|
|
public function indexAction() |
22
|
|
|
{ |
23
|
|
|
$isSubscribed = false; |
24
|
|
|
|
25
|
|
|
$entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
26
|
|
|
if ($entry) { |
27
|
|
|
$isSubscribed = true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$games = $this->getGameService()->getMissionGames($this->game, $this->user); |
31
|
|
|
|
32
|
|
|
$viewModel = $this->buildView($this->game); |
33
|
|
|
$viewModel->setVariables(array( |
|
|
|
|
34
|
|
|
'user' => $this->user, |
35
|
|
|
'games' => $games, |
36
|
|
|
'isSubscribed' => $isSubscribed, |
37
|
|
|
'flashMessages' => $this->flashMessenger()->getMessages(), |
38
|
|
|
)); |
39
|
|
|
|
40
|
|
|
return $viewModel; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function playAction() |
44
|
|
|
{ |
45
|
|
|
$subGameIdentifier = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
46
|
|
|
$subGame = $this->getGameService()->checkGame($subGameIdentifier); |
47
|
|
|
|
48
|
|
|
if (!$subGame) { |
49
|
|
|
$games = $this->getGameService()->getMissionGames($this->game, $this->user); |
50
|
|
|
foreach ($games as $k => $v) { |
51
|
|
|
$g = $v['game']; |
52
|
|
|
$entry = $v['entry']; |
|
|
|
|
53
|
|
|
if ($g->getGame()->isStarted() && $g->getGame()->isOnline()) { |
54
|
|
|
$subGame = $g->getGame(); |
55
|
|
|
$subGameIdentifier = $subGame->getIdentifier(); |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$socialLinkUrl = $this->frontendUrl()->fromRoute( |
|
|
|
|
62
|
|
|
'mission', |
63
|
|
|
array('id' => $this->game->getIdentifier()), |
64
|
|
|
array('force_canonical' => true) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$session = new Container('facebook'); |
68
|
|
|
// Redirect to fan gate if the game require to 'like' the page before playing |
69
|
|
|
if ($session->offsetExists('signed_request')) { |
70
|
|
|
if ($this->game->getFbFan()) { |
71
|
|
|
if ($this->getGameService()->checkIsFan($this->game) === false) { |
72
|
|
|
return $this->redirect()->toRoute( |
73
|
|
|
$this->game->getClassType().'/fangate', |
74
|
|
|
array('id' => $this->game->getIdentifier()) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$this->user) { |
81
|
|
|
|
82
|
|
|
// The game is deployed on Facebook, and played from Facebook : retrieve/register user |
83
|
|
|
if ($session->offsetExists('signed_request')) { |
84
|
|
|
|
85
|
|
|
// Get Playground user from Facebook info |
86
|
|
|
$viewModel = $this->buildView($this->game); |
|
|
|
|
87
|
|
|
$beforeLayout = $this->layout()->getTemplate(); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$view = $this->forward()->dispatch( |
90
|
|
|
'playgrounduser_user', |
91
|
|
|
array( |
92
|
|
|
'controller' => 'playgrounduser_user', |
93
|
|
|
'action' => 'registerFacebookUser', |
94
|
|
|
'provider' => 'facebook' |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->layout()->setTemplate($beforeLayout); |
99
|
|
|
$this->user = $view->user; |
100
|
|
|
|
101
|
|
|
// If the user cannot be created/retrieved from Facebook info, redirect to login/register form |
102
|
|
View Code Duplication |
if (!$this->user) { |
|
|
|
|
103
|
|
|
$redirect = urlencode( |
104
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
105
|
|
|
'mission/play', |
106
|
|
|
array('id' => $this->game->getIdentifier()), |
107
|
|
|
array('force_canonical' => true) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
if (array_search('login', $this->game->getStepsArray())) { |
111
|
|
|
return $this->redirect()->toUrl( |
112
|
|
|
$this->frontendUrl()->fromRoute('mission/login') . '?redirect='.$redirect |
|
|
|
|
113
|
|
|
); |
114
|
|
|
} else { |
115
|
|
|
return $this->redirect()->toUrl( |
116
|
|
|
$this->frontendUrl()->fromRoute('zfcuser/register') . '?redirect='.$redirect |
|
|
|
|
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// The game is not played from Facebook : redirect to login/register form |
122
|
|
|
|
123
|
|
View Code Duplication |
} elseif (!$this->game->getAnonymousAllowed()) { |
|
|
|
|
124
|
|
|
$redirect = urlencode( |
125
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
126
|
|
|
'mission/play', |
127
|
|
|
array('id' => $this->game->getIdentifier()), |
128
|
|
|
array('force_canonical' => true) |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
if (array_search('login', $this->game->getStepsArray())) { |
133
|
|
|
return $this->redirect()->toUrl( |
134
|
|
|
$this->frontendUrl()->fromRoute('mission/login') . '?redirect='.$redirect |
|
|
|
|
135
|
|
|
); |
136
|
|
|
} else { |
137
|
|
|
return $this->redirect()->toUrl( |
138
|
|
|
$this->frontendUrl()->fromRoute('zfcuser/register') . '?redirect='.$redirect |
|
|
|
|
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user); |
145
|
|
View Code Duplication |
if (!$entry) { |
|
|
|
|
146
|
|
|
// the user has already taken part of this game and the participation limit has been reached |
147
|
|
|
$this->flashMessenger()->addMessage('You have already played'); |
148
|
|
|
|
149
|
|
|
return $this->redirect()->toUrl( |
150
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
151
|
|
|
'mission/result', |
152
|
|
|
array('id' => $this->game->getIdentifier()) |
153
|
|
|
) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$beforeLayout = $this->layout()->getTemplate(); |
158
|
|
|
$subViewModel = $this->forward()->dispatch( |
159
|
|
|
'playgroundgame_'.$subGame->getClassType(), |
160
|
|
|
array( |
161
|
|
|
'controller' => 'playgroundgame_'.$subGame->getClassType(), |
162
|
|
|
'action' => 'play', |
163
|
|
|
'id' => $subGameIdentifier |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
if ($this->getResponse()->getStatusCode() == 302) { |
|
|
|
|
168
|
|
|
return $this->redirect()->toUrl( |
169
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
170
|
|
|
'mission/result', |
171
|
|
|
array( |
172
|
|
|
'id' => $this->game->getIdentifier(), |
173
|
|
|
'gameId' => $subGameIdentifier |
174
|
|
|
), |
175
|
|
|
array('force_canonical' => true) |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// suite au forward, le template de layout a changé, je dois le rétablir... |
181
|
|
|
$this->layout()->setTemplate($beforeLayout); |
182
|
|
|
|
183
|
|
|
// give the ability to the mission to have its customized look and feel. |
184
|
|
|
$templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
185
|
|
|
$l = $templatePathResolver->getPaths(); |
186
|
|
|
// I've already added the path for the game so the base path is $l[1] |
187
|
|
|
$templatePathResolver->addPath($l[1].'custom/'.$this->game->getIdentifier()); |
188
|
|
|
|
189
|
|
|
$subViewModel->mission = $this->game; |
190
|
|
|
|
191
|
|
|
return $subViewModel; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function resultAction() |
195
|
|
|
{ |
196
|
|
|
$statusMail = null; |
|
|
|
|
197
|
|
|
|
198
|
|
|
$subGameIdentifier = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
199
|
|
|
$subGame = $this->getGameService()->checkGame($subGameIdentifier); |
200
|
|
|
|
201
|
|
|
$secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
202
|
|
|
$socialLinkUrl = $this->frontendUrl()->fromRoute( |
|
|
|
|
203
|
|
|
'mission', |
204
|
|
|
array('id' => $game->getIdentifier()), |
|
|
|
|
205
|
|
|
array('force_canonical' => true) |
206
|
|
|
).'?key='.$secretKey; |
207
|
|
|
|
208
|
|
|
// With core shortener helper |
209
|
|
|
$socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
|
|
|
|
210
|
|
|
|
211
|
|
|
//TODO check existence of an active entry |
212
|
|
|
/*$lastEntry = $this->getGameService()->findLastInactiveEntry( |
213
|
|
|
$game, $user, $this->params()->fromQuery('anonymous_identifier') |
214
|
|
|
); |
215
|
|
|
if (!$lastEntry) { |
216
|
|
|
return $this->redirect()->toUrl($this->frontendUrl()->fromRoute( |
217
|
|
|
'mission', array('id' => $game->getIdentifier()), |
218
|
|
|
array('force_canonical' => true) |
219
|
|
|
)); |
220
|
|
|
}*/ |
221
|
|
|
|
222
|
|
|
if (!$this->user && !$this->game->getAnonymousAllowed()) { |
223
|
|
|
$redirect = urlencode( |
224
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
225
|
|
|
'mission/result', |
226
|
|
|
array('id' => $game->getIdentifier()) |
227
|
|
|
) |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
return $this->redirect()->toUrl( |
231
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
232
|
|
|
'zfcuser/register', |
233
|
|
|
array('channel' => $channel) |
|
|
|
|
234
|
|
|
) . '?redirect='.$redirect |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
239
|
|
|
$form->setAttribute('method', 'post'); |
240
|
|
|
|
241
|
|
|
/* |
242
|
|
|
if ($this->getRequest()->isPost()) { |
243
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
244
|
|
|
$form->setData($data); |
245
|
|
|
if ($form->isValid()) { |
246
|
|
|
$result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
247
|
|
|
if ($result) { |
248
|
|
|
$statusMail = true; |
249
|
|
|
//$bonusEntry = $this->getGameService()->addAnotherChance($game, $user, 1); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
*/ |
254
|
|
|
|
255
|
|
|
// buildView must be before sendMail because it adds the game template path to the templateStack |
256
|
|
|
// TODO : Improve this. |
257
|
|
|
$viewModel = $this->buildView($this->game); |
|
|
|
|
258
|
|
|
|
259
|
|
|
//$this->sendMail($game, $user, $lastEntry); |
260
|
|
|
|
261
|
|
|
$beforeLayout = $this->layout()->getTemplate(); |
|
|
|
|
262
|
|
|
$subViewModel = $this->forward()->dispatch( |
263
|
|
|
'playgroundgame_'.$subGame->getClassType(), |
264
|
|
|
array( |
265
|
|
|
'controller' => 'playgroundgame_'.$subGame->getClassType(), |
266
|
|
|
'action' => 'result', |
267
|
|
|
'id' => $subGameIdentifier |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
|
271
|
|
|
if ($this->getResponse()->getStatusCode() == 302) { |
|
|
|
|
272
|
|
|
$this->getResponse()->setStatusCode('200'); |
|
|
|
|
273
|
|
|
$urlRedirect = $this->getResponse()->getHeaders()->get('Location'); |
|
|
|
|
274
|
|
|
|
275
|
|
|
$subViewModel = $this->forward()->dispatch( |
276
|
|
|
'playgroundgame_'.$subGame->getClassType(), |
277
|
|
|
array( |
278
|
|
|
'controller' => 'playgroundgame_'.$subGame->getClassType(), |
279
|
|
|
'action' => 'result', |
280
|
|
|
'id' => $subGameIdentifier |
281
|
|
|
) |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
// suite au forward, le template de layout a changé, je dois le rétablir... |
285
|
|
|
$this->layout()->setTemplate($beforeLayout); |
286
|
|
|
|
287
|
|
|
// give the ability to the mission to have its customized look and feel. |
288
|
|
|
$templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
289
|
|
|
$l = $templatePathResolver->getPaths(); |
290
|
|
|
// I've already added the path for the game so the base path is $l[1] |
291
|
|
|
$templatePathResolver->addPath($l[1].'custom/'.$this->game->getIdentifier()); |
292
|
|
|
|
293
|
|
|
$subViewModel->mission = $this->game; |
294
|
|
|
return $subViewModel; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
public function fbshareAction() |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$sg = $this->getGameService(); |
300
|
|
|
$result = parent::fbshareAction(); |
301
|
|
|
$bonusEntry = false; |
302
|
|
|
|
303
|
|
|
if ($result) { |
304
|
|
|
$identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
305
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
306
|
|
|
$game = $sg->checkGame($identifier); |
307
|
|
|
$bonusEntry = $sg->addAnotherChance($game, $user, 1); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$response = $this->getResponse(); |
311
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
312
|
|
|
'success' => $result, |
313
|
|
|
'playBonus' => $bonusEntry |
314
|
|
|
))); |
315
|
|
|
|
316
|
|
|
return $response; |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
View Code Duplication |
public function fbrequestAction() |
|
|
|
|
320
|
|
|
{ |
321
|
|
|
$sg = $this->getGameService(); |
322
|
|
|
$result = parent::fbrequestAction(); |
323
|
|
|
$bonusEntry = false; |
324
|
|
|
|
325
|
|
|
if ($result) { |
326
|
|
|
$identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
327
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
328
|
|
|
$game = $sg->checkGame($identifier); |
329
|
|
|
$bonusEntry = $sg->addAnotherChance($game, $user, 1); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
$response = $this->getResponse(); |
333
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
334
|
|
|
'success' => $result, |
335
|
|
|
'playBonus' => $bonusEntry |
336
|
|
|
))); |
337
|
|
|
|
338
|
|
|
return $response; |
|
|
|
|
339
|
|
|
} |
340
|
|
|
|
341
|
|
View Code Duplication |
public function tweetAction() |
|
|
|
|
342
|
|
|
{ |
343
|
|
|
$sg = $this->getGameService(); |
344
|
|
|
$result = parent::tweetAction(); |
345
|
|
|
$bonusEntry = false; |
346
|
|
|
|
347
|
|
|
if ($result) { |
348
|
|
|
$identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
349
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
350
|
|
|
$game = $sg->checkGame($identifier); |
351
|
|
|
$bonusEntry = $sg->addAnotherChance($game, $user, 1); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$response = $this->getResponse(); |
355
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
356
|
|
|
'success' => $result, |
357
|
|
|
'playBonus' => $bonusEntry |
358
|
|
|
))); |
359
|
|
|
|
360
|
|
|
return $response; |
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
View Code Duplication |
public function googleAction() |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
$sg = $this->getGameService(); |
366
|
|
|
$result = parent::googleAction(); |
367
|
|
|
$bonusEntry = false; |
368
|
|
|
|
369
|
|
|
if ($result) { |
370
|
|
|
$identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
371
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
372
|
|
|
$game = $sg->checkGame($identifier); |
373
|
|
|
$bonusEntry = $sg->addAnotherChance($game, $user, 1); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
$response = $this->getResponse(); |
377
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
378
|
|
|
'success' => $result, |
379
|
|
|
'playBonus' => $bonusEntry |
380
|
|
|
))); |
381
|
|
|
|
382
|
|
|
return $response; |
|
|
|
|
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function getGameService() |
386
|
|
|
{ |
387
|
|
|
if (!$this->gameService) { |
388
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_mission_service'); |
|
|
|
|
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $this->gameService; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public function setGameService(GameService $gameService) |
395
|
|
|
{ |
396
|
|
|
$this->gameService = $gameService; |
|
|
|
|
397
|
|
|
|
398
|
|
|
return $this; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: