<?php
#----------------------------------------------------------#
# カード詳細
#----------------------------------------------------------#
function card_detailR(){
global$disp,$Q,$SET;
#--------------------------------------#
# データチェック
#--------------------------------------#
$db=sql_open();
check_id($_COOKIE[id],$_COOKIE[pass]);
$PL=read_data('player',$_COOKIE[id]);
$TM=read_data('team',$_COOKIE[id]);
#--------------------------------------#
# カードデータ読み込み
#--------------------------------------#
$CD=read_data('player_card',$Q[ch]);
$CD=ch_growth($CD);
if($CD){
	#カード表示
	$html.=ch_card($CD);
	#成績表示
	$CD_RB=mysql_fetch_assoc(mysql_query("SELECT * FROM card_res_b WHERE id='$CD[id]'"));
	$CD_RP=mysql_fetch_assoc(mysql_query("SELECT * FROM card_res_p WHERE id='$CD[id]'"));
	
	$ch_AVE=AVE($CD_RB[2],$CD_RB[3]);
	$ch_OBP=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]));
	$ch_SLG=AVE($CD_RB[2],$CD_RB[8]);
	$ch_OPS=$ch_OBP+$ch_SLG;
	$ch_AVEP=AVE($CD_RB[20],$CD_RB[21]);
	$ch_SBP=AVE(($CD_RB[11]+$CD_RB[12]),$CD_RB[11]);
	$ch_ERA=ERA($CD_RP[11],$CD_RP[9]);
	$ch_WHIP=WHIP($CD_RP[11],($CD_RP[3]+$CD_RP[15]+$CD_RP[16]));
	$ch_PAVE=AVE($CD_RP[2],$CD_RP[3]);
	$ch_PAVEP=AVE($CD_RP[20],$CD_RP[21]);
	$ch_PNUM=ch_ptime($CD_RP[11]);
