<?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/user.php';
$set['title']='FM Чемпионаты';
include_once '../../../sys/inc/thead.php';
title();
aut();
include_once '../../online.php';
if (!isset($user) && !isset($_GET['id'])){header("Location: index.php?".SID);exit;}
if (isset($_GET['id']))$id=intval($_GET['id']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat` WHERE `id` = '$id' LIMIT 1"),0)==0){header("Location: index.php?".SID);exit;}
$chempionat=mysql_fetch_array(mysql_query("SELECT * FROM `fm_chempionat` WHERE `id` = '$id' LIMIT 1"));
$klubi=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id_user` = '$user[id]' LIMIT 1"));
$mogeba=$chempionat['price']*$chempionat['clubs'];
$finish=($chempionat['clubs']-1)*$chempionat['clubs'];
$games = mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` where `id_chempionat` = '$id'"), 0);

$admin = mysql_fetch_array(mysql_query("SELECT auto FROM `fm_admin` WHERE `id` = '1' LIMIT 1"));

////////////////////////////////
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE  `id_chempionat` = '$id'"),0)<$finish && mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_chempionat` = '$id'"),0)==$chempionat['clubs'])
{
$qqq = mysql_query("SELECT * FROM `fm_chempionat_clubs` where `id_chempionat` = '$id'");
while ($fff = mysql_fetch_array($qqq))
{
$klubi1=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id` = '$fff[id_club]' LIMIT 1"));
$ank1=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$klubi1[id_user]' LIMIT 1"));
$coach1=mysql_fetch_array(mysql_query("SELECT * FROM `fm_coach` WHERE `id_club` = '$klubi1[id]' LIMIT 1"));
$p1=mysql_fetch_array(mysql_query("SELECT * FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1"));

$exit=$p1['timer']+$chempionat['timer']+$admin['auto'];
if($time>$exit)
{
$qqqqq = mysql_query("SELECT * FROM `fm_chempionat_clubs` where `id_chempionat` = '$id' order by rand() LIMIT 1");
while ($bbb = mysql_fetch_array($qqqqq))
{
if($bbb['id_club']!=$fff['id_club'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` where `id_club1` = '$fff[id_club]' and `id_club2` = '$bbb[id_club]' and `id_chempionat` = '$id'"), 0)==0)
{
///
$klubi2=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id` = '$bbb[id_club]' LIMIT 1"));
$ank2=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$klubi2[id_user]' LIMIT 1"));
$coach2=mysql_fetch_array(mysql_query("SELECT * FROM `fm_coach` WHERE `id_club` = '$klubi2[id]' LIMIT 1"));
$p2=mysql_fetch_array(mysql_query("SELECT * FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1"));

////////////////////////daxarisxeba//////////////////////////////////////
$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($klubi1['var']==$klubi2['var']){$goli1_1=$rand1; $goli2_1=$rand1;}
if($klubi1['var']>$klubi2['var']){$goli1_1=$rand2; $goli2_1=$rand1;}
if($klubi1['var']<$klubi2['var']){$goli1_1=$rand1; $goli2_1=$rand2;}

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($klubi1['level']==$klubi2['level']){$goli1_2=$rand1; $goli2_2=$rand1;}
if($klubi1['level']>$klubi2['level']){$goli1_2=$rand2; $goli2_2=$rand1;}
if($klubi1['level']<$klubi2['level']){$goli1_2=$rand1; $goli2_2=$rand2;}

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($coach1['level']==$coach2['level']){$goli1_3=$rand1; $goli2_3=$rand1;}
if($coach1['level']>$coach2['level']){$goli1_3=$rand2; $goli2_3=$rand1;}
if($coach1['level']<$coach2['level']){$goli1_3=$rand1; $goli2_3=$rand2;}

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($klubi1['fani']==$klubi2['fani']){$goli1_4=$rand1; $goli2_4=$rand1;}
if($klubi1['fani']>$klubi2['fani']){$goli1_4=$rand2; $goli2_4=$rand1;}
if($klubi1['fani']<$klubi2['fani']){$goli1_4=$rand1; $goli2_4=$rand2;}


$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 2);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$goli1_5 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 0);
$num_c2 = rand(0, 2);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$goli2_5 = $num_d2[$num_e1];

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($user['fm_money']==$ank['fm_money']){$goli1_6=$rand1; $goli2_6=$rand1;}
if($user['fm_money']>$ank['fm_money']){$goli1_6=$rand2; $goli2_6=$rand1;}
if($user['fm_money']<$ank['fm_money']){$goli1_6=$rand1; $goli2_6=$rand2;}

//////////////////////////////////////////////////////////////
$goli1=$goli1_1+$goli1_2+$goli1_3+$goli1_4+$goli1_5+$goli1_6;
$goli2=$goli2_1+$goli2_2+$goli2_3+$goli2_4+$goli2_5+$goli2_6;
//////////////////////////////////////////////////////////////

if($goli1==$goli2)
{
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+1, `fre` = fre+1, `in` = '".($p1['in']+$goli2)."', `out` = '".($p1['out']+$goli1)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+1, `fre` = fre+1, `in` = '".($p2['in']+$goli1)."', `out` = '".($p2['out']+$goli2)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("INSERT INTO `fm_chempionat_game` (`id_chempionat`, `id_club1`, `id_club2`, `goli1`, `goli2`, `time`) values('$id', '$klubi1[id]', '$klubi2[id]', '$goli1', '$goli2', '$time')");
$msgno1="[b]$klubi1[name] $goli1 - $goli2 $klubi2[name][/b] ничья!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank1[id]', '$msgno1', '$time')");
$msgno2="[b]$klubi2[name] $goli2 - $goli1 $klubi1[name][/b] ничья!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank2[id]', '$msgno2', '$time')");

}
elseif($goli1>$goli2)
{
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+3, `win` = win+1, `in` = '".($p1['in']+$goli2)."', `out` = '".($p1['out']+$goli1)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("UPDATE `fm_chempionat_clubs` SET `lose` = lose+1, `in` = '".($p2['in']+$goli1)."', `out` = '".($p2['out']+$goli2)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("INSERT INTO `fm_chempionat_game` (`id_chempionat`, `id_club1`, `id_club2`, `goli1`, `goli2`, `time`) values('$id', '$klubi1[id]', '$klubi2[id]', '$goli1', '$goli2', '$time')");
$msgno1="[b]$klubi1[name] $goli1 - $goli2 $klubi2[name][/b] вы победили!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank1[id]', '$msgno1', '$time')");
$msgno2="[b]$klubi2[name] $goli2 - $goli1 $klubi1[name][/b] вы проиграли!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank2[id]', '$msgno2', '$time')");

}
elseif($goli1<$goli2)
{
mysql_query("UPDATE `fm_chempionat_clubs` SET `lose` = lose+1, `in` = '".($p1['in']+$goli2)."', `out` = '".($p1['out']+$goli1)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+3, `win` = win+1, `in` = '".($p2['in']+$goli1)."', `out` = '".($p2['out']+$goli2)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("INSERT INTO `fm_chempionat_game` (`id_chempionat`, `id_club1`, `id_club2`, `goli1`, `goli2`, `time`) values('$id', '$klubi1[id]', '$klubi2[id]', '$goli1', '$goli2', '$time')");
$msgno1="[b]$klubi1[name] $goli1 - $goli2 $klubi2[name][/b]  вы проиграли!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank1[id]', '$msgno1', '$time')");
$msgno2="[b]$klubi2[name] $goli2 - $goli1 $klubi1[name][/b]  вы победили!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank2[id]', '$msgno2', '$time')");
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE `id_chempionat` = '$id'"),0)==$finish)
{
$qq = mysql_query("SELECT * FROM `fm_chempionat_clubs` where `id_chempionat`='$id' ORDER BY `level` DESC LIMIT 1");
while ($winer = mysql_fetch_array($qq))
{
$kkk=mysql_fetch_array(mysql_query("SELECT id_user FROM `fm_club` WHERE `id` = '$winer[id_club]' LIMIT 1"));
mysql_query("UPDATE user SET fm_money=fm_money+$mogeba WHERE id='$kkk[id_user]';");
$msgno="вы победили в чемпионате [b]$chempionat[name][/b]!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$kkk[id_user]', '$msgno', '$time')");
}
}

///
}
}
}

}
}
////////////////////////////////////////////////////////////////////////////


if (isset($_GET['play']))
{
$play=intval($_GET['play']);

$klubi1=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id_user` = '$user[id]' LIMIT 1"));
$klubi2=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id_user` = '$play' LIMIT 1"));
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$play' LIMIT 1"));
$coach1=mysql_fetch_array(mysql_query("SELECT * FROM `fm_coach` WHERE `id_club` = '$klubi1[id]' LIMIT 1"));
$coach2=mysql_fetch_array(mysql_query("SELECT * FROM `fm_coach` WHERE `id_club` = '$klubi2[id]' LIMIT 1"));
$p1=mysql_fetch_array(mysql_query("SELECT * FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1"));
$p2=mysql_fetch_array(mysql_query("SELECT * FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1"));

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi1[id]'"),0)!=11)$err='У вас нет 11 игроков!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi2[id]'"),0)!=11)$err='У него нет 11 игроков!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_coach` WHERE `id_club` = '$klubi1[id]'"),0)==0)$err='У вас нет тренера!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_coach` WHERE `id_club` = '$klubi2[id]'"),0)==0)$err='У него нет тренера!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_club` WHERE `id_user` = '$user[id]'"),0)==0)$err='У вас нет Клубa!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_club` WHERE `id_user` = '$ank[id]'"),0)==0)$err='У него нет Клубa!';


if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id'"),0)==0)$err='он не учавствует  в этом чемпионате!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_chempionat` = '$id'"),0)!=$chempionat['clubs'])$err='нет '.$chempionat['clubs'].' клубов!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' and `timer` > '$time'"),0)!=0)$err='Время между играми не прошло!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE `id_club1` = '$klubi1[id]' and `id_club2` = '$klubi2[id]' and `id_chempionat` = '$id'"),0)!=0)$err='с ним уже играли!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE `id_chempionat` = '$id'"),0)==$finish)$err='чемпионат окончен!';
if($play==$user['id'])$err="вы не можете играть сам с собой!";
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id'"),0)==0)$err='вы не учавствуйте в этом чемпионате!';
if (!isset($err))
{
////////////////////////daxarisxeba//////////////////////////////////////
$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($klubi1['var']==$klubi2['var']){$goli1_1=$rand1; $goli2_1=$rand1;}
if($klubi1['var']>$klubi2['var']){$goli1_1=$rand2; $goli2_1=$rand1;}
if($klubi1['var']<$klubi2['var']){$goli1_1=$rand1; $goli2_1=$rand2;}

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($klubi1['level']==$klubi2['level']){$goli1_2=$rand1; $goli2_2=$rand1;}
if($klubi1['level']>$klubi2['level']){$goli1_2=$rand2; $goli2_2=$rand1;}
if($klubi1['level']<$klubi2['level']){$goli1_2=$rand1; $goli2_2=$rand2;}

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($coach1['level']==$coach2['level']){$goli1_3=$rand1; $goli2_3=$rand1;}
if($coach1['level']>$coach2['level']){$goli1_3=$rand2; $goli2_3=$rand1;}
if($coach1['level']<$coach2['level']){$goli1_3=$rand1; $goli2_3=$rand2;}

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($klubi1['fani']==$klubi2['fani']){$goli1_4=$rand1; $goli2_4=$rand1;}
if($klubi1['fani']>$klubi2['fani']){$goli1_4=$rand2; $goli2_4=$rand1;}
if($klubi1['fani']<$klubi2['fani']){$goli1_4=$rand1; $goli2_4=$rand2;}


$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 2);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$goli1_5 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 0);
$num_c2 = rand(0, 2);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$goli2_5 = $num_d2[$num_e1];

