Game server and admin dashboard for TerranQuest.
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.

PlayerStats.lib.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. class PlayerStats {
  8. private $userid = 0;
  9. private $claims = 0;
  10. private $attacks = 0;
  11. private $defends = 0;
  12. private $scans = 0;
  13. private $distancekm = 0.0;
  14. const CLAIMS = 1;
  15. const ATTACKS = 2;
  16. const DEFENDS = 3;
  17. const SCANS = 4;
  18. const DISTANCEKM = 5;
  19. public function __construct(User $user) {
  20. global $database;
  21. $this->userid = $user->getUID();
  22. if ($database->has("player_stats", ["accountid" => $this->userid])) {
  23. $stats = $database->get("player_stats", ["claims", "attacks", "defends", "scans", "distancekm"], ["accountid" => $this->userid]);
  24. $this->claims = $stats["claims"];
  25. $this->attacks = $stats["attacks"];
  26. $this->defends = $stats["defends"];
  27. $this->scans = $stats["scans"];
  28. $this->distancekm = $stats["distancekm"];
  29. }
  30. }
  31. public function getStat(int $stat) {
  32. switch ($stat) {
  33. case PlayerStats::CLAIMS:
  34. return $this->claims;
  35. case PlayerStats::ATTACKS:
  36. return $this->attacks;
  37. case PlayerStats::DEFENDS:
  38. return $this->defends;
  39. case PlayerStats::SCANS:
  40. return $this->scans;
  41. case PlayerStats::DISTANCEKM:
  42. return $this->distancekm;
  43. default:
  44. return null;
  45. }
  46. }
  47. public function updateStat(int $stat, $diff) {
  48. switch ($stat) {
  49. case PlayerStats::CLAIMS:
  50. $this->claims += $diff;
  51. break;
  52. case PlayerStats::ATTACKS:
  53. $this->attacks += $diff;
  54. break;
  55. case PlayerStats::DEFENDS:
  56. $this->defends += $diff;
  57. break;
  58. case PlayerStats::SCANS:
  59. $this->scans += $diff;
  60. break;
  61. case PlayerStats::DISTANCEKM:
  62. $this->distancekm += $diff;
  63. break;
  64. }
  65. }
  66. public function save() {
  67. global $database;
  68. if ($database->has("player_stats", ["accountid" => $this->userid])) {
  69. $database->update("player_stats", [
  70. "claims" => $this->claims,
  71. "attacks" => $this->attacks,
  72. "defends" => $this->defends,
  73. "scans" => $this->scans,
  74. "distancekm" => $this->distancekm
  75. ], [
  76. "accountid" => $this->userid
  77. ]
  78. );
  79. } else {
  80. $database->insert("player_stats", [
  81. "accountid" => $this->userid,
  82. "claims" => $this->claims,
  83. "attacks" => $this->attacks,
  84. "defends" => $this->defends,
  85. "scans" => $this->scans,
  86. "distancekm" => $this->distancekm
  87. ]
  88. );
  89. }
  90. }
  91. }