| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- if (!defined('e107_INIT')) {
- require_once('../../../class2.php');
- }
- //print_r($_GET);
- $e107 = e107::getInstance();
- if (!e107::isInstalled('timekeeper')) {
- e107::redirect();
- exit;
- }
- /**
- * The Timekeeper's timer connection class.
- *
- * This class checks if a user has the right to play games and if not states why.
- * This is an adapter to the local Timekeeper application monitor.
- *
- * @author gregory
- */
- class timer {
- private $user;
- private $game;
- private $debug;
- private $sql;
- private $settings;
- /**
- * Constructor.
- * Load $_GET variables via filters in to class variables ready for use
- * and instantiate the database.
- */
- function __construct() {
- $this->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.
- * <ul>
- * <li>@see getSettings();</li>
- * <li>@see isTimekeeperOperable()</li>
- * </ul>
- *
- * @return void
- */
- public function main() {
-
- if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
-
- if($this->debug)
- {
- echo "<T1>Debug mode</T1><br/>";
- }
-
-
- 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__."<br/>";}
-
- $this->settings = $this->sql->retrieve('timekeeper', '*', 'username="' . $this->user . '"');
- }
- /**
- * This function checks if the timekeeper should be operable.
- *
- * This calls the following functions.
- * <ul>
- * <li>@see playTimeWindow()</li>
- * <li>@see minimumQuestionsMet()</li>
- * </ul>
- *
- * @return boolean
- */
- private function isTimekeeperOperable() {
-
- if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
-
- // 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__."<br/>";}
- // 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__."<br/>";}
-
- // 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.
- * <ul>
- * <li>The day for the counters and update the daily counter.</li>
- * <li>The max play time counter and return false if exceeded.</li>
- * <li>Whether there are any free play minutes remaining and if not use score minutes.</li>
- * </ul>
- *
- * @return boolean
- */
- private function checkPlayTime() {
-
- if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
- // 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__."<br/>";}
-
- // 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();
|