<?php

use Battles\GameLogs;
use Battles\Nick;
use Battles\Template;
use Battles\User

require_once "functions.php";
if (User::getInstance()->getRoom() == 51) {
    header('location: city.php');
    exit;
}

if ($_GET['go'] == 'vixod') {
    $les_login = mysql_fetch_array(mysql_query("SELECT `room` FROM `les_game` WHERE `owner` = '" . $user['id'] . "' LIMIT 1"));
    if ($les_login['room'] == 'G8') {
        mysql_query("UPDATE `users`,`online` SET `users`.`room` = '51', `online`.`room` = '51' WHERE `online`.`id` = `users`.`id` AND `online`.`id` = '" . $user['id'] . "'");
        mysql_query("DELETE FROM `les_res` WHERE `owner` = '" . $user['id'] . "' LIMIT 1");
    }
}


$tes = mysql_query("SELECT * FROM `les_res` WHERE `owner` = '" . $user['id'] . "'");
if (!$les_res = mysql_fetch_array($tes)) {
    $i = 0;
    while ($i++ < 25) {
        $res = ['', 'Silver Nugget', 'Stem', 'Animal Bone', 'Suede', 'Iron Ore', 'Silver Nugget', 'Stem', 'Animal Bone', 'Suede', 'Iron Ore', 'Silver Nugget', 'Stem', 'Animal Bone', 'Suede', 'Iron Ore', 'Silver Nugget', 'Stem', 'Animal Bone', 'Suede', 'Iron Ore', 'Silver Nugget', 'Stem', 'Animal Bone', 'Suede', 'Iron Ore'];
        $img = ['', 'Silver_Nugget.jpg', 'Stem.jpg', 'Animal_Bone.jpg', 'Suede.jpg', 'Iron_Ore.jpg', 'Silver_Nugget.jpg', 'Stem.jpg', 'Animal_Bone.jpg', 'Suede.jpg', 'Iron_Ore.jpg', 'Silver_Nugget.jpg', 'Stem.jpg', 'Animal_Bone.jpg', 'Suede.jpg', 'Iron_Ore.jpg', 'Silver_Nugget.jpg', 'Stem.jpg', 'Animal_Bone.jpg', 'Suede.jpg', 'Iron_Ore.jpg', 'Silver_Nugget.jpg', 'Stem.jpg', 'Animal_Bone.jpg', 'Suede.jpg', 'Iron_Ore.jpg'];
        $otdel = rand(1, 9);
        $komnata = rand(0, 7);
        $kmn[1] = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8'];
        $kmn[2] = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8'];
        $kmn[3] = ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8'];
        $kmn[4] = ['D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8'];
        $kmn[5] = ['E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8'];
        $kmn[6] = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8'];
        $kmn[7] = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8'];
        $kmn[8] = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8'];
        $kmn[9] = ['I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8'];
        mysql_query("INSERT INTO les_res (`owner`, `name`, `img`, `kom`) VALUES ('" . $user['id'] . "', '" . $res[$i] . "', '" . $img[$i] . "', '" . $kmn[$otdel][$komnata] . "')");
    }
}

$rand = rand(25, 100);
$rbot = rand(1, 5);
$bots[1] = 'Медведь';
$bots[2] = 'Заяц';
$bots[3] = 'Лось';
$bots[4] = 'Волк';
$bots[5] = 'Лесник';
if ($rand < 15 && $user['battle'] == 0) {
    $sex = mysql_query("SELECT `id`, `maxhp` FROM `users` WHERE `login` = '" . $bots[$rbot] . "' LIMIT 1");
    $dded = mysql_fetch_array($sex);
    mysql_query("INSERT INTO `bots` (`name`, `prototype`, `battle`, `hp`) VALUES ('" . $bots[$rbot] . "', '" . $dded["id"] . "', '', '" . $dded["maxhp"] . "')");
    $bot = mysql_insert_id();
    $teams = [];
    $teams[$user['id']][$bot] = [0, 0, time()];
    $teams[$bot][$user['id']] = [0, 0, time()];
    mysql_query("INSERT INTO `battle`(`teams`, `timeout`, `type`, `status`, `t1`, `t2`, `to1`, `to2`) VALUES ('" . serialize($teams) . "', '3', '1', '0', '" . $user['id'] . "', '" . $bot . "', '" . time() . "', '" . time() . "')");
    $id = mysql_insert_id();
    mysql_query("UPDATE `bots` SET `battle` = {$id} WHERE `id` = {$bot} LIMIT 1");
    $rr = "<b>" . Nick::id($user['id'])->full(1) . "</b> и <b>" . Nick::id($bot)->full(1) . "</b>";
    GameLogs::addBattleLog($id, "Часы показывали <span class=date>" . date("Y.m.d H.i") . "</span>, когда " . $rr . " бросили вызов друг другу. <br />");
    mysql_query("UPDATE `users` SET `battle` = {$id}, `zayavka` = 0 WHERE `id` = '" . $user['id'] . "' LIMIT 1");
    addchp('<b style="color:#990000">Внимание!</b> На вас напал "' . $bots[$rbot] . '".   ', '{[]}' . Nick::id($user["id"])->short() . '{[]}');
}

$les_login = mysql_fetch_array(mysql_query("SELECT * FROM `les_game` WHERE `owner` = '" . $user['id'] . "' LIMIT 1"));

$rooms = ["A1" => ['20', '', 'A2', '', 'B1'], "A2" => ['15', 'A1', 'A3', '', ''], "A3" => ['15', 'A2', '', '', ''], "A4" => ['15', '', 'A5', '', 'B4'], "A5" => ['15', 'A4', '', '', 'B5'], "A6" => ['15', '', 'A7', '', 'B6'], "A7" => ['15', 'A6', 'A8', '', ''], "A8" => ['15', 'A7', '', '', 'B8'], "B1" => ['20', '', '', 'A1', ''], "B2" => ['15', '', 'B3', '', 'C2'], "B3" => ['15', 'B2', 'B4', '', ''], "B4" => ['15', 'B3', '', 'A4', ''], "B5" => ['15', '', 'B6', 'A5', ''], "B6" => ['15', 'B5', 'B7', 'A6', ''], "B7" => ['15', 'B6', '', '', 'C7'], "B8" => ['15', '', '', 'A8', 'C8'], "C1" => ['20', '', '', '', 'D1'], "C2" => ['15', '', 'C3', 'B2', 'D2'], "C3" => ['15', 'C2', '', '', 'D3'], "C4" => ['15', '', '', '', 'D4'], "C5" => ['15', '', '', '', 'D5'], "C6" => ['15', '', 'C7', '', ''], "C7" => ['15', 'C6', 'C8', 'B7', ''], "C8" => ['15', 'C7', '', 'B8', 'D8'], "D1" => ['20', '', 'D2', 'C1', 'E1'], "D2" => ['15', 'D1', 'D3', 'C2', 'E2'], "D3" => ['15', 'D2', 'D4', 'C3', 'E3'], "D4" => ['15', 'D3', 'D5', 'C4', 'E4'], "D5" => ['15', 'D4', '', 'C5', ''], "D6" => ['15', '', 'D7', '', 'E6'], "D7" => ['15', 'D6', '', '', ''], "D8" => ['15', '', '', 'C8', 'E8'], "E1" => ['20', '', 'E2', 'D1', 'F1'], "E2" => ['15', 'E1', 'E3', 'D2', 'F2'], "E3" => ['15', 'E2', 'E4', 'D3', 'F3'], "E4" => ['15', 'E3', 'E5', 'D4', 'F4'], "E5" => ['15', 'E4', 'E6', '', 'F5'], "E6" => ['15', 'E5', '', 'D6', ''], "E7" => ['15', '', 'E8', '', 'F7'], "E8" => ['15', 'E7', '', 'D8', 'F8'], "F1" => ['20', '', 'F2', 'E1', 'G1'], "F2" => ['15', 'F1', 'F3', 'E2', 'G2'], "F3" => ['15', 'F2', 'F4', 'E3', 'G3'], "F4" => ['15', 'F3', 'F5', 'E4', 'G4'], "F5" => ['15', 'F4', '', 'E5', 'G5'], "F6" => ['15', '', '', '', 'G6'], "F7" => ['15', '', '', 'E7', 'G7'], "F8" => ['15', '', '', 'E8', 'G8'], "G1" => ['20', '', '', 'F1', 'H1'], "G2" => ['15', '', '', 'F2', ''], "G3" => ['15', '', '', 'F3', ''], "G4" => ['15', '', '', 'F4', ''], "G5" => ['15', '', 'G6', 'F5', ''], "G6" => ['15', 'G5', 'G7', 'F6', ''], "G7" => ['15', 'G6', 'G8', 'F7', ''], "G8" => ['15', 'G7', '', 'F8', 'H8'], "H1" => ['20', '', 'H2', 'G1', 'I1'], "H2" => ['15', 'H1', '', '', 'I2'], "H3" => ['15', '', '', '', 'I3'], "H4" => ['15', '', 'H5', '', 'I4'], "H5" => ['15', 'H4', '', '', 'I5'], "H6" => ['15', '', 'H7', '', 'I6'], "H7" => ['15', 'H6', '', '', 'I7'], "H8" => ['15', 'H7', '', 'G8', 'I8'], "I1" => ['20', '', 'I2', 'H1', ''], "I2" => ['15', 'I1', 'I3', 'H2', ''], "I3" => ['15', 'I2', 'I4', 'H3', ''], "I4" => ['15', 'I3', 'I5', 'H4', ''], "I5" => ['15', 'I4', 'I6', 'H5', ''], "I6" => ['15', 'I5', 'I7', 'H6', ''], "I7" => ['15', 'I6', 'I8', 'H7', ''], "I8" => ['15', 'I7', '', 'H8', '']];

$time = $_SESSION['time'] - time();
if ($time < 0) {
    $time = 0;
}

if ($_GET["v"] && $_GET["v"] != '' && $time <= 0 && $rooms[$les_login['room']][1] == $_GET["v"]) {
    $_SESSION['time'] = time() + $rooms[$les_login['room']][0];
    mysql_query("UPDATE `les_game` SET `room` = '" . $_GET["v"] . "' WHERE `owner` = '" . $user['id'] . "' LIMIT 1");
    $_GET["v"] = 0;
}

if ($_GET["n"] && $_GET["n"] != '' && $time <= 0 && $rooms[$les_login['room']][2] == $_GET["n"]) {
    $_SESSION['time'] = time() + $rooms[$les_login['room']][0];
    mysql_query("UPDATE `les_game` SET `room` = '" . $_GET["n"] . "' WHERE `owner` = '" . $user['id'] . "' LIMIT 1");
    $_GET["n"] = 0;
}

if ($_GET["l"] && $_GET["l"] != '' && $time <= 0 && $rooms[$les_login['room']][3] == $_GET["l"]) {
    $_SESSION['time'] = time() + $rooms[$les_login['room']][0];
    mysql_query("UPDATE `les_game` SET `room` = '" . $_GET["l"] . "' WHERE `owner` = '" . $user['id'] . "' LIMIT 1");
    $_GET["l"] = 0;
}

if ($_GET["p"] && $_GET["p"] != '' && $time <= 0 && $rooms[$les_login['room']][4] == $_GET["p"]) {
    $_SESSION['time'] = time() + $rooms[$les_login['room']][0];
    mysql_query("UPDATE `les_game` SET `room` = '" . $_GET["p"] . "' WHERE `owner` = '" . $user['id'] . "' LIMIT 1");
    $_GET["p"] = 0;
}

///////////////Сбор/////////////
if ($_GET['res']) {
    $gss = mysql_fetch_array(mysql_query("SELECT * FROM `les_res` WHERE `owner` = '" . $user['id'] . "' AND `id` = '" . mysql_real_escape_string($_GET['res']) . "' AND `kom` = '" . $les_login['room'] . "' LIMIT 1"));
    if ($gss) {
        $f = mysql_query("SELECT `koll` FROM `inventory` WHERE `owner` = '" . $user['id'] . "' AND `type` = '200' AND `name` = '" . $gss['name'] . "' LIMIT 1");
        if ($g = mysql_fetch_array($f)) {
            mysql_query("UPDATE `inventory` SET `koll` = (`koll`+1), `massa` = (`massa`+0.1) WHERE `owner` = '" . $user['id'] . "' AND `type` = '200' AND `name` = '" . $gss['name'] . "' LIMIT 1");
        } else {
            $fo = mysql_query("INSERT INTO `inventory`(`name`, `koll`, `img`, `owner`, `type`, `massa`, `isrep`, `podzem`, `maxdur`, `present`) VALUES ('" . $gss['name'] . "', '1', '" . $gss['img'] . "', '" . $user['id'] . "', '200', '0.1', '0', '1', '1', 'Лес')");
        }
        mysql_query("DELETE FROM `les_res` WHERE `id` = '" . $gss['id'] . "' LIMIT 1");
        addchp('<b style="color:#990000">Внимание!</b> <b>' . $user["login"] . '</b>, поднял предмет "' . $gss['name'] . '".', '{[]}' . Nick::id($user["id"])->short() . '{[]}');
        echo "<font style='color:#990000'>Вы подняли <b>'" . $gss['name'] . "'</b>.</font>";
    } else {
        echo "&nbsp;<font style='font-size:12px; color:#cc0000;'>Кто-то оказался быстрее!</font>";
    }
}
Template::header('Лес');
?>
<script src="i/forest/char1.32.js"></script>
<script src="i/forest/funcs1.6.js"></script>
<center><h4>Лес</h4></center>
<dd>
    <table cellspacing=2 cellpadding=0>
        <tr>
            <td>
                <center>
                    <table width="450" border="0" cellspacing="1" cellpadding="0" bgcolor="#000000">
                        <tr>
                            <td background="i/forest/bg_scroll_01.gif" align="center">Имя</td>
                            <td background="i/forest/bg_scroll_01.gif" align="center">Hp</td>
                            <td background="i/forest/bg_scroll_01.gif" align="center">Расположение</td>
                        </tr>
                        <?
                        $rog = mysql_query("SELECT * FROM `les_game` WHERE `owner` = '" . $user['id'] . "'");
                        while ($more = mysql_fetch_array($rog)) {
                        $big = mysql_fetch_array(mysql_query("SELECT `hp`, `maxhp`, `id`, `login` FROM `users` WHERE `id` = '" . $more['owner'] . "'"));
                        ?>
                        <tr>
                            <td background="i/forest/bg_scroll_05.gif" align="center">
                                <a href=inf.php?<?= $big['id'] ?> target=_blank
                                   title="Информация о <?= $big['login'] ?>"><?= $big['login'] ?></a></td>
                            <td background="i/forest/bg_scroll_05.gif" align="center"><?= $big['hp'] ?>
                                /<?= $big['maxhp'] ?> </td>
                            <td background="i/forest/bg_scroll_05.gif" align="center"><?= $more['room'] ?></td>
                            <?
                            echo "</tr>";
                            }
                            ?>
                    </table>
                </center>
            </td>
        </tr>
    </table>

    <table cellpadding=5 width=100%>
        <tr valign=top>
            <td>
                <table align=center width=95% height=100% cellpadding=5>
                    <tr valign=top>
                        <td width=100%>

                            <table width=100%>
                                <tr>

                                    <td align=center colspan=2 id="navtbl">
                                        <?
                                        if ($rooms[$les_login['room']][1] != '') {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?v=" . $rooms[$les_login['room']][1] . "';\" value=\"На север\" />";
                                        } else {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?v=" . $rooms[$les_login['room']][1] . "';\" value=\"\" />";
                                        }
                                        ?>
                                        <br/><br/>
                                        <?
                                        if ($rooms[$les_login['room']][3] != '') {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?l=" . $rooms[$les_login['room']][3] . "';\" value=\"На запад\" />";
                                        } else {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onClick=\"location.href='?l=" . $rooms[$les_login['room']][3] . "';\" value=\"\" />";
                                        }
                                        ?>
                                        &nbsp;
                                        <?
                                        if ($rooms[$les_login['room']][4] != '') {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?p=" . $rooms[$les_login['room']][4] . "';\" value=\"На восток\" />";
                                        } else {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?p=" . $rooms[$les_login['room']][4] . "';\" value=\"\" />";
                                        }
                                        ?>&nbsp;
                                        <br/><br/>
                                        <?
                                        if ($rooms[$les_login['room']][2] != '') {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?n=" . $rooms[$les_login['room']][2] . "';\" value=\"На юг\" />";
                                        } else {
                                            echo "<input style=\"width: 150px; height: 20;\" type=\"button\" class=\"btn\" onclick=\"location.href='?n=" . $rooms[$les_login['room']][2] . "';\" value=\"\" />";
                                        }
                                        ?>
                                        <br/><br/>


                                        <? if ($les_login['room'] == 'G8') { ?>
                                            <input class=btn style="width:150px;height:20px;" type="button"
                                                   value="Выйти из леса" onclick="location.href='?go=vixod';"/><br/>
                                            <br/>
                                        <? } ?>
                                    </td>
                                    <? $les = mysql_query("SELECT `id` FROM `les_res` WHERE `owner` = '" . $user['id'] . "'"); ?>
                                </tr>
                                <tr valign=top>
                                    <td width="40%" valign="top">
                                        <a href='i/forest/les.jpg' target="_blank">Карта</a>
                                        <br/>
                                        <span>Расположение : <b><?= $les_login['room'] ?></b></span><br/>
                                        <span>Не собрано ресурсов : <b><?= $col = mysql_num_rows($les); ?></b></span><br/><span
                                                id="moveid"></span>
                                        <?
                                        $time = ($_SESSION['time'] - time());
                                        if ($time < 0) {
                                            $time = 0;
                                        }
                                        ?>
                                        <script>go_timer_on(<?=$time?>, 200);</script>
                                    </td>
                                    <td width="60%" align=right valign="bottom">
                                        <?
                                        $le = mysql_query("SELECT `id`, `img`, `name`, `owner`, `kom` FROM `les_res` WHERE `owner` = '" . $user['id'] . "' AND `kom` = '" . $les_login['room'] . "'");
                                        while ($res = mysql_fetch_array($le)) {
                                            echo "<a href='forest.php?res=" . $res['id'] . "'><img src=\"i/sh/" . $res['img'] . "\" title=\"" . $res['name'] . "\" border=0 /></a>";
                                        }
                                        ?>
                                    </td>
                                </tr>
                            </table>
                            <?
                            $img = rand(1, 24);
                            ?>
                        <td align=center>
                            <div id=bgid
                                 style="background-color:#cccccc;border:#666666 1px solid;position:relative;width:450px;height:300px">
                                <img class=img2 height=300 src="forest/<?= $img ?>.jpg"/></div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</dd>