<?
define('PROTECTOR', 1);

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

$time=time();
////////////////////////////////////////////////////////
if ($user_id==1){
if ($inaw==1){
include($path.'files/head.php');
include($path.'files/zag.php');
echo'У вас бой на арене!';
include($path.'files/down.php');exit;
}
///////////////
$id="$_GET[id]";
if(empty($_GET[k])){
/////////////////////////////////////////////////////
////////////////////////
$req = mysql_query("SELECT * FROM `mesto` WHERE `usr` = '$log' LIMIT 1");
////////////////////////////
$mestouser = mysql_fetch_assoc($req);

$req = mysql_query("SELECT * FROM `mob` WHERE `city` = '$udata[city]' and `id`='$id' and `x`='$mestouser[x]' and `y`='$mestouser[y]' LIMIT 1");
////////////////////////////
if (mysql_num_rows($req)==0){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo"Такого монстра не существует.";include($path.'files/down.php');exit;}
///////////////////////////// 
$mob = mysql_fetch_assoc($req);
if($mob['status']=='battle' or $mob['status']=='off'){
header ('Location: okrestnosti.php?');exit; 
}
if($mob[tip] == life or $mob[status] ==сbattle){
mysql_query("UPDATE `mob` SET `status` = 'battle',`oponent`='$log' WHERE `city` = '$udata[city]' and `id`='$id' LIMIT 1"); }

if($mob[city] == clan_city){
mysql_query("UPDATE `mob` SET `status` = 'сbattle',`oponent`='$log' WHERE `city` = '$udata[city]' and `id`='$id' LIMIT 1");
 }

mysql_query("INSERT INTO `tmp` SET `usr` = '$log',`mob` = '$id',`location`='$udata[location]',`city` = '$udata[city]',`ltime`='$time'");

$timer=time()+300;

del_log($lpl='battle');

mysql_query("INSERT INTO log SET usr='$log',text='$log против $mob[name]!Бой начался!',kto='system',place='battle',timer='$timer'");

header ('Location: battle.php?');exit; 
}elseif($_GET['k']=='pk'){
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$_GET[id]'");
////////////////////////////
$avto=mysql_num_rows($req);
if($avto=="0"){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'Нет такого игрока!';
include($path.'files/down.php');exit;
}
$pk = mysql_fetch_assoc($req);

$req = mysql_query("SELECT * FROM `pk` WHERE `usr` = '$pk[usr]' LIMIT 1");
$avto=mysql_num_rows($req);

if($avto>="1"){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'Игрок уже в бою!';
include($path.'files/down.php');exit;
}


$reqtmp = mysql_query("SELECT * FROM `tmp` WHERE `usr` = '$pk[usr]' LIMIT 1");
if (mysql_num_rows($reqtmp)>=1){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'Игрок уже в бою!';
include($path.'files/down.php');exit;
}

$reqtmp = mysql_query("SELECT * FROM `tmp_zamok` WHERE `usr` = '$pk[usr]' LIMIT 1");
if (mysql_num_rows($reqtmp)>=1){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'Игрок уже в бою!';
include($path.'files/down.php');exit;
}

$reqtmp = mysql_query("SELECT * FROM `tmp_zamok` WHERE `usr` = '$log' LIMIT 1");
if (mysql_num_rows($reqtmp)>=1){header ("Location: battle.php?");exit;}

if($pk[usr]==$log){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'На себя нельзя нападать!';
include($path.'files/down.php');exit;
}

$req = mysql_query("SELECT * FROM `mesto` WHERE `usr` = '$pk[usr]'");
$mpk = mysql_fetch_assoc($req);
$req = mysql_query("SELECT * FROM `mesto` WHERE `usr` = '$log'");
$mypk = mysql_fetch_assoc($req);

if(($udata[lvl]-$pk[lvl])>3 and $mypk[place]=='1'){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'На слабых нельзя нападать!';
include($path.'files/down.php');exit;
}
$timeout = time() - 150;

$asd = mysql_num_rows(mysql_query("SELECT laikas, usr FROM online WHERE laikas > '$timeout' AND usr='$pk[usr]'"));
////////////////////////////

if($asd=='0'){
$textl='Битва';
include($path.'files/head.php');
include($path.'files/zag.php');
echo'Этот игрок offline!';
include($path.'files/down.php');exit;
}
if($mpk['city']=='0' or $mpk['city']!=$mypk['city'] or $pk['hp']<='0' or $mpk['x']!=$mypk['x'] or $mpk['y']!=$mypk['y'] or $mpk['towers']!=$mypk['towers']){
$textl='Битва';
require_once'files/head.php';
require_once'files/zag.php';
echo'Нельзя напасть!';
include($path.'files/down.php');exit;
}

mysql_query("INSERT INTO 
    `pk` SET `usr` = '$log',
    `enemy` = '$pk[usr]',
    `napal` = '1',
    `xod` = 'not',
    `ltime`='$time'");
    
    mysql_query("INSERT INTO 
    `pk` SET `usr` = '$pk[usr]',
    `enemy` = '$log',
    `xod` = 'not',
    `ltime`='$time'");

del_log($lpl='pk');

$timer=time()+300;
mysql_query("INSERT INTO log SET usr='$log',text='$log против $pk[usr]!Бой начался!',kto='system',place='pk',timer='$timer'");
mysql_query("INSERT INTO log SET usr='$pk[usr]',text='$log против $pk[usr]!Бой начался!',place='pk',timer='$timer'");

header ('Location: pk.php?');exit;

}else{
echo'Ошибка!';include($path.'files/down.php');exit;}
}else
{
echo 'Ошибка!Вы не авторизованы!<a href="index.php">Авторизуйтесь</a>';require_once 'files/end.php';exit;
}
?>