<?
define('PROTECTOR', 1);

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

$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($udata[klas]!='wizard'){echo"Вход только для магов!";include($path.'files/down.php');exit;}


switch($_GET[mod]){

default:
echo"<a href=\"akademmag.php?mod=new\">Выучить умение</a><br/>";
echo"<a href=\"akademmag.php?mod=up\">Повысить умение</a>";
break;

case 'new':
$req = mysql_query("SELECT * FROM `shop_mag` WHERE `city` = '$udata[city]' and `klas`='wizard'");
////////////////////////////
$avto=mysql_num_rows($req);
if($avto>=1){
While($mag = mysql_fetch_array($req))
{
echo"<a href=\"akademmag.php?mod=info&amp;id=$mag[id]\">$mag[name]</a><br/>";
}
}else{
echo"Нет умений!<br/>";
}
echo"<a href=\"akademmag.php?\">Назад</a>";
break;
case 'info':
$req = mysql_query("SELECT * FROM `shop_mag` WHERE `city` = '$udata[city]' and `id`='$_GET[id]' and `klas`='wizard'");

$avto=mysql_num_rows($req);
if($avto==0){
echo'Ошибка!';
include($path.'files/down.php');
exit;
}
$mag = mysql_fetch_array($req);


$mag_mp=$mag[mp];
$magmp=explode("|",$mag[mp]);
$mag[mp]=$magmp[0];//мп

$mag_hp=$mag[hp];
$maghp=explode("|",$mag[hp]);
$mag[hp]=$maghp[0];//хп

$mag_plushp=$mag[plushp];
$maghp=explode("|",$mag[plushp]);
$mag[plushp]=$maghp[0];//+хп

$mag_uron=$mag[uron];
$maghp=explode("|",$mag[uron]);
$mag[uron]=$maghp[0];//+урон

$mag_cena=$mag[cena];
$magcena=explode("|",$mag[cena]);
$mag[cena]=$magcena[0];//+цена


if($_GET[act]!='by'){

echo'<div class="event">';
echo"<b>$mag[name]:</b><br/>";
echo"Мин. уровень: $mag[lvl]<br/>";
echo"Цена: <img src=pic/main/gold1.png>$mag[cena]<br/>";
echo"Урон: $mag[uron]<br/>";
echo"Забирает маны: $mag[mp]<br/>";
echo"Забирает здоровья: $mag[hp]<br/>";
echo"Даёт здоровья: $mag[plushp]<br/>";
echo"Ресурсы: <br/>";
$res=explode("|",$mag[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 name FROM `mdrop` WHERE `lat_name`='$lat_name' LIMIT 1");
$reso = mysql_fetch_array($req);
echo"$reso[name] - $kol штук<br/>";
$i++;
$b++;
}
echo"</div>";
echo"<a href=\"akademmag.php?mod=info&amp;act=by&amp;id=$mag[id]\"><b>Изучить</b></a><br/>";
}else{

if($udata[lvl]<$mag[lvl]){
echo'Низкий уровень!';
include($path.'files/down.php');
exit;
}

if($udata[money]<$mag[cena]){
echo'Мало <img src=pic/main/gold1.png>!';
include($path.'files/down.php');
exit;
}

$res=explode("|",$mag[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++;
}

if($not==TRUE){
echo'Недостаточно ресурсов!';
include($path.'files/down.php');
exit;
}

if(empty($not)){
$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++;
}
}

$udata[money]=$udata[money]-$mag[cena];//mony

mysql_query("UPDATE `users` SET `money` = '$udata[money]' WHERE `usr` = '$log'");

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',   
        `mp` = '$mag_mp',   
        `hp` = '$mag_hp',   
        `plushp` = '$mag_plushp',   
        `cena` = '$mag_cena',        
        `lvl` = '1'");

echo"Умение $mag[name] изучено!<br/>";
}
echo"<a href=\"akademmag.php?\">Назад</a>";
break;

case 'up':
$req = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' and `klas`='wizard'");
////////////////////////////
$avto=mysql_num_rows($req);

if($avto>=1){
While($mag = mysql_fetch_array($req))
{
echo"<a href=\"akademmag.php?mod=upgo&amp;id=$mag[id]\">$mag[name]</a><br/>";
}
}else{
echo"Нет умений!<br/>";
}
echo"<a href=\"akademmag.php?\">Назад</a>";
break;
case 'upgo':
$req = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' and `klas`='wizard' and `id`='$_GET[id]'");

$avto=mysql_num_rows($req);
if($avto==0){
echo'Ошибка!';
include($path.'files/down.php');
exit;
}
$mag = mysql_fetch_array($req);

if($mag[lvl]>=9){
echo'У умения макс. уровень!';
include($path.'files/down.php');
exit;
}

$magmp=explode("|",$mag[mp]);
$mag[mp]=$magmp[$mag[lvl]];//мп

$maghp=explode("|",$mag[hp]);
$mag[hp]=$maghp[$mag[lvl]];//хп

$maghp=explode("|",$mag[plushp]);
$mag[plushp]=$maghp[$mag[lvl]];//+хп

$maghp=explode("|",$mag[uron]);
$mag[uron]=$maghp[$mag[lvl]];//+урон

$magcena=explode("|",$mag[cena]);
$mag[cena]=$magcena[$mag[lvl]];//+cena


if($_GET[act]!='by'){
$mag[lvl]++;
echo'<div class="event">';
echo"<b>$mag[name]:</b><br/>";
echo"След. уровень: $mag[lvl]<br/>";
echo"Цена: <img src=pic/main/gold1.png>$mag[cena]<br/>";
echo"Урон: $mag[uron]<br/>";
echo"Забирает маны: $mag[mp]<br/>";
echo"Забирает здоровья: $mag[hp]<br/>";
echo"Даёт здоровья: $mag[plushp]<br/>";
echo"</div>";
echo"<a href=\"akademmag.php?mod=upgo&amp;act=by&amp;id=$mag[id]\"><b>Повысить</b></a><br/>";
}else{

if($udata[money]<$mag[cena]){
echo'Мало <img src=pic/main/gold1.png>!';
include($path.'files/down.php');
exit;
}

$udata[money]=$udata[money]-$mag[cena];//mony

$mag[lvl]++;

mysql_query("UPDATE `users` SET `money` = '$udata[money]' WHERE `usr` = '$log'");

mysql_query("UPDATE `mag` SET `lvl` = '$mag[lvl]' WHERE `usr`='$log' and `lat_name`='$mag[lat_name]' LIMIT 1");

echo"Умение $mag[name] повышено!<br/>";
}
echo"<a href=\"akademmag.php?\">Назад</a>";
break;
}
include($path.'files/down.php');
?>