5 use Controllers\IDivineController
;
6 use Services\Http\IHttpResponse
;
7 use Services\Uploads\IUploadManager
;
8 use Services\IUserSession
;
9 use Services\IZipParser
;
10 use DataAccess\StepMania\ISimfileRepository
;
11 use DataAccess\StepMania\IPackRepository
;
12 use DataAccess\IFileRepository
;
13 use Domain\Entities\StepMania\ISimfile
;
14 use Domain\Entities\StepMania\IPack
;
16 class SimfileController
implements IDivineController
18 private $_simfileRepository;
19 private $_packRepository;
20 private $_fileRepository;
22 private $_uploadManager;
23 private $_userSession;
26 public function __construct(
27 IHttpResponse
$response,
28 IUploadManager
$uploadManager,
29 ISimfileRepository
$simfileRepository,
30 IPackRepository
$packRepository,
31 IFileRepository
$fileRepository,
32 IUserSession
$userSession,
35 $this->_response
= $response;
36 $this->_uploadManager
= $uploadManager;
37 $this->_simfileRepository
= $simfileRepository;
38 $this->_packRepository
= $packRepository;
39 $this->_fileRepository
= $fileRepository;
40 $this->_userSession
= $userSession;
41 $this->_zipParser
= $zipParser;
44 public function indexAction() {
49 public function listAction()
51 /* @var $simfile Domain\Entities\StepMania\ISimfile */
52 $simfiles = $this->_simfileRepository
->findAll();
53 $packs = $this->_packRepository
->findAll();
54 $simfileArray = array();
57 foreach($simfiles as $simfile)
59 $simfileArray[] = $this->simfileToArray($simfile);
62 foreach($packs as $pack)
64 $packSimfiles = array();
65 foreach($pack->getSimfiles() as $simfile)
67 $packSimfiles[] = $this->simfileToArray($simfile);
71 'title'=> $pack->getTitle(),
72 'contributors' => $pack->getContributors(),
73 'simfiles' => $packSimfiles,
74 'banner' => $pack->getBanner() ?
'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default'
78 $returnArray = array('simfiles' => $simfileArray, 'packs' => $packArray);
80 $this->_response
->setHeader('Content-Type', 'application/json')
81 ->setBody(json_encode($returnArray))
85 public function uploadAction()
87 //TODO: Put directory in config ?
88 $files = $this->_uploadManager
->setFilesDirectory('../files')
89 ->setDestination('StepMania/')
92 foreach($files as $file)
94 $zipParser = $this->_zipParser
;
95 $zipParser->parse($file);
97 //save the actual zip in the db
98 $this->_fileRepository
->save($file);
100 if($zipParser->isPack())
102 //XXX: Tricky! pack() uses packbuilder and so returns a new pack each time.
103 //I tried to be clever and call pack() multiple times thinking I was getting the same
104 //object. Should I cache it in zipparser?
105 $pack = $zipParser->pack();
106 $packBanner = $pack->getBanner() ?
$this->_fileRepository
->save($pack->getBanner()) : null
;
107 $this->_packRepository
->save($pack);
110 foreach($zipParser->simfiles() as $simfile)
112 $banner = $simfile->getBanner() ?
$this->_fileRepository
->save($simfile->getBanner()) : null
;
113 $simfileZip = $simfile->getSimfile() ?
$this->_fileRepository
->save($simfile->getSimfile()) : null
;
115 if(isset($pack)) $simfile->addToPack($pack);
116 $this->_simfileRepository
->save($simfile);
121 private function simfileToArray(ISimfile
$simfile)
123 $singleSteps = array();
124 $doubleSteps = array();
126 foreach($simfile->getSteps() as $steps)
128 $stepDetails = array(
129 'artist' => $steps->getArtist()->getTag(),
130 'difficulty' => $steps->getDifficulty()->getITGName(),
131 'rating' => $steps->getRating()
134 if($steps->getMode()->getPrettyName() == 'Single')
136 $singleSteps[] = $stepDetails;
138 $doubleSteps[] = $stepDetails;
143 'title' => $simfile->getTitle(),
144 'artist' => $simfile->getArtist()->getName(),
146 'single' => $singleSteps,
147 'double' => $doubleSteps
149 'bgChanges' => $simfile->hasBgChanges() ?
'Yes' : 'No',
150 'fgChanges' => $simfile->hasFgChanges() ?
'Yes' : 'No',
151 'bpmChanges' => $simfile->hasBPMChanges() ?
'Yes' : 'No',
152 'banner' => $simfile->getBanner() ?
'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default'