<?
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');

	$add_exp_list = array();
	$all_exp_list = array();
	$exp_list = array();
	$win_list = array();
	$lose_list = array();
	$nich_list = array();
	$xu_list = array();
	$level_list = array();
	
	$sp = mysql_query('SELECT `id`,`level`,`name` FROM `clan`');
	while( $pl = mysql_fetch_array($sp) ) {
		$timday=time()-86400;
		$xu = mysql_fetch_array(mysql_query('SELECT COUNT(`id`) FROM `users` WHERE `clan` = "'.$pl['id'].'" AND `admin` = 0 AND `online` > "'.$timday.'" AND `real` = 1 LIMIT 1'));
		$xu = $xu[0];
		
		$clanw = mysql_fetch_array(mysql_query('SELECT COUNT(`id`) FROM `battle` WHERE  `type` = 250 AND `time_over` > "'.$timday.'" AND ((`team_win`=1 AND `clan1` = "'.$pl['id'].'") OR (`team_win`=2 AND `clan2` = "'.$pl['id'].'")) LIMIT 1'));
		$clanw = $clanw[0];
		
		$clanl = mysql_fetch_array(mysql_query('SELECT COUNT(`id`) FROM `battle` WHERE  `type` = 250 AND `time_over` > "'.$timday.'" AND ((`team_win`=2 AND `clan1` = "'.$pl['id'].'") OR (`team_win`=1 AND `clan2` = "'.$pl['id'].'")) LIMIT 1'));
		$clanl = $clanl[0];
		
		//$st = mysql_fetch_array(mysql_query('SELECT SUM(`val`) AS `exp` FROM `users_reting` WHERE `clan` = "'.$pl['id'].'" LIMIT 1'));
		$st = mysql_fetch_array(mysql_query('SELECT `exp` FROM `clan` WHERE `id` = "'.$pl['id'].'" LIMIT 1'));
				
		$wn = mysql_fetch_array(mysql_query('SELECT SUM(`win`) AS `win` , SUM(`lose`) AS `lose` , SUM(`nich`) AS `nich` FROM `users` WHERE `clan` = "'.$pl['id'].'" LIMIT 1'));
		
		$rl = mysql_fetch_array(mysql_query('SELECT `exp`,`global`,`exp_real`, `win`,`lose` FROM `aaa_clan_reting_list` WHERE `clan` = "'.$pl['id'].'" ORDER BY `id` DESC LIMIT 1'));
		
		$exp_list[$pl['id']] = $st['exp'];
		$log_list[$pl['id']] = $pl['name'];
		$level_list[$pl['id']] = $pl['level'];
		$win_list[$pl['id']] = $wn['win'];
		$lose_list[$pl['id']] = $wn['lose'];
		$nich_list[$pl['id']] = $wn['nich'];
		
		$xu_list[$pl['id']] = $xu;
		$add_exp = 0 ;
		if( date('d') == 1)
		{
			$global_exp = 0; 
		}
		else
		{
			$global_exp = round($rl['global']);
		}
		//$xul = mysql_query('SELECT `level` FROM `users` WHERE `clan` = "'.$pl['id'].'" AND `admin` = 0 AND `real` = 1 LIMIT 1');
		$xul = mysql_query('SELECT `st`.`repexp`,`st`.`id` FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON (`u`.`id` = `st`.`id`) WHERE `u`.`clan`="'.$pl['id'].'"');
		while( $xull = mysql_fetch_array($xul) )
		 {
			$add_exp = $add_exp + $xull['repexp'];
			mysql_query('UPDATE `stats` SET `repexp` = 0 WHERE `id` = "'.$xull['id'].'" LIMIT 1');
		 }
		
		if ($xu > 0 && $pl['id'] !=62 && $pl['id'] !=93 )
		{
		$add_exp_list[$pl['id']] = $add_exp; //записываем сколько опыта получил за сегодня
		$all_exp_list[$pl['id']] = $global_exp+$add_exp; //записываем сколько опыта получил всего + сегодняшний
		}
		else
		{
		$add_exp_list[$pl['id']] = 0; //записываем сколько опыта получил за сегодня
		$all_exp_list[$pl['id']] = 0; //записываем сколько опыта получил всего + сегодняшний
		}
		
		//$nonlin=mysql_fetch_array(mysql_query('SELECT COUNT(*) as num FROM `users` WHERE `clan` = "'.$pl['id'].'" AND `online` < "'.(time()-86400).'" '));
		//$all_exp_list[$pl['id']] -= $nonlin['num']*50000;
	}
	
	unset($pl,$sp,$xul,$xull);
	
	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_clan_reting_list` ( `level`,`xu`,`win`,`lose`,`nich`,`exp_real`,`clan`,`pos`,`global`,`exp`,`date`,`time` ) VALUES (
				"'.$level_list[$keys[$i]].'","'.$xu_list[$keys[$i]].'","'.$win_list[$keys[$i]].'","'.$lose_list[$keys[$i]].'","'.$nich_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 `clan` SET `money2` = `money2` + "'.($xu_list[$keys[$i]]*3/10).'" 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]].' получает '.($xu_list[$keys[$i]]*3/10).' Екр. в казну клана за 1-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}
			elseif ($i == 1)
			{
				mysql_query('UPDATE `clan` SET `money2` = `money2` + "'.($xu_list[$keys[$i]]*2/10).'" 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]].' получает '.($xu_list[$keys[$i]]*2/10).' Екр. в казну клана за 2-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}
			elseif ($i == 2)
			{
				mysql_query('UPDATE `clan` SET `money2` = `money2` + "'.($xu_list[$keys[$i]]*1/10).'" 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]].' получает '.($xu_list[$keys[$i]]*1/10).' Екр. в казну клана за 3-е место в рейтинге.</font>","capitalcity","","6","1","'.time().'")');
			}
			
		}
		$i++;
	}
	mysql_query('UPDATE `stats` SET `repexp` = 0 WHERE `repexp` > 0');
?>