<?php
defined('_IN_JOHNCMS') or die('Error: restricted access');

require_once("ab.php");

$realtime = time();

$trans=1;
	
function land($land,$type)
{
	$q = mysql_query("select * from `fman_lands` where `code` = '$land' limit 1;");
	
	$a = mysql_fetch_array($q);
	if($type=='im')
	{
		$type='name';
	}
	elseif($type=='link')
	{
		$a['link']='<a href="index.php?act='.$land.'"><img src="flags_icon/'.$land.'.png" alt="'.$land.'"/> '.$a['name'].'</a>';
	}
	
	return $a[$type];
}

function count_players($id)
{
	return mysql_result(mysql_query("select count(*) from `fman_players` where `team_id` = ".intval($id)),0);
}
function newsadd($text,$id) {
	$text=functions::check($text);
mysql_query("insert into `fman_news` set `time`='".time()."', `pl_id`='0', `text`='".mysql_real_escape_string($text)."', `club_id`=".intval($id));
}

function turn($turn,$tur,$land)
{
	if($turn=="sc")$r="UEFA superkubogi";
	if($turn=="lc")$r="Chempionlar ligasi";
	if($turn=="le")$r="Yevropa ligasi";
	if(($turn=="le" || $turn=="lc") && $tur){
		if($land!="eu"){
			$r1=". ".strtr($tur,array(116=>'1/16 final',18=>'1/8 final',14=>'1/4 final',12=>'1/2 final',11=>'Final'));
			$r.=". ".strtr($r1,array('а1'=>'а. Dastlabki o`yin','а2'=>'а. Javob o`yini'));
		}
		else
		{
			$r.=". Guruh ".strtr($r1,array('a'=>'A','b'=>'B','c'=>'C','d'=>'D','e'=>'E','f'=>'F','g'=>'G','h'=>'H','i'=>'I','j'=>'J','k'=>'K','l'=>'L')).". $tur tur";
		}
	}
	if($turn=="cup"){$r="Kubok ".land($land,'rod');
	if($tur)$r.=". ".strtr($tur,array(1=>'Saralash turniri',18=>'1/8 final',14=>'1/4 final',12=>'1/2 final',11=>'Final'));}
	if($turn=="supercup")$r="Superkubok ".land($land,'rod');
	if($turn=="champ"){$r="Chempionat ".land($land,'rod').". ".land($land,'lig');
	if($tur)$r.=". $tur tur";}
	if($turn=="tm")$r="O`rtoqlik matchi";
	return $r;
}
function match($a,$id,$sub)
{
	$realtime=time();
	if(!$id)$id='jmdgjag';
	echo"<div class='menu'>".functions::display_date($a[time])."<br/>";
	if($a['id1']!=$id)echo"<a href='club.php?id=$a[id1]'>$a[kom1]</a> - "; else echo"<b>$a[kom1]</b> - ";
	if($a['id2']!=$id)echo"<a href='club.php?id=$a[id2]'>$a[kom2]</a>"; else echo"<b>$a[kom2]</b>"; $team=$a[kom2];
	if($a['end']==1)echo" <a href='match.php?id=$a[id]'>$a[sc1]:$a[sc2]</a>"; else{
	if($realtime>$a['time'])echo" <a href='match.php?id=$a[id]'><b>$a[sc1]:$a[sc2]</b></a>";}
	if($sub)echo"<div class='sub'>$sub</div>";
	echo"</div>";
}
function navyk($var){
	$k=0;
	if($var==0)$i="yo`q";
	if($var==1)$i="o'ta yomon";
	if($var==2)$i="juda yomon";
	if($var==3)$i="yomon";
	if($var==4)$i="kuchsiz";
	if($var==5)$i="yetarli emas";
	if($var==6)$i="chidasa bo`ladi";
	if($var==7)$i="yaxshi";
	if($var==8)$i="a`lo";
	if($var==9)$i="yorqin";
	if($var==10)$i="havas qiladigan";
	if($var==11)$i="qoilmaqom";
	if($var==12)$i="zo`r";
	if($var==13)$i="jahon miqyosidagi";
	if($var==14)$i="g`ayritabiiy";
	if($var==15)$i="katta hajmda";
	if($var==16)$i="meyoridan ortiq";
	if($var==17)$i="ertakdagidek";
	if($var==18)$i="sehrli";
	if($var==19)$i="afsonaviy";
	if($var>19)$i="hudo bergan";
	return $i;
}