$html.=<<<EOF
<hr><b>今季成績</b>
<table>
<tr><th>打率</th><th>得点圏</th><th>出塁率</th><th>長打率</th><th>OPS</th><th>試合</th><th>打席</th><th>打数</th><th>安打</th><th>単打</th><th>二塁</th><th>三塁</th><th>ＨＲ</th><th>塁打</th></tr>
<tr><td>$ch_AVE</td><td>$ch_AVEP</td><td>$ch_OBP</td><td>$ch_SLG</td><td>$ch_OPS</td><td>$CD_RB[0]</td><td>$CD_RB[1]</td><td>$CD_RB[2]</td><td>$CD_RB[3]</td><td>$CD_RB[4]</td><td>$CD_RB[5]</td><td>$CD_RB[6]</td><td>$CD_RB[7]</td><td>$CD_RB[8]</td></tr>
<tr><th>打点</th><th>得点</th><th>盗塁</th><th>盗塁死</th><th>成功率</th><th>犠打</th><th>犠飛</th><th>四球</th><th>敬遠</th><th>死球</th><th>三振</th><th>併殺</th><th>失策</th></tr>
<tr><td>$CD_RB[9]</td><td>$CD_RB[10]</td><td>$CD_RB[11]</td><td>$CD_RB[12]</td><td>$ch_SBP</td><td>$CD_RB[13]</td><td>$CD_RB[14]</td><td>$CD_RB[15]</td><td>$CD_RB[16]</td><td>$CD_RB[17]</td><td>$CD_RB[18]</td><td>$CD_RB[19]</td><td>$CD_RB[23]</td></tr>
</table><table>
<tr><th>ERA</th><th>WHIP</th><th>勝</th><th>負</th><th>Ｓ</th><th>先発</th><th>完投</th><th>完封</th><th>被打率</th><th>得点圏</th><th>投球回</th><th>登板</th></tr>
<tr><td>$ch_ERA</td><td>$ch_WHIP</td><td>$CD_RP[22]</td><td>$CD_RP[23]</td><td>$CD_RP[24]</td><td>$CD_RP[28]</td><td>$CD_RP[26]</td><td>$CD_RP[27]</td><td>$ch_PAVE</td><td>$ch_PAVEP</td><td>$ch_PNUM</td><td>$CD_RP[0]</td></tr>
<tr><th>打者</th><th>打数</th><th>被安</th><th>被単</th><th>被二</th><th>被三</th><th>被本</th><th>被塁</th><th>自責</th><th>失点</th><th>犠打</th><th>犠飛</th><th>与四</th><th>与敬</th><th>与死</th><th>奪三</th><th>暴投</th></tr>
<tr><td>$CD_RP[1]</td><td>$CD_RP[2]</td><td>$CD_RP[3]</td><td>$CD_RP[4]</td><td>$CD_RP[5]</td><td>$CD_RP[6]</td><td>$CD_RP[7]</td><td>$CD_RP[8]</td><td>$CD_RP[9]</td><td>$CD_RP[10]</td><td>$CD_RP[13]</td><td>$CD_RP[14]</td><td>$CD_RP[15]</td><td>$CD_RP[16]</td><td>$CD_RP[17]</td><td>$CD_RP[18]</td><td>$CD_RP[19]</td></tr>
</table>
EOF;
	#ポイント振り&登録変更
	if($_COOKIE[id]==$CD[player_id]){
	#ポイント設定
	#BTP
	$chB[0]=ch_st_pt($CD[11]);#ミート
	$chB[1]=ch_st_pt($CD[12]);#パワー
	$chB[2]=ch_st_pt($CD[13]);#走力
	$chB[3]=ch_st_pt($CD[14]);#肩力
	$chB[4]=ch_st_pt($CD[15]);#守備力
	$chB[5]=ch_st_pt($CD[16]);#反応
	$chB[6]=ch_st_pt($CD[17]);#E避
	#PCP
	$chP[0]=ch_st_pt($CD[21]);#球速
	$chP[1]=ch_st_pt($CD[22]);#コントロール
	$chP[2]=ch_st_pt($CD[23]);#スタミナ
	$chP[3]=ch_st_pt($CD[24]);#変化量
	$chP[4]=ch_st_pt($CD[25]);
	$chP[5]=ch_st_pt($CD[26]);
	$chP[6]=ch_st_pt($CD[27]);
	$chP[7]=ch_st_pt($CD[28]);
	#DISP
	$display='style="display:none;"';
	if(!$CD[24]){$chP_BR[0]=$display;}
	if(!$CD[25]){$chP_BR[1]=$display;}
	if(!$CD[26]){$chP_BR[2]=$display;}
	if(!$CD[27]){$chP_BR[3]=$display;}
	if(!$CD[28]){$chP_BR[4]=$display;}
#表示
$html.=<<<EOF
<hr><b>成長</b><br>
<table><form method=POST><tr><td colspan=2>
$CD[0]&nbsp;Lv.$CD[7]&nbsp;残りP:<input type=text name="point" id="point" value="$CD[45]" disabled style="width:30px;">
</td></tr><tr><td valign=top><table>
<tr><th>&nbsp;</th><th style="width:55px;">能力値</th><th>P</th><th>P</th><th>&nbsp;</th></tr>
<tr><th>ミート</th><td id="chB0">$CD[11]</td><td><input type=text name="chb0P" id="chB0P" value="0" style="width:30px;"></td><td>$CD[46]</td><td><input type=button value="増" onclick="card_growth('b','0','u')"><input type=button value="減" onclick="card_growth('b','0','d')"></td></tr>
<tr><th>パワー</th><td id="chB1">$CD[12]</td><td><input type=text name="chb1P" id="chB1P" value="0" style="width:30px;"></td><td>$CD[47]</td><td><input type=button value="増" onclick="card_growth('b','1','u')"><input type=button value="減" onclick="card_growth('b','1','d')"></td></tr>
<tr><th>走力</th><td id="chB2">$CD[13]</td><td><input type=text name="chb2P" id="chB2P" value="0" style="width:30px;"></td><td>$CD[48]</td><td><input type=button value="増" onclick="card_growth('b','2','u')"><input type=button value="減" onclick="card_growth('b','2','d')"></td></tr>
<tr><th>肩力</th><td id="chB3">$CD[14]</td><td><input type=text name="chb3P" id="chB3P" value="0" style="width:30px;"></td><td>$CD[49]</td><td><input type=button value="増" onclick="card_growth('b','3','u')"><input type=button value="減" onclick="card_growth('b','3','d')"></td></tr>
<tr><th>守備力</th><td id="chB4">$CD[15]</td><td><input type=text name="chb4P" id="chB4P" value="0" style="width:30px;"></td><td>$CD[50]</td><td><input type=button value="増" onclick="card_growth('b','4','u')"><input type=button value="減" onclick="card_growth('b','4','d')"></td></tr>
<tr><th>反応</th><td id="chB5">$CD[16]</td><td><input type=text name="chb5P" id="chB5P" value="0" style="width:30px;"></td><td>$CD[51]</td><td><input type=button value="増" onclick="card_growth('b','5','u')"><input type=button value="減" onclick="card_growth('b','5','d')"></td></tr>
<tr><th>E回避</th><td id="chB6">$CD[17]</td><td><input type=text name="chb6P" id="chB6P" value="0" style="width:30px;"></td><td>$CD[52]</td><td><input type=button value="増" onclick="card_growth('b','6','u')"><input type=button value="減" onclick="card_growth('b','6','d')"></td></tr>
</table></td><td valign=top><table>
<tr><th>&nbsp;</th><th style="width:55px;">能力値</th><th>P</th><th>P</th><th>&nbsp;</th></tr>
<tr><th>球速</th><td id="chP0">$CD[21]</td><td><input type=text name="chp0P" id="chP0P" value="0" style="width:30px;"></td><td>$CD[53]</td><td><input type=button value="増" onclick="card_growth('p','0','u')"><input type=button value="減" onclick="card_growth('p','0','d')"></td></tr>
<tr><th>コントロール</th><td id="chP1">$CD[22]</td><td><input type=text name="chp1P" id="chP1P" value="0" style="width:30px;"></td><td>$CD[54]</td><td><input type=button value="増" onclick="card_growth('p','1','u')"><input type=button value="減" onclick="card_growth('p','1','d')"></td></tr>
<tr><th>スタミナ</th><td id="chP2">$CD[23]</td><td><input type=text name="chp2P" id="chP2P" value="0" style="width:30px;"></td><td>$CD[55]</td><td><input type=button value="増" onclick="card_growth('p','2','u')"><input type=button value="減" onclick="card_growth('p','2','d')"></td></tr>
<tr><th>スライダー</th><td id="chP3">$CD[24]</td><td><input type=text name="chp3P" id="chP3P" value="0" style="width:30px;"></td><td>$CD[56]</td><td><input type=button value="増" onclick="card_growth('p','3','u')" $chP_BR[0]><input type=button value="減" onclick="card_growth('p','3','d')" $chP_BR[0]></td></tr>
<tr><th>カーブ</th><td id="chP4">$CD[25]</td><td><input type=text name="chp4P" id="chP4P" value="0" style="width:30px;"></td><td>$CD[57]</td><td><input type=button value="増" onclick="card_growth('p','4','u')" $chP_BR[1]><input type=button value="減" onclick="card_growth('p','4','d')" $chP_BR[1]></td></tr>
<tr><th>フォーク</th><td id="chP5">$CD[26]</td><td><input type=text name="chp5P" id="chP5P" value="0" style="width:30px;"></td><td>$CD[58]</td><td><input type=button value="増" onclick="card_growth('p','5','u')" $chP_BR[2]><input type=button value="減" onclick="card_growth('p','5','d')" $chP_BR[2]></td></tr>
<tr><th>シンカー</th><td id="chP6">$CD[27]</td><td><input type=text name="chp6P" id="chP6P" value="0" style="width:30px;"></td><td>$CD[59]</td><td><input type=button value="増" onclick="card_growth('p','6','u')" $chP_BR[3]><input type=button value="減" onclick="card_growth('p','6','d')" $chP_BR[3]></td></tr>
<tr><th>シュート</th><td id="chP7">$CD[28]</td><td><input type=text name="chp7P" id="chP7P" value="0" style="width:30px;"></td><td>$CD[60]</td><td><input type=button value="増" onclick="card_growth('p','7','u')" $chP_BR[4]><input type=button value="減" onclick="card_growth('p','7','d')" $chP_BR[4]></td></tr>
</table></td></tr><tr><td colspan=2>
<span style="color:red;" id="error"></span>
<input type="submit" value="決定" id="submit">
</td></tr>
<input type=hidden name=r value="card_detail_pt">
<input type=hidden name="ch" value="$CD[id]">
</form></table>
EOF;
#登録変更
#icon
$html_icon_url=icon_url($CD[6]);
$i=0;
foreach($SET[list_icon] as $icon){
	$html_icon.="<option value='$icon'>No.$i\n";
	$i++;
}
$html.=<<<EOF
<hr><b>登録変更</b>
<table><form method=POST action="$SET[path_index]" name=chara onSubmit="return name_check();">
<tr><th colspan=2><input type=submit value="登録変更"></th></tr>
<tr><td>
<input type=text name=name size=16 maxlength=12 value="$CD[0]"></td><td style="text-align:center;">
<img name="icon_image" src="$html_icon_url" height=$SET[icon_h]><br>
<select name="icon" onChange="icon_check();">
<option selected value="$CD[6]">設定済み
$html_icon</select><br>
(<a href="$SET[path_index]?r=icon" target="_blank">全キャラアイコン確認</a>)
</td></tr>
<input type=hidden name=ch value="$CD[id]">
<input type=hidden name=r value="card_detail_edit">
</form>
</table>
EOF;
	}
}else{
	$html.="カードのデータが読み取れません...";
}
#--------------------------------------#
$title=title($_COOKIE[id]);
sql_close($db);
#--------------------------------------#

