AviatrixController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Game\GameCard;
  4. use App\Game\GlobalUserInfo;
  5. use App\Game\LogGamecardClick;
  6. use App\Game\Services\OuroGameService;
  7. use App\Game\Services\PlatformService;
  8. use App\Models\AccountsInfo;
  9. use App\Util;
  10. use App\Utility\SetNXLock;
  11. use Illuminate\Http\Request;
  12. use App\Game\Services\AviatrixService;
  13. use Illuminate\Routing\Controller;
  14. use Illuminate\Support\Facades\Crypt;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Redis;
  17. class AviatrixController extends Controller
  18. {
  19. protected $aviatrixService;
  20. public function __construct(AviatrixService $aviatrixService)
  21. {
  22. $this->aviatrixService = $aviatrixService;
  23. }
  24. /**
  25. * 健康检查接口,不需要认证
  26. */
  27. public function healthCheck()
  28. {
  29. Util::WriteLog('24680_aviatri','healthCheck');
  30. $response = $this->aviatrixService->healthCheck();
  31. return response()->json($response, 200);
  32. }
  33. /**
  34. * 获取玩家信息
  35. * 请求参数:
  36. * - cid: string, 品牌标识
  37. * - sessionToken: string, 会话令牌
  38. * - timestamp: integer, 时间戳 (Unix时间格式)
  39. * 示例请求数据:
  40. * {
  41. * "cid": "somebrand",
  42. * "sessionToken": "abc123",
  43. * "timestamp": 1617181723
  44. * }
  45. */
  46. public function getPlayerInfo(Request $request)
  47. {
  48. $post = $request->post();
  49. $signature = $request->header('X-Auth-Signature');
  50. if(!$this->aviatrixService->checkSignature($post,$signature)){
  51. // return response()->json(['message' => 'Invalid authentication signature'], 400);
  52. }
  53. $sessionToken = $post['sessionToken'];
  54. $sessionToken = str_pad(strtr($sessionToken, '-_', '+/'), strlen($sessionToken) % 4, '=', STR_PAD_RIGHT);
  55. $UserID = Crypt::decryptString($sessionToken);
  56. if(!$UserID){
  57. return response()->json(['message' => 'Invalid session token'], 400);
  58. }
  59. $AccountsInfoModel = new AccountsInfo();
  60. // 用户余额
  61. $score = $AccountsInfoModel->Score($UserID);
  62. $response = ['playerId' => strval($UserID),'country' => 'BR','balance' => intval($score),'currency' => env('CONFIG_24680_CURRENCY')];
  63. return response()->json($response);
  64. }
  65. /**
  66. * 玩家下注接口
  67. * 请求参数:
  68. * - cid: string, 品牌标识
  69. * - sessionToken: string, 会话令牌
  70. * - playerId: string, 玩家标识
  71. * - productId: string, 游戏标识
  72. * - txId: string, 事务标识
  73. * - betId: string, 下注标识
  74. * - roundId: string, 回合标识
  75. * - amount: integer, 下注金额
  76. * - currency: string, 货币类型
  77. * - timestamp: integer, 时间戳 (Unix时间格式)
  78. * - odds: float, 可选, 赔率
  79. * - market: string, 可选, 市场类型
  80. * - outcome: string, 可选, 结果
  81. * - specifier: string, 可选, 规格
  82. * - bonusId: string, 可选, 奖金标识
  83. * - roundClosed: boolean, 可选, 回合是否关闭
  84. * 示例请求数据:
  85. * {
  86. * "cid": "somebrand",
  87. * "sessionToken": "abc123",
  88. * "playerId": "john",
  89. * "productId": "nft-aviatrix",
  90. * "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
  91. * "betId": "bet123",
  92. * "roundId": "round123",
  93. * "amount": 100,
  94. * "currency": "EUR",
  95. * "timestamp": 1617181723,
  96. * "odds": 1.5,
  97. * "market": "win",
  98. * "outcome": "success",
  99. * "specifier": "specifier123",
  100. * "bonusId": "bonus123",
  101. * "roundClosed": true
  102. * }
  103. */
  104. public function placeBet(Request $request)
  105. {
  106. $post = $request->post();
  107. $signature = $request->header('X-Auth-Signature');
  108. if(!$this->aviatrixService->checkSignature($post,$signature)){
  109. // return response()->json(['message' => 'Invalid authentication signature'], 400);
  110. }
  111. $sessionToken = $post['sessionToken'];
  112. $sessionToken = str_pad(strtr($sessionToken, '-_', '+/'), strlen($sessionToken) % 4, '=', STR_PAD_RIGHT);
  113. $UserID = Crypt::decryptString($sessionToken);
  114. if(!$UserID){
  115. return response()->json(['message' => 'Invalid session token'], 400);
  116. }
  117. $res = SetNXLock::getExclusiveLock('bet'.$post['betId'], 86400);
  118. if (!$res) {
  119. Util::WriteLog('24680_aviatrix_bet_error',$post);
  120. return response()->json(['message' => 'Bet limit exceeded'], 403);
  121. }
  122. GameCard::$enableStateCheck=false;
  123. $gamecard=GameCard::where('gid','aviatrix')->first();
  124. $in_gameid = OuroGameService::getUserInGame($UserID);
  125. if(!$gamecard){
  126. $gamecard="notfind :::: ".'aviatrix';
  127. Util::WriteLog('24680game', compact('in_gameid', 'gamecard', 'UserID'));
  128. }else {
  129. if ($in_gameid != intval($gamecard->id)) {
  130. Util::WriteLog('24680game', compact('in_gameid', 'gamecard', 'UserID'));
  131. // die;
  132. }
  133. }
  134. $AccountsInfoModel = new AccountsInfo();
  135. // 用户余额
  136. $score = intval($AccountsInfoModel->Score($UserID));
  137. $bet = intval($post['amount']);
  138. if($bet>$score){
  139. Util::WriteLog('24680_aviatrix_bet_error',[$score,$bet,$UserID]);
  140. return response()->json(['message' => 'Bet limit exceeded'], 403);
  141. }
  142. Redis::set('bet_amount_'.$post['betId'],$bet);
  143. Redis::expire('bet_amount_'.$post['betId'],120);
  144. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->decrement('Score', $bet);
  145. PlatformService::platformBet('aviatrix',$post['productId'],$bet,$UserID);
  146. $response = ['createdAt' => date('Y-m-d\TH:i:s.vO'),'balance' => intval($score)-$bet];
  147. Util::WriteLog('24680_aviatrix_bet_res',['createdAt' => date('Y-m-d\TH:i:s.vO'),'balance' => intval($score)-$bet]);
  148. return response()->json($response);
  149. }
  150. /**
  151. * 处理玩家赢得的奖金
  152. * 请求参数:
  153. * - cid: string, 品牌标识
  154. * - sessionToken: string, 会话令牌
  155. * - playerId: string, 玩家标识
  156. * - productId: string, 游戏标识
  157. * - txId: string, 事务标识
  158. * - betId: string, 下注标识
  159. * - roundId: string, 回合标识
  160. * - amount: integer, 奖金金额
  161. * - currency: string, 货币类型
  162. * - timestamp: integer, 时间戳 (Unix时间格式)
  163. * - operation: string, 操作类型 (例如 SettleBet, CancelBet)
  164. * - odds: float, 可选, 赔率
  165. * - market: string, 可选, 市场类型
  166. * - outcome: string, 可选, 结果
  167. * - specifier: string, 可选, 规格
  168. * - bonusId: string, 可选, 奖金标识
  169. * - roundClosed: boolean, 可选, 回合是否关闭
  170. * 示例请求数据:
  171. * {
  172. * "cid": "somebrand",
  173. * "sessionToken": "abc123",
  174. * "playerId": "john",
  175. * "productId": "nft-aviatrix",
  176. * "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
  177. * "betId": "bet123",
  178. * "roundId": "round123",
  179. * "amount": 500,
  180. * "currency": "EUR",
  181. * "timestamp": 1617181723,
  182. * "operation": "SettleBet",
  183. * "odds": 1.5,
  184. * "market": "win",
  185. * "outcome": "success",
  186. * "specifier": "specifier123",
  187. * "bonusId": "bonus123",
  188. * "roundClosed": true
  189. */
  190. public function processWin(Request $request)
  191. {
  192. $post = $request->post();
  193. Util::WriteLog('24680_aviatrix_win',$post);
  194. $signature = $request->header('X-Auth-Signature');
  195. if(!$this->aviatrixService->checkSignature($post,$signature)){
  196. // return response()->json(['message' => 'Invalid authentication signature'], 400);
  197. }
  198. $sessionToken = $post['sessionToken'];
  199. $sessionToken = str_pad(strtr($sessionToken, '-_', '+/'), strlen($sessionToken) % 4, '=', STR_PAD_RIGHT);
  200. $UserID = Crypt::decryptString($sessionToken);
  201. if(!$UserID){
  202. return response()->json(['message' => 'Invalid session token'], 400);
  203. }
  204. $res = SetNXLock::getExclusiveLock('win_'.$post['betId'], 86400);
  205. if (!$res) {
  206. Util::WriteLog('24680_win_error',$post);
  207. return response()->json(['message' => 'Invalid session token'], 400);
  208. }
  209. $AccountsInfoModel = new AccountsInfo();
  210. // 用户余额
  211. $score = intval($AccountsInfoModel->Score($UserID));
  212. $win = intval($post['amount']);
  213. $bet = Redis::get('bet_amount_'.$post['betId'])?:0;
  214. //Util::WriteLog('win_data',$post);
  215. if(true){
  216. PlatformService::platformWin($UserID,'aviatrix',$post['productId'],$win,$bet,$score+$win);
  217. }
  218. $response = ['createdAt' => date('Y-m-d\TH:i:s.vO'),'balance' => intval($score)+$win];
  219. return response()->json($response);
  220. }
  221. /**
  222. * 处理玩家的促销奖金
  223. * 请求参数:
  224. * - cid: string, 品牌标识
  225. * - sessionToken: string, 会话令牌
  226. * - playerId: string, 玩家标识
  227. * - txId: string, 事务标识
  228. * - amount: integer, 奖金金额
  229. * - currency: string, 货币类型
  230. * 示例请求数据:
  231. * {
  232. * "cid": "somebrand",
  233. * "sessionToken": "abc123",
  234. * "playerId": "john",
  235. * "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
  236. * "amount": 100,
  237. * "currency": "EUR"
  238. * }
  239. */
  240. public function promoWin(Request $request)
  241. {
  242. $data = $request->validate([
  243. 'cid' => 'required|string',
  244. 'sessionToken' => 'required|string',
  245. 'playerId' => 'required|string',
  246. 'txId' => 'required|string',
  247. 'amount' => 'required|integer',
  248. 'currency' => 'required|string',
  249. ]);
  250. $response = $this->aviatrixService->promoWin($data);
  251. return response()->json($response);
  252. }
  253. /**
  254. * 关闭比赛
  255. * 请求参数:
  256. * - cid: string, 品牌标识
  257. * - playerId: string, 玩家标识
  258. * - productId: string, 游戏标识
  259. * - matchId: string, 比赛标识
  260. * - txId: string, 事务标识
  261. */
  262. public function closeMatch(Request $request)
  263. {
  264. $data = $request->validate([
  265. 'cid' => 'required|string',
  266. 'playerId' => 'required|string',
  267. 'productId' => 'required|string',
  268. 'matchId' => 'required|string',
  269. 'txId' => 'required|string',
  270. ]);
  271. $response = $this->aviatrixService->closeMatch($data);
  272. return response()->json($response);
  273. }
  274. public function gameLunch(Request $request)
  275. {
  276. $gid=$request->input('gid');
  277. $level=$request->input('level',1);
  278. $user = $request->user();
  279. $userid=$user->UserID;
  280. GameCard::$enableStateCheck=false;
  281. $gamecard=GameCard::where('gid',$gid)->first();
  282. $in_gameid=OuroGameService::getUserInGame($userid,$user->GlobalUID);
  283. if($in_gameid!=intval($gamecard->id)){
  284. Util::WriteLog('24680game',compact('in_gameid','gamecard','user'));
  285. // die;
  286. }
  287. $lang=GlobalUserInfo::getLocale();
  288. $clientLang='en';
  289. if($lang=='pt')$clientLang='pt';
  290. else if($lang=='es')$clientLang='es';
  291. else $clientLang='en';;
  292. $url = $this->aviatrixService->launchGame($userid, $clientLang);
  293. LogGamecardClick::recordClick($gamecard->id,$userid);
  294. echo "<script>
  295. parent.postMessage({cmd:\"closeLoading\"},\"*\");
  296. window.location.href='".$url."';
  297. </script>";
  298. exit();
  299. }
  300. public function lunchGame(Request $request)
  301. {
  302. $userid=$request->input('uid',4688302);
  303. $url = $this->aviatrixService->launchGame($userid, 'pt');
  304. echo "<script>
  305. parent.postMessage({cmd:\"closeLoading\"},\"*\");
  306. window.location.href='".$url."';
  307. </script>";
  308. exit();
  309. }
  310. }