function zamena($m,$pl)
{
	if($pl[pos]!=1)$hu="pos!=1"; else $hu="pos=1";
	$q=mysql_query("select id from fman_players where id!=$m[p1] and id!=$m[p2] and id!=$m[p3] and id!=$m[p4] and id!=$m[p5] and id!=$m[p6] and id!=$m[p7] and id!=$m[p8] and id!=$m[p9] and id!=$m[p10] and id!=$m[p11] and team_id=$m[id] and ".$hu." order by rait desc limit 1;");
	if($q){
		if(!mysql_num_rows($q)){$q=false;
		$q=mysql_query("select id from fman_players where id!=$m[p1] and id!=$m[p2] and id!=$m[p3] and id!=$m[p4] and id!=$m[p5] and id!=$m[p6] and id!=$m[p7] and id!=$m[p8] and id!=$m[p9] and id!=$m[p10] and id!=$m[p11] and team_id=$m[id] order by rait desc limit 1;");}
		$a=mysql_fetch_array($q);
		if($m['p1']==$pl['id'])$d='p1';
		if($m['p2']==$pl['id'])$d='p2';
		if($m['p3']==$pl['id'])$d='p3';
		if($m['p4']==$pl['id'])$d='p4';
		if($m['p5']==$pl['id'])$d='p5';
		if($m['p6']==$pl['id'])$d='p6';
		if($m['p7']==$pl['id'])$d='p7';
		if($m['p8']==$pl['id'])$d='p8';
		if($m['p9']==$pl['id'])$d='p9';
		if($m['p10']==$pl['id'])$d='p10';
		if($m['p11']==$pl['id'])$d='p11';
		if($d)mysql_query("update fman_clubs set ".$d."=$a[id] where id=$m[id]");
	}
}

$qka=mysql_query("select * from `fman_clubs` where `user_id`='$user_id' limit 1;");

if(mysql_num_rows($qka) && $user_id)
{
	$dataclub=mysql_fetch_array($qka);
	$club_id=$dataclub['id'];
	$club_name=$dataclub['name'];
	if($dataclub['u9']<1)
	{
		$club_name=$dataclub['name'].' (so`rov moderatsiya jarayonida)';
		$dataclub=false;
		$club_id=false;
	}
}

$qam=mysql_query("select `id`, `id1`, `id2` from `fman_matches` where `time`<$realtime and `end`=0;");

if($qam && $club_id)
{
	while($m=mysql_fetch_array($qam))
	{
		if($club_id==$m['id1'] || $club_id==$m['id2'] || $rights>=7 || $user_id==1)siv($m['id']);
	}
}

$quvl=mysql_query("select `budj` from `fman_clubs` where id=131;");
$qaqa=mysql_fetch_array($quvl);

if($qaqa['budj']<($realtime-172800))
{
	$qj124=mysql_query("select * from `fman_clubs`");
	
	while($a124=mysql_fetch_array($qj124))
	{
		$kmes4=$a124['stad_ned']-1;
		if($a124['stad_ned'])mysql_query("update fman_clubs set stad_ned=$kmes4 where id=$a124[id]");
		if($kmes4==0)
		{
			mysql_query("update fman_clubs set stadv=$a124[stad_work] where id=$a124[id]");
			newsadd("O`yingohda $a124[stadion] qayta tamirlash ishlari yakunlandi. Sig`imi $a124[stad_work] o`rin bo`lgan yangi arena, eski sig`imi $a124[stadv] o`rin bo`lgan o`rnini eganlaydi.",$a124[id]);
		}
	}
	
	$qj125=mysql_query("select id, travm from players;");
	

	{
		if($a125['travm'])mysql_query("update fman_clubs set stad_ned=".($a125[travm]-1)." where id=$a125[id]");
	}
	
	mysql_query("update `fman_clubs` set `uvol_count`=0, `buy_count`=0, `trans_count`=0");
	mysql_query("update `fman_clubs` set `budj`=$realtime where `id`=131");
}

