1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Service; |
4
|
|
|
|
5
|
|
|
use Zend\Stdlib\ErrorHandler; |
6
|
|
|
|
7
|
|
|
class TradingCard extends Game |
8
|
|
|
{ |
9
|
|
|
protected $tradingcardMapper; |
10
|
|
|
protected $tradingcardmodelMapper; |
11
|
|
|
protected $tradingcardcardMapper; |
12
|
|
|
|
13
|
|
View Code Duplication |
public function getModelPath($model) |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$path = $this->getOptions()->getMediaPath().DIRECTORY_SEPARATOR; |
16
|
|
|
$path .= 'game'.$model->getGame()->getId().DIRECTORY_SEPARATOR; |
17
|
|
|
if (!is_dir($path)) { |
18
|
|
|
mkdir($path, 0777, true); |
19
|
|
|
} |
20
|
|
|
$path .= 'models'.DIRECTORY_SEPARATOR; |
21
|
|
|
if (!is_dir($path)) { |
22
|
|
|
mkdir($path, 0777, true); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $path; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function getModelMediaUrl($model) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$media_url = $this->getOptions()->getMediaUrl().'/'; |
31
|
|
|
$media_url .= 'game'.$model->getGame()->getId().'/models/'; |
32
|
|
|
|
33
|
|
|
return $media_url; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $data |
38
|
|
|
* @return \PlaygroundGame\Entity\Game |
39
|
|
|
*/ |
40
|
|
|
public function updateModel(array $data, $model) |
41
|
|
|
{ |
42
|
|
|
$form = $this->serviceLocator->get('playgroundgame_tradingcardmodel_form'); |
43
|
|
|
$tradingcard = $this->getGameMapper()->findById($data['trading_card_id']); |
44
|
|
|
$model->setGame($tradingcard); |
45
|
|
|
$path = $this->getModelPath($model); |
46
|
|
|
$media_url = $this->getModelMediaUrl($model); |
47
|
|
|
|
48
|
|
|
$form->bind($model); |
49
|
|
|
$form->setData($data); |
50
|
|
|
|
51
|
|
|
if (!$form->isValid()) { |
52
|
|
|
return false; |
|
|
|
|
53
|
|
|
} |
54
|
|
View Code Duplication |
if (!empty($data['upload_image']['tmp_name'])) { |
|
|
|
|
55
|
|
|
ErrorHandler::start(); |
56
|
|
|
$data['upload_image']['name'] = $this->fileNewname( |
57
|
|
|
$path, |
58
|
|
|
$model->getId()."-".$data['upload_image']['name'] |
59
|
|
|
); |
60
|
|
|
move_uploaded_file($data['upload_image']['tmp_name'], $path.$data['upload_image']['name']); |
61
|
|
|
$model->setImage($media_url.$data['upload_image']['name']); |
62
|
|
|
ErrorHandler::stop(true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->getTradingCardModelMapper()->update($model); |
66
|
|
|
$this->getEventManager()->trigger( |
67
|
|
|
__FUNCTION__ .'.post', |
68
|
|
|
$this, |
69
|
|
|
array('model' => $model, 'data' => $data) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $model; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getBooster($game, $user, $entry) |
76
|
|
|
{ |
77
|
|
|
// get booster config from $game |
78
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
79
|
|
|
$nb = $game->getBoosterCardNumber(); |
80
|
|
|
$booster = []; |
81
|
|
|
|
82
|
|
|
$today = new \DateTime("now"); |
83
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
84
|
|
|
|
85
|
|
|
$qb = $em->createQueryBuilder(); |
86
|
|
|
$and = $qb->expr()->andx(); |
87
|
|
|
$and->add( |
88
|
|
|
$qb->expr()->orX( |
89
|
|
|
$qb->expr()->lte('g.availability', ':date'), |
90
|
|
|
$qb->expr()->isNull('g.availability') |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$qb->setParameter('date', $today); |
95
|
|
|
$qb->select('g') |
96
|
|
|
->from('PlaygroundGame\Entity\TradingCardModel', 'g') |
97
|
|
|
->where($and); |
98
|
|
|
|
99
|
|
|
$query = $qb->getQuery(); |
100
|
|
|
$models = $query->getResult(); |
101
|
|
|
|
102
|
|
|
shuffle($models); |
103
|
|
|
|
104
|
|
|
$eventModels = $this->getEventManager()->trigger( |
105
|
|
|
__FUNCTION__ .'.pre', |
106
|
|
|
$this, |
107
|
|
|
array( |
108
|
|
|
'game' => $game, |
109
|
|
|
'user' => $user, |
110
|
|
|
'entry' => $entry, |
111
|
|
|
'models' => $models, |
112
|
|
|
) |
113
|
|
|
)->last(); |
114
|
|
|
|
115
|
|
|
if ($eventModels) { |
116
|
|
|
$models = $eventModels; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
for ($i = 0; $i < $nb; $i++) { |
120
|
|
|
$model = $models[$i]; |
121
|
|
|
$card = new \PlaygroundGame\Entity\TradingCardCard(); |
122
|
|
|
$card->setUser($user); |
123
|
|
|
$card->setModel($model); |
124
|
|
|
$card->setGame($game); |
125
|
|
|
$card->setEntry($entry); |
126
|
|
|
$card = $this->getTradingCardCardMapper()->insert($card); |
127
|
|
|
|
128
|
|
|
$booster[] = $card; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$eventBooster = $this->getEventManager()->trigger( |
132
|
|
|
__FUNCTION__ .'.post', |
133
|
|
|
$this, |
134
|
|
|
array( |
135
|
|
|
'game' => $game, |
136
|
|
|
'user' => $user, |
137
|
|
|
'entry' => $entry, |
138
|
|
|
'booster' => $booster, |
139
|
|
|
) |
140
|
|
|
)->last(); |
141
|
|
|
|
142
|
|
|
if ($eventBooster) { |
143
|
|
|
$booster = $eventBooster; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// sending a booster represents an entry. We close the entry after that |
147
|
|
|
$entry->setActive(0); |
148
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
|
|
|
|
149
|
|
|
|
150
|
|
|
return $booster; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getAlbum($game, $user) |
154
|
|
|
{ |
155
|
|
|
// all the models of the album |
156
|
|
|
$models = $this->getTradingCardModelMapper()->findAll(); |
157
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
158
|
|
|
|
159
|
|
|
if ($user) { |
160
|
|
|
$qb = $em->createQueryBuilder(); |
161
|
|
|
$and = $qb->expr()->andx(); |
162
|
|
|
$and->add($qb->expr()->eq('g.id', ':game')); |
163
|
|
|
$qb->setParameter('game', $game); |
164
|
|
|
$and->add($qb->expr()->eq('u.id', ':user')); |
165
|
|
|
$qb->setParameter('user', $user); |
166
|
|
|
$qb->select('c') |
167
|
|
|
->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
168
|
|
|
->innerJoin('c.game', 'g') |
169
|
|
|
->innerJoin('c.model', 'm') |
170
|
|
|
->innerJoin('c.user', 'u') |
171
|
|
|
->where($and) |
172
|
|
|
->orderBy('m.id', 'ASC') |
173
|
|
|
->groupBy('c.model'); |
174
|
|
|
$query = $qb->getQuery(); |
175
|
|
|
} elseif ($game->getAnonymousAllowed() && $this->getAnonymousIdentifier()) { |
176
|
|
|
$limitDate = $this->getLimitDate('always'); |
177
|
|
|
$entries = $this->getEntryMapper()->findLastEntriesByAnonymousIdentifier( |
178
|
|
|
$game, |
179
|
|
|
$this->getAnonymousIdentifier(), |
180
|
|
|
$limitDate |
181
|
|
|
); |
182
|
|
|
$qb = $em->createQueryBuilder(); |
183
|
|
|
$qb->andWhere('e.id IN (:entries)')->setParameter('entries', $entries); |
184
|
|
|
$qb->select('c') |
185
|
|
|
->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
186
|
|
|
->innerJoin('c.model', 'm') |
187
|
|
|
->innerJoin('c.game', 'g') |
188
|
|
|
->innerJoin('c.entry', 'e') |
189
|
|
|
->orderBy('m.id', 'ASC') |
190
|
|
|
->groupBy('c.model'); |
191
|
|
|
$query = $qb->getQuery(); |
192
|
|
|
} elseif ($game->getAnonymousAllowed()) { |
193
|
|
|
$limitDate = $this->getLimitDate('always'); |
194
|
|
|
$entries = $this->getEntryMapper()->findLastEntriesByIp( |
195
|
|
|
$game, |
196
|
|
|
$this->getIp(), |
197
|
|
|
$limitDate |
198
|
|
|
); |
199
|
|
|
$qb = $em->createQueryBuilder(); |
200
|
|
|
$qb->andWhere('e.id IN (:entries)')->setParameter('entries', $entries); |
201
|
|
|
$qb->select('c') |
202
|
|
|
->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
203
|
|
|
->innerJoin('c.model', 'm') |
204
|
|
|
->innerJoin('c.game', 'g') |
205
|
|
|
->innerJoin('c.entry', 'e') |
206
|
|
|
->orderBy('m.id', 'ASC') |
207
|
|
|
->groupBy('c.model'); |
208
|
|
|
$query = $qb->getQuery(); |
209
|
|
View Code Duplication |
} else { |
|
|
|
|
210
|
|
|
// If the game is supposed to be a regular user game or an anonymous identified game, |
211
|
|
|
// it means that the registration/login is at the end of the game |
212
|
|
|
if ((!$user && !$game->getAnonymousAllowed()) || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) { |
213
|
|
|
return 0; |
214
|
|
|
} |
215
|
|
|
return $this->getEntryMapper()->findLastEntriesByIp($game, $this->getIp(), $limitDate); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
// all the cards of the user |
220
|
|
|
$cards = $query->getResult(); |
221
|
|
|
$cardsArray = []; |
222
|
|
|
foreach ($cards as $card) { |
223
|
|
|
$cardsArray[$card->getModel()->getId()] = $card; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$album = []; |
227
|
|
|
|
228
|
|
|
// I create the complete album including the cards of the user |
229
|
|
|
foreach ($models as $model) { |
230
|
|
|
$sticker['model'] = $model; |
|
|
|
|
231
|
|
|
if (isset($cardsArray[$model->getId()])) { |
232
|
|
|
$sticker['card'] = $cardsArray[$model->getId()]; |
233
|
|
|
} else { |
234
|
|
|
$sticker['card'] = null; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
$album[] = $sticker; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $album; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function getGameEntity() |
243
|
|
|
{ |
244
|
|
|
return new \PlaygroundGame\Entity\TradingCard; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getTradingCardMapper() |
248
|
|
|
{ |
249
|
|
|
if (null === $this->tradingcardMapper) { |
250
|
|
|
$this->tradingcardMapper = $this->serviceLocator->get('playgroundgame_tradingcard_mapper'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this->tradingcardMapper; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function getTradingCardCardMapper() |
257
|
|
|
{ |
258
|
|
|
if (null === $this->tradingcardcardMapper) { |
259
|
|
|
$this->tradingcardcardMapper = $this->serviceLocator->get('playgroundgame_tradingcard_card_mapper'); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this->tradingcardcardMapper; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function getTradingCardModelMapper() |
266
|
|
|
{ |
267
|
|
|
if (null === $this->tradingcardmodelMapper) { |
268
|
|
|
$this->tradingcardmodelMapper = $this->serviceLocator->get('playgroundgame_tradingcard_model_mapper'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $this->tradingcardmodelMapper; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
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.