<?php
require_once ('system/func.php');
auth(); // Закроем от не авторизованных
$clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '".$user['id_clan']."'"));
$clan_bonus = mysql_fetch_assoc(mysql_query("SELECT * FROM `clan_bonus` WHERE `id_clan` = '".$clan['id']."'"));
    function cost($i) {

        switch($i) {
          case 0:
           $cost = 100000;
           break;

          case 1:
           $cost = 200000;
           break;

          case 2:
           $cost = 300000;
           break;

          case 3:
           $cost = 1000;
           break;

          case 4:
           $cost = 800000;
           break;

          case 5:
           $cost = 900000;
           break;

          case 6:
           $cost = 1000000;
           break;

          case 7:
           $cost = 6000;
           break;

          case 8:
           $cost = 1100000;
           break;

          case 9:
           $cost = 1200000;
           break;

          case 10:
           $cost = 1300000;
           break;

          case 11:
           $cost = 8000;
           break;

          case 12:
           $cost = 1400000;
           break;

          case 13:
           $cost = 15000000;
           break;

          case 14:
           $cost = 16000000;
           break;

          case 15:
           $cost = 10000;
           break;

          case 16:
           $cost = 17000000;
           break;

          case 17:
           $cost = 18000000;
           break;

          case 18:
           $cost = 19000000;
           break;

          case 19:
           $cost = 15000;
           break;

          case 20:
           $cost = 20000000;
           break;

          case 21:
           $cost = 22000000;
           break;

          case 22:
           $cost = 23000000;
           break;

          case 23:
           $cost = 25000;
           break;

          case 24:
           $cost = 28000000;
           break;

          case 25:
           $cost = 50000000;
           break;

          case 26:
           $cost = 55000000;
           break;

          case 27:
           $cost = 50000;
           break;

          case 28:
           $cost = 85000000;
           break;

          case 29:
           $cost = 90000000;
           break;

          case 30:
           $cost = 100000000;
           break;

          case 31:
           $cost = 52500;
           break;

          case 32:
           $cost = 130000000;
           break;

          case 33:
           $cost = 140000000;
           break;

          case 34:
           $cost = 150000000;
           break;

          case 35:
           $cost = 200000000;
           break;

          case 36:
           $cost = 100000;
           break;

        }
    return $cost/4;

    }

    function value($i) {

        switch($i) {
          case 0:
           $value = 'gold';
           break;

          case 1:
           $value = 'gold';
           break;

          case 2:
           $value = 'gold';
           break;

          case 3:
           $value = 'almaz';
           break;

          case 4:
           $value = 'gold';
           break;

          case 5:
           $value = 'gold';
           break;

          case 6:
           $value = 'gold';
           break;

          case 7:
           $value = 'almaz';
           break;

          case 8:
           $value = 'gold';
           break;

          case 9:
           $value = 'gold';
           break;

          case 10:
           $value = 'gold';
           break;

          case 11:
           $value = 'almaz';
           break;

          case 12:
           $value = 'gold';
           break;

          case 13:
           $value = 'gold';
           break;

          case 14:
           $value = 'gold';
           break;

          case 15:
           $value = 'almaz';
           break;

          case 16:
           $value = 'gold';
           break;

          case 17:
           $value = 'gold';
           break;

          case 18:
           $value = 'gold';
           break;

          case 19:
           $value = 'almaz';
           break;

          case 20:
           $value = 'gold';
           break;

          case 21:
           $value = 'gold';
           break;

          case 22:
           $value = 'gold';
           break;

          case 23:
           $value = 'almaz';
           break;

          case 24:
           $value = 'gold';
           break;

          case 25:
           $value = 'gold';
           break;

          case 26:
           $value = 'gold';
           break;

          case 27:
           $value = 'almaz';
           break;

          case 28:
           $value = 'gold';
           break;

          case 29:
           $value = 'gold';
           break;

          case 30:
           $value = 'gold';
           break;

          case 31:
           $value = 'almaz';
           break;

          case 32:
           $value = 'gold';
           break;

          case 33:
           $value = 'gold';
           break;

          case 34:
           $value = 'gold';
           break;

          case 35:
           $value = 'gold';
           break;

          case 36:
           $value = 'almaz';
           break;


        }

    return $value;

    }

function price($i = 0)
{

    $valuta = $i % 2 == 0?'gold':'almaz';

    switch($i) {

        case 0:
           $value = 100;
        break;

        case 1:
            $value = 100;
        break;

        default:
            $value = 100 + $i * 100;
// s            $value = 99999999999999;
        break;
    }

    return array($valuta, $value);
}

if (isset($_GET['str'])) {

$price = price($clan['str_lvl']);

if ($price[0] == 'gold' && $clan['gold'] < $price[1]) {
    $_SESSION['msg'] = "Не хватает ".ico('icons','gold.png')." ".($price[1] - $clan['gold'])." золота";
    header("Location: ?$random");
    exit();
} elseif ($price[0] == 'almaz' && $clan['almaz'] < $price[1]) {
    $_SESSION['msg'] = "Не хватает ".ico('icons','almaz.png')." ".($price[1] - $clan['almaz'])." алмазов";
    header("Location: ?$random");
    exit();
}

mysql_query("UPDATE `clans` SET `str_lvl` = `str_lvl` + 1, `str_plus` = `str_plus` + 10, `".$price[0]."` = `".$price[0]."` - '".$price[1]."' WHERE `id` = '".$clan['id']."' LIMIT 1");
mysql_query("UPDATE `users` SET `str` = `str` + 10 WHERE `id_clan` = '".$clan['id']."'");
mysql_query("INSERT INTO `clan_journal` SET `clan` = '$clan[id]', `time` = '".time()."', `text` = 'Лидер улучшил башню клана'");
$_SESSION['msg'] = "Увеличено";
header("Location: ?$random");
exit;
}

if (isset($_GET['def'])) {
$price = price($clan['def_lvl']);

if ($price[0] == 'gold' && $clan['gold'] < $price[1]) {
    $_SESSION['msg'] = "Не хватает ".ico('icons','gold.png')." ".($price[1] - $clan['gold'])." золота";
    header("Location: ?$random");
    exit();
} elseif ($price[0] == 'almaz' && $clan['almaz'] < $price[1]) {
    $_SESSION['msg'] = "Не хватает ".ico('icons','almaz.png')." ".($price[1] - $clan['almaz'])." алмазов";
    header("Location: ?$random");
    exit();
}

mysql_query("UPDATE `clans` SET `def_lvl` = `def_lvl` + 1, `def_plus` = `def_plus` + 10, `".$price[0]."` = `".$price[0]."` - '".$price[1]."' WHERE `id` = '".$clan['id']."' LIMIT 1");
mysql_query("UPDATE `users` SET `def` = `def` + 10 WHERE `id_clan` = '".$clan['id']."'");
mysql_query("INSERT INTO `clan_journal` SET `clan` = '$clan[id]', `time` = '".time()."', `text` = 'Лидер улучшил башню клана'");
$_SESSION['msg'] = "Увеличено";
header("Location: ?$random");
exit;
}

if (isset($_GET['health'])) {
$price = price($clan['max_health_lvl']);

if ($price[0] == 'gold' && $clan['gold'] < $price[1]) {
    $_SESSION['msg'] = "Не хватает ".ico('icons','gold.png')." ".($price[1] - $clan['gold'])." золота";
    header("Location: ?$random");
    exit();
} elseif ($price[0] == 'almaz' && $clan['almaz'] < $price[1]) {
    $_SESSION['msg'] = "Не хватает ".ico('icons','almaz.png')." ".($price[1] - $clan['almaz'])." алмазов";
    header("Location: ?$random");
    exit();
}

mysql_query("UPDATE `clans` SET `max_health_lvl` = `max_health_lvl` + 1, `max_health_plus` = `max_health_plus` + 10, `".$price[0]."` = `".$price[0]."` - '".$price[1]."' WHERE `id` = '".$clan['id']."' LIMIT 1");
mysql_query("UPDATE `users` SET `max_health` = `max_health` + 10 WHERE `id_clan` = '".$clan['id']."'");
mysql_query("INSERT INTO `clan_journal` SET `clan` = '$clan[id]', `time` = '".time()."', `text` = 'Лидер улучшил башню клана'");
$_SESSION['msg'] = "Увеличено";
header("Location: ?$random");
exit;
}

/*


if(isset($_GET['train']) and $user['clan_rang'] == 5 and $clan['stat_level'] < $clan['level']){
if(value($clan['stat_level']) == 'gold') {
if($clan['gold'] < cost($clan['stat_level'])) {
$_SESSION['msg'] = "Не хватает ".ico('icons','gold.png')." ".(cost($clan['stat_level'])-$clan['gold'])." золота";
header("Location: ?$random");
exit();
}else{
mysql_query("UPDATE `clans` SET `stat` = '".($clan['stat']+5)."', `stat_level` = '".($clan['stat_level']+1)."', `gold` = '".($clan['gold']-cost($clan['stat_level']))."' WHERE `id` = '".$clan['id']."'");
mysql_query("UPDATE `users` SET `str` = `str` + '5', `def` = `def` + '5', `max_health` = `max_health` + '5' WHERE `id_clan` = '".$clan['id']."'");
mysql_query("INSERT INTO `clan_journal` SET `clan` = '$clan[id]', `time` = '".time()."', `text` = 'Лидер улучшил статую клана'");
$_SESSION['msg'] = "Уровень алтаря увеличен";
header("Location: ?$random");
exit();
}
}elseif(value($clan['stat_level']) == 'almaz') {
if($clan['almaz'] < cost($clan['stat_level'])) {
$_SESSION['msg'] = "Не хватает ".ico('icons','almaz.png')." ".(cost($clan['stat_level'])-$clan['almaz'])." алмазов";
header("Location: ?$random");
exit();
}else{
mysql_query("UPDATE `clans` SET `stat` = '".($clan['stat']+10)."', `stat_level` = '".($clan['stat_level']+1)."', `almaz` = '".($clan['almaz']-cost($clan['stat_level']))."' WHERE `id` = '".$clan['id']."'");
mysql_query("UPDATE `users` SET `str` = `str` + '10', `def` = `def` + '10', `max_health` = `max_health` + '10' WHERE `id_clan` = '".$clan['id']."'");
mysql_query("INSERT INTO `clan_journal` SET `id_clan` = '$clan[id]', `time` = '".time()."', `text` = 'Лидер улучшил алтарь клана   '");
$_SESSION['msg'] = "Уровень алтаря увеличен";
header("Location: ?$random");
exit();
}
}
}
/*
if(isset($_GET['bonus']) and $user['clan_rang'] == 5 and $clan_bonus == NULL){
if($clan['almaz'] < 250){
$_SESSION['msg'] = "В казне не хватает ".ico('icons','almaz.png')." ".(500-$clan['almaz'])." алмазов";
header('Location: ?');
exit();
}
$stat = ceil($clan['stat']*50/100);
mysql_query("INSERT INTO `clan_bonus` SET `id_clan` = '$clan[id]', `stat` = '$stat', `last` = '".(time()+60*60*72)."'");
mysql_query("UPDATE `users` SET `str` = `str` + '$stat', `def` = `def` + '$stat', `max_health` = `max_health` + '$stat' WHERE `id_clan` = '".$clan['id']."'");
mysql_query("UPDATE `clans` SET `stat` = '".($clan['stat']+$stat)."', `almaz` = '".($clan['almaz']-250)."' WHERE `id` = '".$clan['id']."'");
mysql_query("INSERT INTO `clan_journal` SET `clan` = '$clan[id]', `time` = '".time()."', `text` = 'Лидер активировал бонус клана'");
header('Location: ?');
exit();
}
*/
$title = 'Постройки клана';
require_once ('system/header.php');
echo "<div class='header'>";

$round= round($clan['stat_level'] /  $clan['level'] * 100,1);
if($round > 100)$round = 100;


$p="<center></font> </center><div class='reward_progress'><div class='yellow' style='width:".$round."%;'></div></div>";



//echo "<div class='oh'><img src='/images/clan/tower_agi.png' style='margin-right:3px;margin-top:-2px;' width='75%'></div>";
echo " <center><span class='line'>Постройки клана</span><br><br></center>";
echo "<div class='text_p1'>";
echo "<center><img src='/images/zdan1.png'> </a></center>";

