Implemented sleep streaks
This commit is contained in:
parent
a8273e2633
commit
a090b8f2aa
5 changed files with 41 additions and 2 deletions
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
|
@ -3,3 +3,4 @@
|
|||
$SECRET_KEY = "ENTER_SECRET_HERE";
|
||||
$ACTIVITY_FILE = "last_activity.json";
|
||||
$TEMPLATES_FILE = "templates.json";
|
||||
$STREAKS_FILE_SUFFIX = "_streak.csv";
|
|
@ -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");
|
|
@ -73,4 +73,4 @@ function parseObject(array $data, string $class): object
|
|||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
22
src/streaks.php
Normal file
22
src/streaks.php
Normal 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)));
|
||||
}
|
Loading…
Reference in a new issue