Beginnings of SM parser.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 7 Nov 2014 09:38:57 +0000 (17:38 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 7 Nov 2014 09:38:57 +0000 (17:38 +0800)
Services/ISimfileParser.php [new file with mode: 0644]
Services/SimfileParser.php [new file with mode: 0644]

diff --git a/Services/ISimfileParser.php b/Services/ISimfileParser.php
new file mode 100644 (file)
index 0000000..a7e2208
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+namespace Services;
+
+interface ISimfileParser
+{
+    public function title();
+    public function subtitle();
+    public function bpmChanges();
+    public function stops();
+    public function bgChanges();
+    public function fgChanges();
+    public function steps();
+}
\ No newline at end of file
diff --git a/Services/SimfileParser.php b/Services/SimfileParser.php
new file mode 100644 (file)
index 0000000..6b3cef0
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+
+namespace Services;
+
+use Services\ISimfileParser;
+use Exception;
+
+class SimfileParser implements ISimfileParser
+{
+    
+//        'light' => 'Novice',
+//        'beginner' => 'Novice',
+//        'easy' => 'Easy',
+//        'medium' => 'Medium',
+//        'hard' => 'Hard',
+//        'challenge' => 'Expert',
+//        'edit' => 'Edit'
+    
+    private $_smFileLines;
+        
+    public function __construct($simfileData)
+    {
+        $this->_smFileLines = explode("\n", $simfileData);
+    }
+    
+    public function title()
+    {
+        $title = $this->extractKey('TITLE');
+        if(!$title) throw new Exception ('Invalid SM file. Title missing');
+        
+        return $title;
+    }
+    
+    public function stops()
+    {
+        return $this->extractKey('STOPS') ? 'Yes' : 'No';
+    }
+    
+    public function steps($mode, $difficulty)
+    {
+        $steps = array();
+        
+        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 $steps;
+    }
+    
+    private function extractKey($key) {
+        foreach ($lines as $line) {
+            $pos = strpos($line, '#' . $key . ':');
+            if($pos !== false) return substr(trim($line), strlen($key) + 2, -1);
+        }
+    }
+
+}