<?php
defined('_IN_JOHNCMS') or die('Error: restricted access');
function complite_team($id)
{
	$sql = mysql_query("SELECT * FROM `fman_clubs` WHERE `id`='$id' LIMIT 1");
	if(mysql_num_rows($sql))
	{
		$team = mysql_fetch_assoc($sql);
		for($i = 1; $i <= 11; $i++)
		{
			if(!$team['p'.$i])
			{
				if($i==1)
				{
					$sql = mysql_query("SELECT `id` FROM `fman_players` WHERE `team_id`='$id' and `ampl`='1' and `pos`='0' order by `rait` desc limit 1");
				}
				elseif($i==2 || $i==3 || $i==4)
				{
					$sql = mysql_query("SELECT `id` FROM `fman_players` WHERE `team_id`='$id' and `ampl`>'1' and `ampl`<'5' and `pos`='0' order by `rait` desc limit 1");				
				}
				elseif($i>4 && $i<10)
				{
					$sql = mysql_query("SELECT `id` FROM `fman_players` WHERE `team_id`='$id' and `ampl`>'4' and `ampl`<'10' and `pos`='0' order by `rait` desc limit 1");				
				}
				elseif($i==10 || $i==11)
				{
					$sql = mysql_query("SELECT `id` FROM `fman_players` WHERE `team_id`='$id' and `ampl`>'9' and `pos`='0' order by `rait` desc limit 1");					
				}
				
				if(!mysql_num_rows($sql))
				{
					$sql = mysql_query("SELECT `id` FROM `fman_players` WHERE `team_id`='$id' and `pos`='0' order by `rait` limit 1");
				}
				
				$player = mysql_fetch_assoc($sql);
				
				mysql_query("UPDATE `fman_clubs` SET `p$i`='$player[id]' WHERE `id`='$id' LIMIT 1");
				mysql_query("UPDATE `fman_players` SET `pos`='$i' WHERE `id`='$player[id]' LIMIT 1");

			}
		}
	}
}

function playersmas($type,$td){
if(!$td[p1]){
$qx=mysql_query("select * from fman_players where team_id=$td[id] limit 11");
$e=0;
while($ax=mysql_fetch_array($qx)){
$e=$e+1;
$ek="p".$e;
$td[$ek]=$ax[id];}}
for($i=1;$i<12;$i++){
$p="p".$i;
$k=$td[$p];
$q=mysql_query("select * from fman_players where id=$k;");
if($q)$ig=mysql_fetch_array($q);
if($i==1)$ig[pos]=1;
if($type==1)$itg[$i]=$ig[name];
if($type==2){
$dop=round((($ig[forma]+$ig[nast]+$ig[ust]+$ig[vyn])/5));
$dopkf=rand(1,7);
$dop=$dop+$dopkf;
$u=$ig[u]+$dop;
$drib=$ig[drib]+$dop;
$pas=$ig[pas]+$dop;
$igo=$ig[ig]+$dop;
$otb=$ig[otb]+$dop;
$priem=$ig[priem]+$dop;
$f=$ig[f]+$dop;
$toch=$ig[toch]+$dop;
$gk=$ig[gk]+$dop;
$itg[$i]="$u|$drib|$pas|$igo|$otb|$priem|$f|$toch|$gk|";
}
if($type==3)$itg[$i]=$ig[pos];
if($type==4)$itg[$i]=$ig[id];}
return $itg;}
function addsobytie($minn,$mat,$t1,$t2,$pl1,$pl2,$nv1,$nv2,$ps1,$ps2,$id1,$id2){
$si="s".$minn;
$sc1=$mat[sc1]; $sc2=$mat[sc2];
$qsu=mysql_query("select id from fman_clubs where id!=99 order by fan desc;");
$n=0;
while($asu=mysql_fetch_array($qsu)){
$n=$n+1;
$nj=131-$n;
if($asu[id]==$t1[id])$t1[nj]=$nj;
if($asu[id]==$t2[id])$t2[nj]=$nj;}
$t1[ra]=1;
if($t1[nj]>30)$t1[ra]=2;
if($t1[nj]>70)$t1[ra]=3;
if($t1[nj]>100)$t1[ra]=4;
if($t1[nj]>120)$t1[ra]=5;
$t2[ra]=1;
if($t2[nj]>30)$t2[ra]=2;
if($t2[nj]>70)$t2[ra]=3;
if($t2[nj]>100)$t2[ra]=4;
if($t2[nj]>120)$t2[ra]=5;
$a=explode('|',$mat[$si]);
if($minn==1){
$r[min]=0;
if(!$mat[neit]){
$fan1=$t1[nj]*500;
$fan2=$t2[nj]*250;
$fan3=$fan1+$fan2;
$r[zrit]=$fan3+rand(-500,500);
if($mat[turn]=="tm")$r[zrit]=round(($r[zrit]/10));

if($r[zrit]>$t1[stadv])$r[zrit]=$t1[stadv];
$r[dohod]=$r[zrit]*50;
$huij=' '.$t1[city].' shahridagi <b>'.$t1[stadion].'</b> stadionida  <b>'.$r[zrit].'</b> tomoshabin yig`ildi.';
newsadd("<b>$t1[name]</b> - <a href=club.php?id=$t2[id]>$t2[name]</a> matchi boshlandi. $t1[stadion] stadionida $r[zrit] tomoshabin yig`ildi. Chiptalar sotuvidan tushgan mablag` <b>$".number_format($r[dohod],0,'.',' ')."</b> ni tashkil etdi.<br/>Match boshlangunch bo`lgan byudjet: $".number_format($t1[budj],0,'.',' ')."<br/>Match boshlangandan keyingi byudjet: $".number_format(($r[dohod]+$t1[budj]),0,'.',' ')."",$t1[id]);}else{
$r[dohod]=0;
$huij=' Match neytral maydonda bo`lib o`tadi, sababi bu final janoblar!';}
$r[text]='Assalomu alaykum qadrli futbol ixlosmandlari, bugun men <b>John Champion</b> va sherigim <b>Jimmy Beglin</b> menejer.cf saytida, <b>'.$mat[kom1].' - '.$mat[kom2].'</b> o`yinini sharxlab boramiz.'.$huij;
$d1=0; $m1=0; $f1=0; $d2=0; $m2=0; $f2=0; $u1=0; $u2=0; $u3=0; $u4=0; $u5=0;
for($i=2;$i<12;$i++){
if($ps1[$i]==2 || $ps1[$i]==3 || $ps1[$i]==4){
$u1=$u1+1; $d1=$d1+1;
if($u1!=1)$dr1.=', ';
$dr1.=$pl1[$i];}
if($ps1[$i]==5 || $ps1[$i]==6 || $ps1[$i]==7 || $ps1[$i]==8 || $ps1[$i]==9){
$u2=$u2+1; $m1=$m1+1;
if($u2!=1)$mr1.=', ';
$mr1.=$pl1[$i];}
if($ps1[$i]==10){
$u3=$u3+1; $f1=$f1+1;
if($u3!=1)$fr1.=', ';
$fr1.=$pl1[$i];}
if($ps2[$i]==2 || $ps2[$i]==3 || $ps2[$i]==4){
$u4=$u4+1; $d2=$d2+1;
if($u4!=1)$dr2.=', ';
$dr2.=$pl2[$i];}
if($ps2[$i]==5 || $ps2[$i]==6 || $ps2[$i]==7 || $ps2[$i]==8 || $ps2[$i]==9){
$u5=$u5+1; $m2=$m2+1;
if($u5!=1)$mr2.=', ';
$mr2.=$pl2[$i];}
if($ps2[$i]==10){
$u6=$u6+1; $f2=$f2+1;
if($u6!=1)$fr2.=', ';
$fr2.=$pl2[$i];}}
$r[text].=' Jamoalarning taktik sxemalari bilan tanishamiz. '.$mat[kom1].': '.$d1.'-'.$m1.'-'.$f1.'. Asosiy tarkibdagilar: '.$pl1[1].' - '.$dr1.' - '.$mr1.' - '.$fr1;
if($t1[tak]>1){$r[text].='. Mezbonlarning taktikasi: ';
if($t1[tak]==2)$r[text].='kontrataka';
if($t1[tak]==3)$r[text].='pressing';
if($t1[tak]==4)$r[text].='markazdan hujum';
if($t1[tak]==5)$r[text].='qanotlardan hujum';}
$r[text].='.'.$mat[kom2].' klubining taktik sxemasi: '.$d2.'-'.$m2.'-'.$f2.'. Asosiy tarkibda: '.$pl2[1].' - '.$dr2.' - '.$mr2.' - '.$fr2;
if($t2[tak]>1){$r[text].='. Mehmon klubning bugungi o`yinga tanlagan taktikasi: ';
if($t2[tak]==2)$r[text].='kontrataka';
if($t2[tak]==3)$r[text].='pressing';
if($t2[tak]==4)$r[text].='markazdan hujum';
if($t2[tak]==5)$r[text].='qanotlardan hujum';}
$r[text].='.';
mysql_query("update fman_clubs set budj=".($t1[budj]+$r[dohod])." where id=$t1[id]");
$mat[sc1]=0;
$mat[sc2]=0;
$mat[$si]=$r[min].'|'.$r[text].'|1|';}
if($minn>1 && $minn<12 || $minn>12 && $minn<16){
if($minn<12){
$hu=($minn-1)*9;
$r[min]=rand(($hu-8),$hu);}else{
if($minn==13){$al=91; $av=100;}
if($minn==14){$al=101; $av=110;}
if($minn==15){$al=111; $av=119;}
$r[min]=rand($al,$av);}

$ataky=strtr($minn,array(2=>1,3=>2,4=>1,5=>2,6=>1,7=>2,8=>1,9=>2,10=>1,11=>2,13=>1,14=>2,15=>1));
if($ataky==1){$ta=$t1; $to=$t2; $pl=$pl1; $ps=$ps1; $id=$id1; $nva=$nv1; $nvo=$nv2; $plo=$pl2; $pso=$ps2; $ido=$id2;} if($ataky==2){$ta=$t2; $to=$t1; $pl=$pl2; $ps=$ps2; $id=$id2; $nva=$nv2; $nvo=$nv1; $plo=$pl1; $pso=$ps1; $ido=$id1;}
$hw=rand(1,100);
$atc=40; $atl=30; $atr=30;
if($ta[tak]==4){$atc=70; $atl=15; $atr=15;}
if($ta[tak]==5){$atc=20; $atl=40; $atr=40;}
if($hw<=$atc)$atak=1;
if($hw<=($atc+$atl) && $hw>$atc)$atak=2;
if($hw<=($atc+$atl+$atr) && $hw>($atc+$atl))$atak=3;
if($atak==1){
$i=false;
for($i=2;$i<12;$i++){
if($ps[$i]==5 || $ps[$i]==7)$ja1[]=$i;
if($ps[$i]==9 || $ps[$i]==10)$ja2[]=$i;

if($ps[$i]==2 || $ps[$i]==3 || $ps[$i]==4)$ja3[]=$i;
if($pso[$i]==7 || $pso[$i]==5)$jo1[]=$i;
if($pso[$i]==10 || $pso[$i]==9 || $pso[$i]==6 || $pso[$i]==8)$jo2[]=$i;
if($pso[$i]==3)$jo3[]=$i;}}
if($atak==2){
$i=false;
for($i=2;$i<12;$i++){
if($ps[$i]==2)$ja1[]=$i;
if($ps[$i]==6 || $ps[$i]==10)$ja2[]=$i;

if($ps[$i]==3 || $ps[$i]==4 || $ps[$i]==5)$ja3[]=$i;
if($pso[$i]==6)$jo1[]=$i;
if($pso[$i]==10 || $pso[$i]==8 || $pso[$i]==9)$jo2[]=$i;
if($pso[$i]==2)$jo3[]=$i;}}
if($atak==3){
$i=false;
for($i=2;$i<12;$i++){
if($ps[$i]==4)$ja1[]=$i;
if($ps[$i]==8 || $ps[$i]==10)$ja2[]=$i;
if($ps[$i]==3 || $ps[$i]==2 || $ps[$i]==5)$ja3[]=$i;
if($pso[$i]==8)$jo1[]=$i;
if($pso[$i]==4)$jo3[]=$i;
if($pso[$i]==6 || $pso[$i]==10 || $pso[$i]==9)$jo2[]=$i;}}
if(count($ja1)>1)$ia1=rand(0,(count($ja1)-1)); else $ia1=0;
if(count($ja2)>1)$ia2=rand(0,(count($ja2)-1)); else $ia2=0;

if(count($ja3)>1)$ia3=rand(0,(count($ja3)-1)); else $ia3=0;
if(count($jo1)>1)$io1=rand(0,(count($jo1)-1)); else $io1=0;
if(count($jo2)>1)$io2=rand(0,(count($jo2)-1)); else $io2=0;
if(count($jo3)>1)$io3=rand(0,(count($jo3)-1)); else $io3=0;
$ja1=$ja1[$ia1]; $ja2=$ja2[$ia2]; $jo1=$jo1[$io1]; $jo2=$jo2[$io2]; $jo3=$jo3[$io3]; $ja3=$ja3[$ia3];
$na=explode('|',$nva[$ja1]);
$no=explode('|',$nvo[$jo1]);
$rt1=$na[0]+$na[1]+$na[2];
$rt2=$no[0]+$no[4]+$no[4];
if($rt1==$rt2){
$suka=rand(1,2);
if($suka==1)$rt1=$rt1+1; if($suka==2)$rt2=$rt2+1;}
$text=''.$ta[name].' klubi '.strtr($atak,array(1=>'markazdan',2=>'o`ng qanotdan',3=>'chap qanotdan')).' hujum boshladi.';
if($rt1>$rt2){
$text.=' '.$pl[$ja1].' ';
$tj=''.$plo[$jo1].' bilan kurashda yutib chiqdi ';
if($na[0]>$no[0])$tj=''.$plo[$jo1].'dan tezlikda o`tib ketdi ';
if($na[1]>$no[4])$tj=' '.$plo[$jo1].'ni aldab o`tdi ';
$text.=$tj.' va to`pni '.$pl[$ja2].'ga uzatdi.';
$atak_proh=1;}else{
$text.=' '.$pl[$ja1].' ';
$tj=''.$plo[$jo1].' bilan kurashda yutqazdi ';
$h=rand(-2,7);
if($no[0]>($na[0]+$h))$tj=''.$plo[$jo1].'dan tezlikda qolib ketdi ';
if($no[1]>($na[4]+$h))$tj=''.$plo[$jo1].'ni alday olmadi ';
$text.=$tj.'va to`pni yo`qotdi, natijada '.$to[name].' klubida qarshi hujumga chiqish uchun yaxshi imkoniyat paydo bo`ldi. Birnecha tezkor uzatmalardan so`ng to`p '.$plo[$jo2].'ga uzatildi.';}
if($atak_proh!=1){

$k1=$ta;
$k2=$to; $k3=$nva; $k4=$nvo; $k5=$pl; $k6=$plo; $k7=$id; $k8=$ido; $k9=$ja1; $k10=$jo1; $k11=$ja2; $k12=$jo2; $k13=$ja3; $k14=$jo3;
$ta=$k2; $to=$k1; $nva=$k4; $nvo=$k3; $pl=$k6; $plo=$k5; $id=$k8; $ido=$k7; $ja1=$k10; $jo1=$k9; $ja2=$k12; $jo2=$k11; $ja3=$k14; $jo3=$k13;}
$na=explode('|',$nva[$ja2]);
$no=explode('|',$nvo[$jo3]);
$gko=explode('|',$nvo[1]);
$at=rand(1,3);
if($at==1){
$rg1=$na[5]+$na[6]+$na[7];
$rg2=$no[4]+$gko[8]+$gko[8];
if($rg1==$rg2){$he=rand(1,2); if($he==1)$rg1=$rg1+1; else $rg2=$rg2+1;}
if($rg1>$rg2){
$text.=' Va bu GOOOL! U to`pni qabul qildi va ';
$tj='uzoqdan zarba berdi. To`p raqib darvozasiga to`singa tegib kirdi.';
if($na[6]>$gko[8])$tj='raqib darvozasining uzoq burchagiga to`pni yo`lladi, darvozabonga imkoniyat qoldirmadi. ';
if($na[7]>$gko[8])$tj='aniq zarba bilan to`pni darvozabonning yaqin burchagiga yo`lladi, to`p to`singa tegib darvozaga kirdi. ';
if($na[6]>$gko[8] && $na[7]>$gko[8])$tj='aniq va kuchli zarba yo`lladi. Darvozabon hatto o`rnidan qo`zg`alishga ham ulgurmadi.';
$gol=1;}else{
$tj=' Ammo u omadsiz o`ynadi va to`pni yo`qotdi.';
if($gko[8]>$na[6])$tj=' U to`pni qabul qildi va raqib darvozasiga yo`lladi, ammo zarba kuchsiz bo`ldi va darvozabon to`pni qiyinchiliksiz ushlab oldi.';
if($gko[8]>$na[7])$tj=' U to`pni qabul qildi va zarbaaa!!! Uuux qanday noaniq... Ehtimol, tomoshabinlar to`pni ushlab olishidan xursand bo`ldi.';
}
$text.=$tj;
}
if($at==3){
$rg1=$na[3]+$na[3]+$na[3];
$rg2=$no[3]+$no[3]+$gko[8];
if($rg1==$rg2){$he=rand(1,2); if($he==1)$rg1=$rg1+1; else $rg2=$rg2+1;}
if($rg1>$rg2){
$text.=' GOOOOOL!!!! U ikkinchi qavatda '.$plo[$jo3].' bilan o`ynadi va to`pni raqib darvozabon yoniga yo`lladi. Darvozabon to`p yo`nalishini topa oldi, ammo darvozabonning barmoqlarining uchi gina to`pga tegdi.';
$gol=1;}else{
$text.=' Ammo u ikkinchi qavatda '.$plo[$jo3].' bilan kurashda yutqazdi va to`pni yo`qotdi.';
}}
if($at==2){
$rg1=$na[5]+$na[0]+$na[1];
$rg2=$no[4]+$no[0]+$gko[8];
if($rg1==$rg2){$he=rand(1,2); if($he==1)$rg1=$rg1+1; else $rg2=$rg2+1;}
if($rg1>$rg2){
$text.=' GOOOL!!!! U to`pni qabul qildi';
$tj=', va '.$plo[$jo3].' ning yonidan o`tkazib to`pni raqib darvozasiga aniq yo`lladi. Darvozabonda imkoniyat yo`q edi!';
if($na[1]>$no[4])$tj=', '.$plo[$jo3].'ni ajoyib tarzda aldab o`tdi va to`pni raqib darvozasiga yo`lladi. Darvozabon hatto to`pga qarashga ham ulgurmadi.';
if($na[0]>$no[0])$tj=', takrorlanmas tezlik bilan '.$plo[$jo3].'dan o`tib ketdi va to`pni raqib darvozasi tomon yo`naltirdi. ';
$gol=1;}else{
$tj='Yo`q yo`q bu qanday qilib??? U o`z oyoqlariga elakishib qoldi va '.$plo[$jo3].'  undan to`pni olib qo`ydi.';
if($no[4]>$na[5])$tj='Ammo u to`pni yaxshi qabullay olmadi va '.$plo[$jo3].' undan to`pni olib qo`ydi.';
}
$text.=$tj;
}

if($gol==1){
if($t1[id]==$ta[id])$sc1=$sc1+1; else $sc2=$sc2+1;
$text.=' '.$sc1.'-'.$sc2;
$mat[$si]=$r[min].'|'.$text.'|2|'.$pl[$ja2].'|'.$id[$ja2].'|'.$ta[id];
//плюсуем гол
mysql_query("update `fman_players` set `goals`=`goals`+1 where `id`= '$id[$ja2]' limit 1");
}else $mat[$si]=$r[min].'|'.$text.'|1|';
} if($minn==12 || $minn==16){
if(!$mat[cupp]){$text='Final hushtagi yangradi match '.$sc1.'-'.$sc2.' hisobi bilan yakunlandi.'; $kon=1;}
if($mat[cupp] && $minn==12)$text='Hushtak chalindi, asosiy vaqt '.$sc1.'-'.$sc2.' bilan yakunlandi. Bizni qo`shimcha vaqtlar kutmoqda.';
if($mat[cupp] && $minn==16){
	
///////////////////////////////////////////////////////////////////
    //////////////////           ПЕНАЛЬТИ          //////////////////////////
    ///////////////////////////////////////////////////////////////////	
	$rezult[0]=$sc1;
$rezult[1]=$sc2;
$lastid = mysql_insert_id();


    if ($rezult[0] == $rezult[1])
    {
    $input = array ("5:3", "5:4", "4:2", "4:3", "3:2", "3:5", "4:5", "2:4", "3:4", "2:3");
    $rand_keys = array_rand ($input);

    $penult = explode(":",$input[$rand_keys]);

    $pen1 = $penult[0];
    $pen2 = $penult[1];
    }
	
if ($mat[turnir]=='1')
			{
if ($pen1 > $pen2) {
	$text='Asosiy va qo`shimcha vaqt '.$sc1.'-'.$sc2.' hisobi bilan yakunlandi. O`yin g`olib 11 metrlik zarbalardan keyin aniqlanadi. Uzoq kurashdan so`ng '.$pen1.'-'.$pen2.' hisobini <b>'.$t1[name].'</b> klubi saqlab qoldi!';
} else {
	$text='Asosiy va qo`shimcha vaqt '.$sc1.'-'.$sc2.' hisobi bilan yakunlandi. O`yin g`olib 11 metrlik zarbalardan keyin aniqlanadi. Uzoq kurashdan so`ng '.$pen1.'-'.$pen2.' hisobini <b>'.$t2[name].'</b> klubi saqlab qoldi!';
}
			} else {
	$x1=rand(3,4);
$x2=rand(1,2);
if($x2==1)$ta=$t1;
if($x2==2)$ta=$t2;
$text='Asosiy va qo`shimcha vaqt '.$sc1.'-'.$sc2.' hisobi bilan yakunlandi. O`yin g`olib 11 metrlik zarbalardan keyin aniqlanadi. Uzoq kurashdan so`ng 5-'.$x1.' hisobini <b>'.$ta[name].'</b> klubi saqlab qoldi!'; 
 }

$kon=1;}
if($kon==1){
$rait='
';
$mat[end]=1;
$mat[rait]=$rait;
if($mat[turn]=="tm")$statm=1;
if($mat[turn]=="champ" || $mat[turn]=="cup" || $mat[turn]=="supercup")$statm=2;
if(!$statm)$statm=3;
for($c=1;$c<12;$c++){
$qp1=mysql_query("select * from fman_players where id=$id1[$c];");
$ap1=mysql_fetch_array($qp1);
$qp2=mysql_query("select * from fman_players where id=$id2[$c];");
$ap2=mysql_fetch_array($qp2);
if($t1[user_id])mysql_query("update fman_players set ball=".(($ap1[rait]*$statm)+$ap1[ball])." where id=$ap1[id]");
if($t2[user_id])mysql_query("update fman_players set ball=".(($ap2[rait]*$statm)+$ap2[ball])." where id=$ap2[id]");}}
$sc1=$mat[sc1]; $sc2=$mat[sc2];
$v1=0; $n1=0; $p1=0; $zg1=$sc1; $pg1=$sc2; $o1=0;
$v2=0; $n2=0; $p2=0; $zg2=$sc2; $pg2=$sc1; $o2=0;
if($sc1>$sc2){$v1=1; $p2=1; $o1=3;}
if($sc2>$sc1){$v2=1; $p1=1; $o2=3;}
if($sc1==$sc2){$n1=1; $n2=1; $o1=1; $o2=1;}
$or1=$o1*$statm;
mysql_query("update fman_clubs set `budj`=`budj`+$t1[spons] where id=$t1[id]");
mysql_query("update fman_clubs set fan=".($t1[fan]+$or1)." where id=$t1[id]");
mysql_query("update fman_clubs set oput=".($t1[oput]+20)." where id=$t1[id]");
mysql_query("update fman_clubs set oput=".($t2[oput]+20)." where id=$t2[id]");

if ($mat[sc1]>$mat[sc2] || $mat[pen1]>$mat[pen2]) {
mysql_query("update fman_clubs set win=".($t1[win]+1)." where id=$t1[id]");
mysql_query("update fman_clubs set lose=".($t2[lose]+1)." where id=$t2[id]");
} elseif ($mat[sc1]==$mat[sc2] && $mat[pen1]==0 && $mat[pen2]==0) {
mysql_query("update fman_clubs set draw=".($t1[draw]+1)." where id=$t1[id]");
mysql_query("update fman_clubs set draw=".($t2[draw]+1)." where id=$t2[id]");
} elseif($mat[sc1]<$mat[sc2] || $mat[pen1]<$mat[pen2]){
mysql_query("update fman_clubs set win=".($t2[win]+1)." where id=$t2[id]");
mysql_query("update fman_clubs set lose=".($t1[lose]+1)." where id=$t1[id]");
} elseif ($mat[sc1]==$mat[sc2] && $mat[pen1]>$mat[pen2]) {
mysql_query("update fman_clubs set win=".($t1[win]+1)." where id=$t1[id]");
mysql_query("update fman_clubs set lose=".($t2[lose]+1)." where id=$t2[id]");
} elseif ($mat[sc1]==$mat[sc2] && $mat[pen1]<$mat[pen2]) {
mysql_query("update fman_clubs set win=".($t2[win]+1)." where id=$t2[id]");
mysql_query("update fman_clubs set lose=".($t1[lose]+1)." where id=$t1[id]");
}
if ($mat[turnir]==1)  {
mysql_query("update `r_cupgame` set `rez1`='" . $mat['sc1'] . "', `rez2`='" . $mat['sc2'] . "',`pen1`='".$pen1."', `pen2`='".$pen2."', `id_report`='" . $mat['id'] . "' where `id`='" . $mat['id_match'] . "'");
}

if($t1['spons']>0)
{
newsadd("$t2[name]ga qarshi matchdan so`ng sponsorlardan $$t1[spons] klub byudjetiga qo`shildi.",$t1[id]);
}
$or2=$o2*$statm;
mysql_query("update fman_clubs set `budj`=`budj`+$t2[spons] where id=$t2[id]");
mysql_query("update fman_clubs set fan=".($t2[fan]+$or2)." where id=$t2[id]");
if($t2['spons']>0)
{
newsadd("$t1[name]ga qarshi matchdan so`ng sponsorlardan $$t2[spons] klub byudjetiga qo`shildi.",$t2[id]);
}
if($mat[turn]=="champ" && $mat[tur]>($t1[v]+$t1[n]+$t1[p])){
mysql_query("update fman_clubs set v=".($t1[v]+$v1).", n=".($t1[n]+$n1).", p=".($t1[p]+$p1).", zg=".($t1[zg]+$zg1).", pg=".($t1[pg]+$pg1).", o=".($t1[o]+$o1)." where id=$t1[id]");
mysql_query("update fman_clubs set v=".($t2[v]+$v2).", n=".($t2[n]+$n2).", p=".($t2[p]+$p2).", zg=".($t2[zg]+$zg2).", pg=".($t2[pg]+$pg2).", o=".($t2[o]+$o2)." where id=$t2[id]");
$qp=false; $aj=false;
$qp=mysql_query("select * from fman_players where team_id=$t1[id];");
$z1=0;
while($aj=mysql_fetch_array($qp)){
$markt=(1000000*$aj[rait])+(($aj[u]+$aj[vyn]+$aj[ig]+$aj[otb]+$aj[drib]+($aj[gk]*4)+$aj[f]+$aj[toch]+$aj[priem]+$aj[pas])*500000);
$zarplat=round(($markt/800));
$z1=$z1+$zarplat;}
$qp=false; $aj=false;
$qp=mysql_query("select * from fman_players where team_id=$t2[id];");
$z2=0;
while($aj=mysql_fetch_array($qp)){
$markt=(1000000*$aj[rait])+(($aj[u]+$aj[vyn]+$aj[ig]+$aj[otb]+$aj[drib]+($aj[gk]*4)+$aj[f]+$aj[toch]+$aj[priem]+$aj[pas])*500000);
$zarplat=round(($markt/800));
$z2=$z2+$zarplat;}
$sod1=20*$t1[stadv]; $sod2=20*$t2[stadv];
mysql_query("update fman_clubs set budj=".($t1[budj]-($z1+$sod1))." where id=$t1[id]");
mysql_query("update fman_clubs set budj=".($t2[budj]-($z2+$sod2))." where id=$t2[id]");
newsadd("<b>Haftalik finans yangilanish</b><br/>O`yinchilarga to`lov: $".number_format($z1,0,'.',' ')."<br/>Stadion uchun: $".number_format($sod1,0,'.',' ')."<br/>Yangilanishgacha byudjet: $".number_format($t1[budj],0,'.',' ')."<br/>Yangilanishdan keyingi byudjet: $".number_format(($t1[budj]-($z1+$sod1)),0,'.',' ')."",$t1[id]);
newsadd("<b>Haftalik finans yangilanish</b><br/>O`yinchilarga to`lov: $".number_format($z2,0,'.',' ')."<br/>Stadion uchun: $".number_format($sod2,0,'.',' ')."<br/>Yangilanishgacha byudjet: $".number_format($t2[budj],0,'.',' ')."<br/>Yangilanishdan keyingi byudjet:$".number_format(($t2[budj]-($z2+$sod2)),0,'.',' ')."",$t2[id]);}
if($minn==12)$mn=90; else $mn=120;
$mat[$si]=$mn.'|'.$text.'|1|';}
mysql_query("update fman_matches set end=$mat[end], sc1='$sc1', sc2='$sc2', pen1='$pen1', pen2='$pen2',  ".$si."='$mat[$si]' where id=$mat[id]");}
function siv($id){
$realtime=time();

$qjtt=mysql_query("select * from fman_matches where id=$id;");
	$m=mysql_fetch_array($qjtt);
	/*tarkib avto tanlash*/
	complite_team($m['id1']);
	complite_team($m['id2']);
	
for($i=1;$i<17;$i++){
$qjtt=mysql_query("select * from fman_matches where id=$id;");
$m=mysql_fetch_array($qjtt);
$si="s".$i;
$mmj=explode('|',$m[$si]);
$n=0; $c=0;
$t=$m[turn];
if(($t=="cup" || $t=="lc" || $t=="le") && $m[tur]==11)$n=1;
if($t=="sc" || $t=="supercup")$n=1;
if(($t=="sc" || $t=="supercup" || $t=="cup") && $m[sc1]==$m[sc2])$c=1;
if(($t=="le" || $t=="lc") && $m[tur]==11 && $m[sc1]==$m[sc2])$c=1;
if(($t=="le" || $t=="lc") && ($m[tur]==1162 || $m[tur]==182 || $m[tur]==142 || $m[tur]==122)){
$pt=$m[tur]-1;
$sq=mysql_query("select sc1, sc2 from fman_matches where turn='$t' and tur=$pt and id1=$m[id2] and id2=$m[id1];");
$sd=mysql_fetch_array($sq);
if($m[sc1]==$sd[sc1] && $m[sc2]==$sd[sc2])$c=1;}
$m[neit]=$n; $m[cupp]=$c;

if($c==1 && $i==12)$cj=1;
if($m[time]<($realtime-(60*$i)) && !$mmj[1] && ($i<13 || $cj)){
$qjt1=mysql_query("select * from fman_clubs where id=$m[id1];");
$td1=mysql_fetch_array($qjt1);
$qjt2=mysql_query("select * from fman_clubs where id=$m[id2];");
$td2=mysql_fetch_array($qjt2);
$pl1=playersmas(1,$td1);
$nv1=playersmas(2,$td1);
$ps1=playersmas(3,$td1);
$pl2=playersmas(1,$td2);
$id1=playersmas(4,$td1);
$id2=playersmas(4,$td2);
$nv2=playersmas(2,$td2);
$ps2=playersmas(3,$td2);
addsobytie($i,$m,$td1,$td2,$pl1,$pl2,$nv1,$nv2,$ps1,$ps2,$id1,$id2);
}}}
?>