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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "inventory.itemjson",
  19. "items.classid",
  20. "itemclasses.classname",
  21. "items.itemcode"
  22. ],
  23. [
  24. "AND" => [
  25. "inventory.itemuuid" => $VARS["uuid"],
  26. "inventory.accountid" => $user->getUID()
  27. ]
  28. ]
  29. );
  30. $itemuuid = $VARS["uuid"];
  31. $item['itemcode'] = json_decode($item['itemcode'], true);
  32. if (empty($item['itemjson']) || $item['itemjson'] == "[]") {
  33. $itemjson = json_encode(['uses' => $item['itemcode']['uses']]);
  34. } else {
  35. $itemjson = json_decode($item["itemjson"], true);
  36. }
  37. $player = $database->get("players", ["energy", "maxenergy", "teamid"], ["accountid" => $user->getUID()]);
  38. switch ($item["classname"]) {
  39. case "healmagic":
  40. if ($player["energy"] < $player["maxenergy"]) {
  41. $newhp = $player["energy"] + $item["itemcode"]["amount"];
  42. if ($newhp > $player["maxenergy"]) {
  43. $newhp = $player["maxenergy"];
  44. }
  45. $diff = $newhp - $player["energy"];
  46. $database->update("players", ["energy" => $newhp], ["accountid" => $user->getUID()]);
  47. if ($itemjson["uses"] <= 1) {
  48. $database->delete("inventory", ["AND" => ["itemuuid" => $itemuuid, "accountid" => $user->getUID()]]);
  49. } else if ($itemjson["uses"] > 1) {
  50. $itemjson["uses"] -= 1;
  51. $database->update("inventory", ["itemjson" => json_encode($itemjson)], ["itemuuid" => $itemuuid]);
  52. }
  53. sendJsonResp($Strings->build("Restored {x} energy points.", ["x" => $diff], false));
  54. } else {
  55. sendJsonResp($Strings->get("That would have no effect.", false));
  56. }
  57. break;
  58. case "artifact":
  59. if (empty($VARS["placeid"]) || !$database->has("locations", ["AND" => ["osmid" => $VARS["placeid"], "teamid" => $player["teamid"]]])) {
  60. sendJsonResp($Strings->get("You can't use that right now.", false));
  61. }
  62. $place = $database->get("locations", ["locationid", "teamid", "ownerid", "currentlife", "maxlife"], ["locationid" => $VARS["placeid"]]);
  63. $placelife = $place["currentlife"] + $item["itemcode"]["amount"];
  64. $placemax = $place["maxlife"] + $item["itemcode"]["amount"];
  65. $database->update("locations", ["currentlife" => $placelife, "maxlife" => $placemax], ["osmid" => $VARS["placeid"]]);
  66. // TODO: give user some exp
  67. sendJsonResp($Strings->get("Artifact activated."));
  68. break;
  69. default:
  70. sendJsonResp($Strings->get("You can't use that right now.", false));
  71. }