From: Cameron Ball Date: Mon, 10 Nov 2014 09:01:16 +0000 (+0800) Subject: Bleh X-Git-Url: http://git.cameron1729.xyz/?p=rock.divinelegy.git;a=commitdiff_plain;h=32c8dd849d26e016f02fc50f4d024526e451ceab Bleh --- diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php index 3a042d7..8585cc5 100644 --- a/Controllers/SimfileController.php +++ b/Controllers/SimfileController.php @@ -2,11 +2,16 @@ namespace Controllers; +use ZipArchive; +use Exception; use Controllers\IDivineController; use Services\Http\IHttpRequest; use Services\Http\IHttpResponse; use Services\Uploads\IUploadManager; +use Services\ISimfileParser; use DataAccess\StepMania\ISimfileRepository; +use DataAccess\IUserRepository; +use Domain\Entities\StepMania\ISimfileStepByStepBuilder; class SimfileController implements IDivineController { @@ -14,17 +19,26 @@ class SimfileController implements IDivineController private $_response; private $_request; private $_uploadManager; + private $_simfileParser; + private $_simfileBuilder; + private $_userRepository; public function __construct( IHttpRequest $request, IHttpResponse $response, IUploadManager $uploadManager, - ISimfileRepository $repository + ISimfileRepository $repository, + IUserRepository $userRepository, + ISimfileParser $simfileParser, + ISimfileStepByStepBuilder $simfileBuilder ) { $this->_request = $request; $this->_response = $response; $this->_uploadManager = $uploadManager; $this->_simfileRepository = $repository; + $this->_userRepository = $userRepository; + $this->_simfileParser = $simfileParser; + $this->_simfileBuilder = $simfileBuilder; } public function indexAction() { @@ -81,22 +95,52 @@ class SimfileController implements IDivineController public function uploadAction() { //logic for if pack or individual file - + $request = $this->_request->post(); + //XXX: Could be a place UserService could look for token ? + $token = $request['token']; + //TODO: Put directory in config ? $filenames = $this->_uploadManager->setDestination('../files/StepMania/') ->process(); - //parse .sm files and save to DB. should use SimfileParser service - - echo '
';
         print_r($filenames);
