· 6 years ago · Oct 30, 2019, 02:34 PM
1<?php namespace App\Http\Controllers;
2
3use App\User;
4use App\CoinWallet;
5use App\Rooms;
6use App\Profit;
7use App\Jackpot;
8use App\JackpotBets;
9use Illuminate\Http\Request;
10use Illuminate\Support\Facades\Session;
11use Carbon\Carbon;
12use DB;
13
14class JackpotController extends Controller {
15
16 private $coinwallet;
17
18 public function __construct(Request $r) {
19 parent::__construct();
20
21 $rooms = Rooms::where('status', 0)->orderBy('id', 'desc')->get();
22 foreach($rooms as $s) {
23 $room = Rooms::where('name', $r->get('room'))->first();
24 if(!$room) $this->room = $s->name;
25 else $this->room = $room->name;
26 }
27 $this->game = Jackpot::where('room', $this->room)->orderBy('game_id', 'desc')->first();
28 if(!$this->game) Jackpot::create([
29 'room' => $this->room,
30 'game_id' => 1,
31 'hash' => bin2hex(random_bytes(16))
32 ]);
33 view()->share('rooms', $rooms);
34 view()->share('room', $this->room);
35
36 DB::connection()->getPdo()->exec('SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
37 }
38
39 public function getRooms() {
40 $room = Rooms::where('status', 0)->get();
41
42 return $room;
43 }
44
45 public function index() {
46 // $coinwallet = CoinWallet::where(['user_id' => $this->user->id])->get();
47 // print_r($coinwallet);
48 // exit;
49 if (isset($this->user->id)) {
50 $bal = $this->user->balance;
51 $user_id = $this->user->id;
52 $coin = CoinWallet::where(['user_id' => $user_id])->get();
53 $btc_address = '';
54 $eth_balance = '';
55 $btc_balance = '';
56 $eth_address = '';
57 if (count($coin) > 0) {
58 foreach($coin as $row) {
59 if ($row['coin_type'] == '1') {
60 $btc_address = $row['address'];
61 $btc_balance = $row['balance'];
62 $btc_key = $row['private_key'];
63 }
64 if ($row['coin_type'] == '2') {
65 $eth_address = $row['address'];
66 $eth_balance = $row['balance'];
67 $eth_key = $row['private_key'];
68 }
69 }
70 }
71
72 if ($btc_address == '') {
73 $btc_url = "https://merklejobs.com:3005/createbtc"; // BTC(bitcoin) Addaress token generation API
74 $btch = curl_init();
75 curl_setopt($btch, CURLOPT_URL, $btc_url);
76 curl_setopt($btch, CURLOPT_BINARYTRANSFER, true);
77 curl_setopt($btch, CURLOPT_RETURNTRANSFER, true);
78 $btc_res = curl_exec($btch);
79
80 if (curl_errno($btch)) {
81 $btc_address = '';
82 $btc_key = '';
83 } else {
84 $btc_create = json_decode($btc_res, true);
85 $btc_address = $btc_create['address'];
86 $btc_key = $btc_create['private_key'];
87 }
88 curl_close($btch);
89 if ($btc_address != '' && $btc_key != '') {
90 CoinWallet::create(['user_id' => $user_id,
91 'coin_type' => '1',
92 'address' => $btc_address,
93 'private_key' => $btc_key,
94 'status' => '1',
95 'created_by' => '1',
96 'created_date' => date('Y-m-d h:i:s'),
97 'created_ip' => request()->ip(),]);
98 }
99 }
100
101 if ($eth_address == '') {
102 $eth_url = "https://merklejobs.com:3005/createeth"; // ETH(ethereum) Addaress token generation API
103 $ethh = curl_init();
104 curl_setopt($ethh, CURLOPT_URL, $eth_url);
105 curl_setopt($ethh, CURLOPT_BINARYTRANSFER, true);
106 curl_setopt($ethh, CURLOPT_RETURNTRANSFER, true);
107 $res_eth = curl_exec($ethh);
108
109 if (curl_errno($ethh)) {
110 $eth_address = '';
111 $eth_key = '';
112 } else {
113 $eth_create = json_decode($res_eth, true);
114 $eth_address = $eth_create['address'];
115 $eth_key = $eth_create['private_key'];
116 }
117 curl_close($ethh);
118
119 if ($eth_address != '' && $eth_key != '') {
120 CoinWallet::create(['user_id' => $user_id,
121 'coin_type' => '2',
122 'address' => $eth_address,
123 'private_key' => $eth_key,
124 'status' => '1',
125 'created_by' => '1',
126 'created_date' => date('Y-m-d h:i:s'),
127 'created_ip' => request()->ip()]);
128 }
129 }
130
131 // app('App\Http\Controllers\CoinController')->StoreCoinHistory($user_id);
132 // app('App\Http\Controllers\CoinController')->addCoins($user_id, $bal);
133
134 }
135
136 $lastWinners = $this->getLastWinners(7);
137 return view('pages.jackpot', [ 'lastWinners' => $lastWinners ]);
138 }
139
140 public function newGame(Request $r) {
141 $room = Rooms::where('name', $r->get('room'))->first();
142 if(is_null($room)) return response()->json([
143 'success' => false,
144 'msg' => 'Could not find the room you want to bet in!'
145 ]);
146
147 $game = Jackpot::create([
148 'room' => $room->name,
149 'game_id' => $this->game->game_id+1,
150 'hash' => bin2hex(random_bytes(16))
151 ]);
152
153 return response()->json([
154 'success' => true,
155 'data' => [
156 'game' => [
157 'game_id' => $game->game_id,
158 'id' => $game->id,
159 'hash' => $game->hash,
160 ],
161 'time' => $room->time
162 ]
163 ]);
164 }
165
166 public function getLastWinners($count = 1) {
167 $lastGames = Jackpot::where('status', 3)->orderBy('id', 'desc')->take($count)->get();
168 $users = [];
169
170 foreach($lastGames as $lastGame) {
171 array_push($users, $lastGame->winner_id);
172 }
173
174 $users = User::getUsers($users);
175
176 $output = [];
177
178 foreach($lastGames as $lastGame) {
179 foreach($users as $user) {
180 if($lastGame->winner_id == $user->id) {
181 $result = new \stdClass();
182 $result->unique_id = $user->unique_id;
183 $result->username = $user->username;
184 $result->win_balance = $lastGame->winner_balance;
185 $result->win_bonus = $lastGame->winner_bonus;
186 array_push($output, $result);
187 }
188 }
189 }
190
191 return $output;
192 }
193
194 public function parseJackpotGame($id) {
195 $game = Jackpot::where('id', $id)->first();
196 if(!$game) return null;
197
198 $room = Rooms::where('name', $game->room)->first();
199
200 $bets = JackpotBets::where('game_id', $game->id)->orderBy('id', 'asc')->get();
201
202 $returnPrice = 0;
203 $lastTicket = 0;
204 $returnBets = [];
205 $returnAmount = [];
206 $returnUsers = [];
207
208 foreach($bets as $bet) {
209 $user = (isset($returnUsers[$bet->user_id])) ? $returnUsers[$bet->user_id] : User::where('id', $bet->user_id)->first();
210 $user->uid = $user->unique_id;
211 $user = User::hideUserFilter($user);
212 if($user) {
213 $betSum = $bet->sum;
214 if($bet->balance == 'bonus') $betSum = round($bet->sum/$this->settings->exchange_curs, 2);
215 $lastTicket++;
216 $returnUsers[$user->id] = $user;
217 $returnUsers[$user->id]->color = $bet->color;
218 $returnBets[] = [
219 'user' => [
220 'id' => $user->unique_id,
221 'user_id' => $user->id,
222 'username' => $user->username,
223 'avatar' => $user->avatar
224 ],
225 'bet' => [
226 'amount' => $bet->sum,
227 'color' => $bet->color,
228 'balance' => $bet->balance,
229 'from' => floor($lastTicket),
230 'to' => floor($lastTicket+($betSum*100))
231 ],
232 'chart' => [
233 'src' => $user->avatar,
234 'width' => 35,
235 'height' => 35
236 ]
237 ];
238
239 if(!isset($returnAmount[$user->uid])) $returnAmount[$user->uid] = $betSum; else $returnAmount[$user->uid] += $betSum;
240
241 $lastTicket += $betSum*100;
242 $returnPrice += $betSum;
243 }
244 }
245
246 foreach($returnBets as $key => $bet) {
247 $returnBets[$key]['bet']['chance'] = number_format(($returnAmount[$bet['user']['id']]/$returnPrice)*100, 2);
248 }
249
250 $returnChances = [];
251 foreach($returnUsers as $key => $user) {
252 $returnChances[] = [
253 'game_id' => $game->id,
254 'sum' => $returnAmount[$user->uid],
255 'user' => [
256 'id' => $user->id,
257 'username' => $user->username,
258 'avatar' => $user->avatar
259 ],
260 'color' => $user->color,
261 'chance' => number_format(($returnAmount[$user->uid]/$returnPrice)*100, 2)
262 ];
263 }
264
265 $circleStart = 0;
266 $circleAll = 0;
267 foreach($returnChances as $key => $ch) {
268 $userChance = $ch['chance']/100;
269 $circleAll += $userChance;
270 $returnChances[$key]['circle'] = [
271 'color' => $ch['color'],
272 'start' => $circleStart,
273 'end' => $circleStart + (360*$userChance)
274 ];
275
276 $circleStart = $returnChances[$key]['circle']['end'];
277 }
278
279 foreach($returnUsers as $key => $user) $returnUsers[$key]['circleValue'] = ($user['chance']/$circleAll)*100;
280
281 return [
282 'success' => true,
283 'data' => [
284 'id' => $game->id,
285 'game_id' => $game->game_id,
286 'hash' => $game->hash,
287 'amount' => $returnPrice,
288 'chances' => $returnChances,
289 'bets' => array_reverse($returnBets),
290 'time' => $room->time,
291 'room' => $room->name,
292 'min' => $room->min,
293 'max' => $room->max
294 ]
295 ];
296 }
297
298 public function initRoom(Request $r) {
299 $room = Rooms::where('name', $r->get('room'))->first();
300 if(is_null($room)) return [
301 'success' => false
302 ];
303
304 $game = Jackpot::where('room', $room->name)->orderBy('id', 'desc')->first();
305 if(is_null($game)) return [
306 'success' => false
307 ];
308
309 return $this->parseJackpotGame($game->id);
310 }
311
312 public function newBet(Request $r) {
313 if(\Cache::has('action.user.' . $this->user->id)) return response()->json(['msg' => 'Wait before previous action!', 'type' => 'error']);
314 \Cache::put('action.user.' . $this->user->id, '', 2);
315 if($this->user->ban) return;
316 $room = Rooms::where('name', $r->get('room'))->first();
317 if(is_null($room)) return [
318 'success' => false,
319 'msg' => 'Could not find the room you want to bet in!'
320 ];
321
322 $game = Jackpot::where('room', $room->name)->orderBy('id', 'desc')->first();
323 if(is_null($game)) return [
324 'success' => false,
325 'msg' => 'Failed to find a game in the room '.$room->name
326 ];
327
328 if($game->status > 1) return [
329 'success' => false,
330 'msg' => 'Bets in this game are closed!'
331 ];
332
333 $user = User::where('id', $this->user->id)->first();
334 $userbets = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->get();
335
336 if($userbets->count() >= $room->bets) return [
337 'success' => false,
338 'msg' => 'You can`t do more '.$room->bets.' bets per game!'
339 ];
340
341 if(floatval($r->get('amount')) < $room->min) return [
342 'success' => false,
343 'msg' => 'Minimum bet amount '.$room->min.' coin!'
344 ];
345
346 if(floatval($r->get('amount')) > ($room->max - $userbets->sum('sum'))) return [
347 'success' => false,
348 'msg' => 'You can`t bet more '.$room->max.' coins per game!'
349 ];
350
351 DB::beginTransaction();
352
353 try {
354
355 if(is_null($user)) {
356 DB::rollback();
357 return [
358 'success' => false,
359 'msg' => 'Unknown error!'
360 ];
361 }
362
363 $balance = ($r->get('balance') == 'balance') ? 'balance' : 'bonus';
364 $betType = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->where('balance', '!=', $balance)->count();
365
366 if($betType > 0) {
367 DB::rollback();
368 return [
369 'success' => false,
370 'msg' => 'You have already placed a bet with '. (($balance == 'balance') ? 'bonus' : 'money') .' score!'
371 ];
372 }
373 if($balance != 'balance' && $balance != 'bonus') {
374 DB::rollback();
375 return [
376 'success' => false,
377 'msg' => 'Unable to determine your balance type!'
378 ];
379 }
380
381 if($user[$balance] < floatval($r->get('amount'))) {
382 DB::rollback();
383 return [
384 'success' => false,
385 'msg' => 'Insufficient funds!'
386 ];
387 }
388
389 DB::table('users')->where('id', $user->id)->update([
390 $balance => $user[$balance]-floatval($r->get('amount'))
391 ]);
392
393 $bet = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->first();
394 DB::table('jackpot_bets')->insert([
395 'room' => $room->name,
396 'game_id' => $game->id,
397 'user_id' => $user->id,
398 'sum' => floatval($r->get('amount')),
399 'color' => ($bet) ? $bet->color : $this->getRandomColor(),
400 'balance' => $r->get('balance')
401 ]);
402
403 DB::commit();
404 } catch(Exception $e) {
405 DB::rollback();
406 return [
407 'success' => false,
408 'msg' => 'Unknown error!'
409 ];
410 }
411
412 $user = User::where('id', $this->user->id)->first();
413 if($balance == 'balance') $this->redis->publish('updateBalance', json_encode([
414 'unique_id' => $user->unique_id,
415 'balance' => round($user->balance, 2)
416 ]));
417 if($balance == 'bonus') $this->redis->publish('updateBonus', json_encode([
418 'unique_id' => $user->unique_id,
419 'bonus' => round($user->bonus, 2)
420 ]));
421
422 $data = $this->parseJackpotGame($game->id);
423 if($data['success'] && count($data['data']['chances']) >= 2 && $game->status < 1) {
424 Jackpot::where('id', $game->id)->update([
425 'status' => 1
426 ]);
427 $this->redis->publish('jackpot.timer', json_encode([
428 'room' => $room->name,
429 'time' => $room->time,
430 'game' => $game->id
431 ]));
432 }
433
434 $this->redis->publish('jackpot', json_encode([
435 'type' => 'update',
436 'room' => $room->name,
437 'data' => $data
438 ]));
439
440 return [
441 'success' => true,
442 'msg' => 'Your bet has entered the game!'
443 ];
444 }
445
446 public function adminBet(Request $r) {
447 $room = Rooms::where('name', $r->get('room'))->first();
448 $user = User::where('user_id', $r->get('user'))->first();
449 if(is_null($room)) return [
450 'success' => false,
451 'msg' => 'Could not find the room you want to bet in!'
452 ];
453 if(is_null($user)) return [
454 'success' => false,
455 'msg' => 'User could not be found!'
456 ];
457
458 $game = Jackpot::where('room', $room->name)->orderBy('id', 'desc')->first();
459 if(is_null($game)) return [
460 'success' => false,
461 'msg' => 'Failed to find a game in the room '.$room->name
462 ];
463
464 if($game->status > 1) return [
465 'success' => false,
466 'msg' => 'Bets in this game are closed!'
467 ];
468
469 DB::beginTransaction();
470
471 try {
472 $userbets = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->get();
473
474 if($userbets->count() >= $room->bets) {
475 DB::rollback();
476 return [
477 'success' => false,
478 'msg' => 'You can`t do more '.$room->bets.' bets per game!'
479 ];
480 }
481
482 if(floatval($r->get('amount')) < $room->min) {
483 DB::rollback();
484 return [
485 'success' => false,
486 'msg' => 'Minimum bet amount '.$room->min.'coin!'
487 ];
488 }
489
490 if(floatval($r->get('amount')) > ($room->max - $userbets->sum('sum'))) {
491 DB::rollback();
492 return [
493 'success' => false,
494 'msg' => 'You can`t bet more '.$room->max.' coins per game!'
495 ];
496 }
497
498 if(is_null($user)) {
499 DB::rollback();
500 return [
501 'success' => false,
502 'msg' => 'Unknown error!'
503 ];
504 }
505
506 $balance = ($r->get('balance') == 'balance') ? 'balance' : 'bonus';
507 $betType = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->where('balance', '!=', $balance)->count();
508
509 if($betType > 0) {
510 DB::rollback();
511 return [
512 'success' => false,
513 'msg' => 'You have already placed a bet with '. (($balance == 'balance') ? 'bonus' : 'money') .' score!'
514 ];
515 }
516 if($balance != 'balance' && $balance != 'bonus') {
517 DB::rollback();
518 return [
519 'success' => false,
520 'msg' => 'Unable to determine your balance type!'
521 ];
522 }
523
524 $bet = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->first();
525 DB::table('jackpot_bets')->insert([
526 'room' => $room->name,
527 'game_id' => $game->id,
528 'user_id' => $user->id,
529 'sum' => floatval($r->get('amount')),
530 'color' => ($bet) ? $bet->color : $this->getRandomColor(),
531 'balance' => $r->get('balance'),
532 'fake' => 1
533 ]);
534
535 DB::commit();
536 } catch(Exception $e) {
537 DB::rollback();
538 return [
539 'success' => false,
540 'msg' => 'Unknown error!'
541 ];
542 }
543
544 $data = $this->parseJackpotGame($game->id);
545 if($data['success'] && count($data['data']['chances']) >= 2 && $game->status < 1) {
546 Jackpot::where('id', $game->id)->update([
547 'status' => 1
548 ]);
549 $this->redis->publish('jackpot.timer', json_encode([
550 'room' => $room->name,
551 'time' => $room->time,
552 'game' => $game->id
553 ]));
554 }
555
556 $this->redis->publish('jackpot', json_encode([
557 'type' => 'update',
558 'room' => $room->name,
559 'data' => $data
560 ]));
561
562 return [
563 'success' => true,
564 'msg' => 'Your bet has entered the game!'
565 ];
566 }
567
568 public function addBetFake() {
569 $room = Rooms::where('status', 0)->inRandomOrder()->first();
570 $user = $this->getUser();
571
572 $game = Jackpot::where('room', $room->name)->orderBy('id', 'desc')->first();
573
574 if(is_null($room)) return [
575 'success' => false,
576 'fake' => $this->settings->fakebets,
577 'msg' => '[ROOM #'.$room->name.'] Could not find the room you want to bet in!'
578 ];
579 if(is_null($user)) return [
580 'success' => false,
581 'fake' => $this->settings->fakebets,
582 'msg' => '[ROOM #'.$room->name.'] User could not be found!'
583 ];
584 if(is_null($game)) return [
585 'success' => false,
586 'fake' => $this->settings->fakebets,
587 'msg' => '[ROOM #'.$room->name.'] Failed to find a game!'
588 ];
589
590 if($game->status > 1) return [
591 'success' => false,
592 'fake' => $this->settings->fakebets,
593 'msg' => '[ROOM #'.$room->name.'] Bets in this game are closed!'
594 ];
595
596 $sum = $room->min+mt_rand($this->settings->fake_min_bet * 2, $this->settings->fake_max_bet * 2) / 2;
597
598 DB::beginTransaction();
599
600 try {
601 $userbets = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->get();
602
603 if($userbets->count() >= $room->bets) {
604 DB::rollback();
605 return [
606 'success' => false,
607 'fake' => $this->settings->fakebets,
608 'msg' => '[ROOM #'.$room->name.'] You can`t do more '.$room->bets.' bets per game!'
609 ];
610 }
611
612 if(floatval($sum) < $room->min) {
613 DB::rollback();
614 return [
615 'success' => false,
616 'fake' => $this->settings->fakebets,
617 'msg' => '[ROOM #'.$room->name.'] Minimum bet amount '.$room->min.'coins!'
618 ];
619 }
620
621 if(floatval($sum) > ($room->max - $userbets->sum('sum'))) {
622 DB::rollback();
623 return [
624 'success' => false,
625 'fake' => $this->settings->fakebets,
626 'msg' => '[ROOM #'.$room->name.'] You can`t bet more '.$room->max.'coins per game!'
627 ];
628 }
629
630 if(is_null($user)) {
631 DB::rollback();
632 return [
633 'success' => false,
634 'fake' => $this->settings->fakebets,
635 'msg' => '[ROOM #'.$room->name.'] Unknown error!'
636 ];
637 }
638
639 $bl = ['balance', 'bonus'];
640 $bl_true = $bl[array_rand($bl)];
641
642 $balance = ($bl_true == 'balance') ? 'balance' : 'bonus';
643 $betType = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->where('balance', '!=', $balance)->count();
644
645 if($betType > 0) {
646 DB::rollback();
647 return [
648 'success' => false,
649 'fake' => $this->settings->fakebets,
650 'msg' => '[ROOM #'.$room->name.'] You have already placed a bet with '. (($balance == 'balance') ? 'bonus' : 'money') .' score!'
651 ];
652 }
653 if($balance != 'balance' && $balance != 'bonus') {
654 DB::rollback();
655 return [
656 'success' => false,
657 'fake' => $this->settings->fakebets,
658 'msg' => '[ROOM #'.$room->name.'] Unable to determine your balance type!'
659 ];
660 }
661
662 $bet = JackpotBets::where('game_id', $game->id)->where('user_id', $user->id)->first();
663 DB::table('jackpot_bets')->insert([
664 'room' => $room->name,
665 'game_id' => $game->id,
666 'user_id' => $user->id,
667 'sum' => floatval($sum),
668 'color' => ($bet) ? $bet->color : $this->getRandomColor(),
669 'balance' => $balance,
670 'fake' => 1
671 ]);
672
673 DB::commit();
674 } catch(Exception $e) {
675 DB::rollback();
676 return [
677 'success' => false,
678 'fake' => $this->settings->fakebets,
679 'msg' => '[ROOM #'.$room->name.'] Unknown error!'
680 ];
681 }
682
683 $data = $this->parseJackpotGame($game->id);
684 if($data['success'] && count($data['data']['chances']) >= 2 && $game->status < 1) {
685 Jackpot::where('id', $game->id)->update([
686 'status' => 1
687 ]);
688 $this->redis->publish('jackpot.timer', json_encode([
689 'room' => $room->name,
690 'time' => $room->time,
691 'game' => $game->id
692 ]));
693 }
694
695 $this->redis->publish('jackpot', json_encode([
696 'type' => 'update',
697 'room' => $room->name,
698 'data' => $data
699 ]));
700
701 return [
702 'success' => true,
703 'fake' => $this->settings->fakebets,
704 'msg' => '[ROOM #'.$room->name.'] Your bet has entered the game!'
705 ];
706 }
707
708 public function getSlider(Request $r) {
709 $room = Rooms::where('name', $r->get('room'))->first();
710 if(!$room) return [
711 'success' => false,
712 'msg' => 'Could not find room '.$r->get('room')
713 ];
714
715 $game = Jackpot::where('room', $room->name)->orderBy('id', 'desc')->first();
716 if(!$game) return [
717 'success' => false,
718 'msg' => 'Failed to find a game in the room '.$room->name
719 ];
720
721 if($game->id != $r->get('game')) return [
722 'success' => false,
723 'msg' => 'Found game #'.$game->id.'. Not constitute #'.$r->get('game')
724 ];
725
726 $data = $this->parseJackpotGame($game->id);
727 if(!$data['success']) return [
728 'success' => false,
729 'msg' => 'Unknown error! Repeat...',
730 'retry' => true
731 ];
732
733 $data = $data['data'];
734
735 $winnerBet = null;
736 if(!$game->winner_id) {
737 $winnerTicket = ($game->winner_ticket > 0) ? $game->winner_ticket : mt_rand(0, $data['bets'][0]['bet']['to']);
738 } else {
739 $winner2 = [];
740 foreach($data['bets'] as $key => $d) {
741 if($game->winner_id == $data['bets'][$key]['user']['user_id']) $winner2[] = $d['bet'];
742 }
743 $winner2 = $winner2[array_rand($winner2)];
744 $winnerTicket = mt_rand($winner2['from'], $winner2['to']);
745 }
746 foreach($data['bets'] as $bet) if($bet['bet']['from'] <= $winnerTicket && $bet['bet']['to'] >= $winnerTicket) $winnerBet = $bet;
747 if(is_null($winnerBet)) return [
748 'success' => false,
749 'msg' => 'Could not find the winning bet! Repeat..',
750 'retry' => true
751 ];
752
753 DB::beginTransaction();
754 try {
755 DB::table('jackpot')->where('id', $game->id)->update([
756 'winner_id' => $winnerBet['user']['user_id'],
757 'winner_ticket' => $winnerTicket,
758 'status' => 2
759 ]);
760
761 $winner_money = $this->sendMoney($winnerBet['user']['user_id'], $game->id);
762
763 DB::commit();
764 } catch(Exception $e) {
765 DB::rollback();
766 return [
767 'success' => false,
768 'msg' => 'Unknown error! Repeat...',
769 'retry' => true
770 ];
771 }
772
773 $rotate = [];
774 foreach($data['chances'] as $key => $d) {
775 if($winnerBet['user']['user_id'] == $data['chances'][$key]['user']['id']) $rotate = $d['circle'];
776 }
777 $cords = null;
778 $center = $rotate['end']-$rotate['start'];
779 if(floor($center) > 1) $cords = mt_rand(floor($rotate['start']), floor($rotate['end']));
780 if(floor($center) < 1) $cords = $rotate['start'] + ($center/2);
781
782 $this->redis->publish('jackpot', json_encode([
783 'type' => 'slider',
784 'room' => $room->name,
785 'data' => [
786 'cords' => 1440+$cords,
787 'winner_id' => $winnerBet['user']['id'],
788 'winner_name' => $winnerBet['user']['username'],
789 'winner_avatar' => $winnerBet['user']['avatar'],
790 'winner_balance' => $winner_money[0],
791 'winner_bonus' => $winner_money[1],
792 'ticket' => $winnerTicket
793 ]
794 ]));
795
796 $this->redis->publish('jackpot.liveTrack', json_encode([
797 'winner_id' => $winnerBet['user']['id'],
798 'winner_name' => $winnerBet['user']['username'],
799 'winner_balance' => $winner_money[0],
800 'winner_bonus' => $winner_money[1],
801 ]));
802
803 return [
804 'success' => true
805 ];
806 }
807
808 private function sendMoney($user_id, $game_id) {
809 $game = Jackpot::where('id', $game_id)->first();
810 $bet = JackpotBets::where('game_id', $game->id)->where('user_id', $user_id)->first();
811
812 $money_bets = JackpotBets::where('game_id', $game->id)->where('balance', 'balance')->sum('sum');
813 $bonus_bets = JackpotBets::where('game_id', $game->id)->where('balance', 'bonus')->sum('sum');
814
815 $w_bet_money = JackpotBets::where('game_id', $game->id)->where('user_id', $game->winner_id)->where('balance', 'balance')->sum('sum');
816 $w_bet_bonus = JackpotBets::where('game_id', $game->id)->where('user_id', $game->winner_id)->where('balance', 'bonus')->sum('sum');
817
818 $sum_money = round($w_bet_money + (($money_bets - $w_bet_money) - ($money_bets - $w_bet_money)/100*$this->settings->jackpot_commission), 2);
819 $sum_bonus = round($w_bet_bonus + (($bonus_bets - $w_bet_bonus) - ($bonus_bets - $w_bet_bonus)/100*$this->settings->jackpot_commission), 2);
820
821 $comission = round(($money_bets - $w_bet_money)/100*$this->settings->jackpot_commission, 2);
822
823 $sum = [$sum_money, $sum_bonus];
824 $user = User::where(['id' => $user_id, 'fake' => 0])->first();
825
826 if(!is_null($user)) {
827 if($bet->balance == 'balance') {
828 $user->balance += $sum_money;
829 $user->bonus += $sum_bonus;
830 $user->requery += round(($money_bets - $w_bet_money) - ($money_bets - $w_bet_money)/100*$this->settings->jackpot_commission, 2);
831 $user->save();
832
833 if($user->ref_id) {
834 $ref = User::where('unique_id', $user->ref_id)->first();
835 if($ref) {
836 $ref_sum = round((($money_bets - $w_bet_money) - ($money_bets - $w_bet_money)/100*$this->settings->jackpot_commission)/100*$this->settings->ref_perc, 2);
837 if($ref_sum > 0) {
838 $ref->ref_money += $ref_sum;
839 $ref->ref_money_all += $ref_sum;
840 $ref->save();
841
842 Profit::create([
843 'game' => 'ref',
844 'sum' => -$ref_sum
845 ]);
846 }
847 }
848 }
849
850 if($comission > 0) Profit::create([
851 'game' => 'jackpot',
852 'sum' => $comission
853 ]);
854
855 $this->redis->publish('updateBalanceAfter', json_encode([
856 'unique_id' => $user->unique_id,
857 'balance' => round($user->balance, 2),
858 'timer' => 8
859 ]));
860
861 $this->redis->publish('updateBonusAfter', json_encode([
862 'unique_id' => $user->unique_id,
863 'bonus' => round($user->bonus, 2),
864 'timer' => 8
865 ]));
866 }
867
868 if($bet->balance == 'bonus') {
869 $user->bonus += $sum_money+$sum_bonus;
870 $user->save();
871
872 $this->redis->publish('updateBonusAfter', json_encode([
873 'unique_id' => $user->unique_id,
874 'bonus' => round($user->bonus, 2),
875 'timer' => 8
876 ]));
877 }
878 } else {
879 $sum = [$sum_money, $sum_bonus];
880 if($money_bets > 0) Profit::create([
881 'game' => 'jackpot',
882 'sum' => $money_bets
883 ]);
884 }
885
886 JackpotBets::where(['game_id' => $game->id, 'user_id' => $bet->user_id])->update([
887 'win' => 1
888 ]);
889
890 $game->winner_balance = $sum_money;
891 $game->winner_bonus = $sum_bonus;
892 $game->status = 3;
893 $game->save();
894
895 return $sum;
896 }
897
898 private function getRandomColor() {
899 $color = str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
900 return $color;
901 }
902
903 public function getGame(Request $r) {
904 $room = $r->get('room');
905 $option = Rooms::where('name', $room)->first();
906 $game = Jackpot::where('room', $room)->orderBy('id', 'desc')->first();
907
908 return response()->json([
909 'room' => $room,
910 'game' => $game->id,
911 'time' => $option->time,
912 'status' => $game->status
913 ]);
914 }
915
916 public function history() {
917 return view('pages.jackpotHistory');
918 }
919
920 public function initHistory(Request $r) {
921 $room = Rooms::where('name', $r->get('room'))->first();
922 if(is_null($room)) return [
923 'success' => false
924 ];
925
926 $games = Jackpot::where('room', $room->name)->where('status', 3)->where('updated_at', '>=', Carbon::today())->orderBy('game_id', 'desc')->limit(30)->get();
927
928 $history = [];
929 foreach($games as $game) {
930 $winner = User::where('id', $game->winner_id)->first();
931 $price = JackpotBets::where('game_id', $game->id)->sum('sum');
932 $bet = JackpotBets::where('game_id', $game->id)->where('user_id', $winner->id)->sum('sum');
933 $chance = round($bet/$price*100, 2);
934 if(isset($winner)) {
935 $history[] = [
936 'game_id' => $game->game_id,
937 'winner_id' => $winner->unique_id,
938 'winner_name' => $winner->username,
939 'winner_avatar' => $winner->avatar,
940 'winner_chance' => $chance,
941 'winner_balance' => $game->winner_balance,
942 'winner_bonus' => $game->winner_bonus,
943 'winner_ticket' => $game->winner_ticket,
944 'hash' => $game->hash
945 ];
946 }
947 }
948
949 return ['success' => true, 'history' => $history];
950 }
951
952 public function gotThis(Request $r) {
953 $game_id = $r->get('game_id');
954 $game = Jackpot::where('id', $game_id)->first();
955 $userid = $r->get('user_id');
956 $user = User::where('id', $userid)->first();
957 $bets = JackpotBets::where(['game_id' => $game_id, 'user_id' => $user->id])->first();
958
959 if(!$game->id) return [
960 'msg' => 'Failed to get game number!',
961 'type' => 'error'
962 ];
963
964 if($game->status == 3) return [
965 'msg' => 'The game has already begun!',
966 'type' => 'error'
967 ];
968
969 if(!$userid) return [
970 'msg' => 'Failed to get player ID!',
971 'type' => 'error'
972 ];
973
974 if(is_null($bets)) return [
975 'msg' => 'This player did not bet!',
976 'type' => 'error'
977 ];
978
979 Jackpot::where('id', $game_id)->update([
980 'winner_id' => $user->id
981 ]);
982
983 return [
984 'msg' => 'You pick player '.$user->username.' in the game!',
985 'type' => 'success'
986 ];
987 }
988
989 private function getUser() {
990 $user = User::where('fake', 1)->inRandomOrder()->first();
991 if($user->time != 0) {
992 $now = Carbon::now()->format('H');
993 if($now < 06) $time = 4;
994 if($now >= 06 && $now < 12) $time = 1;
995 if($now >= 12 && $now < 18) $time = 2;
996 if($now >= 18) $time = 3;
997 $user = User::where(['fake' => 1, 'time' => $time])->inRandomOrder()->first();
998 }
999 return $user;
1000 }
1001}