Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.90 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4. use App\Deal;
  5. use App\User;
  6. use App\Team;
  7. use DateTime;
  8. use App\Match;
  9. use App\Coupon;
  10. use Illuminate\Support\Facades\Auth;
  11. use Carbon\Carbon;
  12. use GuzzleHttp;
  13. use Goutte\Client;
  14.  
  15.  
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Session;
  18.  
  19. class IndexController extends Controller
  20. {
  21. public function index()
  22. {
  23. if(Auth::check())
  24. {
  25. return redirect('/bet');
  26. }
  27. return view('index');
  28. }
  29.  
  30. public function main()
  31. {
  32. $currentUser = User::find(Auth::id());
  33. $matches = Match::orderBy('kickoff_date','ASC')->get()->groupBy(function($match,$index) {
  34. return Carbon::parse($match->kickoff_date)->format('d/m');
  35. });
  36. date_default_timezone_set('Asia/Ho_Chi_Minh');
  37. $now = Carbon::now('Asia/Ho_Chi_Minh')->format('d/m');
  38. $listDate = array();
  39. foreach ($matches as $key => $value){
  40. array_push($listDate,$key);
  41. }
  42. $totalDate = 3;
  43. $finalMatches = array();
  44. $showMatches = array();
  45. $firstDayMatches = array();
  46. $firstDayTeams = array();
  47. $savedIndex = count($listDate)-1;
  48. $activeTab = 0;
  49. // Fixed with total show date equal with 3 ?? :D ??
  50. if($this->compareDayonMonth($listDate[count($listDate)-1],$now) >= 0){
  51. for($i = 0; $i <count($listDate); $i++)
  52. {
  53. if($this->compareDayonMonth($listDate[$i],$now) >= 0 && count($finalMatches) < $totalDate )
  54. {
  55. $savedIndex = $i;
  56. break;
  57. }
  58. }
  59. if($savedIndex < count($listDate) - 1)
  60. {
  61. if($savedIndex == 0)
  62. {
  63. for($i = $savedIndex; $i < $totalDate;$i++)
  64. {
  65. array_push($finalMatches,$listDate[$i]);
  66. array_push($showMatches,$matches[$listDate[$i]]);
  67. }
  68. $activeTab = 0;
  69. }else{
  70. array_push($finalMatches,$listDate[$savedIndex - 1]);
  71. array_push($showMatches,$matches[$listDate[$savedIndex - 1]]);
  72. array_push($finalMatches,$listDate[$savedIndex - 0]);
  73. array_push($showMatches,$matches[$listDate[$savedIndex - 0]]);
  74. array_push($finalMatches,$listDate[$savedIndex + 1]);
  75. array_push($showMatches,$matches[$listDate[$savedIndex + 1]]);
  76.  
  77. $activeTab = 1;
  78. }
  79. }else{
  80. for($i = $savedIndex - 2; $i <= $savedIndex; $i++ )
  81. {
  82. array_push($finalMatches,$listDate[$i]);
  83. array_push($showMatches,$matches[$listDate[$i]]);
  84. }
  85. $activeTab = 2;
  86. }
  87. }else{
  88. for ($i = count($listDate)-1; $i > count($listDate) - 1 - $totalDate; $i--)
  89. {
  90. array_push($finalMatches,$listDate[$i]);
  91. array_push($showMatches,$matches[$listDate[$i]]);
  92. }
  93. $finalMatches = array_reverse($finalMatches);
  94. $showMatches = array_reverse($showMatches);
  95. }
  96. // Old
  97. // if($this->compareDayonMonth($listDate[count($listDate)-1],$now) >= 0)
  98. // {
  99. // for($i = 0; $i <count($listDate); $i++)
  100. // {
  101. // if($this->compareDayonMonth($listDate[$i],$now) >= 0 && count($finalMatches) < $totalDate )
  102. // {
  103. // array_push($finalMatches,$listDate[$i]);
  104. // array_push($showMatches,$matches[$listDate[$i]]);
  105. // if(count($finalMatches)==1)
  106. // {
  107. // $savedIndex = $i;
  108. // }
  109. // }
  110. // }
  111. // $finalMatches = array_reverse($finalMatches);
  112. // $showMatches = array_reverse($showMatches);
  113. // $activeTab = $totalDate - count($finalMatches);
  114. // if(count($finalMatches) < $totalDate)
  115. // {
  116. // $remainedItem = $totalDate - count($finalMatches);
  117. // for($i = $savedIndex-1; $i >= $savedIndex - $remainedItem ;$i--)
  118. // {
  119. // array_push($finalMatches,$listDate[$i]);
  120. // array_push($showMatches,$matches[$listDate[$i]]);
  121. // }
  122. // }
  123. //
  124. // }else{
  125. // for($i = count($listDate) - 1;$i > 0; $i--)
  126. // {
  127. // if( count($finalMatches) < $totalDate)
  128. // {
  129. // array_push($finalMatches,$listDate[$i]);
  130. // array_push($showMatches,$matches[$listDate[$i]]);
  131. // }
  132. // }
  133. // }
  134. //
  135. $totalMatches = $showMatches;
  136. $showMatches = $showMatches[$activeTab];
  137. foreach($showMatches as $match)
  138. {
  139. $team_1 = Team::where('short_name',$match['team_1'])->get();
  140. $team_2 = Team::where('short_name',$match['team_2'])->get();
  141. array_push($firstDayMatches,$match);
  142. array_push($firstDayTeams,[$team_1[0],$team_2[0]]);
  143. }
  144. $activeMatch = $matches[$finalMatches[$activeTab]][0];
  145. $team_1 = Team::where('short_name',$activeMatch['team_1'])->get();
  146. $team_2 = Team::where('short_name',$activeMatch['team_2'])->get();
  147. /* Check is deal or not */
  148. date_default_timezone_set('Asia/Ho_Chi_Minh');
  149. $now = Carbon::now('UTC');
  150. $compareDate = Carbon::parse($activeMatch['kickoff_date']);
  151. $is_finished = intval($activeMatch['is_finished']);
  152. if($now > $compareDate || $is_finished){
  153. $canVote = 'disabled';
  154. }else{
  155. $canVote = '';
  156. }
  157. $deal = Deal::where('match_id',$activeMatch['id'])
  158. ->where('user_id',$currentUser->id)
  159. ->get();
  160. if(count($deal)){
  161. $deal = $deal->first()->toArray();
  162. $votedTeam = $deal['voted_team'];
  163. }else{
  164. $votedTeam = "";
  165. }
  166. /* Statistic */
  167. $countTeam1 = count(Deal::where('match_id',$activeMatch['id'])
  168. ->where('voted_team',$activeMatch['team_1'])
  169. ->get());
  170. $countTeam2 = count(Deal::where('match_id',$activeMatch['id'])
  171. ->where('voted_team',$activeMatch['team_2'])
  172. ->get());
  173.  
  174. /* Check if match finis */
  175. $award = false;
  176. $correctCount = 0;
  177. $winner = "";
  178. if($is_finished)
  179. {
  180. $result = $activeMatch['result'];
  181. $goals = explode('-',$result);
  182. $left_goal = $goals[0];
  183. $right_goal = $goals[1];
  184. $deal = Deal::where('match_id',$activeMatch['id'])
  185. ->where('user_id',$currentUser->id)->get();
  186. if(floatval($goals[0])>floatval($goals[1]) + floatval($activeMatch['condition']))
  187. {
  188. $winner = $activeMatch['team_1'];
  189. }
  190. if(floatval($goals[0])<floatval($goals[1]) + floatval($activeMatch['condition']))
  191. {
  192. $winner = $activeMatch['team_2'];
  193. }
  194. $correctDeal = Deal::where('match_id',$activeMatch['id'])
  195. ->where('voted_team',$winner)->get();
  196. $correctCount = count($correctDeal);
  197. if(count($deal))
  198. {
  199. $deal = $deal->first()->toArray();
  200. $dealTeam = $deal['voted_team'];
  201. $award = false;
  202. if($winner == $dealTeam)
  203. {
  204. $award = true;
  205. }
  206. }
  207.  
  208. }
  209. return view('main',compact('finalMatches','showMatches','activeMatch','firstDayMatches','firstDayTeams','team_1','team_2','activeTab','totalMatches','canVote','votedTeam','countTeam1','countTeam2','award','left_goal','right_goal','award','is_finished','correctCount'));
  210. }
  211.  
  212. public function loadMatch(Request $request)
  213. {
  214. $currentUser = User::find(Auth::id());
  215. $match_id = $request->input('match_id');
  216. $match = Match::where('id',$match_id)->get()->first()->toArray();
  217. $team_1 = Team::where('short_name',$match['team_1'])->get()->first()->toArray();
  218. $team_2 = Team::where('short_name',$match['team_2'])->get()->first()->toArray();
  219. /* Check if match finish */
  220. $award = false;
  221. $correctCount = 0;
  222. //
  223. date_default_timezone_set('Asia/Ho_Chi_Minh');
  224. $now = Carbon::now('Asia/Ho_Chi_Minh');
  225. $compareDate = Carbon::parse($match['kickoff_date']);
  226. $is_finished = intval($match['is_finished']);
  227. if($now > $compareDate || $is_finished){
  228. $canVote = 'disabled';
  229. }else{
  230. $canVote = '';
  231. }
  232. $deal = Deal::where('match_id',$match['id'])
  233. ->where('user_id',$currentUser->id)
  234. ->get();
  235. if(count($deal)){
  236. $deal = $deal->first()->toArray();
  237. $votedTeam = $deal['voted_team'];
  238. }else{
  239. $votedTeam = "";
  240. }
  241. $winner = "";
  242. if($is_finished)
  243. {
  244. $result = $match['result'];
  245. $goals = explode('-',$result);
  246. $left_goal = $goals[0];
  247. $right_goal = $goals[1];
  248. $deal = Deal::where('match_id',$match['id'])
  249. ->where('user_id',$currentUser->id)->get();
  250. $winner = "";
  251. if(floatval($goals[0])>floatval($goals[1]) + floatval($match['condition']))
  252. {
  253. $winner = $match['team_1'];
  254. }
  255. if(floatval($goals[0])<floatval($goals[1]) + floatval($match['condition']))
  256. {
  257. $winner = $match['team_2'];
  258. }
  259. $correctDeal = Deal::where('match_id',$match['id'])
  260. ->where('voted_team',$winner)->get();
  261. $correctCount = count($correctDeal);
  262. if(count($deal))
  263. {
  264. $deal = $deal->first()->toArray();
  265. $dealTeam = $deal['voted_team'];
  266. $award = false;
  267. if($winner == $dealTeam)
  268. {
  269. $award = true;
  270. }
  271. }
  272.  
  273. }
  274. $countTeam1 = count(Deal::where('match_id',$match['id'])
  275. ->where('voted_team',$match['team_1'])
  276. ->get());
  277. $countTeam2 = count(Deal::where('match_id',$match['id'])
  278. ->where('voted_team',$match['team_2'])
  279. ->get());
  280. //
  281. $content = view('templates.board',compact('match','team_1','team_2','votedTeam','canVote','countTeam1','countTeam2','award','left_goal','right_goal','award','is_finished','correctCount'))->render();
  282. return $content;
  283. }
  284.  
  285. public function loadTab(Request $request)
  286. {
  287. $currentUser = User::find(Auth::id());
  288. $date_str = $request->input('date');
  289. $currentYear = Carbon::now()->year;
  290. $date = str_replace('/','-',$date_str.'/'.$currentYear);
  291. $date = Carbon::parse($date)->format('d-m-Y');
  292. $date = new DateTime($date);
  293. $date = $date->format('Y-m-d');
  294. $matches = Match::where('kickoff_date', '>=', $date)
  295. ->where('kickoff_date','<', date('Y-m-d', strtotime("+1 day", strtotime($date))))
  296. ->get()->toArray();
  297. $teams = array();
  298. foreach ($matches as $match)
  299. {
  300. $team_1 = Team::where('short_name',$match['team_1'])->get()->first()->toArray();
  301. $team_2 = Team::where('short_name',$match['team_2'])->get()->first()->toArray();
  302. array_push($teams,[$team_1,$team_2]);
  303. }
  304. $match = $matches[0];
  305. $team_1 = $teams[0][0];
  306. $team_2 = $teams[0][1];
  307. $award = false;
  308. $correctCount = 0;
  309. //
  310. date_default_timezone_set('Asia/Ho_Chi_Minh');
  311. $now = Carbon::now('Asia/Ho_Chi_Minh');
  312. $compareDate = Carbon::parse($match['kickoff_date']);
  313. $is_finished = intval($match['is_finished']);
  314. if($now > $compareDate || $is_finished){
  315. $canVote = 'disabled';
  316. }else{
  317. $canVote = '';
  318. }
  319. $deal = Deal::where('match_id',$match['id'])
  320. ->where('user_id',$currentUser->id)
  321. ->get();
  322. if(count($deal)){
  323. $deal = $deal->first()->toArray();
  324. $votedTeam = $deal['voted_team'];
  325. }else{
  326. $votedTeam = "";
  327. }
  328. $winner = "";
  329. if($is_finished)
  330. {
  331. $result = $match['result'];
  332. $goals = explode('-',$result);
  333. $left_goal = $goals[0];
  334. $right_goal = $goals[1];
  335. $deal = Deal::where('match_id',$match['id'])
  336. ->where('user_id',$currentUser->id)->get();
  337. if(floatval($goals[0])>floatval($goals[1]) + floatval($match['condition']))
  338. {
  339. $winner = $match['team_1'];
  340. }
  341. if(floatval($goals[0])<floatval($goals[1]) + floatval($match['condition']))
  342. {
  343. $winner = $match['team_2'];
  344. }
  345. $correctDeal = Deal::where('match_id',$match['id'])
  346. ->where('voted_team',$winner)->get();
  347. $correctCount = count($correctDeal);
  348. if(count($deal))
  349. {
  350. $deal = $deal->first()->toArray();
  351. $dealTeam = $deal['voted_team'];
  352. $award = false;
  353. if($winner == $dealTeam)
  354. {
  355. $award = true;
  356. }
  357. }
  358.  
  359. }
  360. $countTeam1 = count(Deal::where('match_id',$match['id'])
  361. ->where('voted_team',$match['team_1'])
  362. ->get());
  363. $countTeam2 = count(Deal::where('match_id',$match['id'])
  364. ->where('voted_team',$match['team_2'])
  365. ->get());
  366. $contentList = view('templates.list',compact('matches','teams'))->render();
  367. $contentBoard = view('templates.board',compact('match','team_1','team_2','canVote','votedTeam','countTeam1','countTeam2','award','left_goal','right_goal','award','is_finished','correctCount'))->render();
  368. return json_encode(array(
  369. 'content_list' => $contentList,
  370. 'content_board' => $contentBoard
  371. ));
  372.  
  373. }
  374.  
  375. public function coupon()
  376. {
  377. $matches = Match::all()->groupBy(function($match,$index) {
  378. return Carbon::parse($match->kickoff_date);
  379. });
  380. $coupon = Coupon::find(1);
  381. $now = Carbon::now()->format('d/m');
  382. $listDate = array();
  383. foreach ($matches as $key => $value){
  384. array_push($listDate,$key);
  385. }
  386. $totalDate = 3;
  387. $finalMatches = array();
  388. $showMatches = array();
  389. $firstDayMatches = array();
  390. $firstDayTeams = array();
  391. $savedIndex = count($listDate);
  392. if($this->compareDayonMonth($listDate[count($listDate)-1],$now) >= 0)
  393. {
  394. for($i = count($listDate) - 1;$i >= 0; $i--)
  395. {
  396. if($this->compareDayonMonth($listDate[$i],$now) >= 0 && count($finalMatches) < $totalDate )
  397. {
  398. array_push($finalMatches,$listDate[$i]);
  399. array_push($showMatches,$matches[$listDate[$i]]);
  400. $savedIndex = $savedIndex - 1;
  401. }
  402. }
  403. $activeTab = 0;
  404. if(count($finalMatches) < $totalDate)
  405. {
  406. $remainedItem = $totalDate - count($finalMatches);
  407. for($i = $savedIndex-1; $i >= $savedIndex - $remainedItem ;$i--)
  408. {
  409. array_push($finalMatches,$listDate[$i]);
  410. array_push($showMatches,$matches[$listDate[$i]]);
  411. }
  412. }
  413. }else{
  414. for($i = count($listDate) - 1;$i > 0; $i--)
  415. {
  416. if( count($finalMatches) < $totalDate)
  417. {
  418. array_push($finalMatches,$listDate[$i]);
  419. array_push($showMatches,$matches[$listDate[$i]]);
  420. }
  421. }
  422. }
  423. $finalMatches = array_reverse($finalMatches);
  424. $showMatches = array_reverse($showMatches);
  425. $showMatches = $showMatches[$activeTab];
  426. foreach($showMatches as $match)
  427. {
  428. $team_1 = Team::where('short_name',$match['team_1'])->get();
  429. $team_2 = Team::where('short_name',$match['team_2'])->get();
  430. array_push($firstDayMatches,$match);
  431. array_push($firstDayTeams,[$team_1[0],$team_2[0]]);
  432. }
  433. $activeMatch = $matches[$finalMatches[$activeTab]][0];
  434. $team_1 = Team::where('short_name',$activeMatch['team_1'])->get();
  435. $team_2 = Team::where('short_name',$activeMatch['team_2'])->get();
  436. /* Check is deal or not */
  437. return view('coupon',compact('finalMatches','showMatches','activeMatch','firstDayMatches','firstDayTeams','team_1','team_2','activeTab','coupon'));
  438. }
  439.  
  440. public function compareDayonMonth($date1,$date2)
  441. {
  442. $date1 = explode("/",$date1);
  443. $date2 = explode("/",$date2);
  444. if(intval($date1[0]) > intval($date2[0]) ||intval($date1[1]) > intval($date2[1]))
  445. {
  446. return 1;
  447. }
  448. if(intval($date1[0]) == intval($date2[0]))
  449. {
  450. if(intval($date1[1]) > intval($date2[1]))
  451. {
  452. return 1;
  453. }
  454. if(intval($date1[1]) == intval($date2[1]))
  455. {
  456. return 0;
  457.  
  458. }
  459. if(intval($date1[1]) < intval($date2[1]))
  460. {
  461. return -1;
  462. }
  463. }
  464. if(intval($date1[0]) < intval($date2[0]))
  465. {
  466. return -1;
  467. }
  468.  
  469. }
  470.  
  471. public function vote(Request $request)
  472. {
  473. $match_id = $request->input('match_id');
  474. $team = $request->input('team');
  475. $currentUser = User::find(Auth::id());
  476. $deal = Deal::where('match_id',$match_id)
  477. ->where('user_id',$currentUser->id)->get();
  478. $match = Match::where('id',$match_id)->get();
  479. if(count($match)){
  480. $match = $match->first->toArray();
  481. date_default_timezone_set('Asia/Ho_Chi_Minh');
  482. $now = Carbon::now('Asia/Ho_Chi_Minh');
  483. $compareDate = Carbon::parse($match['kickoff_date']);
  484. if($now < $compareDate)
  485. {
  486. if(count($deal)){
  487. $deal = $deal->first()->toArray();
  488. if($deal['voted_team'] == $team){
  489. return json_encode([
  490. 'success' => 0,
  491. 'message' => 'Đội tuyển đã được bình chọn cho trận đấu'
  492. ]);
  493. }else{
  494. $editDeal = Deal::find($deal['id']);
  495. $editDeal->update([
  496. 'voted_team' => $team
  497. ]);
  498. $editDeal->save();
  499. $content = $this->updateMatch($match_id);
  500. return json_encode(array(
  501. 'success' => 1,
  502. 'content' => $content
  503. ));
  504. }
  505. }else{
  506. Deal::create([
  507. 'match_id' => $match_id,
  508. 'voted_team' => $team,
  509. 'user_id' => $currentUser->id,
  510. 'voted_goal' => '',
  511. 'created_at' => Carbon::now()
  512. ]);
  513. $content = $this->updateMatch($match_id);
  514. return json_encode(array(
  515. 'success' => 1,
  516. 'content' => $content
  517. ));
  518. }
  519. }
  520. }
  521.  
  522. }
  523.  
  524. public function updateMatch($match_id)
  525. {
  526. $currentUser = User::find(Auth::id());
  527. $match_id = $match_id;
  528. $match = Match::where('id',$match_id)->get()->first()->toArray();
  529. $team_1 = Team::where('short_name',$match['team_1'])->get()->first()->toArray();
  530. $team_2 = Team::where('short_name',$match['team_2'])->get()->first()->toArray();
  531. $award = false;
  532. $correctCount = 0;
  533. //
  534. date_default_timezone_set('Asia/Ho_Chi_Minh');
  535. $now = Carbon::now('Asia/Ho_Chi_Minh');
  536. $compareDate = Carbon::parse($match['kickoff_date']);
  537. $is_finished = intval($match['is_finished']);
  538. if($now > $compareDate || $is_finished){
  539. $canVote = 'disabled';
  540. }else{
  541. $canVote = '';
  542. }
  543. $deal = Deal::where('match_id',$match['id'])
  544. ->where('user_id',$currentUser->id)
  545. ->get();
  546. if(count($deal)){
  547. $deal = $deal->first()->toArray();
  548. $votedTeam = $deal['voted_team'];
  549. }else{
  550. $votedTeam = "";
  551. }
  552. $winner = "";
  553. if($is_finished)
  554. {
  555. $result = $match['result'];
  556. $goals = explode('-',$result);
  557. $left_goal = $goals[0];
  558. $right_goal = $goals[1];
  559. $deal = Deal::where('match_id',$match['id'])
  560. ->where('user_id',$currentUser->id)->get();
  561. if(floatval($goals[0])>floatval($goals[1]) + floatval($match['condition']))
  562. {
  563. $winner = $match['team_1'];
  564. }
  565. if(floatval($goals[0])<floatval($goals[1]) + floatval($match['condition']))
  566. {
  567. $winner = $match['team_2'];
  568. }
  569. $correctDeal = Deal::where('match_id',$match['id'])
  570. ->where('voted_team',$winner)->get();
  571. $correctCount = count($correctDeal);
  572. if(count($deal))
  573. {
  574. $deal = $deal->first()->toArray();
  575. $dealTeam = $deal['voted_team'];
  576. $award = false;
  577. if($winner == $dealTeam)
  578. {
  579. $award = true;
  580. }
  581. }
  582. }
  583. $countTeam1 = count(Deal::where('match_id',$match['id'])
  584. ->where('voted_team',$match['team_1'])
  585. ->get());
  586. $countTeam2 = count(Deal::where('match_id',$match['id'])
  587. ->where('voted_team',$match['team_2'])
  588. ->get());
  589. $content = view('templates.board',compact('match','team_1','team_2','votedTeam','canVote','countTeam1','countTeam2','award','left_goal','right_goal','award','is_finished','correctCount'))->render();
  590. return $content;
  591. }
  592.  
  593. public function login(Request $request)
  594. {
  595. $telephone = $request->input('telephone');
  596. $password = $request->input('password');
  597. $user = User::where('telephone',$telephone)
  598. ->get()->first();
  599. if($user){ // $user
  600. $hasher = app('hash');
  601. if($hasher->check($password,$user->password)){
  602. Auth::loginUsingId($user->id);
  603. return array(
  604. 'success' => 1
  605. );
  606. }else{
  607. return array(
  608. 'success' => 0,
  609. 'message' => 'Mật khẩu không chính xác'
  610. );
  611. }
  612. }else{
  613. // sync from Phong Vu
  614. $client = new Client();
  615. $guzzleClient = new \GuzzleHttp\Client(array(
  616. 'curl' => array(
  617. CURLOPT_SSL_VERIFYPEER => false,
  618. CURLOPT_SSL_VERIFYHOST => false
  619. ),
  620. )
  621. );
  622. $client->setClient($guzzleClient);
  623. $response = $client->request('GET', env('APP_PV').'?telephone='.urlencode($telephone).'&password='.urlencode($password));
  624. $authenticated = $response->filter('p')->each(function($node){
  625. if(strpos($node->text(),'password_is_valid'))
  626. {
  627. return true;
  628. }
  629. });
  630. if($authenticated[0])
  631. {
  632. User::create([
  633. 'telephone' => $telephone,
  634. 'password' => bcrypt($password)
  635. ]);
  636. $user = User::where('telephone',$telephone)
  637. ->get()->first();
  638. Auth::loginUsingId($user->id);
  639. return array(
  640. 'success' => 1
  641. );
  642. }
  643. }
  644. return array(
  645. 'success' => 0,
  646. 'message' => 'Tài khoản hoặc mật khẩu không chính xác'
  647. );
  648. }
  649. public function getAward(Request $request)
  650. {
  651. $user = User::find(Auth::id());
  652. $match_id = $request->input('match_id');
  653. $deal = Deal::where('user_id',$user->id)
  654. ->where('match_id',$match_id)->get();
  655. if(count($deal))
  656. {
  657. $match = Match::find($match_id);
  658. if($match)
  659. {
  660. if(intval($match['is_finished']))
  661. {
  662. $result = $match['result'];
  663. $goals = explode('-',$result);
  664. $left_goal = $goals[0];
  665. $right_goal = $goals[1];
  666. $winner = "NO ONE";
  667. if(floatval($goals[0])>floatval($goals[1]) + floatval($match['condition']))
  668. {
  669. $winner = $match['team_1'];
  670. }
  671. if(floatval($goals[0])<floatval($goals[1]) + floatval($match['condition']))
  672. {
  673. $winner = $match['team_2'];
  674. }
  675. $deal = $deal->first()->toArray();
  676. $dealTeam = $deal['voted_team'];
  677. if($winner == $dealTeam)
  678. {
  679. return json_encode([
  680. 'success' => 1,
  681. 'coupon' => array(
  682. 'code' => $match['code'],
  683. 'code_type' => $match['code_type'],
  684. 'code_quantity' => $match['code_quantity']
  685. )
  686. ]);
  687. }
  688. return json_encode([
  689. 'success' => 0,
  690. 'message' => 'Kết quả dự đoán không chính xác'
  691. ]);
  692. }else{
  693. return json_encode([
  694. 'success' => 0,
  695. 'message' => 'Trận đấu chưa kết thúc'
  696. ]);
  697. }
  698. }else{
  699. return json_encode([
  700. 'success' => 0,
  701. 'message' => 'Trận đấu không phù hợp'
  702. ]);
  703. }
  704. }else{
  705. return json_encode([
  706. 'success' => 0,
  707. 'message' => 'Không có phần thưởng phù hợp'
  708. ]);
  709. }
  710. }
  711.  
  712. public function checkFinshedMatch()
  713. {
  714. date_default_timezone_set('Asia/Ho_Chi_Minh');
  715. $now = Carbon::now('Asia/Ho_Chi_Minh');
  716. $matches = Match::where('is_finished',0)
  717. ->orderBy('kickoff_date','ASC')
  718. ->get();
  719. if($matches)
  720. {
  721. $match = $matches->first()->toArray();
  722. $match_id = $match['api_match_id'];
  723. $league_id = $match['api_league_id'];
  724. if($match_id && $league_id) //
  725. {
  726. $client = new Client();
  727. //env('SCORE_LIVE')
  728. $res = $client->request('GET','http://localhost:8000/assets/test.json',[
  729. 'query' => [
  730. 'key' => env('LIVE_SCORE_API_KEY'),
  731. 'secret' => env('LIVE_SCORE_API_SECRET'),
  732. // 'league' => $league_id
  733. ]
  734. ]);
  735.  
  736. $response = GuzzleHttp\json_encode($res);
  737. if($response['success'] == "true")
  738. {
  739. if(isset($response['data']['match']))
  740. {
  741. $matches = $response['data']['match'];
  742. foreach($matches as $match)
  743. {
  744. if($match['id'] == $match_id)
  745. {
  746. if($match['status'] == "FINISHED")
  747. {
  748. return [
  749. 'success'=> 1,
  750. 'score' => $match['score'],
  751. 'match_id'=> $match['id']
  752. ];
  753. }
  754. }
  755. }
  756. }
  757. }
  758. return [
  759. 'success' => 0
  760. ];
  761. }
  762. }
  763. }
  764.  
  765. public function updateCoupon()
  766. {
  767. $check = $this->checkFinshedMatch();
  768. if(intval($check['success']))
  769. {
  770. $match = Match::where('id',$check['match_id'])->firstOrFail();
  771. if($match)
  772. {
  773. // Create and Update Coupon
  774. $totalBets = Deal::where('match_id',$check['match_id']);
  775. if($totalBets->firstOrFail())
  776. {
  777. $totalBets = $totalBets->get()->count();
  778. // Group stage rule
  779.  
  780. }
  781.  
  782. }
  783. }
  784. }
  785. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement