<?

    ob_start();

  list($msec,$sec)
             = explode(chr(32), microtime()); 
  $gtime     = $sec+$msec; 

?>

<!DOCTYPE html><html>
  <head><title><?=$title?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
	 
<link rel="shortcut icon" href="/favicon.ico">
<link rel="stylesheet" href="/images/style/style.css" type="text/css" /> 
<script type="text/javascript" src="/timer.js"></script>
 </head>
<body>


<?

    if($user) {
    
switch($user['level']) {
    
case 1:
$exp = 209;
break;

case 2:
$exp = 505;
break;

case 3:
$exp = 1004;
break;

case 4:
$exp = 1950;
break;

case 5:
$exp = 2908;
break;

case 6:
$exp = 3088;
break;

case 7:
$exp = 5102;
break;

case 8:
$exp = 8810;
break;

case 9:
$exp = 12802;
break;

case 10:
$exp = 16302;
break;

case 11:
$exp = 22087;
break;

case 12:
$exp = 30405;
break;

case 13:
$exp = 47560;
break;

case 14:
$exp = 63900;
break;

case 15:
$exp = 89207;
break;

case 16:
$exp = 119600;
break;

case 17:
$exp = 145105;
break;
case 18:
$exp = 190230;
break;
case 19:
$exp = 259066;
break;
case 20:
$exp = 325503;
break;
case 21:
$exp = 399501;
break;
case 22:
$exp = 472005;
break;
case 23:
$exp = 589905;
break;
case 24:
$exp = 700894;
break;
case 25:
$exp = 859950;
break;
case 26:
$exp = 991092;
break;
case 27:
$exp = 1201072;
break;
case 28:
$exp = 1462040;
break;
case 29:
$exp = 1809901;
break;
case 30:
$exp = 2010027;
break;
case 31:
$exp = 2400106;
break;
case 32:
$exp = 2730601;
break;
case 33:
$exp = 2990979;
break;
case 34:
$exp = 3500000;
break;
case 35:
$exp = 5000000;
break;
case 36:
$exp = 6000000;
break;
case 37:
$exp = 7250000;
break;
case 38:
$exp = 8050000;
break;
case 39:
$exp = 9075000;
break;
case 40:
$exp = 10100000;
break;
case 40:
$exp = 10109000;
break;
case 41:
$exp = 12205000;
break;
case 42:
$exp = 13050000;
break;
case 43:
$exp = 14075000;
break;
case 44:
$exp = 10600000;
break;
case 45:
$exp = 10725000;
break;
case 46:
$exp = 10850000;
break;
case 47:
$exp = 19075000;
break;
case 48:
$exp = 21000000;
break;
case 49:
$exp = 22025000;
break;
case 50:
$exp = 29500000;
break;
case 51:
$exp = 27000000;
break;
case 52:
$exp = 20900000;
break;
case 53:
$exp = 93100000;
break;
case 54:
$exp = 30300000;
break;
case 55:
$exp = 30500000;
break;
case 56:
$exp = 30700000;
break;
case 57:
$exp = 30900000;
break;
case 58:
$exp = 40100000;
break;
case 59:
$exp = 4950000000;
break;     
case 60:   
$exp = 0;   
break;
}
        
$exp_progress = round(100/($exp/$user['exp']));

    
    if($exp_progress > 100) {
$exp_progress = 100;      
}

    function clan_exp($i) {
    switch($i) {  
case 1:
$clan_exp = 3000;
break;
case 2:
$clan_exp = 5800;
break;
case 3:
$clan_exp = 11000;
break;
case 4:
$clan_exp = 20100;
break;
case 5:
$clan_exp = 30940;
break;
case 6:
$clan_exp = 70320;
break;
case 7:
$clan_exp = 130460;
break;
case 8:
$clan_exp = 204490;
break;
case 9:
$clan_exp = 440080;
break;
case 10:
$clan_exp = 780460;
break;
case 11:
$clan_exp = 1380080;
break;
case 12:
$clan_exp = 2402050;
break;
case 13:
$clan_exp = 4132030;
break;
case 14:
$clan_exp = 7024900;
break;
case 15:
$clan_exp = 11801080;
break;
case 16:
$clan_exp = 19590090;
break;
case 17:
$clan_exp = 32129000;
break;
case 18:
$clan_exp = 52048090;
break;
case 19:
$clan_exp = 83278020;
break;
case 20:
$clan_exp = 13157905;
break;
case 21:
$clan_exp = 20526040;
break;
case 22:
$clan_exp = 31610065;
break;
case 23:
$clan_exp = 48048018;
break;
case 24:
$clan_exp = 72072027;
break;
case 25:
$clan_exp = 106660695;
break;
case 26:
$clan_exp = 155730374;
break;
case 27:
$clan_exp = 224250658;
break;
case 28:
$clan_exp = 318444034;
break;
case 29:
$clan_exp = 445802207;
break;
case 30:
$clan_exp = 601500000;
break;
case 31:
$clan_exp = 830700000;
break;
case 32:
$clan_exp = 1102100000;
break;
case 33:
$clan_exp = 1408000000;
break;
case 34:
$clan_exp = 1920400000;
break;
case 35:
$clan_exp = 2406300000;
break;
case 36:
$clan_exp = 3210300000;
break;
case 37:
$clan_exp = 5601030000;
break;
case 38:
$clan_exp = 7210300000;
break;
case 39:
$clan_exp = 8410300000;
break;
case 40:
$clan_exp = 10000000000;
break; 
}
    
return $clan_exp;
    
}
    
    if($clan && $clan['level'] < 40 && $clan['exp'] >= clan_exp($clan['level'])) {   
mysql_query('UPDATE `clans` SET `level` = `level` + 1,
                                        `exp` = "0" WHERE `id` = "'.$clan['id'].'"');
}



$mail = mysql_result(mysql_query("SELECT COUNT(id) FROM `mail` WHERE `to` = '".$user['id']."' and `read` = '0'"),0); //Сообщения
echo' <div class="empty_block white"> 
<img src="/images/ico/png/up.png" width="18">'.n_f($user['level']).' 
<span style="float: right;">
'.($mail > 0?'<a href="/mail/"><img src="/images/ico/png/post.png"></a> ' : '').'
<img src="/images/ico/png/gold.png" width="18">'.n_f($user['g']).'
<img src="/images/ico/png/silver.png" width="18">'.n_f($user['s']).'
</span> </div>';

echo '<div class="progress-black">
    <div class="progress-green" style="width:'.$exp_progress.'%;"></div></div>'; //Опыт
	

	

    if($user['level'] < 60 && $user['exp'] >= $exp) {    
$g = ($user['level'] * 5);  
mysql_query('UPDATE `users` SET `level` = `level` + 1, `exp` = "0",                                                                      
                                `g` = "'.($user['g'] + $g).'" WHERE `id` = "'.$user['id'].'"');

echo'<div class="empty_block item_center">
<font color="#f05010">Вы получили новый уровень!</font></br>
<font color="#90b0c0">Награда:</font> <img src="/images/ico/png/gold.png" alt="*" width="18"/> '.$g.'
</div>';  
}



///////////////////////////////// //Арена  /////////////////
 if(mysql_result(mysql_query('SELECT * FROM `arena` WHERE `user` = "'.$user['id'].'"'),0) == 0) {//Добавляем в бд, если зашел в первый раз
mysql_query('INSERT INTO `arena` (`user`, `attack`, `opponent`, `end`) VALUES ("'.$user['id'].'", "15", "'.$user['id'].'", "0")');
}
$date =intval(time()+7200);  //2 часа
$be=mysql_fetch_array(mysql_query("SELECT * FROM `arena` WHERE `user`= '".$user['id']."' LIMIT 1"));

if($be['attack'] == '0' AND $be['end'] == '0'){
$end='1';
mysql_query('UPDATE  `arena` SET `end` = "'.$end.'", `time` = "'.$date.'" WHERE `user`="'.$user['id'].'" LIMIT 1');
}

if($be['time'] <= time() AND $be['end'] == '1'){
$end='0';
mysql_query('UPDATE  `arena` SET `attack` = "15", `end` = "'.$end.'"  WHERE `user`="'.$user['id'].'" LIMIT 1');
}
///////////////////////////////////////////////////////////////
/////////////////////////////////// Логово монстров ///////////
 if(mysql_result(mysql_query('SELECT * FROM `lair_users` WHERE `user_id` = "'.$user['id'].'"'),0) == 0) {//Добавляем в бд, если зашел в первый раз
mysql_query('UPDATE  `users` SET `lair` = "1", `lair_glava` = "1", `lair_boi` = "2" WHERE `id`="'.$user['id'].'" LIMIT 1');
mysql_query('INSERT INTO `lair_users` (`user_id`, `user_str`, `user_vit`, `user_def`, `boss_id`, `boss_str`, `boss_vit`, `boss_def`, `time`, `boss_dead`) VALUES ("'.$user['id'].'", "'.$user['str'].'", "'.$user['vit'].'", "'.$user['def'].'", "1", "15", "100", "25", "1518857233", "1" )');
	header('Location:/');
	exit;} 

$date =intval(time()+7200);  //2 часа
$be=mysql_fetch_array(mysql_query("SELECT * FROM `lair_users` WHERE `user_id`= '".$user['id']."' LIMIT 1"));

if($user['lair_boi'] == '0' AND $be['boss_dead'] == '0'){
mysql_query('UPDATE  `lair_users` SET `time` = "'.$date.'", `boss_dead` = "1" WHERE `user_id`="'.$user['id'].'" LIMIT 1');}

if($be['time'] <= time() AND $be['boss_dead'] == '1'){
mysql_query('UPDATE  `users` SET `lair_boi` = "2" WHERE `id`="'.$user['id'].'" LIMIT 1');
mysql_query('UPDATE  `lair_users` SET `boss_dead` = "0" WHERE `user_id`="'.$user['id'].'" LIMIT 1');
header('location: /');   
exit;}
 
///////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////
/////////////////////////////////// Сокровищница драконов ///////////


 
///////////////////////////////////////////////////////////////


//         Турнир по ключикам
//////////////////////////////////////////////////////////////
// Monday  => Понедельник
// Tuesday => Вторник
// Sunday  => Воскресенье

$settings=mysql_fetch_array(mysql_query("SELECT * FROM `settings`  LIMIT 1"));

$date=date("l"); //День недели (Monday, ..., Sunday)
$bonus_date=date('d.m.Y'); //Дата 01.01.2018

if($bonus_date != $settings['task_tournament_date'] AND $date == 'Monday'){//Запускаем каждый понедельник
	$q = mysql_query('SELECT * FROM `users` WHERE `task_kluch` >= "0" ORDER BY `task_kluch` DESC LIMIT 10');
while($row = mysql_fetch_array($q)) {
$i++;
  switch($i) {
case 1:
$task_gold = '1000';
break;
case 2:
$task_gold = '900';
break;
case 3:
$task_gold = '800';
break;
case 4:
$task_gold = '700';
break;
case 5:
$task_gold = '600';
break;
case 6:
$task_gold = '500';
break;
case 7:
$task_gold = '400';
break;
case 8:
$task_gold = '300';
break;
case 9:
$task_gold = '200';
break;
case 10:
$task_gold = '100';
break;
}
if($i <= 10) {//Награда для ТОП-3
mysql_query('UPDATE  `users` SET `g` = `g` + "'.$task_gold.'" WHERE `id`="'.$row['id'].'" ORDER BY `task_kluch` LIMIT 10');
$text='В ежеднеельном турнире по ключам Вы заняли  '.$i.'-е место и ваша награда [img=20]/images/ico/png/gold.png[/img] '.$task_gold.' золота'; //Текст уведомления
mysql_query("INSERT INTO `mail` SET `from`='2',`to`='".$row['id']."',`text`='".$text."',`time`='".time()."'"); //Отправляем уведомление
mysql_query('UPDATE `contacts` SET `time` = \''.time().'\' WHERE `user` = "2" AND `ho` = "'.$row['id'].'" ');  //Оновляем время 
mysql_query('UPDATE `contacts` SET `time` = \''.time().'\' WHERE `ho` = "2" AND `user` = "'.$row['id'].'" ');  //Оновляем время 	
$task_pob.=' <font color="#fff">'.$i.'.</font><a href="/user/'.$row['id'].'">'.nick($row['id']).'</a>  <span style="float: right;"><img src="/images/ico/png/keys.png" alt="*" width="18"/> '.$row['task_kluch'].'</span> </br>';
}}

mysql_query('UPDATE  `settings` SET `task_tournament_date` = "'.$bonus_date.'" WHERE `id`="1" LIMIT 1');
$text = ' </br> <font color="#7afe4e">
<center>Еженедельные турниры завершен! </center></br>
Победителями турнира по ключам стали: </br>
</font>';
$text2= ' <img src="/images/ico/png/keys.png" alt="*" width="18"/> <a href="/task/dailyInfo"> <u>Подробнее о турнире</u> </a>';

mysql_query("INSERT INTO `forum_topic` (`sub`,
                                        `name`,
                                        `user`,
                                        `text`,
                                         `time`) VALUES ('1',
                                                         'Турнир от ".$bonus_date." ',
                                                         '1',
                                                         '".$text." </br> ".$task_pob." </br>".$text2."',
														 '".time()."')");
mysql_query('UPDATE  `users` SET `task_kluch` = "0" ');//Обнуляем всем ключи															  
mysql_query('UPDATE  `users` SET `notified` = "0" ');//Добавляем уведомление на главную		  
				
}



///////////////////////////////////////////////////////////'
/////////////////////////////////////////


$lair_mobs_clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `clan_lair_boss` WHERE `clan` = '".$clan['id']."' "));
if($lair_mobs_clan AND $lair_mobs_clan['time'] <= time()){
	echo' gevre';
	mysql_query('DELETE FROM `clan_lair_boss` WHERE `id` = "'.$lair_mobs_clan['id'].'" ');
}


}else{

}

?>