Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.27 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4. require '../vendor/autoload.php';
  5. use Illuminate\Http\Request;
  6.  
  7. use App;
  8. use App\Http\Requests;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\User;
  11. use App\Models\Game;
  12. use App\Models\Participant;
  13. use ElephantIO\Client;
  14. use ElephantIO\Engine\SocketIO\Version1X;
  15. use Illuminate\Support\Facades\DB;
  16. use RandomOrg\Random;
  17. use Ixudra\Curl\Facades\Curl;
  18. use App\Models\Item;
  19. use GuzzleHttp;
  20.  
  21. class GameController extends Controller
  22. {
  23.     protected $password = "Amina1808";
  24.     public $variants = ['red', 'zero', 'black'];
  25.     public $user = null;
  26.     public $red = [1,3,5,7,9,12,14];
  27.     public $black = [2,4,6,8,10,11,13];
  28.     public $randomApiKey = ['c1fb44db-528a-464f-8ce5-689c1e0330da', '73ca5ef5-31c6-495a-9383-a40c6d193de8', 'd03a344a-1c9f-4018-ab79-25cf5cb9cc83',
  29.         'cf9b96fa-cf3b-414f-bb37-473608bea64f','4a1efc1e-9369-4a2c-be43-616eaec1676f'];
  30.     public $i = 0;
  31.     public $client = null;
  32.     public $prices = null;
  33.  
  34.     public function __construct()
  35.     {
  36.         parent::__construct();//в главном контроллере устанавливается язык пользователя
  37.         $this->client = new Client(new Version1X('http://193.124.59.17:8890'));
  38.         if(isset($_SESSION['steamid'])) $this->user = User::where('steamid', $_SESSION['steamid'])->first();
  39.     }
  40.  
  41.     public function index()
  42.     {
  43.         $items = Item::where('price', '>', 0)->orderBy('price', 'DESC')->get();
  44.         $participants = Participant::all();
  45.         $participants = reset($participants);
  46.         $participants = array_reverse($participants);
  47.         $users = User::all()->groupBy("steamid");
  48.         $games = Game::where('active', 0)->orderBy('id', 'DESC')->limit(5)->get();
  49.         $games = reset($games);
  50.         $games = array_reverse($games);
  51.         return view('content.main', ['user' => $this->user, 'users' => $users, 'participants' => $participants,
  52.                                      'games' => $games, 'red' => $this->red, 'black' => $this->black, 'items' => $items]);
  53.     }
  54.  
  55.     public function emitter()
  56.     {
  57.         $this->client->initialize();
  58.         if($this->user != null)
  59.         {
  60.             $_POST['data']['steamid'] = $this->user->steamid;
  61.             $this->client->emit($_POST['emitter'], $_POST['data']);
  62.         }
  63.         $this->client->close();
  64.     }
  65.  
  66.     public function wallet()
  67.     {
  68.         if(isset($this->user))
  69.         {
  70.             echo $this->user->wallet;
  71.         }
  72.     }
  73.  
  74.     public function updateItems()
  75.     {
  76.         Item::where('id', '>', 0)->delete();
  77.         $client = new GuzzleHttp\Client();
  78.         $request = new GuzzleHttp\Psr7\Request('GET', 'https://api.csgofast.com/price/all');
  79.         $promise = $client->sendAsync($request)->then(function ($response) {
  80.             $this->prices = json_decode($response->getBody(), true);
  81.  
  82.             $client = new GuzzleHttp\Client();
  83.             $request = new GuzzleHttp\Psr7\Request('GET', 'http://steamcommunity.com/profiles/76561198260132417/inventory/json/730/2/');
  84.             $promise = $client->sendAsync($request)->then(function ($response) {
  85.                 $client = new GuzzleHttp\Client();
  86.                 $response = json_decode($response->getBody());
  87.                 foreach($response->rgInventory as $item)
  88.                 {
  89.                     $request = new GuzzleHttp\Psr7\Request('GET', 'http://api.steampowered.com/ISteamEconomy/GetAssetClassInfo/v0001?key=&format=json&language=ru&appid=730&class_count=2&classid0=0&classid1='.$item->classid);
  90.                     $promise = $client->sendAsync($request)->then(function ($response) {
  91.                         $response = current(json_decode($response->getBody())->result);
  92.                         $img = "https://steamcommunity-a.akamaihd.net/economy/image/class/730/".$response->classid."/360fx360f";
  93.                         Item::create([
  94.                             'classid' => $response->classid,
  95.                             'name' => $response->name,
  96.                             'market_hash_name' => $response->market_hash_name,
  97.                             'img' => $img
  98.                         ]);
  99.                     });
  100.                     $promise->wait();
  101.                 }
  102.             });
  103.             $promise->wait();
  104.         });
  105.         $promise->wait();
  106.  
  107.         foreach($this->prices as $key => $value)
  108.         {
  109.             $value = $value + $value*0.03;
  110.             Item::where('market_hash_name', $key)->update([
  111.                 'price' => ceil($value*100)//1 chip is equal 0.01$
  112.             ]);
  113.         }
  114.     }
  115.  
  116.     public function withdrawal($classid)
  117.     {
  118.         if(isset($this->user))
  119.         {
  120.             $item = Item::where('classid', $classid)->first();
  121.             if($item)
  122.             {
  123.                 if ($this->user->wallet >= $item->price)
  124.                 {
  125.                     $token = $this->user->tradeoffer;
  126.                     if($token)
  127.                     {
  128.                         preg_match("/token=.*/", $token, $token);
  129.                         $token = str_replace("token=", "", $token[0]);
  130.                         User::where('steamid', $this->user->steamid)->update([
  131.                             'wallet' => $this->user->wallet - $item->price
  132.                         ]);
  133.                         DB::table('winners')->insert([
  134.                             'user_id' => $this->user->steamid,
  135.                             'usersteamid' => $this->user->steamid,
  136.                             'token' => $token,
  137.                             'items' => $classid
  138.                         ]);
  139.                         $item->delete();
  140.                         echo 'success';
  141.                     }
  142.                     else
  143.                     {
  144.                         echo 'notification("Ошибка", "Вы не ввели ссылку на обмен, вы можете сделать
  145.                         это, кликнув по своему аккаунту в верхнем меню", "danger")';
  146.                     }
  147.                 }
  148.                 else
  149.                 {
  150.                     echo 'notification("Ошибка", "Недостаточно средств", "danger")';
  151.                 }
  152.             }
  153.             else
  154.             {
  155.                 echo 'notification("Ошибка", "Предмет уже забрали, обновите страницу", "danger")';
  156.             }
  157.         }
  158.     }
  159.  
  160.     public function deposit()
  161.     {
  162.         return redirect('https://steamcommunity.com/tradeoffer/new/?partner=173783932&token=t0kzwpMs');
  163.     }
  164.  
  165.     public function tradeoffer()
  166.     {
  167.         User::where('steamid', $this->user->steamid)->update([
  168.             'tradeoffer'  =>  urldecode($_REQUEST['link'])
  169.         ]);
  170.     }
  171.  
  172.     public function bet()
  173.     {
  174.         $variant = $_POST['variant'];
  175.         $bet = round($_POST['bet']);
  176.         if($this->user != null && in_array($variant,$this->variants) && $this->user->wallet >= $bet && $bet > 0)
  177.         {
  178.             $participant = Participant::where('steamid', $this->user->steamid);
  179.             if($participant->count() < 3 && $bet <= 1000 && ($participant->sum('bet') + $bet) <= 1000)
  180.             {
  181.                 $game = Game::all()->last();
  182.                 if($game->active)
  183.                 {
  184.                     Participant::create([
  185.                         'game' => $game->id,
  186.                         'steamid' => $this->user->steamid,
  187.                         'bet' => $bet,
  188.                         'variant' => $variant
  189.                     ]);
  190.                     User::where('steamid', $this->user->steamid)->update([
  191.                         'wallet'  => $this->user->wallet - $bet
  192.                     ]);
  193.                     echo '$("balance").html(parseInt($("balance").html()) - parseInt('.$bet.'));
  194.                    $(".your-'.$variant.'").html(parseInt($(".your-'.$variant.'").html()) + parseInt('.$bet.'));
  195.                    notification("Успех", "Ставка принята", "success");
  196.                ';
  197.                     $this->client->initialize();
  198.                     if($this->user != null)
  199.                     {
  200.                         $data = [
  201.                             'steamid' => $this->user->steamid,
  202.                             'name' => $this->user->name,
  203.                             'avatar' => $this->user->avatar,
  204.                             'variant' => $variant,
  205.                             'bet' => $bet,
  206.                         ];
  207.                         $this->client->emit('newBet', $data);
  208.                     }
  209.                     $this->client->close();
  210.                 }else{
  211.                     echo 'notification("Ошибка", "Игра окончена", "danger");';
  212.                 }
  213.             }else{
  214.                 echo 'notification("Ошибка", "Максимальное количество ставок за одну игру 3, максимальная ставка 1000 фишек", "danger");';
  215.             }
  216.         }
  217.     }
  218.  
  219.     public function game_over()
  220.     {
  221.         if(isset($_POST['password']) && $_POST['password'] == $this->password)
  222.         {
  223.             $game = Game::all()->last();
  224.             Game::where('active', 1)->update([
  225.                 'active' => 0
  226.             ]);
  227.             $win_variant = "zero";
  228.             if(in_array($game->number,$this->red))
  229.             {
  230.                 $win_variant = "red";
  231.             }else if(in_array($game->number, $this->black)){
  232.                 $win_variant = "black";
  233.             }
  234.             echo $win_variant;
  235.             $winners = Participant::where("variant", $win_variant)->get();
  236.             foreach($winners as $winner)
  237.             {
  238.                 $user = User::where('steamid', $winner->steamid)->first();
  239.                 $amount = $user->wallet + ($winner->bet * 2);
  240.                 if($win_variant == "zero"){
  241.                     $amount = $user->wallet + ($winner->bet * 8);
  242.                 }
  243.                 User::where('steamid', $winner->steamid)->update([
  244.                     'wallet'  => $amount
  245.                 ]);
  246.             }
  247.             Participant::where('id', '>', '0')->delete();
  248.         }
  249.     }
  250.     public function get_game($id)
  251.     {
  252.         $game = Game::where('active', 0)->where('id', $id)->first();
  253.         $response = '<form style="display: none" class="randomForm" action="https://api.random.org/verify" method="post" target="_blank">
  254.                        <input type="hidden" name="format" value="json">';
  255.         $response .= "<input type='hidden' name='random' value='".$game->random."'>";
  256.         $response .= ' <input type="hidden" name="signature" value="'.$game->signature.'">
  257.                        <input class="chip ';
  258.         if(in_array($game->number, $this->red)) $response .= 'red';
  259.         else if(in_array($game->number, $this->black)) $response .= 'black';
  260.         else $response .= 'green';
  261.         $response .= '" type="submit" value="'.$game->number.'"/></form>';
  262.         echo $response;
  263.     }
  264.     public function make()
  265.     {
  266.         if(isset($_POST['password']) && $_POST['password'] == $this->password)
  267.         {
  268.             try {
  269.                 $generator = new Random($this->randomApiKey[$this->i]);
  270.                 $data = $generator->generateIntegers(1, 0, 14, false, 10, true);
  271.                 $random  = $data['result']['random'];
  272.                 $number = $random['data'][0];
  273.                 $signature = $data['result']['signature'];
  274.             } catch (Exception $e) {
  275.                 $this->i++;
  276.                 $this->make();
  277.                 exit();
  278.             }
  279.             $this->i = 0;
  280.             $last = Game::where('active', 1)->first();
  281.             if(!isset($last))
  282.             {
  283.                 $game = Game::create([
  284.                     'number' => $number,
  285.                     'random' => json_encode($random),
  286.                     'signature' => $signature
  287.                 ]);
  288.                 if(Game::all()->count() > 10)
  289.                 {
  290.                     Game::where("active", 0)->orderBy('id', 'ASC')->limit(Game::all()->count() - 6)->delete();
  291.                 }
  292.                 echo $game->id;
  293.             }
  294.             else
  295.             {
  296.                 echo $last->id;
  297.             }
  298.         }
  299.         else
  300.         {
  301.             return redirect('/');
  302.         }
  303.  
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement