Game server and admin dashboard for TerranQuest.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

submitcode.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. /**
  8. * https://www.sitepoint.com/php-random-number-generator/
  9. */
  10. class Random {
  11. // random seed
  12. private static $RSeed = 0;
  13. // set seed
  14. public static function seed($s = 0) {
  15. self::$RSeed = abs(intval($s)) % 9999999 + 1;
  16. self::num();
  17. }
  18. // generate random number
  19. public static function num($min = 0, $max = 9999999) {
  20. if (self::$RSeed == 0) {
  21. self::seed(mt_rand());
  22. }
  23. self::$RSeed = (self::$RSeed * 125) % 2796203;
  24. return self::$RSeed % ($max - $min + 1) + $min;
  25. }
  26. }
  27. $returndata = [
  28. "status" => "OK",
  29. "item" => "",
  30. "munzee" => "",
  31. "messages" => []
  32. ];
  33. try {
  34. if (strpos($VARS["code"], "munzee") > 1) {
  35. if (!empty($VARS["latitude"]) && !empty($VARS["longitude"]) && !empty($VARS["accuracy"])) {
  36. include 'capturemunzee.php';
  37. }
  38. }
  39. } catch (Exception $ex) {
  40. //file_put_contents("munzee.log", "Error with Munzee code: $ex\n", FILE_APPEND);
  41. }
  42. if ($database->has('claimedcodes', ["AND" => ['code' => $VARS["code"], 'accountid' => getRequestUser()->getUID()]])) {
  43. $returndata['messages'][] = $Strings->get("You've already discovered that.", false);
  44. } else {
  45. $codearray = str_split($VARS["code"]);
  46. $codeint = 0;
  47. foreach ($codearray as $chr) {
  48. $codeint += ord($chr);
  49. }
  50. Random::seed($codeint);
  51. // Get all items, except ones that have no rarity, which can't be found normally
  52. $items = $database->select("items", ["itemid", "weight"], ["weight[>]" => 0]);
  53. $weighted = [];
  54. foreach ($items as $item) {
  55. for ($i = 0; $i < $item["weight"]; $i++) {
  56. $weighted[] = $item["itemid"];
  57. }
  58. }
  59. $itemid = $weighted[Random::num(0, count($weighted))];
  60. $database->insert('inventory', ['accountid' => getRequestUser()->getUID(), 'itemid' => $itemid]);
  61. $database->insert('claimedcodes', ['code' => $VARS["code"], 'accountid' => getRequestUser()->getUID()]);
  62. $player = new Player(getRequestUser());
  63. $player->stats->updateStat(PlayerStats::SCANS, 1);
  64. $player->save();
  65. $itemname = $database->get('items', 'itemname', ['itemid' => $itemid]);
  66. $returndata["item"] = $itemname;
  67. $returndata['messages'][] = $Strings->build("You found one {item}", ["item" => $itemname], false);
  68. }
  69. if (strpos($VARS["code"], "munzee") !== false && $database->has('munzee', ['accountid' => getRequestUser()->getUID()])) {
  70. if (!empty($VARS["latitude"]) && !empty($VARS["longitude"]) && !empty($VARS["accuracy"])) {
  71. $latitude = $VARS["latitude"];
  72. $longitude = $VARS["longitude"];
  73. $accuracy = $VARS["accuracy"];
  74. /* Check if we need to refresh the bearer token first */
  75. if ($database->has('munzee', ["AND" => ['accountid' => getRequestUser()->getUID(), 'expires[<=]' => (time() + 30)]])) {
  76. $url = 'https://api.munzee.com/oauth/login';
  77. $fields = array(
  78. 'client_id' => urlencode($SETTINGS["munzee"]["id"]),
  79. 'client_secret' => urlencode($SETTINGS["munzee"]["secret"]),
  80. 'grant_type' => 'refresh_token',
  81. 'refresh_token' => $database->select('munzee', 'refreshtoken', ['accountid' => getRequestUser()->getUID()])[0]
  82. );
  83. foreach ($fields as $key => $value) {
  84. $fields_string .= $key . '=' . $value . '&';
  85. }
  86. rtrim($fields_string, '&');
  87. $ch = curl_init();
  88. $options = array(
  89. CURLOPT_URL => $url,
  90. CURLOPT_POST => 1,
  91. CURLOPT_POSTFIELDS => $fields_string,
  92. CURLOPT_RETURNTRANSFER => 1, // return web page
  93. CURLOPT_HEADER => false, // don't return headers
  94. CURLOPT_ENCODING => "", // handle compressed
  95. CURLOPT_USERAGENT => "TerranQuest Game Server", // name of client
  96. CURLOPT_AUTOREFERER => true, // set referrer on redirect
  97. CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
  98. CURLOPT_TIMEOUT => 120, // time-out on response
  99. );
  100. curl_setopt_array($ch, $options);
  101. $result = curl_exec($ch);
  102. curl_close($ch);
  103. $data = json_decode($result, TRUE)['data'];
  104. $status_code = json_decode($result, TRUE)['status_code'];
  105. if ($status_code == 200) {
  106. $database->update('munzee', ['bearertoken' => $data['token']['access_token'], 'refreshtoken' => $data['token']['refresh_token'], 'expires' => $data['token']['expires']], ['accountid' => getRequestUser()->getUID()]);
  107. }
  108. }
  109. /* Check again now */
  110. if ($database->has('munzee', ["AND" => ['accountid' => getRequestUser()->getUID(), 'expires[>]' => (time() + 30)]])) {
  111. $url = 'https://api.munzee.com/capture/light/';
  112. $header = array(
  113. 'Authorization: ' . $database->select('munzee', ['bearertoken'], ['accountid' => getRequestUser()->getUID()])[0]['bearertoken']
  114. );
  115. $time = time();
  116. $fields = ['data' => json_encode([
  117. "language" => "EN",
  118. "latitude" => $latitude,
  119. "longitude" => $longitude,
  120. "accuracy" => $accuracy,
  121. "time" => time(),
  122. "code" => $VARS["code"]
  123. ])
  124. ];
  125. //open connection
  126. $ch = curl_init();
  127. $options = array(
  128. CURLOPT_URL => $url,
  129. CURLOPT_POST => true,
  130. CURLOPT_POSTFIELDS => $fields,
  131. CURLOPT_HTTPHEADER => $header,
  132. CURLOPT_RETURNTRANSFER => true, // return web page
  133. CURLOPT_HEADER => false, // don't return headers
  134. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  135. CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
  136. CURLOPT_ENCODING => "", // handle compressed
  137. CURLOPT_USERAGENT => "TerranQuest Game Server", // name of client
  138. CURLOPT_AUTOREFERER => true, // set referrer on redirect
  139. CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
  140. CURLOPT_TIMEOUT => 120, // time-out on response
  141. );
  142. curl_setopt_array($ch, $options);
  143. $result = curl_exec($ch);
  144. //close connection
  145. curl_close($ch);
  146. $data = json_decode($result, TRUE);
  147. if ($data['status_code'] == 200) {
  148. // Add munzee capture info to response
  149. $returndata["munzee"] = $data["data"]["result"];
  150. }
  151. }
  152. }
  153. }
  154. exitWithJson($returndata);