Improved logic in tasks and unfinished scripts plus some bug fixes
[SonOfLokstallBot.git] / src / unfinished.php
1 <?php declare(strict_types=1);
2
3 require_once('common.php');
4
5 $taskMatrix = require 'taskMatrix.php';
6
7 // The actual date/time when the script is called
8 $currentYear = (int)(new DateTimeImmutable())->format('Y');
9 $currentMonth = (int)(new DateTimeImmutable())->format('n');
10 $currentDay = (int)(new DateTimeImmutable())->format('d');
11
12 // The years/months/weeks that we are "rewinding" to, for calculations
13 $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
14 $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
15 $seasonForThisWeek = getSeason($monthForThisWeek);
16 $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
17 $dayNum = getDayNumber($currentYear, $currentMonth, $currentDay);
18
19 $extractTasks = function($tasks, $path) {
20 return array_merge($tasks, file_exists($path) ? lines(trim(file_get_contents($path))) : []);
21 };
22
23 $unfinishedForYear = array_diff(
24 array_merge(...map(
25 function($season) use ($taskMatrix) {
26 return getTasksForTheSeason($season, $taskMatrix);
27 }
28 )(['summer', 'autumn', 'winter', 'spring'])),
29 array_reduce(getFilePathsForYear($yearForThisWeek), $extractTasks, [])
30 );
31
32 $unfinishedForSeason = array_diff(
33 getTasksForTheSeason($seasonForThisWeek, $taskMatrix),
34 array_reduce(getFilePathsForSeason($yearForThisWeek, $seasonForThisWeek), $extractTasks, [])
35 );
36
37 $unfinishedForMonth = array_diff(
38 getTasksForTheMonth($monthForThisWeek, $taskMatrix),
39 array_reduce(getFilePathsForMonth($yearForThisWeek, $monthForThisWeek), $extractTasks, [])
40 );
41
42 $filePathForWeek = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
43 $unfinishedForWeek = array_diff(
44 getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix),
45 file_exists($filePathForWeek) ? lines(trim(file_get_contents($filePathForWeek))) : []
46 );
47
48 //EOY => (EOM & EOW) & !EOSx
49 //EOS => (EOM & EOW) & !EOY
50 $taskLists = array_merge(
51 isEndOfYear($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForYear))] : [],
52 isEndOfSeason($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForSeason))] : [],
53 isEndOfMonth($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForMonth))] : [],
54 [unlines(map(getString)($unfinishedForWeek))]
55 );
56
57 $seasonName = ucfirst($seasonForThisWeek);
58 $goodOrBad = function($string, $goodOrBad) {
59 return getString($string . ($goodOrBad ? '' : 'Good'));
60 };
61
62 $taskMessages = [
63 [$goodOrBad('endOfWeek', $unfinishedForWeek)],
64 [
65 $goodOrBad('endOfMonth', $unfinishedForMonth),
66 $goodOrBad('andAlsoEndOfWeek', $unfinishedForWeek)
67 ],
68 [
69 $goodOrBad('endOf' . $seasonName, $unfinishedForSeason),
70 $goodOrBad('andAlsoEndOfMonth', $unfinishedForMonth),
71 $goodOrBad('finallyEndOfWeek', $unfinishedForWeek)
72 ],
73 [
74 $goodOrBad('endOfYear', $unfinishedForYear),
75 $goodOrBad('andAlsoEndOfMonth', $unfinishedForMonth),
76 $goodOrBad('finallyEndOfWeek', $unfinishedForWeek)
77 ]
78 ];
79
80 $messages = zipWith(
81 function($message, $list) {
82 return ununlines(
83 array_merge(
84 is_array($message) ? $message : [$message],
85 [$list]
86 )
87 );
88 },
89 // Similar magic to tasks.php
90 $taskMessages[
91 isEndOfMonth($yearForThisWeek, $monthForThisWeek, $dayNum) +
92 isEndOfSeason($yearForThisWeek, $monthForThisWeek, $dayNum) +
93 (isEndOfYear($yearForThisWeek, $monthForThisWeek, $dayNum) ? 2 : 0) // EOY is independant of EOS, to get the right index need to add 2 instead of 1.
94 ],
95 $taskLists
96 );
97
98 foreach ($messages as $message) {
99 sendToGroupChat($message);
100 sleep(rand(2,4));
101 }