$num_a1 = rand(0, 0);
$num_b1 = rand(0, 0);
$num_c1 = rand(0, 1);
$num_d1 = array($num_a1, $num_b1, $num_c1);
$num_e1 = rand(0, 2);
$rand1 = $num_d1[$num_e1];

$num_a2 = rand(0, 0);
$num_b2 = rand(0, 1);
$num_c2 = rand(0, 1);
$num_d2 = array($num_a2, $num_b2, $num_c2);
$num_e2 = rand(0, 2);
$rand2 = $num_d2[$num_e1];

if($user['fm_money']==$ank['fm_money']){$goli1_6=$rand1; $goli2_6=$rand1;}
if($user['fm_money']>$ank['fm_money']){$goli1_6=$rand2; $goli2_6=$rand1;}
if($user['fm_money']<$ank['fm_money']){$goli1_6=$rand1; $goli2_6=$rand2;}

//////////////////////////////////////////////////////////////
$goli1=$goli1_1+$goli1_2+$goli1_3+$goli1_4+$goli1_5+$goli1_6;
$goli2=$goli2_1+$goli2_2+$goli2_3+$goli2_4+$goli2_5+$goli2_6;
//////////////////////////////////////////////////////////////

if($goli1==$goli2)
{
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+1, `fre` = fre+1, `in` = '".($p1['in']+$goli2)."', `out` = '".($p1['out']+$goli1)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+1, `fre` = fre+1, `in` = '".($p2['in']+$goli1)."', `out` = '".($p2['out']+$goli2)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("INSERT INTO `fm_chempionat_game` (`id_chempionat`, `id_club1`, `id_club2`, `goli1`, `goli2`, `time`) values('$id', '$klubi1[id]', '$klubi2[id]', '$goli1', '$goli2', '$time')");
$msgno="[b]$klubi2[name] $goli2 - $goli1 $klubi1[name][/b] ничья!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank[id]', '$msgno', '$time')");
msg ("<b>$goli1 - $goli2</b> ничья!");
}
elseif($goli1>$goli2)
{
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+3, `win` = win+1, `in` = '".($p1['in']+$goli2)."', `out` = '".($p1['out']+$goli1)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("UPDATE `fm_chempionat_clubs` SET `lose` = lose+1, `in` = '".($p2['in']+$goli1)."', `out` = '".($p2['out']+$goli2)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("INSERT INTO `fm_chempionat_game` (`id_chempionat`, `id_club1`, `id_club2`, `goli1`, `goli2`, `time`) values('$id', '$klubi1[id]', '$klubi2[id]', '$goli1', '$goli2', '$time')");
$msgno="[b]$klubi2[name] $goli2 - $goli1 $klubi1[name][/b] вы проиграли!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank[id]', '$msgno', '$time')");
msg ("<b>$goli1 - $goli2</b> вы победили!");
}
elseif($goli1<$goli2)
{
mysql_query("UPDATE `fm_chempionat_clubs` SET `lose` = lose+1, `in` = '".($p1['in']+$goli2)."', `out` = '".($p1['out']+$goli1)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi1[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("UPDATE `fm_chempionat_clubs` SET `level` = level+3, `win` = win+1, `in` = '".($p2['in']+$goli1)."', `out` = '".($p2['out']+$goli2)."', `timer` = '".($time+$chempionat['timer'])."' WHERE `id_club` = '$klubi2[id]' and `id_chempionat` = '$id' LIMIT 1");
mysql_query("INSERT INTO `fm_chempionat_game` (`id_chempionat`, `id_club1`, `id_club2`, `goli1`, `goli2`, `time`) values('$id', '$klubi1[id]', '$klubi2[id]', '$goli1', '$goli2', '$time')");
$msgno="[b]$klubi2[name] $goli2 - $goli1 $klubi1[name][/b] вы победили!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank[id]', '$msgno', '$time')");
msg ("<b>$goli1 - $goli2</b> вы проиграли!");
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE `id_chempionat` = '$id'"),0)==$finish)
{
$qq = mysql_query("SELECT * FROM `fm_chempionat_clubs` where `id_chempionat`='$id' ORDER BY `level` DESC LIMIT 1");
while ($winer = mysql_fetch_array($qq))
{
$kkk=mysql_fetch_array(mysql_query("SELECT id_user FROM `fm_club` WHERE `id` = '$winer[id_club]' LIMIT 1"));
mysql_query("UPDATE user SET fm_money=fm_money+$mogeba WHERE id='$kkk[id_user]';");
$msgno="вы победили в чемпионате [b]$chempionat[name][/b]!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$kkk[id_user]', '$msgno', '$time')");
}
}


}
}

