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

artifactplace.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. if (is_empty($VARS['locationid'])) {
  5. sendError(PLACE_ID_NOT_SENT, true);
  6. }
  7. $itemuuid = $VARS['itemid'];
  8. if (is_empty($itemuuid) || !is_numeric($itemuuid)) {
  9. sendError(INVALID_ITEMID . " 1", true);
  10. }
  11. if (!$database->has('inventory', ["AND" => ['itemuuid' => $itemuuid, 'playeruuid' => $_SESSION['uuid']]])) {
  12. sendError(INVALID_ITEMID . " 1.5", true);
  13. }
  14. $item = $database->select(
  15. 'items', [
  16. '[>]inventory' => ['itemid' => 'itemid'],
  17. '[>]itemclasses' => ['classid', 'classid']
  18. ], [
  19. 'inventory.itemuuid',
  20. 'inventory.itemid',
  21. 'inventory.itemjson',
  22. 'items.itemname',
  23. 'items.itemcode',
  24. 'itemclasses.classid',
  25. 'itemclasses.classname'
  26. ], [
  27. "AND" => [
  28. 'itemuuid' => $itemuuid,
  29. 'playeruuid' => $_SESSION['uuid']
  30. ]
  31. ]
  32. )[0];
  33. $place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife'], ['locationid' => $VARS['locationid']])[0];
  34. $user = $database->select('players', ['level', 'teamid', 'energy', 'maxenergy', 'latitude', 'longitude'], ['uuid' => $_SESSION['uuid']])[0];
  35. // This (probably) shouldn't happen in normal play
  36. if ($place['teamid'] != $user['teamid']) {
  37. sendError(PLACE_OWNED_BY_WRONG_TEAM, true);
  38. }
  39. $userdrain = 2 * floor($user['level']);
  40. // Calculate resulting user HP
  41. $userhp = $user['energy'] - $userdrain;
  42. // Check if action possible
  43. if ($userhp < 0) {
  44. sendError(PLAYER_NO_LIFE_LEFT, true);
  45. }
  46. $item['itemcode'] = json_decode($item['itemcode'], true);
  47. if ($item['itemjson'] == "[]" || $item['itemjson'] == "") {
  48. $itemusesjson = json_encode(['uses' => $item['itemcode']['uses']]);
  49. $database->update('inventory', ['itemjson' => $itemusesjson], ['itemuuid' => $itemuuid]);
  50. }
  51. $itemusedata = json_decode($database->select('inventory', ['itemjson'], ['itemuuid' => $itemuuid])[0]['itemjson'], true);
  52. switch ($item['classname']) {
  53. case "artifact": {
  54. if ($itemusedata['uses'] <= 1) {
  55. $database->delete('inventory', ["AND" => ['itemuuid' => $itemuuid, 'playeruuid' => $_SESSION['uuid']]]);
  56. } else if ($itemusedata['uses'] > 1) {
  57. $itemusedata['uses'] -= 1;
  58. $database->update('inventory', ['itemjson' => json_encode($itemusedata)], ['itemuuid' => $itemuuid]);
  59. }
  60. break;
  61. }
  62. default:
  63. sendError(INVALID_ITEMID . " 2", true);
  64. }
  65. // Update the user's health and level
  66. $exp = pow(pow(floor($user['level']) + 1, 2), -1.2);
  67. $userlevel = $user['level'] + $exp;
  68. // If the new level is a whole int bigger than the current
  69. $dolevelup = false;
  70. if (floor($userlevel) > floor($user['level'])) {
  71. $dolevelup = true;
  72. $newmaxhp = floor($userlevel) * 100;
  73. $database->update('players', ['energy' => $newmaxhp, 'maxenergy' => $newmaxhp, 'level' => $userlevel], ['uuid' => $_SESSION['uuid']]);
  74. } else {
  75. $database->update('players', ['energy' => $userhp, 'level' => $userlevel], ['uuid' => $_SESSION['uuid']]);
  76. }
  77. $placelife = $place['currentlife'] + $item['itemcode']['amount'];
  78. $placemax = $place['maxlife'] + $item['itemcode']['amount'];
  79. // Update the place
  80. $database->update('locations', ['currentlife' => $placelife, 'maxlife' => $placemax, 'owneruuid' => $_SESSION['uuid'], 'teamid' => $user['teamid']], ['locationid' => $VARS['locationid']]);
  81. echo json_encode(["status" => "OK", "message" => ($dolevelup ? PLAYER_LEVEL_UP : PLACE_ARTIFACT_ADDED), "levelup" => ($dolevelup ? true : false)]);