<?php
  session_start();
$sid = $_SESSION['hash'];
require("inc/site_config.php");
require("inc/config.php");
require("inc/bd.php");
$pass = $_POST['pass'];
$login = $_POST['login'];
$type = $_POST['type'];
$email = $_POST['email'];
$error = 0;
$fa = "";
if($type == "betNum1")
{

$bet = $_POST['bet'];
$type = "[1000-1500]";

$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$wager = $row['wager'];
$user_id = $row['id'];
$deposit = $row['deposit'];
$sliv = $row['sliv'];
}
session_start(); 
if($_SESSION['timestamp'] + 3 > time()){ 
$error = 3;
$fa = "error";
$mess = "Не нужно нажимать так быстро!";
} 
else{
$_SESSION['timestamp'] = time();
}
if($bet > $balance){
  $error = 3;
  $fa = "error";
  $mess = "Недостаточно средств!";
}
if($bet < 1){
  $error = 3;
  $fa = "error";
  $mess = "bet error.";
}
if(empty($sid)){
  $error = 3;
  $fa = "error";
  $mess = "Сессия не активна!";
}
if($error == 0)
{ 
$update_sql2 = "Update kot_user set balance = balance - '$bet' WHERE hash='$sid'";
mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
$number = rand(0,10000);
$formula = 0;
$fff = rand(0,100);
if($fff > 55){
	$number = rand(1000,10000);
}
if($number <= 1500 &&  $number >= 1000){
	$cef = 20;
	$formula = $bet * $cef;
    $update_sql2 = "Update kot_users set balance = balance + '$formula' WHERE hash='$sid'";
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
}
else{
	$cef = 0;
}
$insert_sql1 = "INSERT INTO `numbersGame` (`user_id`, `type`, `bet`, `number`, `win`) VALUES ('{$user_id}', '{$type}', '{$bet}', '{$number}', '{$formula}');";
 mysql_query($insert_sql1) ;
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balancenew = $row['balance'];
}
$fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'number' => "$number",
	'profit' => "$formula",
	'balance' => "$balance",
	'new_balance' => "$balancenew"
    );
}
if($type == "betNum2")
{

$bet = $_POST['bet'];
$type = "[3000-3500]";

$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$wager = $row['wager'];
$user_id = $row['id'];
$deposit = $row['deposit'];
$sliv = $row['sliv'];
}
session_start(); 
if($_SESSION['timestamp'] + 3 > time()){ 
$error = 3;
$fa = "error";
$mess = "Не нужно нажимать так быстро!";
} 
else{
$_SESSION['timestamp'] = time();
}
if($bet > $balance){
  $error = 3;
  $fa = "error";
  $mess = "Недостаточно средств!";
}
if($bet < 1){
  $error = 3;
  $fa = "error";
  $mess = "bet error.";
}
if(empty($sid)){
  $error = 3;
  $fa = "error";
  $mess = "Сессия не активна!";
}
if($error == 0)
{ 
$update_sql2 = "Update kot_user set balance = balance - '$bet' WHERE hash='$sid'";
mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
$number = rand(0,10000);
$formula = 0;
$fff = rand(0,100);
if($fff > 55){
	$number = rand(4000,10000);
}
if($number >= 3000 && $number <= 3500){
	$cef = 20;
	$formula = $bet * $cef;
    $update_sql2 = "Update kot_user set balance = balance + '$formula' WHERE hash='$sid'";
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
}
else{
	$cef = 0;
}
$insert_sql1 = "INSERT INTO `numbersGame` (`user_id`, `type`, `bet`, `number`, `win`) VALUES ('{$user_id}', '{$type}', '{$bet}', '{$number}', '{$formula}');";
 mysql_query($insert_sql1) ;
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balancenew = $row['balance'];
}
$fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'number' => "$number",
	'profit' => "$formula",
	'balance' => "$balance",
	'new_balance' => "$balancenew"
    );
}
if($type == "betNum3")
{

$bet = $_POST['bet'];
$type = "[7000-7500]";

$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$wager = $row['wager'];
$user_id = $row['id'];
$deposit = $row['deposit'];
$sliv = $row['sliv'];
}
session_start(); 
if($_SESSION['timestamp'] + 3 > time()){ 
$error = 3;
$fa = "error";
$mess = "Не нужно нажимать так быстро!";
} 
else{
$_SESSION['timestamp'] = time();
}
if($bet > $balance){
  $error = 3;
  $fa = "error";
  $mess = "Недостаточно средств!";
}
if($bet < 1){
  $error = 3;
  $fa = "error";
  $mess = "bet error.";
}
if(empty($sid)){
  $error = 3;
  $fa = "error";
  $mess = "Сессия не активна!";
}
if($error == 0)
{ 
$update_sql2 = "Update kot_user set balance = balance - '$bet' WHERE hash='$sid'";
mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
$number = rand(0,10000);
$formula = 0;
$fff = rand(0,100);
if($fff > 55){
	$number = rand(0,7000);
}
if($number >= 7000 && $number <= 7500){
	$cef = 20;
	$formula = $bet * $cef;
    $update_sql2 = "Update kot_user set balance = balance + '$formula' WHERE hash='$sid'";
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
}
else{
	$cef = 0;
}
$insert_sql1 = "INSERT INTO `numbersGame` (`user_id`, `type`, `bet`, `number`, `win`) VALUES ('{$user_id}', '{$type}', '{$bet}', '{$number}', '{$formula}');";
 mysql_query($insert_sql1) ;
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balancenew = $row['balance'];
}
$fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'number' => "$number",
	'profit' => "$formula",
	'balance' => "$balance",
	'new_balance' => "$balancenew"
    );
}
if($type == "betNum4")
{

$bet = $_POST['bet'];
$type = "[9000-9500]";

$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$wager = $row['wager'];
$user_id = $row['id'];
$deposit = $row['deposit'];
$sliv = $row['sliv'];
}
session_start(); 
if($_SESSION['timestamp'] + 3 > time()){ 
$error = 3;
$fa = "error";
$mess = "Не нужно нажимать так быстро!";
} 
else{
$_SESSION['timestamp'] = time();
}
if($bet > $balance){
  $error = 3;
  $fa = "error";
  $mess = "Недостаточно средств!";
}
if($bet < 1){
  $error = 3;
  $fa = "error";
  $mess = "bet error.";
}
if(empty($sid)){
  $error = 3;
  $fa = "error";
  $mess = "Сессия не активна!";
}
if($error == 0)
{ 
$update_sql2 = "Update kot_user set balance = balance - '$bet' WHERE hash='$sid'";
mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
$number = rand(0,10000);
$formula = 0;
$fff = rand(0,100);
if($fff > 55){
	$number = rand(0,9500);
}
if($number >= 9000 && $number <= 9500){
	$cef = 20;
	$formula = $bet * $cef;
    $update_sql2 = "Update kot_user set balance = balance + '$formula' WHERE hash='$sid'";
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
}
else{
	$cef = 0;
}
$insert_sql1 = "INSERT INTO `numbersGame` (`user_id`, `type`, `bet`, `number`, `win`) VALUES ('{$user_id}', '{$type}', '{$bet}', '{$number}', '{$formula}');";
 mysql_query($insert_sql1) ;
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balancenew = $row['balance'];
}
$fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'number' => "$number",
	'profit' => "$formula",
	'balance' => "$balance",
	'new_balance' => "$balancenew"
    );
}
if ( $type == "craft" ){

 $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{

$money = $row['balance'];
$id = $row['id'];
}

$bet = $_POST['summa'];
$chance = $_POST['chance'];
$price = $_POST['price'];
$img = $_POST['img'];
$name = $_POST['name'];

$needChance = round(100 - $needChance, 2);
if ( $bet > $money ){

$msg = "Недостаточно средств!";
$msgType = "error";
$alert = 2;

}
if ( $bet < 1 ){

$msg = "Минимальная ставка - 1";
$msgType = "error";
$alert = 2;

}
if ( $chance < 5 ){

$msg = "Минимальный шанс - 5%";
$msgType = "error";
$alert = 2;

}elseif ( $chance > 75 ){

$msg = "Максимальный шанс - 75%";
$msgType = "error";
$alert = 2;

}
if ( !is_numeric($bet) || !is_numeric($chance) || !is_numeric($id) || empty($name) || empty($img) || !is_numeric($price) ){

$msg = "Ошибка!";
$msgType = "error";
$alert = 2;

}



if ( $alert == '' ){

$randNum = rand(0, 100);

if ( $randNum <= $chance){

$alert = 1;

$newBalance = $money - $bet;
$newBalance += $price;
$newBalance = round($newBalance, 2);

mysql_query("UPDATE kot_user SET balance = '$newBalance' WHERE hash='$sid'");

}else{

$newBalance = $money - $bet;
$newBalance = round($newBalance, 2);

mysql_query("UPDATE kot_user SET balance = '$newBalance' WHERE hash='$sid'");

}

}

$result = array("msg" => "$msg", "alert" => "$alert", "type" => "$msgType", "rand" => "$randNum", "chance" => "$chance", "price" => "$price", "img" => "$img", "name" => "$name", "newBalance" => "$newBalance");

}
if($type == "keno") {
    $summa_keno = $_POST['summa_keno'];
    $summa = $summa_keno;
    $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$login = $row['login'];
$balance = $row['balance'];

}
    
