<?php
define('PROTECTOR', 1);

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

$textl='Разговор';
include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
going();
zasad();
ryd();
place_okr();
place_zamok();
place_tower();
include($path.'files/core.php');
include($path.'files/head.php');
include($path.'files/zag.php');

if(empty($_GET[id])){
echo"Такого персонажа не существует";
include($path.'files/down.php');exit;
}
$req = mysql_query("SELECT * FROM `npc` WHERE `id` = '$_GET[id]'");
$avto=mysql_num_rows($req);
if($avto>=1){
if(empty($_GET[say])){
$_GET[say]='begin';
}
//////////////////////////////////
$npc = mysql_fetch_array($req);
////////////////////////////////
///////////////определим квест

$ue=FALSE;

if(!empty($npc[quest])){
$v=explode(",",$npc[quest]);
$vk=count($v);
$i=0;
$b=1;
while($b<=$vk){
$req = mysql_query("SELECT * FROM `userquests` WHERE `quest` = '$v[$i]' and `usr`='$log' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==1){$j = mysql_fetch_array($req);}
if($avto==0){
$npc[quest]=$v[$i];
$ue=TRUE;
break;
}elseif($j[status]==start){
$npc[quest]=$v[$i];
$ue=TRUE;
break;
}
$i++;
$b++;
}
}

if($ue==FALSE){
$npc[quest]=0;
}

$req = mysql_query("SELECT id FROM `npc_speack` WHERE `npc` = '$_GET[id]' and `dialog`='$_GET[say]' and `quest`='$npc[quest]' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==0){
$req = mysql_query("SELECT * FROM `npc_speack` WHERE `npc` = '$_GET[id]' and `dialog`='$_GET[say]' and `quest`='0' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==0){
$_GET[say]='begin';
}
}
////////////////////
$req = mysql_query("SELECT * FROM `quests` WHERE `id`='$npc[quest]' LIMIT 1");
$quest = mysql_fetch_array($req);

if($_GET[say]==quest_start){
////////////
if(empty($npc[quest])){
echo'У этого персонажа нет для вас квестов!';
include($path.'files/down.php');exit;
}
//////////////////
$req = mysql_query("SELECT * FROM `userquests` WHERE `quest` = '$npc[quest]' and `usr`='$log'");
$avto=mysql_num_rows($req);
if($avto==1){$q = mysql_fetch_array($req);}
if($avto==0){

mysql_query("INSERT INTO
        `userquests` SET
        `usr` = '$log',
        `quest` = '$npc[quest]',
        `status` = 'start'");

$req = mysql_query("SELECT * FROM `quest_kill` WHERE `quest` = '$npc[quest]' and `city`='$udata[city]'");
$avto=mysql_num_rows($req);
if($avto>=1){
$mag = mysql_fetch_array($req);

mysql_query("INSERT INTO
        `user_kill` SET
        `usr` = '$log',
        `quest` = '$mag[quest]',
        `city` = '$mag[city]',
        `killing` = '$mag[killing]'");

}

$req = mysql_query("SELECT * FROM `quest_assasin` WHERE `quest` = '$npc[quest]' and `city`='$udata[city]'");
$avto=mysql_num_rows($req);
if($avto>=1){
While($mag = mysql_fetch_array($req))
{
mysql_query("INSERT INTO
        `user_assasin` SET
        `usr` = '$log',
        `quest` = '$mag[quest]',
        `city` = '$mag[city]',
        `mob` = '$mag[mob]'");
}
}

$_GET[say]='quest_start';    
}else{
$_GET[say]='begin';
}
}
///////////////////////
elseif(!empty($npc[quest]) and $ue==TRUE){
//////////////////////////
$req = mysql_query("SELECT * FROM `userquests` WHERE `usr`='$log' and `status`='start' and `quest`='$npc[quest]'");
////////////////////////////
$avto=mysql_num_rows($req);
if($avto==1){
$req = mysql_query("SELECT * FROM `quests` WHERE `id`='$npc[quest]'");
$q = mysql_fetch_array($req);

if($q[arenawins]>$udata[arenawins]){

$not=TRUE;
}

if($q[slava]>$udata[slava]){

$not=TRUE;
}

if($q[wins]>$udata[wins]){

$not=TRUE;
}

if(!empty($q[nead_res])){
$res=explode("|",$q[nead_res]);
$kres=count($res);
$i=0;
$b=1;
while($b<=$kres){
$r=explode(",",$res[$i]);
$lat_name=$r[0];
$kol=$r[1];

$req = mysql_query("SELECT * FROM `res` WHERE `usr`='$log' and `lat_name`='$lat_name' and `kol`>='$kol' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==0){
$not=TRUE;
}
$res[$b]=$lat_name;
$rb[$b]=$kol;
$i++;
$b++;
}
}
///////проверяем килл
$req = mysql_query("SELECT * FROM `user_kill` WHERE `quest` = '$npc[quest]' and `usr`='$log'");
$avto=mysql_num_rows($req);
if($avto>=1){
$not=TRUE;
}
//проверяем асса
$req = mysql_query("SELECT * FROM `user_assasin` WHERE `quest` = '$npc[quest]' and `usr`='$log'");
$avto=mysql_num_rows($req);
if($avto>=1){
$not=TRUE;
}

if(empty($not)){
mysql_query("UPDATE `userquests` SET `status` = 'complete' WHERE `usr` = '$log' and `quest`='$q[id]'");
$_GET[say]='quest_complete';
$b=1;
while($b<=$kres){
$r=explode(",",$res[$i]);
$lat_name=$res[$b];
$kol=$rb[$b];

$req = mysql_query("SELECT * FROM `res` WHERE `usr`='$log' and `lat_name`='$lat_name' LIMIT 1");
$re = mysql_fetch_array($req);
if(($re[kol]-$kol)<=0){
mysql_query("DELETE FROM `res` WHERE `usr`='$log' and `lat_name`='$lat_name' LIMIT 1");//чистим логи
}else{
$ros=$re[kol]-$kol;
mysql_query("UPDATE `res` SET `kol` = '$ros' WHERE `usr`='$log' and `lat_name`='$lat_name' LIMIT 1");
}
$b++;
}
///оружие
$item = false;
$b = mysql_query("SELECT * FROM quest_item WHERE quest = '$npc[quest]'");
////////////////////////////
$avto=mysql_num_rows($b);
if($avto>=1){
While($mag = mysql_fetch_array($b))
{
        mysql_query("INSERT INTO
        `item` SET
        `usr` = '$log',
        `tip` = '$mag[tip]',
        `name` = '$mag[name]',
        `cena` = '$mag[cena]',
        `umin` = '$mag[umin]',
        `umax` = '$mag[umax]',
        `pgolova` = '$mag[pgolova]',
        `pbody` = '$mag[pbody]',
        `pnogi` = '$mag[pnogi]',
        `hp` = '$mag[hp]',
        `mp` = '$mag[mp]',
        `krit` = '$mag[krit]',
        `ukrit` = '$mag[ukrit]',
        `antikrit` = '$mag[antikrit]',
        `sila` = '$mag[sila]',
        `lovk` = '$mag[lovk]',
        `um` = '$mag[um]',
        `prot` = '$mag[prot]',
        `klas` = '$mag[klas]',
        `nsila` = '$mag[nsila]',
        `num` = '$mag[num]',
        `nlovk` = '$mag[nlovk]',
        `nlvl` = '$mag[nlvl]',
        `image` = 'not'");
        
        $item = $item . "Получена новая вещь $mag[name]<br/>";
}
}
//////ресы
$resurs = false;

$b = mysql_query("SELECT * FROM quest_res WHERE quest = '$npc[quest]'");
////////////////////////////
$avto=mysql_num_rows($b);
if($avto>=1){
While($mag = mysql_fetch_array($b))
{
$req = mysql_query("SELECT * FROM `res` WHERE `usr`='$log' and `lat_name`='$mag[lat_name]' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==1){
$m = mysql_fetch_array($req);
$magkol=$mag[kol]+$m[kol];
mysql_query("UPDATE `res` SET `kol` = '$magkol' WHERE `usr`='$log' and `lat_name`='$mag[lat_name]' LIMIT 1");
$resurs = $resurs . "Получен новый ресурс $mag[name]($mag[kol] штук)<br/>";
}else{
mysql_query("INSERT INTO
        `res` SET
        `usr` = '$log',
        `name` = '$mag[name]',
        `lat_name` = '$mag[lat_name]',
        `tip` = '$mag[tip]',
        `what` = '$mag[what]',
        `give` = '$mag[give]',
        `kol` = '$mag[kol]',
        `cena` = '$mag[cena]'");
        
        $resurs = $resurs . "Получен новый ресурс $mag[name]($mag[kol] штук)<br/>";
}}
}
//маг
$magic = false;

$b = mysql_query("SELECT * FROM quest_mag WHERE quest = '$npc[quest]' and `klas` = '$udata[klas]'");
////////////////////////////
$avto=mysql_num_rows($b);
if($avto>=1){
While($mag = mysql_fetch_array($b))
{
$req = mysql_query("SELECT * FROM `mag` WHERE `usr`='$log' and `lat_name`='$mag[lat_name]' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==0){
mysql_query("INSERT INTO
        `mag` SET
        `usr` = '$log',
        `name` = '$mag[name]',
        `lat_name` = '$mag[lat_name]',
        `tip` = '$mag[tip]',
        `klas` = '$mag[klas]',
        `uron` = '$mag[uron]',
        `hp` = '$mag[hp]',
        `plushp` = '$mag[plushp]',
        `cena` = '$mag[cena]',
        `lvl` = '$mag[lvl]',
        `mp` = '$mag[mp]'");
        
        $magic = $magic . "Получено новое умение $mag[name]<br/>";
}}
}

//ауры
$aura = false;

$b = mysql_query("SELECT * FROM quest_aura WHERE quest = '$npc[quest]'");
////////////////////////////
$avto=mysql_num_rows($b);
if($avto>=1){
While($mag = mysql_fetch_array($b))
{
$req = mysql_query("SELECT * FROM `item_aura` WHERE `usr`='$log' and `lat_name`='$mag[lat_name]' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto==0){

mysql_query("INSERT INTO
        `item_aura` SET
        `usr` = '$log',
        `name` = '$mag[name]',
        `lat_name` = '$mag[lat_name]',
        `cena` = '$mag[cena]',
        `umin` = '$mag[umin]',
        `umax` = '$mag[umax]',
        `protect` = '$mag[protect]',
        `hp` = '$mag[hp]',
        `mp` = '$mag[mp]',
        `krit` = '$mag[krit]',
        `ukrit` = '$mag[ukrit]',
        `antikrit` = '$mag[antikrit]',
        `klas` = '$mag[klas]',
        `nsila` = '$mag[nsila]',
        `num` = '$mag[num]',
        `nlovk` = '$mag[nlovk]',
        `nlvl` = '$mag[nlvl]',
        `actime` = '$mag[actime]',
        `kol` = '1'");
        
        $aura = $aura . "Получено новая аура $mag[name]<br/>";
}}
}

echo"Квест <b>$quest[name]</b> выполнен!<br/>";
if(!empty($item)){
echo"$item";
}
if(!empty($resurs)){
echo"$resurs";
}
if(!empty($magic)){
echo"$magic";
}
if(!empty($aura)){
echo"$aura";
}
$udata[money]=$quest[money]+$udata[money];
$udata[exp]=$quest[exp]+$udata[exp];
mysql_query("UPDATE users SET exp='$udata[exp]',money='$udata[money]' WHERE usr = '$log'");//пишем данные в плэера


echo"+ $quest[exp] опыта!<br/>";
echo"+ $quest[money] монет!<br/>";
}
}
/////////////////////////
}
############################
$req = mysql_query("SELECT * FROM `npc_speack` WHERE `npc` = '$_GET[id]' and `dialog`='$_GET[say]' and `quest`='$npc[quest]'");
$avto=mysql_num_rows($req);
if($avto==0){
$req = mysql_query("SELECT * FROM `npc_speack` WHERE `npc` = '$_GET[id]' and `dialog`='$_GET[say]' and `quest`='0'");
$dialog = mysql_fetch_array($req);
}else{
$dialog = mysql_fetch_array($req);
}
$dialog=explode("|",$dialog[text]);
$dialog[0]= str_replace('<name>',"$log", "$dialog[0]");
echo'<div class="event">';
echo"<img src=\"pic/npc/$npc[img]\" alt=\"$npc[name]\" style=\"float:left;margin-right:8px;\"/>$dialog[0]";
if($_GET[say]=='end'){
echo"<br/><a href=\"taverna.php?\">[конец диалога]</a>";
}else{
$k=count($dialog)-1;
$i = 1;
while ($i <= $k){
$in=$i+1;
$say=$dialog[$in];
if($dialog[$i]==quest){
$req = mysql_query("SELECT * FROM `userquests` WHERE `quest` = '$npc[quest]' and `usr`='$log'");
$avto=mysql_num_rows($req);
if($avto==0){
$req = mysql_query("SELECT text FROM `npc_speack` WHERE `npc` = '$_GET[id]' and `dialog`='quest_ask' and `quest`='$npc[quest]'");
$di = mysql_fetch_array($req);
$say='quest_info';
$dialog[$i]=$di[text];

}else{
$nod=1;
}
}
if(empty($nod)){
$dialog[$i]= str_replace('<name>',"$log", "$dialog[$i]");
echo "<br/><a href=\"npc.php?&amp;id=$_GET[id]&amp;say=$say\">$dialog[$i]</a>";
}else{
unset($nod);
}
$i=$i+2;;
}
}
echo'</div>';
echo "<a href=\"taverna.php?\">Назад</a>";
}else{
echo"Такого персонажа не существует";
}
include($path.'files/down.php');
?>