OnlyGameController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Facade\TableName;
  4. use App\Game\Config\GameBasicConfig;
  5. use App\Game\GameCard;
  6. use App\Game\GlobalUserInfo;
  7. use App\Game\LogGamecardClick;
  8. use App\Game\Route;
  9. use App\Game\Services\OuroGameService;
  10. use App\Game\Services\PlatformService;
  11. use App\Http\Controllers\Api\ApiController;
  12. use App\Http\Controllers\Controller;
  13. use App\Models\AccountsInfo;
  14. use App\Services\IpCheck;
  15. use App\Util;
  16. use App\Utility\SetNXLock;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Redis;
  20. // use Yansongda\Pay\Log;
  21. class OnlyGameController extends Controller
  22. {
  23. public function gameLunch(Request $request)
  24. {
  25. $gid=$request->input('gid');
  26. $level=$request->input('level',1);
  27. $user = $request->user();
  28. $userid=$user->UserID;
  29. if($user->Channel!=99){
  30. http_response_code(404);
  31. exit();
  32. }
  33. GameCard::$enableStateCheck=false;
  34. $gamecard=GameCard::where('gid',$gid)->first();
  35. $in_gameid=OuroGameService::getUserInGame($userid,$user->GlobalUID);
  36. if($in_gameid!=intval($gamecard->id)){
  37. Util::WriteLog('24680game',compact('in_gameid','gamecard','user'));
  38. // die;
  39. }
  40. $lang=GlobalUserInfo::getLocale();
  41. $clientLang='en';
  42. if($lang=='pt')$clientLang='pt';
  43. else if($lang=='es')$clientLang='es';
  44. else $clientLang='en';;
  45. $api = 'https://int.onlyplay.net/api/get_frame';
  46. $AccountsInfoModel = new AccountsInfo();
  47. // 用户余额
  48. $score = $AccountsInfoModel->Score($userid);
  49. $baseInfo = [
  50. 'game_bundle' => $gid,
  51. 'callback_url' => GameBasicConfig::$ApiServer,
  52. 'partner_id' => 949,
  53. 'user_id' => strval($userid),
  54. 'balance' => intval($score),
  55. 'currency' => env('CONFIG_24680_CURRENCY'),
  56. 'decimals' => 2,
  57. 'lang' => $clientLang,
  58. 'token' => sha1(time())
  59. ];
  60. $baseInfo['sign'] = $this->_sign($baseInfo);
  61. $headers = [
  62. 'Content-Type' => 'application/json',
  63. ];
  64. $retryTimes=0;
  65. $rs=null;
  66. while ($rs==null&&$retryTimes<3) {
  67. try {
  68. $rs = Util::curlPost2($api, json_encode($baseInfo), true, $headers);
  69. $rs = json_decode($rs, true);
  70. }catch (\Exception $e){
  71. }
  72. usleep(100*1000);
  73. $retryTimes++;
  74. }
  75. $url = $rs['url'];
  76. if($url){
  77. $gamecard=GameCard::where('gid',$gid)->where('brand','OnlyPlay')->first();
  78. //$gamecard->increment('play_num',1);
  79. //$this->logGameClick($gamecard->id,$userid);
  80. LogGamecardClick::recordClick($gamecard->id,$userid);
  81. //LogGamecardClick::recordClick($gameid,$UserID);
  82. //dd($rs);exit();
  83. echo "<script>
  84. parent.postMessage({cmd:\"closeLoading\"},\"*\");
  85. window.location.href='".$url."';
  86. </script>";
  87. //header("Location:$url");
  88. exit();
  89. }else{
  90. echo "<script>alert('system wrong')</script>";
  91. exit();
  92. }
  93. }
  94. private function _sign($params)
  95. {
  96. $secretKey = 'j7OEDKE4Gd1r1PYO7rni13wA3iapu1QPVoG6NoklJoe74j3hCAh84NutR1JG';
  97. ksort($params);
  98. $sign_string = '';
  99. foreach ($params as $key => $value) {
  100. if(is_array($value)){
  101. $value = json_encode($value);
  102. }
  103. if($value === true){
  104. $value = 1;
  105. }
  106. if($value === false){
  107. $value = 0;
  108. }
  109. $sign_string .= $key.$value;
  110. }
  111. $sign = sha1($sign_string . $secretKey);
  112. return $sign;
  113. }
  114. private function _checkSign($data){
  115. //return true;
  116. Util::WriteLog('callback_data',$data);
  117. if(!is_array($data) ||!@$data['sign']){
  118. Util::WriteLog('24680_sign_error',$data);
  119. return false;
  120. }
  121. $sign = $data['sign'];
  122. unset($data['sign']);
  123. return $sign==$this->_sign($data);
  124. }
  125. public function platformInfo(Request $request)
  126. {
  127. $post = $request->post();
  128. //Util::WriteLog('game_test_data',$post);
  129. if (!is_array($post)) {
  130. $post = \GuzzleHttp\json_decode($post, true);
  131. }
  132. if(!$this->_checkSign($post)){
  133. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  134. }
  135. $UserID = $post['user_id'];
  136. $AccountsInfoModel = new AccountsInfo();
  137. // 用户余额
  138. $score = $AccountsInfoModel->Score($UserID);
  139. return ['success' => true,'balance' => intval($score)];
  140. }
  141. public function platformBet(Request $request)
  142. {
  143. $post = $request->post();
  144. if (!is_array($post)) {
  145. $post = \GuzzleHttp\json_decode($post, true);
  146. }
  147. if(!$this->_checkSign($post)){
  148. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  149. }
  150. $UserID = $post['user_id'];
  151. $AccountsInfoModel = new AccountsInfo();
  152. // 用户余额
  153. $score = intval($AccountsInfoModel->Score($UserID));
  154. $res = SetNXLock::getExclusiveLock('bet'.$post['tx_id'], 86400);
  155. if (!$res) {
  156. Util::WriteLog('24680_bet_error',$post);
  157. return ['success' => true,'balance' => intval($score)];
  158. //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  159. }
  160. GameCard::$enableStateCheck=false;
  161. $gamecard=GameCard::where('gid',$post['game_bundle'])->first();
  162. $in_gameid = OuroGameService::getUserInGame($UserID);
  163. if(!$gamecard){
  164. $gamecard="notfind :::: ".$post['game_bundle'];
  165. Util::WriteLog('24680game', compact('in_gameid', 'gamecard', 'UserID'));
  166. }else {
  167. if ($in_gameid != intval($gamecard->id)) {
  168. Util::WriteLog('24680game', compact('in_gameid', 'gamecard', 'UserID'));
  169. // die;
  170. }
  171. }
  172. $bet = intval($post['amount']);
  173. if($bet>$score){
  174. return ['success' => false,'code' => 5001,'message' => 'Insufficient funds'];
  175. }
  176. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->decrement('Score', $bet);
  177. //用户cancel的时候
  178. Redis::set('bet_amount_'.$post['tx_id'],$bet);
  179. Redis::expire('bet_amount_'.$post['tx_id'],120);
  180. PlatformService::platformBet('only',$post['game_bundle'],$bet,$UserID);
  181. Util::WriteLog('24680_only_bet_success',array_merge($post, ['success' => true,'balance' => intval($score)-$bet]));
  182. return ['success' => true,'balance' => intval($score)-$bet];
  183. }
  184. public function platformWin(Request $request)
  185. {
  186. $post = $request->post();
  187. if (!is_array($post)) {
  188. $post = \GuzzleHttp\json_decode($post, true);
  189. }
  190. if(!$this->_checkSign($post)){
  191. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  192. }
  193. $UserID = $post['user_id'];
  194. $AccountsInfoModel = new AccountsInfo();
  195. // 用户余额
  196. $score = intval($AccountsInfoModel->Score($UserID));
  197. $res = SetNXLock::getExclusiveLock('win_'.$post['tx_id'], 86400);
  198. if (!$res) {
  199. Util::WriteLog('24680_win_error',$post);
  200. //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  201. return ['success' => true,'balance' => intval($score)];
  202. }
  203. $betKey = 'bet_amount_'.$post['ref_tx_id'];
  204. $win = intval($post['amount']);
  205. $bet = Redis::get($betKey)?:0;
  206. if(true){
  207. PlatformService::platformWin($UserID,'only',$post['game_bundle'],$win,$bet,$score+$win);
  208. }
  209. Redis::del($betKey);
  210. Util::WriteLog('24680_only_win_success',array_merge($post, ['success' => true,'balance' => intval($score)+$win]));
  211. return ['success' => true,'balance' => intval($score)+$win];
  212. }
  213. public function platformCancel(Request $request)
  214. {
  215. $post = $request->post();
  216. if (!is_array($post)) {
  217. $post = \GuzzleHttp\json_decode($post, true);
  218. }
  219. if(!$this->_checkSign($post)){
  220. return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
  221. }
  222. $UserID = $post['user_id'];
  223. $AccountsInfoModel = new AccountsInfo();
  224. // 用户余额
  225. $score = intval($AccountsInfoModel->Score($UserID));
  226. $bet=0;
  227. $key='bet_amount_'.$post['ref_tx_id'];
  228. if(Redis::exists($key)){
  229. $bet=Redis::get($key);
  230. if($bet>0){
  231. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('Score', $bet);
  232. PlatformService::platformBet('only',$post['game_bundle'],-$bet,$UserID);
  233. }
  234. Redis::del($key);
  235. }
  236. $score+=$bet;
  237. Util::WriteLog('24680_only_cancel_success',array_merge($post, ['success' => true,'balance' => intval($score)]));
  238. return ['success' => true,'balance' => intval($score)];
  239. }
  240. public function roundInfo(Request $request){
  241. $api = 'https://int.onlyplay.net/api/get_frame';
  242. $baseInfo = [
  243. 'round_id' => $request->input('round'),
  244. 'partner_id' => 949,
  245. 'date' => date('Y-m-d')
  246. ];
  247. $baseInfo['sign'] = $this->_sign($baseInfo);
  248. $headers = [
  249. 'Content-Type' => 'application/json',
  250. ];
  251. $rs = Util::curlPost2($api,json_encode($baseInfo),true,$headers);
  252. dd($rs);
  253. }
  254. }