if($balance < $summa){
    $error = 1;
     $fa = "error";
    $mess = "Недостаточно средств";
}
if( $summa < 1 ){
    $error = 1;
     $fa = "error";
    $mess = "Ставки от 1р";
}
if(!is_numeric($summa)) {
          $error = 1;
     $fa = "error";
    $mess = "Ставки от 1р";
        
    }
    $l = $_POST['l'];
     $arr = $_POST['massiv'];
     //$c1 = implode( $arr);
     $c = $arr;
     $arr1 = $c[0];
     $arr2 = $c[1];
     $arr3 = $c[2];
     $arr4 = $c[3];
     $arr5 = $c[4];
     $arr6 = $c[5];
     $arr7 = $c[6];
     $arr8 = $c[7];
     $arr9 = $c[8];
     $arr10 = $c[9];
     if ($l > 10){
         $error = 1;
         $fa = "error";
         $mess = "Максимальное количество элементов, которое вы можете выбрать - 10. <br> Вы выбрали $l";
     }
     if ($l == 0){
         $error = 1;
         $fa = "error";
         $mess = "Выберите минимум 1 элемент";
     }
     if ($error == 0){
     $fa = "success";
     $arr12 = "$arr1 $arr2 $arr3 $arr4 $arr5 $arr6 $arr7 $arr8 $arr9 $arr10";
     
     $n1 = rand(1,40);
     $n2 = rand(1,40);
     $n3 = rand(1,40);
     $n4 = rand(1,40);
     $n5 = rand(1,40);
     $n6 = rand(1,40);
     $n7 = rand(1,40);
     $n8 = rand(1,40);
     $n9 = rand(1,40);
     $n10 = rand(1,40);
     
     $n = "$n1 $n2 $n3 $n4 $n5 $n6 $n7 $n8 $n9 $n10";
     
     $count = 0;
        if (in_array($n1, $arr)) {	
            $count += 1;
        }
         if (in_array($n2, $arr)) {	
            $count += 1;
        }
         if (in_array($n3, $arr)) {	
            $count += 1;
        }
         if (in_array($n4, $arr)) {	
            $count += 1;
        }
         if (in_array($n5, $arr)) {	
            $count += 1;
        }
         if (in_array($n6, $arr)) {	
            $count += 1;
        }
         if (in_array($n7, $arr)) {	
            $count += 1;
        }
         if (in_array($n8, $arr)) {	
            $count += 1;
        }
         if (in_array($n9, $arr)) {	
            $count += 1;
        }
         if (in_array($n10, $arr)) {	
            $count += 1;
        }
        $k = 0;
        $priz = 0;
        
        if ($l == 1){
            if($count == 1){
                $k = 3.8;
            }
        }
        if($l == 2){
            if($count == 1){
                $k = 1.7;
            }
            if($count == 2){
                $k = 5.2;
            }
        }
        if ($l == 3){
            if($count == 1){
                $k = 0;
            }
            if ($count == 2){
                $k = 2.7;
            }
            if($count == 3){
                $k = 48;
            }
            }
        if($l == 4){
            if($count == 1){
                $k = 0;
            }
            if($count == 2){
                $k = 1.7;
            }
            if($count == 3){
                $k = 10;
            }
            if($count == 4){
                $k = 84;
            }
        }
        if($l == 5){
            if($count == 1){
                $k = 0;
            }
            if($count == 2){
                $k = 1.4;
            }
            if($count == 3){
                $k = 4;
            }
            if($count == 4){
                $k = 14;
            }
            if($count == 5){
                $k = 290;
            }
        }
        if ($l == 6){
            if($count == 1){
                $k = 0;
            }
            if ($count == 2){
                $k=0;
            }
            if($count == 3){
            $k = 3;
            }
            if($count == 4){
                $k = 9;
            }
            if($count == 5){
                $k = 160;
            }
            if($count == 6){
                $k = 720;
            }
        }
        if($l == 7){
            if($count == 1){
                $k = 0;
            }
            if($count == 2){
                $k = 0;
            }
            if($count == 3){
                $k = 2;
            }
            if ($count == 4){
                $k = 7;
            }
            if($count == 5){
                $k = 30;
            }
            if($count == 6){
                $k = 280;
            }
            if ($count == 7){
                $k = 800;
            }
        }
        if($l == 8){
            if($count == 1){
                $k = 0;
            }
            if($count == 2){
                $k = 0;
            }
            if($count == 3){
                $k = 2;
            }
            if ($count == 4){
                $k = 4;
            }
            if($count == 5){
                $k = 10;
            }
            if($count == 6){
                $k = 50;
            }
            if ($count == 7){
                $k = 300;
            }
            if ($count == 8){
                $k = 850;
            }
        }
        if($l == 9){
            if($count == 1){
                $k = 0;
            }
            if($count == 2){
                $k = 0;
            }
            if($count == 3){
                $k = 2;
            }
            if ($count == 4){
                $k = 2.5;
            }
            if($count == 5){
                $k = 4.5;
            }
            if($count == 6){
                $k = 12;
            }
            if ($count == 7){
                $k = 60;
            }
            if ($count == 8){
                $k = 320;
            }
            if ($count == 9){
                $k = 900;
            }
            
        }
        if($l == 10){
            if($count == 1){
                $k = 0;
            }
            if($count == 2){
                $k = 0;
            }
            if($count == 3){
                $k = 1.5;
            }
            if ($count == 4){
                $k = 2;
            }
            if($count == 5){
                $k = 4;
            }
            if($count == 6){
                $k = 6;
            }
            if ($count == 7){
                $k = 22;
            }
            if ($count == 8){
                $k = 80;
            }
            if ($count == 9){
                $k = 400;
            }
            if ($count == 10){
                $k = 1000;
            }
        }
        
     }
     $priz = $summa_keno * $k;
     
     $result = array(
'success' => "$fa",
'error' => "$mess",
'arr' => "Вам выпали эти числа: $n<hr> Вы угадали: $count <hr>Ваш коэффициент: $k x <hr>Ваш выигрыш: $priz p <hr> Вы выбрали эти числа: $arr1 $arr2 $arr3 $arr4 $arr5 $arr6 $arr7 $arr8 $arr9 $arr10"

    );
    
}
if($type == "coinflip") {
    $sum = $_POST['size'];
    $lay = $_POST['lay'];
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
}
if($sum > $balance) {
    $error = 1;
    $messs = "Недостаточно средств";
    $fa = "fatal";
}
if($sum < 1) {
    $error = 2;
    $messs = "Сумма ставки от 1";
    $fa = "fatal";
}
if(!is_numeric($sum)) {
    $error = 3;
    $messs = "Введите сумму корректно";
    $fa = "fatal";
}
if(!$_SESSION['hash']) {
    $error = 4;
    $messs = "Авторизуйтесь!";
    $fa = "fatal";
}
    if($error == 0) {
    $rnd = rand(0,21);
    if($rnd <= 10) {
        $rand = 1;
    }
    if($rnd > 10 && $rand < 21) {
        $rand = 2;
    }
    if($rnd == 21) {
        $rand = 30;
    }
    if($lay == 1) {
        if($rnd <= 10) {
            $newbalance = $balance + ($sum * 2) - $sum;
            $fsum = $sum * 2;
            $update_sql2 = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
            $mess = "Выпал 'Орёл', вы выиграли $fsum";
             $fa = "success";
        }else{
            $newbalance = $balance - $sum;
            $update_sql2 = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
            $mess = "Вы проиграли";
             $fa = "error";
        }
    }
   // сторона 2
   if($lay == 2) {
        if($rnd > 10 && $rnd < 21) {
            $newbalance = $balance + ($sum * 2) - $sum;
            $fsum = $sum * 2;
            $update_sql2 = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
            $mess = "Выпала 'Решка', вы выиграли $fsum";
             $fa = "success";
        }else{
            $newbalance = $balance - $sum;
            $update_sql2 = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
            $mess = "Вы проиграли";
             $fa = "error";
        }
    }
    // сторона 3
    if($lay == 30) {
        if($rnd == 21) {
            $newbalance = $balance + ($sum * 10) - $sum;
            $fsum = $sum * 10;
            $update_sql2 = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
            $mess = "Выпал 'Орёл', вы выиграли $fsum";
             $fa = "success";
        }else{
            $newbalance = $balance - $sum;
            $update_sql2 = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
            $mess = "Вы проиграли";
             $fa = "error";
        }
    }
    }
     $result = array(
'success' => "$fa",
'error' => "$messs",
'flipResult' => "$rand",
'balance' => "$balance",
'new_balance' => "$newbalance",
'message' => "$mess"
    );
}
if($type == "wheel") {
    $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
}
    $size = $_POST['size'];
    $colorWheel = $_POST['color'];
    if($size > $balance) {
        $error = 1;
        $mess = "Недостаточно средств";
        $fa = "fatal";
    }
