<?php
//const GAME = true;
use Helper\Conversion;

if (!defined('GAME_VERSION')) {
    require_once '_incl_data/autoload.php';
}
//include('_incl_data/__config.php');
//include('_incl_data/class/__db_connect.php');

Core\Config::init();
Core\Database::init();
//$c = (new \Core\Config())->get();
$u = User::start();
?>

<!DOCTYPE HTML>
<html>
<head>

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <meta http-equiv=Cache-Control Content=no-cache>
    <meta http-equiv=PRAGMA content=NO-CACHE>
    <link href="//img.new-combats.tech/css/main.css" rel="stylesheet" type="text/css">
    <title>Монитор поединков</title>
</head>
<body style="padding-top:0px; margin-top:7px; height:100%; background-color:#dedede;">
<script type="text/javascript" src="js/jquery.js"></script>
<center><strong>Просматривает персонаж: </strong><?= User::getLogin($u->info['id']) ?></center>
<hr>
<INPUT style="float:right" class="btn btn-success" TYPE=button value="Обновить" style='width: 100px'
       onClick="location.href='monitoring'"></div>
<h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Заявки
    на хаотические поединки</h3>
<?php
$html = '';
$i = 0;
$sp = mysql_query('SELECT * FROM `zayvki` WHERE `razdel` = 5 AND `cancel` = 0 AND `btl_id` = 0 ORDER BY `id` ASC');
while ($pl = mysql_fetch_array($sp)) {
    $i++;
    //
    $users = '';
    //
    $spu = mysql_query('SELECT `id` FROM `stats` WHERE `zv` = "' . $pl['id'] . '"');
    while ($plu = mysql_fetch_array($spu)) {
        $users .= User::getLogin($plu['id']) . ',';
    }
    //
    if ($users == '') {
        $users = '<i><font color=grey><small>Поле боя ждет своих героев!</small></font></i>';
    } else {
        $users = rtrim($users, ',');
    }
    //
    $html .= $i . '. <span class="date">' . date('H:i', $pl['time']) . '</span> ';
    $html .= ' <img title="' . $u->city_name[$pl['city']] . '" src="//img.new-combats.tech/i/city_ico/' . $pl['city'] . '.gif">';
    $html .= ' (' . $users . ') (' . $pl['min_lvl_1'] . '-' . $pl['max_lvl_1'] . ') Тип боя: <img src="//img.new-combats.tech/i/fighttype' . $pl['type'] . '.gif"> ';
    if ($pl['noinc'] > 0) {
        $html .= '<img src="//img.new-combats.tech/closefight.gif" title="В поединок нельзя вмешаться">';
    }
    if ($pl['fastfight'] > 0) {
        $html .= '<img src="//img.new-combats.tech/fastfight.gif" title="Для старта поединка требуется минимум двое участников">';
    }
    if ($pl['arand'] > 0) {
        $html .= '<img src="//img.new-combats.tech/arand.gif" title="Команды делятся на две равные команды (равные по количеству, но не по мощности)">';
    }
    $html .= '<font color="grey">Бой начнется через <b>' . Conversion::secondsToTimeout(
            $pl['time'] + $pl['time_start'] - time()
        ) . '</b>, таймаут ' . Conversion::secondsToTimeout($pl['timeout']) . '</font>';
    if ($pl['priz'] > 0) {
        $html .= '<a href="/n/567/" target="_blank"><span style="color:#e65700;" title="Победители имеют 5% шанс на получение Жетона Успеха "><b>(Героическое Сражение)</b></span></a></font></i>';
    }
    $html .= '<br>';
}
if ($html == '') {
    $html = '<center>(Раздел пуст)</center>';
}
echo $html;
$html = '';
?>
<h3>Текущие бои</h3>
<?php
$i = 0;
$sp = mysql_query('SELECT * FROM `battle` WHERE `team_win` = -1');
while ($pl = mysql_fetch_array($sp)) {
    //
    $users = '';
    $usersa = [];
    $userst = [];
    //
    $spu = mysql_query(
        'SELECT `a`.`id`,`b`.`team` FROM `users` AS `a` LEFT JOIN `stats` AS `b` ON `a`.`id` = `b`.`id` WHERE `a`.`battle` = "' . $pl['id'] . '"'
    );
    while ($plu = mysql_fetch_array($spu)) {
        if (!isset($usersa[$plu['team']])) {
            $userst[] = $plu['team'];
        }
        $usersa[$plu['team']] .= User::getLogin($plu['id']) . ',';
    }
    //
    if (count($usersa) > 0) {
        $j = 0;
        while ($j < count($userst)) {
            if ($users != '') {
                $users .= ' &nbsp;<b><font color=red><small>против</small></font></b>&nbsp; ';
            }
            $users .= $usersa[$userst[$j]];
            $users = rtrim($users, ',');
            $j++;
        }
        //
        $i++;
        //

        $html .= $i . '. <span class="date">' . date('d.m.Y H:i', $pl['time_start']) . '</span> ';
        $html .= ' <img title="' . $u->city_name[$pl['city']] . '" src="//img.new-combats.tech/i/city_ico/' . $pl['city'] . '.gif">';
        $html .= ' (' . $users . ') Тип боя: <img src="//img.new-combats.tech/i/fighttype' . $pl['type'] . '.gif"> ';
        if ($pl['noinc'] > 0) {
            $html .= '<img src="//img.new-combats.tech/closefight.gif" title="В поединок нельзя вмешаться">';
        }
        if ($pl['fastfight'] > 0) {
            $html .= '<img src="//img.new-combats.tech/fastfight.gif" title="Для старта поединка требуется минимум двое участников">';
        }
        if ($pl['arand'] > 0) {
            $html .= '<img src="//img.new-combats.tech/arand.gif" title="Команды делятся на две равные команды (равные по количеству, но не по мощности)">';
        }
        $html .= '<font color="grey">, таймаут ' . Conversion::secondsToTimeout($pl['timeout']) . '</font>';
        if ($pl['priz'] > 0) {
            $html .= '<a href="/n/567/" target="_blank"><span style="color:#e65700;" title="Та сторона которая победила получит 5% шанс на получение дополнительно Жетона Успеха "><b>(Героическое Сражение)</b></span></a></font></i>';
        }
        $html .= ' <a href="/logs.php?log=' . $pl['id'] . '" target="_blank">&raquo;&raquo;</a> ';
        $html .= '<br>';
    }
    //
}
if ($html == '') {
    $html = '<center>(Раздел пуст)</center>';
}
echo $html;
$html = '';
?>
<h3>Башня смерти</h3>
<?php
$sp = mysql_query('SELECT * FROM `bs_turnirs`');
$i = 0;
while ($pl = mysql_fetch_array($sp)) {
    $i++;
    //
    $html .= $i . '. <img title="' . $u->city_name[$pl['city']] . '" src="//img.new-combats.tech/i/city_ico/' . $pl['city'] . '.gif">';
    //
    $html .= ' [' . $pl['level'] . ']';
    if ($pl['status'] == 0) {
        //Ожидаем начала нового турнира
        $html .= ' Начало турнира в "Башне Смерти" : <span class="date">' . date(
                'd.m.Y H:i', $pl['time_start']
            ) . '</span> (<small> <font color=grey>Начнется через <b>' . Conversion::secondsToTimeout(
                $pl['time_start'] - time()
            ) . '</b></font></small>) Призовой фонд на текущий момент: <b>' . round(
                ($pl['money'] / 100 * 85), 2
            ) . '</b> кр. Всего подано заявок: <b>' . $pl['users'] . '</b>';
    } else {
        //Уже идет турнир
        $users = '';
        $spu = mysql_query('SELECT `id` FROM `users` WHERE `inTurnir` = "' . $pl['id'] . '"');
        while ($plu = mysql_fetch_array($spu)) {
            $users .= User::getLogin($plu['id']) . ',';
        }
        $users = rtrim($users, ',');
        $html .= ' <span title="[' . $pl['status'] . ']">Турнир Башни Смерти уже идет.</span>';
        $html .= ' Участники: ' . $users . ', Лог турнира: <a href="/towerlog.php?towerid=' . $pl['id'] . '&id=' . $pl['count'] . '" target="_blank">&raquo;&raquo;</a>';
    }
    //
    $html .= '<br>';
    //
}
if ($html == '') {
    $html = '<center>(Раздел пуст)</center>';
}
echo $html;
$html = '';
?>

<h3>Войны кланов</h3>
<?php
if ($html == '') {
    $html = '<center>(Раздел пуст)</center>';
}
echo $html;
$html = '';
?>

<h3>История Великих сражений</h3>
<?php
if ($html == '') {
    $html = '<center>(Раздел пуст)</center>';
}
echo $html;
$html = '';
?>

<div align="right"><font style="color:#999;" size="1" face="verdana" color="black">
        <hr style="border-color:#CCC;">
        Бойцовский Клуб v2.1 &copy; <?= date('Y') ?>, «www.new-combats.com»™ &nbsp; &nbsp; </font></div>
<br/><br/>

</body>
</html>