$disp.=head();
#ポイント振り準備
if($_COOKIE[id]==$CD[player_id]){
$disp.=<<<EOF
<script type="text/javascript">
<!--
//MAX
max=$CD[45];//残りポイント
sum=0;

//ST
st=new Array();
EOF;
$disp.="st[0]=\"".ch_st(1,0)."\";\n";
$disp.="st[1]=\"".ch_st(11,0)."\";\n";
$disp.="st[2]=\"".ch_st(21,0)."\";\n";
$disp.="st[3]=\"".ch_st(31,0)."\";\n";
$disp.="st[4]=\"".ch_st(41,0)."\";\n";
$disp.="st[5]=\"".ch_st(51,0)."\";\n";
$disp.="st[6]=\"".ch_st(61,0)."\";\n";
$disp.="st[7]=\"".ch_st(71,0)."\";\n";
$disp.="st[8]=\"".ch_st(81,0)."\";\n";
$disp.="st[9]=\"".ch_st(91,0)."\";\n";
$disp.="st[10]=\"".ch_st(101,0)."\";\n";
$disp.="st[11]=\"".ch_st(111,0)."\";\n";
$disp.=<<<EOF
//ST_B,BP
st_b=new Array($chB[0],$chB[1],$chB[2],$chB[3],$chB[4],$chB[5],$chB[6]);
st_bp=new Array(0,0,0,0,0,0,0);
//ST_P,PC
st_p=new Array($chP[0],$chP[1],$chP[2],$chP[3],$chP[4],$chP[5],$chP[6],$chP[7]);
st_pp=new Array(0,0,0,0,0,0,0,0);
st_pc=new Array("$CD[56]","$CD[57]","$CD[58]","$CD[59]","$CD[60]");

function set_get(num){
	var sum=Math.floor(Math.pow(2,(3/2))/5*Math.pow((5*num),(1/2))*100)/100;
	return sum;
}

function card_st(){
	//BT
	chBN_st=new Array();//ST
	chB_st=new Array();//ST表\示
	for(var i=0;i<=6;i++){
		chBN_st[i]=set_get(st_b[i]+st_bp[i]);
		chB_st[i]=Math.floor(chBN_st[i]/10);
		//表\示
		var name1="chB"+i;
		var name2="chB"+i+"P";
		document.getElementById(name1).innerHTML=st[chB_st[i]]+"&nbsp;"+chBN_st[i];
		document.getElementById(name2).value=st_bp[i];
	}
	//PC
	chPN_st=new Array();//ST
	chP_st=new Array();//ST表\示
	for(var i=0;i<=7;i++){
		chPN_st[i]=set_get(st_p[i]+st_pp[i]);
		chP_st[i]=Math.floor(chPN_st[i]/10);
		//表\示
		var name1="chP"+i;
		var name2="chP"+i+"P";
		document.getElementById(name1).innerHTML=st[chP_st[i]]+"&nbsp;"+chPN_st[i];
		document.getElementById(name2).value=st_pp[i];
	}
	//
	document.getElementById('point').value=(max-sum);
	//
	ch_pc=new Array();//STcheck
	ch_pc[0]=st_pp[3]+st_pc[0];
	ch_pc[1]=st_pp[4]+st_pc[1];
	ch_pc[2]=st_pp[5]+st_pc[2];
	ch_pc[3]=st_pp[6]+st_pc[3];
	ch_pc[4]=st_pp[7]+st_pc[4];
	var check=0;
	if(ch_pc[0]>0){check+=1;}
	if(ch_pc[1]>0){check+=1;}
	if(ch_pc[2]>0){check+=1;}
	if(ch_pc[3]>0){check+=1;}
	if(ch_pc[4]>0){check+=1;}

	if(max==0){
		document.getElementById('error').innerHTML="成長ポイントが0です";
		document.getElementById('submit').style.display='none';
	}else if(check>=3){
		document.getElementById('error').innerHTML="変化球量は2種類まで成長させることができます。<br>";
		document.getElementById('submit').style.display='none';
	}else if((max-sum)==0){
		document.getElementById('error').innerHTML="残りポイントが0です";
		document.getElementById('submit').style.display='block';
	}else{
		document.getElementById('error').innerHTML="";
		document.getElementById('submit').style.display='block';
	}

}

function card_growth(type,no,ud){

	var bpa=st_bp[0]+st_bp[1]+st_bp[2]+st_bp[3]+st_bp[4]+st_bp[5]+st_bp[6];
	var ppa=st_pp[0]+st_pp[1]+st_pp[2]+st_pp[3]+st_pp[4]+st_pp[5]+st_pp[6]+st_pp[7];
	var sumc=bpa+ppa;
	//
	var ok=1;
	for(var i=0;i<=6;i++){
		if(st_bp[i]<0){
			ok=0;
		}
	}
	for(var i=0;i<=7;i++){
		if(st_pp[i]<0){
			ok=0;
		}
	}
	//
	if((ud=="u"&&(max-sumc)>=5)||(ud=="d"&&sumc>=5&&ok==1)){
		if(type == "b"){
			if(ud == "u"){
				st_bp[no]+=5;
			}else if(ud == "d"){
				if((st_bp[no]-5)>=0){
					st_bp[no]-=5;
				}
			}
			if(st_bp[no]<0){
				st_bp[no]=0;
			}
		}else if(type == "p"){
			if(ud == "u"){
				st_pp[no]+=5;
			}else if(ud == "d"){
				if((st_pp[no]-5)>=0){
					st_pp[no]-=5;
				}
			}
			if(st_bp[no]<0){
				st_pp[no]=0;
			}
		}
		//
		var bpa=st_bp[0]+st_bp[1]+st_bp[2]+st_bp[3]+st_bp[4]+st_bp[5]+st_bp[6];
		var ppa=st_pp[0]+st_pp[1]+st_pp[2]+st_pp[3]+st_pp[4]+st_pp[5]+st_pp[6]+st_pp[7];
		sum=bpa+ppa;
		card_st();
	}
}
//登録変更
function name_check(){
	if(document.chara.name.value == ''){
		window.alert("選手名が未入力です。");
		return false;
	}else if(document.chara.name.value.match('[&! =.,<>]') != null){
		window.alert("選手名には半角記号or半角スペースは使用できません。");
		return false;
	}else{
		if(confirm('選手登録を変更します、よろしいですか？')){
			return true;
		}else{
				return false;
		}
	}
}
function icon_check(){
		document.chara.icon_image.src='$SET[dir_icon]/'+document.chara.icon.value;
}
//-->
</script>
EOF;
}
$disp.=html_bar();
$disp.=$title;
$disp.="<tr><td colspan=2";
if($_COOKIE[id]==$CD[player_id]){$disp.="  onMouseOver=\"card_st();\"";}
$disp.=">";
$disp.=<<<EOF
<a href="$SET[path_index]?r=card_list">カードリスト</a>&gt;カード詳細(<a href="$SET[path_index]?r=card_detail&ch=$CD[id]">URL</a>)<blockquote>
$html
</blockquote>
</td></tr>
EOF;
$disp.=foot();
}
#----------------------------------------------------------#
# カードポイント振り分け
#----------------------------------------------------------#
function card_detail_ptR(){
global$disp,$Q,$SET;
#--------------------------------------#
# データチェック
#--------------------------------------#
$db=sql_open();
check_id($_COOKIE[id],$_COOKIE[pass]);
#--------------------------------------#
# データ書き換え
#--------------------------------------#
$check=data_check('player_card','id',$Q[ch]);
if($check){
	$CD=read_dataL('player_card',$Q[ch]);
	#ポイントチェック
	$check_up_pt=0;
	#B
	for($i=0;$i<=6;$i++){
		$q_name='chb'.$i.'P';
		$pt_num=46+$i;
		if($Q[$q_name]>0){
			$Q[$q_name]=floor($Q[$q_name]);
			$check_up_pt+=$Q[$q_name];
			$CD[$pt_num]+=$Q[$q_name];
		}
	}
	#P
	for($i=0;$i<=7;$i++){
		$q_name='chp'.$i.'P';
		$pt_num=53+$i;
		if($Q[$q_name]>0){
			$Q[$q_name]=floor($Q[$q_name]);
			$check_up_pt+=$Q[$q_name];
			$CD[$pt_num]+=$Q[$q_name];
		}
	}
	#check
	if($CD[45]>=$check_up_pt&&$check_up_pt>=0){
		$CD[45]-=$check_up_pt;
		update('player_card',$Q[ch],$CD);
	}
}
#--------------------------------------#
sql_close($db);
#--------------------------------------#
card_detail();
}
#----------------------------------------------------------#
# カード情報変更
#----------------------------------------------------------#
function card_detail_editR(){
global$disp,$Q,$SET;
#--------------------------------------#
# データチェック
#--------------------------------------#
$db=sql_open();
check_id($_COOKIE[id],$_COOKIE[pass]);
#--------------------------------------#
# データ書き換え
#--------------------------------------#
$check=data_check('player_card','id',$Q[ch]);
if($check){
	$CD=read_dataL('player_card',$Q[ch]);
	$CD[0]=$Q[name];
	$CD[6]=$Q[icon];
	update('player_card',$Q[ch],$CD);
}
#--------------------------------------#
sql_close($db);
#--------------------------------------#
card_detail();
}
#----------------------------------------------------------#
?>