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

claimplace.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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_PLAYER_TEAM, true);
  12. }
  13. if ($place['currentlife'] > 0) {
  14. sendError(PLACE_NO_LIFE_CLAIM, true);
  15. }
  16. $userdrain = 5 * 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. // Update the place
  36. $database->update('locations', ['currentlife' => 100, 'maxlife' => 100, 'owneruuid' => $_SESSION['uuid'], 'teamid' => $user['teamid']], ['locationid' => $VARS['locationid']]);
  37. echo json_encode(["status" => "OK", "message" => ($dolevelup ? PLAYER_LEVEL_UP : PLACE_SUCCESS), "levelup" => ($dolevelup ? true : false)]);