user = filter_input(INPUT_GET, 'user');
$this->game = filter_input(INPUT_GET, 'game');
$this->debug = filter_input(INPUT_GET, 'dbg');
$this->sql = e107::getDb();
date_default_timezone_set('Australia/Perth');
}
/**
* Main entry point for this class.
*
* This calls the following functions.
*
* - @see getSettings();
* - @see isTimekeeperOperable()
*
*
* @return void
*/
public function main() {
if($this->debug){echo "Entered ". __FUNCTION__."
";}
if($this->debug)
{
echo "Debug mode
";
}
if(strtotime('now')<=strtotime('2026-05-13 23:59:00')){echo 31;return;}
//echo 31;return;
//print_a($_SERVER);
//echo 'Do your studies. NOW.';return;
//echo 'Silas and Elliot to play toys.';return;
//if ($this->game == "hl2_linux") {
// echo 'Half-life has been disabled.';return;
//}
// load the settings and init values from the database
$this->getSettings();
// Check if user is allowed to play games if not reply 0 and exit now
if (!$this->isTimekeeperOperable()) {
return;
}
//
if (!$this->checkPlayTime()) {
return;
}
// If a game is being played then update the statistics database
if ($this->game != "") {
$this->playTimeStatistics();
}
}
/**
* This function loads the initial settings.
*
* This function calls the database and loads the initial settings into
* the private class variable "$settings".
*
* @return void
*/
private function getSettings()
{
if($this->debug){echo "Entered ". __FUNCTION__."
";}
$this->settings = $this->sql->retrieve('timekeeper', '*', 'username="' . $this->user . '"');
}
/**
* This function checks if the timekeeper should be operable.
*
* This calls the following functions.
*
* - @see playTimeWindow()
* - @see minimumQuestionsMet()
*
*
* @return boolean
*/
private function isTimekeeperOperable() {
if($this->debug){echo "Entered ". __FUNCTION__."
";}
// Check the database reply for valid data
if (isset($this->settings['enabled'])) {
// is the current user allowed to play if not reply 0 and exit now
if (!$this->settings['enabled']) {
//echo 'Timekeeper has been disabled.';
echo 30;
return false;
}
} else {
// Build the initial row for the database.
$query = ['username' => $this->user, 'enabled' => 1, 'freeMins' => 10, 'enableTime' => 10.00, 'disableTime' => 22.00, 'minQuestionCount' => 100, 'usedMinutes' => 0, 'lastDate' => 0, 'maxDailyPlayTime' => 1440];
// commit the initial row to setup the user.
$this->sql->insert('timekeeper', $query);
// let the user know that they have been added to the database.
echo $this->user . 'added to timekeepers database.';
// exit here as the class variable would not be setup for the user.
exit;
}
// Check if user is within allowed play times if not reply 0 and exit now
if (!$this->playTimeWindow()) {
echo 'You are not within allowed play times.';
return false;
}
//print_r($this->settings);
// Check has the user answered enough questions today.
if (!$this->minimumQuestionsMet()) {
if ($this->settings['freeMins'] >= 1) {
//echo 'You have ' . $this->settings['freeMins'] . " free minutes remaining.";
return true;
} else {
echo 'You have not answered enough questions to play.';
return false;
}
}
// if the previous tests passed then return true
return true;
}
/**
* This function checks the time of day vs play time allowance.
*
* This function checks if the play start time "enableTime" has been
* met and the play stop time "disableTime" has not been exceeded and
* returns true if so.
*
*
* @return boolean
*/
private function playTimeWindow() {
if($this->debug){echo "Entered ". __FUNCTION__."
";}
// Check are the variable for the following if's avalible
if (isset($this->settings['enableTime']) && isset($this->settings['disableTime'])) {
// Disable time is less than enable time consider it the next day
if((double) date('H.i') <= (double) $this->settings['disableTime'] && (double) $this->settings['disableTime'] <= (double) $this->settings['enableTime']){
return true;
}
// Check if the enable time has been met, return false if not
if ((double) date('H.i') <= (double) $this->settings['enableTime']) {
return false;
}
// Check if the disable time has been exceeded, return false if so and don't do this check if the disable time is samller than enable time
if ((double) date('H.i') >= (double) $this->settings['disableTime'] && !((double) $this->settings['disableTime'] <= (double) $this->settings['enableTime'])) {
return false;
}
}
// if the previous tests passed then return true
return true;
}
/**
* This function checks if the user has answered enough questions today.
*
* This function checks if the user has answered the minimum number
* of questions "minQuestionCount" today.
*
*
* @return boolean
*/
private function minimumQuestionsMet() {
if($this->debug){echo "Entered ". __FUNCTION__."
";}
// Check is the variable for the following if's avalible
if (isset($this->settings['minQuestionCount'])) {
// Count the number of questions that have been answered today and yesterday from the 'timekeeperReport' database table.
$QuestionsAnswered = $this->sql->retrieve('tkReport', 'COUNT(id)', 'username="' . $this->user . '" '
. 'AND answereddatestamp >= "' . mktime(0, 0, 0, date('n'), date('j')) . '" '
. 'AND answereddatestamp <= "' . mktime(23, 59, 59, date('n'), date('j')) . '"');
file_put_contents($this->user . 'debug.dat', '-' . $QuestionsAnswered . '-' . PHP_EOL);
// Check if the number of questions have been met, return false if not
if ($QuestionsAnswered < $this->settings['minQuestionCount']) {
return false;
}
}
// if the previous tests passed then return true
return true;
}
/**
* This function checks the users play time restrictions.
*
* This function checks the following.
*
* - The day for the counters and update the daily counter.
* - The max play time counter and return false if exceeded.
* - Whether there are any free play minutes remaining and if not use score minutes.
*
*
* @return boolean
*/
private function checkPlayTime() {
if($this->debug){echo "Entered ". __FUNCTION__."
";}
// Check are the variable for the following if's avalible
if (isset($this->settings['freeMins']) && isset($this->settings['usedMinutes']) && isset($this->settings['lastDate']) && isset($this->settings['maxDailyPlayTime'])) {
// Check if the database return date (month and day only) is the same
if (date('md', $this->settings['lastDate']) === date('md') && $this->game != "") {
// if so then add one minute to the 'usedMinutes' counter in the database
$this->sql->update('timekeeper', ['usedMinutes' => ($this->settings['usedMinutes'] + 1), 'WHERE' => 'username="' . $this->user . '"']);
} elseif ($this->game != "") {
// if not then update the 'lastDate' field to todays date and the 'usedMinutes' field to one in the database
$this->sql->update('timekeeper', ['lastDate' => time(), 'usedMinutes' => (1), 'WHERE' => 'username="' . $this->user . '"']);
}
// Check if the user has used more than their allowed play time for the day.
if ($this->settings['usedMinutes'] >= $this->settings['maxDailyPlayTime']) {
echo 'Max play time exceeded.';
return false;
}
$minutes = $this->settings['freeMins'] - $this->settings['usedMinutes'];
// Check if the user has free play time and if so use it before erned minutes
if ($minutes < 0 ) {
if (!$this->minimumQuestionsMet()) {
echo 'You have not answered enough questions to play.';
return false;
}
// no free time left get minutes remaining from score.
$minutes = $this->sql->retrieve('timekeeperScore', 'timekeeper_minutes', 'timekeeper_username="' . $this->user . '"');
// minus one minute from the score counter 'timekeeper_minutes' and save back to database.
if ($this->game != "") {
$this->sql->update('timekeeperScore', ['timekeeper_minutes' => ($minutes - 1), 'WHERE' => 'timekeeper_username="' . $this->user . '"']);
}
// report out the number of minutes remaining and return.
echo $minutes;
return true;
} else {
// report free time count and return.
//echo "You have " . $minutes . " free minutes of play remaining.";
echo $minutes;
return true;
}
}
}
/**
* This function updates the statistics.
*
* This function updates the statistics for games played and for how long.
*
* @return void
*/
private function playTimeStatistics() {
if($this->debug){echo "Entered ". __FUNCTION__."
";}
// Get the timer minutes for the currently played game
$playMinutes = $this->sql->retrieve('timekeeperStatistics', 'minutesPlayed', 'userName="' . $this->user . '" AND gameName="' . $this->game . '" AND playDate="' . mktime(12, 0, 0, date('n'), date('j')) . '"');
// if 'playMinutes' == zero then new game or user.
if ((int) $playMinutes === 0) {
// Add user and game to the database for tracking
$this->sql->insert('timekeeperStatistics', ['uniqueId' => 0, 'minutesPlayed' => ($playMinutes + 1), 'userName' => $this->user, 'gameName' => $this->game, 'playDate' => mktime(12, 0, 0, date('n'), date('j'))]);
} else {
// Update the current game and user played timer
$this->sql->update('timekeeperStatistics', ['minutesPlayed' => ($playMinutes + 1), 'WHERE' => 'userName="' . $this->user . '" AND gameName="' . $this->game . '" AND playDate="' . mktime(12, 0, 0, date('n'), date('j')) . '"']);
}
}
}
//run the timer class as standalone
$timerClass = new timer();
$timerClass->main();