-        echo '
'; - } - - public function testAction($testArg) - { - $this->_response->setHeader('Content-Type', 'application/json') - ->setBody(json_encode(array('testArg' => $testArg))) - ->sendResponse(); + foreach($filenames as $filename => $hash) + { + $za = new ZipArchive(); + //XXX: We assume all files are zips. Should be enforced by validation elsewhere. + $res = $za->open('../files/StepMania/' . $hash . '.zip'); + + if($res !== true) throw new Exception ('Could not open zip for reading.'); + + for($i=0; $i<$za->numFiles; $i++) + { + $stat = $za->statIndex($i); + if(pathinfo($stat['name'], PATHINFO_EXTENSION) == 'sm') + { + $smData = file_get_contents('zip://../files/StepMania/' . $hash . '.zip#' . $stat['name']); + break; + } + } + } + + if(!$smData) throw new Exception('Could not extract simfile.'); + + /* @var $parser \Services\ISimfileParser */ + $parser = $this->_simfileParser; + $parser->parse($smData); + + //TODO: Instantiating VOs like this bad ? + $this->_simfileBuilder->With_Title($parser->title()) + ->With_Artist(new \Domain\VOs\StepMania\Artist($parser->artist())) + ->With_Uploader($this->_userRepository->findByAuthToken($token)) //obj + ->With_BPM($bpm) //obj + ->With_BpmChanges($parser->bpmChanges()) + ->With_Stops($parser->stops()) + ->With_FgChanges($parser->fgChanges()) + ->With_BgChanges($parser->bgChanges()) + ->With_Steps($steps) //obj + ->build(); + + //parse .sm files and save to DB. should use SimfileParser service } } diff --git a/DataAccess/UserRepository.php b/DataAccess/UserRepository.php index b98ddd2..e4acdc3 100644 --- a/DataAccess/UserRepository.php +++ b/DataAccess/UserRepository.php @@ -7,6 +7,8 @@ use DataAccess\DataMapper\IDataMapper; use Domain\Entities\IUser; use DataAccess\Queries\IQueryBuilderFactory; use Services\IFacebookSessionFactory; +use Facebook\FacebookRequest; +use Facebook\GraphUser; //TODO: Implement some sort of caching. Probably OK for now not to worry. class UserRepository implements IUserRepository @@ -53,7 +55,9 @@ class UserRepository implements IUserRepository public function findByAuthToken($token) { $facebookSession = $this->_facebookSessionFactory->createInstance($token); - $id = $facebookSession->getGraphObject(GraphUser::className())->getId(); + $facebookRequest = (new FacebookRequest($facebookSession, 'GET', '/me?fields=hometown,first_name,last_name'))->execute(); + + $id = $facebookRequest->getGraphObject(GraphUser::className())->getId(); return $this->findByFacebookId($id); } diff --git a/Services/ISimfileParser.php b/Services/ISimfileParser.php index a7e2208..059dec0 100644 --- a/Services/ISimfileParser.php +++ b/Services/ISimfileParser.php @@ -4,8 +4,10 @@ namespace Services; interface ISimfileParser { + public function parse($smFileData); public function title(); public function subtitle(); + public function artist(); public function bpmChanges(); public function stops(); public function bgChanges(); diff --git a/Services/IUserSession.php b/Services/IUserSession.php new file mode 100644 index 0000000..96b13dd --- /dev/null +++ b/Services/IUserSession.php @@ -0,0 +1,9 @@ +_smFileLines = explode("\n", $simfileData); + $this->_smFileLines = explode(";", $simfileData); } public function title() { $title = $this->extractKey('TITLE'); - if(!$title) throw new Exception ('Invalid SM file. Title missing'); + if(!$title) throw new Exception ('Invalid SM file. TITLE missing'); return $title; } + public function artist() + { + $artist = $this->extractKey('ARTIST'); + if(!$artist) throw new Exception ('Invalid SM file. ARTIST missing'); + + return $artist; + } + public function stops() { - return $this->extractKey('STOPS') ? 'Yes' : 'No'; + $stops = $this->extractKey('STOPS') ? 'Yes' : 'No'; + if(!$stops) throw new Exception ('Invalid SM file. STOPS missing'); + + return $stops; } - public function steps($mode, $difficulty) + public function fgChanges() { - $steps = array(); + $fgChanges = $this->extractKey('FGCHANGES') ? 'Yes' : 'No'; + if(!$fgChanges) throw new Exception ('Invalid SM file. FGCHANGES missing'); + + return $fgChanges; + } + + public function bgChanges() + { + $bgChanges = $this->extractKey('BGCHANGES') ? 'Yes' : 'No'; + if(!$bgChanges) throw new Exception ('Invalid SM file. BGCHANGES missing'); + + return $bgChanges; + } + + public function bpmChanges() + { + $bmpChanges = $this->extractKey('BPMS') ? 'Yes' : 'No'; + if(!$bpmChanges) throw new Exception ('Invalid SM file. BPMS missing'); + + return $bpmChanges; + } + + public function subtitle() + { + $subtitle = $this->extractKey('SUBTITLE'); + if(!$subtitle) throw new Exception ('Invalid SM file. SUBTITLE missing'); - foreach ($this->_smFileLines as $index => $line) { - if (strpos($line, '#NOTES') !== false) { - $mode = substr(trim($lines[$index + 1]), 0, -1) == 'dance-single' ? 'single' : null; - $notes[$mode][] = array( - 'artist' => substr(trim($lines[$index + 2]), 0, -1), - 'difficulty' => substr(trim($lines[$index + 3]), 0, -1), - 'rating' => substr(trim($lines[$index + 4]), 0, -1) - ); + return $subtitle; + } + + function steps() + { + if(empty($this->_smFileLines)) throw new Exception('SM file data not set.'); + $allSteps = array(); + + foreach ($this->_smFileLines as $line) + { + $pos = strpos($line, '#NOTES:'); + if ($pos !== false) + { + $noteData = trim(substr($line, $pos + 9)); + $allSteps = array_merge($allSteps, $this->stepsArrayFromNoteData($noteData)); } } + if(empty($allSteps)) throw new Exception('Invalid Sm file. NOTES missing'); + return $allSteps; + } + + private function stepsArrayFromNoteData($noteData) + { + $stepData = array_map('trim', explode(':', $noteData)); + $steps = array(); + $mode = $stepData[0] == 'dance-single' ? 'single' : null; + $steps[$mode][] = array( + 'artist' => $stepData[1], + 'difficulty' => $stepData[2], + 'rating' => $stepData[3] + ); + return $steps; } - private function extractKey($key) { - foreach ($lines as $line) { + private function extractKey($key) + { + if(empty($this->_smFileLines)) throw new Exception('SM file data not set.'); + + foreach ($this->_smFileLines as $line) + { $pos = strpos($line, '#' . $key . ':'); - if($pos !== false) return substr(trim($line), strlen($key) + 2, -1); + if ($pos !== false) return trim(substr($line, $pos + strlen($key) + 2)); } } - } diff --git a/Services/Uploads/UserSession.php b/Services/Uploads/UserSession.php new file mode 100644 index 0000000..ec941e6 --- /dev/null +++ b/Services/Uploads/UserSession.php @@ -0,0 +1,34 @@ +_httpRequest = $httpRequest; + $this->_userRepository = $userRepository; + } + + public function getCurrentUser() { + if(empty($this->_currentUser)) + { + $request = $this->_httpRequest->isGet() ? $this->_httpRequest->get() + : json_decode($this->_httpRequest->getBody(), true); + + $token = isset($request['token']) ? $request['token'] : null; + $this->_currentUser = $this->_userRepository->findByAuthToken($token); + } + + return $this->_currentUser; + } +} + diff --git a/config/DI.php b/config/DI.php index 86dbb1f..c5afcff 100644 --- a/config/DI.php +++ b/config/DI.php @@ -12,6 +12,9 @@ return [ 'Domain\Entities\IUserStepByStepBuilder' => DI\object('Domain\Entities\UserStepByStepBuilder'), 'Domain\Entities\IUserBuilder' => DI\object('Domain\Entities\UserBuilder'), 'Domain\Entities\IUserFactory' => DI\object('Domain\Entities\UserFactory'), + 'Domain\Entities\StepMania\ISimfileStepByStepBuilder' => DI\object('Domain\Entities\StepMania\SimfileStepByStepBuilder'), + 'Domain\Entities\StepMania\ISimfileBuilder' => DI\object('Domain\Entities\StepMania\SimfileBuilder'), + 'Domain\Entities\StepMania\ISimfileFactory' => DI\object('Domain\Entities\StepMania\SimfileFactory'), //services 'Services\Http\IHttpResponse' => DI\object('Services\Http\HttpResponse'), @@ -22,6 +25,7 @@ return [ 'Services\Uploads\IFileFactory' => DI\object('Services\Uploads\FileFactory'), 'Services\IFacebookSessionFactory' => DI\object('Services\FacebookSessionFactory') ->constructor(DI\link('facebook.app')), + 'Services\ISimfileParser' => DI\object('Services\SimfileParser'), //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), diff --git a/public_html/index.php b/public_html/index.php index 17e529f..7b8f8dd 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -25,4 +25,3 @@ $controller = $container->get('Controllers\\' . ucfirst($controllerName) . 'Cont // Last thing to do, call the action on the specified controller. call_user_func_array(array($controller, $controllerAction . 'Action'), $controllerActionArgs); - diff --git a/public_html/upload.html b/public_html/upload.html index 953aeb4..a551bb2 100644 --- a/public_html/upload.html +++ b/public_html/upload.html @@ -2,6 +2,7 @@
+