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.

useitem.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. $user = getRequestUser();
  8. if (!$database->has("inventory", ["AND" => ["itemuuid" => $VARS["uuid"], "accountid" => $user->getUID()]])) {
  9. sendJsonResp("That item doesn't exist.", "ERROR");
  10. }
  11. $item = $database->get(
  12. "items",
  13. [
  14. "[>]inventory" => ["itemid" => "itemid"],
  15. "[>]itemclasses" => ["classid" => "classid"]
  16. ],
  17. [
  18. "items.itemid",
  19. "inventory.itemjson",
  20. "items.classid",
  21. "itemclasses.classname",
  22. "items.itemcode"
  23. ],
  24. [
  25. "AND" => [
  26. "inventory.itemuuid" => $VARS["uuid"],
  27. "inventory.accountid" => $user->getUID()
  28. ]
  29. ]
  30. );
  31. $itemuuid = $VARS["uuid"];
  32. $item['itemcode'] = json_decode($item['itemcode'], true);
  33. if (empty($item['itemjson']) || $item['itemjson'] == "[]") {
  34. $itemjson = json_encode(['uses' => $item['itemcode']['uses']]);
  35. } else {
  36. $itemjson = json_decode($item["itemjson"], true);
  37. }
  38. $player = new Player($user);
  39. switch ($item["classname"]) {
  40. case "healmagic":
  41. if ($player->energy->getEnergy() < $player->energy->getMaxEnergy()) {
  42. $diff = ($player->energy->getEnergy() + $item["itemcode"]["amount"]) < $player->energy->getMaxEnergy() ? $item["itemcode"]["amount"] : ($player->energy->getMaxEnergy() - $player->energy->getEnergy());
  43. $player->changeEnergy($item["itemcode"]["amount"]);
  44. if ($itemjson["uses"] <= 1) {
  45. $database->delete("inventory", ["AND" => ["itemuuid" => $itemuuid, "accountid" => $user->getUID()]]);
  46. } else if ($itemjson["uses"] > 1) {
  47. $itemjson["uses"] -= 1;
  48. $database->update("inventory", ["itemjson" => json_encode($itemjson)], ["itemuuid" => $itemuuid]);
  49. }
  50. $player->save();
  51. sendJsonResp($Strings->build("Restored {x} energy points.", ["x" => $diff], false));
  52. } else {
  53. sendJsonResp($Strings->get("That would have no effect.", false));
  54. }
  55. break;
  56. case "artifact":
  57. if (empty($VARS["placeid"])) {
  58. sendJsonResp($Strings->get("You can't use that right now.", false));
  59. }
  60. $place = new Place($VARS["placeid"]);
  61. if ($place->isClaimed() && $place->getTeamID() == $player->getTeamID()) {
  62. $place->addArtifact(Artifact::create($player->getUID(), $place->getLocationID(), new Energy($item["itemcode"]["amount"], $item["itemcode"]["amount"]), $item["itemid"]));
  63. $place->save();
  64. $player->addExp();
  65. $player->save();
  66. if ($itemjson["uses"] <= 1) {
  67. $database->delete("inventory", ["AND" => ["itemuuid" => $itemuuid, "accountid" => $user->getUID()]]);
  68. } else if ($itemjson["uses"] > 1) {
  69. $itemjson["uses"] -= 1;
  70. $database->update("inventory", ["itemjson" => json_encode($itemjson)], ["itemuuid" => $itemuuid]);
  71. }
  72. sendJsonResp($Strings->get("Artifact activated.", false));
  73. }
  74. sendJsonResp($Strings->get("You can't use that right now.", false));
  75. break;
  76. default:
  77. sendJsonResp($Strings->get("You can't use that right now.", false));
  78. }