<?php
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/adm_check.php';
include_once '../sys/inc/user.php';
$set['title']='Рыбалка онлайн';
include_once '../sys/inc/thead.php';
title();
aut();

if(!isset($user)){
  header("Location:/index.php");
  exit;
}





include_once 'inc/start.php';
$posts=array();
$id=abs(intval($_GET['id']));
$query=mysql_query("SELECT * FROM `fish_cup` WHERE `id`='".$id."'");
if(mysql_num_rows($query)==0)
$posts[]=array('title'=>'Ошибка! Такого турнира нет!');
else
{
$arr=mysql_fetch_array($query);
if($arr['date_start']<time() && $arr['date_end']>time()){
$yc=mysql_result(mysql_query("SELECT COUNT(*) FROM `fish_yc` WHERE `id_cup`='".$arr['id']."'"),0);
if($yc<1){
mysql_query("DELETE FROM `fish_yc` WHERE `id_cup`='".$arr['id']."'");
mysql_query("DELETE FROM `fish_cup` WHERE `id`='".$arr['id']."'");
header("location:cup.php");
exit;
}
$ycc=mysql_result(mysql_query("SELECT COUNT(*) FROM `fish_yc` WHERE `id_cup`='".$arr['id']."' AND `id_u`='".$user_id."'"),0);
if($ycc!=0){
$y=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='y'"),0);
$l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='l'"),0);
$k=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='k'"),0);
$n=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='n'"),0);
$p=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='p'"),0);
$nap=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);
$t=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);

if($t!=0){
$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='t'"),0);
$t_ves=mysql_result(mysql_query("SELECT `yd` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);
}
else
$t_ves=0;
if($nap!=0){
$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);
$nap_ves=mysql_result(mysql_query("SELECT `yd` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);
}
else
$nap_ves=0;
if($p!=0){
$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='p'"),0);
$p_ves=mysql_result(mysql_query("SELECT `yd` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);
}
else
$p_ves=0;


$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='y'"),0);
$y_ves=mysql_result(mysql_query("SELECT `ves` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);

$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='l'"),0);
$l_ves=mysql_result(mysql_query("SELECT `ves` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);

$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='k'"),0);
$k_ves=mysql_result(mysql_query("SELECT `ves` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);


$y_l=mysql_result(mysql_query("SELECT `id_i` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='n'"),0);
$n_ves=mysql_result(mysql_query("SELECT `ves` FROM `fish_pod` WHERE `id`='".$y_l."'"),0);





if($y==0 || $l==0 || $k==0 || $n==0 || $fish['energy']<=0){
$posts[]=array('title'=>'Ошибкa!');
if($y==0)
$posts[]=array('title'=>'У вас нет удочки!');
if($l==0)
$posts[]=array('title'=>'У вас нет лески!');
if($k==0)
$posts[]=array('title'=>'У вас нет крючка!');
if($fish['energy']<=0)
$posts[]=array('title'=>'У вас нехватает энергии! Съеште что-нибудь, или подождите '.TimeSpent($fish["last_update"]-(time()-60*10)).'!');
if($n==0)
$posts[]=array('title'=>'У вас нет наживки!');
}
else
{
$fishes=mysql_fetch_array(mysql_query("SELECT * FROM `fish_yc` WHERE `id_cup`='".$id."' AND `id_u`='".$user_id."'"));
$posts[]=array('title'=>'~~<a href="cup_id.php?refresh&amp;id='.$id.'">Закинуть удочку</a>~~');
if(isset($_GET['refresh'])){
$rand=rand($fish['y'],100);
if($rand<98){
$quite=mysql_fetch_array(mysql_query("SELECT * FROM `fish` WHERE `level`<='".$fish['level']."' ORDER BY RAND() DESC LIMIT 1"));
$mas=rand(1,10);
if($rand<5)
$mas1=$quite['min_ves'];
else
$mas1=$quite['min_ves']+(($quite['min_ves']/100)*($mas*10));

if(is_double($mas1)){
$rand=rand(1,9);
$mas1=$mas1.$rand;
}
$rand=rand(1,2);
$sr=($y_ves+$l_ves+$n_ves+$k_ves)/4;

if($mas1>$sr && $rand==2 && $quite['level']!=40)
{
$posts[]=array('title'=>'Вы упустили рыбу<br/>','post'=>'Вы получаете + 1 опыта');
mysql_query("UPDATE `fish_u` SET `last_update`='".time()."', `energy`=`energy`-1,`exp`=`exp`+1 WHERE `id_u`='".$user_id."'");
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$l."'");
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$n."'");
}else{
if($p_ves!=0){
$mas1=$mas1+(($mas1/100)*$p_ves);
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$p."'");
}
if($nap_ves!=0){
$mas1=$mas1+(($mas1/100)*$nap_ves);
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$nap."'");
}
if($t_ves!=0){
$mas1=$mas1+(($mas1/100)*$t_ves);
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$t."'");
}
$posts[]=array('icon'=>'<img src="f/'.$quite["id"].'.png"/>','title'=>'Вы поймали рыбу '.$quite["name"].'<br/>','post'=>'Ёе вес составил '.$mas1.' кг . Вы получаете + '.$quite['cost'].' денег и+ '.$quite['exp'].' опыта');
if($mas1>$fish['max_ves'])
mysql_query("UPDATE `fish_u` SET `max_ves`='".$mas1."',`max_kogo`='".$quite['name']."' WHERE `id_u`='".$user_id."'");
if($mas1>$fishes['max_ves']){
mysql_query("UPDATE `fish_yc` SET `max_ves`='".$mas1."', `max_kogo`='".$quite['name']."' WHERE `id_cup`='".$id."' AND `id_u`='".$user_id."'");
}

mysql_query("UPDATE `fish_u` SET `last_update`='".time()."', `energy`=`energy`-1,`exp`=`exp`+$quite[exp], `balls`=`balls`+$quite[cost] WHERE `id_u`='".$user_id."'");
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$l."'");
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$n."'");
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `fish_ulow` WHERE `id_u`='".$user_id."' AND `id_r`='".$quite['id']."'"),0)==0)
mysql_query("INSERT INTO `fish_ulow`(`id_u`,`id_r`,`ves`,`kolvo`) VALUES('".$user_id."','".$quite['id']."','".$mas1."','1')");
else
mysql_query("UPDATE `fish_ulow` SET `ves`=`ves`+$mas1,`kolvo`=`kolvo`+1 WHERE `id_u`='".$user_id."' AND `id_r`='".$quite['id']."'");
}
}
else
{
$posts[]=array('icon'=>'<img src="f/z.png"/>','title'=>'Вы поймали золотую рыбку<br/>','post'=>'Вы получаете + 300  денег и 10 рубинов');

mysql_query("UPDATE `fish_u` SET `last_update`='".time()."', `energy`=`energy`-1,`rub`=`rub`+10, `balls`=`balls`+300 WHERE `id_u`='".$user_id."'");
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$l."'");
mysql_query("UPDATE `fish_i` SET `kolvo`=`kolvo`-1 WHERE `id`='".$n."'");

}
}
}



}
$posts[]=array('icon'=>'<img src="cup.png"/>','title'=>'<a href="cup_table.php?id='.$id.'">Турнирная таблица</a><br/>
До конец : '.vremja($arr["date_end"]).' 
');
}
elseif($arr['date_end']<time())
{
$win=mysql_fetch_array(mysql_query("SELECT * FROM `fish_yc` WHERE `max_ves`!='0' ORDER BY `max_ves` DESC LIMIT 1"));
$ank=new_user($win['id_u']);
$posts[]=array('icon'=>'<img src="cup.png"/>',
'title'=>'Турнир окончен! Победитель - '.$ank['nick'].'<br/>
<a href="cup_table.php?id='.$id.'">Турнирная таблица</a>
');
if($arr['act']==0){
mysql_query("UPDATE `fish_cup` SET `act`='1' WHERE `id`='".$id."'"); 
mysql_query("UPDATE `fish_u` SET `win`=`win`+1, `balls`=`balls`+$act[priz] WHERE `id_u`='".$ank['id']."'");
}
}
else
{
if(!isset($_GET['y'])){
$posts[]=array('title'=>'Местное время : '.date("d.m.y | H:i:s").'');
$posts[]=array('icon'=>'<img src="cup.png"/>','title'=>'<b>'.output_text($arr["name"]).'</b><br/>',
'post'=>output_text($arr["rules"]).'<br/> Призовой фонд: '.$arr["priz"].' <img src="d.png" /><br/>Статус :
'.($arr['act']==0 ? 'Открыт' : 'Завершен').'<br/>
Начало : '.date("d.m.y в H:i:s",$arr['date_start']).'<br/>
Конец : '.date("d.m.y в H:i:s",$arr['date_end']).'<br/>
Минимальный уровень участника : '.$arr["min_level"].'<br/>
Максимальный уровень участника : '.$arr["max_level"].'
');
$result=mysql_result(mysql_query("SELECT COUNT(*) FROM `fish_yc` WHERE `id_cup`='".$arr['id']."' AND `id_u`='".$user_id."'"),0);
if($result==0){
if(isset($_GET['in'])){
if($fish['level']>=$arr['min_level'] && $fish['level']<=$arr['max_level']){
mysql_query("INSERT INTO `fish_yc`(`id_cup`,`id_u`) VALUES('".$id."','".$user_id."')");
mes('Вы допущены к участию в этом турнире');
}
else
{
mes('Вы к участию не допускаетесь! Прочитайте требования');
}
}
$posts[]=array('title'=>'- <a href="cup_id.php?id='.$id.'&amp;in">Участвовать</a>');
}
else
$posts[]=array('title'=>'Вы уже участвуете в этом кубке');
$yc=mysql_result(mysql_query("SELECT COUNT(*) FROM `fish_yc` WHERE `id_cup`='".$arr['id']."'"),0);

$posts[]=array('title'=>'- <a href="cup_id.php?id='.$id.'&amp;y">Список участников</a>('.$yc.')');

}
else
{
$query1=mysql_query("SELECT * FROM `fish_yc` WHERE `id_cup`='".$id."'");
while($array=mysql_fetch_array($query1)){
$ank=new_user($array['id_u']);
$posts[]=array('title'=>'<a href="/info.php?id='.$ank['id'].'">'.$ank['nick'].'</a>');
}
ret('Информация о турнире','cup_id.php?id='.$id);
}
}
}

$count=count($posts);
for($i=0;$i<$count;$i++){
if($i%2)
echo "<div class='p_m'><table><tr>".implode($posts[$i])."</tr></table></div>";
else
echo "<div class='p_t'><table><tr>".implode($posts[$i])."</tr></table></div>";
}
if($user_id==1)
ret('В админку','cup_admin.php');
ret('В игру','index.php');
include_once '../sys/inc/tfoot.php';

?>