|
1
|
|
|
<?php |
|
2
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
5
|
|
|
|
|
6
|
|
|
class TradingCardController 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
|
|
|
/** |
|
19
|
|
|
* Example of AJAX File Upload with Session Progress and partial validation. |
|
20
|
|
|
* It's now possible to send a base64 image in this case the call is the form : |
|
21
|
|
|
* this._ajax( |
|
22
|
|
|
* { |
|
23
|
|
|
* url: url.dataset.url, |
|
24
|
|
|
* method: 'post', |
|
25
|
|
|
* body: 'photo=' + image |
|
26
|
|
|
* }, |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Zend\Stdlib\ResponseInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
public function ajaxuploadAction() |
|
31
|
|
|
{ |
|
32
|
|
|
// Call this for the session lock to be released (other ajax calls can then be made) |
|
33
|
|
|
session_write_close(); |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
if (! $this->game) { |
|
|
|
|
|
|
36
|
|
|
$this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
|
37
|
|
|
'success' => 0 |
|
38
|
|
|
))); |
|
39
|
|
|
|
|
40
|
|
|
return $this->getResponse(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user); |
|
44
|
|
|
if (!$entry) { |
|
45
|
|
|
// the user has already taken part to this game and the participation limit has been reached |
|
46
|
|
|
$this->getResponse()->setContent( |
|
47
|
|
|
\Zend\Json\Json::encode( |
|
48
|
|
|
array( |
|
49
|
|
|
'success' => 0 |
|
50
|
|
|
) |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return $this->getResponse(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
58
|
|
|
$data = $this->getRequest()->getFiles()->toArray(); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
if (empty($data)) { |
|
|
|
|
|
|
61
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
|
|
62
|
|
|
$key = key($data); |
|
63
|
|
|
$uploadImage = array( |
|
64
|
|
|
'name' => $key.'.png', |
|
65
|
|
|
'error' => 0, |
|
66
|
|
|
'base64' => $data[$key] |
|
67
|
|
|
); |
|
68
|
|
|
$data = array($key => $uploadImage); |
|
69
|
|
|
} |
|
70
|
|
|
$path = $this->getGameService()->getGameUserPath($this->game, $this->user); |
|
71
|
|
|
$media_url = '/'.$this->getGameService()->getGameUserMediaUrl($this->game, $this->user); |
|
72
|
|
|
$uploadFile = $this->getGameService()->uploadFile( |
|
73
|
|
|
$path, |
|
74
|
|
|
$data[$key], |
|
|
|
|
|
|
75
|
|
|
false |
|
76
|
|
|
); |
|
77
|
|
|
$result = $media_url.$uploadFile; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
|
81
|
|
|
'success' => true, |
|
82
|
|
|
'fileUrl' => $result |
|
|
|
|
|
|
83
|
|
|
))); |
|
84
|
|
|
|
|
85
|
|
|
return $this->getResponse(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function playAction() |
|
89
|
|
|
{ |
|
90
|
|
|
$playError = null; |
|
91
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user, $playError); |
|
92
|
|
View Code Duplication |
if (!$entry) { |
|
|
|
|
|
|
93
|
|
|
$reason = ""; |
|
|
|
|
|
|
94
|
|
|
if ($playError === -1) { |
|
95
|
|
|
// the user has already taken part to this game and the participation limit has been reached |
|
96
|
|
|
$this->flashMessenger()->addMessage('Vous avez déjà participé'); |
|
|
|
|
|
|
97
|
|
|
$reason = '?playLimitReached=1'; |
|
98
|
|
|
$noEntryRedirect = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
99
|
|
|
$this->game->getClassType().'/result', |
|
100
|
|
|
array( |
|
101
|
|
|
'id' => $this->game->getIdentifier(), |
|
102
|
|
|
) |
|
103
|
|
|
) .$reason; |
|
104
|
|
|
} else if ($playError === -2) { |
|
105
|
|
|
// the user has not accepted the mandatory rules of the game |
|
106
|
|
|
$this->flashMessenger()->addMessage('Vous devez accepter le réglement'); |
|
|
|
|
|
|
107
|
|
|
$reason = '?NoOptin=1'; |
|
108
|
|
|
$noEntryRedirect = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
109
|
|
|
$this->game->getClassType(), |
|
110
|
|
|
array( |
|
111
|
|
|
'id' => $this->game->getIdentifier(), |
|
112
|
|
|
) |
|
113
|
|
|
) .$reason; |
|
114
|
|
|
} else if ($playError === -3) { |
|
115
|
|
|
// the user has enough points to buy an entry to this game |
|
116
|
|
|
$this->flashMessenger()->addMessage("Vous ne pouvez pas acheter la partie"); |
|
|
|
|
|
|
117
|
|
|
$reason = '?NotPaid=1'; |
|
118
|
|
|
$noEntryRedirect = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
119
|
|
|
$this->game->getClassType(), |
|
120
|
|
|
array( |
|
121
|
|
|
'id' => $this->game->getIdentifier(), |
|
122
|
|
|
) |
|
123
|
|
|
) .$reason; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->redirect()->toUrl($noEntryRedirect); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
$viewModel = $this->buildView($this->game); |
|
129
|
|
|
$booster = null; |
|
130
|
|
|
if ($entry) { |
|
131
|
|
|
$booster = $this->getGameService()->getBooster($this->game, $this->user, $entry); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$album = $this->getGameService()->getAlbum($this->game, $this->user); |
|
135
|
|
|
$viewModel->setVariables( |
|
|
|
|
|
|
136
|
|
|
array( |
|
137
|
|
|
'booster' => $booster, |
|
138
|
|
|
'album' => $album |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
return $viewModel; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function resultAction() |
|
146
|
|
|
{ |
|
147
|
|
|
$playLimitReached = false; |
|
148
|
|
|
if ($this->getRequest()->getQuery()->get('playLimitReached')) { |
|
|
|
|
|
|
149
|
|
|
$playLimitReached = true; |
|
150
|
|
|
} |
|
151
|
|
|
$lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
|
152
|
|
|
if (!$lastEntry) { |
|
153
|
|
|
return $this->redirect()->toUrl( |
|
154
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
155
|
|
|
'tradingcard', |
|
156
|
|
|
array('id' => $this->game->getIdentifier()), |
|
157
|
|
|
array('force_canonical' => true) |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
$album = $this->getGameService()->getAlbum($this->game, $this->user); |
|
162
|
|
|
$viewModel = $this->buildView($this->game); |
|
163
|
|
|
$viewModel->setVariables( |
|
|
|
|
|
|
164
|
|
|
array( |
|
165
|
|
|
'album' => $album, |
|
166
|
|
|
'playLimitReached' => $playLimitReached, |
|
167
|
|
|
'entry' => $lastEntry |
|
168
|
|
|
) |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
return $viewModel; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function getGameService() |
|
175
|
|
|
{ |
|
176
|
|
|
if (!$this->gameService) { |
|
177
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_tradingcard_service'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $this->gameService; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
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.