getLottery.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. session_id($_POST['token']);
  3. session_start();
  4. require_once("config.php");
  5. if(!isset($_SESSION['userID']) || !isset($_POST['activityID']))
  6. {
  7. exit("{}");
  8. }
  9. $userID = $_SESSION['userID'];
  10. $activityID = $_POST['activityID'];
  11. $sessionKey = 'hasAnswerRight_' . $activityID;
  12. $_SESSION[$sessionKey] = 1; //忽略掉答题, 直接进入抽奖
  13. if(!isset($_SESSION[$sessionKey]))
  14. {
  15. exit("{}");
  16. }
  17. $canRoll = (intval($_SESSION[$sessionKey]) == 1);
  18. if(!$canRoll)
  19. {
  20. exit("{}");
  21. }
  22. if(!ENABLE_TEST)
  23. {
  24. unset($_SESSION[$sessionKey]);
  25. }
  26. //$userID = 25586;
  27. //$activityID = 15;
  28. $dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME, DB_PORT);
  29. $sql = "SELECT `status`, `user_id` FROM `pingan_user_info` WHERE `user_id` = $userID AND `activity_id` = $activityID";
  30. $queryResult = $dbLink->query($sql);
  31. $rows = $queryResult->fetch_assoc();
  32. $queryResult->free();
  33. if($rows['status'] > 0)
  34. {
  35. exit("{}");
  36. }
  37. $sql = "SELECT `id`, `order`,`prize_name`, `prize_rate`, `sum`, `cur` FROM `pingan_prize_config` WHERE `activity_id` = $activityID ORDER BY `order` ASC";
  38. $queryResult = $dbLink->query($sql);
  39. $rollRate = [];
  40. while($row = $queryResult->fetch_assoc()){
  41. $rollRate[$row['order']] = [$row['prize_name'], $row['prize_rate'], $row['sum'], $row['cur'], $row['id']];
  42. }
  43. $queryResult->free();
  44. $rand = mt_rand(1, 10000);
  45. //echo "rand1 = " . $rand . "<br/>";
  46. $pack_id = 0;
  47. $prizeID = 0;
  48. $rate = 0;
  49. $count = 0;
  50. for ($i = 1; $i <= count($rollRate); $i++) {
  51. $rate += $rollRate[$i][1] * 100;
  52. $sum = $rollRate[$i][2];
  53. $cur = $rollRate[$i][3];
  54. //echo "compare i = " . $i . ", itemRate = " . $rollRate[$i][1] . ",totalRate = " . $rate . "<br/>";
  55. if ($rate >= $rand) {
  56. if($cur + 1 <= $sum)
  57. {
  58. $pack_id = $i;
  59. $prizeID = $rollRate[$i][4];
  60. //echo "packID = " . $pack_id . "<br/>";
  61. $msg = $rollRate[$i][0];
  62. break;
  63. }
  64. else
  65. {
  66. //本奖品无剩余,重新抽
  67. $count++;
  68. $rate = 0;
  69. $i = 1;
  70. $rand = mt_rand(1, 10000);
  71. //echo "reroll = " . $rand . "<br/>";
  72. }
  73. }
  74. if($count > 100)
  75. {
  76. //echo "max count break <br/>";
  77. break;
  78. }
  79. }
  80. if($pack_id > 0)
  81. {
  82. $updateSQL = 'UPDATE `pingan_user_info` SET `status` = ?, `prize` = ? WHERE `user_id` = ? AND `activity_id` = ?';
  83. if (!($stmt = $dbLink->prepare($updateSQL))) {
  84. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  85. }
  86. $s = ENABLE_TEST ? 0 : 1;
  87. $stmt->bind_param("iiii", $s, $pack_id, $userID, $activityID);
  88. $stmt->execute();
  89. $updateSQL = 'UPDATE `pingan_prize_config` SET `cur` = `cur` + 1 WHERE `id` = ? AND `activity_id` = ?';
  90. if (!($stmt = $dbLink->prepare($updateSQL))) {
  91. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  92. }
  93. $stmt->bind_param("ii", $prizeID, $activityID);
  94. $stmt->execute();
  95. $data = [
  96. 'code' => 200,
  97. 'data' => [
  98. 'id' => $pack_id,
  99. 'name' => $msg
  100. ]
  101. ];
  102. exit(json_encode($data));
  103. }
  104. exit(json_encode(array("code"=>444)));