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