Add command to get the prank digest earlier
authorCameron Ball <cameron@moodle.com>
Wed, 8 May 2019 03:39:09 +0000 (11:39 +0800)
committerCameron Ball <cameron@moodle.com>
Wed, 8 May 2019 03:39:09 +0000 (11:39 +0800)
src/common.php
src/gnb.php
src/prankdigest.php

index f0cf7c8..9abcc2b 100644 (file)
@@ -17,6 +17,13 @@ function identity($x) {
     return $x;
 }
 
+const scoresFileContentsToScoresList = 'scoresFileContentsToScoresList';
+function scoresFileContentsToScoresList($who) {
+    return function($lines) use ($who) {
+        return filter(notEmpty)(foldMap(āˆ˜(map(filterk(ā‰ ('NODATA'))), unflatten, insert($who)(3), delemit(':')))(lines($lines)));
+    };
+}
+
 const notEmpty = 'notEmpty';
 function notEmpty($value) : bool {
     return !empty($value);
index 01d4c6a..3b2bc74 100644 (file)
@@ -13,12 +13,92 @@ if(getTelegram()->getWebHookUpdates()->get('message') && !canChatWith(getTelegra
     exit(0);
 }
 
-const scoresFileContentsToScoresList = 'scoresFileContentsToScoresList';
-function scoresFileContentsToScoresList($who) {
-    return function($lines) use ($who) {
-        return filter(notEmpty)(foldMap(āˆ˜(map(filterk(ā‰ ('NODATA'))), unflatten, insert($who)(3), delemit(':')))(lines($lines)));
-    };
-}
+getTelegram()->addCommand(
+    new class extends Command {
+        protected $name = 'digest';
+        protected $description = 'Get the prank digest for today';
+
+        public function handle($arguments) {
+            $diffEmojiMap = [
+                'Beginner' => 'šŸ—‘',
+                'Easy' => 'šŸ“—',
+                'Medium' => 'šŸ“™',
+                'Hard' => 'šŸ“•',
+                'Challenge' => 'šŸ“˜',
+                'Edit' => 'šŸ““'
+            ];
+
+            $songHashMap = unserialize(file_get_contents(__DIR__ . '/hashmap.txt'));
+            $scoreFiles = foldMap(āˆ˜(ā˜(lhead)(null), 'file_get_contents'))(glob(PATH_TO_GROOVENET . '/*.scores.txt'));
+            $faveFiles = foldMap(ā˜(null)(āˆ˜(lines, 'trim', 'file_get_contents')))(glob(PATH_TO_GROOVENET . '/*.favourites.txt'));
+            $names = map(ā˜(produceValue(0))(null))(array_keys($faveFiles));
+            $hashes = map(map(ā˜(produceValue(0))(null)))(array_values($faveFiles));
+
+            $oldFaveMap = unserialize(file_get_contents(__DIR__ . '/faves.txt'));
+            $faveMap = āˆ˜(map(āˆ˜(flip(concat)("'s Faves"), 'ucwords', field(0), delemit('.'), 'basename')), foldMap(ā˜(field(1))(field(0))), foldMap(id))(zzipWith(āˆ˜(map, aaray_merge))($names)($hashes));
+            $oldScoreTesseract = unserialize(file_get_contents(__DIR__ . '/tesseract.txt'));
+            $scoreTesseract = map(map(map(maxKey)))(
+                āˆ˜(foldMap(id), zzipWith(pa)(map(ltail)($scoreFiles)), map(āˆ˜(scoresFileContentsToScoresList, lhead)))($scoreFiles)
+            );
+
+            $diff = array_diff_assoc_recursive($scoreTesseract, $oldScoreTesseract);
+            $asdf = prefixKey('', $diff);
+            $pranks = [];
+
+            foreach ($asdf as $k => $v) {
+                $k = explode('.', $k);
+
+                if (!isset($oldScoreTesseract[$k[0]][$k[1]][$k[2]])) {
+                    continue;
+                }
+
+                $previous = $oldScoreTesseract[$k[0]][$k[1]][$k[2]];
+                $scorer = array_keys($previous)[0];
+                $score = $previous[$scorer];
+
+                if ($scorer !== $k[3]) {
+                    $pranks[] = glue('')([
+                        "ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”\n",
+                        $diffEmojiMap[$k[1]],
+                        $k[2] == 'dance-double' ? $diffEmojiMap[$k[1]] : '',
+                        ' ' . explode('/', $songHashMap[$k[0]])[1],
+                        "\n",
+                        $k[3] . ": " . truncate($v*100, 2) . "%",
+                        " (was $scorer: " . truncate($score*100, 2) . "%)"
+                    ]);
+                }
+            }
+
+            if ($pranks) {
+                $numE = count($pranks);
+                $pranks[$numE- 1] .= "\nā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”";
+            }
+
+            $faveDiff = array_diff_assoc($faveMap, $oldFaveMap);
+            $newFaves = zzipWith(āˆ˜(concat, flip(concat)(" in "), field(1), delemit('/'), āˆ˜(flip(field))($songHashMap)))(array_keys($faveDiff))(array_values($faveDiff));
+
+            if ($pranks || $newFaves) {
+                getTelegram()->sendMessage(
+                    [
+                        'chat_id' => CHAT_ID,
+                        'parse_mode' => 'Markdown',
+                        'text' => unlines(
+                            array_merge(
+                                ['Welcome to the GrooveNet digest', ""],
+                                $pranks ? ["Latest pranks:", '```'] : [],
+                                $pranks,
+                                $pranks ? ['```'] : [],
+                                $newFaves ? ['Latest faves:', '```'] : [],
+                                $newFaves,
+                                $newFaves ? ['```'] : []
+                            )
+                        )
+                    ]
+                );
+            }
+        }
+    }
+);
 
 getTelegram()->addCommand(
     new class extends Command {
index 96867f6..d859b69 100644 (file)
@@ -2,13 +2,6 @@
 
 require_once('common.php');
 
-const scoresFileContentsToScoresList = 'scoresFileContentsToScoresList';
-function scoresFileContentsToScoresList($who) {
-    return function($lines) use ($who) {
-        return filter(notEmpty)(foldMap(āˆ˜(map(filterk(ā‰ ('NODATA'))), unflatten, insert($who)(3), delemit(':')))(lines($lines)));
-    };
-}
-
 $diffEmojiMap = [
     'Beginner' => 'šŸ—‘',
     'Easy' => 'šŸ“—',
@@ -65,7 +58,7 @@ if ($pranks) {
 }
 
 $faveDiff = array_diff_assoc($faveMap, $oldFaveMap);
-$newFaves = zzipWith(āˆ˜(concat, flip(concat)(" in "), field(1),delemit('/'), āˆ˜(flip(field))($songHashMap)))(array_keys($faveDiff))(array_values($faveDiff));
+$newFaves = zzipWith(āˆ˜(concat, flip(concat)(" in "), field(1), delemit('/'), āˆ˜(flip(field))($songHashMap)))(array_keys($faveDiff))(array_values($faveDiff));
 
 if ($pranks || $newFaves) {
     getTelegram()->sendMessage(