<?
require_once '../core/system.php';
echo only_reg();
$header = 'Осада';
require_once '../core/head.php';
$id=abs(intval($_GET[id]));

$usc = mysql_query("SELECT * FROM `zamki_user` WHERE `uid` = '$user[id]'");
if(mysql_num_rows($usc)==0){
header("location: index.php");
}
$usi = mysql_fetch_assoc($usc);
$usz = mysql_fetch_assoc(mysql_query("SELECT * FROM `zamki` WHERE `id` = '$usi[zid]'"));
$uscff = mysql_query("SELECT * FROM `zamki_user` WHERE `dead` = '0'");
if($usz[status]==1){
if(($usz[start]+600)-time()<1)
{
	$_SESSION[dob]="";
	$_SESSION['light'] = "<span class='dred' style='font-weight:bold;'>К сожалению никто не успел захватить замок!прибыло подкрепление ,которое всех перебило.!</span>";
	if($usz[status]==1){
		mysql_query("UPDATE `zamki` SET `status`='0' WHERE id='$usi[zid]'");
		$opponents = mysql_query("SELECT * FROM `zamki_user`");
		while($opp = mysql_fetch_array($opponents))
		{
			$time = time();
			$msgs='К сожалению, никто не выиграл, но может вам повезёт в следующем бою';
			mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`, `read`) values('1', '".abs(intval($opp[uid]))."', '$msgs', '$time', '1')");
			mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('".abs(intval($opp[uid]))."', '1', '$time')");
			mysql_query("UPDATE `users_konts` SET `new_msg` = '1' WHERE `id_kont` = '1' AND `id_user` = '".abs(intval($opp[uid]))."'");
		}
		mysql_query("DELETE FROM zamki_user");
		mysql_query("DELETE FROM zamki_clan");
	}
	header("location: index.php");
	exit();
}}

if($usi[hp]>0 and ($usz[start]+600)-time()>0){
if($usz[status]==0){
if(strtotime("11:30")>time()){$tn=ftime(strtotime("11:30")-time());}
elseif(strtotime("15:30")>time()){$tn=ftime(strtotime("15:30")-time());}
elseif(strtotime("20:30")>time()){$tn=ftime(strtotime("20:30")-time());}
if($tn<0){$tn=ftime(strtotime("11:30")-time()+86400);}
if($tn>0){
echo'<center><img src="img/tower.png" alt=""/><br/>До атаки '.$tn.'<br/><a href="?">Обновить</a></center>';
}else{
echo'<center><img src="img/tower.png" alt=""/><br/>Ждите...<br/><a href="?">Обновить</a></center>';
}
}else{
$uscr = mysql_num_rows(mysql_query("SELECT DISTINCT `cid` FROM `zamki_user` WHERE `dead` = '0'"));
if($uscr>0){
	echo"
	<div class='player menuList' style='padding:5px;'>
	<img src='/images/icon/vit.png' alt='vit'/> $usi[hp] хп. | До окончания ".ftime(($usz[start]+600)-time())."
	</div>
	";

if($usz[starazh1s]==1 or $usz[starazh2s]==1)
{
	if($usz[timeatack]<time()-5){
		$ztime=floor((time()-$usz[timeatack])/5);
		$ztimer=$ztime*$usz[strazhuron];
		if($usz[starazh1s]==1 and $usz[starazh2s]==1)$ztimer=$ztimer*2;
		$ztime=$ztime*5;
		$ztimer=rand($ztimer-10,$ztimer+5);
		if($ztimer<=0){$ztimer=1;}
		mysql_query("UPDATE `zamki_user` SET `hp`=`hp`-'$ztimer' WHERE zid='$usi[zid]'");
		mysql_query("UPDATE `zamki` SET `timeatack`='".($usz[timeatack]+$ztime)."' WHERE id='$usi[zid]'");
		$opponentsd=mysql_query("SELECT * FROM `zamki_user`");
		while($oppdd = mysql_fetch_array($opponentsd))
		{
			if($oppdd[hp]<1){
				mysql_query("UPDATE `zamki_user` SET `dead`='1' WHERE uid='".abs(intval($oppdd['uid']))."'");
			}
			
		}
	}
	if($_GET[attack]==1){
		if($usz[starazh1h]>0){
		$ur=rand($usi[uron]-20,$usi[uron]+20);
		$uron=$usz[starazh1h]-$ur;
		if($uron<1){mysql_query("UPDATE `zamki_user` SET `kill`=`kill`+1 WHERE uid='$user[id]'");
		mysql_query("UPDATE `zamki` SET `starazh1s`='0' WHERE id='$usi[zid]'");
		$_SESSION['light'] = "<span class='dgreen' style='font-weight:bold;'>Вы убили первого стража!</span>";
		}
		mysql_query("UPDATE `zamki` SET `starazh1h`='".($uron)."' WHERE id='$usi[zid]'");
		mysql_query("UPDATE `zamki_user` SET `nanesuron`=`nanesuron`+'".($ur)."' WHERE  uid='$user[id]'");
		header("location: ?");
		}
	}
	if($_GET[attack]==2){
		if($usz[starazh2h]>0){
		$ur=rand($usi[uron]-20,$usi[uron]+20);
		$uron=$usz[starazh2h]-$ur;
		if($uron<1){mysql_query("UPDATE `zamki_user` SET `kill`=`kill`+1 WHERE  uid='$user[id]'");
		mysql_query("UPDATE `zamki` SET `starazh2s`='0' WHERE id='$usi[zid]'");
		$_SESSION['light'] = "<span class='dgreen' style='font-weight:bold;'>Вы убили второго стража!</span>";
		}
		mysql_query("UPDATE `zamki` SET `starazh2h`='".($uron)."' WHERE id='$usi[zid]'");
		mysql_query("UPDATE `zamki_user` SET `nanesuron`=`nanesuron`+'".($ur)."' WHERE  uid='$user[id]'");
		header("location: ?");
		}
	}
	if($usz[starazh1h]>0){echo"
	<br/><img src='img/strazh1.jpg' style='margin-right:10px;margin-left:4px;float:left;' alt=''/><img src='/images/icon/vit.png' alt='vit'/> $usz[starazh1h] хп.<br/><img src='/images/icon/str.png' alt='str'/> Сила: ~$usz[strazhuron]<br/><a class='btn' href='?attack=1'><span style='margin-top:5px;margin-left:-3px;' class='label'>Атакoвать</span></a><br/><br/>";
	}
	if($usz[starazh2h]>0){echo"
	<br/><img src='img/strazh2.jpg' style='margin-right:10px;margin-left:4px;float:left;' alt=''/><img src='/images/icon/vit.png' alt='vit'/> $usz[starazh2h] хп.<br/><img src='/images/icon/str.png' alt='str'/> Сила: ~$usz[strazhuron]<br/><a class='btn' href='?attack=2'><span style='margin-top:5px;margin-left:-3px;' class='label'>Атакoвать</span></a><br/><br/>";
	}
}
elseif($usz[kings]==1)
{
	if($usz[timeatack]<time()-5){
		$ztime=floor((time()-$usz[timeatack])/5);
		$ztimer=$ztime*$usz[kinguron];
		$ztime=$ztime*5;
		$ztimer=rand($ztimer-10,$ztimer+5);
		if($ztimer<=0){$ztimer=1;}
		mysql_query("UPDATE `zamki_user` SET `hp`=`hp`-'$ztimer' WHERE zid='$usi[zid]'");
		mysql_query("UPDATE `zamki` SET `timeatack`='".($usz[timeatack]+$ztime)."' WHERE id='$usi[zid]'");
		$opponentsd=mysql_query("SELECT * FROM `zamki_user`");
		while($oppdd = mysql_fetch_array($opponentsd))
		{
			if($oppdd[hp]<1){
		mysql_query("UPDATE `zamki_user` SET `dead`='1' WHERE uid='".abs(intval($oppdd['uid']))."'");
			}
			
		}
	}
	if($_GET[attack]==1){
		if($usz[kingh]>0){
			$ur=rand($usi[uron]-20,$usi[uron]+20);
			$uron=$usz[kingh]-$ur;
			if($uron<1){mysql_query("UPDATE `zamki_user` SET `kill`=`kill`+1 WHERE uid='$user[id]'");
			mysql_query("UPDATE `zamki` SET `kings`='0' WHERE id='$usi[zid]'");
				$_SESSION['light'] = "<span class='dgreen' style='font-weight:bold;'>Вы убили короля!</span>";
			}
			mysql_query("UPDATE `zamki` SET `kingh`='".($uron)."' WHERE id='$usi[zid]'");
			mysql_query("UPDATE `zamki_user` SET `nanesuron`=`nanesuron`+'".($ur)."' WHERE uid='$user[id]'");
			header("location: ?");
		}
	}
	if($usz[kingh]>0){echo"
		<br/><img src='img/king.jpg' style='margin-right:10px;margin-left:4px;float:left;' alt=''/><img src='/images/icon/vit.png' alt='vit'/> $usz[kingh] хп.<br/><img src='/images/icon/str.png' alt='str'/> Сила: ~$usz[kinguron]<br/><a class='btn' href='?attack=1'><span style='margin-top:5px;margin-left:-3px;' class='label'>Атакoвать</span></a><br/><br/>";
	}
	
	
}else{	
	$uscr = mysql_num_rows(mysql_query("SELECT DISTINCT `cid` FROM `zamki_user` WHERE `dead` = '0'"));
	if($uscr>1){
		
		
		
		if($_GET[attack]==1){
			
			$oppx = mysql_fetch_array(mysql_query("SELECT * FROM `zamki_user` WHERE `uid` = '".abs(intval($_GET['id']))."' LIMIT 1"));
			if($usi[cid]==$oppx[cid] or $oppx[dead]==1){
				header("location: ?");
			}
			
			$oppn = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".abs(intval($_GET['id']))."' LIMIT 1"));
			if($oppx[hp]>0){
				$ur=rand($usi[uron]-20,$usi[uron]+20);
				$uron=$oppx[hp]-$ur;
				if($uron<1){mysql_query("UPDATE `zamki_user` SET `kill`=`kill`+1 WHERE uid='$user[id]'");
					mysql_query("UPDATE `zamki_user` SET `dead`='1' WHERE uid='".abs(intval($_GET['id']))."'");
					$_SESSION['light'] = "<span class='dgreen' style='font-weight:bold;'>Вы убили $oppn[nick]!</span>";
				}
				mysql_query("UPDATE `zamki_user` SET `hp`='".($uron)."' WHERE uid='".abs(intval($_GET['id']))."'");
				mysql_query("UPDATE `zamki_user` SET `nanesuron`=`nanesuron`+'".($ur)."' WHERE  uid='$user[id]'");
				$_SESSION[dob]=abs(intval($_GET['id']));
				header("location: ?");
			}
		}
		
		if(empty($_SESSION[dob])){
		$opponents = mysql_query("SELECT * FROM `zamki_user` WHERE `cid` != '".$usi['cid']."' and `hp`>'0' ORDER BY RAND() DESC LIMIT 1");
		}else{
		$opponents = mysql_query("SELECT * FROM `zamki_user` WHERE uid!= '".abs(intval($_SESSION[dob]))."' and `cid` != '".$usi['cid']."' and `hp`>'0' ORDER BY RAND() DESC LIMIT 1");
		$oppus = mysql_fetch_array(mysql_query("SELECT * FROM `zamki_user` WHERE `uid` = '".abs(intval($_SESSION[dob]))."' LIMIT 1"));
		$oppusd = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".abs(intval($_SESSION[dob]))."' LIMIT 1"));
		if($oppus[hp]>0){
		echo"<br/><img src='img/dob.jpg' style='margin-right:10px;margin-left:4px;float:left;' alt=''/>$oppusd[nick]<br/><img src='/images/icon/vit.png' alt='vit'/> $oppus[hp] хп.<br/><img src='/images/icon/str.png' alt='str'/> Сила: ~$oppus[uron]<br/><a class='btn' href='?attack=1&id=$oppusd[id]'><span style='margin-top:5px;margin-left:-3px;' class='label'>Добить</span></a><br/><br/>";
		}
		}
		while($opp = mysql_fetch_array($opponents))
		{
			$oppu = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".$opp['uid']."' LIMIT 1"));
			echo"<br/><img src='img/atak.jpg' style='margin-right:10px;margin-left:4px;float:left;' alt=''/>$oppu[nick]<br/><img src='/images/icon/vit.png' alt='vit'/> $opp[hp] хп.<br/><img src='/images/icon/str.png' alt='str'/> Сила: ~$opp[uron]<br/><a class='btn' href='?attack=1&id=$opp[uid]'><span style='margin-top:5px;margin-left:-3px;' class='label'>Атакoвать</span></a><br/><br/>";
		}
	}else{
		$_SESSION['light'] = "<span class='dgreen' style='font-weight:bold;'>Победа!</span>";
		if($usz[status]==1){
		mysql_query("UPDATE `zamki` SET `status`='0',`kontrol` = '".$usi['cid']."',`controltime` = '".time()."' WHERE id='$usi[zid]'");
		$opponents = mysql_query("SELECT * FROM `zamki_user` WHERE `cid` = '".$usi['cid']."'");
		while($opp = mysql_fetch_array($opponents))
		{
			$exp=ceil($opp[nanesuron]/3)+($opp[kill]*10);
			$gold=ceil($opp[nanesuron]/500)+($opp[kill]);
			$silver=ceil($opp[nanesuron]/50)+($opp[kill]*20);
			mysql_query("UPDATE `user` SET `exp`=`exp`+'$exp',`gold`=`gold`+'$gold',`silver`=`silver`+'$silver' WHERE id='$opp[uid]'");
			$time = time();
			$msgs='Ваш клан победил!Вы нанесли '.$opp[nanesuron].' урона и убили '.$opp[kill].' противников,за это вы получили: '.$exp.' опыта, '.$gold.' золота, '.$silver.' серебра';
			mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`, `read`) values('1', '".abs(intval($opp[uid]))."', '$msgs', '$time', '1')");
			mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('".abs(intval($opp[uid]))."', '1', '$time')");
			mysql_query("UPDATE `users_konts` SET `new_msg` = '1' WHERE `id_kont` = '1' AND `id_user` = '".abs(intval($opp[uid]))."'");
			
		}
		$opponents = mysql_query("SELECT * FROM `zamki_user` WHERE `cid` != '".$usi['cid']."'");
		while($opp = mysql_fetch_array($opponents))
		{
			$exp=ceil($opp[nanesuron]/6)+($opp[kill]*10);
			$gold=ceil($opp[nanesuron]/1000)+($opp[kill]);
			$silver=ceil($opp[nanesuron]/100)+($opp[kill]*20);
			mysql_query("UPDATE `user` SET `exp`=`exp`+'$exp',`gold`=`gold`+'$gold',`silver`=`silver`+'$silver' WHERE id='$opp[uid]'");
			$time = time();
			$msgs='Ваш клан проиграл!Вы нанесли '.$opp[nanesuron].' урона и убили '.$opp[kill].' противников,за это вы получили: '.$exp.' опыта, '.$gold.' золота, '.$silver.' серебра';
			mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`, `read`) values('1', '".abs(intval($opp[uid]))."', '$msgs', '$time', '1')");
			mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('".abs(intval($opp[uid]))."', '1', '$time')");
			mysql_query("UPDATE `users_konts` SET `new_msg` = '1' WHERE `id_kont` = '1' AND `id_user` = '".abs(intval($opp[uid]))."'");	
		}
		mysql_query("DELETE FROM zamki_user");
		mysql_query("DELETE FROM zamki_clan");
		}
		$_SESSION[dob]="";
		header("location: index.php");
	}
}
}else{
	$_SESSION[dob]="";
	$_SESSION['light'] = "<span class='dred' style='font-weight:bold;'>Поражение!</span>";
	if($usz[status]==1){
		mysql_query("UPDATE `zamki` SET `status`='0' WHERE id='$usi[zid]'");
		$opponents = mysql_query("SELECT * FROM `zamki_user`");
		while($opp = mysql_fetch_array($opponents))
		{
			$time = time();
			$msgs='К сожелению, никто не выиграл, но может вам повезёт в следующем бою';
			mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`, `read`) values('1', '".abs(intval($opp[uid]))."', '$msgs', '$time', '1')");
			mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('".abs(intval($opp[uid]))."', '1', '$time')");
			mysql_query("UPDATE `users_konts` SET `new_msg` = '1' WHERE `id_kont` = '1' AND `id_user` = '".abs(intval($opp[uid]))."'");
		}
		mysql_query("DELETE FROM zamki_user");
		mysql_query("DELETE FROM zamki_clan");
	}
	header("location: index.php");
	
	
}

}
}else{
	echo'dead';
	$_SESSION[dob]="";
	$_SESSION['light'] = "<span class='dgreen' style='font-weight:bold;'>Вы погибли,ждите конца боя!</span>";
	header("location: index.php");
}

require_once '../core/foot.php';
?>