<?
include './system/common.php';
include './system/functions.php';
include './system/user.php';
if(!$user) {
header('location: /');
exit;
}
$id = _string(_num($_GET['id']));
if(!$id && $clan) {
    $id = $clan['id'];
}
$i = mysql_fetch_array(mysql_query('SELECT * FROM `clans` WHERE `id` = "'.$id.'" LIMIT 1'));


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

if(!$i == $membb['clan']) {
header('location: /clans/');
exit;
}

	if(!$i) {
	header('location: /clans/');
	exit;
	}
	$title = 'Клановые задания';    
include './system/h.php';


if($id !== $membb['clan']) {
header('location: /clans/');
exit;
}

if(date('w') != 0 AND date('w') != 6){
	$clans_q = mysql_fetch_array(mysql_query('SELECT * FROM `clans_q` WHERE `clans` = "'.$id.'" LIMIT 1'));
	if(!$clans_q){
	mysql_query('INSERT INTO `clans_q` SET `clans` = "'.$id.'"');
	mysql_query('UPDATE `clan_memb` SET `plat` = "0" WHERE `clan` = "'.$id.'"');
	mysql_query('UPDATE `clans` SET `plat` = "0" WHERE `id` = "'.$id.'"');
	header('location: /clan/quest/'.$id.'/');
	}
if (isset($_SESSION['ok'])){
echo "<div class='ok'><center><img src='/images/icon/ok.png'> ".$_SESSION['ok']."</center></div>";
$_SESSION['ok']=NULL;
}
if (isset($_SESSION['err'])){
echo "<div class='ok'><center><img src='/images/icon/error.png'> ".$_SESSION['err']."</center></div>";
$_SESSION['err']=NULL;
}
echo "</br><center><span style='color: #90b0c0;'> Выполняй задания и добывай кристаллы для клана!</span></br>
Добыто кристаллов:<img src='/images/icon/crys/diamond.png' alt=''> ".($clan['plat'])." </center></br>";
$locainfo = array("","<b><span class='dgreen'><img src='/images/icon/quest.png'> Воин арены</span></b></br>Проведи 1000 боев на арене.</br><span style='color: #90b0c0;'>Прогресс:</span> ".$clans_q['q_1']." из 1000",
						"<b><span class='dgreen'><img src='/images/icon/quest.png'> Легендарный воин арены</span></b></br>Проведи 55 боев на арене.</br><span style='color: #90b0c0;'>Прогресс:</span> ".$clans_q['q_2']." из 55",
						"<b><span class='dgreen'><img src='/images/icon/quest.png'> Гладиатор</span></b></br>Проведи 30 боев в лиге.</br><span style='color: #90b0c0;'>Прогресс:</span> ".$clans_q['q_3']." из 30",
						"<b><span class='dgreen'><img src='/images/icon/quest.png'> Легендарный гладиатор</span></b></br>Проведи 20 боев в лиге.</br><span style='color: #90b0c0;'>Прогресс:</span> ".$clans_q['q_4']." из 20",
						"<b><span class='dgreen'><img src='/images/icon/quest.png'> Поиск ресурсов</span></b></br>Проведи 15 поисков в пещере.</br><span style='color: #90b0c0;'>Прогресс:</span> ".$clans_q['q_5']." из 15",
						"<b><span class='dgreen'><img src='/images/icon/quest.png'> Дуэльный мастер</span></b></br>Проведи 20 боев в дуэлях.</br><span style='color: #90b0c0;'>Прогресс:</span> ".$clans_q['q_6']." из 20");//наш прогрес
$locainfo_q = array("","1000","55","30","20","15","20");//сколько осталось
$self = array("","arena","arena","league","league","cave","duel");//ссылки на локации
$locainfo_end = array("","Проведи 1000 боев на арене.</br>Будет тоступно через</br>"._time($clans_q['1_time'] - time())."",
						"Проведи 55 боев на арене.</br>Будет тоступно через</br>"._time($clans_q['2_time'] - time())."",
						"Проведи 30 боев в лиге.</br>Будет тоступно через</br>"._time($clans_q['3_time'] - time())."",
						"Проведи 20 боев в лиге.</br>Будет тоступно через</br>"._time($clans_q['4_time'] - time())."",
						"Проведи 15 поисков в пещере.</br>Будет тоступно через</br>"._time($clans_q['5_time'] - time())."",
						"Проведи 20 боев в дуэлях.</br>Будет тоступно через</br>"._time($clans_q['6_time'] - time())."");//время до обнуления
$gold = array("","100","100","105","250","50","200"); //золото награда
$silver = array("","100","100","150","250","50"); //серебро 
$exp = array("","1500","1500","2500","3500","1000","2000"); //опыт
$kristall = rand(1,10);
	for($a = 1; $a < 7; $a++){
	if(isset($_GET['go'])){
		
	$go = abs(intval($_GET['go']));
	if($go == $a){
		if($clans_q['user_'.$a] != 0) {
			$_SESSION['err'] = "Нельзя";
	header('location:/clan/'.$id.'/quest/');
			exit;
		}
		if($clans_q[$a.'_time'] != 0) {
			$_SESSION['err'] = "Нельзя";
	header('location:/clan/'.$id.'/quest/');
			exit;
		}
	mysql_query('UPDATE `clans_q` SET `user_'.$a.'` = "'.$user['id'].'" WHERE `clans` = "'.$id.'"');
	mysql_query('INSERT INTO `clan_q_user` SET `clans` = "'.$id.'", `user` = "'.$user['id'].'", `time` = "'.(time() + 86400 * 2).'"');
	//время 2 дня на выполнение 
	header("location:/".$self[$a]."/");
	}
	}
	if(isset($_GET['go2'])){
	$go = abs(intval($_GET['go2']));
	if($go == $a){
		if ($clans_q['user_'.$a.'_p'] != 0) {
			$_SESSION['err'] = "Нельзя";
	header('location:/clan/task/'.$id.'/');
			exit;
		}
	mysql_query('UPDATE `clans_q` SET `user_'.$a.'_p` = "'.$user['id'].'" WHERE `clans` = "'.$id.'"');
	header("location:/".$self[$a]."/");
	}
	}
	$clans_user = mysql_fetch_array(mysql_query('SELECT * FROM `users` WHERE `id` = "'.$clans_q['user_'.$a].'" LIMIT 1'));
	$clans_user_p = mysql_fetch_array(mysql_query('SELECT * FROM `users` WHERE `id` = "'.$clans_q['user_'.$a.'_p'].'" LIMIT 1'));
	if(isset($_GET['end'])){
		
	$end = abs(intval($_GET['end']));
	if($end == $a){
		if ($clans_q['user_'.$a] != $user['id']) {
			$_SESSION['err'] = "Нельзя";
	header('location:/clan/task/'.$id.'/');
			exit;
		}
	mysql_query('UPDATE `clans_q` SET `user_'.$a.'` = "0", `user_'.$a.'_p` = "0", `q_'.$a.'` = "0" WHERE `clans` = "'.$id.'"');
	mysql_query('DELETE FROM `clan_q_user` WHERE `clans` = "'.$id.'" AND `user` = "'.$clans_q['user_'.$a].'"');
	header('location:/clan/task/'.$id.'/');
	}
	}
	if(isset($_GET['end2'])){
		
	$end = abs(intval($_GET['end2']));
	if($end == $a){
		if ($clans_q['user_'.$a.'_p'] != $user['id']) {
			$_SESSION['err'] = "Нельзя";
	header('location:/clan/'.$id.'/quest/');
			exit;
		}
	mysql_query('UPDATE `clans_q` SET  `user_'.$a.'_p` = "0" WHERE `user_'.$a.'_p` = "'.$user['id'].'"');
	mysql_query('DELETE FROM `clan_q_user` WHERE `clans` = "'.$id.'" AND `user` = "'.$clans_q['user_'.$a.'_p'].'"');
	header('location:/clan/'.$id.'/quest/');
	}
	}
	if(isset($_GET['enter'])){
	$enter = abs(intval($_GET['enter']));
	if($enter == $a){
		if ($clans_q['user_'.$a] != $user['id']) {
			$_SESSION['err'] = "Нельзя";
	header('location:/clan/'.$id.'/quest/');
			exit;
		}
	if($clans_q['user_'.$a.'_p'] == 0){
	$text = "[img=/images/icon/race/".$user['r'].".png][url=/user/".$user['id']."/]".$user['login']."[/url] добыл для клана [img=/images/icon/crys/diamond.png] ".$kristall."  кристаллов";
	}else{
	$text = "[img=/images/icon/race/".$clans_user_p['r'].".png] [url=/user/".$clans_user_p['id']."]".$clans_user_p['login']."[/url] и [img=/images/icon/race/".$clans_user_p['r'].".png] [url=/user/".$clans_user_p['id']."/]".$user['login']."[/url] добыли для клана [img=/images/icon/crys/diamond.png] ".$kristall."  кристаллов";
	}
	mysql_query("INSERT INTO `chat` (`clan`, `user`,`text`, `time`) VALUES ('".$id."', '2','".$text."','".time()."')"); 
	mysql_query('UPDATE `clans_q` SET `user_'.$a.'` = "0", `user_'.$a.'_p` = "0", `q_'.$a.'` = "0", `'.$a.'_time` = "'.(time() + 3600 * 20).'" WHERE `clans` = "'.$id.'"');
	mysql_query('UPDATE `users` SET `g` = `g` + "'.$gold[$a].'", `exp` = `exp` + "'.$exp[$a].'" WHERE `id` = "'.$user['id'].'"');
	mysql_query('UPDATE `clans` SET `plat` = `plat` + "'.$kristall.'" WHERE `id` = "'.$id.'"');
	mysql_query('UPDATE `clan_memb` SET `plat` = `plat` + "'.$kristall.'" WHERE `user` = "'.$user['id'].'"');
	mysql_query('DELETE FROM `clan_q_user` WHERE `clans` = "'.$id.'" AND `user` = "'.$clans_q['user_'.$a].'"');
	$_SESSION['ok'] = "Награда: <img src='/images/icon/gold.png' alt='*'/> ".$gold[$a]." <img src='/images/icon/exp.png' alt='*'/> ".$exp[$a]." <img src='/images/icon/crys/diamond.png' alt='*'/> ".$kristall."";
	header('location:/clan/'.$id.'/quest/');
	}
	}
	$progres = round(($clans_q['q_'.$a]/$locainfo_q[$a])*100);
	$q = mysql_num_rows(mysql_query ('SELECT * FROM `clan_q_user` WHERE `user` = "'.$user['id'].'" AND `clans` = "'.$id.'"'));
	$time_user = mysql_fetch_array(mysql_query ('SELECT * FROM `clan_q_user` WHERE `user` = "'.$clans_q['user_'.$a].'" AND `clans` = "'.$id.'"'));
	if($clans_q[$a.'_time'] < time() AND $clans_q[$a.'_time'] != 0){
	mysql_query('UPDATE `clans_q` SET `'.$a.'_time` = "0" WHERE `clans` = "'.$id.'"');
	header('location: /clan/'.$id.'/quest/');
	}
	if($time_user['time'] < time() AND $time_user['time'] != 0){
	mysql_query('DELETE FROM `clan_q_user` WHERE `clans` = "'.$id.'" AND `user` = "'.$clans_q['user_'.$a].'"');
	mysql_query('UPDATE `clans_q` SET `user_'.$a.'` = "0", `user_'.$a.'_p` = "0", `q_'.$a.'` = "0" WHERE `clans` = "'.$id.'"');
	header('location: /clan/'.$id.'/quest/');
	}
	if($clans_q['q_'.$a] > $locainfo_q[$a]){
	mysql_query('UPDATE `clans_q` SET `q_'.$a.'` = "'.$locainfo_q[$a].'" WHERE `clans` = "'.$id.'"');
	header('location: /clan/'.$id.'/quest/');
	}
    if ($q!=0) {
echo "".($clans_q['user_'.$a] == $user['id']) ? "
<div class='mini-line'></div>".$locainfo[$a]."</br><div style='border:1px solid #000; background: #ffffff; height: 4px; width:40%'><div style='background: #fc3; height: 4px; width: ".$progres."%'></div></div><span style='color: #90b0c0;'>Награда:</span> <img src='/images/icon/gold.png' alt='*'/> ".$gold[$a]." <img src='/images/icon/exp.png' alt='*'/> ".$exp[$a]."</br>
Выполняет: <img src='/images/icon/race/".$clans_user['r'].($clans_user['online'] > time() - 300 ? "":"-off").".png' alt='*'/> <a href='/user/".$clans_user['id']."/'>".$clans_user['login']."</a>
".($clans_q['user_'.$a.'_p'] != 0 ? "<img src='/images/icon/race/".$clans_user_p['r'].($clans_user_p['online'] > time() - 300 ? "":"-off").".png' alt=''/> <a href='/user/".$clans_user_p['id']."/'>".$clans_user_p['login']."</a>":"")."
(осталось: ".(_time($time_user['time'] - time())).")
 ".($clans_q['q_'.$a] == $locainfo_q[$a] ? "<br/><center>Задание выполнено</center><br/><center><a class='btn' href='?enter=".$a."'><span class='end'><span class='label'>Завершить</span></span></center></a>":" <br/><center><a class='btn' href=/".$self[$a]."/><span class='end'><span class='label'>Продолжить</span></span></center></a><br/><center><a class='btn' href='?end=".$a."'><span class='end'><span class='label'>Отменить</span></span></center></a>")."
 ":
 "".($clans_q[$a.'_time'] ==0 ? "<div class='mini-line'></div>".$locainfo[$a]."</br><div style='border:1px solid #000; background: #ffffff; height: 4px; width:40%'><div style='background: #fc3; height: 4px; width: ".$progres."%'></div></div>
 <span style='color: #90b0c0;'>Награда:</span> <img src='/images/icon/gold.png' alt='*'/> ".$gold[$a]." <img src='/images/icon/exp.png' alt='*'/> ".$exp[$a]."</br>
":"")." ".($clans_q['user_'.$a.'_p'] != 0 ? "Выполняет: <img src='/images/icon/race/".$clans_user['r'].($clans_user['online'] > time() - 300 ? "":"-off").".png' alt='*'/> <a href='/user/".$clans_user['id']."/'>".$clans_user['login']."</a> <img src='/images/icon/race/".$clans_user_p['r'].($clans_user_p['online'] > time() - 300 ? "":"-off").".png' alt=''/> <a href='/user/".$clans_user_p['id']."/'>".$clans_user_p['login']."</a> (осталось: ".(tl($time_user['time'] - time())).")<br/><br/><center><a class='btn' href='/".$self[$a]."/'><span class='end'><span class='label'>Продолжить</span></span></a></center>":" ")."";
	}else{	
echo "".($clans_q['user_'.$a] != 0) ? "<div class='mini-line'></div>".$locainfo[$a]."</br><div style='border:1px solid #000; background: #ffffff; height: 4px; width:40%'><div style='background: #fc3; height: 4px; width: ".$progres."%'></div></div>
<span style='color: #90b0c0;'>Награда:</span> <img src='/images/icon/gold.png' alt='*'/> ".$gold[$a]." <img src='/images/icon/exp.png' alt='*'/> ".$exp[$a]."</br>
Выполняет: <img src='/images/icon/race/".$clans_user['r'].($clans_user['online'] > time() - 300 ? "":"-off").".png' alt='*'/> <a href='/user/".$clans_user['id']."/'>".$clans_user['login']."</a> 
".($clans_q['user_'.$a.'_p'] != 0 ? "<img src='/images/icon/race/".$clans_user_p['r'].($clans_user_p['online'] > time() - 300 ? "":"-off").".png' alt=''/> <a href='/user/".$clans_user_p['id']."/'>".$clans_user_p['login']."</a> " :"")." (осталось: ".(tl($time_user['time'] - time())).")<br/><br/>".($clans_q['q_'.$a] == $locainfo_q[$a] ? "<center>Задание выполнено</center>":" ".($clans_q['user_'.$a.'_p'] != 0 ? "<center><a class='btn' href='/".$self[$a]."/'><span class='end'><span class='label'>Продолжить</span></span></center></a></br><center><a class='btn' href='?end2=".$a."'><span class='end'><span class='label'>Отменить</span></span></center></a></br>":"<center><a class='btn' href='?go2=".$a."'><span class='end'><span class='label'>Помочь</span></span></center></a></br>")."")."":"".($clans_q[$a.'_time'] ==0 ? "<div class='mini-line'></div>".$locainfo[$a]."</br><div style='border:1px solid #000; background: #ffffff; height: 4px; width:40%'>
<div style='background: #fc3; height: 4px; width: ".$progres."%'></div></div>
<span style='color: #90b0c0;'>Награда:</span> <img src='/images/icon/gold.png' alt='*'/> ".$gold[$a]." <img src='/images/icon/exp.png' alt='*'/> ".$exp[$a]."</br>
<center><a class='btn' href='?go=".$a."'><span class='end'><span class='label'>Выполнить</span></span></center></a>":"")."";
	}
    }
	for($a = 1; $a < 7; $a++){	
	echo "".($clans_q[$a.'_time'] != 0 ? "<div class='mini-line'></div><span style='color: #999999; font-size: 12px;'>".$locainfo_end[$a]."</br></span>": "")."";
	}
}else{
$i_user = 0;
$q = mysql_query('SELECT * FROM `clan_memb` WHERE `plat` > "0"  AND `clan` = "'.$id.'" ORDER BY `plat` DESC');
while($pomog = mysql_fetch_array($q)){
$clans_user = mysql_fetch_array(mysql_query('SELECT * FROM `users` WHERE `id` = "'.$pomog['user'].'"'));
$i_user++;
$messages_user_dmg.="".$i_user.". <img src='/images/icon/race/".$clans_user['r'].($clans_user['online'] > time() - 300 ? "":"-off").".png' alt='*'/> <a href='/user/".$clans_user['id']."/'>".$clans_user['login']."</a>  <img src='/images/icon/crys/diamond.png' alt='*'/> ".$pomog['plat']."</br>";
}
$gold = ($i['plat'] * 20);
$silver = ($i['plat'] * 200);
echo'<div class="resours"><center><b>Помогли клану:</b><br>';	
if($clans_user == 0) echo "Нет не кого";
echo "Всего клан зароботал <img src='/images/icon/crys/diamond.png' alt='*'/> ".$i['plat']."</br>";
echo "Награда <img src='/images/icon/gold.png' alt='*'/> ".$gold." <img src='/images/icon/silver.png' alt='*'/> ".$silver."</br>";				
echo''.$messages_user_dmg.'</center></div>';
}
include './system/f.php';
?>