Improved logic in tasks and unfinished scripts plus some bug fixes
[SonOfLokstallBot.git] / src / unfinished.php
index 8c7369d..b106c76 100644 (file)
@@ -4,18 +4,17 @@ require_once('common.php');
 
 $taskMatrix = require 'taskMatrix.php';
 
-$mondays = [
-    (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
-    (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
-    (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
-    (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
-];
-
-$currentMonth = (int)(new DateTimeImmutable())->format('n');
-$currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
-$currentSeason = getSeason($currentMonth);
+// The actual date/time when the script is called
 $currentYear = (int)(new DateTimeImmutable())->format('Y');
-$currentWeekOfMonth = closestIndex($currentDayOfMonth, $mondays) + 1;
+$currentMonth = (int)(new DateTimeImmutable())->format('n');
+$currentDay = (int)(new DateTimeImmutable())->format('d');
+
+// The years/months/weeks that we are "rewinding" to, for calculations
+$yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
+$monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
+$seasonForThisWeek = getSeason($monthForThisWeek);
+$weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
+$dayNum = getDayNumber($currentYear, $currentMonth, $currentDay);
 
 $extractTasks = function($tasks, $path) {
     return array_merge($tasks, file_exists($path) ? lines(trim(file_get_contents($path))) : []);
@@ -27,35 +26,35 @@ $unfinishedForYear = array_diff(
              return getTasksForTheSeason($season, $taskMatrix);
          }
      )(['summer', 'autumn', 'winter', 'spring'])),
-     array_reduce(getFilePathsForYear($currentYear), $extractTasks, [])
+     array_reduce(getFilePathsForYear($yearForThisWeek), $extractTasks, [])
 );
 
 $unfinishedForSeason = array_diff(
-    getTasksForTheSeason($currentSeason, $taskMatrix),
-    array_reduce(getFilePathsForSeason($currentYear, $currentSeason), $extractTasks, [])
+    getTasksForTheSeason($seasonForThisWeek, $taskMatrix),
+    array_reduce(getFilePathsForSeason($yearForThisWeek, $seasonForThisWeek), $extractTasks, [])
 );
 
 $unfinishedForMonth = array_diff(
-    getTasksForTheMonth($currentMonth, $taskMatrix),
-    array_reduce(getFilePathsForMonth($currentYear, $currentMonth), $extractTasks, [])
+    getTasksForTheMonth($monthForThisWeek, $taskMatrix),
+    array_reduce(getFilePathsForMonth($yearForThisWeek, $monthForThisWeek), $extractTasks, [])
 );
 
-$filePathForWeek = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
+$filePathForWeek = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
 $unfinishedForWeek = array_diff(
-    getTasksForTheWeek($currentWeekOfMonth, $currentMonth, $taskMatrix),
+    getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix),
     file_exists($filePathForWeek) ? lines(trim(file_get_contents($filePathForWeek))) : []
 );
 
 //EOY => (EOM & EOW) & !EOSx
 //EOS => (EOM & EOW) & !EOY
 $taskLists = array_merge(
-    isEndOfYear($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getString)($unfinishedForYear))] : [],
-    isEndOfSeason($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getString)($unfinishedForSeason))] : [],
-    isEndOfMonth($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getString)($unfinishedForMonth))] : [],
+    isEndOfYear($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForYear))] : [],
+    isEndOfSeason($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForSeason))] : [],
+    isEndOfMonth($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForMonth))] : [],
     [unlines(map(getString)($unfinishedForWeek))]
 );
 
-$seasonName = ucfirst($currentSeason);
+$seasonName = ucfirst($seasonForThisWeek);
 $goodOrBad = function($string, $goodOrBad) {
     return getString($string . ($goodOrBad ? '' : 'Good'));
 };
@@ -89,9 +88,9 @@ $messages = zipWith(
     },
     // 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.
+        isEndOfMonth($yearForThisWeek, $monthForThisWeek, $dayNum) +
+        isEndOfSeason($yearForThisWeek, $monthForThisWeek, $dayNum) +
+        (isEndOfYear($yearForThisWeek, $monthForThisWeek, $dayNum) ? 2 : 0) // EOY is independant of EOS, to get the right index need to add 2 instead of 1.
     ],
     $taskLists
 );