<?php
define('PROTECTOR', 1);
include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
$textl='Противостояние';
//ryd();
going();
include($path.'files/core.php');

if($_GET['mod']=='taran' OR $_GET['mod']=='go' OR $_GET['mod']=='outtower' OR $_GET['mod']=='repair'){
$header=TRUE;
}else{
}
include($path.'files/head.php');
include($path.'files/zag.php');

$random=rand(1000,9999);

$timer=time()+300;

switch($_GET['mod']){

default:

echo'<div class="main"><div class="block_zero center"><span style="color: #9bc;">Тысячелетнее противостояние Бессмертных!</span></div><div class="mini-line"></div><div class="block_zero center"><img src="image/logo/clanwar.png" width="120" height="60" alt="logo"></div><div class="mini-line"></div><div class="block_zero center">';domin();echo'</div><div class="dot-line"></div>';

echo"<div class='menuList'>";

if($mestouser['towers']==0){
$req = mysql_query("SELECT * FROM `towers`");

$avto=mysql_num_rows($req);
if($avto>=1){
While($tower = mysql_fetch_array($req)){

if($tower['control']=='not'){
$tow="<img src='image/icon/bot.png'>";
}else{
$tow="<img src='image/towers/$tower[control].png'>";
}

if($udata['lvl']>$tower['mlvl'] or $udata['lvl']<$tower['lvl']){
$col="<span style='color: #c66;'>";
}else{
$col="<span style='color: #9c9;'>";
}



echo"<li><a href=\"towers.php?mod=go&amp;id=$tower[id]\">$tow$col$tower[name]<img src='image/icon/level.png'><span style='color: #ffffff;'>$tower[lvl]-$tower[mlvl]ур<br><small>Прочность: <img src='image/icon/health.png'>$tower[hp]/$tower[hpall]<br>Награда: <img src='image/icon/premium.png'>$tower[point] очков славы</small></span></li></a>";
}
}else{
echo'<div class="main"><div class="block_zero center">Нет Врат!</div></div>';
}
}
echo'</div><div class="mini-line"></div>';
echo'<ul class="hint"><li>Лидирующая сторона получает 25% скидку при покупке вооружения!</a></li></ul>';
echo'</div></div>';
break;


case 'vhod':

echo'<div class="main"><div class="block_zero center"><span style="color: #9bc;">Тысячелетнее противостояние Бессмертных!</span></div><div class="mini-line"></div><div class="block_zero center"><img src="image/logo/newfight.jpg"  alt="logo"></div><div class="mini-line"></div><div class="block_zero center">';

$tower = mysql_fetch_array(mysql_query("SELECT * FROM `towers` WHERE id='$mestouser[towers]'"));

if($tower['control']=='not'){
echo"<img src='image/icon/bot.png' alt='*'/> ";
}else{
echo"<img src='image/towers/$tower[control].png' alt='*'/> ";
}

echo"$tower[name] <br/><img src=\"hptower.php\" alt=\"*\"/></div><div class='dot-line'></div><div class=\"block_zero center\">";

if($tower['control']=='black'){
$strazh="Врата охраняют Титаны!";
}
elseif($tower['control']=='white'){
$strazh="Врата охраняют Боги!";
}else{
$strazh="Врата никто не охраняет!";
}

if($tower['control']!=$udata['storona']){
echo"$strazh<div class=\"separ\"></div><a href=\"towers.php?mod=taran\"><span class=\"btn\"><span class=\"end\"><input class=\"label\" type=\"submit\" value=\"Таранить Врата\"></span></span></a>";
}else{
echo"$strazh<div class=\"separ\"></div><a href=\"towers.php?mod=repair\"><span class=\"btn\"><span class=\"end\"><input class=\"label\" type=\"submit\" value=\"Укрепить Врата\"></span></span></a>";
}
echo'</div><div class="dot-line"></div><div class="block_zero">';

echo'Бессмертные:';
$odin=false;
$timeout=time()-300;//оффлайн
$req = mysql_query("SELECT * FROM online WHERE laikas > '$timeout' and `usr`!='$log'");
While($who = mysql_fetch_array($req)){
$reqs = mysql_query("SELECT * FROM `mesto` WHERE `usr` = '$who[usr]' LIMIT 1");
$mesto = mysql_fetch_array($reqs);//место
$reqs = mysql_query("SELECT id,city,storona,hp FROM `users` WHERE `usr` = '$who[usr]' LIMIT 1");
$cit = mysql_fetch_array($reqs);//город

if($mesto['city']==0 && $mesto['towers']==$mestouser['towers']){
$idlog=$who['usr'];
if($cit['storona']==$udata['storona']){
echo'<font color="green"> Союзник: ';
echo''.$idlog.', '; 
}else{
echo'<font color="red"> Враг: ';
echo''.$idlog.', '; 
/*
echo" <a href=\"ok.php?k=pk&amp;id=$cit[id]\">Бить($cit[hp] хп)</a></font><br/>";
*/
}
$odin=true;
}
}
if($odin==false){
echo' никого нет';
}

echo'</div><div class="dot-line"></div><div class="block_zero center">';

echo"<a href=\"towers.php?mod=outtower\"><span class=\"btn\"><span class=\"end\"><input class=\"label\" type=\"submit\" value=\"Выйти из боя\"></span></span></a>";

echo'</div><div class="dot-line"></div><div class="block_zero">';

log_msg($tlog='tower');
echo'</font></div></div>';
break;






case 'go':
$req = mysql_query("SELECT * FROM `towers` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."'");
$avto=mysql_num_rows($req);
$tower = mysql_fetch_array($req);
if($avto==0){header('Location: towers.php?');
exit();
}

if($udata['lvl']>$tower['mlvl'] AND $log!='Admin'){
header('Location: towers.php?');
$_SESSION['err'] = 'У Вас высокий уровень!';
exit();
}

if($udata['lvl']<$tower['lvl'] AND $log!='Admin'){
header('Location: towers.php?');
$_SESSION['err'] = 'У Вас низкий уровень!';
exit();
}

mysql_query("UPDATE `mesto` SET `towers` = '".mysql_real_escape_string($_GET['id'])."' WHERE `usr`='$log'");

mysql_query("INSERT INTO log SET usr='$log',text='Вы вошли во $tower[name]',kto='system',place='tower',timer='$timer'");

header('Location: towers.php?mod=vhod');
exit();

break;

case 'taran':
$req = mysql_query("SELECT * FROM `towers` WHERE id='$mestouser[towers]'");
$avto=mysql_num_rows($req);
if($avto==0){
header('Location: towers.php?');
$_SESSION['err'] = 'Вы не находитесь возле Врат!';
exit();
}

$tower = mysql_fetch_array($req);
if($tower['control']==$udata['storona']){
header('Location: towers.php?mod=vhod');
$_SESSION['message'] = 'Врата уже Ваши!';
exit();
}

if($tower['hp']<=0){
header('Location: towers.php?mod=vhod');
$_SESSION['message'] = 'Врата уже Ваши!';
exit();
}else{
$uron=rand($udata['sila']-$udata['sila']/8,$udata['sila']+$udata['sila']/8);
$muron=2+rand(0,$udata['hp']/5);
$tower['hp']=$tower['hp']-$uron;

if($tower['hp']<=0){
$tow = mysql_query("SELECT * FROM `towers`");
$am=0;
While($sum = mysql_fetch_array($tow)){
$am=$am+$sum['point'];
}
$proc=$am/100;
$point=round($tower['point']/$proc);
$hpin=round($tower['hpall']/1.5);
mysql_query("UPDATE `towers` SET `control`='$udata[storona]',`hp`='$hpin' WHERE id='$mestouser[towers]'");
$req = mysql_query("SELECT * FROM `domination` WHERE `id` = '1' LIMIT 1");
$dom = mysql_fetch_array($req);
$m=explode(",",$tower['money']);
$m=rand($m['0'],$m['1']);
$udata['money']=$udata['money']+$m;
$udata['slava']=$udata['slava']+$tower['point'];
$udata['exp']=$udata['exp']+$tower['exp'];

if($udata['storona']=='white'){
$my=$dom['white'];
$enemy=$dom['black'];
}else{
$my=$dom['black'];
$enemy=$dom['white'];
}
$my=$my+$point;
if($tower['control']=='not'){
if($udata['storona']=='white'){
mysql_query("UPDATE `domination` SET `white` = '$my' WHERE `id` = '1'");
}else{
mysql_query("UPDATE `domination` SET `black` = '$my' WHERE `id` = '1'");
}
}else{
$enemy=$enemy-$point;
if($udata['storona']=='white'){
mysql_query("UPDATE `domination` SET `white` = '$my',`black`='$enemy' WHERE `id` = '1'");
}else{
mysql_query("UPDATE `domination` SET `black` = '$my',`white`='$enemy' WHERE `id` = '1'");
}
}
mysql_query("UPDATE `users` SET `slava` = '$udata[slava]',`exp` = '$udata[exp]',`money` = '$udata[money]' WHERE `usr` = '$log'");
$resd = mysql_query("SELECT `prodob` FROM `dost` WHERE `usr`='$log'") or die (mysql_error());
$res = mysql_fetch_array($resd);
$dob=$res['prodob']+1;
mysql_query("UPDATE `dost` SET `prodob` = '$dob' WHERE `usr` = '$log'");
$req = mysql_query("SELECT * FROM `clan` where `lider`='$udata[clan]'");
$avto=mysql_num_rows($req);
$clan = mysql_fetch_array($req);
if($avto==1){
$clexp=$tower[exp]/10;
}
mysql_query("UPDATE `clan` SET `exp` = `exp`+'$clexp' WHERE `lider` = '$udata[clan]'");
mysql_query("UPDATE `users` SET `clanexp` = `clanexp`+'$clexp' WHERE `usr` = '$log'");
mysql_query("INSERT INTO log SET usr='$log',text='Вы захватили $tower[name]! + $tower[exp] опыта, +$m монет',kto='user',place='tower',timer='$timer'");
header ("Location: towers.php?mod=vhod");
exit;
}
if($udata['hp']<='0'){
//mysql_query("UPDATE `mesto` SET `place` = 'gorod',`city` = '0' WHERE usr='$log'");
header ("Location: index.php?");
exit;
}
mysql_query("UPDATE `towers` SET `hp` = '$tower[hp]' WHERE id='$mestouser[towers]'");
mysql_query("UPDATE `users` SET `hp` = '$udata[hp]'-'$muron' WHERE usr='$log'");
mysql_query("INSERT INTO log SET usr='$log',text='Вы нанесли урон $uron',kto='user',place='tower',timer='$timer'");
mysql_query("INSERT INTO log SET usr='$log',text='Вас ударили -$muron',kto='enemy',place='tower',timer='$timer'");
header ("Location: towers.php?mod=vhod");
exit;
}
break;

case 'repair':
$req = mysql_query("SELECT * FROM `towers` WHERE `id` = '$mestouser[towers]' AND `control` = '$udata[storona]'");
$avto=mysql_num_rows($req);
if($avto==0){
header('Location: towers.php?');
$_SESSION['err'] = 'Вы не находитесь возле Врат!';
exit();
}
$tower = mysql_fetch_array($req);
$req = mysql_query("SELECT * FROM `res` WHERE `lat_name` = 'kamen' AND `kol` >= '1' AND `usr` = '$log'");
$kamen=mysql_num_rows($req);
if($kamen==0){
mysql_query("INSERT INTO log SET usr='$log',text='Нет камня',kto='system',place='tower',timer='$timer'");

header ("Location: towers.php?mod=vhod");exit;}
$kam = mysql_fetch_array($req);
if($tower['hp']>=$tower['hpall']){
mysql_query("INSERT INTO log SET usr='$log',text='Врата целы',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?mod=vhod");exit;
}$tower['hp']=$tower['hp']+50;
if($tower['hp']>$tower['hpall']){$tower['hp']=$tower['hpall'];
}


$kam['kol']--;
mysql_query("UPDATE `res` SET `kol` = '$kam[kol]' WHERE `usr`='$log' and `lat_name` = 'kamen'");
mysql_query("UPDATE `towers` SET `hp` = '$tower[hp]' WHERE id='$mestouser[towers]' and control='$udata[storona]'");
mysql_query("INSERT INTO log SET usr='$log',text='Вы отремонтировали Врата на 50 едениц, потратили 1 камень',kto='user',place='tower',timer='$timer'");
header ("Location: towers.php?mod=vhod");exit;
break;

case 'outtower':
mysql_query("UPDATE `mesto` SET `towers` = '0' WHERE `usr`='$log'");
header ("Location: towers.php?");exit;
break;}
echo'</font>';
include($path.'files/down.php');
?>