This commit is contained in:
2024-12-31 23:41:09 +02:00
parent 94f2c28237
commit 7332c751ca
2 changed files with 91 additions and 12 deletions

View File

@@ -0,0 +1,77 @@
import fs from "fs";
import path from "path";
import { exec } from "child_process";
const stateFilePath = path.join(
process.env.HOME || "~",
".regretevator-challenge-state"
);
const regretevatorStateFile = path.join(
process.env.HOME || "~",
".regretevator_state"
);
if (fs.existsSync(regretevatorStateFile)) process.exit(0);
function sendReminderNotification() {
exec(
`notify-send -a "tuxstrap" -u low "OCbwoy3's Dotfiles" "It's time for Regretevator!"`
);
}
function sendReminderNotificationPlayed(floors: number) {
exec(
`notify-send -a "tuxstrap" -u low "OCbwoy3's Dotfiles" "Man... Only ${floors} floors, really? You can do better than that."`
);
}
function sendSpamNotifications() {
for (let i = 0; i < 2; i++) {
exec(
`notify-send -a "tuxstrap" -u low "OCbwoy3's Dotfiles" "HURRY UP!!!!!"`
);
}
}
function writeState(floorsSurvived: number) {
const state = {
date: new Date().toISOString().split("T")[0],
floors: floorsSurvived,
};
fs.writeFileSync(stateFilePath, JSON.stringify(state, null, 2));
}
function checkChallengeState() {
if (fs.existsSync(stateFilePath)) {
const state = JSON.parse(fs.readFileSync(stateFilePath, "utf-8"));
const today = new Date().toISOString().split("T")[0];
const stateDate = state.date;
const floorsSurvived = state.floors;
const now = new Date();
const hoursLeft = 24 - now.getHours();
if (stateDate === today) {
if (floorsSurvived < 25) {
if (floorsSurvived === 0) {
sendReminderNotification();
} else {
sendReminderNotificationPlayed(floorsSurvived);
}
if (hoursLeft <= 4) {
sendSpamNotifications();
}
}
} else {
sendReminderNotification();
writeState(0); // Write state with 0 floors if the user hasn't played today
}
} else {
sendReminderNotification();
writeState(0); // Write state with 0 floors if no state file exists
}
}
// Run the check
checkChallengeState();

View File

@@ -2,22 +2,24 @@
pkgs.stdenv.mkDerivation {
name = "tuxstrap-cron-executable";
src = pkgs.fetchFromGitHub {
owner = "ocbwoy3";
repo = "tuxstrap";
rev = "v1.2.0";
sha256 = "sha256-YZ+GD9hs8PAf45kFklrR3j1hUWovKWllr2iQuh3gRms="; # Replace with the actual sha256
};
src = ./files/crontab-executable.ts;
buildInputs = [ pkgs.bun ];
buildPhase = ''
bun build ./daily-challenge-reminder-crontab.ts --compile --outfile=tuxstrap-crontab
unpackPhase = ''
mkdir -p $out
cp $src tuxstrap-crontab.bin
'';
installPhase = ''
mkdir -p $out/bin
# cp daily-challenge-reminder-crontab.ts $out/bin/crontab-script.ts
cp tuxstrap-crontab $out/bin/
mkdir -p $out/lib
echo '${"#"}!/bin/bash' > $out/bin/tuxstrap-crontab
echo '${pkgs.bun}/bin/bun run $out/lib/crontab-executable.ts' >> $out/bin/tuxstrap-crontab
chmod +x $out/bin/tuxstrap-crontab
mv crontab-executable $out/lib/
'';
meta = with nixpkgs.lib; {
description = "tuxstrap crontab executable";
};
}