From 5698a27fb50a566dd3c0adefcac18ab0d599ed70 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Sun, 6 Jan 2019 03:03:17 +0800 Subject: [PATCH] The webhook script needs to use the rewind technique --- src/common.php | 2 +- src/purjolok.php | 85 +++++++++++++++++++++++++++++--------------------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/common.php b/src/common.php index bc627f4..e71dc5a 100644 --- a/src/common.php +++ b/src/common.php @@ -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); diff --git a/src/purjolok.php b/src/purjolok.php index 386596a..ab224a3 100644 --- a/src/purjolok.php +++ b/src/purjolok.php @@ -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') -- 2.11.0