You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbase.inc 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. require_once "gamefunction.inc";
  3. class dbase
  4. {
  5. //properties
  6. protected $db;
  7. //methods
  8. public function __construct()
  9. {
  10. $this->db = new SQLite3("db.sqlite");
  11. }
  12. //check login
  13. public function loginQuery($login, $password)
  14. {
  15. $stmt = $this->db->prepare("SELECT * FROM users WHERE login = :usernm AND password = :pswrd");
  16. $stmt->bindValue(':usernm', $login, SQLITE3_TEXT);
  17. $stmt->bindValue(':pswrd', $password, SQLITE3_TEXT);
  18. $gameLogin = $stmt->execute();
  19. return $gameLogin->fetchArray(SQLITE3_ASSOC);
  20. }
  21. //check if name exists
  22. public function registerQuery($login)
  23. {
  24. $resultStmt = $this->db->prepare("SELECT login FROM users WHERE LOWER(login) = LOWER(:lgin)");
  25. $resultStmt->bindValue(':lgin', $login, SQLITE3_TEXT);
  26. $result = $resultStmt->execute();
  27. return $result->fetchArray(SQLITE3_ASSOC);
  28. }
  29. //insert new user into db
  30. public function registerInsert($login, $password)
  31. {
  32. $insertStmt = $this->db->prepare("INSERT INTO users(login, password) VALUES (:lgin, :pswd)");
  33. $insertStmt->bindValue(':lgin', $login, SQLITE3_TEXT);
  34. $insertStmt->bindValue(':pswd', $password, SQLITE3_TEXT);
  35. $insertStmt->execute();
  36. }
  37. public function highScoreQuery()
  38. {
  39. return $this->db->query("SELECT login, count(*) AS HighScore
  40. FROM game JOIN users ON users.id = user_id
  41. WHERE won = 1
  42. GROUP BY login
  43. ORDER BY highScore DESC, login ASC");
  44. }
  45. //score current user
  46. public function userScoreQuery($spelerId)
  47. {
  48. $stmtUserScore = $this->db->prepare("SELECT date, won, difficulty FROM game WHERE user_id = :usrid ORDER BY date");
  49. $stmtUserScore->bindValue(':usrid', $spelerId, SQLITE3_INTEGER);
  50. return $stmtUserScore->execute();
  51. }
  52. //insert new result after end game
  53. public function insertResult()
  54. {
  55. $resultStmt = $this->db->prepare("INSERT INTO game (user_id, date, won, difficulty) VALUES (:splrId, :date, :splrscr, '{$_SESSION['game']->difficulty}')");
  56. $resultStmt->bindValue(':splrId', $_SESSION['spelerId'], SQLITE3_INTEGER);
  57. $resultStmt->bindValue(':date', date('Y-m-d H:i:s'), SQLITE3_TEXT);
  58. $resultStmt->bindValue(':splrscr', $_SESSION['game']->gameResult, SQLITE3_INTEGER);
  59. $resultStmt->execute();
  60. }
  61. }
  62. $dbase = new dbase;