if($size < 1) {
    $error = 2;
    $messs = "Сумма ставки от 1";
    $fa = "fatal";
}
    if(!is_numeric($size)) {
        $error = 2;
        $mess = "Введите сумму корректно";
        $fa = "fatal";
    }
    if(!$_SESSION['hash']) {
        $error = 3;
        $mess = "Авторизуйтесь";
        $fa = "fatal";
    }
    if($error == 0) {
        $arrayWheel = [5,2,3,2,3,2,3,2,5,2,5,2,3,2,3,2,3,2,5,2,5,2,3,2,3,2,3,2,3,2,3,2,5,2,5,2,3,2,3,2,3,2,5,2,5,2,3,2,3,2,3,2,5,50];
    $randWheel =  mt_rand(6,50); //цвет который выпадет
    $valuesWheel = $arrayWheel[$randWheel];    
    // получаем цвет
     if ($arrayWheel[$randWheel] == 2) {
    $key2Wheel = [
       1,  3,  5,  7,  9, 11, 13, 15,
      17, 19, 21, 23, 25, 27, 29, 31,
      33, 35, 37, 39, 41, 43, 45, 47,
      49, 51
    ];
    $random = mt_rand(0,25);
    $key = $key2Wheel[$random];
  }
  if ($arrayWheel[$randWheel] == 3) {
    $key3Wheel = [
      2,4,6,
      12,14,16,
      22,24,26,
      28,30,36,
      38,40,46,
      48,50
    ];
    $random = mt_rand(0,16);
    $key = $key3Wheel[$random];
  }
  if ($arrayWheel[$randWheel] == 5) {
    $key5Wheel = [
      0,8,10,18,20,32,34,42,44,52
    ];
    $random = mt_rand(0,9);
    $key = $key5Wheel[$random];
  }
  if ($valuesWheel == 50) {
    $key5Wheel = [
      53
    ];
    $key = 53;
  }
 
  if($colorWheel == $valuesWheel){ // вы выиграли
  $newbalance = $balance + ($size * $colorWheel) - $size;
  $win = $size * $colorWheel;
  $query = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
  $mess = "Вы выиграли <b>$win</b>";
  $type = "success";
  }
  if($colorWheel != $valuesWheel){
  $newbalance = $balance - $size;
  $query = mysql_query("UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'");
  $mess = "Вы проиграли";
  $type = "error";
  }
  $fa = "success";
    }
    
    $result = array(
'valuesWheel' => "$valuesWheel",
'key' => "$key",
'success' => "$fa",
'error' => "$messs",
'balance' => "$balance",
'new' => "$newbalance",
'mess' => "$mess",
'type' => "$type"
    );
}
if($type == "play_pvp") {
    $id = $_POST['id'];
    
    //Пользователь который ставит ставку
 $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$login = $row['login'];
$balance = $row['balance'];

}
//BИнформация о игре
 $sql_select = "SELECT * FROM pvp WHERE id='$id'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$user2 = $row['user2'];
$user1 = $row['user1'];
$summa = $row['priz'];
$priz=($row['priz']  + $row['priz'] )*0.9 ;
}

   if($user1 ==  $login){
    $error = 1;
     $fa = "error";
    $mess = "Нельзя играть с самим собой ";
}
 
if($balance < $summa){
    $error = 1;
     $fa = "error";
    $mess = "Недостаточно средств";
}

if($error == 0)
{ 
	//Cписываем баланс
	$newbalance = $balance - $summa;
    mysql_query("update kot_user set balance=$newbalance where hash='$sid'");
   //Помещение в базу данных второго участника
    mysql_query("update pvp set user2='$login' where id='$id'");
        
        $sql_select = "SELECT * FROM pvp WHERE id='$id'";
        $result = mysql_query($sql_select);
        $row = mysql_fetch_array($result);
        if($row)
        {	
        $user2 = $row['user2'];
        $user1 = $row['user1'];
        $summa = $row['priz'];
        $priz=($row['priz']  + $row['priz'] )*0.9 ;
        }
    
       //Выбор победителя
       $r = rand(1,2);
       $time = time();
       if ($r == 1){
           //Помещаем победителя в победители
            mysql_query("update pvp set userwin='$user1' where id='$id'");
            mysql_query("update pvp set time=$time where id='$id'");
            //Инфа о победителе
                $sql_select = "SELECT * FROM kot_user WHERE login='$user1'";
                $result = mysql_query($sql_select);
                $row = mysql_fetch_array($result);
                if($row)
                {	
                $balance = $row['balance'];
                
                }
          //Начисляем баланс победителю
          $newbalance = $priz + $balance;
            mysql_query("update kot_user set balance=$newbalance where login='$user1'");


       }
        if ($r == 2){
           //Помещаем победителя в победители
            mysql_query("update pvp set userwin='$user2' where id='$id'");
            mysql_query("update pvp set time=$time where id='$id'");
            //Инфа о победителе
                $sql_select = "SELECT * FROM kot_user WHERE login='$user2'";
                $result = mysql_query($sql_select);
                $row = mysql_fetch_array($result);
                if($row)
                {	
                $balance = $row['balance'];
                
                }
          //Начисляем баланс победителю
          $newbalance = $priz + $balance;
            mysql_query("update kot_user set balance=$newbalance where login='$user2'");


       }


 


 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'text' => "$mess",
	'balance' => "$newbalance"

    );
}


if($type == "create_pvp") {
    $summa = $_POST['summa'];
 $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$login = $row['login'];
$balance = $row['balance'];

}
    
if($balance < $summa){
    $error = 1;
     $fa = "error";
    $mess = "Недостаточно средств";
}
if( $summa < 1 ){
    $error = 1;
     $fa = "error";
    $mess = "Ставки от 1р";
}
if(!is_numeric($summa)) {
          $error = 1;
     $fa = "error";
    $mess = "Ставки от 1р";
        
    }
