2
0

PPlayController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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\LogGamecardClick;
  7. use App\Game\Services\OuroGameService;
  8. use App\Game\Services\PgSoftService;
  9. use App\Game\Services\PlatformService;
  10. use App\Game\Services\PPlayService;
  11. use App\Game\Services\ServerService;
  12. use App\Http\helper\NumConfig;
  13. use App\Models\AccountsInfo;
  14. use App\Notification\TelegramBot;
  15. use App\Util;
  16. use App\Utility\SetNXLock;
  17. use GuzzleHttp\Client;
  18. use GuzzleHttp\Exception\RequestException;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Routing\Controller;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Redis;
  23. class PPlayController extends Controller
  24. {
  25. protected $ppSoftService;
  26. protected $client;
  27. protected $currency='BRL';
  28. public function __construct(PPlayService $ppSoftService)
  29. {
  30. $this->ppSoftService = $ppSoftService;
  31. $this->client = new Client();
  32. $this->currency=env('CONFIG_24680_CURRENCY',$this->currency);
  33. }
  34. private function handleRequestException(\Exception $e)
  35. {
  36. TelegramBot::getDefault()->sendMsgWithEnv($e->getMessage().$e->getTraceAsString());
  37. }
  38. /** Set API response locale from request (lang param or Accept-Language) for description translation */
  39. private function setApiLocale(Request $request)
  40. {
  41. $lang = $request->get('lang') ?: $request->header('Accept-Language', '');
  42. if ($lang && preg_match('/^es/i', $lang)) {
  43. \App::setLocale('es');
  44. } else {
  45. \App::setLocale('en');
  46. }
  47. }
  48. /** Get translated API description by key */
  49. private function desc($key)
  50. {
  51. return trans('messages.api.description.' . $key);
  52. }
  53. private function callSubApi($ext_player_id,$request)
  54. {
  55. $apiurl=ServerService::GetApiByGUID($ext_player_id);
  56. try {
  57. // 获取当前请求的 GET 和 POST 数据
  58. // $getData = $request->query(); // 获取 GET 数据
  59. $postData = $request->post(); // 获取 POST 数据
  60. $response = $this->client->post( $apiurl . $_SERVER['REQUEST_URI'], [
  61. 'verify'=>false,
  62. // 'query' => $getData, // 传递 GET 数据
  63. 'form_params' => $postData, // 传递 POST 数据
  64. ]);
  65. $res=json_decode($response->getBody(),true);
  66. Util::WriteLog('pgsub',$res);
  67. return $res;
  68. } catch (RequestException $e) {
  69. return $this->handleRequestException($e);
  70. }
  71. }
  72. public function backLobby()
  73. {
  74. echo "<script>
  75. parent.postMessage({cmd:\"back\"},\"*\");
  76. </script>";
  77. }
  78. public function gameLunch(Request $request)
  79. {
  80. $gid=$request->input('gid');
  81. $user = $request->user();
  82. $userid=$user->UserID;
  83. if($user->Channel!=99){
  84. http_response_code(404);
  85. exit();
  86. }
  87. GameCard::$enableStateCheck=false;
  88. $gamecard=GameCard::where('gid',$gid)->first();
  89. $in_gameid=OuroGameService::getUserInGame($userid,$user->GlobalUID);
  90. if($in_gameid!=intval($gamecard->id)){
  91. Util::WriteLog('24680game',compact('in_gameid','gamecard','user'));
  92. // die;
  93. }
  94. $lang=GlobalUserInfo::getLocale();
  95. $supportLang = ['en','da','de','es','fi','fr','id','it','ja','ko','nl','no','pl','pt','ro','ru','sv','th','tr','vi','zh','my'];
  96. if(!in_array($lang,$supportLang)){
  97. $lang = 'en';
  98. }
  99. $rs = $this->ppSoftService->getLaunchURL($gid,$user->GlobalUID,$user->GlobalUID,$this->currency,$lang);
  100. if(@$rs['gameURL']){
  101. $gamecard=GameCard::where('gid',$gid)->where('brand','PP')->first();
  102. $gamecard->increment('play_num',1);
  103. //$this->logGameClick($gamecard->id,$userid);
  104. LogGamecardClick::recordClick($gamecard->id,$userid);
  105. echo "<script>
  106. parent.postMessage({cmd:\"closeLoading\"},\"*\");
  107. window.location.href='".$rs['gameURL']."';
  108. </script>";
  109. exit();
  110. }else{
  111. echo "<script>alert('system wrong')</script>";
  112. exit();
  113. }
  114. }
  115. public function gameLogin(Request $request)
  116. {
  117. $gid = $request->input('gid','vs20olympx');
  118. $guid = $request->input('guid','0685128230-b53b-eb1a-0004688302');
  119. $uid = $request->input('uid','4688302');
  120. $lang=GlobalUserInfo::getLocale();
  121. $supportLang = ['en','da','de','es','fi','fr','id','it','ja','ko','nl','no','pl','pt','ro','ru','sv','th','tr','vi','zh','my'];
  122. if(!in_array($lang,$supportLang)){
  123. $lang = 'en';
  124. }
  125. $rs = $this->ppSoftService->getLaunchURL($gid,$guid,$guid,$this->currency,$lang);
  126. echo "<script>
  127. window.location.href='".$rs['gameURL']."';
  128. </script>";
  129. // var_dump($rs);
  130. }
  131. public function gameList()
  132. {
  133. $response = $this->ppSoftService->getCasinoGames();
  134. $gameList = $response['gameList'];
  135. foreach ($gameList as &$game) {
  136. unset($game['jurisdictions']);
  137. }
  138. return response()->json($gameList);
  139. }
  140. public function gameLobbyList()
  141. {
  142. $response = $this->ppSoftService->getLobbyGames();
  143. return response()->json($response);
  144. }
  145. public function authenticate(Request $request){
  146. $this->setApiLocale($request);
  147. $post = $request->all();
  148. Util::WriteLog('pp_authenticate',$post);
  149. if(!$this->ppSoftService->checkHash($post)){
  150. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  151. }
  152. $token = $post['token'];
  153. if(!ServerService::IsLocalUser($token)) {
  154. return $this->callSubApi($token,$request);
  155. }
  156. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $token);
  157. if (!$user) return response()->json(['error' => 2,'description' => $this->desc('player_not_found')]);
  158. $response = [
  159. 'userId' => $token,//$user->UserID,
  160. 'currency' => $this->currency,
  161. 'cash' => GlobalUserInfo::getScoreByUserID($user->UserID),
  162. 'bonus' => 0,
  163. 'error' => 0,
  164. 'description' => $this->desc('success')
  165. ];
  166. return response()->json($response);
  167. }
  168. public function balance(Request $request)
  169. {
  170. $this->setApiLocale($request);
  171. $post = $request->all();
  172. if(!ServerService::IsLocalUser($post['userId'])) {
  173. return $this->callSubApi($post['userId'],$request);
  174. }
  175. $UserID=ServerService::GlobalToUserID($post['userId']);
  176. Util::WriteLog('pp_balance',$post);
  177. if(!$this->ppSoftService->checkHash($post)){
  178. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  179. }
  180. $response = [
  181. 'currency' => $this->currency,
  182. 'cash' => GlobalUserInfo::getScoreByUserID($UserID),
  183. 'bonus' => 0,
  184. 'error' => 0,
  185. 'description' => $this->desc('success')
  186. ];
  187. return response()->json($response);
  188. }
  189. public function bet(Request $request)
  190. {
  191. $this->setApiLocale($request);
  192. $post = $request->all();
  193. if(!ServerService::IsLocalUser($post['userId'])) {
  194. return $this->callSubApi($post['userId'],$request);
  195. }
  196. $UserID=ServerService::GlobalToUserID($post['userId']);
  197. Util::WriteLog('pp_bet',$post);
  198. if(!$this->ppSoftService->checkHash($post)){
  199. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  200. }
  201. $transactionId = $post['reference'];
  202. $score = GlobalUserInfo::getScoreByUserID($UserID);
  203. $res = SetNXLock::getExclusiveLock('pp_bet'.$transactionId, 600);
  204. if (!$res) {
  205. Util::WriteLog('pp_bet_repeat',$post);
  206. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' => $score,'bonus' => 0,'usedPromo' => 0,'error' => 0,'description' => $this->desc('success')];
  207. }
  208. if(round($post['amount'],2)>round($score,2)){
  209. return response()->json(['error' => 1,'description' => $this->desc('insufficient_balance')]);
  210. }
  211. $bet = intval($post['amount']*NumConfig::NUM_VALUE);
  212. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->decrement('Score', $bet);
  213. PlatformService::platformBet('pp',$post['gameId'],$bet,$UserID);
  214. $betKey = 'pp_bet_amount_'.$transactionId;
  215. Redis::set($betKey,$bet);
  216. Redis::expire($betKey,600);
  217. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' =>round($score-$post['amount'],2),'bonus' => 0,'usedPromo' => 0,'error' => 0,'description' => $this->desc('success')];
  218. }
  219. public function result(Request $request)
  220. {
  221. $this->setApiLocale($request);
  222. $post = $request->all();
  223. if(!ServerService::IsLocalUser($post['userId'])) {
  224. return $this->callSubApi($post['userId'],$request);
  225. }
  226. $UserID=ServerService::GlobalToUserID($post['userId']);
  227. Util::WriteLog('pp_result',$post);
  228. if(!$this->ppSoftService->checkHash($post)){
  229. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  230. }
  231. $transactionId = $post['reference'];
  232. $score = GlobalUserInfo::getScoreByUserID($UserID);
  233. $res = SetNXLock::getExclusiveLock('pp_result'.$transactionId, 600);
  234. if (!$res) {
  235. Util::WriteLog('pp_win_repeat',$post);
  236. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' => $score,'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  237. }
  238. $betKey = 'pp_bet_amount_'.$transactionId;
  239. $bet = Redis::get($betKey)?:0;
  240. Redis::del($betKey);
  241. $win = $post['amount'];
  242. if(isset($post['promoWinAmount'])){
  243. if($post['promoWinAmount']>0){
  244. $win = round($post['promoWinAmount']+$post['amount'],2);
  245. }
  246. }
  247. PlatformService::platformWin($UserID,'pp',$post['gameId'],intval($win*NumConfig::NUM_VALUE),$bet,intval($score+$win)*NumConfig::NUM_VALUE);
  248. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' =>round($score+$win,2),'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  249. }
  250. public function bonusWin(Request $request)
  251. {
  252. $this->setApiLocale($request);
  253. $post = $request->all();
  254. if(!ServerService::IsLocalUser($post['userId'])) {
  255. return $this->callSubApi($post['userId'],$request);
  256. }
  257. $UserID=ServerService::GlobalToUserID($post['userId']);
  258. Util::WriteLog('pp_bonusWin',$post);
  259. if(!$this->ppSoftService->checkHash($post)){
  260. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  261. }
  262. $transactionId = $post['reference'];
  263. $score = GlobalUserInfo::getScoreByUserID($UserID);
  264. // $res = SetNXLock::getExclusiveLock('pp_bonusWin'.$transactionId, 600);
  265. // if (!$res) {
  266. // Util::WriteLog('pp_bonusWin_repeat',$post);
  267. // return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' => $score,'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  268. // }
  269. //
  270. // PlatformService::platformWin($UserID,'pp','bonusWin',intval($post['amount']*NumConfig::NUM_VALUE),0,intval($score+$post['amount'])*NumConfig::NUM_VALUE);
  271. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' =>round($score,2),'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  272. }
  273. public function jackpotWin(Request $request)
  274. {
  275. $this->setApiLocale($request);
  276. $post = $request->all();
  277. if(!ServerService::IsLocalUser($post['userId'])) {
  278. return $this->callSubApi($post['userId'],$request);
  279. }
  280. $UserID=ServerService::GlobalToUserID($post['userId']);
  281. Util::WriteLog('pp_jackpotWin',$post);
  282. if(!$this->ppSoftService->checkHash($post)){
  283. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  284. }
  285. $transactionId = $post['reference'];
  286. $score = GlobalUserInfo::getScoreByUserID($UserID);
  287. $res = SetNXLock::getExclusiveLock('pp_jackpotWin'.$transactionId, 600);
  288. if (!$res) {
  289. Util::WriteLog('pp_jackpotWin_repeat',$post);
  290. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' => $score,'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  291. }
  292. PlatformService::platformWin($UserID,'pp',@$post['gameId'],intval($post['amount']*NumConfig::NUM_VALUE),0,intval($score+$post['amount'])*NumConfig::NUM_VALUE);
  293. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' =>round($score+$post['amount'],2),'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  294. }
  295. public function refund(Request $request)
  296. {
  297. $this->setApiLocale($request);
  298. $post = $request->all();
  299. if(!ServerService::IsLocalUser($post['userId'])) {
  300. return $this->callSubApi($post['userId'],$request);
  301. }
  302. $UserID=ServerService::GlobalToUserID($post['userId']);
  303. Util::WriteLog('pp_refund',$post);
  304. if(!$this->ppSoftService->checkHash($post)){
  305. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  306. }
  307. $transactionId = $post['reference'];
  308. $betKey = 'pp_bet_amount_'.$transactionId;
  309. $bet = Redis::get($betKey)?:0;
  310. if($bet){
  311. Redis::del($betKey);
  312. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('Score', $bet);
  313. }
  314. return ['transactionId' => $transactionId,'error' => 0,'description' => $this->desc('success')];
  315. }
  316. public function promoWin(Request $request)
  317. {
  318. $this->setApiLocale($request);
  319. $post = $request->all();
  320. if(!ServerService::IsLocalUser($post['userId'])) {
  321. return $this->callSubApi($post['userId'],$request);
  322. }
  323. $UserID=ServerService::GlobalToUserID($post['userId']);
  324. Util::WriteLog('pp_promoWin',$post);
  325. if(!$this->ppSoftService->checkHash($post)){
  326. return response()->json(['error' => 5,'description' => $this->desc('invalid_hash')]);
  327. }
  328. $transactionId = $post['reference'];
  329. $score = GlobalUserInfo::getScoreByUserID($UserID);
  330. $res = SetNXLock::getExclusiveLock('pp_promoWin'.$transactionId, 600);
  331. if (!$res) {
  332. Util::WriteLog('pp_promoWin_repeat',$post);
  333. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' => $score,'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  334. }
  335. PlatformService::platformWin($UserID,'pp',@$post['gameId'],intval($post['amount']*NumConfig::NUM_VALUE),0,intval($score+$post['amount'])*NumConfig::NUM_VALUE);
  336. return ['transactionId' => $transactionId,'currency' => $this->currency,'cash' =>round($score+$post['amount'],2),'bonus' => 0,'error' => 0,'description' => $this->desc('success')];
  337. }
  338. }