Advertisement
Guest User

Untitled

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