if($error == 0)
{ 
	
    $update_sql2 = "insert into pvp  (user1, priz) values ('$login', '$summa')";
    mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 
$newbalance = $balance - $summa;
    $update_sql2 = "update kot_user set balance=$newbalance where hash='$sid'";
    mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());



 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'text' => "$mess",
	'balance' => "$newbalance"

    );
}
if($type == "banoff") {

     $ban = $_POST['ban'];
         $message = 'Пользователь <b>'.$ban.' </b>был разблокирован';
$login = '<font color="red">System</font>';
$img = "https://sun9-57.userapi.com/c205820/v205820318/6f527/YF1QSArbx2Q.jpg";

if($ban ==
' ' or $ban == ''){
  $error = 2;
  $fa = "error";
  $mess = "Введите сообщение";
}


if($error == 0)
{ 
	
  $update_sql2 = "insert into kot_messages  (name, img, text) values ('$login', '$img', '$message')";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 

$update_sql2 = "update kot_user set chat_ban='0' where login='$ban'";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());


 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",

    );
}
if($type == "banon") {

     $ban = $_POST['ban'];
         $message = 'Пользователь <b>'.$ban.' </b>был заблокирован';
$login = '<font color="red">System</font>';
$img = "https://sun9-57.userapi.com/c205820/v205820318/6f527/YF1QSArbx2Q.jpg";

if($ban ==
' ' or $ban == ''){
  $error = 2;
  $fa = "error";
  $mess = "Введите сообщение";
}


if($error == 0)
{ 
	
	//  $update_sql2 = "insert into kot_messages  (name, img, text) values ('$login', '$img', '$message')";
	  
	//  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 

$update_sql2 = "update kot_user set ban='1' where ip='$ban'";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());


 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",

    );
}
if($type == "promo") {
    $message = $_POST['promo'];
     
$login = '<font color="red">Новый промокод</font>';
$img = "https://sun9-36.userapi.com/c206520/v206520399/6e90c/5_E2TOFAi64.jpg";

if($message ==
' ' or $message == ''){
  $error = 2;
  $fa = "error";
  $mess = "Введите сообщение";
}


if($error == 0)
{ 
	
	  $update_sql2 = "insert into kot_messages  (name, img, text,promo) values ('$login', '$img', '$message', '1')";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 




 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",

    );
}
if($type == "clean") {
    
     
$login = '<font color="red">System</font>';
$img = "https://sun9-57.userapi.com/c205820/v205820318/6f527/YF1QSArbx2Q.jpg";
$message = "Была произведена очистка чата";
if($error == 0)
{ 
	 $update_sql2 = "TRUNCATE kot_messages";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	  $update_sql2 = "insert into kot_messages  (name, img, text) values ('$login', '$img', '$message')";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 

 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",

    );
}
if($type == "system") {
    $message = $_POST['message'];
     
$login = '<font color="red">System</font>';
$img = "https://sun9-57.userapi.com/c205820/v205820318/6f527/YF1QSArbx2Q.jpg";

if($message ==
' ' or $message == ''){
  $error = 2;
  $fa = "error";
  $mess = "Введите сообщение";
}


if($error == 0)
{ 
	
	  $update_sql2 = "insert into kot_messages  (name, img, text) values ('$login', '$img', '$message')";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 

 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",

    );
}
if($type == "mess") {
    $message = $_POST['message'];
     $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$login = $row['login'];
$img = $row['img'];
$ban = $row['chat_ban'];
}
if($ban == '1'){
  $error = 2;
  $fa = "error";
  $mess = "Вы забанены";
}
if($message ==
' ' or $message == ''){
  $error = 2;
  $fa = "error";
  $mess = "Введите сообщение";
}


if($error == 0)
{ 
	
	  $update_sql2 = "insert into kot_messages  (name, img, text) values ('$login', '$img', '$message')";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 




 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",

    );
}
if($type == "pvp2") {
    $id1 = $_POST['id1'];
    $pvpsumma1 = 10;
     $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$login = $row['login'];
$img = $row['img'];
}
  


if($balance < 1){
  $error = 3;
  $fa = "error";
  $mess = "Недостаточно средств!";
}
if($pvpsumma1 > $balance){
  $error = 2;
  $fa = "error";
  $mess = "Недостаточно средств!";
}


if($error == 0)
{ 
	$balancenew = $balance - $pvpsumma1;
	  $update_sql2 = "Update kot_user set balance =$balancenew WHERE hash='$sid'";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 

mysql_query("update kot_pvp set img1=$img WHERE id='1'");


 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$balancenew"
    );
}
if($type == "getpvp") {
  $id = $_POST['id'];
  $selecter1 = "SELECT * FROM kot_pvp WHERE id = '$id'";
$result_select1 = mysql_query($selecter1);
$row1 = mysql_fetch_array($result_select1);
if($row1)
{	
$login = $row1['login'];

}
$login="67";

    $fa = "success";
  
    $result = array(
    'success' => "$fa",
	'error' => "$mess",
	'img1' => "$login",
	
    'id' => "$id"
      
    ); 
  
}
if($type == "pvp") {
    $pvpsumma = $_POST['pvpsumma'];
     $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$login = $row['login'];
$img = $row['img'];
}
  


if($balance < 1){
  $error = 3;
  $fa = "error";
  $mess = "Недостаточно средств!";
}
if($pvpsumma > $balance){
  $error = 2;
  $fa = "error";
  $mess = "Недостаточно средств!";
}


if($error == 0)
{ 
	$balancenew = $balance - $pvpsumma;
	  $update_sql2 = "Update kot_user set balance =$balancenew WHERE hash='$sid'";
	  
	  mysql_query($update_sql2) or die("Ошибка вставки" . mysql_error());
	 

mysql_query("insert into kot_pvp ( img2, summa) VALUES ('$img', '$pvpsumma')");
	 



	 


 $fa = "success";
	  
}
// массив для ответа
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$balancenew"
    );
}
if($type == "dice") {
    $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$id1 = $row['id'];
$balance = $row['balance'];
$bonus = $row['bonus'];
$win = $row['win'];
$sliv = $row['sliv'];
}

$sql_select33 = "SELECT SUM(suma) FROM kot_payments WHERE user_id='$id1'";
$result33 = mysql_query($sql_select33);
$row = mysql_fetch_array($result33);
if($row)
{	
$deps = $row['SUM(suma)'];
}

    $cel = $_POST['celwin'];
    $rand = rand(1000,7000) / 100;
    $sum = $_POST['betsize'];
if($sum > $balance) {
    $error = 1;
    $mess = "Недостаточно средств";
    $fa = "fatal";
}
if(!is_numeric($sum)) {
    $error = 2;
    $mess = "Введите сумму корректно";
    $fa = "fatal";
}

