Implement script to warn about unfinished tasks
[SonOfLokstallBot.git] / unfinished.php
index e637ba9..55cb519 100644 (file)
@@ -1,8 +1,8 @@
-<?php
+<?php declare(strict_types=1);
 
 require_once('common.php');
 
-$dt = new DateTimeImmutable();
+$taskMatrix = require 'taskMatrix.php';
 
 $mondays = [
     (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
@@ -11,25 +11,92 @@ $mondays = [
     (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
 ];
 
-$directory = sprintf(
-    "tasks/%s/%s/%s",
-    $dt->format('Y'),
-    $dt->format('F'),
-    $dt->format('W')
+$currentMonth = (int)(new DateTimeImmutable())->format('n');
+$currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
+$currentSeason = getSeason($currentMonth);
+$currentYear = (int)(new DateTimeImmutable())->format('Y');
+$currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
+
+$extractTasks = function($tasks, $path) {
+    return array_merge($tasks, file_exists($path) ? lines(trim(file_get_contents($path))) : []);
+};
+
+$unfinishedForYear = array_diff(
+     array_merge(...map(
+         function($season) use ($taskMatrix) {
+             return getTasksForTheSeason($season, $taskMatrix);
+         }
+     )(['summer', 'autumn', 'winter', 'spring'])),
+     array_reduce(getFilePathsForYear($currentYear), $extractTasks, [])
+);
+
+$unfinishedForSeason = array_diff(
+    getTasksForTheSeason($currentSeason, $taskMatrix),
+    array_reduce(getFilePathsForSeason($currentYear, $currentSeason), $extractTasks, [])
+);
+
+$unfinishedForMonth = array_diff(
+    getTasksForTheMonth($currentMonth, $taskMatrix),
+    array_reduce(getFilePathsForMonth($currentYear, $currentMonth), $extractTasks, [])
 );
-$completedTasksFile = "$directory" . "/completed.txt";
-$completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
-$closestMonday = closest($dt->format('d'), $mondays);
+$filePathForWeek = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
+$unfinishedForWeek = array_diff(
+    getTasksForTheWeek($currentWeekOfMonth, $currentMonth, $taskMatrix),
+    file_exists($filePathForWeek) ? lines(trim(file_get_contents($filePathForWeek))) : []
+);
 
-$tasksForTheWeek = getTasksForTheWeek(
-    array_search($closestMonday, $mondays),
-    $dt->format('m'),
-    require 'taskMatrix.php'
+//EOY => (EOM & EOW) & !EOS
+//EOS => (EOM & EOW) & !EOY
+$taskLists = array_merge(
+    isEndOfYear($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getStringAndCode)($unfinishedForYear))] : [],
+    isEndOfSeason($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getStringAndCode)($unfinishedForSeason))] : [],
+    isEndOfMonth($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getStringAndCode)($unfinishedForMonth))] : [],
+    [unlines(map(getStringAndCode)($unfinishedForWeek))]
 );
 
-$unfinished = array_diff($tasksForTheWeek, $completedTasks);
+$seasonName = ucfirst($currentSeason);
+$goodOrBad = function($string, $goodOrBad) {
+    return getString($string . ($goodOrBad ? '' : 'Good'));
+};
 
-print_r(
-    array_diff(['treeflowers', 'someothershit'], ['treeflowers'])
+$taskMessages = [
+    [$goodOrBad('endOfWeek', $unfinishedForWeek)],
+    [
+        $goodOrBad('endOfMonth', $unfinishedForMonth),
+        $goodOrBad('andAlsoEndOfWeek', $unfinishedForWeek)
+    ],
+    [
+        $goodOrBad('endOf' . $seasonName, $unfinishedForSeason),
+        $goodOrBad('andAlsoEndOfMonth', $unfinishedForMonth),
+        $goodOrBad('finallyEndOfWeek', $unfinishedForWeek)
+    ],
+    [
+        $goodOrBad('endOfYear', $unfinishedForYear),
+        $goodOrBad('andAlsoEndOfMonth', $unfinishedForMonth),
+        $goodOrBad('finallyEndOfWeek', $unfinishedForWeek)
+    ]
+];
+
+$messages = zipWith(
+    function($message, $list) {
+        return ununlines(
+            array_merge(
+                is_array($message) ? $message : [$message],
+                [$list]
+            )
+        );
+    },
+    // Similar magic to tasks.php
+    $taskMessages[
+        isEndOfMonth($currentYear, $currentMonth, $currentDayOfMonth) +
+        isEndOfSeason($currentYear, $currentMonth, $currentDayOfMonth) +
+        (isEndOfYear($currentYear, $currentMonth, $currentDayOfMonth) ? 2 : 0) // EOY is independant of EOS, to get the right index need to add 2 instead of 1.
+    ],
+    $taskLists
 );
+
+foreach ($messages as $message) {
+    sendToGroupChat($message);
+    sleep(rand(2,4));
+}