GameTestController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Facade\TableName;
  4. use App\Game\GameCard;
  5. use App\Game\GlobalUserInfo;
  6. use App\Game\Route;
  7. use App\Http\Controllers\Api\ApiController;
  8. use App\Http\Controllers\Controller;
  9. use App\Models\AccountsInfo;
  10. use App\Services\IpCheck;
  11. use App\Util;
  12. use App\Utility\SetNXLock;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Redis;
  16. // use Yansongda\Pay\Log;
  17. class GameTestController extends Controller
  18. {
  19. public function gameLunch()
  20. {
  21. $api = 'https://int.stage.onlyplay.net/api/get_frame';
  22. $baseInfo = [
  23. 'game_bundle' => 'juicycrush',
  24. 'callback_url' => 'https://br.24b.pro',
  25. 'partner_id' => 949,
  26. 'user_id' => strval(4478930),
  27. 'balance' => 23527,
  28. 'currency' => env('CONFIG_24680_CURRENCY'),
  29. 'decimals' => 2,
  30. 'lang' => 'liang',
  31. 'token' => sha1(time())
  32. ];
  33. $baseInfo['sign'] = $this->_sign($baseInfo);
  34. $headers = [
  35. 'Content-Type' => 'application/json',
  36. ];
  37. $rs = Util::curlPost2($api,json_encode($baseInfo),true,$headers);
  38. dd($rs);
  39. }
  40. private function _sign($params)
  41. {
  42. $secretKey = 'd4ecdd3835395886eaaffb20d4192159b4f8b4cd15b52763952dabb2b6e5cfe25aa6c8a8594b2480c3766ce7dac3296465fb09091c91e602d34ceddc';
  43. ksort($params);
  44. $sign_string = '';
  45. foreach ($params as $key => $value) {
  46. if(is_array($value)){
  47. $value = json_encode($value);
  48. }
  49. $sign_string .= $key.$value;
  50. }
  51. $sign = sha1($sign_string . $secretKey);
  52. return $sign;
  53. }
  54. private function _checkSign($data){
  55. //return true;
  56. if(!is_array($data) ||!$data['sign']){
  57. return false;
  58. }
  59. Util::WriteLog('callback_data',$data);
  60. $sign = $data['sign'];
  61. unset($data['sign']);
  62. return $sign==$this->_sign($data);
  63. }
  64. public function platformInfo(Request $request)
  65. {
  66. $post = $request->post();
  67. //Util::WriteLog('game_test_data',$post);
  68. if (!is_array($post)) {
  69. $post = \GuzzleHttp\json_decode($post, true);
  70. }
  71. if(!$this->_checkSign($post)){
  72. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  73. }
  74. $UserID = $post['user_id'];
  75. $AccountsInfoModel = new AccountsInfo();
  76. // 用户余额
  77. $score = $AccountsInfoModel->Score($UserID);
  78. return ['success' => true,'balance' => intval($score)];
  79. }
  80. public function platformBet(Request $request)
  81. {
  82. $post = $request->post();
  83. if (!is_array($post)) {
  84. $post = \GuzzleHttp\json_decode($post, true);
  85. }
  86. if(!$this->_checkSign($post)){
  87. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  88. }
  89. $res = SetNXLock::getExclusiveLock('bet'.$post['tx_id'], 86400);
  90. if (!$res) {
  91. return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  92. }
  93. $UserID = $post['user_id'];
  94. $AccountsInfoModel = new AccountsInfo();
  95. // 用户余额
  96. $score = intval($AccountsInfoModel->Score($UserID));
  97. $bet = intval($post['amount']);
  98. if($bet>$score){
  99. return ['success' => false,'code' => 5001,'message' => 'Insufficient funds'];
  100. }
  101. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->decrement('Score', $bet);
  102. //TODO 记录下注记录 记下对局id信息
  103. return ['success' => true,'balance' => intval($score)-$bet];
  104. }
  105. public function platformWin(Request $request)
  106. {
  107. $post = $request->post();
  108. if (!is_array($post)) {
  109. $post = \GuzzleHttp\json_decode($post, true);
  110. }
  111. if(!$this->_checkSign($post)){
  112. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  113. }
  114. $res = SetNXLock::getExclusiveLock('win_'.$post['tx_id'], 86400);
  115. if (!$res) {
  116. return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  117. }
  118. $UserID = $post['user_id'];
  119. $AccountsInfoModel = new AccountsInfo();
  120. // 用户余额
  121. $score = intval($AccountsInfoModel->Score($UserID));
  122. $win = intval($post['amount']);
  123. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('Score', $win);
  124. //TODO 记录下注记录 记下对局id信息
  125. return ['success' => true,'balance' => intval($score)+$win];
  126. }
  127. public function platformCancel(Request $request)
  128. {
  129. $post = $request->post();
  130. if (!is_array($post)) {
  131. $post = \GuzzleHttp\json_decode($post, true);
  132. }
  133. if(!$this->_checkSign($post)){
  134. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  135. }
  136. $UserID = $post['user_id'];
  137. $AccountsInfoModel = new AccountsInfo();
  138. // 用户余额
  139. $score = intval($AccountsInfoModel->Score($UserID));
  140. //TODO 记录下注记录 记下对局id信息
  141. return ['success' => true,'balance' => intval($score)];
  142. }
  143. }