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.

Artifact.lib.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Artifact {
  8. private $uuid = null;
  9. private $locationid = null;
  10. private $energy = 0;
  11. private $delete = false;
  12. /**
  13. * Create a new artifact.
  14. * @global type $database
  15. * @param int $ownerid
  16. * @param int $locationid
  17. * @param \Energy $energy
  18. * @param int $itemid
  19. * @return \Artifact The newly created artifact.
  20. */
  21. public static function create(int $ownerid, int $locationid, \Energy $energy, int $itemid) {
  22. global $database;
  23. $database->insert("artifacts", ["currentlife" => $energy->getEnergy(), "maxlife" => $energy->getMaxEnergy(), "locationid" => $locationid, "accountid" => $ownerid, "itemid" => $itemid]);
  24. return (new Artifact($database->id()));
  25. }
  26. public function __construct($uuid) {
  27. global $database;
  28. $data = $database->get("artifacts", ["artuuid", "currentlife", "maxlife", "locationid"], ["artuuid" => $uuid]);
  29. $this->uuid = $data["artuuid"];
  30. $this->energy = new Energy($data["currentlife"], $data["maxlife"]);
  31. $this->locationid = $data["locationid"];
  32. }
  33. public function changeEnergy(int $diff) {
  34. $this->energy->setEnergy($this->energy->getEnergy() + $diff);
  35. }
  36. public function getEnergy(): \Energy {
  37. return $this->energy;
  38. }
  39. /**
  40. * Check if the artifact will be deleted on save.
  41. * @return bool
  42. */
  43. public function deleted(): bool {
  44. if ($this->energy->getEnergy() == 0 || $this->delete) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * Mark for deletion on save.
  51. */
  52. public function delete() {
  53. $this->delete = true;
  54. }
  55. public function save() {
  56. global $database;
  57. if ($this->deleted()) {
  58. $database->delete("artifacts", ["artuuid" => $this->uuid]);
  59. } else {
  60. $database->update("artifacts", ["currentlife" => $this->energy->getEnergy(), "maxlife" => $this->energy->getMaxEnergy()], ["artuuid" => $this->uuid]);
  61. }
  62. }
  63. }