<?
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') {
	if(!isset($_GET['test15684555'])) {
		die(getIP().'<br>'.$_SERVER['SERVER_ADDR']);
	}
}
define('GAME',true);
include('_incl_data/__config.php');
include('_incl_data/class/__db_connect.php');

//if(isset($_GET['test'])) {
	
	//Рейтинг по вкладам
	$ub = array();
	$ui = array();
	$sp = mysql_query('SELECT * FROM `pay_operation` WHERE `good` > 0 ORDER BY `id` DESC');
	while( $pl = mysql_fetch_array($sp) ) {
		if(!isset($ub[$pl['uid']])) {
			$ui[] = $pl['uid'];
		}
		$ub[$pl['uid']][date('d.m.Y',$pl['good'])] = $pl['ekr'];
	}
	$i = 0;
	while( $i < count($ui) ) {
		if(isset($ui[$i])) {
			$j = 1;
			$k = 0;
			$nj = 0;
			while( $j <= 1000 ) {
				$dt = date('d.m.Y',time() - (86400 * $j));
				if( isset($ub[$ui[$i]][$dt]) ) {
					$k++;
					$nj = 0;
				}elseif( $j > 0 && $nj >= 6 ) {
					$j = 1000;
				}else{
					$nj++;
				}
				$j++;
			}
			mysql_query('DELETE FROM `users_paybonus` WHERE `id` = "'.$ui[$i].'" LIMIT 1');
			mysql_query('INSERT INTO `users_paybonus` (`id`,`level`) VALUES (
				"'.$ui[$i].'","'.$k.'"
			)');
		}
		$i++;
	}
	
	$add_exp_list = array();
	$all_exp_list = array();
	$exp_list = array();
	$win_list = array();
	
	$test = mysql_fetch_array(mysql_query('SELECT `id` FROM `aaa_reting_list` WHERE `date` = "'.date('dmY').'" LIMIT 1'));
	if(isset($test['id'])) {
		mysql_query('DELETE FROM `aaa_reting_list` WHERE `date` = "'.date('dmY').'"');
	}
	unset($test);
		
	$sp = mysql_query('SELECT `id`,`login`,`win` FROM `users` WHERE `real` = 1 AND `admin` = 0');
	while( $pl = mysql_fetch_array($sp) ) {
		$st = mysql_fetch_array(mysql_query('SELECT `id`,`exp`,`repexp` FROM `stats` WHERE `id` = "'.$pl['id'].'" AND `bot` = 0 LIMIT 1'));
		$rl = mysql_fetch_array(mysql_query('SELECT `exp`,`global`,`exp_real` FROM `aaa_reting_list` WHERE `uid` = "'.$pl['id'].'" ORDER BY `id` DESC LIMIT 1'));
		
		$exp_list[$pl['id']] = $st['exp'];
		
		$win_list[$pl['id']] = $pl['win'];
		$log_list[$pl['id']] = $pl['login'];
		
		$add_exp = $st['exp']; // - $rl['exp_real'];
		if( date('d') == 1)
		{
		$global_exp = 0; //значение рейтинга обнуляется 1го числа
		}
		else
		{
			//$global_exp = 0;
			$global_exp = round($rl['global']);
		}
		$global_exp = $global_exp + round($st['repexp']);
		//$cp = mysql_query('SELECT `exp` FROM `aaa_reting_list` WHERE `uid` = "'.$pl['id'].'" ORDER BY `id` DESC LIMIT 100');
		//$dd = 0;
		//while( $cl = mysql_fetch_array($cp) ) {
		//	$global_exp += round($cl['exp']/100*(100-$dd));
		//	$dd++;
		//}
		
		$add_exp_list[$pl['id']] = $add_exp; //записываем сколько опыта получил за сегодня
		$all_exp_list[$pl['id']] = $global_exp;// + $add_exp; //записываем сколько опыта получил всего + сегодняшний				
	}
	unset($pl,$sp);
	
	arsort($all_exp_list);
	
	$keys = array_keys($all_exp_list);
	
	$i = 0;
	while( $i <= count($keys) ) {
		if( isset($keys[$i]) ) {
			mysql_query('INSERT INTO `aaa_reting_list` ( `win`,`exp_real`,`uid`,`pos`,`global`,`exp`,`date`,`time` ) VALUES (
				"'.$win_list[$keys[$i]].'","'.$exp_list[$keys[$i]].'","'.$keys[$i].'","'.($i+1).'","'.$all_exp_list[$keys[$i]].'","'.$add_exp_list[$keys[$i]].'","'.date('dmY').'","'.time().'"
			)');
		    if ($i == 0)
			{
				mysql_query('UPDATE `users` SET `money2` = `money2` + 1 WHERE `id` = "'.$keys[$i].'" LIMIT 1');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#cb0000><b>Рейтинг игроков '.date('d-m-Y').':</b></font>","capitalcity","","6","1","'.time().'")');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#000000>Игрок '.$log_list[$keys[$i]].' получает 1 Екр. за 1-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}
			elseif ($i == 1)
			{
				mysql_query('UPDATE `users` SET `money2` = `money2` + 0.75 WHERE `id` = "'.$keys[$i].'" LIMIT 1');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#000000>Игрок '.$log_list[$keys[$i]].' получает 0.75 …кр. за 2-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}
			elseif ($i == 2)
			{
				mysql_query('UPDATE `users` SET `money2` = `money2` + 0.5 WHERE `id` = "'.$keys[$i].'" LIMIT 1');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#000000>Игрок '.$log_list[$keys[$i]].' получает 0.5 …кр. за 3-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');				
			}
			elseif ($i == 3)
			{
				mysql_query('UPDATE `users` SET `money2` = `money2` + 0.25 WHERE `id` = "'.$keys[$i].'" LIMIT 1');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#000000>Игрок '.$log_list[$keys[$i]].' получает 0.25 …кр. за 4-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}
			elseif ($i == 4)
			{
				mysql_query('UPDATE `users` SET `money2` = `money2` + 0.15 WHERE `id` = "'.$keys[$i].'" LIMIT 1');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=#000000>Игрок '.$log_list[$keys[$i]].' получает 0.15 …кр. за 5-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}		
		}
		$i++;
	}
	
//}
?>