<?php
#----------------------------------------------------------#
# 順位表示
#----------------------------------------------------------#
function team_rankR($refresh){
global$disp,$Q,$SET;
global$D,$RESULT;
#--------------------------------------#
# データチェック
#--------------------------------------#
$db=sql_open();
check_id($_COOKIE[id],$_COOKIE[pass]);
$PL=read_data('player',$_COOKIE[id]);
$TM=read_data('team',$_COOKIE[id]);
$title=title(1);
#--------------------------------------#
# データ集約
#--------------------------------------#
$TIME=read_data('time','time');
$sql=mysql_query("SELECT * FROM team");
while ($OTM=mysql_fetch_assoc($sql)){
	$OL=read_data('player',$OTM[id]);
	#勝率
	if($OTM[2]&&$OTM[2]!=$OTM[5]){
		$RES=$OTM[3];
	}else{
		$RES=0;
	}
	$RES_WINPER[$OTM[id]]=AVE(($OTM[2]-$OTM[5]),$OTM[3]);
	#集約
	if($OL[20]==1){
		$RES1[$RES]=SET_STR_AR($RES1,$RES,$OTM[id],'!');
	}elseif($OL[20]==2){
		$RES2[$RES]=SET_STR_AR($RES2,$RES,$OTM[id],'!');
	}elseif($OL[20]==3){
		$RES3[$RES]=SET_STR_AR($RES3,$RES,$OTM[id],'!');
	}
	#試合登録なし
	if(!$OL[7]){continue;}
	#1部の選手成績集約
	if($OL[20]==3){
		#投・野合同
		$sql2=mysql_query("SELECT * FROM player_card WHERE player_id='$OTM[id]'");
		while ($CD=mysql_fetch_assoc($sql2)){
			$CD_RB=read_data('card_res_b',$CD[id]);
			$CD_RP=read_data('card_res_p',$CD[id]);
			$CD_DATA=$CD[id].'?'.$OTM[id];
			#野手
			if($CD_RB[1]>=($TIME[num]*3.1)){
				#OPS
				$RES=AVE(($CD_RB[2]+$CD_RB[15]+$CD_RB[16]+$CD_RB[17]+$CD_RB[14]),($CD_RB[3]+$CD_RB[15]+$CD_RB[16]+$CD_RB[17]))+AVE($CD_RB[2],$CD_RB[8]);
				ranking_set('OPS',$RES,$CD_DATA);
				#AVE
				$RES=AVE($CD_RB[2],$CD_RB[3]);
				ranking_set('AVE',$RES,$CD_DATA);
				#BAS
				$RES=AVE($CD_RB[20],$CD_RB[21]);
				ranking_set('BAS',$RES,$CD_DATA);
				#OBP
				$RES=AVE(($CD_RB[2]+$CD_RB[15]+$CD_RB[16]+$CD_RB[17]+$CD_RB[14]),($CD_RB[3]+$CD_RB[15]+$CD_RB[16]+$CD_RB[17]));
				ranking_set('OBP',$RES,$CD_DATA);
				#SLG
				$RES=AVE($CD_RB[2],$CD_RB[8]);
				ranking_set('SLG',$RES,$CD_DATA);
			}
				#HR
				ranking_set('HR',$CD_RB[7],$CD_DATA);
				#RUNS
				ranking_set('RUNS',$CD_RB[10],$CD_DATA);
				#RBI
				ranking_set('RBI',$CD_RB[9],$CD_DATA);
				#SB
				ranking_set('SB',$CD_RB[11],$CD_DATA);
				#SO
				ranking_set('BSO',$CD_RB[18],$CD_DATA);
				#ER
				ranking_set('ER',$CD_RB[23],$CD_DATA);
				#DP
				ranking_set('DP',$CD_RB[19],$CD_DATA);
			#投手
			if($CD_RP[11]>=($TIME[num]*3)){
				#ERA
				$RES=ERA($CD_RP[11],$CD_RP[9]);
				ranking_set('ERA',$RES,$CD_DATA);
				#WHIP
				$RES=WHIP($CD_RP[11],($CD_RP[3]+$CD_RP[15]+$CD_RP[16]));
				ranking_set('WHIP',$RES,$CD_DATA);
				#SO
				if($CD_RP[11]>0){
					$RES=floor($CD_RP[18]/$CD_RP[11]*27*100)/100;
				}else{
					$RES=0;
				}
				ranking_set('PSO',$RES,$CD_DATA);
			}
				#WIN
				ranking_set('WIN',$CD_RP[22],$CD_DATA);
				#LOSE
				ranking_set('LOSE',$CD_RP[23],$CD_DATA);
				#SAVE
				ranking_set('SAVE',$CD_RP[24],$CD_DATA);
		}
	}
}
#--------------------------------------#
# 順位データ出力
#--------------------------------------#
#$html_rank1=rank_set($RES1,1);
#$html_rank2=rank_set($RES2,2);
$html_rank3=rank_set($RES3,3);
#--------------------------------------#
# ランキング出力
#--------------------------------------#
#P
$P_ERA=ranking_html('ERA','D');
$P_WHIP=ranking_html('WHIP','D');
$P_SO=ranking_html('PSO','U');
$P_WIN=ranking_html('WIN','U');
$P_LOSE=ranking_html('LOSE','U');
$P_SAVE=ranking_html('SAVE','U');
#B
$B_OPS=ranking_html('OPS','U');
$B_AVE=ranking_html('AVE','U');
$B_BAS=ranking_html('BAS','U');
$B_OBP=ranking_html('OBP','U');
$B_SLG=ranking_html('SLG','U');
$B_HR=ranking_html('HR','U');
$B_RUNS=ranking_html('RUNS','U');
$B_RBI=ranking_html('RBI','U');
$B_SB=ranking_html('SB','U');
$B_SO=ranking_html('BSO','U');
$B_ER=ranking_html('ER','U');
$B_DP=ranking_html('DP','U');
#--------------------------------------#
sql_close($db);
#--------------------------------------#
$disp2.=head();
$disp2.=html_bar();
$disp2.=$title;
$disp2.=<<<EOF
<tr><td colspan=2>
<b>順位</b><blockquote>
<table>
<tr><th>順位</th><th colspan=3>名前</th><th>勝率</th><th>試合</th><th>勝</th><th>敗</th><th>分</th><th>得点</th><th>失点</th><th>得失</th></tr>
$html_rank3</table>
</blockquote><hr>
<b>打者ランキング</b><blockquote>
<table>
<tr style="text-align:center;"><th>打率</th><th>OPS</th><th>得点圏打率</th></tr>
<tr valign=top><td><table>$B_AVE</table></td><td><table>$B_OPS</table></td><td><table>$B_BAS</table></td></tr>
<tr style="text-align:center;"><th>出塁率</th><th>長打率</th><th>ホームラン数</th></tr>
<tr valign=top><td><table>$B_OBP</table></td><td><table>$B_SLG</table></td><td><table>$B_HR</table></td></tr>
<tr style="text-align:center;"><th>得点数</th><th>打点数</th><th>盗塁数</th></tr>
<tr valign=top><td><table>$B_RUNS</table></td><td><table>$B_RBI</table></td><td><table>$B_SB</table></td></tr>
<tr style="text-align:center;"><th>三振数</th><th>併殺数</th><th>失策数</th></tr>
<tr valign=top><td><table>$B_SO</table></td><td><table>$B_DP</table></td><td><table>$B_ER</table></td></tr>
</table>
</blockquote>
<hr>
<b>投手ランキング</b><blockquote>
<table>
<tr style="text-align:center;"><th>防御率</th><th>WHIP</th><th>奪三振率</th></tr>
<tr valign=top><td><table>$P_ERA</table></td><td><table>$P_WHIP</table></td><td><table>$P_SO</table></td></tr>
<tr style="text-align:center;"><th>勝ち数</th><th>負け数</th><th>セーブ数</th></tr>
<tr valign=top><td><table>$P_WIN</table></td><td><table>$P_LOSE</table></td><td><table>$P_SAVE</table></td></tr>
</table>
</blockquote>
</td></tr>
EOF;
$disp2.=foot();
#出力用
	#更新用
	if($refresh){
		#------------------#
		$dbW=sql_open();
		#------------------#
		#優勝チーム出力
		$WIDt=explode('!',$D[rank_no1]);
		if(count($WIDt)>1){
			foreach($WIDt as $key=>$val){
				$OTM=read_data('team',$val);
				$RESPS[$val]=($OTM[6]-$OTM[7]);
			}
			arsort($RESPS);
			foreach($RESPS as $key=>$val){
				$WID=$key;
				break;
			}
		}else{
			$WID=$D[rank_no1];
		}
		$WIN=array();
		$WIN[id]=$TIME[no];
		$WL=read_data('player',$WID);
		$WTM=read_data('team',$WID);
		$WIN[wid]=$WID;
		$WIN[tname]=$WL[1];
		$WIN[win]=$WTM[3];
		$WIN[lose]=$WTM[4];
		$WIN[draw]=$WTM[5];
		$WIN[pt]=$WTM[6];
		$WIN[lpt]=$WTM[7];
		
		$WIN_COL=insert_colum($WIN);
		$WIN_DATA=insert_data($WIN);
		$sql="INSERT INTO winner $WIN_COL VALUES $WIN_DATA";
		mysql_query($sql,$dbW) or error("WINNERの作成に失敗しました...接続を確認してください($sql)");
		#------------------#
		#sql_close($dbW);
		#------------------#
		#出力
		return $disp2;
	}else{
		$disp.=$disp2;
	}
}
#----------------------------------------------------------#
# HTML化
#----------------------------------------------------------#
function team_rank_htmlR(){
global$TIME;
	$html=team_rankR(1);
	$filename='cap'.$TIME[no].'.html';
	$FO = fopen($filename, 'w+');
	// 文字列を書き出します。
	fwrite($FO,$html);
	// ファイルをクローズします。
	fclose($FO);
}
#----------------------------------------------------------#
# 順位表示
#----------------------------------------------------------#
function rank_set($RES,$L){
global$D,$RES_WINPER,$SET,$disp;
if($RES){
	$no=1;
	krsort($RES);
	foreach($RES as $key=>$val){
		$IDs=explode('!',$val);
		$RESP=array();
		foreach($IDs as $ID){
			$OTM=read_data('team',$ID);
			$PWINPER=AVE(($OTM[2]-$OTM[5]),$OTM[3]);
			$RESP["$PWINPER"]=SET_STR_AR($RESP,$PWINPER,$ID,'!');
		}
		krsort($RESP);
		foreach($RESP as $WINPER=>$val){
			$IDt=explode('!',$val);
			$n=0;
			#外へ呼び出し
			if($L==3&&$no==1){
				$D[rank_no1]=$val;
			}
			#同率
			if(count($IDt)>1){
				$RESPS=array();
				foreach($IDt as $key=>$val){
					$OTM=read_data('team',$val);
					$RESPS[$val]=($OTM[6]-$OTM[7]);
				}
				arsort($RESPS);
				foreach($RESPS as $key=>$val){
					$OL=read_data('player',$key);
					$OTM=read_data('team',$key);
					$html_flag_url=icon_url($OL[5]);$html_icon_url=icon_url($OL[6]);
					#表示
					$html_rank.="<tr style='text-align:center;'><td>";
					if($n!=0){
						$html_rank.="〃";
					}else{
						$html_rank.="$no";
					}
					$html_rank.="</td><td>$OL[1]</td><td><img src='$html_flag_url' height=$SET[icon_h_m] title='$OL[1]'></td><td><img src='$html_icon_url' height=$SET[icon_h_m] title='$OL[0]'></td>";
					$html_rank.="<td>$WINPER</td><td>$OTM[2]</td><td>$OTM[3]</td><td>$OTM[4]</td><td>$OTM[5]</td><td>$OTM[6]</td><td>$OTM[7]</td><td>".($OTM[6]-$OTM[7])."</td></tr>\n";
					$n++;
					$no++;
				}
			}else{
				$OL=read_data('player',$val);
				$OTM=read_data('team',$val);
				$html_flag_url=icon_url($OL[5]);$html_icon_url=icon_url($OL[6]);
				#表示
				$html_rank.="<tr style='text-align:center;'><td>$no</td>";
				$html_rank.="<td>$OL[1]</td><td><img src='$html_flag_url' height=$SET[icon_h_m] title='$OL[1]'></td><td><img src='$html_icon_url' height=$SET[icon_h_m] title='$OL[0]'></td>";
				$html_rank.="<td>$WINPER</td><td>$OTM[2]</td><td>$OTM[3]</td><td>$OTM[4]</td><td>$OTM[5]</td><td>$OTM[6]</td><td>$OTM[7]</td><td>".($OTM[6]-$OTM[7])."</td></tr>\n";
				$no++;
			}
			if($OL[20]==3&&$no>10){
				#break 2;
			}
		}
	}
	return $html_rank;
}
}
#----------------------------------------------------------#
# ランキングセット
#----------------------------------------------------------#
function ranking_set($TYPE,$RES,$DATA){
global$RESULT,$disp;
	if($RES=='-'){
	}else{
		$RESULT[$TYPE]["$RES"]=SET_STR_AR($RESULT[$TYPE],$RES,$DATA,'!');
	}
}
#----------------------------------------------------------#
# ランキングHTML
#----------------------------------------------------------#
function ranking_html($TYPE,$sort){
global$RESULT,$SET;
$RANKING=$RESULT[$TYPE];
	if(count($RANKING)>0){
		if($sort=='U'){
			krsort($RANKING);
		}elseif($sort=='D'){
			ksort($RANKING);
		}
		foreach($RANKING as $key=>$val){
			$KEYS=$val;
			$DATA=$key;
			break;
		}
	}
	#表示
	if($KEYS&&(($sort=='U'&&$DATA>0)||$sort=='D')){
		$IDs=explode('!',$KEYS);
		foreach($IDs as $key=>$val){
			$RANK=explode("?",$val);
			$CD_ID=$RANK[0];$PL_ID=$RANK[1];
			$OL=read_data('player',$PL_ID);
			$html_flag_url=icon_url($OL[5]);
			$CD=read_data('player_card',$CD_ID);
			$html.="<tr><td>$CD[0]</td><td><img src=\"$SET[dir_icon]/$CD[6]\" height=\"$SET[icon_h_m]\" title=\"$CD[0]\"></td><td><img src='$html_flag_url' height=$SET[icon_h_m] title='$OL[1]'></td><td>$DATA</td></tr>\n";
		}
	}else{
		$html="<tr><td colspan=4>該当者なし</td></tr>";
	}
	return $html;

}
#----------------------------------------------------------#
?>