echo "<center>".ico('icons','str.png')." +$clan[str_plus] к силе<br></center></div>";


if ($user['clan_rang'] == 5) {
echo "<div class='header'>";
//echo "<span class='lime'><b>Улучшение</b></span><br>";
// if($user['clan_rang'] == 5)echo "<div class='separ'></div><a href='?bonus' class='seach'>Активировать бонус</a><br>Стоимость: ".ico('icons','almaz.png')." 250 алмазов";

echo "<div class='btn_grey'><a href='?str'>".ico('icons','str.png')." Повысить силу за ".ico('icons', price($clan['str_lvl'])[0].'.png')." ".price($clan['str_lvl'])[1]."</a><br>";
echo "</div></div>";

}








echo "<div class='text_p1'>";
echo "<center><img src='/images/zdan2.png'> </a></center>";

echo "<center>".ico('icons','def.png')." +$clan[def_plus] к защите<br></center></div>";

if ($user['clan_rang'] == 5) {
echo "<div class='header'>";
//echo "<span class='lime'><b>Улучшение</b></span><br>";
// if($user['clan_rang'] == 5)echo "<div class='separ'></div><a href='?bonus' class='seach'>Активировать бонус</a><br>Стоимость: ".ico('icons','almaz.png')." 250 алмазов";


echo "<div class='header'>";

echo "<div class='btn_grey'><a href='?def'>".ico('icons','def.png')." Повысить защиту за ".ico('icons', price($clan['def_lvl'])[0].'.png')." ".price($clan['def_lvl'])[1]."</a><br>";
echo "</div></div>";

}



echo "<div class='text_p1'>";
echo "<center><img src='/images/zdan3.png'> </a></center>";

echo "<center>".ico('icons','health.png')." +$clan[max_health_plus] к жизни<br></center></div>";


if ($user['clan_rang'] == 5) {

echo "<div class='header'>";


echo "<div class='btn_grey'><a href='?health'>".ico('icons','health.png')." Повысить жизнь за ".ico('icons', price($clan['max_health_lvl'])[0].'.png')." ".price($clan['max_health_lvl'])[1]."</a><br>";



echo "</div></div>";
}





echo "<div style='clear:both;'></div>";
echo "<div class='stat_bar2'><div class='progress blue' style='width:".round($clan['stat_level']/$clan['level']*100,1)."%;'></div></div>";
//if($clan['stat_level'] < $clan['level'] and $user['clan_rang'] == 5)echo "<center><a href='?train' class='btn-grey'>Улучшить за ".(value($clan['stat_level']) == 'almaz' ? ico('icons','almaz.png') : ico('icons','gold.png'))." ".cost($clan['stat_level'])."</a>";
echo "</div>";
/*if ($user['clan_rang'] == 5) {
echo "<div class='header'>";
//echo "<span class='lime'><b>Улучшение</b></span><br>";
// if($user['clan_rang'] == 5)echo "<div class='separ'></div><a href='?bonus' class='seach'>Активировать бонус</a><br>Стоимость: ".ico('icons','almaz.png')." 250 алмазов";

echo "<div class='btn_grey'><a href='?str'>".ico('icons','str.png')." Повысить силу за ".ico('icons', price($clan['str_lvl'])[0].'.png')." ".price($clan['str_lvl'])[1]."</a><br>";
echo "</div></div>";
echo "<div class='header'>";

echo "<div class='btn_grey'><a href='?def'>".ico('icons','def.png')." Повысить защиту за ".ico('icons', price($clan['def_lvl'])[0].'.png')." ".price($clan['def_lvl'])[1]."</a><br>";
echo "</div></div>";
echo "<div class='header'>";


echo "<div class='btn_grey'><a href='?health'>".ico('icons','health.png')." Повысить жизнь за ".ico('icons', price($clan['max_health_lvl'])[0].'.png')." ".price($clan['max_health_lvl'])[1]."</a><br>";



echo "</div></div>";
}
*/
$clan = 0;
require_once ('system/footer.php');
?>
