<?
	define('PROTECTOR', 1);
	ob_start();
	$head = 'catacomb';//фикс. места
	
	$textl='Подземелья';
	
	
	@include('files/db.php');
	@include('files/auth.php');
	@include('files/func.php');
	@include('files/core.php');
	@include('files/head.php');
	@include('files/zag.php');
	$id=sint($_GET[id]);
	$row2 = mysql_query("SELECT * FROM `catacomb_user` WHERE id_room = '$id'");
	$avt2=mysql_num_rows($row2);
	$row = mysql_query("SELECT * FROM `catacomb_room` WHERE id = '$id' LIMIT 1");
	$avt=mysql_num_rows($row);
	if($avt==0){echo'Комната не созданна или удалена!';@include('files/down.php');exit();}
	$cat = mysql_fetch_array($row);
	if($udata[lvl]>=70){
		switch($_GET[mod]){
			default:
			
			echo '
			</div><div class="menu">
			<b>'.check($cat[name]).'</b> ['.$avt2.'/'.$cset_max_user.']';
			if(cus($udata[id],$id)){
				if($cat[timer]==1){
					echo'</div><div class="menu"><center>Бой через '.($cat[timer_time]-time()).' сек.<br/><a href="?id='.$id.'"><input type="submit" value="Обновить" class="ibutton"></a></center>';
					if(time()>=$cat[timer_time]){$xc = $cat[level]+1;
						mysql_query("UPDATE catacomb_room SET timer = '0', level = '$xc' WHERE id=$id");
						header("Location: /catacomb.php?");
					}
				}
			}
			if($cat[timer]==0){
				if($udata[id]==$cat[lider]){ECHO'</div><div class="menu">';if($avt2<$cset_min_user){
				echo'<input type="submit" value="Старт" class="ibutton" disabled="disabled" /> - недостаточно участников!';}else{
				echo'<a href="?mod=start&id='.$id.'"><input type="submit" value="Старт" class="ibutton"></a>';}}
				}
				echo'</div><div class="menu">';
				userr($cat[lider]);echo' [Лидер]<br/>';
				
				$rowe = mysql_query("SELECT * FROM `catacomb_user` WHERE id_room = $id and id!=$cat[lider]");
				while($cate = mysql_fetch_array($rowe)){
				userr($cate[id]);echo' <br/>';
				}
				if($cat[level]==0){
				if($cat[timer]==0){
				echo'</div><div class="menu">';
				if(!cus($udata[id],$id)){
				if(!cuss($udata[id])){
				if($avt2<$cset_max_user){
				if($udata[catacomb_rock]>=$catacomb_rock){
				echo'<a href="?mod=add&id='.$id.'"><input type="submit" value="Присоединиться" class="ibutton"></a>';
				}else{echo'Для вступления нужно '.$catacomb_rock.' камней подземелья';}}
				}
				}else{
				if($udata[id]==$cat[lider]){echo'<a href="?mod=dell&id='.$id.'"><input type="submit" value="Распустить комнату" class="ibutton"></a>';
				}else{
				echo'<a href="?mod=del&id='.$id.'"><input type="submit" value="Покинуть комнату" class="ibutton"></a>';}
				}}}
				break;
				case'start':	
				if($cat[level]==0){
				if($cat[timer]==0){
				if($udata[id]==$cat[lider]){
				if($avt2>=$cset_min_user){
				$tt=time()+$cset_time;
				mysql_query("UPDATE catacomb_room SET timer = '1', timer_time = '$tt' WHERE id=$id");
				$rowecat = mysql_query("SELECT * FROM `catacomb` WHERE level = 1");
				$catecat = mysql_fetch_array($rowecat);
				$ra=rand($catecat[bot_min],$catecat[bot_max]);
				for($i=0;$i<$ra;$i++){
				mysql_query("INSERT INTO catacomb_bot SET room = '$id', hp = '$catecat[bot_hp]', uron = '$catecat[bot_uron]', lovk = '$catecat[bot_lovk]', type = 'mob', img = '".rand(1,40)."'");
				}
				mysql_query("INSERT INTO catacomb_bot SET room = '$id', hp = '$catecat[boss_hp]', uron = '$catecat[boss_uron]', lovk = '$catecat[boss_lovk]', type = 'boss', img = '".rand(1,10)."'");
				}}}}
				header("Location: catacomb_room.php?id=$id");
				break;
				case'add':	
				if($udata[catacomb_rock]>=$catacomb_rock){
				if($cat[level]==0){
				if($cat[timer]==0){
				if(!cus($udata[id],$id)){
				if(!cuss($udata[id])){
				mysql_query("INSERT INTO catacomb_user SET id_room = '$id', id = '$udata[id]', hp = '$udata[hpall]', uron = '$udata[sila]', lovk = '$udata[lovk]'");
				mysql_query("UPDATE users SET catacomb_rock = catacomb_rock - $catacomb_rock WHERE id = $udata[id]");
				}
				}
				}	
				}
				}
				header("Location: catacomb_room.php?id=$id");
				break;
				case'del':	
				if($cat[level]==0){
				if($cat[timer]==0){
				if(cus($udata[id],$id)){
				mysql_query("DELETE FROM catacomb_user WHERE id_room = '$id' and id = '$udata[id]'");
				mysql_query("UPDATE users SET catacomb_rock = catacomb_rock + $catacomb_rock WHERE id = $udata[id]");
				} 
				}
				}
				header("Location: catacomb_room.php?id=$id");
				break;
				case'dell':	
				if($cat[level]==0){
				if($cat[timer]==0){
				if($udata[id]==$cat[lider]){
				$rowe = mysql_query("SELECT * FROM `catacomb_user` WHERE id_room = $id and id!=$cat[lider]");
				while($cate = mysql_fetch_array($rowe)){
				$text = 'Лидер '.$log.' распустил комнату!';
				$rowet = mysql_query("SELECT * FROM `users` WHERE id=$cate[id]");
				$catet = mysql_fetch_array($rowet);
				$to = $catet[usr];
				$time = date("H:i d.m.y");
				mysql_query("UPDATE users SET catacomb_rock = catacomb_rock + $catacomb_rock WHERE id = $catet[id]");
				mysql_query("INSERT INTO `msg_r` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
				mysql_query("INSERT INTO `msg_i` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time',`mail_msg` = '$text'");
				}
				mysql_query("DELETE FROM catacomb_user WHERE id_room = '$id'");
				mysql_query("DELETE FROM catacomb_room WHERE id = '$id'");
				} 
				}
				}
				header("Location: catacombi.php?");
				break;
				}
				}else{
				echo'</div><div class="menu">
				Доступоно с 70 лвл!
				</div>';
				}
				
				@include('files/down.php');
				?>				