if (isset($_GET['in']))
{
if ($user['fm_money']<$chempionat['price'])$err="У вас нет $chempionat[price] Долларов!";
if ($klubi['level']<$chempionat['level'])$err="не хватает уровня!";
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat` WHERE `id` = '$id' LIMIT 1"),0)==0)$err="такой чемпионат не существует!";
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi[id]'"),0)!=11)$err='У вас нет 11 игроков!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_coach` WHERE `id_club` = '$klubi[id]'"),0)==0)$err='У вас нет тренера!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_club` WHERE `id_user` = '$user[id]'"),0)==0)$err='У вас нет Клуба!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi[id]' and `id_chempionat` = '$id'"),0)!=0)$err='вы уже участвуете в этом чемпионате!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_chempionat` = '$id'"),0)==$chempionat['clubs'])$err='нет мест в чемпионате!';
if (!isset($err))
{
mysql_query("UPDATE user SET fm_money=fm_money-$chempionat[price] WHERE id='$user[id]';");
mysql_query("INSERT INTO `fm_chempionat_clubs` (`id_chempionat`, `id_club`, `timer`, `time`) values('$id', '$klubi[id]', '$time', '$time')");
msg('ваш клуб добавлен в чемпионат!');
}
}
err();
echo "<div class='frends2'>";
echo "<b>$chempionat[name]</b><br />\n";
echo "<span style='color:red'>Kоличество клубов:</span> <span style='color:Green'>$chempionat[clubs]</span><br/>";
echo "<span style='color:red'>Стоимость участия:</span> <span style='color:Green'>$chempionat[price]$</span><br/>";
echo "<span style='color:red'>Призовой фонд:</span> <span style='color:Green'>$mogeba$</span><br/>";
echo "<span style='color:red'>Уровень клубов:</span> <span style='color:Green'>$chempionat[level]</span><br/>";
$vremja=$chempionat[timer];
$timediff=$vremja;
$oneMinute=60;
$oneHour=60*60;
$oneDay=60*60*24;
$dayfield=floor($timediff/$oneDay);
$hourfield=floor(($timediff-$dayfield*$oneDay)/$oneHour);
$minutefield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour)/$oneMinute);
$secondfield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour-$minutefield*$oneMinute));
if($dayfield>0)$day=$dayfield.' Сут.';
if($hourfield>0)$hour=$hourfield.' Час.';
if($minutefield>0)$minute=$minutefield.' Мин.';
if($secondfield>0)$secc=$secondfield.' Сек.';
$time_1="$day $hour $minute $secc";

