<?php
require_once('system/func.php');
auth(); // Закроем от не авторизованных
$title = 'Клановые монстры';
require_once('system/header.php');
$clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id_clan` > '0' and `id` = '$user[id]' "));
$clan_monsters_fight_clan = mysql_fetch_array(mysql_query("SELECT * FROM `clan_monsters_fight` WHERE `id_clan` = '$user[id_clan]' "));
$monsters = mysql_fetch_assoc(mysql_query("SELECT * FROM `clan_monsters` WHERE `id` = '$clan_monsters_fight_clan[id_monster] '"));
$monsters_reward = mysql_fetch_assoc(mysql_query("SELECT * FROM `clan_monsters` WHERE `id` = ' $clan_monsters_fight_clan[id_monsters]'"));
$clan_key = mysql_fetch_array(mysql_query("SELECT * FROM `clans` WHERE `id` = '$user[id_clan]'  "));

















if (!$clan) {//Проверка состоит ли в клане  игрок
    header('location:/');
    exit;
}

if ($user['monsters_status'] == 'reward') {
    header('location:/monsters/');
    exit;
}

if (!$clan_monsters_fight_clan) {
    header('location:/monsters/');
    exit;
}


if ($clan_monsters_fight_clan['health_monsters'] <= 0) {
    header('location:/monsters/');
    exit;

}

if ($clan_monsters_fight_clan['time'] < time()) {
    mysql_query("UPDATE `users` SET `monsters_status`='fight' , `id_monster`='0' WHERE `id_clan`='$clan_monsters_fight_clan[id_clan]' AND `monsters_status` != 'reward'");
    mysql_query("DELETE FROM `monsters_log` WHERE `id_clan`='$user[id_clan]' ");
    mysql_query("DELETE FROM `clan_monsters_fight` WHERE `id_clan`='$user[id_clan]' ");
    header('location:/monsters/');
    exit;
}


#####################################Настройки############################################
$percent_hp_neeed_attack = 10; // Если меньше 10 %  жизни то юзер отдыхает
$health_monster_progress = round($clan_monsters_fight_clan['health_monsters'] / $monsters['health'] * 100, 1);//Линия жизни монстра
if ($health_monster_progress > 100) {
    $health_monster_progress = 100;
}//Фиксуем линию жизни


$dmgUser = ceil((($user['str'] / 2) - ($monsters['def'] / 6) + rand(1, 10)));
if ($dmgUser < 1) {
    $dmgUser = rand(1, 3);
}
$dmgEnemy = ceil((($monsters['str'] / 3) - ($user['def'] / 6) + rand(1, 10)));
if ($dmgEnemy < 1) {
    $dmgEnemy = rand(1, 3);
}


//$dmg_time = time() - $user['last_attack']; //выводим сколько до удара сек
//Если совершили удар , то время в last_attack
//if ($dmg_time < 2) {
    //$dmgUser = 1;
//}  //Промах
//if ($dmg_time == 2) {
    //$dmgUser = round(ceil($dmgUser / 4));
//}  // 25 % урона
//if ($dmg_time == 3) {
    //$dmgUser = round(ceil($dmgUser / 2));
//}  // 50 % урона


$name_sql = 'monsters_key_' . ($clan_monsters_fight_clan['id_monster'] + 1) . ''; // Это название монстров в клане


#####################################Конец############################################


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


    case 'attack': //Начать бой

        if ($user['monsters_status'] == 'reward') {
            header('location:/monsters/');
            exit;
        }

        if ($user['health'] < (($user['max_health'] / 100) * $percent_hp_neeed_attack)) {

            header('Location: /monster/fight/?SESSION=' . $user['password'] . '');
            exit();

        }
        mysql_query("UPDATE `users` SET `last_attack` = '" . time() . "', `id_monster`='$clan_monsters_fight_clan[id_monster]' WHERE `id`='" . $user['id'] . "' ");


        if ($dmgUser < 2) {
            $logUser = '' . $user['login'] . ' промахнулся <br>';
            mysql_query("INSERT  INTO `monsters_log` SET  `text` = '" . $logUser . "' ,   `id_clan` = '" . $user['id_clan'] . "'  ");
        } else {
            $logUser = color('lime') . '' . $user['login'] . ' нанес  ' . ico('icons', 'str.png') . ' ' . $monsters['name'] . '  ' . $dmgUser . '<br></font> ';

            $logEnemy = color('tomato') . '' . $monsters['name'] . ' нанес ' . ico('icons', 'str.png') . ' ' . $user['login'] . '    ' . $dmgEnemy . '        </font><br>';

            mysql_query("UPDATE `clan_monsters_fight` SET `health_monsters`  =   '" . ($clan_monsters_fight_clan['health_monsters'] - $dmgUser) . "' WHERE `id_clan` = '" . $user['id_clan'] . "' ");
            mysql_query("UPDATE `users` SET `health`  =   '" . ($user['health'] - $dmgEnemy) . "' WHERE `id` = '" . $user['id'] . "'");
            mysql_query("INSERT  INTO `monsters_log` SET  `text` = '" . $logUser . "' ,   `id_clan` = '" . $user['id_clan'] . "'  ");
            mysql_query("INSERT  INTO `monsters_log` SET  `text` = '" . $logEnemy . "' ,   `id_clan` = '" . $user['id_clan'] . "'  ");

        }



        if ($dmgUser >= $clan_monsters_fight_clan['health_monsters']) {
            mysql_query("UPDATE `clans` SET `almaz`  =   '" . ($clan_key['almaz'] + $monsters['reward_gold_clan']) . "' , `exp`  =   '" . ($clan_key['exp'] + $monsters['reward_exp_clan']) . "' WHERE `id` = '" . $clan_key['id'] . "' LIMIT 1 ");
            mysql_query("UPDATE `clan_monsters_fight` SET `status`  =  'kill' WHERE `id_clan` = '" . $user['id_clan'] . "' ");
            mysql_query("UPDATE `clans`  SET  `$name_sql`='" . ($clan_key[$name_sql] + 1) . "'  WHERE `id`='$clan_key[id]' LIMIT 1 ");
            mysql_query("UPDATE `users` SET `monsters_status` = 'reward' WHERE `id_clan` = '" . $user['id_clan'] . "' AND `monsters_status` = 'fight'");

            header('Location: /monsters/?SESSION=' . $user['password'] . '');
            exit();
        }

        header('Location: /monster/fight/?SESSION=' . $user['password'] . '');
        exit();
        break;


}


echo div('wrapper center') . tl($clan_monsters_fight_clan['time'] - time()) . "</div>";




echo div('header') . " <img src='" . $monsters['img'] . "'> " . $monsters['name'] . " " . ico('icons', 'level.png') . "" . $monsters['level'] . "ур.<br>  " . ico('icons', 'health.png') . " " . $clan_monsters_fight_clan ['health_monsters'] . " " . ico('icons', 'str.png') . " " . $monsters['str'] . "" . ico('icons', 'def.png') . "" . $monsters['def'] . "  ";
echo '<div class="life_bar"><div class="life_bar-red" style="width:' . $health_monster_progress . '% "></div></div></div>';
if ($user['health'] > (($user['max_health'] / 100) * $percent_hp_neeed_attack)) {
    echo div('header') . "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/monster/fight/attack/'> Ударить </span></span></div> </a></div>";
} else {
    echo div('header') . "Для атаки нужно минимум 10% " . ico('icons', 'health.png') . " жизни. </div>";
    echo div('header') . "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/rinok?koldun&url=/monster_fight.php'> Восстановить </span></span></div> </a></div>";
    echo div('header') . "Стоимость 200 ".ico('icons','gold.png')." </div>";
    //echo div('header') . "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?'> Обновить </span></span></div> </a></div>";
    
    
    
   
    




    
    
    

  
}



$monsters = mysql_query("SELECT * FROM `monsters_log` WHERE `id_clan` = '$user[id_clan]' ORDER BY `id` DESC LIMIT 25 ");

//echo div('text_p1');


while ($log_m = mysql_fetch_assoc($monsters)) {
    echo div('text_p1');
    echo $log_m['text'] . " ";
echo " </div>";

}
//echo " </div>";
require_once('system/footer.php');
