<?
$turnir=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_turnir` WHERE `active` = '1' ORDER by `id` DESC")); 
if(isset($_GET['gift']))require_header ('Награды');
else if(isset($_GET['st']))require_header ('Завершенные турниры');
else if(isset($_GET['start'])){}
else if(isset($_GET['exit'])){}
else require_header ('Турнир '.(isset($turnir['name'])?'"'.$turnir['name'].'"':null).'');
if(!isset($_GET['start']) && !isset($_GET['exit'])){
echo'<div class="content">
<div class="block">';
}
if(isset($_GET['st']))
{
if(!is_numeric($_GET['st'])){
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_turnir` WHERE `active` = '0'"),0);

if ($k_post==0)
{
echo "Список пуст...";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$num=($page-1)*$k_page;
$q=mysql_query("SELECT * FROM `kolhoz_turnir` WHERE `active` = '0' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

while($post=mysql_fetch_array($q))
{
$num++;
echo "<li><span class='minor'>$num.</span><img width='16' height='16' src='images/turnir/$post[id].png' alt='*'> <a class='' href='/turnir/st-$post[id]'><span>$post[name]</span></a>  <img width='16' height='16' src='/images/icons/clock.png' alt='*'> ".(vremja($post['time']))."</li>";
}
if ($k_page>1){echo'<ul class="pt"></ul>'; new_str("turnir/st",$k_page,$page);} // Вывод страниц
echo '<li class="pt"><img width="16" height="16" src="images/icons/trophy-bronze.png" alt="*"> <a href="/turnir">Турнир</a><li/>';
}else{

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_turnir_st` WHERE `id_turnir` = '".(intval($_GET['st']))."'"),0);

if ($k_post==0)
{
echo "Список пуст...";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$num=($page-1)*$k_page;
$q=mysql_query("SELECT * FROM `kolhoz_turnir_st` WHERE `id_turnir` = '".(intval($_GET['st']))."'ORDER BY `mesto` ASC LIMIT $start, $set[p_str]");

while($post=mysql_fetch_array($q))
{
$num++;
if($num<4)$gift=unserialize($post['gift']); else $gift=$post['gift'];
echo "<li><span class='minor'>$num.</span> ".($num<4?"<img width='16' height='16' src='images/icons/".$num."m.png' alt='*'>":"".on_k($post['id_user'])."")." <a class='' href='/user/$post[id_user]'><span>".nc($post['id_user'])."</span></a>  <img width='16' height='16' src='/images/turnir/$post[id_turnir].png' alt='*'> $post[count] ".($num<4?"<img width='16' height='16' src='images/icons/experience.png' alt='*'> $gift[0] <img width='16' height='16' src='images/icons/money.png' alt='*'> $gift[1] <img width='16' height='16' src='images/icons/ruby.png' alt='*'> $gift[2] <img width='16' height='16' src='images/icons/pkey.png' alt='*'> $gift[3]":"<img width='16' height='16' src='images/icons/experience.png' alt='*'> $gift")."</li>";
}
$st=intval($_GET['st']);
if ($k_page>1){echo'<ul class="pt"></ul>'; new_str("turnir/st-$st/",$k_page,$page);} // Вывод страниц
echo '<li class="pt"><img width="16" height="16" src="images/icons/trophy-bronze.png" alt="*"> <a href="/turnir/st">Назад</a><li/>';

} //is_numeric
}
else if(isset($_GET['exit']) && $ku['active_turnir']==1)
{
if(isset($_GET['ok'])){
mysql_query("UPDATE `kolhoz_user` SET `active_turnir` = '0' WHERE `id` = '$ku[id]'");
header("Location:/turnir");
}else{
podtv("turnir/exit/ok","turnir");
}


}
else if(isset($_GET['start']) && $ku['active_turnir']==0)
{
if(isset($_GET['ok'])){
if($ku['rubies']>=$turnir['cena'])
{
mysql_query("UPDATE `kolhoz_user` SET `active_turnir` = '1',`rubies` = '".($ku['rubies']-$turnir['cena'])."' WHERE `id` = '$ku[id]'");
$ku['rubies']=$ku['rubies']-$turnir['cena'];
//$_SESSION['msg']='Вы приняли участие в турнире';
header("Location:/turnir");
}
else
{
$rm=$turnir['cena']-$ku['rubies'];
$_SESSION['no_rubies']=$rm;
}
}else{
podtv("/turnir/start/ok","/turnir");
}
}else{
if($turnir['id']>0){
if(isset($_GET['gift']))
{

echo '
<li><img width="16" height="16" src="images/icons/1m.png" alt="*"> 1 место: <img width="16" height="16" src="images/icons/ruby.png" alt="*"> <span>'.(strkol($turnir['rubies'])).'</span> <img width="16" height="16" src="images/icons/experience.png" alt="*">
<span>'.(strkol($turnir['rating'])).'</span> <img width="16" height="16" src="images/icons/money.png" alt="*"> <span>'.(strkol($turnir['money'])).'</span>  <img width="16" height="16" src="images/icons/pkey.png" alt="*"> <span>'.$turnir['timekey'].' д.</span></li>
<li class="ptm"><img width="16" height="16" src="images/icons/2m.png" alt="*"> 2 место: <img width="16" height="16" src="images/icons/ruby.png" alt="*"> <span>'.(strkol($turnir['rubies']/2)).'</span> <img width="16" height="16" src="images/icons/experience.png" alt="*">
<span>'.(strkol($turnir['rating']/2)).'</span> <img width="16" height="16" src="images/icons/money.png" alt="*"> <span>'.(strkol($turnir['money']/2)).'</span>  <img width="16" height="16" src="images/icons/pkey.png" alt="*"> <span>'.(round($turnir['timekey']/1.5)).' д.</span></li>
<li class="ptm"><img width="16" height="16" src="images/icons/3m.png" alt="*"> 3 место: <img width="16" height="16" src="images/icons/ruby.png" alt="*"> <span>'.(strkol($turnir['rubies']/4)).'</span> <img width="16" height="16" src="images/icons/experience.png" alt="*">
<span>'.(strkol($turnir['rating']/4)).'</span> <img width="16" height="16" src="images/icons/money.png" alt="*"> <span>'.(strkol($turnir['money']/4)).'</span>  <img width="16" height="16" src="images/icons/pkey.png" alt="*"> <span>'.(round($turnir['timekey']/3)).' д.</span></li>
';
echo '<li class="pt"><img width="16" height="16" src="images/icons/trophy-bronze.png" alt="*"> <a href="/turnir">Турнир</a><li/>';
}else{
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_user` WHERE `active_turnir` = '1' AND `turnir_count`>'0'"),0);
echo'<h3>Участники</h3>';
if ($k_post==0)
{
echo "Список пуст...";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$num=($page-1)*$set['p_str'];
$q=mysql_query("SELECT * FROM `kolhoz_user` WHERE `active_turnir` = '1' AND `turnir_count`>'0' ORDER BY `turnir_count` DESC, `rating` DESC LIMIT $start, $set[p_str]");
if($ku['turnir_count']>0){$qyou=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_user` WHERE `active_turnir` = '1' AND (`turnir_count`>'$ku[turnir_count]' || (`turnir_count`='$ku[turnir_count]' AND `rating`>'$ku[rating]')) ORDER BY `turnir_count` DESC, `rating` DESC"),0)+1;
echo '<span class="title">Вы находитесь на '.$qyou.' месте</span>';
}
while($post=mysql_fetch_array($q))
{
$num++;
echo "<li><span class='minor'>$num.</span> ".on_k($post['id'])." <a class='' href='/user/$post[id]'><span>".nc($post['id'])."</span></a>  <img width='16' height='16' src='/images/turnir/$turnir[id].png' alt='*'> $post[turnir_count]</li>";
}
if ($k_page>1){echo'<ul class="pt"></ul>'; new_str("turnir/",$k_page,$page);} // Вывод страниц
echo'<li>Итоги турнира будут подведены: '.(vremja($turnir['time'])).'</li>';
echo '<li class="pt"><img width="16" height="16" src="images/icons/present.png" alt="*"> <a href="/turnir/gift">Призы</a></li>';
echo '<li><img width="16" height="16" src="images/icons/trophy-bronze.png" alt="*"> <a href="/turnir/st">Завершенные турниры</a><li/>';
if($ku['active_turnir']==0)echo '<li><img width="16" height="16" src="images/icons/tick.png" alt="*"> <a href="/turnir/start">Принять участие</a> (<img width="16" height="16" src="images/icons/ruby.png" alt="*"> '.$turnir['cena'].')<li/>';
if($ku['active_turnir']==1)echo '<li><img width="16" height="16" src="images/icons/cross.png" alt="*"> <a href="/turnir/exit">Отказаться</a><li/>';
} //else
}else{echo'Активных турниров в нет <li class="pt"><img width="16" height="16" src="images/icons/trophy-bronze.png" alt="*"> <a href="/turnir/st">Завершенные турниры</a><li/>';}

}
?>