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

code2item.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. /**
  5. * https://www.sitepoint.com/php-random-number-generator/
  6. */
  7. class Random {
  8. // random seed
  9. private static $RSeed = 0;
  10. // set seed
  11. public static function seed($s = 0) {
  12. self::$RSeed = abs(intval($s)) % 9999999 + 1;
  13. self::num();
  14. }
  15. // generate random number
  16. public static function num($min = 0, $max = 9999999) {
  17. if (self::$RSeed == 0) {
  18. self::seed(mt_rand());
  19. }
  20. self::$RSeed = (self::$RSeed * 125) % 2796203;
  21. return self::$RSeed % ($max - $min + 1) + $min;
  22. }
  23. }
  24. $origcode = $VARS['code'];
  25. $latitude = $VARS['latitude'];
  26. $longitude = $VARS['longitude'];
  27. $accuracy = $VARS['accuracy'];
  28. $returndata = [
  29. "status" => "OK",
  30. "messages" => []
  31. ];
  32. if (is_empty($origcode)) {
  33. sendError(BARCODE_INVALID, true);
  34. }
  35. try {
  36. if (strpos($origcode, "munzee") > 1) {
  37. include 'capturemunzee.php';
  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' => $origcode, 'playeruuid' => $_SESSION['uuid']]])) {
  43. $returndata['messages'][] = ["title" => BARCODE_ALREADY_FOUND_TITLE, "text" => BARCODE_ALREADY_FOUND_MSG];
  44. } else {
  45. if ($origcode == "http://terranquest.net/#9001") {
  46. // Secret awesome codez
  47. $database->insert('inventory', ['playeruuid' => $_SESSION['uuid'], 'itemid' => 9001]);
  48. $database->insert('claimedcodes', ['code' => $origcode, 'playeruuid' => $_SESSION['uuid']]);
  49. $itemname = $database->select('items', ['itemname'], ['itemid' => 9001])[0]['itemname'];
  50. } else {
  51. $codearray = str_split($origcode);
  52. $codeint = 0;
  53. foreach ($codearray as $chr) {
  54. $codeint += ord($chr);
  55. }
  56. Random::seed($codeint);
  57. $itemcode = Random::num(1, 11);
  58. $database->insert('inventory', ['playeruuid' => $_SESSION['uuid'], 'itemid' => $itemcode]);
  59. $database->insert('claimedcodes', ['code' => $origcode, 'playeruuid' => $_SESSION['uuid']]);
  60. $itemname = $database->select('items', ['itemname'], ['itemid' => $itemcode])[0]['itemname'];
  61. }
  62. $returndata["messages"][] = ["title" => BARCODE_ITEM_FOUND_TITLE, "text" => str_replace("%1", $itemname, BARCODE_ITEM_FOUND_MSG)];
  63. }
  64. die(json_encode($returndata));