Improved logic in tasks and unfinished scripts plus some bug fixes
[SonOfLokstallBot.git] / src / tasks.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 $taskLists = array_merge(
20 isStartOfSeason($monthForThisWeek, $dayNum) ? [unlines(map(getString)(getTasksForTheSeason($seasonForThisWeek, $taskMatrix)))] : [],
21 isStartOfMonth($dayNum) ? [unlines(map(getString)(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))] : [],
22 [unlines(map(getString)(getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix)))]
23 );
24
25 $taskMessages = [
26 [getString('happyMonday')],
27 [
28 [getString('beginningOf'. ucfirst(getMonthName($monthForThisWeek))), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
29 getString('anywayHeresTheWeek')
30 ],
31 [
32 getString('beginningOf' . ucfirst(getSeason($monthForThisWeek))),
33 [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
34 getString('finallyHeresTheWeek')
35 ]
36 ];
37
38 $messages = zipWith(
39 function($message, $list) {
40 return ununlines(
41 array_merge(
42 is_array($message) ? $message : [$message],
43 [$list]
44 )
45 );
46 },
47 // Magic. startOfSeason implies startofMonth so we get 2, start of month without start of season gives 1 and
48 // a regular week (not the start of a season or month) gives 0. And this is how the indicies are ordered in the array.
49 $taskMessages[(int)isStartOfSeason($monthForThisWeek, $dayNum) + (int)isStartOfMonth($dayNum)],
50 $taskLists
51 );
52
53 foreach ($messages as $message) {
54 sendToGroupChat($message);
55 sleep(rand(2,4));
56 }