<?php

/**
 * Горбунов Александр
 * https://vk.com/sasha_gorbunov
 * Распространение и перепродажа запрещена
 */

require_once('./system/func.php');
$title = 'Выживание';
require_once('./system/header.php');
auth();
require_once('./class/team_war.php');
echo "<div class=\"ribbon mb2\"><div class=\"rl\"><div class=\"rr\">$title</div></div></div>";
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);


if ($user['level'] >= 10) {
    $battle = new team_war();
    $timer = $battle->getTime(time());
    $countPlayers = $battle->getCountPlayers();
    $countPlayersUserLevel = $battle->getCountPlayerLevel();
    $queue = $battle->getCheckQueue();
    $fight = $battle->getCheckFight();
    $start = $battle->startFight();

    if ($start && $queue) {
        header("Location: /team_war.php?id=battle");
        exit();
    }

    if ($fight && $_GET['id'] != 'battle') {
        header("Location: /team_war.php?id=battle");
        exit();
    }

    if (isset($_GET['enter'])) {
        $battle->setCheckQueue();
        header("Location: /team_war.php");
        exit();
    }


    if (isset($_GET['id']) && $_GET['id'] === 'battle' && $queue) {
        if ($battle->finalFight() == true && $_GET['id'] != 'result') {
            $battle->setFinalResult();
            header("Location: /team_war.php?id=result");
            exit();
        }

        if ($battle->getDead() == true) {
            echo "<div class='block' align='center'>
                <a class='btn_grey' href='/team_war.php?id=battle'><span class='end'><span class='label'>Обновить</a>
                <br/><font color='#999'>Вы были убиты во время сражения, ожидайте окончания боя</font></div>";
        } else {
            if (isset($_GET['target']) && $_GET['target'] === 'true') {
                $battle->targetRefresh();
                header('location: /team_war.php?id=battle');
                exit();
            } else if (isset($_GET['attack']) && $_GET['attack'] === 'true') {
                $battle->attack();
                header('Location: /team_war.php?id=battle');
                exit();
            } else if (isset($_GET['rock']) && $user['kamen'] >= 1 && $battle->kamen < time()) {
                $battle->getUserRock($user['id']);
                header('Location: /team_war.php?id=battle');
                exit();
            } else if (isset($_GET['grass']) && $user['trava'] >= 1 && $battle->trava < time()) {
                $battle->getUserGrass($user['id']);
                header('Location: /team_war.php?id=battle');
                exit();
            }

            $_opponent = $battle->getOpp($battle->target);
            $_oppHealth = $battle->getHealthOpp($battle->target);
            echo "<div class='wrapper center'>";
            echo $_opponent['login'] . " " . ico('icons', 'health.png') . " " . $_oppHealth . "<br>";
            //echo "<div class='stat_bar'><div class='progress' style='width: " . round($_oppHealth / $_opponent['max_health'] * 100, 1) . "%;'></div></div>";
            

                        echo "<div class='stat_bar'><div class='progress' style='width: " . round($_oppHealth , $_opponent['max_health'] * 100, 1) . "%;'></div></div>";


            echo "<a href='/team_war.php?id=battle&attack=true' class='btn_grey'>Атаковать</a></br></br>" . "<a href='/team_war.php?id=battle&target=true' class='btn_grey'>Сменить цель</a><hr>";
            echo "<table cellpadding='5' cellspacing='0' align='center'><tr>";
            //echo "<td class='center' style='border-right: 1px solid #8D673A;'>";
           
            echo "</td>";
            echo "</tr></table>";
            echo "</div>";
            # Лог
            echo "<div class='wrapper'>";
            $logs = $battle->getLogBattle();
            if ($logs == false) {
                echo "<div class='center white'>Логов не найдено</div>";
            } else {
                foreach ($logs as $log) {
                    $dmg_user = mysql_fetch_array(mysql_query('SELECT `login`, `sex` FROM `users` WHERE `id` = "' . $log['id_user'] . '"'));
                    $opp_user = mysql_fetch_array(mysql_query('SELECT `login` FROM `users` WHERE `id` = "' . $log['id_opp'] . '"'));
                    if (isset($log['dead'])) {
                        echo " <font color='white'>" . $dmg_user['login'] . " убил" . ($user['sex'] == 'w' ? 'а' : '') .  " <font color='red'>" . $opp_user['login'] . "</font><br>";
                    } else {
                        echo " <font color='white'>" . $dmg_user['login'] . " ударил" . ($user['sex'] == 'w' ? 'а' : '') .  " <font color='red'>" . $opp_user['login'] . " на <b>" . $log['damage'] . "</b></font><br>";
                    }
                }
            }
            echo '</div>';
        }
    } else if (isset($_GET['id']) && $_GET['id'] === 'result') {

        if ($battle->getResultCheck() == true) {
            $battle->getResult();
            echo "<div class='block' align='center'>
                <a class='btn2' href='/team_war.php'><span class='end'><span class='label'>Назад</a>
            </div>";
        } else {
            echo "<div class='block' align='center'>
                <a class='btn2' href='/team_war.php'><span class='end'><span class='label'>Назад</a>
                <br/><font color='#999'>Результаты устарели и были очищены</font></div>";
        }
    } else {
        echo "<div class=\"header center\">В очереди игроков: " . $countPlayers . " <br />" . "В очереди игроков вашего уровня: " . $countPlayersUserLevel . " <br />" . "<a href='team_war.php?enter' class='btn_grey'>" . ($queue ? 'Отменить участие' : 'Принять участие') . "</a></div>";

        echo "<div class=\"header center\">Битва начнется через: " . tl($timer - time()) . "<br /><a href=\"\" class=\"btn_grey\">Обновить</a><br />";
        //echo "<a href=\"/team_war.php?id=result\" class=\"btn2\">Предыдущие Ваши результаты</a></div>";
    }
} else {
    echo "<div class='wrapper center'>Для участия в Командном бою требуется 10 уровень</div>";
}

require_once('system/footer.php');
/*
 * Горбунов Александр 
 * https://vk.com/sasha_gorbunov
 * Распространение и перепродажа запрещена
 */