<?
define('PROTECTOR', 1);

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

include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
going();
place_city();
place_okr();
place_zamok();
ryd();
include($path.'files/core.php');

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

switch($_GET[mod]){

default:
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> (Уровень $tower[lvl]/$tower[mlvl], $tower[cena] монет)<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/>";
if($tower[control]!=$udata[storona]){
echo"<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/>';
}
}
log_msg($tlog='tower');
echo"<a href=\"towers.php?mod=outtower\">Башни</a><br/>";
echo"<a href=\"to_gorod.php?\">Город</a>";
break;

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

if($tower[cena]>$udata[money]){

mysql_query("INSERT INTO log SET usr='$log',text='Нехватает монет',kto='system',place='tower',timer='$timer'");

header ("Location: towers.php?");exit;
}

if($tower[mlvl]<$udata[lvl] and $log!='Admin'){

mysql_query("INSERT INTO log SET usr='$log',text='У вас большой уровень',kto='system',place='tower',timer='$timer'");

header ("Location: towers.php?");exit;
}

if($tower[lvl]>$udata[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("UPDATE `users` SET `money` = '".($udata[money]-$tower[cena])."' WHERE `usr`='$log'");

mysql_query("INSERT INTO log SET usr='$log',text='Вы вошли в башню $tower[name] за $tower[cena] монет',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[umin],$udata[umax])+$udata[sila];

$tower[hp]=$tower[hp]-$uron;

if($tower[hp]<=0){
mysql_query("UPDATE `towers` SET `control`='$udata[storona]',`hp`='$tower[hpall]' 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+$tower[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-$tower[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'");

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;
}
mysql_query("UPDATE `towers` SET `hp` = '$tower[hp]' WHERE id='$mestouser[towers]'");

mysql_query("INSERT INTO log SET usr='$log',text='Вы нанесли урон $uron',kto='user',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]+5;
if($tower[hp]>$tower[hpall]){$tower[hp]=$tower[hpall];}

if(($kam[kol]-1)==0){
mysql_query("DELETE FROM `res` WHERE `usr`='$log' and `lat_name` = 'kamen'");
}else{
$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='Вы отремонтировали башню на 5 едениц',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($path.'files/down.php');
?>
