From a090b8f2aab0ca75612d0c0254f06e656cfbae43 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Tue, 9 Jan 2024 22:07:36 +0100 Subject: [PATCH] Implemented sleep streaks --- src/activity.php | 4 ++++ src/config.php | 1 + src/index.php | 14 +++++++++++++- src/storage.php | 2 +- src/streaks.php | 22 ++++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/streaks.php diff --git a/src/activity.php b/src/activity.php index b8880e4..d40bbbb 100644 --- a/src/activity.php +++ b/src/activity.php @@ -13,6 +13,8 @@ class Activity public string $template; // ID of the template that is supposed to be applied to this activity + public array $streak; // Last success dates of the associated streak + public function __construct() { $this->startTime = time(); @@ -22,6 +24,7 @@ class Activity $this->available = false; $this->working = false; $this->template = ""; + $this->streak = array(); } public function initialLoad(): void @@ -49,6 +52,7 @@ class Activity "working" => $this->working, "currentDuration" => $this->getCurrentDuration(), "remainingDuration" => $this->getRemainingDuration(), + "streak" => $this->streak, ); } } \ No newline at end of file diff --git a/src/config.php b/src/config.php index ca508d0..d0b5406 100644 --- a/src/config.php +++ b/src/config.php @@ -3,3 +3,4 @@ $SECRET_KEY = "ENTER_SECRET_HERE"; $ACTIVITY_FILE = "last_activity.json"; $TEMPLATES_FILE = "templates.json"; +$STREAKS_FILE_SUFFIX = "_streak.csv"; \ No newline at end of file diff --git a/src/index.php b/src/index.php index 42d9aa5..95977d5 100755 --- a/src/index.php +++ b/src/index.php @@ -7,6 +7,7 @@ require_once("config.php"); require_once("storage.php"); require_once("activity.php"); require_once("http.php"); +require_once("streaks.php"); function loadGivenParameters(Activity $activity): void { @@ -64,6 +65,9 @@ if (!isset($_GET["secret"]) || $_GET["secret"] != $SECRET_KEY) { respondAndDie($e->getMessage()); } handleTemplate($activity); + if ($activity->title == "Sleeping") { + $activity->streak = getStreakEntries("sleep"); + } respondWithJson($activity->getPublicData()); } @@ -71,4 +75,12 @@ if (!isset($_GET["secret"]) || $_GET["secret"] != $SECRET_KEY) { $activity = new Activity(); loadGivenParameters($activity); storeActivity($activity); -respondAndDie("Activity updated"); + +// Handle sleep streak +$startingHour = (int) date("H"); +$startingMinute = (int) date("i"); +if ($activity->title == "Sleeping" && $startingHour > 15 && ($startingHour < 22 || ($startingHour == 22 && $startingMinute <= 5))) { + markTodayAsSuccess("sleep"); +} + +respondAndDie("Activity updated"); \ No newline at end of file diff --git a/src/storage.php b/src/storage.php index f82bb85..0f5eb94 100644 --- a/src/storage.php +++ b/src/storage.php @@ -73,4 +73,4 @@ function parseObject(array $data, string $class): object } return $instance; -} +} \ No newline at end of file diff --git a/src/streaks.php b/src/streaks.php new file mode 100644 index 0000000..76eb54e --- /dev/null +++ b/src/streaks.php @@ -0,0 +1,22 @@ +