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

attackplace.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
  5. if (is_empty($VARS['locationid'])) {
  6. sendError(PLACE_ID_NOT_SENT, true);
  7. }
  8. $place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife', 'osmid'], ['locationid' => $VARS['locationid']])[0];
  9. $user = $database->select('players', ['level', 'teamid', 'energy', 'maxenergy', 'latitude', 'longitude'], ['uuid' => $_SESSION['uuid']])[0];
  10. // This (probably) shouldn't happen in normal play
  11. if ($place['teamid'] == $user['teamid']) {
  12. sendError(PLACE_OWNED_BY_SAME_TEAM, true);
  13. }
  14. // The damage formulas
  15. require_once 'type_grid.php';
  16. $userdrain = pow(floor($user['level']), 0.5) * 5;
  17. $type_mod = $TYPE_GRID[$user['teamid']][$place['teamid']];
  18. if ($type_mod == 0.5) {
  19. $type_mod = 0.8;
  20. } else if ($type_mod == 2) {
  21. $type_mod = 1.5;
  22. }
  23. $terrain_mod = 1.0;
  24. $weather_mod = 1.0;
  25. $distance_mod = 1.0;
  26. if (!is_empty($VARS['lat']) && !is_empty($VARS['long'])) {
  27. try {
  28. require_once 'latlong_validate.php';
  29. if (!is_empty(DARKSKY_APIKEY)) {
  30. require 'weather_inc.php';
  31. $weather_icon = $currently['icon'];
  32. $weather_mod = $WEATHER_GRID[$weather_icon][$user['teamid'] - 1];
  33. if ($weather_mod == 0.5) {
  34. $weather_mod = .9;
  35. } else if ($weather_mod == 2) {
  36. $weather_mod = 1.1;
  37. }
  38. }
  39. } catch (Exception $e) {
  40. $weather_mod = 1.0;
  41. }
  42. try {
  43. $terrain = json_decode(file_get_contents("http://gis.terranquest.net/terrain.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long']), TRUE);
  44. if ($terrain['status'] == 'OK') {
  45. $terraintype = $terrain['type'];
  46. $terrain_mod = $TERRAIN_GRID[$terraintype][$user['teamid'] - 1];
  47. if ($terrain_mod == 0.5) {
  48. $terrain_mod = .9;
  49. } else if ($terrain_mod == 2) {
  50. $terrain_mod = 1.1;
  51. } else if ($terrain_mod == 3) {
  52. $terrain_mod = 1.3;
  53. }
  54. }
  55. } catch (Exception $e) {
  56. $terrain_mod = 1.0;
  57. }
  58. try {
  59. // Round to 2 digits (approx. 1.11 meters/3.6 feet)
  60. $lat = number_format((float) $VARS['lat'], 5, '.', '');
  61. $long = number_format((float) $VARS['long'], 5, '.', '');
  62. $placedata = json_decode(file_get_contents("http://gis.terranquest.net/place.php?key=" . GIS_API_KEY . "&osmid=" . $place['osmid']), true);
  63. $placelong = $placedata['features'][0]['geometry']['coordinates'][0];
  64. $placelat = $placedata['features'][0]['geometry']['coordinates'][1];
  65. $playerlocation = GeoLocation::fromDegrees($lat, $long);
  66. $placelocation = GeoLocation::fromDegrees($placelat, $placelong);
  67. $distance_mod = DISTANCE_GRID($playerlocation->distanceTo($placelocation, 'miles'));
  68. } catch (Exception $e) {
  69. $distance_mod = 1.0;
  70. }
  71. }
  72. $damage = pow(floor($user['level']), 0.5) * 4 * $type_mod * $terrain_mod * $weather_mod * $distance_mod;
  73. //$damage = 2 * $userdrain * $TYPE_GRID[$user['teamid']][$place['teamid']];
  74. // Check if action possible
  75. if ($user['energy'] < $userdrain) {
  76. sendError(PLAYER_NO_LIFE_LEFT, true);
  77. }
  78. // Calculate resulting user HP
  79. $userhp = $user['energy'] - $userdrain;
  80. // Calculate resulting place HP
  81. $placehp = $place['currentlife'] - $damage;
  82. // No negatives plz
  83. if ($placehp < 0) {
  84. $placehp = 0;
  85. }
  86. // Update the user's health and level
  87. $exp = pow(pow(floor($user['level']) + 1, 2), -1.2);
  88. $userlevel = $user['level'] + $exp;
  89. // If the new level is a whole int bigger than the current
  90. $dolevelup = false;
  91. if (floor($userlevel) > floor($user['level'])) {
  92. $dolevelup = true;
  93. $newmaxhp = floor($userlevel) * 100;
  94. $database->update('players', ['energy' => $newmaxhp, 'maxenergy' => $newmaxhp, 'level' => $userlevel], ['uuid' => $_SESSION['uuid']]);
  95. } else {
  96. $database->update('players', ['energy' => $userhp, 'level' => $userlevel], ['uuid' => $_SESSION['uuid']]);
  97. }
  98. if ($placehp == 0) {
  99. // It's dead
  100. $database->update('locations', ['owneruuid' => null, 'teamid' => 0, 'currentlife' => 0, 'maxlife' => 0], ['locationid' => $VARS['locationid']]);
  101. } else {
  102. // or not
  103. $database->update('locations', ['currentlife' => $placehp], ['locationid' => $VARS['locationid']]);
  104. }
  105. echo json_encode(["status" => "OK", "message" => ($dolevelup ? PLAYER_LEVEL_UP : PLACE_SUCCESS), "levelup" => ($dolevelup ? true : false)]);