<?php
require_once('system/func.php');
$title = 'Сражение ';
require_once('system/header.php');
auth(); // Закроем от гостей


$fights = mysql_fetch_assoc(mysql_query("SELECT * FROM `fights` WHERE `id_user`='" . $myID . "' LIMIT 1"));


$ability = mysql_query("SELECT * FROM `ability` WHERE `id_user` = '" . $myID . "' ORDER BY `id` LIMIT 1");
$ability = mysql_fetch_array($ability);

if ($fights['health'] > $user['max_health'] + $ClassHp) {
    $fights['health'] = $user['max_health'] + $ClassHp;
}

//Если игрок не был еще на сражениях
if (!$fights) {
    mysql_query("INSERT INTO `fights` SET `id_user`='" . $myID . "'");
    mysql_query("UPDATE `fights` SET  `health`='$hp' WHERE `id_user`='" . $myID . "' LIMIT 1");

}

//Регистрация мобов
$mob = mysql_query("SELECT * FROM `fights_mob`  WHERE  `lvl` <= '$user[level]'   ORDER BY RAND()");
$mob = mysql_fetch_assoc($mob);


//Если    игрок убежа во время боя
// if($fights['exit'] > time()){
//echo div('header ')." <img src='/images/fight/logo.jpg'></div>";
//$hp = $user['max_health'];
//mysql_query("update `ability` set    `energy` = '0'  ,  `statusHp` = '0'  ,  `statusDef` = '0' ,  `statusStr` = '0'  where (`id_user` = '".$myID."')");

//mysql_query("UPDATE `fights` SET  `health`='$hp' ,  `status` = '0'  WHERE `id_user`='".$myID."' LIMIT 1");
//mysql_query("DELETE FROM `fights_log`  WHERE `id_user`='".$myID."'");

//echo div('header')." Вы убежали во время боя<br> Вернуться в бой через: ".tl($fights['exit']  -time())." <br><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?'>Обновить  <span id='time_54000'>5 сек</span></div></span></span></a></div>";
//require_once ('system/footer.php');
//exit();
//}


if ($fights['resp'] > time()) {
    echo div('header') . " <img src='/images/fight/logo.jpg'></div>";
    echo div('header') . " Вы отступили<br> Вернуться в бой через: " . tl($fights['resp'] - time()) . "<br> <div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?'>Обновить </div></span></span></a></div>";
    $hp = $user['max_health'];
    mysql_query("DELETE FROM `fights_log`  WHERE `id_user`='" . $myID . "'");

    mysql_query("UPDATE `fights` SET  `health`='$hp' ,  `status` = '0'  WHERE `id_user`='" . $myID . "' LIMIT 1");
    require_once('system/footer.php');
    exit();
}


if (isset($_GET['start'])) {
    mysql_query("UPDATE  `fights` SET `status` = '2' WHERE  `id_user`='" . $myID . "'");
    $hp = $user['max_health'];
    mysql_query("UPDATE `fights` SET  `health`='$hp'WHERE `id_user`='" . $myID . "' LIMIT 1");
    header('Location: /fights/battle/');
    exit();
}

if ($fights['heatlh'] > $user['max_health']) {
    $fights['health'] = $user['max_health'];
}
switch (isset($_GET['action'])) {
    case 'battle':

        if ($fights['status'] != 2) {
            header('Location: /fights/');
            exit();

        }

        //Если у игрока мало жизней
        if ($fights['health'] <= 0) {
            mysql_query("update `ability` set    `energy` = '0'  ,  `statusHp` = '0'  ,  `statusDef` = '0' ,  `statusStr` = '0'  where (`id_user` = '" . $myID . "')");

            mysql_query("UPDATE `fights` SET `resp`='" . (time() + 300) . "' WHERE `id_user`='" . $myID . "' LIMIT 1");
            mysql_query("UPDATE `fights` SET `opp_name`= '$mob[name]' ,  `opp_health`='$mob[health]' ,  `opp_max_health`='$mob[health]' ,  `str`= '$mob[str]' ,  `def`='$mob[def]'    WHERE `id_user`='" . $myID . "' LIMIT 1");
            header('Location: /fights/');
            exit();
        }

        if (isset($_GET['change']) and $fights['last'] < time()) {


            mysql_query("UPDATE `fights` SET `opp_name`= '0' ,  `opp_health`='0' ,  `opp_max_health`='0' ,  `str`= '0' ,  `def`='0'  WHERE `id_user`='" . $myID . "' LIMIT 1");
            mysql_query("UPDATE `fights` SET `last`='" . (time() + 60) . "' WHERE `id_user`='" . $myID . "' LIMIT 1");

            $log = 'Вы убежали  ';
            mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");
            header('Location: /fights/battle/');

            exit();
        }


        if (isset($_GET['exit'])) {
            mysql_query("UPDATE `fights` SET `opp_name`= '0' ,  `opp_health`='0' ,  `opp_max_health`='0' ,  `str`= '0' ,  `def`='0'  ,  `status`='0'  WHERE `id_user`='" . $myID . "' LIMIT 1");
            mysql_query("UPDATE `fights` SET `exit`='" . (time() + 900) . "' WHERE `id_user`='" . $myID . "' LIMIT 1");
            mysql_query("DELETE FROM `fights_log`  WHERE `id_user`='" . $myID . "'");

            $_SESSION['msg'] = 'Вы покинули Сражение  ';

            header('Location: /main');
            exit();
        }


        if (isset($_GET['next'])) {
            mysql_query("UPDATE  `fights` SET `status` = '2' WHERE  `id_user`='" . $myID . "'");

            $r = rand(1, 5);

            switch ($r) {
                case 1:
                    mysql_query("UPDATE `fights` SET `opp_name`= '$mob[name]' ,  `opp_health`='$mob[health]' ,  `opp_max_health`='$mob[health]' ,  `str`= '$mob[str]' ,  `def`='$mob[def]'    WHERE `id_user`='" . $myID . "' LIMIT 1");
                    $log = 'На вас напал ' . $mob['name'] . '';
                    mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");
                    header('Location: /fights/battle/');
                    exit();
                    break;

                default:
                    switch ($r) {
                        case 2:
                            $log = 'Вы отправились на Север';
                            break;

                        case 3:
                            $log = 'Вы отправились на Юг';
                            break;

                        case 4:
                            $log = 'Вы отправились на Восток';

                        case 5:
                            $log = 'Вы отправились на Запад';

                    }

                    mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");
                    header('Location: /fights/battle/');
                    exit();
            }

        }

        //Урон
        $crit = 15;
        $ChanseCrit = rand(1, 100);
        $dodje = 100;
        $dmg = 0;
        $dmg2 = 0;


        $dmg = rand(round($user['str'] / 4), round($user['str'] / 5));
        $dmg2 = rand(round($fights['str'] / 3), round($fights['str'] / 4));

        $dmg += rand(1, 10);
        $dmg2 += rand(1, 10);
        //Крит
        if ($ChanseCrit <= $crit) {
            $log_crit = true;
            $dmg *= 2;

        }
        if ($ability['statusDef'] == 1) {
            $dmg2 = ceil($dmg2 - ($ability['spellDefAbsorb'] + ($user['def'] / 20)));
        }
        if ($ability['statusStr'] == 1) {
            $SpellStr = ceil($dmg + ($ability['spellStrDmg'] + ($user['str'] / 15)));
        }

        $dmg += $SpellStr;
        if ($dmg <= 0) {
            $dmg = 1;
        }
        if ($dmg2 <= 0) {
            $dmg2 = rand(1, 10);
        }

        //Умения  Сила жизни
        if (isset($_GET['UseSpellHp']) and $ability['statusHp'] == 0 and $ability['energy'] >= 1) {
            //Если юзали и меньше энергии
            if ($ability['statusHp'] == 1 and $ability['energy'] <= 1) {
                header('Location: /fights/battle/');

                exit();

            }


            $SpellRespHp = ceil($ability['spellHpResp'] + ($user['max_health'] / 100 * 10));

            $logSpellHp = color('lime') . ' Вы полечились  на  ' . ico('ability', 'spell_health.png') . ' ' . $SpellRespHp . ' здоровья </font>';
            mysql_query("update `fights` set    `health` = '" . ($fights['health'] + $SpellRespHp) . "' where (`id_user` = '" . $myID . "')");
            mysql_query("update `ability` set    `energy` = '" . ($ability['energy'] - 1) . "'  ,  `statusHp` = '1'   where (`id_user` = '" . $myID . "')");
            mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $logSpellHp . "'");
            header('Location: /fights/battle/');

            exit();


        }
        //Умения  Крепкая броня
        if (isset($_GET['UseSpellDef']) and $ability['statusDef'] == 0 and $ability['energy'] >= 1) {
            //Если юзали и меньше энергии
            if ($ability['statusDef'] == 1 and $ability['energy'] <= 1) {
                header('Location: /fights/battle/');

                exit();

            }
            $spellDef = ceil($ability['spellDefAbsorb'] + ($user['def'] / 20));

            $logSpellDef = color('lime') . ' Вы уменьшили урон   на  ' . ico('ability', 'spell_defence.png') . ' ' . $spellDef . ' урона </font>';
            mysql_query("update `ability` set    `energy` = '" . ($ability['energy'] - 1) . "'  ,  `statusDef` = '1'   where (`id_user` = '" . $myID . "')");
            mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $logSpellDef . "'");
            header('Location: /fights/battle/');

            exit();
        }

        //Умения  Сила удара
        if (isset($_GET['UseSpellStr']) and $ability['statusStr'] == 0 and $ability['energy'] >= 1) {
            //Если юзали и меньше энергии
            if ($ability['statusStr'] == 1 and $ability['energy'] <= 1) {
                header('Location: /fights/battle/');

                exit();

            }
            $spellStr = ceil($ability['spellStrDmg'] + ($user['str'] / 15));

            $logSpellStr = color('orange') . ' Вы увеличили урон   на  ' . ico('ability', 'double_attack.jpg') . ' ' . $spellStr . '  </font>';
            mysql_query("update `ability` set    `energy` = '" . ($ability['energy'] - 1) . "'  ,  `statusStr` = '1'   where (`id_user` = '" . $myID . "')");
            mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $logSpellStr . "'");
            header('Location: /fights/battle/');

            exit();


        }
        if (isset($_GET['attack']) && $fights['opp_health'] > 0 && $fights['health'] > 0) {
            if ($fights['opp_health'] < 0 AND $fights['health'] < 0) {
                header('Location: /fights/battle/');
                exit();

            }
            mysql_query("update `ability` set    `energy` = '" . ($ability['energy'] + 1) . "'     where (`id_user` = '" . $myID . "')");


            mysql_query("UPDATE `fights` SET `opp_health`=`opp_health`-'" . $dmg . "' WHERE `id_user`='" . $myID . "' LIMIT 1");
            mysql_query("UPDATE `fights` SET `health`=`health`-'" . $dmg2 . "' WHERE `id_user`='" . $myID . "' LIMIT 1");

            if ($fights['health'] < 1) {
                $gold = 100;
                mysql_query("UPDATE `fights` SET `resp` = `resp`  + '" . (time() + 60) . "' WHERE `id_user`='" . $myID . "' LIMIT 1");
                $log = color('tomato') . 'Вас   убил  ' . $fights[opp_name] . ' и получили  </font> ' . $gold . ' ';
                mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");
            } else {
                $log = color('tomato') . ' ' . $fights[opp_name] . '      ударил вас   на ' . $dmg2 . '</font>';
                mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");


            }

            //$_SESSION['check'] = mt_rand(1, 100) . microtime(true);

            if ($dmg >= $fights['opp_health']) {
                //Награда
                $LG = 1000;
                $LE = 530;

                $RandReward = rand(1, 3);
                if ($fights['LimitGold'] < ($user['level'] * 100) and $RandReward == 1) {
                    $RewardGold = ceil(($user['level'] * 100) / 100 * 4);
                    $reward = ico('icons', 'gold.png') . ' ' . $RewardGold . ' ';
                    mysql_query("UPDATE `users` SET `gold`=`gold`+ '" . $RewardGold . "' WHERE `id`='" . $myID . "' LIMIT 1");
                    mysql_query("UPDATE `fights` SET `LimitGold`=`LimitGold`+ '" . $RewardGold . "' WHERE `id_user`='" . $myID . "' LIMIT 1");

                } else if ($fights['LimitExp'] < ($user['level'] * 100) and $RandReward == 2) {
                    $RewardExp = ceil(($user['level'] * 50) / 100 * 4);
                    $reward = ico('icons', 'exp.png') . ' ' . $RewardExp . ' ';
                    mysql_query("UPDATE `users` SET `exp`=`exp`+ '" . $RewardExp . "' WHERE `id`='" . $myID . "' LIMIT 1");
                    mysql_query("UPDATE `fights` SET `LimitExp`=`LimitExp`+ '" . $RewardExp . "' WHERE `id_user`='" . $myID . "' LIMIT 1");


                    if ($user['clan_rang'] > 0) {
                        $RewardExp = ceil(($user['level'] * 50) / 100 * 2);
                        $exp = $RewardExp;

                        mysql_query("UPDATE `users` SET `clan_exp` = '" . ($user['clan_exp'] + ceil($exp * $user[trust] / 100)) . "' WHERE `id` = '" . $myID . "' LIMIT 1");
                        mysql_query("UPDATE `clans` SET `exp` = `exp` + '" . ceil($exp * $user[trust] / 100) . "' WHERE `id` = '" . $user['id_clan'] . "' LIMIT 1");
                        mysql_query("UPDATE `users` SET `ResetClanExp` = `ResetClanExp` + '" . ceil($exp * $user[trust] / 100) . "' WHERE `id` = '" . $myID . "' LIMIT 1");

                    }

                } else if ($fights['LimitAlmaz'] < ($user['level'] * 2) and $RandReward == 3) {
                    $RewardAlmaz = 2;

                    $reward = ico('icons', 'almaz.png') . ' ' . $RewardAlmaz . ' ';
                    mysql_query("UPDATE `users` SET `almaz`=`almaz`+ '" . $RewardAlmaz . "' WHERE `id`='" . $myID . "' LIMIT 1");
                    mysql_query("UPDATE `fights` SET `LimitAlmaz`=`LimitAlmaz`+ '" . $RewardAlmaz . "' WHERE `id_user`='" . $myID . "' LIMIT 1");
                } else {


                    $skull = rand(1, 5);
                    mysql_query("UPDATE `fights` SET `skull`=`skull`+ '" . $skull . "' WHERE `id_user`='" . $myID . "' LIMIT 1");
                    $reward = ico('icons', 'skull.png') . ' ' . $skull . ' ';

                }


                //Акция
                if ($user['action_fight'] == 0) {
                    mysql_query("UPDATE `users`  SET  `action_koll`='" . ($user['action_koll'] + 1) . "'  WHERE `id`='" . $myID . "'");
                }


                //Кампания
                if ($campaign['id_camp'] == 2) {
                    mysql_query("UPDATE `campaign`  SET  `fight`='" . ($campaign['fight'] + 1) . "'  WHERE `id_user`='" . $myID . "'");
                }


                //Кампания
                if ($campaign['id_camp'] == 4) {
                    mysql_query("UPDATE `campaign`  SET  `fight`='" . ($campaign['fight'] + 1) . "'  WHERE `id_user`='" . $myID . "'");
                }


                $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '1'"));
                $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'] . "')");
                }

                $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '5'"));
                $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'] . "')");
                }


                mysql_query("update `ability` set    `energy` = '0'  ,  `statusHp` = '0'  ,  `statusDef` = '0' ,  `statusStr` = '0'  where (`id_user` = '" . $myID . "')");


                $log = color('green') . 'Вы  убили  ' . $fights[opp_name] . ' и получили   </font> ' . $reward . '';
                mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");

            } else {


                $log = '<font color = green> Вы ударили ' . $fights['opp_name'] . ' на  ' . $dmg . ' </font> ' . color('white') . '' . ($log_crit == true ? '(крит)</font>' : '</font>');
                mysql_query("INSERT INTO `fights_log` SET `id_user`='" . $myID . "', `text`='" . $log . "'");
            }

            header('Location: /fights/battle/');
            exit();
        }

        $progressUser = round($fights['health'] / $user['max_health'] * 100, 1);


        if ($fights['opp_health'] > 0 AND $fights['health'] > 0) {

            echo div('wrapper red') . " " . div('oh') . "  </div>
        " . color('tomato') . "" . $fights['opp_name'] . "</font><br>" . ico('icons', 'str.png') . "
        Сила: " . $fights['str'] . " <br>" . ico('icons', 'health.png') . " Здоровье: " . $fights['opp_health'] . " <br> " . ico('icons', 'def.png') . " Броня: " . $fights['def'] . "<br><br><br><br>";
            //echo "<div class='exp_bar'><div class='progress_life' style='width:$progressOpponent%'></div></div></div>";


            if ($ability) {


                echo div('header');
                echo " <a href='?attack'>" . img('ability', 'spell_attack.png') . "</a>";
                if ($ability['statusHp'] == 1 or $ability['energy'] < 1) {
                    echo " <a href='?'>" . spell('ability', 'spell_health.png ') . "</a>";
                } else {
                    echo " <a href='?UseSpellHp'>" . img('ability', 'spell_health.png ') . "</a>";
                }


                if ($ability['statusDef'] == 1 or $ability['energy'] < 1) {
                    echo " <a href='?'>" . spell('ability', 'spell_defence.png ') . "</a>";
                } else {
                    echo " <a href='?UseSpellDef'>" . img('ability', 'spell_defence.png ') . "</a>";
                }


                if ($ability['statusStr'] == 1 or $ability['energy'] < 1) {
                    echo " <a href='?'>" . spell('ability', 'double_attack.jpg ') . "</a>";
                } else {
                    echo " <a href='?UseSpellStr'>" . img('ability', 'double_attack.jpg ') . "</a>";
                }


                echo "</div>";


            } else {


                echo "<div class='header'><a href='?attack'>" . img('ability', 'spell_attack.png') . "</a></div>";

            }


            echo div('wrapper gold') . " " . color('lime') . "Вы </font><br><span class='center'> " . ico('icons', 'str.png') . "
        " . $user['str'] . " " . ico('icons', 'health.png') . "  " . $fights['health'] . "  " . ico('icons', 'def.png') . "    " . $user['def'] . "</span>";
            //echo "<div class='exp_bar'><div class='progress_life' style='width:$progressUser%'></div></div></div>";


        } else {
            //$batle = mysql_query("SELECT * FROM `bashna` WHERE `id` = '1' ORDER BY `id` LIMIT 1");
            //$batle = mysql_fetch_array($batle);
            //if ($batle['resp_boss'] < time()) {

            //$timeOrk = "На севере появился орк из леса  <br><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/battle_ork/' > Отправиться и сразиться </div></span></span> </a>";

            //} else {
            //$timeOrk = " <br><span style ='float:right;'>" . isTimer($batle['resp_boss']) . "</span>";
            // }
            // echo div('header');
            //echo $timeOrk;
            //echo "</div>";

            echo ' <center>'.div('header').''.div('header').''.div('content_blue').'</div><img src="/images/revolt.png"> <br><br></div></div>';

            echo div('header');

            echo " <div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?next'> Отправиться </div></span></span></a>";
            echo "</div>";


        }


        echo div('wrapper');
        echo span('gold') . " " . ico('icons', 'gold.png') . " $fights[LimitGold] из " . ico('icons', 'gold.png') . " " . ($user['level'] * 100) . " </span>  " . span('almaz') . "  " . ico('icons', 'almaz.png') . "  $fights[LimitAlmaz] из " . ico('icons', 'almaz.png') . " " . ($user['level'] * 2) . "    </span>      " . span('white') . "" . ico('icons', 'exp.png') . "    $fights[LimitExp] из " . ico('icons', 'exp.png') . " " . ($user['level'] * 100) . "    </span>     ";


        echo "</div>";

        $q = mysql_query("SELECT * FROM `fights_log`  WHERE `id_user` = '" . $myID . "'  ORDER BY(`id`) DESC LIMIT 20");
        echo div('text_p1');

        while ($log = mysql_fetch_array($q)) {

            echo '' . $log['text'] . ' <br>';

        }
        echo "</div>";
        echo div('header');
        //echo " <div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?exit'> Покинуть бой </span></span></div> </a>";
        echo "</div>";
        require_once('system/footer.php');

        break;


}

if ($user['level'] < 5) {
    echo div('block_c') . " <img src='/images/fight/logo.jpg'><br>";
    echo " Сражение доступно с 5-го уровня </a>";
    echo "</div>";

    require_once('system/footer.php');
    exit();

}

if ($fights['status'] != 2) {

    switch (isset($_GET['act'])) {
        case 'trade':
            $title = 'Лавка';
            require_once('system/header.php');

            // Сброс усилений, если время кончилось
            // Сила
            if ($fights['BonusStrTime'] < time() && $fights['BonusStrStatus'] == 1) {
                mysql_query('UPDATE `fights` SET `BonusStrStatus` = "0"  WHERE `id_user` = "' . $myID . '"');
                mysql_query('UPDATE `users` SET `str` = "' . ($user['str'] - 3500) . '" WHERE `id` = "' . $myID . '"');
            }

            // Здоровье
            if ($fights['BonusHpTime'] < time() && $fights['BonusHpStatus'] == 1) {
                mysql_query('UPDATE `fights` SET `BonusHpStatus` = "0"  WHERE `id_user` = "' . $myID . '"');
                mysql_query('UPDATE `users` SET `max_health` = "' . ($user['max_health'] - 3500) . '" WHERE `id` = "' . $myID . '"');
            }


            if (isset($_GET['e']) and $fights['BonusStrTime'] < time()) {
                if ($fights['skull'] >= 8000) {
                    //Кампания
                    if ($campaign['id_camp'] == 2) {
                        mysql_query("UPDATE `campaign`  SET  `skull`='" . ($campaign['skull'] + 8000) . "'  WHERE `id_user`='" . $myID . "'");
                    }


                    mysql_query('UPDATE `fights` SET `BonusStrTime` = "' . (time() + (60 * 60 * 24)) . '" , `BonusStrStatus` = "1" , `skull` = "' . ($fights['skull'] - 8000) . '"  WHERE `id_user` = "' . $myID . '"');
                    mysql_query('UPDATE `users` SET `str` = "' . ($user['str'] + 3500) . '" WHERE `id` = "' . $myID . '"');
                    $_SESSION['msg'] = "Бонус силы приобретен!";
                    header("Location: ?$random");
                    exit();
                } else {
                    $_SESSION['msg'] = "Не хватает " . ico('icons', 'skull.png') . " " . (8000 - $fights['skull']) . " черепов";
                    header("Location: /fights/trade/?$random");
                    exit();
                }
            }

            if (isset($_GET['h']) and $fights['BonusHpTime'] < time()) {
                if ($fights['skull'] >= 8000) {

                    //Кампания
                    if ($campaign['id_camp'] == 2) {
                        mysql_query("UPDATE `campaign`  SET  `skull`='" . ($campaign['skull'] + 80000) . "'  WHERE `id_user`='" . $myID . "'");
                    }


                    mysql_query('UPDATE `fights` SET `BonusHpTime` = "' . (time() + 60 * 60 * 24) . '" , `BonusHpStatus` = "1" , `skull` = "' . ($fights['skull'] - 8000) . '"  WHERE `id_user` = "' . $myID . '"');
                    mysql_query('UPDATE `users` SET `max_health` = "' . ($user['max_health'] + 3500) . '" WHERE `id` = "' . $myID . '"');
                    $_SESSION['msg'] = "Бонус  приобретен!";
                    header("Location: ?$random");
                    exit();
                } else {
                    $_SESSION['msg'] = "Не хватает " . ico('icons', 'skull.png') . " " . (8000 - $fights['skull']) . " черепов";
                    header("Location: /fights/trade/?$random");
                    exit();
                }
            }


            echo div('header') . " Черепа: " . ico('icons', 'skull.png') . " $fights[skull] </div>";
            echo div('wrapper center');
            echo div('oh') . " <img src='/images/other/1.png' class='bord'> +3500 к " . ico('icons', 'str.png') . " силе на 24 часов</div>";
            if ($fights['BonusStrTime'] < time()) {
                echo "<br><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?e'>Активировать " . ico('icons', 'skull.png') . " 8000 </div></span></span></a>";
            } else {
                echo "<br><br><br><br><center>" . color('lime') . " Активно <br></font><a href='?'>Осталось еще: <a> " . tl($fights['BonusStrTime'] - time()) . "</a></center>";
            }
            echo "</div>";


            echo div('wrapper center');
            echo div('oh') . " <img src='/images/other/3.png' class='bord'> +3500 к " . ico('icons', 'health.png') . " здоровью на 24 часов</div>";
            if ($fights['BonusHpTime'] < time()) {
                echo "<br><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?h' >Активировать " . ico('icons', 'skull.png') . " 8000</div></span></span> </a>";
            } else {
                echo "<br><br><br><br><center>" . color('lime') . " Активно <br></font><a href='?'>Осталось еще: <a> " . tl($fights['BonusHpTime'] - time()) . "</a></center>";
            }
            echo "</div>";
echo div('header') . " <a href='/fights/'><div class='main_link'><div class='ssilki_dop p_0'><img src='/images/icons/back.png' width ='28px'><span class='cb_ttl'> Вернуться назад  </div></span></span> </div></a>";

            require_once('system/footer.php');


            exit;
            break;
    }


    if (isset($_GET['are'])) {

        if ($fights['skull'] < 9000) {

            $_SESSION['msg'] = "Не хватает " . ico('icons', 'skull.png') . " " . (9000 - $fights['skull']) . " черепов";
            header("Location: /fights/r/?$random");
            exit();
        } else {

            $i = rand(1, 6);
            if ($i == 1) {
                $r = 1;
            } else if ($i == 6) {
                $r = 40;
            } else if ($i == 5) {
                $r = 20;
            } else if ($i == 4) {
                $r = 10;
            } else if ($i == 3) {
                $r = 1;
            } else if ($i == 2) {
                $r = 10;
            }

            $item = mysql_fetch_assoc(mysql_query("SELECT * FROM `item` WHERE `level` = '$r' ORDER BY RAND() LIMIT 1"));
            mysql_query('UPDATE `fights` SET  `skull` = "' . ($fights['skull'] - 9000) . '"  WHERE `id_user` = "' . $myID . '"');

            //Кампания
            if ($campaign['id_camp'] == 2) {
                mysql_query("UPDATE `campaign`  SET  `skull`='" . ($campaign['skull'] + 9000) . "'  WHERE `id_user`='" . $myID . "'");
            }


            mysql_query("INSERT INTO `inv` SET `id_user` = '" . ($user['id']) . "', `name` = '$item[name]', `str` = '$item[str]', `def` = '$item[def]', `health` = '$item[health]', `tip` = '$item[tip]', `skill` = '$item[skill]',  `level` = '$item[level]',`odeta` = 'net' , `item` = '$item[item]'") or die(mysql_error());

            $_SESSION['item'] = "<a>Новая вещь!</a><br><font color=grey>$item[name]</font><br>
<img src='/images/items/$item[tip]/$item[item].png'>";
            header("Location: /fights/r/?$random");
            exit();

        }

    }


    switch (isset($_GET['item'])) {
        case 'items':
            $title = 'Лавка';
            require_once('system/header.php');
            echo div('header') . " Черепа: " . ico('icons', 'skull.png') . " $fights[skull] </div>";
            echo div('header') . "" . span('info') . " Испытай удачу - получи вещь высокого качества!</span></div>";
            echo div('wrapper center');
            echo "<img src='/images/other/chest.png' width='120px' style='opacity: 0.3;'> <br>Случайное снаряжение<br><br>";

            echo "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/fights/?are'> " . span('grey') . " Испытать удачу  " . ico('icons', 'skull.png') . " 9000 </div></span></span></a>";
            echo "</div>";

            echo div('header') . " <a href='/fights/'><div class='main_link'><div class='ssilki_dop p_0'><img src='/images/icons/back.png' width ='28px'><span class='cb_ttl'> Вернуться назад  </div></span></span> </div></a>";
            require_once('system/footer.php');
            break;
    }


    echo div('header') . " <img src='/images/fight/logo.jpg'></div>";
    echo div('header') . " <div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?start'> Войти на поле битвы </span></span></div> </a>";
    echo "</div>";
    echo div('wrapper center');

    echo "<table style='width:100%;'>";

    echo "<td class='center'> <li> <div class='btn_grey'><span class='cb_end'><span class='cb_ttl'> <a  href='/fights/trade/'> " . span('gold') . "    Эффекты </span></span></div></span></a></li></td>";
    echo "<td class='center'><li> <div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a  href='/fights/r/'> <span class='gold center'>Испытать удачу </span></span></div></span></a></li></td>";

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


}



        
        
        