if ($cel < 2){
$error = 1;
    $mess = "Ошибка";
    $fa = "fatal";
}
if ($cel > 98){
$error = 1;
    $mess = "Ошибка";
    $fa = "fatal";
}
if($sum < 1) {
    $error = 3;
    $mess = "Минимальная сумма - 1";
    $fa = "fatal";
}
if($cel < 1) {
    $error = 4;
    $mess = "Минимальный шанс - 1%";
    $fa = "fatal";
}
if(!is_numeric($cel)) {
    $error = 5;
    $mess = "Ошибка";
    $fa = "fatal";
}
if(!$_SESSION['hash']) {
    $error = 6;
    $mess = "Авторизуйтесь";
    $fa = "fatal";
}
if($error == 0 and $cel > 0) {
    
        if($cel <= $rand) {


            $win1 = round((100 - $cel), 2);
    $win = round((99 / $win1), 2);
    $newbalance = $balance + round((($sum * $win) - $sum),2);
       $update_sql2 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql2);      
$fa = "success";
}
if($cel > $rand) {

    $newbalance = $balance - $sum;
       $update_sql2 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql2);      
    $fa = "error";
}
       
    }
    $result = array(
'success' => "$fa",
'error' => "$mess",
'num' => "$rand",
'balance' => "$balance",
'new_balance' => "$newbalance"
    );
}
if($type == "battledice") {
  $sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
}
  $rand = rand(1, 999);
  $team = $_POST['team'];
  $per = $_POST['per'];
  $sum = $_POST['sum'];
  $win = round((100 / $per) * $sum, 2);
  if($team != 'red' && $team != 'blue' || $team == '') {
   $error = 1;
   $mess = "Выберите цвет";
   $fa = "fatal";
  }
  
  if($sum > $balance) {
   $error = 2;
   $mess = "Недостаточно средств";
   $fa = "fatal"; 
  }
  if(!is_numeric($sum)) {
   $error = 3;
   $mess = "Ошибка!";
   $fa = "fatal"; 
  }
  if($sum < 1) {
   $error = 4;
   $mess = "Ставки от 1";
   $fa = "fatal"; 
  }
  if($sum > $max_bet) {
   $error = 5;
   $mess = "Макс ставка - $max_bet";
   $fa = "fatal"; 
  }
  if($per < 1 || $per > 95) {
   $error = 6;
   $mess = "Шанс от 1 до 95";
   $fa = "fatal"; 
  }
  if(!is_numeric($per)) {
   $error = 7;
   $mess = "Ошибка!";
   $fa = "fatal"; 
  }
  if (!preg_match("#^[.0-9]+$#",$sum)) {
   $error = 8;
   $mess = "Недопустимые символы!";
   $fa = "fatal"; 
  }
  if (!preg_match("#^[.0-9]+$#",$per)) {
   $error = 8;
   $mess = "Недопустимые символы!";
   $fa = "fatal"; 
  }
  if(!$_SESSION['hash']) {
   $error = 9;
   $mess = "Авторизуйтесь";
   $fa = "fatal";  
  }
  
  if($error == 0) {
  if($team == "red") {
    $win_range = ($per / 100) * 999;
    if($rand > $win_range) {
       $newbalance = $balance - $sum;
     $update_sql2 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql2); 
     $mess = "Выпал билет $rand";
     $fa = "error"; 
    }
    if($rand <= $win_range) {
       $newbalance = ($balance + $win) - $sum;
     $update_sql2 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql2); 
     $fa = "success"; 
    }
  }
  
 if($team == "blue") {
   $win_range = 999 - ($per / 100) * 999;
    if($rand < $win_range) {
     $newbalance = $balance - $sum;
     $update_sql2 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql2); 
     $mess = "Выпал билет $rand";
     $fa = "error"; 
    }
    if($rand >= $win_range) {
       $newbalance = ($balance + $win) - $sum;
     $update_sql2 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql2); 
     $fa = "success"; 
    }
  }
  }
 
  $result = array(
	'success' => "$fa",
	'error' => "$mess",
    'number' => "$rand",
    'win' => "$win",
    'balance' => "$balance",
    'new_balance' => "$newbalance"
    );
}
if($type == "resetPassPanel") {
 $newpass = $_POST['newPass'];
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$newpass)) {
    $error = 1;
	$mess = "Недопустимые символы";
	$fa = "error";	
  }
  if($error == 0) {
 $update_sql2 = "UPDATE kot_user SET pass = '$newpass' WHERE hash = '$sid'";
    mysql_query($update_sql2); 
  $fa = "success";
  }
  $result = array(
	'success' => "$fa",
	'error' => "$mess"
    );
}
if($type == "deposit")
{
	
$size = $_POST['sum'];
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$bala = $row['balance'];
$user_id = $row['id'];
}
 
  if($error == 0) {
$podpis = md5($fk_id.':'.$size.':'.$fk_secret_1.':'. $user_id);
  $fa = "success";
}
    $result = array(
	'success' => "success",
	'locations' => "http://www.free-kassa.ru/merchant/cash.php?m=".$fk_id."&oa={$size}&o={$user_id}&s=".$podpis.""
    );
  

}
if($type == "continue_reg") {
  $login = $_POST['login'];
  $pass = $_POST['pass'];
  $dllogin = strlen($login);
  $dlpass = strlen($pass);
  $sql_select1 = sprintf("SELECT COUNT(*) FROM kot_user WHERE login='%s'", mysql_real_escape_string($login));
$result1 = mysql_query($sql_select1);
$row = mysql_fetch_array($result1);
if($row)
{
$count = $row['COUNT(*)'];
}  
  if($login == '' || $pass == '') {
   $error = 1;
   $mess = "Заполните все поля";
   $fa = "error";
  }
  if($login != '' && $pass != '') {
  if($dllogin < 5 || $dllogin > 15) {
   $error = 2;
   $mess = "Логин от 5 до 15 симв.";
   $fa = "error";
  }
  if($dlpass < 6 || $dlpass > 12) {
   $error = 3;
   $mess = "Пароль от 6 до 12 симв.";
   $fa = "error";
  }
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$login)) {
    $error = 4;
	$mess = "Недопустимые символы";
	$fa = "error";	
  }
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$pass)) {
    $error = 5;
	$mess = "Недопустимые символы";
	$fa = "error";	
  } 
  if($count >= 1) {
    $error = 6;
	$mess = "Логин занят";
	$fa = "error";	
  }
  }
  if($error == 0) {
    $update_sql1 = "UPDATE kot_user SET login = '$login' WHERE hash = '$sid'";
    mysql_query($update_sql1);
    $update_sql2 = "UPDATE kot_user SET pass = '$pass' WHERE hash = '$sid'";
    mysql_query($update_sql2);
    $fa = "success";
  }
  $result = array(
	'success' => "$fa",
	'error' => "$mess"
    );
}
if($type == "registration") {
  $login1 = $_POST['login'];
  $pass = $_POST['pass'];
  $repeatpass = $_POST['repeatpass'];
  $dllogin = strlen($login1);
  $dlpass = strlen($pass);
  $sql_select1 = sprintf("SELECT COUNT(*) FROM kot_user WHERE login='%s'", mysql_real_escape_string($login1));
$result1 = mysql_query($sql_select1);
$row = mysql_fetch_array($result1);
if($row)
{
$count = $row['COUNT(*)'];
}
$ip_c = $_SERVER['REMOTE_ADDR'];
$sql_select11 = sprintf("SELECT COUNT(*) FROM kot_user WHERE ip='%s'", mysql_real_escape_string($ip_c));
$result11 = mysql_query($sql_select11);
$row = mysql_fetch_array($result11);
if($row)
{
$count_ip = $row['COUNT(*)'];
}
  if($login1 == '' || $pass == '' || $repeatpass == '') {
    $error = 1;
    $mess = "Заполните все поля";
    $fa = "error";
  }
  if($pass != $repeatpass) {
    $error = 2;
    $mess = "Пароли не совпадают";
    $fa = "error";
  }
  if($login1 != '' && $pass != '' && $repeatpass != '') {
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$login1)) {
    $error = 3;
	$mess = "Недопустимые символы";
	$fa = "error";	
  }
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$pass)) {
    $error = 4;
	$mess = "Недопустимые символы";
	$fa = "error";	
  } 
  if($dllogin < 4 || $dllogin > 20) {
    $error = 5;
    $mess = "Логин от 4 до 20 симв.";
    $fa = "error";
  }
  if($count >= 1) {
    $error = 6;
    $mess = "Логин занят";
    $fa = "error";  
  }
  if($count_ip >= 1) {
    $error = 7;
    $mess = "Такой IP уже зареган";
    $fa = "error";
  }
    if($dlpass < 6 || $dlpass > 12) {
    $error = 8;
    $mess = "Пароль от 6 до 12 симв.";
    $fa = "error";
  }
  }
  if($error == 0) {
$ip = $_SERVER["REMOTE_ADDR"];
$ref = $_SESSION["ref"];
$datas = date("d.m.Y");
	$datass = date("H:i:s");
	$data = "$datas $datass";
	$chars3="qazxswedcvfrtgnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP"; 
$max3=32; 
$size3=StrLen($chars3)-1; 
$passwords3=null; 
while($max3--) 
$hash.=$chars3[rand(32,$size3)];
	$insert_sql1 = "INSERT INTO `kot_user` (`date_reg`, `ip`, `ref_id`, `login`, `pass`, `hash`, `balance`, `social`) 
	VALUES ('{$data}', '{$ip}', '{$ref}', '{$login}', '{$pass}', '{$hash}', '{$bonus_reg}', '');";
mysql_query($insert_sql1);
    $_SESSION['hash'] = $hash;
    $_SESSION['login'] = 1;
    $fa = "success";
  }
  $result = array(
	'success' => "$fa",
	'error' => "$mess"
    );
}
if($type == "login") {
  $login = $_POST['login'];
  $pass = $_POST['pass'];
  $sql_select1 = sprintf("SELECT COUNT(*) FROM kot_user WHERE login='%s' AND pass='%s'", mysql_real_escape_string($login), mysql_real_escape_string($pass));
$result1 = mysql_query($sql_select1);
$row = mysql_fetch_array($result1);
if($row)
{
$count = $row['COUNT(*)'];
}  
  if($login == '' || $pass == '') {
    $error = 1;
    $mess = "Заполните все поля";
    $fa = "error";
  }
  if($login != '' && $pass != '') {
  if($count == 0) {
    $error = 2;
    $mess = "Пользователь не найден";
    $fa = "error";
  }
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$login)) {
    $error = 3;
	$mess = "Недопустимые символы";
	$fa = "error";	
} 
  if (!preg_match("#^[aA-zZ0-9\-_]+$#",$pass)) {
    $error = 4;
	$mess = "Недопустимые символы";
	$fa = "error";
} 
  }
  if($error == 0) {
    $sql_select2 = sprintf("SELECT * FROM kot_user WHERE login='%s' AND pass='%s'", mysql_real_escape_string($login), mysql_real_escape_string($pass));
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{
$hash = $row['hash'];   
}
    $_SESSION['hash'] = $hash;
    $_SESSION['login'] = 1;
    $fa = "success";
  }
  $result = array(
	'success' => "$fa",
	'error' => "$mess"
    );
}
if($type == "deletewithdraw") {
  $id_delete = $_POST['del'];
$sql_select2 = "SELECT * FROM kot_user WHERE hash='$sid'";
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{
$user_id = $row['id'];
$login = $row['login'];
$ban = $row['ban'];
$balance = $row['balance'];
}
$sql_select3 = "SELECT * FROM kot_withdraws WHERE id='$id_delete'";
$result3 = mysql_query($sql_select3);
$row = mysql_fetch_array($result3);
if($row)
{
$user_id_w = $row['user_id'];
$sum = $row['sum'];
$status = $row['status'];
}  
if($status != 0) {
   $error = 1;
   $mess = "";
   $fa = "error";
}
if($user_id != $user_id_w) {
   $error = 2;
   $mess = "";
   $fa = "error";
}
  if($error == 0) {
    $delete = "DELETE FROM `kot_withdraws` WHERE id = '$id_delete'";
mysql_query($delete);
  $newbalance = $balance + $sum;
    $update_sql1 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql1);
    $fa = "success";
  }
  $result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$newbalance"
    );
}
if($type == "withdrawuser") {

$sql_select2 = "SELECT * FROM kot_user WHERE hash='$sid'";
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{
$user_id = $row['id'];
$login = $row['login'];
$ban = $row['ban'];
$balance = $row['balance'];
}
$sql_select23 = "SELECT SUM(suma) FROM kot_payments WHERE user_id='$user_id'";
$result23 = mysql_query($sql_select23);
$row = mysql_fetch_array($result23);
if($row)
{
$sumdep = $row['SUM(suma)'];
}
if($sumdep == '') {
 $sumdep = 0; 
}
$system = $_POST['system'];
$wallet = $_POST['wallet'];
$sum = $_POST['sum'];
  if($system == 4) {
      $ps = "qiwi";
    }
    if($system == 2) {
      $ps = "payeer";
    }
    if($system == 3) {
      $ps = "wm";
    }
    if($system == 1) {
      $ps = "ya";
    }
    if($system == 5) {
      $ps = "beeline";
    }
    if($system == 6) {
      $ps = "megafon";
    }
    if($system == 7) {
      $ps = "mts";
    }
    if($system == 8) {
      $ps = "tele";
    }
    if($system == 9) {
      $ps = "visa";
    }
    if($system == 10) {
      $ps = "mc";
    }
$dwallet = strlen($wallet);
if($wallet == '' || $sum == '') {
  $error = 1;
  $mess = "Заполните все поля";
  $fa = "error";
}
if($sum > $balance) {
  $error = 2;
  $mess = "Недостаточно средств";
  $fa = "error";
}
if($ban == 1) {
  $error = 3;
  $mess = "Ваш аккаунт заблокирован";
  $fa = "error";
}
  if($sum != '' && $wallet != '') {
if(!is_numeric($sum)) {
  $error = 4;
  $mess = "Введите корректную сумму";
  $fa = "error";
}
if($dwallet < 8 || $dwallet > 20) {
  $error = 5;
  $mess = "Кошелек от 8 до 20 символов";
  $fa = "error";
}
  
if($sum < $min_withdraw_sum) {
  $error = 6;
  $mess = "Минимальная сумма вывода $min_withdraw_sum";
  $fa = "error";
}
if (!preg_match("#^[aA-zZ0-9\-_.]+$#",$sum)) 
{
	$mess = "Недопустимые символы в сумме";
	$fa = "error";
	$error = 7;
} 
if (!preg_match("#^[+0-9PpWw]+$#",$wallet)) 
{
	$mess = "Недопустимые символы в реквизитах";
	$fa = "error";
	$error = 8;
}
if($sumdep < $dep_withdraw) {
    $mess = "Пополните баланс на $dep_withdraw";
    $error = 9;
	$fa = "error";
	
  }
  }
  if($error == 0) {
    $summ = round($sum, 2);
    $newbalance = $balance - $sum;
    $datas = date("d.m.Y");
	$datass = date("H:i:s");
	$data = "$datas $datass";
    $insert_sql11 = "INSERT INTO `kot_withdraws` (`id`, `user_id`, `ps`, `wallet`, `sum`, `date`, `status`) VALUES (NULL, '$user_id', '$ps', '$wallet', '$summ', '$data', '0');";
    mysql_query($insert_sql11); 
    $update_sql1 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql1);
    $fa = "success";
}
  $result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$newbalance"
    );
}
if($type == "createPromoUser") {

$sql_select2 = "SELECT * FROM kot_user WHERE hash='$sid'";
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{
    $login = $row['login'];
$user_id = $row['id'];
$ban = $row['ban'];
$balance = $row['balance'];
}
$name = $_POST['createname'];
$sum = $_POST['createsum'];
$act = $_POST['createactive'];
$sql_select4 = sprintf("SELECT COUNT(*) FROM kot_promo WHERE name='%s'", mysql_real_escape_string($name));
$result4 = mysql_query($sql_select4);
$row = mysql_fetch_array($result4);

if($row)
{
$count = $row['COUNT(*)'];
}
$sql_select23 = "SELECT SUM(suma) FROM kot_payments WHERE user_id='$user_id'";
$result23 = mysql_query($sql_select23);
$row = mysql_fetch_array($result23);
if($row)
{
$sumdep = $row['SUM(suma)'];
}
if($sumdep == '') {
 $sumdep = 0; 
}
if($sumdep < 40 && $adm == 0 ){
  $error = 1;
  $mess = "Для создания промо пополните баланс на 40₽";
  $fa = "error";
}
if($name == '' || $sum == '' || $act == '') {
  $error = 1;
  $mess = "Заполните все поля";
  $fa = "error";
}
if($ban == 1) {
  $error = 2;
  $mess = "Ваш аккаунт заблокирован";
  $fa = "error";
}
if(($sum * $act) > $balance) {
  $error = 3;
  $mess = "Недостаточно средств";
  $fa = "error";
}
  if($name != '' && $sum != '' && $act != '') {
  if($sum < 1) {
  $error = 4;
  $mess = "Сумма от 1";
  $fa = "error";
}
  if($act < 1) {
  $error = 5;
  $mess = "Кол-во от 1";
  $fa = "error";
}
  if(!is_numeric($sum)) {
  $error = 6;
  $mess = "Сумма цифрами";
  $fa = "error";
}
  if(!is_numeric($act)) {
  $error = 7;
  $mess = "Кол-во цифрами";
  $fa = "error";
}
  if (!preg_match("#^[а-яА-ЯaA-zZ0-9\-_]+$#",$name)) {
   $error = 8;
   $mess = "Недопустимые символы";
   $fa = "error";
}   
  if($count > 0) {
  $error = 9;
  $mess = "Промокод уже существует";
  $fa = "error";
}  
}
  if($error == 0) {
    $datas = date("d.m.Y");
	$datass = date("H:i:s");
	$data = "$datas $datass";
  $newbalance = $balance - ($sum * $act);
  $insert_sql1 = "INSERT INTO `kot_promo` (`id`, `date`, `name`, `sum`, `active`, `actived`, `id_active`, `login`) VALUES (NULL, '$data', '$name', '$sum', '$act', '0', '', '$login');";
    mysql_query($insert_sql1);
    $update_sql1 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql1);
    $fa = "success";
}
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$newbalance"
    );
}
if($type == "activePromo") {
  
$sql_select2 = "SELECT * FROM kot_user WHERE hash='$sid'";
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{
$tp = $row['tp'];
$bonus = $row['bonus'];
$user_id = $row['id'];
$ban = $row['ban'];
$balance = $row['balance'];
}
// инфу о пользователе мы получили, получаем промо
$promo = $_POST['promoactive']; // получаем введенное промо
$sql_select = sprintf("SELECT COUNT(*) FROM kot_promo WHERE name='%s'", mysql_real_escape_string($promo));
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$count = $row['COUNT(*)'];
$timep = 600 - (time() - $tp);
}
if($timep > 0){
    $error = 2;
    $mess = "Подождите $timep секунд";
    $fa = "error";
}
 if($balance > 5) {
    $error = 2;
    $mess = "Баланс больше 5";
    $fa = "error";
  }

  if($promo == '') {
    $error = 1;
    $mess = "Введите промокод";
    $fa = "error";
  }
  if($count == 0) {
    $error = 2;
    $mess = "Промокод не найден";
    $fa = "error";
  }
 if($count != 0) {
    $sql_select1 = "SELECT * FROM kot_promo WHERE name='$promo'";
$result1 = mysql_query($sql_select1);
$row = mysql_fetch_array($result1);
if($row)
{	
$sum = $row['sum'];
$limit = $row['active'];
$actived = $row['actived'];
$idactive = $row['id_active'];
}
  }
