<?
require_once '../core/system.php';
echo only_reg();
$header = 'Замки';
require_once '../core/head.php';
$id=abs(intval($_GET[id]));
switch($_GET[mod]){
	default:
$spis = mysql_query("SELECT * FROM `zamki` order by `id` asc");
echo'<div class="player menuList">';

while($s = mysql_fetch_assoc($spis)){
	echo'
		<center><img src="'.$s[img].'" alt=""/><br/>';
		if($s[kontrol]==0){echo'Контроль: нет.';}else{
		$usc = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '$s[kontrol]'"));
		echo'Контроль: '.$usc[name].'';
		}
		echo'</center>
		<li><a href="?mod=zamok&id='.$s['id'].'">'.$s[name].'</a></li>';
}
echo'</div>';
break;
case 'zamok':
	$spis = mysql_query("SELECT * FROM `zamki` WHERE id='".$id."'");
	echo'<div class="player menuList">';
	if(strtotime("11:30")>time()){$tn=ftime(strtotime("11:30")-time());}
	elseif(strtotime("15:30")>time()){$tn=time(strtotime("15:30")-time());}
	elseif(strtotime("20:30")>time()){$tn=time(strtotime("20:30")-time());}
	if($tn<0){$tn=ftime(strtotime("11:30")-time()+86400);}
	$s = mysql_fetch_assoc($spis);
		echo'
		<center><img src="'.$s[img].'" alt=""/><br/>';
		if($s[kontrol]==0){echo'Контроль: нет.';}else{
			$usc = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '$s[kontrol]'"));
			echo'Контроль: '.$usc[name].'';
		}
		<?
		if($tn>0){echo '<br/>До осады '.$tn;}else{if($battle['time'] - time()){echo'';}else{echo'<br/>Замок в осаде';}}
		
		echo'
		</center>
		
		Король:<br/>
		'.$s[kinghp].' хп.<br/>
		'.$s[kinguron].' урон.<br/>
		Стража:<br/>
		'.$s[strazhhp].' хп.<br/>
		'.$s[strazhuron].' урон.<br/></div>
		
		Бонус владения:<br/>
		'.$s[bongold].' золота в час<br/>
		'.$s[bonparam].'% к параметрам<br/>
		';
	

	
	$usc = mysql_query("SELECT * FROM `clans` WHERE `owner` = '$user[id]'");
	$zxzx = mysql_fetch_assoc($usc);
	$uscc = mysql_query("SELECT * FROM `zamki_clan` WHERE `clan` = '$zxzx[id]' and zid='$id'");
	$usccz = mysql_query("SELECT * FROM `zamki_clan` WHERE `clan` = '$zxzx[id]'");
	if(mysql_num_rows($uscc)){
		echo'<li><a href="game.php">К замку</a></li>';
		$kusc = mysql_num_rows(mysql_query("SELECT * FROM `zamki_clan` WHERE zid='$id'"));
		echo'Кланов: '.$kusc.'';
	}
	if(mysql_num_rows($usc) and mysql_num_rows($usccz)==0 and $s[status]==0){	
		echo'<li><a href="?mod=zajavka&id='.$s['id'].'">Подать заявку</a></li>';
	$kusc = mysql_num_rows(mysql_query("SELECT * FROM `zamki_clan` WHERE zid='$id'"));
	echo'Кланов: '.$kusc.'';
	}
	
break;
case 'zajavka':
	$spis = mysql_query("SELECT * FROM `zamki` WHERE id='".$id."'");
	$s = mysql_fetch_assoc($spis);
	
	$usc = mysql_query("SELECT * FROM `clans` WHERE `leader` = '$user[id]'");
	$usc2 = mysql_query("SELECT * FROM `zamki_user` WHERE `uid` = '$user[id]'");
	
	
	
	if(mysql_num_rows($usc) and mysql_num_rows($usc2)==0 and $s[status]==0){
		$oppx = mysql_fetch_array($usc);
		mysql_query("INSERT INTO `zamki_clan` (`clan`, `zid`) values('$oppx[id]', '".$id."')");
		$opponents=mysql_query("SELECT * FROM `clan_us` WHERE `clans` = '$oppx[id]' and act='1'");
		while($opp = mysql_fetch_array($opponents))
		{
			$time = time();
			if($opp[user]!=$user[id]){
				$msgs='Лидер клана завет на осаду замка!помогите ему!';
				mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`, `read`) values('1', '".abs(intval($opp[user]))."', '$msgs', '$time', '1')");
				mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('".abs(intval($opp[user]))."', '1', '$time')");
				mysql_query("UPDATE `users_konts` SET `new_msg` = '1' WHERE `id_kont` = '1' AND `id_user` = '".abs(intval($opp[user]))."'");
			}
			$uscg = mysql_query("SELECT * FROM `user` WHERE `id` = '$opp[user]'");
			$zxzxg = mysql_fetch_assoc($uscg);
			$ur=ceil(($zxzxg[sila]+$zxzxg[lovk]+$zxzxg[zashit])/3);
			mysql_query("INSERT INTO `zamki_user` (`cid`, `zid`, `uid`, `hp`, `uron`) values ('$oppx[id]', '".$id."', '".$zxzxg[id]."', '".$zxzxg[max_health]."', '".$ur."')");
		}
	}
	header("location: index.php?mod=zamok&id=".$id."");
break;
}
require_once '../core/foot.php';
?>