Implemented sleep streaks

This commit is contained in:
Maximilian Giller 2024-01-09 22:07:36 +01:00
parent a8273e2633
commit a090b8f2aa
5 changed files with 41 additions and 2 deletions

View file

@ -13,6 +13,8 @@ class Activity
public string $template; // ID of the template that is supposed to be applied to this 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() public function __construct()
{ {
$this->startTime = time(); $this->startTime = time();
@ -22,6 +24,7 @@ class Activity
$this->available = false; $this->available = false;
$this->working = false; $this->working = false;
$this->template = ""; $this->template = "";
$this->streak = array();
} }
public function initialLoad(): void public function initialLoad(): void
@ -49,6 +52,7 @@ class Activity
"working" => $this->working, "working" => $this->working,
"currentDuration" => $this->getCurrentDuration(), "currentDuration" => $this->getCurrentDuration(),
"remainingDuration" => $this->getRemainingDuration(), "remainingDuration" => $this->getRemainingDuration(),
"streak" => $this->streak,
); );
} }
} }

View file

@ -3,3 +3,4 @@
$SECRET_KEY = "ENTER_SECRET_HERE"; $SECRET_KEY = "ENTER_SECRET_HERE";
$ACTIVITY_FILE = "last_activity.json"; $ACTIVITY_FILE = "last_activity.json";
$TEMPLATES_FILE = "templates.json"; $TEMPLATES_FILE = "templates.json";
$STREAKS_FILE_SUFFIX = "_streak.csv";

View file

@ -7,6 +7,7 @@ require_once("config.php");
require_once("storage.php"); require_once("storage.php");
require_once("activity.php"); require_once("activity.php");
require_once("http.php"); require_once("http.php");
require_once("streaks.php");
function loadGivenParameters(Activity $activity): void function loadGivenParameters(Activity $activity): void
{ {
@ -64,6 +65,9 @@ if (!isset($_GET["secret"]) || $_GET["secret"] != $SECRET_KEY) {
respondAndDie($e->getMessage()); respondAndDie($e->getMessage());
} }
handleTemplate($activity); handleTemplate($activity);
if ($activity->title == "Sleeping") {
$activity->streak = getStreakEntries("sleep");
}
respondWithJson($activity->getPublicData()); respondWithJson($activity->getPublicData());
} }
@ -71,4 +75,12 @@ if (!isset($_GET["secret"]) || $_GET["secret"] != $SECRET_KEY) {
$activity = new Activity(); $activity = new Activity();
loadGivenParameters($activity); loadGivenParameters($activity);
storeActivity($activity); storeActivity($activity);
// 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"); respondAndDie("Activity updated");

22
src/streaks.php Normal file
View file

@ -0,0 +1,22 @@
<?php
require_once("config.php");
function markTodayAsSuccess(string $streakId)
{
global $STREAKS_FILE_SUFFIX;
$date = date("Y-m-d");
$entries = getStreakEntries($streakId);
if (in_array($date, $entries)) {
return;
}
file_put_contents($streakId . $STREAKS_FILE_SUFFIX, "$date\n", FILE_APPEND);
}
function getStreakEntries(string $streakId)
{
global $STREAKS_FILE_SUFFIX;
return explode("\n", trim(file_get_contents($streakId . $STREAKS_FILE_SUFFIX)));
}