6 use Controllers\IDivineController
;
7 use Services\Http\IHttpResponse
;
8 use Services\Uploads\IUploadManager
;
9 use Services\IUserSession
;
10 use Services\IZipParser
;
11 use Services\ISMOMatcher
;
12 use Services\IStatusReporter
;
13 use DataAccess\StepMania\ISimfileRepository
;
14 use DataAccess\StepMania\IPackRepository
;
15 use DataAccess\IFileRepository
;
16 use DataAccess\IDownloadRepository
;
17 use Domain\Entities\StepMania\ISimfile
;
18 use Domain\Entities\IFile
;
19 use Domain\Entities\StepMania\IPack
;
22 class SimfileController
implements IDivineController
24 private $_simfileRepository;
25 private $_packRepository;
26 private $_fileRepository;
28 private $_uploadManager;
31 private $_downloadRepository;
32 private $_statusReporter;
33 private $_userSession;
35 public function __construct(
36 IHttpResponse
$response,
37 IUploadManager
$uploadManager,
38 ISimfileRepository
$simfileRepository,
39 IPackRepository
$packRepository,
40 IFileRepository
$fileRepository,
41 IUserSession
$userSession,
42 IZipParser
$zipParser,
43 ISMOMatcher
$smoMatcher,
44 IDownloadRepository
$downloadRepository,
45 IStatusReporter
$statusReporter
47 $this->_response
= $response;
48 $this->_uploadManager
= $uploadManager;
49 $this->_simfileRepository
= $simfileRepository;
50 $this->_packRepository
= $packRepository;
51 $this->_fileRepository
= $fileRepository;
52 $this->_zipParser
= $zipParser;
53 $this->_smoMatcher
= $smoMatcher;
54 $this->_downloadRepository
= $downloadRepository;
55 $this->_statusReporter
= $statusReporter;
56 $this->_userSession
= $userSession;
59 public function indexAction() {
64 public function listAction()
66 /* @var $simfile Domain\Entities\StepMania\ISimfile */
67 $simfiles = $this->_simfileRepository
->findAll();
68 $packs = $this->_packRepository
->findAll();
69 $simfileArray = array();
72 foreach($simfiles as $simfile)
74 $simfileArray[] = $this->simfileToArray($simfile);
77 foreach($packs as $pack)
80 'title'=> $pack->getTitle(),
81 'contributors' => $pack->getContributors(),
82 'simfiles' => $this->getPackSimfilesArray($pack),
83 'banner' => $pack->getBanner() ?
'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',
84 'mirrors' => $this->getPackMirrorsArray($pack),
85 'size' => $pack->getFile() ? Util
::bytesToHumanReadable($pack->getFile()->getSize()) : null
,
86 'uploaded' => $pack->getFile() ?
date('F jS, Y', $pack->getFile()->getUploadDate()) : null
90 $returnArray = array('simfiles' => $simfileArray, 'packs' => $packArray);
92 $this->_response
->setHeader('Content-Type', 'application/json')
93 ->setBody(json_encode($returnArray))
97 public function latestSimfileAction()
99 $simfile = $this->_simfileRepository
->findRange(0, -1);
100 $this->_response
->setHeader('Content-Type', 'application/json')
101 ->setBody(json_encode($this->simfileToArray(reset($simfile))))
105 public function latestPackAction()
107 $pack = $this->_packRepository
->findRange(0, -1);
108 $pack = reset($pack);
111 'title'=> $pack->getTitle(),
112 'contributors' => $pack->getContributors(),
113 'simfiles' => $this->getPackSimfilesArray($pack),
114 'banner' => $pack->getBanner() ?
'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',
115 'mirrors' => $this->getPackMirrorsArray($pack),
116 'size' => $pack->getFile() ? Util
::bytesToHumanReadable($pack->getFile()->getSize()) : null
,
117 'uploaded' => $pack->getFile() ?
date('F jS, Y', $pack->getFile()->getUploadDate()) : null
120 $this->_response
->setHeader('Content-Type', 'application/json')
121 ->setBody(json_encode($packArray))
125 public function popularAction()
127 $returnArray = array();
128 $popularDownloads = $this->_downloadRepository
->findPopular();
129 $popularDownloads = reset($popularDownloads);
130 $packOrFileId = $popularDownloads->getFile()->getId();
132 $simfile = $this->_simfileRepository
->findByFileId($packOrFileId);
135 $returnArray = $this->simfileToArray(reset($simfile));
137 $pack = $this->_packRepository
->findByFileId($packOrFileId);
138 $pack = reset($pack);
139 $returnArray = array(
140 'title'=> $pack->getTitle(),
141 'contributors' => $pack->getContributors(),
142 'simfiles' => $this->getPackSimfilesArray($pack),
143 'banner' => $pack->getBanner() ?
'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',
144 'mirrors' => $this->getPackMirrorsArray($pack),
145 'size' => $pack->getFile() ? Util
::bytesToHumanReadable($pack->getFile()->getSize()) : null
,
146 'uploaded' => $pack->getFile() ?
date('F jS, Y', $pack->getFile()->getUploadDate()) : null
150 $this->_response
->setHeader('Content-Type', 'application/json')
151 ->setBody(json_encode($returnArray))
156 public function uploadAction()
158 if(!$this->_userSession
->getCurrentUser()) $this->_statusReporter
->error('You must be authenticated to upload files');
160 //TODO: Put directory in config ?
161 $files = $this->_uploadManager
->setFilesDirectory('../files')
162 ->setDestination('StepMania/')
165 foreach($files as $file)
167 $zipParser = $this->_zipParser
;
168 $zipParser->parse($file);
170 if(!$zipParser->simfiles()) $this->_statusReporter
->error('That zip doesn\'t seem to have any simfiles in it.');
172 //save the actual zip in the db
173 $this->findAndAddSmoMirror($file);
174 $this->_fileRepository
->save($file);
176 if($zipParser->isPack())
178 //XXX: Tricky! pack() uses packbuilder and so returns a new pack each time.
179 //I tried to be clever and call pack() multiple times thinking I was getting the same
180 //object. Should I cache it in zipparser?
181 $pack = $zipParser->pack();
182 $packBanner = $pack->getBanner() ?
$this->_fileRepository
->save($pack->getBanner()) : null
;
183 $this->_packRepository
->save($pack);
186 foreach($zipParser->simfiles() as $simfile)
188 $banner = $simfile->getBanner() ?
$this->_fileRepository
->save($simfile->getBanner()) : null
;
189 $simfileZip = $simfile->getSimfile() ?
$this->_fileRepository
->save($simfile->getSimfile()) : null
;
191 if(isset($pack)) $simfile->addToPack($pack);
192 $this->_simfileRepository
->save($simfile);
196 $this->_statusReporter
->success('Uploaded succesfully');
199 private function getPackMirrorsArray(IPack
$pack)
201 $packMirrors = array();
205 $packMirrors[] = array('source' => 'DivinElegy', 'uri' => 'files/pack/' . $pack->getFile()->getHash());
208 if($pack->getFile()->getMirrors())
210 foreach($pack->getFile()->getMirrors() as $mirror)
212 $packMirrors[] = array('source' => $mirror->getSource(), 'uri' => $mirror->getUri());
219 private function getPackSimfilesArray(IPack
$pack)
221 $packSimfiles = array();
222 foreach($pack->getSimfiles() as $simfile)
224 $packSimfiles[] = $this->simfileToArray($simfile);
227 return $packSimfiles;
230 private function findAndAddSmoMirror(IFile
$file)
232 $basename = pathinfo($file->getFilename(), PATHINFO_FILENAME
);
233 $match = $this->_smoMatcher
->match($basename, $file->getSize());
235 //XXX: Direct instantiation of FileMirror bad?
236 if($match && $match['confidence'] > 90)
238 $file->addMirror(new \Domain\VOs\
FileMirror($match['href'], 'Stepmania Online'));
242 private function simfileToArray(ISimfile
$simfile)
244 $singleSteps = array();
245 $doubleSteps = array();
247 foreach($simfile->getSteps() as $steps)
249 $stepDetails = array(
250 'artist' => $steps->getArtist()->getTag(),
251 'difficulty' => $steps->getDifficulty()->getITGName(),
252 'rating' => $steps->getRating()
255 if($steps->getMode()->getPrettyName() == 'Single')
257 $singleSteps[] = $stepDetails;
259 $doubleSteps[] = $stepDetails;
264 'title' => $simfile->getTitle(),
265 'artist' => $simfile->getArtist()->getName(),
267 'single' => $singleSteps,
268 'double' => $doubleSteps
270 'bgChanges' => $simfile->hasBgChanges() ?
'Yes' : 'No',
271 'fgChanges' => $simfile->hasFgChanges() ?
'Yes' : 'No',
272 'bpmChanges' => $simfile->hasBPMChanges() ?
'Yes' : 'No',
273 'banner' => $simfile->getBanner() ?
'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default',
274 'download' => $simfile->getSimfile() ?
'files/simfile/' . $simfile->getSimfile()->getHash() : null
,
275 'size' => $simfile->getSimfile() ? Util
::bytesToHumanReadable($simfile->getSimfile()->getSize()) : null
,
276 'uploaded' => $simfile->getSimfile() ?
date('F jS, Y', $simfile->getSimfile()->getUploadDate()) : null