PHP game server for TerranQuest. https://terranquest.net

useitem.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. $itemuuid = $VARS['itemuuid'];
  5. if (is_empty($itemuuid) || !is_numeric($itemuuid) || !$database->has('inventory', ["AND" => ['itemuuid' => $itemuuid, 'playeruuid' => $_SESSION['uuid']]])) {
  6. sendError(INVALID_ITEMID, true);
  7. }
  8. $item = $database->select(
  9. 'items', [
  10. '[>]inventory' => ['itemid' => 'itemid'],
  11. '[>]itemclasses' => ['classid', 'classid']
  12. ], [
  13. 'inventory.itemuuid',
  14. 'inventory.itemid',
  15. 'inventory.itemjson',
  16. 'items.itemname',
  17. 'items.itemcode',
  18. 'itemclasses.classid',
  19. 'itemclasses.classname'
  20. ], [
  21. "AND" => [
  22. 'itemuuid' => $itemuuid,
  23. 'playeruuid' => $_SESSION['uuid']
  24. ]
  25. ]
  26. )[0];
  27. $player = $database->select('players', ['energy', 'maxenergy'], ['uuid' => $_SESSION['uuid']])[0];
  28. $item['itemcode'] = json_decode($item['itemcode'], true);
  29. if ($item['itemjson'] == "[]" || $item['itemjson'] == "") {
  30. $itemusesjson = json_encode(['uses' => $item['itemcode']['uses']]);
  31. $database->update('inventory', ['itemjson' => $itemusesjson], ['itemuuid' => $itemuuid]);
  32. }
  33. $itemusedata = json_decode($database->select('inventory', ['itemjson'], ['itemuuid' => $itemuuid])[0]['itemjson'], true);
  34. switch ($item['classname']) {
  35. case "healmagic":
  36. // Only use item if it will do something
  37. if ($player['energy'] < $player['maxenergy']) {
  38. $newhp = $player['energy'] + $item['itemcode']['amount'];
  39. if ($newhp > $player['maxenergy']) {
  40. $newhp = $player['maxenergy'];
  41. }
  42. $database->update('players', ['energy' => $newhp], ['uuid' => $_SESSION['uuid']]);
  43. if ($itemusedata['uses'] <= 1) {
  44. $database->delete('inventory', ["AND" => ['itemuuid' => $itemuuid, 'playeruuid' => $_SESSION['uuid']]]);
  45. } else if ($itemusedata['uses'] > 1) {
  46. $itemusedata['uses'] -= 1;
  47. $database->update('inventory', ['itemjson' => json_encode($itemusedata)], ['itemuuid' => $itemuuid]);
  48. }
  49. }
  50. break;
  51. }
  52. sendOK();
  53. //echo json_encode($item);