refillplace.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. $place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife'], ['locationid' => $VARS['locationid']])[0];
  8. $user = $database->select('players', ['level', 'teamid', 'energy', 'maxenergy', 'latitude', 'longitude'], ['uuid' => $_SESSION['uuid']])[0];
  9. // This (probably) shouldn't happen in normal play
  10. if ($place['teamid'] != $user['teamid']) {
  11. sendError(PLACE_OWNED_BY_WRONG_TEAM, true);
  12. }
  13. if ($place['currentlife'] == 100) {
  14. sendError(PLACE_HEALTH_IS_FULL, true);
  15. }
  16. $userdrain = 2 * floor($user['level']);
  17. // Calculate resulting user HP
  18. $userhp = $user['energy'] - $userdrain;
  19. // Check if action possible
  20. if ($userhp < 0) {
  21. sendError(PLAYER_NO_LIFE_LEFT, true);
  22. }
  23. // Update the user's health and level
  24. $exp = pow(pow(floor($user['level']) + 1, 2), -1.2);
  25. $userlevel = $user['level'] + $exp;
  26. // If the new level is a whole int bigger than the current
  27. $dolevelup = false;
  28. if (floor($userlevel) > floor($user['level'])) {
  29. $dolevelup = true;
  30. $newmaxhp = floor($userlevel) * 100;
  31. $database->update('players', ['energy' => $newmaxhp, 'maxenergy' => $newmaxhp, 'level' => $userlevel], ['uuid' => $_SESSION['uuid']]);
  32. } else {
  33. $database->update('players', ['energy' => $userhp, 'level' => $userlevel], ['uuid' => $_SESSION['uuid']]);
  34. }
  35. $placelife = $place['currentlife'] + 10;
  36. if ($placelife > $place['maxlife']) {
  37. $placelife = $place['maxlife'];
  38. }
  39. // Update the place
  40. $database->update('locations', ['currentlife' => $placelife, 'owneruuid' => $_SESSION['uuid'], 'teamid' => $user['teamid']], ['locationid' => $VARS['locationid']]);
  41. echo json_encode(["status" => "OK", "message" => ($dolevelup ? PLAYER_LEVEL_UP : PLACE_HEALTH_REFILLED), "levelup" => ($dolevelup ? true : false)]);