if($club_id)
{
	$qhui=mysql_query("select * from fman_news where club_id=$club_id order by time desc limit 2;");
	while($ahui=mysql_fetch_array($qhui))
	{
		$thui[]=$ahui['text'];
		$ihui[]=$ahui['id'];
	}
	
	if($thui[0]==$thui[1])mysql_query("delete from fman_news where id=$ihui[0];");
}

$qtpj=mysql_query("select * from fman_players where trans=1 and trans_time<$realtime;");

while($atpj=mysql_fetch_array($qtpj))
{
	if($club_id==$atpj[team_id] || $club_id==$atpj[zarplat] || $rights>0)
	{
		if(!$atpj['zarplat'] && $atpj[team_id])
		{
			mysql_query("update fman_players set trans=0, zarplat=0 where id=$atpj[id]");
		}
		if(!$atpj['zarplat'] && !$atpj[team_id])
		{
			mysql_query("update fman_players set trans_time=".(($realtime+259200)-date("s"))." where id=$atpj[id]");
		}
		if($atpj['zarplat'])
		{
			mysql_query("update fman_players set team_id=$atpj[zarplat], trans=0, zarplat=0 where id=$atpj[id]");
			$qtpj1=mysql_query("select * from fman_clubs where id=$atpj[team_id];");
			$atpj1=mysql_fetch_array($qtpj1);
			$qtpj2=mysql_query("select * from fman_clubs where id=$atpj[zarplat];");
			$atpj2=mysql_fetch_array($qtpj2);
			mysql_query("update fman_clubs set budj=".($atpj2[budj]-$atpj[cena])." where id=$atpj2[id]");
			mysql_query("insert into fman_news set time=$realtime, pl_id=$atpj[id], name='$atpj[name]', id1=$atpj[team_id], id2=$atpj[zarplat], club_id=0");
			if($atpj[team_id]){
		
			mysql_query("update fman_clubs set budj=".($atpj1[budj]+$atpj[cena])." where id=$atpj1[id]");
			newsadd("Futbolchingiz <a href='player.php?id=$atpj[id]'>$atpj[name]</a>  <a href='club.php?id=$atpj[zarplat]'>$atpj2[name]</a>klubiga sotildi. Transfer summasi <b>$".number_format($atpj[cena],0,'.',' ')."</b> ni tashkil etdi.",$atpj[team_id]);
			newsadd("Sizning klubingizga <a href='club.php?id=$atpj[team_id]'>$atpj1[name]</a> klubidan <a href='player.php?id=$atpj[id]'>$atpj[name]</a> transfer qilindi. Transfer summasi <b>$".number_format($atpj[cena],0,'.',' ')."</b> ni tashkil etdi.",$atpj[zarplat]);}
			
			
			
		}
	}
}

if($club_id)
{
	if($dataclub['last_pay']==0)
	{
		mysql_query("update `fman_clubs` set `last_pay`=".time()." where `id`='$club_id'");
	}
	else
	{
		$kpays = floor((time()-$dataclub['last_pay'])/86400);
		if($kpays>=1)
		{
			$plus = $dataclub['plus']*$kpays;
			mysql_query("update `fman_clubs` set `last_pay`=".time().", `budj`=`budj`+'$plus' where `id`='$club_id'");
		}
	}
}

?>
 