<?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']='Football Game';
include_once '../../sys/inc/thead.php';
title();
err();
aut();

include_once 'online.php';


if (isset($_GET['ok']))
{
$ok = intval($_GET['ok']);
$game=mysql_fetch_array(mysql_query("SELECT * FROM `fm_game` WHERE `id` = '$ok' LIMIT 1"));
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$game[user]' LIMIT 1"));
$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` = '$ank[id]' 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"));
$fsoni=intval($game['balls']);
if ($ank['fm_money']<$fsoni)$err="У него нет $fsoni Долларов!";
if ($user['fm_money']<$fsoni)$err="У вас нет $fsoni Долларов!";
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_game` WHERE `user` = '$ank[id]' and `ank` = '$user[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($klubi['fani']>$klubi2['fani']){$goli1_4=$rand2; $goli2_4=$rand1;}
if($klubi['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;
//////////////////////////////////////////////////////////////
$fani1=rand(0,300);
$fani2=rand(0,1000);

if($goli1==$goli2)
{
$foot1 = mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi1[id]' LIMIT 11");
while ($ball1 = mysql_fetch_array($foot1))
{
$randi1=rand(0,1);
mysql_query("UPDATE `fm_gamers` SET `level` = '".($ball1['level']+$randi1)."', `timevar` = '0' WHERE `id` = '$ball1[id]' LIMIT 1");
}
$foot2 = mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi2[id]' LIMIT 11");
while ($ball2 = mysql_fetch_array($foot2))
{
$randi2=rand(0,1);
mysql_query("UPDATE `fm_gamers` SET `level` = '".($ball2['level']+$randi2)."', `timevar` = '0' WHERE `id` = '$ball2[id]' LIMIT 1");
}
mysql_query("UPDATE `fm_coach` SET `level` = '".($coach1['level']+1)."', `timedar` = '0' WHERE `id_club` = '$klubi1[id]' LIMIT 1");
mysql_query("UPDATE `fm_coach` SET `level` = '".($coach2['level']+1)."', `timedar` = '0' WHERE `id_club` = '$klubi2[id]' LIMIT 1");
mysql_query("UPDATE `fm_club` SET `fani` = '".($klubi1['fani']+$fani1)."', `timedar` = '0', `var` = '0', `level` = '".($klubi1['level']+1)."', `fre` = '".($klubi1['fre']+1)."', `out` = '".($klubi1['out']+$goli1)."', `in` = '".($klubi1['in']+$goli2)."' WHERE `id` = '$klubi1[id]' LIMIT 1");
mysql_query("UPDATE `fm_club` SET `fani` = '".($klubi2['fani']+$fani1)."', `timedar` = '0', `var` = '0', `level` = '".($klubi2['level']+1)."', `fre` = '".($klubi2['fre']+1)."', `out` = '".($klubi2['out']+$goli2)."', `in` = '".($klubi2['in']+$goli1)."' WHERE `id` = '$klubi2[id]' LIMIT 1");
mysql_query("INSERT INTO `fm_ist` (`club1`, `club2`, `goli1`, `goli2`, `balls`, `time`) values('$klubi2[id]', '$klubi1[id]', '$goli2', '$goli1', '$fsoni', '$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)
{
$foot1 = mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi1[id]' LIMIT 11");
while ($ball1 = mysql_fetch_array($foot1))
{
$randi1=rand(0,3);
mysql_query("UPDATE `fm_gamers` SET `level` = '".($ball1['level']+$randi1)."', `timevar` = '0' WHERE `id` = '$ball1[id]' LIMIT 1");
}
$foot2 = mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi2[id]' LIMIT 11");
while ($ball2 = mysql_fetch_array($foot2))
{
mysql_query("UPDATE `fm_gamers` SET `timevar` = '0' WHERE `id` = '$ball2[id]' LIMIT 1");
}
mysql_query("UPDATE `fm_coach` SET `level` = '".($coach1['level']+3)."', `timedar` = '0' WHERE `id_club` = '$klubi1[id]' LIMIT 1");
mysql_query("UPDATE `fm_coach` SET `level` = '".($coach2['level']-1)."', `timedar` = '0' WHERE `id_club` = '$klubi2[id]' LIMIT 1");
mysql_query("UPDATE `fm_club` SET `fani` = '".($klubi1['fani']+$fani2)."', `timedar` = '0', `var` = '0', `level` = '".($klubi1['level']+3)."', `win` = '".($klubi1['win']+1)."', `out` = '".($klubi1['out']+$goli1)."', `in` = '".($klubi1['in']+$goli2)."' WHERE `id` = '$klubi1[id]' LIMIT 1");
mysql_query("UPDATE `fm_club` SET `timedar` = '0', `var` = '0', `lose` = '".($klubi2['lose']+1)."', `out` = '".($klubi2['out']+$goli2)."', `in` = '".($klubi2['in']+$goli1)."' WHERE `id` = '$klubi2[id]' LIMIT 1");
mysql_query("INSERT INTO `fm_ist` (`club1`, `club2`, `goli1`, `goli2`, `balls`, `time`) values('$klubi2[id]', '$klubi1[id]', '$goli2', '$goli1', '$fsoni', '$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')");
mysql_query("UPDATE user SET fm_money=fm_money+$fsoni WHERE id='$user[id]';");
mysql_query("UPDATE user SET fm_money=fm_money-$fsoni WHERE id='$ank[id]';");
msg ("<b>$goli1 - $goli2</b> вы победили!");
}
elseif($goli1<$goli2)
{
$foot1 = mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi1[id]' LIMIT 11");
while ($ball1 = mysql_fetch_array($foot1))
{
mysql_query("UPDATE `fm_gamers` SET `timevar` = '0' WHERE `id` = '$ball1[id]' LIMIT 1");
}
$foot2 = mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi2[id]' LIMIT 11");
while ($ball2 = mysql_fetch_array($foot2))
{
$randi2=rand(0,3);
mysql_query("UPDATE `fm_gamers` SET `level` = '".($ball2['level']+$randi2)."', `timevar` = '0' WHERE `id` = '$ball2[id]' LIMIT 1");
}
mysql_query("UPDATE `fm_coach` SET `level` = '".($coach1['level']-1)."', `timedar` = '0' WHERE `id_club` = '$klubi1[id]' LIMIT 1");
mysql_query("UPDATE `fm_coach` SET `level` = '".($coach2['level']+3)."', `timedar` = '0' WHERE `id_club` = '$klubi2[id]' LIMIT 1");
mysql_query("UPDATE `fm_club` SET `timedar` = '0', `var` = '0', `lose` = '".($klubi1['lose']+1)."', `out` = '".($klubi1['out']+$goli1)."', `in` = '".($klubi1['in']+$goli2)."' WHERE `id` = '$klubi1[id]' LIMIT 1");
mysql_query("UPDATE `fm_club` SET `fani` = '".($klubi2['fani']+$fani2)."', `timedar` = '0', `var` = '0', `level` = '".($klubi2['level']+3)."', `win` = '".($klubi2['win']+1)."', `out` = '".($klubi2['out']+$goli2)."', `in` = '".($klubi2['in']+$goli1)."' WHERE `id` = '$klubi2[id]' LIMIT 1");
mysql_query("INSERT INTO `fm_ist` (`club1`, `club2`, `goli1`, `goli2`, `balls`, `time`) values('$klubi2[id]', '$klubi1[id]', '$goli2', '$goli1', '$fsoni', '$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')");
mysql_query("UPDATE user SET fm_money=fm_money-$fsoni WHERE id='$user[id]';");
mysql_query("UPDATE user SET fm_money=fm_money+$fsoni WHERE id='$ank[id]';");
msg ("<b>$goli1 - $goli2</b> вы проиграли!");
}

mysql_query("DELETE FROM `fm_game` WHERE `id` = '$ok' LIMIT 1");
}
}

if (isset($_GET['no']))
{
$no = intval($_GET['no']);
$game=mysql_fetch_array(mysql_query("SELECT * FROM `fm_game` WHERE `id` = $no LIMIT 1"));
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$game[user]' LIMIT 1"));
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_game` WHERE `id` = '$no' LIMIT 1"),0)==0)$err="Нет такой игры!";
if (!isset($err)){
$msgno="[b]$user[nick][/b] отказался играть!";
mysql_query("INSERT INTO `fm_jurnal` (`id_user`, `msg`, `time`) values('$ank[id]', '$msgno', '$time')");
mysql_query("DELETE FROM `fm_game` WHERE `id` = '$no' LIMIT 1");
msg ('вы отказались играть!');
}
}

err();

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_game` WHERE `ank` = '$user[id]'"), 0);

if ($k_post==0)
{
echo '<div class="menu">Нет игры!</div>';
}

$q = mysql_query("SELECT * FROM `fm_game` WHERE `ank` = '$user[id]' ORDER BY time DESC LIMIT 1");
while ($f = mysql_fetch_array($q))
{
$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".$f['user']."' LIMIT 1"));
$klubi = mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id_user` = '".$f['user']."' LIMIT 1"));
if($num==1){  echo "<div class='frends'>";  $num=0;  }else{  echo "<div class='frends2'>";  $num=1;} 
echo "".online($a['id'])." \n";
echo '<a href="/info.php?id='.$a['id'].'"><span style="color:'.$a['ncolor'].'">'.$a['nick'].'</span></a> ('.vremja($f['time']).')<br />';
echo 'Владелец команды <a href="/fm/club.php?id='.$a['id'].'">'.$klubi['name'].'</a> предлагает вам товарищескую игру!<br />';
echo "<b>ставка: $f[balls]$</b>";
echo '<br /><a href="ok.php?ok='.$f['id'].'">Играть</a> | <a href="ok.php?no='.$f['id'].'">Отказаться</a>';

echo "</div>";

}

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

?>