Paginate scores
authorCameron Ball <cameron@moodle.com>
Thu, 9 May 2019 02:39:12 +0000 (10:39 +0800)
committerCameron Ball <cameron@moodle.com>
Thu, 9 May 2019 02:39:12 +0000 (10:39 +0800)
src/gnb.php

index e585c2b..c139a87 100644 (file)
@@ -205,6 +205,11 @@ getTelegram()->addCommand(
                 'Edit' => 'šŸ““'
             ];
 
+            $shit = explode(' ', $arguments);
+
+            $hsname = $shit[0];
+            $page = $shit[1] ?? 1;
+
             $songHashMap = unserialize(file_get_contents(__DIR__ . '/hashmap.txt'));
 
             $scoreFiles = foldMap(āˆ˜(ā˜(lhead)(null), 'file_get_contents'))(glob(PATH_TO_GROOVENET . '/*.scores.txt'));
@@ -213,10 +218,16 @@ getTelegram()->addCommand(
             );
 
             $asdf = prefixKey('', $scoreTesseract);
+            uksort($asdf, function($a, $b) use ($songHashMap) {
+                $a = explode('/', $songHashMap[explode('.', $a)[0]])[1];
+                $b = explode('/', $songHashMap[explode('.', $b)[0]])[1];
+
+                return strcmp($a, $b);
+            });
             $pranks = [];
             foreach ($asdf as $k => $v) {
                 $k = explode('.', $k);
-                if ($k[3] !== $arguments) {
+                if ($k[3] !== $hsname) {
                     continue;
                 }
 
@@ -233,20 +244,30 @@ getTelegram()->addCommand(
             if (!$pranks) {
                 $this->replyWithMessage(
                     [
-                        'text' => "$arguments doesn't have any scores."
+                        'text' => "$hsname doesn't have any scores."
                     ]
                 );
                 return;
             }
 
+            $chunky = array_chunk($pranks, 10);
+            if (!isset($chunky[$page-1])) {
+                $this->replyWithMessage(['text' => 'Invalid page number: ' . $page . "\nQuery without a page number to see the valid range."]);
+                return;
+            }
+
+            $pranks = $chunky[$page - 1];
             $numE = count($pranks);
-            $pranks[$numE- 1] .= "\nā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”";
+            $pranks[$numE-1] .= "\nā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”";
+
+            $extra = count($chunky) > 1 ? " (page $page of " . count($chunky) . ")" : '';
+
             $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id');
             getTelegram()->sendMessage(
                 [
                     'chat_id' => $chatid,
                     'parse_mode' => 'Markdown',
-                    'text' => "High scores for $arguments ...\n\n```" . unlines($pranks) . "```"
+                    'text' => "High scores for $hsname"."$extra ...\n\n```\n" . unlines($pranks) . "```"
                 ]
             );
         }