timer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. if (!defined('e107_INIT')) {
  3. require_once('../../../class2.php');
  4. }
  5. //print_r($_GET);
  6. $e107 = e107::getInstance();
  7. if (!e107::isInstalled('timekeeper')) {
  8. e107::redirect();
  9. exit;
  10. }
  11. /**
  12. * The Timekeeper's timer connection class.
  13. *
  14. * This class checks if a user has the right to play games and if not states why.
  15. * This is an adapter to the local Timekeeper application monitor.
  16. *
  17. * @author gregory
  18. */
  19. class timer {
  20. private $user;
  21. private $game;
  22. private $debug;
  23. private $sql;
  24. private $settings;
  25. /**
  26. * Constructor.
  27. * Load $_GET variables via filters in to class variables ready for use
  28. * and instantiate the database.
  29. */
  30. function __construct() {
  31. $this->user = filter_input(INPUT_GET, 'user');
  32. $this->game = filter_input(INPUT_GET, 'game');
  33. $this->debug = filter_input(INPUT_GET, 'dbg');
  34. $this->sql = e107::getDb();
  35. date_default_timezone_set('Australia/Perth');
  36. }
  37. /**
  38. * Main entry point for this class.
  39. *
  40. * This calls the following functions.
  41. * <ul>
  42. * <li>@see getSettings();</li>
  43. * <li>@see isTimekeeperOperable()</li>
  44. * </ul>
  45. *
  46. * @return void
  47. */
  48. public function main() {
  49. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  50. if($this->debug)
  51. {
  52. echo "<T1>Debug mode</T1><br/>";
  53. }
  54. if(strtotime('now')<=strtotime('2026-05-13 23:59:00')){echo 31;return;}
  55. //echo 31;return;
  56. //print_a($_SERVER);
  57. //echo 'Do your studies. NOW.';return;
  58. //echo 'Silas and Elliot to play toys.';return;
  59. //if ($this->game == "hl2_linux") {
  60. // echo 'Half-life has been disabled.';return;
  61. //}
  62. // load the settings and init values from the database
  63. $this->getSettings();
  64. // Check if user is allowed to play games if not reply 0 and exit now
  65. if (!$this->isTimekeeperOperable()) {
  66. return;
  67. }
  68. //
  69. if (!$this->checkPlayTime()) {
  70. return;
  71. }
  72. // If a game is being played then update the statistics database
  73. if ($this->game != "") {
  74. $this->playTimeStatistics();
  75. }
  76. }
  77. /**
  78. * This function loads the initial settings.
  79. *
  80. * This function calls the database and loads the initial settings into
  81. * the private class variable "$settings".
  82. *
  83. * @return void
  84. */
  85. private function getSettings()
  86. {
  87. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  88. $this->settings = $this->sql->retrieve('timekeeper', '*', 'username="' . $this->user . '"');
  89. }
  90. /**
  91. * This function checks if the timekeeper should be operable.
  92. *
  93. * This calls the following functions.
  94. * <ul>
  95. * <li>@see playTimeWindow()</li>
  96. * <li>@see minimumQuestionsMet()</li>
  97. * </ul>
  98. *
  99. * @return boolean
  100. */
  101. private function isTimekeeperOperable() {
  102. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  103. // Check the database reply for valid data
  104. if (isset($this->settings['enabled'])) {
  105. // is the current user allowed to play if not reply 0 and exit now
  106. if (!$this->settings['enabled']) {
  107. //echo 'Timekeeper has been disabled.';
  108. echo 30;
  109. return false;
  110. }
  111. } else {
  112. // Build the initial row for the database.
  113. $query = ['username' => $this->user, 'enabled' => 1, 'freeMins' => 10, 'enableTime' => 10.00, 'disableTime' => 22.00, 'minQuestionCount' => 100, 'usedMinutes' => 0, 'lastDate' => 0, 'maxDailyPlayTime' => 1440];
  114. // commit the initial row to setup the user.
  115. $this->sql->insert('timekeeper', $query);
  116. // let the user know that they have been added to the database.
  117. echo $this->user . 'added to timekeepers database.';
  118. // exit here as the class variable would not be setup for the user.
  119. exit;
  120. }
  121. // Check if user is within allowed play times if not reply 0 and exit now
  122. if (!$this->playTimeWindow()) {
  123. echo 'You are not within allowed play times.';
  124. return false;
  125. }
  126. //print_r($this->settings);
  127. // Check has the user answered enough questions today.
  128. if (!$this->minimumQuestionsMet()) {
  129. if ($this->settings['freeMins'] >= 1) {
  130. //echo 'You have ' . $this->settings['freeMins'] . " free minutes remaining.";
  131. return true;
  132. } else {
  133. echo 'You have not answered enough questions to play.';
  134. return false;
  135. }
  136. }
  137. // if the previous tests passed then return true
  138. return true;
  139. }
  140. /**
  141. * This function checks the time of day vs play time allowance.
  142. *
  143. * This function checks if the play start time "enableTime" has been
  144. * met and the play stop time "disableTime" has not been exceeded and
  145. * returns true if so.
  146. *
  147. *
  148. * @return boolean
  149. */
  150. private function playTimeWindow() {
  151. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  152. // Check are the variable for the following if's avalible
  153. if (isset($this->settings['enableTime']) && isset($this->settings['disableTime'])) {
  154. // Disable time is less than enable time consider it the next day
  155. if((double) date('H.i') <= (double) $this->settings['disableTime'] && (double) $this->settings['disableTime'] <= (double) $this->settings['enableTime']){
  156. return true;
  157. }
  158. // Check if the enable time has been met, return false if not
  159. if ((double) date('H.i') <= (double) $this->settings['enableTime']) {
  160. return false;
  161. }
  162. // 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
  163. if ((double) date('H.i') >= (double) $this->settings['disableTime'] && !((double) $this->settings['disableTime'] <= (double) $this->settings['enableTime'])) {
  164. return false;
  165. }
  166. }
  167. // if the previous tests passed then return true
  168. return true;
  169. }
  170. /**
  171. * This function checks if the user has answered enough questions today.
  172. *
  173. * This function checks if the user has answered the minimum number
  174. * of questions "minQuestionCount" today.
  175. *
  176. *
  177. * @return boolean
  178. */
  179. private function minimumQuestionsMet() {
  180. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  181. // Check is the variable for the following if's avalible
  182. if (isset($this->settings['minQuestionCount'])) {
  183. // Count the number of questions that have been answered today and yesterday from the 'timekeeperReport' database table.
  184. $QuestionsAnswered = $this->sql->retrieve('tkReport', 'COUNT(id)', 'username="' . $this->user . '" '
  185. . 'AND answereddatestamp >= "' . mktime(0, 0, 0, date('n'), date('j')) . '" '
  186. . 'AND answereddatestamp <= "' . mktime(23, 59, 59, date('n'), date('j')) . '"');
  187. file_put_contents($this->user . 'debug.dat', '-' . $QuestionsAnswered . '-' . PHP_EOL);
  188. // Check if the number of questions have been met, return false if not
  189. if ($QuestionsAnswered < $this->settings['minQuestionCount']) {
  190. return false;
  191. }
  192. }
  193. // if the previous tests passed then return true
  194. return true;
  195. }
  196. /**
  197. * This function checks the users play time restrictions.
  198. *
  199. * This function checks the following.
  200. * <ul>
  201. * <li>The day for the counters and update the daily counter.</li>
  202. * <li>The max play time counter and return false if exceeded.</li>
  203. * <li>Whether there are any free play minutes remaining and if not use score minutes.</li>
  204. * </ul>
  205. *
  206. * @return boolean
  207. */
  208. private function checkPlayTime() {
  209. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  210. // Check are the variable for the following if's avalible
  211. if (isset($this->settings['freeMins']) && isset($this->settings['usedMinutes']) && isset($this->settings['lastDate']) && isset($this->settings['maxDailyPlayTime'])) {
  212. // Check if the database return date (month and day only) is the same
  213. if (date('md', $this->settings['lastDate']) === date('md') && $this->game != "") {
  214. // if so then add one minute to the 'usedMinutes' counter in the database
  215. $this->sql->update('timekeeper', ['usedMinutes' => ($this->settings['usedMinutes'] + 1), 'WHERE' => 'username="' . $this->user . '"']);
  216. } elseif ($this->game != "") {
  217. // if not then update the 'lastDate' field to todays date and the 'usedMinutes' field to one in the database
  218. $this->sql->update('timekeeper', ['lastDate' => time(), 'usedMinutes' => (1), 'WHERE' => 'username="' . $this->user . '"']);
  219. }
  220. // Check if the user has used more than their allowed play time for the day.
  221. if ($this->settings['usedMinutes'] >= $this->settings['maxDailyPlayTime']) {
  222. echo 'Max play time exceeded.';
  223. return false;
  224. }
  225. $minutes = $this->settings['freeMins'] - $this->settings['usedMinutes'];
  226. // Check if the user has free play time and if so use it before erned minutes
  227. if ($minutes < 0 ) {
  228. if (!$this->minimumQuestionsMet()) {
  229. echo 'You have not answered enough questions to play.';
  230. return false;
  231. }
  232. // no free time left get minutes remaining from score.
  233. $minutes = $this->sql->retrieve('timekeeperScore', 'timekeeper_minutes', 'timekeeper_username="' . $this->user . '"');
  234. // minus one minute from the score counter 'timekeeper_minutes' and save back to database.
  235. if ($this->game != "") {
  236. $this->sql->update('timekeeperScore', ['timekeeper_minutes' => ($minutes - 1), 'WHERE' => 'timekeeper_username="' . $this->user . '"']);
  237. }
  238. // report out the number of minutes remaining and return.
  239. echo $minutes;
  240. return true;
  241. } else {
  242. // report free time count and return.
  243. //echo "You have " . $minutes . " free minutes of play remaining.";
  244. echo $minutes;
  245. return true;
  246. }
  247. }
  248. }
  249. /**
  250. * This function updates the statistics.
  251. *
  252. * This function updates the statistics for games played and for how long.
  253. *
  254. * @return void
  255. */
  256. private function playTimeStatistics() {
  257. if($this->debug){echo "Entered ". __FUNCTION__."<br/>";}
  258. // Get the timer minutes for the currently played game
  259. $playMinutes = $this->sql->retrieve('timekeeperStatistics', 'minutesPlayed', 'userName="' . $this->user . '" AND gameName="' . $this->game . '" AND playDate="' . mktime(12, 0, 0, date('n'), date('j')) . '"');
  260. // if 'playMinutes' == zero then new game or user.
  261. if ((int) $playMinutes === 0) {
  262. // Add user and game to the database for tracking
  263. $this->sql->insert('timekeeperStatistics', ['uniqueId' => 0, 'minutesPlayed' => ($playMinutes + 1), 'userName' => $this->user, 'gameName' => $this->game, 'playDate' => mktime(12, 0, 0, date('n'), date('j'))]);
  264. } else {
  265. // Update the current game and user played timer
  266. $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')) . '"']);
  267. }
  268. }
  269. }
  270. //run the timer class as standalone
  271. $timerClass = new timer();
  272. $timerClass->main();