<?
define('PROTECTOR', 1);

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

$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');


switch($_GET[mod]){

default:
echo"<a href=\"shopelexirs.php?mod=elexirs\">Отдел элексиров</a>";
break;

case 'elexirs':
$req = mysql_query("SELECT * FROM `shopelexirs` WHERE `city` = '$udata[city]' and `tip`='elexir'");
////////////////////////////
$avto=mysql_num_rows($req);

While($mag = mysql_fetch_array($req))
{
echo"<a href=\"shopelexirs.php?mod=info&amp;id=$mag[id]\">$mag[name]</a> ($mag[cena] монет за шт.)
<form action='shopelexirs.php?mod=sell&amp;id=$mag[id]' method='post'><input type=number value=0 name=kolvo SIZE=4><input type=submit value='>'></form>";
}
echo"<a href=\"shopelexirs.php?\">Назад</a>";
break;
case 'info':
$req = mysql_query("SELECT * FROM `shopelexirs` WHERE `city` = '$udata[city]' and `id`='$_GET[id]'");

$avto=mysql_num_rows($req);
if($avto==0){
echo'Ошибка!';
include($path.'files/down.php');
exit;
}
$mag = mysql_fetch_array($req);
switch($mag[what]){
case 'hp':
$tip='жизнь';
break;
case 'mp':
$tip='ману';
break;
}
echo"<b>$mag[name]</b><br/>
Востанавливает: $tip<br/>
Едениц: $mag[give]<br/>
Цена: $mag[cena]<br/>
";
echo"<a href=\"shopelexirs.php?\">Назад</a>";
break;

case 'sell':
if($_POST[kolvo] <= 0){
	echo'Покупаемое число зелий не должно быть меньше 1<br/>';
	echo'<a href=shopelexirs.php?mod=elexirs>Назад</a>'; include($path.'files/down.php'); exit;}
else{
$req = mysql_query("SELECT * FROM `shopelexirs` WHERE `city` = '$udata[city]' and `id`='$_GET[id]'");

$avto=mysql_num_rows($req);
if($avto==0){
echo'Ошибка, такой вещи нет!';
include($path.'files/down.php');
exit;
}
$mag = mysql_fetch_array($req);

$req=mysql_query("SELECT * FROM domination WHERE id = '1'");
$dom = mysql_fetch_assoc($req);
if($dom['white']>$dom['black']){
$liders='white';
}elseif($dom['black']>$dom['white']){
$liders='black';
}else{
$liders='not';
}
if($udata['storona']==$liders){
$mag[cena]=round($mag[cena]-(($mag[cena]/100)*15));
}

$nmoney=$udata[money]-($mag[cena]*$_POST[kolvo]);

if($nmoney<0){
echo'Не хватает монет!';
include($path.'files/down.php');
exit;
}
$req1 = mysql_query("SELECT * FROM `res` WHERE `usr` = '$log' and `lat_name` = '$mag[lat_name]'");
$res = mysql_fetch_array($req1);
$avto1=mysql_num_rows($req1);
if($avto1==0){
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` = '$_POST[kolvo]',
        `cena` = '$mag[cena]'");

$req = mysql_query("SELECT * FROM `zamok` WHERE `city` = '$udata[city]' LIMIT 1");
////////////////////////////
$city = mysql_fetch_array($req);
if($city[clan]!='not' and $udata[clan]!=$city[clan]){
$req = mysql_query("SELECT * FROM `clan` WHERE `lider`='$city[clan]'");
////////////////////////////
$clan = mysql_fetch_array($req);
$clan[money]=$clan[money]+round(($mag[cena]/100)*2)*$_POST[kolvo];

mysql_query("UPDATE `clan` SET `money` = '$clan[money]' WHERE `lider` = '$city[clan]'");
}

}else{
$nk=$res[kol]+$_POST[kolvo];
mysql_query("UPDATE `res` SET `kol` = '$nk' WHERE `usr` = '$log' and `lat_name` = '$mag[lat_name]'");
}
mysql_query("UPDATE users SET money = '$nmoney' WHERE usr = '$log'");

echo"
Вещь $mag[name] в кол-ве $_POST[kolvo] куплена и помещена в инвентарь!<br/>
Монет потрачено: ".$mag[cena]*$_POST[kolvo]."<br/>
Монет осталось: $nmoney<br/>
";
echo"<a href=\"shopelexirs.php?\">Назад</a>";
                                     }
break;
}
include($path.'files/down.php');
?>