<?php
require_once('system/func.php');
auth(); // Закроем от не авторизованных
$title = 'Клановые монстры';
require_once('system/header.php');
$id = intval($_GET['id']);


if ($user['id_clan'] == 0) {
    echo '<div class="bbtn mb5"><span class="btn_start"><span class="btn_end"> Для участия в боях с монстрами нужно состоять в клане. </div>';
    require_once('system/footer.php');
}


$monsters = mysql_fetch_assoc(mysql_query("SELECT * FROM `clan_monsters` WHERE `id` = '$id'"));
$clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id_clan` > '0' and `id` = '$user[id]' "));
$clan_key = mysql_fetch_array(mysql_query("SELECT * FROM `clans` WHERE `id` = '$user[id_clan]'  "));
$clan_monsters_fight_clan = mysql_fetch_array(mysql_query("SELECT * FROM `clan_monsters_fight` WHERE `id_clan` = '$user[id_clan]' "));
###########################################Настройки локации########################################
$name_sql = 'monsters_key_' . $monsters['id'] . ''; // Это название монстров в клане
$time_need_kill_monster = 60; //Сколько минут что бы убить монстра 
$div = "</div>";//Закрыт div class


$monsters_reward = mysql_fetch_assoc(mysql_query("SELECT * FROM `clan_monsters` WHERE `id` = ' $user[id_monster]'"));


if ($user['monsters_status'] == 'reward' ) {
    if (isset($_GET['take'])) {
        //mysql_query("DELETE  FROM `monsters_log`  WHERE `id_clan`='$clan_key[id]' ");
        //mysql_query("DELETE  FROM `clan_monsters_fight` WHERE `id_clan`='$clan_key[id]' ");

        mysql_query("UPDATE `users` SET `almaz`='" . ($user['almaz'] + $monsters_reward['reward_gold_user']) . "'  ,  `exp`='" . ($user['exp'] + $monsters_reward['reward_exp_user']) . "' ,  `monsters_status`='fight' , `id_monster`='0' WHERE `id`='$user[id]' ");

        $_SESSION['msg'] = div('header') . " Награда " . ico('icons', 'almaz.png') . " $monsters_reward[reward_gold_user] и " . ico('icons', 'exp.png') . " $monsters_reward[reward_exp_user] </div>";
        header('location: /monsters/');
        exit();

    }

    echo div('wrapper center') . " $monsters_reward[name] повержен </div>";
    echo div('header') . " Награда " . ico('icons', 'almaz.png') . " $monsters_reward[reward_gold_user] и " . ico('icons', 'exp.png') . " $monsters_reward[reward_exp_user] <br> Ключ +1</div>";

    echo div('header') . "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/monsters/reward/'> Забрать награду </span></span></div> </a></div>";
    $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '21'"));
    $q = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest_user` WHERE `id_quest` = '$quest[id]' AND `id_user` = '$myID'"));
    if ($q['last'] < time()) {
        mysql_query("update `quest_user` set `koll` = `koll`+'1' where (`id` = '" . $q['id'] . "')");
    }
    require_once('system/footer.php');
} else {

    if ($clan_monsters_fight_clan['health_monsters'] > 0) {
        header('location:/monster/fight/');
        exit;


    }

    switch (isset($_GET['act'])) {

        case 'start': //Начать бой
            if ($clan['clan_rang'] < 4) { // Проверяем если игрок не может начать бой
                $_SESSION['msg'] = div('header') . "Начать бой могут генералы и лидер клана</div>";
                header('location:/monsters/');
                exit;
            }
            //Конец проверки

            if ($clan_key['level'] < $monsters['level']) {//Проверка доступен монстр по уровню
                $_SESSION['msg'] = div('header') . "Уровень монстра больше чем уровень клана</div>";
                header('location:/monsters/');
                exit;
            }

            //Конец проверки
            if ($monsters['id'] > 1) {
                if ($clan_key[$name_sql] < 1) {//Проверить хватает ли ключов ,но делаем для 1 монстра исключение
                    $_SESSION['msg'] = div('header') . "Не хватает ключей</div>";
                    header('location:/monsters/');
                    exit;
                }//Конец проверки
            }

            if ($_GET['act'] == 'start') {
                mysql_query("DELETE  FROM `monsters_log`  WHERE `id_clan`='$clan_key[id]' ");
                mysql_query("DELETE  FROM `clan_monsters_fight` WHERE `id_clan`='$clan_key[id]' ");

                mysql_query("INSERT INTO `clan_monsters_fight` SET `id_monster` = '$monsters[id]', `id_clan` = '$clan_key[id]'  , `health_monsters`  = '$monsters[health]' ,  `time`  = '" . (time() + 60 * $time_need_kill_monster) . "' ");
                mysql_query("UPDATE `clans`  SET  `$name_sql`='" . ($clan_key[$name_sql] - 1) . "'  WHERE `id`='$clan_key[id]' ");
                mysql_query("UPDATE `users` SET `monsters_status`='fight' , `id_monster`='$monsters[id]' WHERE `id`='$user[id]' ");


                header('location:/monster/fight/');
                exit;
            }
            break;
    }

    $mosters = mysql_query("SELECT * FROM `clan_monsters`");

    while ($m = mysql_fetch_assoc($mosters)) {
    //echo div('text_p1');

        echo div('text_p1') . " <left><img src='" . $m['img'] . "'></left><cener> " . $m['name'] . " " . ico('icons', 'level.png') . "" . $m['level'] . "ур.</center><center>  " . ico('icons', 'health.png') . " " . $m[health] . " " . ico('icons', 'str.png') . " " . $m[str] . "" . ico('icons', 'def.png') . "" . $m[def] . " </center> ";
        $name_sql = 'monsters_key_' . $m['id'] . '';
//echo "</div>";

        if ($m['id'] > 1) {
            echo  " <center>Ключей: $clan_key[$name_sql] шт.</center></div>";
        }
echo "</div>";
        echo $c;
        echo  "<center><div class='header'><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/monsters/start/$m[id]/'> Начать сражение </center></span></span></a></div></div>";

    }
}


require_once('system/footer.php');
