<?
define('PROTECTOR', 1);

$headmod = 'towers';//фикс. места

@include('files/db.php');
@include('files/auth.php');
@include('files/func.php');
ryd();
@include('files/core.php');

if($_GET[mod]==taran or $_GET[mod]==go or $_GET[mod]==outtower or $_GET[mod]==repair){
$header=TRUE;}else{
$textl='Башни';}
@include('files/head.php');
@include('files/zag.php');
////////////////////////
$random=rand(1000,9999);
////////////////
$timer=time()+300;

switch($_GET[mod]){

default:
echo"<font color='#ffc22b'> • Захватывайте башни и получайте бонусы! Значком <img src='pic/main/status/mmod.png' alt=''/> показано, сколько очков славы вы получите за захват$div</font>";
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'){
echo"<img src='pic/main/nocontrol.png' alt='*'/> ";}else{
echo"<img src='pic/main/$tower[control].png' alt='*'/> ";}
echo"<a href=\"towers.php?mod=go&amp;id=$tower[id]\">$tower[name]</a> (<img src='pic/up.png' alt=''/>$tower[lvl]/$tower[mlvl], $tower[hp]/$tower[hpall]хп) <img src='pic/main/status/mmod.png' alt=''/>$tower[point]<br/>";
}}else{
echo'Нет башен!<br/>';}}else{
$req = mysql_query("SELECT * FROM `towers` WHERE id='$mestouser[towers]'");
$tower = mysql_fetch_array($req);

if($tower[control]=='not'){
echo"<img src='pic/main/nocontrol.png' alt='*'/> ";
}else{
echo"<img src='pic/main/$tower[control].png' alt='*'/> ";}
echo"$tower[name] - $tower[hp]/$tower[hpall]хп<br/>
<img src=\"hp.php\" alt=\"*\"/><br/>";
if($tower[control]==black){$strazh="Страж темных";}else{$strazh="Страж светлых";}
if($tower[control]!=$udata[storona]){
echo"Охрана: <b>$strazh</b></br><a href=\"towers.php?mod=taran\">Бить башню</a><br/>";
}else{
echo"<a href=\"towers.php?mod=repair\">Ремонтировать</a><br/>";}
echo'<b>Игроки:</b><br/>';
$odin=false;
$timeout=time()-150;//оффлайн
$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']==3 && $mesto['towers']==$mestouser['towers']){
$idlog=$who[usr];
if($cit[storona]==$udata[storona]){echo'<font color="green">Союзник: ';}else{
echo'<font color="red">Враг: ';}
idlog($idlog); echo" <a href=\"ok.php?k=pk&amp;id=$cit[id]\">Бить($cit[hp] хп)</a></font><br/>";
$odin=true;}}
if($odin==false){
echo'Никого нет!<br/>';}
echo"$div<font color=\"#ffc22b\"><b>Локации:</b></br>
<img src=\"pic/main/def.png\" alt=\"*\"/> <a href=\"towers.php?mod=outtower\">Башни</a><br/>";
}
log_msg($tlog='tower');
break;

case 'go':
$req = mysql_query("SELECT * FROM `towers` WHERE id='$_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'){
mysql_query("INSERT INTO log SET usr='$log',text='У вас большой уровень',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?");exit;}
if($udata[lvl]<$tower[lvl] and $log!='Admin'){
mysql_query("INSERT INTO log SET usr='$log',text='У вас маленький уровень',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?");exit;}
mysql_query("UPDATE `mesto` SET `towers` = '$_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?");exit;
break;

case 'taran':
$req = mysql_query("SELECT * FROM `towers` WHERE id='$mestouser[towers]'");
$avto=mysql_num_rows($req);
if($avto==0){
mysql_query("INSERT INTO log SET usr='$log',text='Вы ненаходитесь в башне',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?");exit;}
$tower = mysql_fetch_array($req);
if($tower[control]==$udata[storona]){
mysql_query("INSERT INTO log SET usr='$log',text='Башня уже ваша',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?");exit;}
if($tower[hp]<=0){
mysql_query("INSERT INTO log SET usr='$log',text='Башня уже ваша',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?");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'");
mysql_query("INSERT INTO log SET usr='$log',text='Вы захватили башню $tower[name]! + $tower[exp] опыта, +$m монет',kto='user',place='tower',timer='$timer'");
header ("Location: towers.php?");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?");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){
mysql_query("INSERT INTO log SET usr='$log',text='Вы ненаходитесь в башне',kto='system',place='tower',timer='$timer'");
header ("Location: towers.php?");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?");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?");exit;
}$tower[hp]=$tower[hp]+40;
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='Вы отремонтировали башню на 40 едениц, потратили 1 камень',kto='user',place='tower',timer='$timer'");
header ("Location: towers.php?");exit;
break;

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