PPlayTestController.php 12 KB

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