if($sum > 40000000) {
    $error = 2;
    $mess = "Сумма промокода больше чем должна быть";
    $fa = "error";
  }
  if($count == 1) {
  if($limit == $actived || $actived > $limit) {
    $error = 3;
    $mess = "Активации закончились";
    $fa = "error";
  }
  if($ban == 1) {
    $error = 4;
    $mess = "Ваш аккаунт заблокирован";
    $fa = "error";
  }
  }
  if (preg_match("/$user_id /",$idactive))  {	
	$error = 5;
    $mess = "Вы уже активировали этот код";
    $fa = "error";
   }
  if($error == 0) {
      $time = time();
$newbonus = $bonus +( $sum * 8);
    $newbalance = $balance + $sum;
    $newactive = $actived + 1;
    $newid = "$user_id $idactive";
    $update_sql1 = "UPDATE kot_user SET balance = '$newbalance' WHERE hash = '$sid'";
    mysql_query($update_sql1);

  $update_sql1 = "UPDATE kot_user SET bonus = '$newbonus' WHERE hash = '$sid'";
    mysql_query($update_sql1);
    // обновляем бд (2)
    $update_sql2 = "UPDATE kot_promo SET actived = '$newactive' WHERE name = '$promo'";
    mysql_query($update_sql2); 
    // обновляем бд (3)
    $update_sql3 = "UPDATE kot_promo SET id_active = '$newid' WHERE name = '$promo'";
    mysql_query($update_sql3);
    $update_sql3 = "UPDATE kot_user SET tp = '$time' WHERE hash = '$sid'";
    mysql_query($update_sql3);
    $fa = "success";
  }
  $result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$newbalance"
    );
}
if($type == "bonus")	
{
    $datab = Date("dmY");
$min_bonus_size = $min_bonus_s * 100;
$max_bonus_size = $max_bonus_s * 100;
	
$sql_select = "SELECT * FROM kot_user WHERE hash='$sid'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$bonus = $row['bdate'];
$balance = $row['balance'];
$vk = $row['social'];
}
if($bonus == $datab){
$error = 1;
$fa = "error";
$mess = "Вы получали бонус за эти 24 часа";
}
if($vk == '') {
$error = 2;
$fa = "error";
$mess = "Для получения войдите через вк";    
}
if($vk != '') {
    $user = explode( 'vk.com', $vk )[1];
$http = "http://";
$vks = str_replace($user, '', $vk);
$vks = str_replace($http, '', $vks);
if($vks == "vk.com" || $vks == "m.vk.com")
{
	//good
		$domainvk = explode( 'http://vk.com/id', $vk )[1];
if (!is_numeric($domainvk))
{
	$domainvk = explode( 'com/', $vk )[1];
}

		$vk1 = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$domainvk}&access_token=".$tokenvk."&v=5.74"));
        $vk1 = $vk1->response[0]->id;
	$resp = file_get_contents("https://api.vk.com/method/groups.isMember?group_id=".$idvk."&user_id={$vk1}&access_token=".$tokenvk."&v=5.74");
$data = json_decode($resp, true);
    

if($data['response']=='0')
{
$error = 3;
$fa = "error";
$mess = "Подпишитесь на группу";    
}
if($data['response']=='1')
{
}
}
}
if($error == 0) {
    $randomb = rand($min_bonus_size,$max_bonus_size) / 100;
    $balancenew = $balance + $randomb;
$update_sql = "Update kot_user set balance='$balancenew' WHERE hash='$sid'";
      mysql_query($update_sql) or die("Ошибка вставки" . mysql_error());
$update_sql1 = "Update kot_user set bdate='$datab' WHERE hash='$sid'";
      mysql_query($update_sql1) or die("Ошибка вставки" . mysql_error());
$fa = "success";

}
$result = array(
	'success' => "$fa",
	'error' => "$mess",
	'balance' => "$balance",
	'new_balance' => "$balancenew",
	'bonussize' => "$randomb"
    );
}