echo "<span style='color:red'>Время между играми:</span> <span style='color:Green'>$time_1</span><br/>";
echo "<span style='color:red'>Началось:</span> <span style='color:Green'>".vremja($chempionat[time])."</span><br/>";
echo "</div>";
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE `id_chempionat` = '$id'"),0)==$finish)
{
echo "<div class='frends2'>";
$qq = mysql_query("SELECT * FROM `fm_chempionat_clubs` where `id_chempionat`='$id' ORDER BY `level` DESC LIMIT 1");
while ($winer = mysql_fetch_array($qq))
{
$cl=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id` = '$winer[id_club]' LIMIT 1"));
echo "<img src='../icon/cup.png'> Победитель: <a href='../club.php?id=$cl[id_user]'>$cl[name]</a><br />";
}
echo "</div>";
}
echo "<div class='frends2'>";
echo "<img src='../icon/m.gif'> <a href='games.php?id=$id'>Игры чемпионата</a> ($games/$finish)<br/>";
echo "</div>";
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi[id]' and `id_chempionat` = '$id'"),0)==0)
{
echo "<div class='frends2'>";
echo "<img src='../icon/ok.png'> <a href='chempionat.php?id=$id&amp;in'>Принять участие</a><br/>";
echo "</div>";
}
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` where `id_chempionat`='$id'"), 0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
if ($k_post==0){echo '<div class="frends2">Участники чемпионата не найдены!</div>';}else{echo'<table width="100%" border="0" cellspacing="1" cellpadding="2"><tr bgcolor="#cccccc"><td>клуб</td><td>1</td><td>X</td><td>2</td><td>out</td><td>in</td><td>очки</td></tr>';}
$q = mysql_query("SELECT * FROM `fm_chempionat_clubs` where `id_chempionat`='$id' ORDER BY `level` DESC LIMIT $chempionat[clubs]");
while ($f = mysql_fetch_array($q))
{
$club = mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id` = '$f[id_club]' LIMIT 1"));
$me = mysql_fetch_array(mysql_query("SELECT * FROM `fm_chempionat_clubs` WHERE `id_chempionat` = '$id' and `id_club` = '$klubi[id]' LIMIT 1"));
$a = mysql_fetch_array(mysql_query("SELECT id FROM `user` WHERE `id` = '$club[id_user]' LIMIT 1"));
if($num==1){
echo "<tr bgcolor='#eeeeee'>";
$num=0;}
else{
echo "<tr>";
$num=1;}

if($club['id_user']==$user['id'] || mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_club` = '$klubi[id]' and `id_chempionat` = '$id'"),0)==0 || mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_clubs` WHERE `id_chempionat` = '$id'"),0)!=$chempionat['clubs']){$feri='Red';}
else
{
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_chempionat_game` WHERE `id_club1` = '$klubi[id]' and `id_club2` = '$club[id]' and `id_chempionat` = '$id'"),0)==1){$feri='Blue';}else{ if($time>$me['timer']){$feri='Green';}else{$feri='Red';} }
}
echo "<td><a href='chempionat.php?id=$id&amp;play=$a[id]'><span style='color:$feri'>$club[name]</span></a></td>";
echo "<td>$f[win]</td><td>$f[fre]</td><td>$f[lose]</td><td>$f[out]</td><td>$f[in]</td><td>$f[level]</td></tr>\n";
}
if ($k_post!=0){echo'</table>';}
echo "<div class='menu'>";
echo "<span style='color:Blue'><b>Уже играли</b></span> | ";
echo "<span style='color:Green'><b>Можете играть</b></span> | ";
echo "<span style='color:Red'><b>Не можете играть</b></span>";
echo "</div>";
echo "<div class='foot'>";
echo "&#171; <a href='index.php'>Назад</a><br/>";
echo "</div>";

include_once '../../../sys/inc/tfoot.php';

?>