<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
ini_set('error_reporting', E_ALL);
ini_set("log_errors","1");
ini_set("error_log","php-errors.log");

/*function getIP() {
   if(isset($_SERVER['HTTP_X_REAL_IP'])) return $_SERVER['HTTP_X_REAL_IP'];
   return $_SERVER['REMOTE_ADDR'];
}

if(getIP() != $_SERVER['SERVER_ADDR'] && getIP() != '127.0.0.1') 
	{
	
		die(getIP().'<br>'.$_SERVER['SERVER_ADDR']);
	}
*/

define('GAME',true);
include('_incl_data/__config.php');
include('_incl_data/class/__db_connect.php');
include('_incl_data/class/__user.php');

$body = file_get_contents('php://input');
$user = mysql_fetch_array(mysql_query('SELECT * FROM `users` WHERE `id` = "'.$body.'" LIMIT 1'));
$titm = mysql_fetch_array(mysql_query('SELECT * FROM `actions` WHERE `uid` = "'.$body.'" AND `vals` = "loto" AND `time` > '.(time()-24*60*60).'  ORDER BY `time` DESC LIMIT 1'));
if(isset($user) && $user['id']==$u->info['id'] )
{
	if ($titm==false)
	{
	// не редкий > t1 42% чуть чуть редкий > t2 25%  немного редкий > t3 15% редкий > t4 10% очень редкий > t5 5% невероятно редкий > t6 3%
	/*$droplist = array(
			0=>4514, t1
            1=>4515, t1
            2=>4688, t1  
            3=>4689, t1
            4=>4690, t1
            5=>5009, t2
            6=>5008, t2
            7=>5010, t2
            8=>5239, t2
            9=>5069, t2
            10=>10035, t1
            11=>10036, t1
            12=>10037, t1
            13=>10008, t3
            14=>10020, t3
            15=>6112, t4
            16=>6113, t4
            17=>6114, t4
            18=>6115, t4
            19=>6116, t4
            20=>6360, t6 
            21=>6462, t5
            22=>5022, t6
            23=>5023, t6 
            24=>6445, t5 
            25=>6455, t3
            26=>10009, t4
            27=>1032, t1
            28=>1031, t1
            29=>1034 t1
	);*/
	$nrnd=rand(1,100);
	if ($nrnd <= 1) //t6
	{
		$droplistt = mysql_query('SELECT * FROM `loto` WHERE `type` = "t6"');
		$y=mysql_fetch_array(mysql_query('SELECT COUNT(`type`) AS `COUNT` FROM `loto` WHERE `type` = "t6"'));
		$rndt=rand(0,$y['COUNT']-1);
		$i=0;
		while($pl = mysql_fetch_array($droplistt)) 
		{
			if($i==$rndt)
			{
				$droplist = $pl;
			}
		$i++;
		}
	}
	elseif (($nrnd-1) <= 5) // t5
	{
		$droplistt = mysql_query('SELECT * FROM `loto` WHERE `type` = "t5"');
		$y=mysql_fetch_array(mysql_query('SELECT COUNT(`type`) AS `COUNT` FROM `loto` WHERE `type` = "t5"'));
		$rndt=rand(0,$y['COUNT']-1);
		$i=0;
		while($pl = mysql_fetch_array($droplistt)) 
		{
			if($i==$rndt)
			{
				$droplist = $pl;
			}
		$i++;
		}
	}
	elseif (($nrnd-1-5) <= 7) // t4
	{
		$droplistt = mysql_query('SELECT * FROM `loto` WHERE `type` = "t4"');
		$y=mysql_fetch_array(mysql_query('SELECT COUNT(`type`) AS `COUNT` FROM `loto` WHERE `type` = "t4"'));
		$rndt=rand(0,$y['COUNT']-1);
		$i=0;
		while($pl = mysql_fetch_array($droplistt)) 
		{
			if($i==$rndt)
			{
				$droplist = $pl;
			}
		$i++;
		}
	}
	elseif (($nrnd-1-5-7) <= 10) // t3
	{
		$droplistt = mysql_query('SELECT * FROM `loto` WHERE `type` = "t3"');
		$y=mysql_fetch_array(mysql_query('SELECT COUNT(`type`) AS `COUNT` FROM `loto` WHERE `type` = "t3"'));
		$rndt=rand(0,$y['COUNT']-1);
		$i=0;
		while($pl = mysql_fetch_array($droplistt)) 
		{
			if($i==$rndt)
			{
				$droplist = $pl;
			}
		$i++;
		}
	}
	elseif (($nrnd-1-5-7-10) <= 15) // t2
	{		
		$droplistt = mysql_query('SELECT * FROM `loto` WHERE `type` = "t2"');
		$y=mysql_fetch_array(mysql_query('SELECT COUNT(`type`) AS `COUNT` FROM `loto` WHERE `type` = "t2"'));
		$rndt=rand(0,$y['COUNT']-1);
		$i=0;
		while($pl = mysql_fetch_array($droplistt)) 
		{
			if($i==$rndt)
			{
				$droplist = $pl;
			}
		$i++;
		}
	}
	else // t1
	{
		$droplistt = mysql_query('SELECT * FROM `loto` WHERE `type` = "t1"');
		$y=mysql_fetch_array(mysql_query('SELECT COUNT(`type`) AS `COUNT` FROM `loto` WHERE `type` = "t1"'));
		$rndt=rand(0,$y['COUNT']-1);
		$i=0;
		while($pl = mysql_fetch_array($droplistt)) 
		{
			if($i==$rndt)
			{
				$droplist = $pl;
			}
		$i++;
		}
	}

	if (isset($droplist['id']))
		{
			$idve=$droplist['idgame'];
			$u->addItem($idve,$user['id'],'|frompisher=1|nosale=1'.$user['login']);
			$inf = mysql_fetch_array(mysql_query('SELECT * FROM `items_main` WHERE `id` = "'.$idve.'" LIMIT 1'));
			$titm = mysql_query('INSERT INTO `actions` (`uid`,`time`,`city`,`room`,`vars`,`ip`,`vals`) VALUES ("'.$user['id'].'","'.time().'","'.loto.'","'.$user['room'].'","'.$idve.'","'.mysql_real_escape_string($_SERVER['HTTP_X_REAL_IP']).'","loto")');
			mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#000000>Игрок <b>'.$user['login'].'</b> выиграл в бесплатной рулетке <b>'.$inf['name'].'</b>!</font>","capitalcity","","6","1","'.time().'")');
			echo  $droplist['id']; //			
		}
	else
		{
			echo  'Error';
		}
	}
	else
	{
		echo "Wait asign!";
	}
}
else 
{
		echo "No user!";
}