if($type == "exit") {
 unset($_SESSION['hash']); 
 unset($_SESSION['login']);
  $fa = "success";
  $result = array(
	'success' => "$fa",
	'error' => "$mess"
    );
}
if($type == "minbet") {
 // $winsum = $_POST['win'];
  
  $sum = $_POST['sum'];
  $per = $_POST['per'];
  $nwin = ($per * 10000) - 1;
  $winsum = round(((100 / $per * $sum) - $sum), 2);
  //$nwin = $_POST['nwin'];
  $sql_select2 = "SELECT * FROM kot_user WHERE hash='$sid'";
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{	
$balance = $row['balance'];
$ban = $row['ban'];
$sliv = $row['sliv'];
$fart = $row['win'];
$login = $row['login'];
$user_id = $row['id'];
}
  $sql_select27 = "SELECT COUNT(*) FROM kot_chance WHERE per='$per'";
$result27 = mysql_query($sql_select27);
$row = mysql_fetch_array($result27);
if($row)
{	
$get_per = $row['COUNT(*)'];
}
if($get_per != 0) {
  $sql_select0 = "SELECT * FROM kot_chance WHERE per='$per'";
$result0 = mysql_query($sql_select0);
$row = mysql_fetch_array($result0);
if($row)
{	
$active_chance = $row['active'];
$chance = $row['chance'];
$is_drop = $row['is_drop'];
}
}
  if($fart == 0 && $sliv == 0) {
  if($active_chance == 1) {
    if($chance == 0 && $is_drop == 0) {
       $rand = rand($nwin, 999999);
    }
    if($chance > 0 && $is_drop == 1) {
      $get_win = rand(1, 100);
      if($chance >= $get_win) {
        $rand = rand(0, $nwin);
      }
      if($chance < $get_win) {
        $rand = rand($nwin, 999999);
      }
    }
  }
  if($sliv == 0 && $active_chance == 0) {
  $rand = rand(0, 999999);
  }
  }
  if($sliv == 1) {
    $rand = rand($nwin, 999999);
  }
  if($fart == 1) {
    $rand = rand(0, $nwin);
  }
  $hash = hash('sha512', $rand);
  if((empty($_SESSION['hash'])) || $_SESSION['login'] != 1){
    $error = 2;
    $mess = "Авторизуйтесь";
    $fa = "error";
  }
     if($_SESSION['hash'] != '') {
       if($sum > $balance) {
         $newbalance = $balance;
         $error = 4;
         $mess = "Недостаточно средств";
         $fa = "error";
       }
       if($per > $max_per || $per < $min_per) {
         $newbalance = $balance;
         $error = 98;
         $mess = "% Шанс от $min_per до $max_per";
         $fa = "error";
       }
       if($ban == 1) {
         $newbalance = $balance;
         $error = 97;
         $mess = "Ваш аккаунт заблокирован";
         $fa = "error";
       }
       if($sum < $min_bet) {
         $newbalance = $balance;
         $error = 64;
         $mess = "Ставки от $min_bet";
         $fa = "error";
       }
       if($sum > $max_bet) {
         $newbalance = $balance;
         $error = 69;
         $mess = "Макс. ставка $max_bet";
         $fa = "error";
       }
       if(!is_numeric($sum)) {
           $newbalance = $balance;
         $error = 77;
         $mess = "Введите сумму корректно";
         $fa = "error";
          
       }
       if($error == 0) {
  if($rand <= $nwin)
  {
    $summ = round($sum, 2);
    $winsumm = round($winsum, 2) + $sum;
  $insert_sql1 = "INSERT INTO `kot_games` (`id`,`user_id`, `login`, `number`, `cel`, `sum`, `chance`, `type`, `win_summa`) 
	VALUES ('NULL', '$user_id', '$login', '$rand', '0 - $nwin', '$summ', '$per', 'win', '$winsumm');";
mysql_query($insert_sql1);
  $newbalance = $balance + $winsum;
    $update_sql4 = "Update kot_user set balance='$newbalance' WHERE hash='$sid'";
      mysql_query($update_sql4);
  $fa = "success";
  }
       }
       
       if($error == 0) {
  if($rand > $nwin)
  {
    $summ = round($sum, 2);
    $winsumm = round($winsum, 2) + $sum;
  $insert_sql1 = "INSERT INTO `kot_games` (`id`,`user_id`, `login`, `number`, `cel`, `sum`, `chance`, `type`, `win_summa`) 
	VALUES ('NULL', '$user_id', '$login', '$rand', '0 - $nwin', '$summ', '$per', 'lose', '0');";
mysql_query($insert_sql1);
  $newbalance = $balance - $sum;
    $update_sql4 = "Update kot_user set balance='$newbalance' WHERE hash='$sid'";
      mysql_query($update_sql4);
  $error = 1;
  $mess = "Выпало <b>$rand</b>";
  $fa = "error";
  }
   }  
     }
     
  $winning = $winsum + $sum;
  $result = array(
	'success' => "$fa",
	'error' => "$mess",
	'number' => "$rand",
    'hash' => "$hash",
    'fullwin' => "$winning",
    'balance' => "$balance",
    'new_balance' => "$newbalance"

    );
}
if($type == "maxbet") {
  // $winsum = $_POST['win'];
  $per = $_POST['per'];
  $nwin = 1000000 - ($per * 10000);
  //$nwin = $_POST['nwin'];
  $sum = $_POST['sum'];
  $winsum = round(((100 / $per * $sum) - $sum), 2);
  $sql_select2 = "SELECT * FROM kot_user WHERE hash='$sid'";
$result2 = mysql_query($sql_select2);
$row = mysql_fetch_array($result2);
if($row)
{
$balance = $row['balance'];
$ban = $row['ban'];
$sliv = $row['sliv'];
$fart = $row['win'];
$login = $row['login'];
$user_id = $row['id'];
}
 $sql_select27 = "SELECT COUNT(*) FROM kot_chance WHERE per='$per'";
$result27 = mysql_query($sql_select27);
$row = mysql_fetch_array($result27);
if($row)
{	
$get_per = $row['COUNT(*)'];
}
if($get_per != 0) {
  $sql_select0 = "SELECT * FROM kot_chance WHERE per='$per'";
$result0 = mysql_query($sql_select0);
$row = mysql_fetch_array($result0);
if($row)
{	
$active_chance = $row['active'];
$chance = $row['chance'];
$is_drop = $row['is_drop'];
}
}
  if($fart == 0) {
  if($active_chance == 1) {
    if($chance == 0 && $is_drop == 0) {
       $rand = rand(0, $nwin);
    }
    if($chance > 0 && $is_drop == 1) {
      $get_win = rand(1, 100);
      if($chance >= $get_win) {
        $rand = rand($nwin, 999999);
      }
      if($chance < $get_win) {
        $rand = rand(0, $nwin);
      }
    }
  }
  if($sliv == 0 && $active_chance != 1) {
  $rand = rand(0,999999);
  }
  }
  if($sliv == 1) {
    $rand = rand(0, $nwin);
  }
  if($fart == 1) {
    $rand = rand($nwin, 999999);
  }
  $hash = hash('sha512', $rand);
  if((empty($_SESSION['hash'])) || $_SESSION['login'] != 1){
    $error = 2;
    $mess = "Авторизуйтесь";
    $fa = "error";
  }
  
     if($_SESSION['hash'] != '') {
       if($sum > $balance) {
         $newbalance = $balance;
         $error = 4;
         $mess = "Недостаточно средств";
         $fa = "error";
       }
       if($per > $max_per || $per < $min_per) {
         $newbalance = $balance;
         $error = 98;
         $mess = "% Шанс от $min_per до $max_per";
         $fa = "error";
       }
       if($ban == 1) {
         $newbalance = $balance;
         $error = 97;
         $mess = "Ваш аккаунт заблокирован";
         $fa = "error";
       }
       if($sum < $min_bet) {
         $newbalance = $balance;
         $error = 64;
         $mess = "Ставки от $min_bet";
         $fa = "error";
       }
       if($sum > $max_bet) {
         $newbalance = $balance;
         $error = 69;
         $mess = "Макс. ставка $max_bet";
         $fa = "error";
       }
       if(!is_numeric($sum)) {
           $newbalance = $balance;
         $error = 77;
         $mess = "Введите сумму корректно";
         $fa = "error";
          
       }
       if($error == 0) {
  if($rand >= $nwin)
  {
    $summ = round($sum, 2);
    $winsumm = round($winsum, 2) + $sum;
  $insert_sql1 = "INSERT INTO `kot_games` (`id`,`user_id`, `login`, `number`, `cel`, `sum`, `chance`, `type`, `win_summa`) 
	VALUES ('NULL', '$user_id', '$login', '$rand', '$nwin - 999999', '$summ', '$per', 'win', '$winsumm');";
mysql_query($insert_sql1);
  $newbalance = $balance + $winsum;
    $update_sql4 = "Update kot_user set balance='$newbalance' WHERE hash='$sid'";
      mysql_query($update_sql4);
  $fa = "success";
  }
       } 
     }
    
       
       if($error == 0) {
  if($rand < $nwin)
  {
    $summ = round($sum, 2);
    $winsumm = round($winsum, 2) + $sum;
  $insert_sql1 = "INSERT INTO `kot_games` (`id`,`user_id`, `login`, `number`, `cel`, `sum`, `chance`, `type`, `win_summa`) 
	VALUES ('NULL', '$user_id', '$login', '$rand', '$nwin - 999999', '$summ', '$per', 'lose', '0');";
mysql_query($insert_sql1);
  $newbalance = $balance - $sum;
    $update_sql4 = "Update kot_user set balance='$newbalance' WHERE hash='$sid'";
      mysql_query($update_sql4);
  $error = 1;
  $mess = "Выпало <b>$rand</b>";
  $fa = "error";
  }
       }
     
     
  $winning = $winsum + $sum;
  $result = array(
	'success' => "$fa",
	'error' => "$mess",
	'number' => "$rand",
    'hash' => "$hash",
    'fullwin' => "$winning",
    'balance' => "$balance",
    'new_balance' => "$newbalance"

    );
}

	/* огромное спасибо за покупку, моя страница в вк - https://vk.com/id321223555 по всем вопросам и т.д */
    echo json_encode($result);
?>