The webhook script needs to use the rewind technique
authorCameron Ball <cameron@cameron1729.xyz>
Sat, 5 Jan 2019 19:03:17 +0000 (03:03 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Sat, 5 Jan 2019 19:51:08 +0000 (03:51 +0800)
src/common.php
src/purjolok.php

index bc627f4..e71dc5a 100644 (file)
@@ -395,7 +395,7 @@ function getFilePathsForYear(int $year) {
 
 function closestIndex(int $n, array $list) {
     $a = map(function(int $v) use ($n) : int {
-            return (int)abs($v - $n);
+            return abs($v - $n);
         })
         ($list);
 
index 386596a..ab224a3 100644 (file)
@@ -16,19 +16,23 @@ if(getTelegram()->getWebHookUpdates()->get('message') && !canChatWith(getTelegra
 $message = getTelegram()->getWebHookUpdates()->get('message') ? getTelegram()->getWebHookUpdates()->get('message')->get('text') : '';
 
 if ($between = between(reveal($message ?? ''), '[taskid]')) {
-    $dt = new DateTimeImmutable();
-    $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'),
-    ];
-    $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays) + 1;
-    $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
+    // 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);
+
+    $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
     $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
     $tasksForTheWeek = getTasksForTheWeek(
-        $currentWeekOfMonth,
-        (int)$dt->format('m'),
+        $yearForThisWeek,
+        $monthForThisWeek,
+        $weekNum,
         require 'taskMatrix.php'
     );
 
@@ -231,30 +235,25 @@ getTelegram()->addCommand(
         protected $description = 'List tasks for this week';
 
         public function handle($arguments) {
-            $dt = new DateTimeImmutable();
-            $directory = sprintf(
-                "tasks/%s/%s/%s",
-                $dt->format('Y'),
-                $dt->format('F'),
-                $dt->format('W')
-            );
-
-            $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');
-            $currentWeekOfMonth = closestIndex((new DateTimeImmutable())->format('d'), $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);
+
             $tasksForTheWeek = getTasksForTheWeek(
-                $currentWeekOfMonth,
-                (int)$dt->format('m'),
+                $yearForThisWeek,
+                $monthForThisWeek,
+                $weekNum,
                 require 'taskMatrix.php'
             );
 
-            $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
+            $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
             $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
             if (!array_diff($tasksForTheWeek, $completedTasks)) {
@@ -280,25 +279,29 @@ getTelegram()->addCommand(
         protected $description = 'Mark a task as completed';
 
         public function handle($arguments) {
-            $dt = new DateTimeImmutable();
-            $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'),
-            ];
-            $currentWeekOfMonth = closestIndex($dt->format('d'), $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');
 
-            $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
+            // 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);
 
             $tasksForTheWeek = getTasksForTheWeek(
-                $currentWeekOfMonth,
-                (int)$dt->format('m'),
+                $yearForThisWeek,
+                $monthForThisWeek,
+                $weekNum,
                 require 'taskMatrix.php'
             );
 
+            $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
             $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
+
+
             if (!array_diff($tasksForTheWeek, $completedTasks)) {
                 $this->replyWithMessage([
                     'text' => getString('tasksAllCompleted')