<?php
require_once 'core/sys.php';
head('Алуко');
reg();
$my_atk = (rand(3,7)*$user['level'])+rand(1,11);
$aluko = mysql_fetch_assoc(mysql_query("SELECT * FROM `aluko` ORDER BY `id` LIMIT 1"));

echo '<div class="name">Алуко</div>';
	if($aluko['health']==0){
	echo '<div class="row"><img src="img/aluko/1.png"> Алуко<br>
	Алуко повержен!<br></div>';
	foot();
	exit;
	}
	if($my_atk >$aluko['health'] and $aluko['health']!=0){
	mysql_query("UPDATE `aluko` SET `health`=0 WHERE `id`='".$aluko['id']."'");
	echo '<div class="row"><img src="img/aluko/1.png"> Алуко<br>
	Алуко повержен!<br></div>';
	$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `aluko_log`"),0);
		if($total>0){
		$q_nagr = mysql_query("SELECT * FROM `aluko_log` GROUP BY `user_id` ORDER BY RAND()");
		/*3 лучших */
		$top_q  = mysql_query("
	SELECT SUM( uron ) ,  `user_id` 
	FROM  `aluko_log` 
	GROUP BY  `user_id` 
	ORDER BY  SUM( uron ) DESC 
	LIMIT 3");
			$topes_us = 'Лучшие в борьбе с Алуко:<br> ';
			while($top= mysql_fetch_assoc($top_q)){
				$max_uron = mysql_result(mysql_query("SELECT SUM( uron ) FROM `aluko_log` WHERE `user_id`='".$top['user_id']."'"),0);
				mysql_query("INSERT INTO `journal` SET 
				`user_id`='".$top['user_id']."',
				`text`='За победу над Алуко Вы получаете <img src=\"img/money/rubin.png\"> ".$aluko['rubin']."',
				`time`='".time()."'");
				/*mysql_query("INSERT INTO `journal` SET 
				`user_id`='".$top['user_id']."',
				`text`='Вы один из лучших(нанесли <b>".$max_uron."</b> урона),кто сражался с Алуко.Ваша награда <img src=\"img/stat/oput.png\"> ".$aluko['oput']."',
				`time`='".time()."'");	*/		
				mysql_query("UPDATE `users` SET 
				`rubin`=`rubin`+".$aluko['rubin'].",
				`journal`=`journal`+1  WHERE `id`='".$top['user_id']."'");
				$name_top = mysql_fetch_assoc(mysql_query("SELECT `login` FROM `users` WHERE `id`='".$top['user_id']."' LIMIT 1"));
				$topes_us.= '<span class="login">'.$name_top['login'].'</span>('.$max_uron.'- <img src="img/money/rubin.png"> '.$aluko['rubin'].')<br>'; 
				$aluko['rubin']=$aluko['rubin']-1;
				$aluko['oput']= round($aluko['oput']/2);
			}
			mysql_query("INSERT INTO `chat` SET
			`user_id`='3',
			`text`='".$topes_us."',
			`time`='".time()."'");
			#sleep(1);
		//////
			while($nagr = mysql_fetch_assoc($q_nagr)){
				mysql_query("INSERT INTO `journal` SET 
				`user_id`='".$nagr['user_id']."',
				`text`='За победу над Алуко Вы получаете <img src=\"img/money/money.png\"> ".$aluko['money']."',
				`time`='".time()."'");		
				mysql_query("UPDATE `users` SET `money`=`money`+".$aluko['money'].",`journal`=`journal`+1 WHERE `id`='".$nagr['user_id']."'");
				$aluko['money']= round($aluko['money']/2);
				mysql_query("DELETE FROM `aluko_log` WHERE `user_id`='".$nagr['user_id']."'");
			}
			mysql_query("TRUNCATE TABLE  `aluko_log`");
					}
	foot();
	exit;
	}
$aluko['health'] = $aluko['health']-$my_atk;	
$aluko_sl = array('ткнул в глаз','нанес урон','порезал','ударил','царапнул');
shuffle($aluko_sl);
$rand_m = rand(1,15);
	if($user['mana']<15){
	echo '<div id="error">Вы не можете бить восстановите ману<br>
	<a id="link_button" href="aluko.php">В атаку!</a><br></div>';
	foot();
	exit;
	}
	mysql_query("INSERT INTO `aluko_log` SET
	`user_id`='".$user['id']."',
	`text`='".$aluko_sl[0]." <b>Алуко</b> на <b>".$my_atk."</b>',
	`time`='".time()."',
	`uron`='".$my_atk."'");
	mysql_query("UPDATE `aluko` SET `health`=`health`-".$my_atk." WHERE `id`='".$aluko['id']."'");
	mysql_query("UPDATE `users` SET `mana`=`mana`-".$rand_m.",`mana_resp`='".time()."' WHERE `id`='".$user['id']."'");
	$all_uron = mysql_result(mysql_query("SELECT SUM( uron )FROM `aluko_log` WHERE `user_id`='".$user['id']."'"),0);
	$perc_health_aluko = round($aluko['health']/$aluko['max_health']*100,2);
	echo '<div class="row">
	<img src="img/aluko/1.png"> Алуко<br>
	Все еще думаешь победить меня,жалкий одаренный?<br>
	Здоровье: <font color="orange">'.$aluko['health'].'</font>/<font color="orange">'.$aluko['max_health'].'</font>
	<div class="health_attack"><div class="health_attack2" style="width: '.$perc_health_aluko.'%;"></div></div>
	Весь урон: <b>'.$all_uron.'</b><br>
	<a id="link_button" href="aluko.php">В атаку!</a><br>
	</div>
	';
		if($user['clan']>0){
		echo '<div class="name"><a href="clan.php?clan='.$user['clan'].'">Мой клан</a></div>';
		}
	$aluko_log_q = mysql_query("SELECT * FROM `aluko_log` ORDER BY `time` DESC LIMIT 15");
	echo '<div class="row">История:<br>';
	while($aluko_log = mysql_fetch_assoc($aluko_log_q)){
	$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='".$aluko_log['user_id']."'"));
	echo '<a href="pers.php?ank='.$ank['id'].'">'.$ank['login'].'</a> '.$aluko_log['text'].'<br>';
	}
	echo '</div>';
foot();
?>