Created
March 24, 2013 01:22
-
-
Save salamander2/5230027 to your computer and use it in GitHub Desktop.
Bash script to control execution of a game (time played, etc). It only allows the game to be played between certain hours and also tracks the total time in a day that has been played, killing the game if either condition is surpassed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#set up constants | |
TIME1=12 #start hour (24 hour clock) | |
TIME2=20 #end hour (24 hr clock) | |
TIMELIMIT=60 #mintues | |
PGMNAME="mahjongg" #exact name used for killall and for TIMEFILE | |
PGMPATH="/usr/games/$PGMNAME" | |
TIMEFILE="$HOME/.local/$PGMNAME.elapsed" | |
STARTTIME=`date +%s` #seconds since 0000 UTC | |
#functions (Bash functions are really limited -- should have used PERL) | |
## This function calculates the time spent playing the game | |
## and updates the ELAPSED variable and the TIMEFILE contents. | |
## Converting seconds to minutes truncates instead of rounding so add 29 seconds | |
#XX Problem - this increases the number of minutes faster that it should be. | |
#XX Fixed. This should really be changed to track seconds played, not TIMELIMIT in minutes. | |
function updateTime () { | |
ENDTIME=`date +%s` | |
local minutes #local variables must be declared before initialization | |
#minutes=$((($ENDTIME - $STARTTIME + 29)/60)) | |
#minutes=$((($ENDTIME - $STARTTIME)/60)) | |
#ELAPSED=$(($ELAPSED + $minutes)) | |
ELAPSED=$(($ELAPSED + 1)) | |
echo -e "minutes=$minutes\telapsed=$ELAPSED" >> $HOME/mj.log | |
echo $ELAPSED > $TIMEFILE | |
} | |
if [ ! -f $TIMEFILE ]; then | |
echo 0 > $TIMEFILE | |
fi | |
ELAPSED=`cat $TIMEFILE` | |
#check if time is within permitted hour range | |
HOUR=$(date +%H) | |
if [ "$HOUR" -lt $TIME1 -o "$HOUR" -ge $TIME2 ]; then | |
rm $TIMEFILE | |
zenity --error --text "$PGMNAME is disabled. You can only play between $TIME1:00 and $TIME2:00" | |
exit 0 | |
fi | |
# check timespent | |
if [ $ELAPSED -ge $TIMELIMIT ]; then | |
zenity --error --text "$PGMNAME is disabled. You have reached the limit of $TIMELIMIT minutes per day" | |
exit 0 | |
fi | |
#run game program. Mahjongg, at least, will come into the foreground automatically (its a GUI) | |
zenity --info --text "You have played $ELAPSED minutes today (of $TIMELIMIT). Press OK to continue" --timeout 15 | |
`$PGMPATH` & | |
#main loop to track time | |
playon=true | |
while $playon ; do | |
#sleeping for a whole number of minutes makes it less likely to induce rounding errors | |
#when I try to convert & round from seconds to minutes. | |
sleep 60 | |
#check if Mahjongg is still being played | |
#RESULT=`ps -ef | grep $PGMPATH | grep -v grep` | |
#if [ -z $RESULT ]; then #this has problems because of multiple output lines | |
#or possibly strings with spaces in them | |
RESULT=`ps -ef | grep $PGMPATH | grep -v grep | wc -w` | |
if [ $RESULT -eq 0 ]; then | |
updateTime | |
zenity --info --text "Thank you for playing $PGMNAME. You have played $ELAPSED minutes so far today." --timeout 10 | |
exit 0 | |
fi | |
updateTime | |
#check for total time played so far | |
if [ $ELAPSED -ge $TIMELIMIT ]; then | |
$(sleep 1 && wmctrl -a Warning -b add,above)& | |
$(zenity --warning --text "You have reached the limit of $TIMELIMIT minutes per day. Your game will end in 2 minutes" --timeout 30) | |
playon=false | |
fi | |
#check for playing past the deadline | |
HOUR=$(date +%H) | |
if [ "$HOUR" -ge $TIME2 ]; then | |
rm $TIMEFILE | |
$(sleep 1 && wmctrl -a Warning -b add,above)& | |
$(zenity --warning --text "You have reached the ending time of $TIME2:00 . You played $ELAPSED minutes today. Your game will end in 2 minutes" --timeout 30) | |
playon=false | |
fi | |
done | |
sleep 120 | |
killall $PGMNAME | |
#---- THE END ----# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment