<?php
define('PROTECTOR', 1);
include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
if(isset($duel['status'])){
if($duel['status']=="begin"){
$header=TRUE;
}
}else{
$textl='Стрелка';
}
include($path.'files/core.php');
$limit=time()-300;
mysql_query("DELETE FROM `duel` WHERE `time` <= '$limit'");
mysql_query("DELETE FROM `userduel` WHERE `time` <= '$limit'");
mysql_query("DELETE FROM `duellog` WHERE `usr` = '$log' or `opon`='$log'");
$req = mysql_query("SELECT `duid` FROM `userduel` WHERE `usr` = '$log' LIMIT 1");
$duel = mysql_fetch_array($req);
$redu = mysql_query("SELECT * FROM `duel` WHERE `id` = '$duel[duid]' LIMIT 1");
$dued = mysql_fetch_array($redu);
if($dued['status']=="begin"){
header("Location: fiting.php?");exit;
}
include($path.'files/head.php');
include($path.'files/zag.php');
if($udata['lvl']<5){
echo'<div class="main"><div class="block_zero center"><img src="/image/!.png" alt=""/> Доступно с 5-го уровня!</div></div>';
include($path.'files/down.php');
exit;
}
$time=time();
switch($_GET['mod']){
default:
$rand=rand(1000000,10000000);
if(empty($_GET['cur'])){
$_GET['cur']="dost";
}
echo'<div class="main"><div class="mini-line"><div class="block_zero center"><img src="image/logo/coliseum.png" alt="logo"></div><div class="block_zero center"><span style="color: #9bc;">Стрелка - это увлекательное сражение двух команд: Красных и Синих. Раcставь все точки над "И" и покажи на что ты способен</div>';
echo'</div><div class="dot-line"></div><div class="block_zero center">';

echo'<a href="duel.php?cur='.$_GET['cur'].'&reload='.$rand.'"><span class="btn"><span class="end"><input class="label" type="submit" value="Обновить"></span></span></a><br>';

if($_GET['cur']=='dost'){
$asg = mysql_query("SELECT * FROM duel WHERE minlvl <= '$udata[lvl]' and maxlvl >= '$udata[lvl]' AND status='sob' LIMIT 1");
}

$reg=mysql_num_rows($asg);
if($reg==0){
echo'<img src="image/icon/0.png" alt="*"/><img src="image/icon/1.png" alt="*"/> Заключенные: (0/6)';
echo'</div><div class="dot-line"></div><div class="block_zero center">';
}else{
While($duel = mysql_fetch_array($asg)){

echo'<img src="image/icon/0.png" alt="*"/><img src="image/icon/1.png" alt="*"/> Заключенные: ('.$duel['zav'].'/'.$duel['zavmax'].')';
echo'</div><div class="dot-line"></div><div class="block_zero center">';

if($udata['storona']=='white'){
$pic='1';
}else{
$pic='0';
}
//echo'<img src="image/icon/'.$pic.'.png" alt="*"/><a href="search.php?nick='.$duel['usr'].'&go=go"> '.$duel['usr'].' </a><br>';

if($duel['time']<=time()){
mysql_query("UPDATE `duel` SET `status` = 'begin' WHERE `status` = 'sob' AND `id`='$duel[id]'");
header("Location: duel.php?");exit;
}


if($reg!==0){
if($duel['status']=='sob'){
$vremya1=$duel['time']-$time;
echo'Начало стрелы через: '.Sec2Time($vremya1).'<br>';
}
}

$userduel=mysql_fetch_array(mysql_query("SELECT `duid` FROM `userduel` WHERE `usr` = '$log' LIMIT 1"));

if($userduel['duid']==$duel['id']){
echo'<a href="duel.php?mod=pobeg&id='.$duel['id'].'"><span class="btn"><span class="end"><input class="label" type="submit" value="Выйти"></span></span></a>';
}else{
if($duel['status']=='sob'){
echo'<a href="duel.php?mod=reg&id='.$duel['id'].'"><span class="btn"><span class="end"><input class="label" type="submit" value="Участвовать"></span></span></a>';
}

}

}

}


if($reg==0){
echo'<a href="duel.php?mod=create"><span class="btn"><span class="end"><input class="label" type="submit" value="Участвовать"></span></span></a>';
}
echo'</div><div class="mini-line"></div><div class="main"><ul class="hint"><li>Стрела идет до последнего выжившего или до окончания отведенного времени</li></ul>';

//echo"</div><div class='line'></div>";
echo'</div></div>';
//exit();
break;

case 'null':
if($reg==0){
header('Location: duel.php?mod=create');exit();
}
break;

case 'create':
if($dueli==1){
header('Location: duel.php?');exit();
}else{
$_POST['level']=70;
$_POST['count']=6;
$_POST['start']=1;
$min=$udata['lvl']-$_POST['level'];
if($min<0){
$min=1;
}
$max=$udata['lvl']+$_POST['level'];
if($max>70){
$max=70;
}
$time=$time+$_POST['start']*240;
mysql_query("INSERT INTO
        `duel` SET
        `usr` = '$log',
        `minlvl` = '$min',
        `maxlvl` = '$max',
        `status` = 'sob',
        `zav` = '1',
        `time` = '$time',
        `zavmax` = '$_POST[count]'");
        $userduel=mysql_fetch_array(mysql_query("SELECT * FROM `duel` WHERE `usr` = '$log' and `status`='sob' LIMIT 1"));
mysql_query("INSERT INTO
        `userduel` SET
        `usr` = '$log',
        `duid` = '$userduel[id]',
        `lvl` = '$udata[lvl]',
        `hp` = '$udata[hp]',
        `status` = 'active',
        `time` = '$time',
        `storona` = '$udata[storona]' ");
        header('Location: duel.php?');exit();
}
break;

case 'reg':
if($dueli==1){
header('Location: duel.php?');exit();
break;
}
$id = $_GET['id'];
$req=mysql_query("SELECT `storona` FROM `userduel` WHERE `duid` = '".mysql_real_escape_string($id)."' ORDER BY `time` DESC LIMIT 1");
$storm=mysql_fetch_array($req);
if($storm['storona']=="white"){
$st="black";
}else{
$st="white";
}
$userduel=mysql_query("SELECT * FROM `duel` WHERE `id` = '".mysql_real_escape_string($id)."' AND `status`='sob' LIMIT 1");
$avt=mysql_num_rows($userduel);
if($avt==0){
header('Location: duel.php?');exit();
break;
}
$userduel=mysql_fetch_array($userduel);
if($userduel['zav']==$userduel['zavmax']){
header('Location: duel.php?');exit();
break;
}
if($userduel['minlvl']>$udata['lvl']){
header('Location: duel.php?');exit();
}
if($userduel['maxlvl']<$udata['lvl']){
header('Location: duel.php?');exit();
break;
}
mysql_query("INSERT INTO
        `userduel` SET
        `usr` = '$log',
        `duid` = '".mysql_real_escape_string($id)."',
        `lvl` = '$udata[lvl]',
        `hp` = '$udata[hp]',
        `time` = '$time',
        `status` = 'active',
        `storona` = '$st' ");
mysql_query("UPDATE `duel` SET `zav` = '$userduel[zav]'+'1' WHERE `status` = 'sob' AND `id`='".mysql_real_escape_string($id)."'");
header('Location: duel.php?');exit();
break;

case 'pobeg':
if($dueli==1){
$id = $_GET['id'];
$userduel=mysql_query("SELECT * FROM `duel` WHERE `id` = '".mysql_real_escape_string($id)."' AND `usr`='$log' LIMIT 1");
$avt=mysql_num_rows($userduel);
$userduel=mysql_fetch_array($userduel);
if($avt==1){
mysql_query("DELETE FROM `duel` WHERE `usr`='$log'");
mysql_query("DELETE FROM `userduel` WHERE `duid`='".mysql_real_escape_string($id)."'");
}
mysql_query("DELETE FROM `userduel` WHERE `usr`='$log' AND `duid`='".mysql_real_escape_string($id)."'");
mysql_query("UPDATE `duel` SET `zav` = `zav`-'1' WHERE `status` = 'sob' AND `id`='".mysql_real_escape_string($id)."'");
mysql_query("UPDATE `users` SET `predatel` = `predatel`+'1' WHERE `usr` = '$log'");
header('Location: duel.php?');exit();
}else{
echo"Какая-то ошибка";
}
break;

}
include($path.'files/down.php');
?>