Improved logic in tasks and unfinished scripts plus some bug fixes
[SonOfLokstallBot.git] / src / tasks.php
index eb37cc7..38ecb83 100644 (file)
@@ -3,31 +3,34 @@
 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('m');
-$currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
-$currentWeekOfMonth = closestIndex($currentDayOfMonth, $mondays) + 1;
+
+// The actual date/time when the script is called
+$currentYear = (int)(new DateTimeImmutable())->format('Y');
+$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);
 
 $taskLists = array_merge(
-    isStartOfSeason($currentMonth, $currentDayOfMonth) ? [unlines(map(getString)(getTasksForTheSeason(getSeason($currentMonth), $taskMatrix)))] : [],
-    isStartOfMonth($currentDayOfMonth) ? [unlines(map(getString)(getTasksForTheMonth($currentMonth, $taskMatrix)))] : [],
-    [unlines(map(getString)(getTasksForTheWeek($currentWeekOfMonth, $currentMonth, $taskMatrix)))]
+    isStartOfSeason($monthForThisWeek, $dayNum) ? [unlines(map(getString)(getTasksForTheSeason($seasonForThisWeek, $taskMatrix)))] : [],
+    isStartOfMonth($dayNum) ? [unlines(map(getString)(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))] : [],
+    [unlines(map(getString)(getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix)))]
 );
 
 $taskMessages = [
     [getString('happyMonday')],
     [
-        [getString('beginningOf'. ucfirst(getMonthName($currentMonth))), getString('thisMonthThereAre', count(getTasksForTheMonth($currentMonth, $taskMatrix)))],
+        [getString('beginningOf'. ucfirst(getMonthName($monthForThisWeek))), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
          getString('anywayHeresTheWeek')
     ],
     [
-        getString('beginningOf' . ucfirst(getSeason($currentMonth))),
-        [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($currentMonth, $taskMatrix)))],
+        getString('beginningOf' . ucfirst(getSeason($monthForThisWeek))),
+        [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
         getString('finallyHeresTheWeek')
     ]
 ];
@@ -43,7 +46,7 @@ $messages = zipWith(
     },
     // Magic. startOfSeason implies startofMonth so we get 2, start of month without start of season gives 1 and
     // a regular week (not the start of a season or month) gives 0. And this is how the indicies are ordered in the array.
-    $taskMessages[(int)isStartOfSeason($currentMonth, $currentDayOfMonth) + (int)isStartOfMonth($currentDayOfMonth)],
+    $taskMessages[(int)isStartOfSeason($monthForThisWeek, $dayNum) + (int)isStartOfMonth($dayNum)],
     $taskLists
 );