1
|
|
|
<?php |
2
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
3
|
|
|
|
4
|
|
|
class TradingCardController extends GameController |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @var gameService |
8
|
|
|
*/ |
9
|
|
|
protected $gameService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Example of AJAX File Upload with Session Progress and partial validation. |
13
|
|
|
* It's now possible to send a base64 image in this case the call is the form : |
14
|
|
|
* this._ajax( |
15
|
|
|
* { |
16
|
|
|
* url: url.dataset.url, |
17
|
|
|
* method: 'post', |
18
|
|
|
* body: 'photo=' + image |
19
|
|
|
* }, |
20
|
|
|
* |
21
|
|
|
* @return \Zend\Stdlib\ResponseInterface |
22
|
|
|
*/ |
23
|
|
|
public function ajaxuploadAction() |
24
|
|
|
{ |
25
|
|
|
// Call this for the session lock to be released (other ajax calls can then be made) |
26
|
|
|
session_write_close(); |
27
|
|
|
|
28
|
|
View Code Duplication |
if (! $this->game) { |
|
|
|
|
29
|
|
|
$this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
30
|
|
|
'success' => 0 |
31
|
|
|
))); |
32
|
|
|
|
33
|
|
|
return $this->getResponse(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user); |
37
|
|
|
if (!$entry) { |
38
|
|
|
// the user has already taken part of this game and the participation limit has been reached |
39
|
|
|
$this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
40
|
|
|
'success' => 0 |
41
|
|
|
))); |
42
|
|
|
|
43
|
|
|
return $this->getResponse(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
47
|
|
|
|
48
|
|
|
$data = $this->getRequest()->getFiles()->toArray(); |
|
|
|
|
49
|
|
|
|
50
|
|
View Code Duplication |
if (empty($data)) { |
|
|
|
|
51
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
52
|
|
|
$key = key($data); |
53
|
|
|
$uploadImage = array('name' => $key.'.png', 'error' => 0, 'base64' => $data[$key]); |
54
|
|
|
$data = array($key => $uploadImage); |
55
|
|
|
} |
56
|
|
|
$path = $this->getGameService()->getGameUserPath($this->game, $this->user); |
57
|
|
|
$media_url = '/'.$this->getGameService()->getGameUserMediaUrl($this->game, $this->user); |
58
|
|
|
$uploadFile = $this->getGameService()->uploadFile( |
59
|
|
|
$path, |
60
|
|
|
$data[$key] |
|
|
|
|
61
|
|
|
); |
62
|
|
|
$result = $media_url.$uploadFile; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
66
|
|
|
'success' => true, |
67
|
|
|
'fileUrl' => $result |
|
|
|
|
68
|
|
|
))); |
69
|
|
|
|
70
|
|
|
return $this->getResponse(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function playAction() |
74
|
|
|
{ |
75
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user); |
76
|
|
|
$viewModel = $this->buildView($this->game); |
77
|
|
|
$booster = null; |
78
|
|
|
if ($entry) { |
79
|
|
|
$booster = $this->getGameService()->getBooster($this->game, $this->user, $entry); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$album = $this->getGameService()->getAlbum($this->game, $this->user); |
83
|
|
|
$viewModel->setVariables(array('booster' => $booster, 'album' => $album)); |
|
|
|
|
84
|
|
|
|
85
|
|
|
return $viewModel; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function resultAction() |
89
|
|
|
{ |
90
|
|
|
$album = $this->getGameService()->getAlbum($this->game, $this->user); |
91
|
|
|
$viewModel = $this->buildView($this->game); |
92
|
|
|
$viewModel->setVariables(array('album' => $album)); |
|
|
|
|
93
|
|
|
|
94
|
|
|
return $viewModel; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getGameService() |
98
|
|
|
{ |
99
|
|
|
if (!$this->gameService) { |
100
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_tradingcard_service'); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->gameService; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.