<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];

  $lgdate=$_SESSION['lgdate'];

  $lgpath=$_SESSION['lgpath'];



  # Title Bar

  $title='Top Performances';



  page_head($title,$_SESSION['lgpath']);

  echo " <div id='topbar'>\n";

  main_titlebar($lgid,$_SESSION['lgpath'],$title);

  nav_toolbar("season","Season");

  echo " </div>   <!-- END topbar DIV -->\n";

  echo " <div id='contentpane'>\n";





  ################## START OF PAGE CONTENT #########################

  $simLen=get_sim_length($_SESSION['adminpath'],$lgid);



  include($_SESSION['adminpath']."/dbopen.php");



  ##### Get Team Info #####

  $query="SELECT team_id,abbr,name,nickname,logo_file_name,sub_league_id,allstar_team FROM teams WHERE league_id=$lgid;";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  while ($row=mysqli_fetch_array($result))			    

  {

    	  $tid=$row['team_id'];

	  $tname=$row['name']." ".$row['nickname'];

    	  $tname=str_replace(".","",$tname);

	  $teams[$tid]['name']=$tname;

    	  $teamcity[$tid]=$row['name'];

	  $tabbr[$tid]=$row['abbr'];

	  $teams[$tid]['logo']=urlencode($row['logo_file_name']);

    

	  $slid=$row['sub_league_id'];

	  $teamSLID[$tid]=$slid;

    	  if ($row['allstar_team']==1) {$subleagues[$slid]['asID']=$tid;}

  }



  ##### Get Games w/ Great Ind Performance #####

  $queryB="SELECT game_id,pgb.player_id,first_name,last_name,h,d,t,hr,rbi,sb FROM players_game_batting as pgb,players as p WHERE pgb.player_id=p.player_id AND pgb.league_id=".$lgid." AND (hr>2 OR h>5 OR rbi>7 or sb>3 OR ((h-d-t-hr)>0 AND d>0 AND t>0 AND hr>0));";

  if ($_GET['showq']==1) {echo "$queryB<br/>\n";}

  $resultB=mysqli_query($db,$queryB);

  while ($row=mysqli_fetch_array($resultB))

  {

    	  $gid=$row['game_id'];

	  $pid=$row['player_id'];

    	  $fi=$row['first_name'];

	  $fi=$fi[0];

    	  $name=$fi.". ".$row['last_name'];

	  $h=$row['h'];

    	  $d=$row['d'];

	  $t=$row['t'];

    	  $hr=$row['hr'];

	  $s=$h-$d-$t-$hr;

    	  $rbi=$row['rbi'];

	  $sb=$row['sb'];



	  if ($hr>2) 

   	  {

		  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> belts $hr HR's";

	 	  $indy.="+if(game_id=$gid,".(20*$hr).",0)";

	  }

    	  if ($h>5)

	  {

	 	  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> knocks $h hits";

		  $indy.="+if(game_id=$gid,".(10*$h).",0)";

   	  }

	  if ($rbi>7)

   	  {

		  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> plates $rbi";

	 	  $indy.="+if(game_id=$gid,".(10*$rbi).",0)";

	  }

    	  if ($sb>3)

	  {

	 	  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> steals $sb bases";

		  $indy.="+if(game_id=$gid,".(15*$sb).",0)";

   	  }

	  if (($s>0)&&($d>0)&&($t>0)&&($hr>0))

   	  {

		  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> hits for the cycle";

	 	  $indy.="+if(game_id=$gid,75,0)";

	  }

  }



  $queryP="SELECT game_id,pgp.player_id,first_name,last_name,k,(ip*3+ipf)/3 as ip,ha,cg,sho FROM players_game_pitching_stats as pgp,players as p WHERE pgp.player_id=p.player_id AND pgp.league_id=".$lgid." AND (k>14 OR ((ip*3+ipf)/3)>9 OR (ha=0 AND ((ip*3+ipf)/3)>7) OR (ha<3 AND cg=1 AND sho=1));";

  if ($_GET['showq']==1) {echo "$queryP<br/>\n";}

  $resultP=mysqli_query($db,$queryP);

  while ($row=mysqli_fetch_array($resultP))

  {

	  $gid=$row['game_id'];

    	  $pid=$row['player_id'];

	  $fi=$row['first_name'];

    	  $fi=$fi[0];

	  $name=$fi.". ".$row['last_name'];

    	  $k=$row['k'];

	  $ip=$row['ip'];

    	  $ha=$row['ha'];

	  $cg=$row['cg'];

    	  $sho=$row['sho'];

	  if (floor($ip)==$ip) {$dispIP=round($ip,0);} else {$dispIP=round(floor($ip),0)." ".round((3*($ip-floor($ip))),0)."/3";}



	  if ($k>14)

	  {

	 	  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> strikes out $k";

		  $indy.="+if(game_id=$gid,".(4*$k).",0)";

   	  }

	  if ($ip>9)

   	  {

		  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> goes ".$dispIP." innings";

	 	  $indy.="+if(game_id=$gid,".(6*$ip).",0)";

	  }

    	  if (($ha==0)&&($ip>7))

	  {

	 	  if ($cg==1) {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> pitches a no-hitter";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> fails to allow a hit";}

		  $indy.="+if(game_id=$gid,75,0)";

	  }

    	  if (($ha<3)&&($cg==1)&&($sho==1)&&($ha!=0))

	  {

	 	  $games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> pitches a $ha-hit shutout";

		  $indy.="+if(game_id=$gid,40,0)";

   	  }

  }



  ##### Get Games List #####

  $gList="";

  $pList="";

  $query="SELECT game_id,games.date,home_team,away_team,innings,runs0,runs1,hits0,hits1,errors0,errors1,winning_pitcher,losing_pitcher,save_pitcher,game_type,";

  $query.=" if(game_type=4,200,0)";

  $query.="+if(hits0<2,50,0)";

  $query.="+if(hits1<2,50,0)";

  $query.="+if((hits0+hits1)<7,10,if((hits0+hits1)>20,(hits0+hits1-20)/4,0))";

  $query.="+if(innings>12,50,0)";

  $query.="+if(ABS(runs0-runs1)<2,25,if(ABS(runs0-runs1)<3,10,0))";

  $query.="+if(innings>9,25,0)";

  $query.="+if((runs0+runs1)<3,50,0)";

  $query.="+if((runs0+runs1)>19,25,0)";

  $query.="+.001*(runs0+runs1+hits0+hits1)";

  $query.=$indy;

  $query.=" as score FROM games WHERE DATEDIFF('$lgdate',games.date)<$simLen AND played=1 AND league_id=$lgid ORDER BY score DESC";

  $query.=" LIMIT ";

  if ($simLen<5)  {$query.="5"; }

  if ($simLen<10) {$query.="10";}

  else            {$query.="15";}

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  $rowCnt=0;

	  while ($row=mysqli_fetch_array($result))

	  {

		  if (($rowCnt>($simLen/2))&&($row['score']<30)) {continue;}



		  $gid=$row['game_id'];

		  $gdate=$row['date'];



		  $tWins=$tWins+$row['win'];

		  $tLosses=$tLosses+$row['loss'];





		  $games[$gid][$gdate]=1;

		  $gList.=",$gid";



		  $games[$gid]['date']=$gdate;

		  $games[$gid]['hid']=$row['home_team'];

		  $games[$gid]['aid']=$row['away_team'];

		  $games[$gid]['innings']=$row['innings'];

		  $games[$gid]['runs0']=$row['runs0'];

		  $games[$gid]['runs1']=$row['runs1'];

		  $games[$gid]['hits0']=$row['hits0'];

		  $games[$gid]['hits1']=$row['hits1'];

		  $games[$gid]['errors0']=$row['errors0'];

		  $games[$gid]['errors1']=$row['errors1'];

		  $games[$gid]['game_type']=$row['game_type'];

		  $games[$gid]['gamescore']=$row['score'];

		  $gamedate[$gid]=$row['date'];



		  $wp=$row['winning_pitcher'];

		  $lp=$row['losing_pitcher'];

		  $sp=$row['save_pitcher'];

		  $games[$gid]['winning_pitcher']=$wp;

		  $games[$gid]['losing_pitcher']=$lp;

		  $games[$gid]['save_pitcher']=$sp;



		  $players[$wp]['fnd']=1;

		  $players[$lp]['fnd']=1;

		  $players[$sp]['fnd']=1;



		  $rowCnt++;

	  }

  }

  $gList=trim($gList,",");



  if ($gList=="")

  {

	  echo "<div class='textbox' style='padding:2px;'>No games found</div>\n";

    

	  mysqli_close($db);

	

	  ################## END OF PAGE #########################

	  echo " </div>   <!-- END contentpane DIV -->\n";



	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  ##### Get Inning-by-Inning Scores #####

  $query="SELECT game_id,team,inning,score FROM games_score WHERE game_id IN ($gList) ORDER BY game_id,team,inning";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  $prevGid=-1;

	  while ($row=mysqli_fetch_array($result))

	  {

		  $gid=$row['game_id'];

		  $tid=$row['team'];

		  $inn=$row['inning'];



		  $score=$row['score'];



		  if ($inn<=($games[$gid]['innings']-12)) {continue;}

	

		  if ($tid==0)

		  {

			  if ($prevGid!=$gid) {$innText[$gid]['inn']="<td class='hc' width=18>&#160;</td>";}

			  $innText[$gid]['inn'].="<td class='hc' width=15>$inn</td>";

		  }

		  elseif (($games[$gid]['runs1']>$games[$gid]['runs0'])&&($row['inning']>8)&&($row['inning']==$games[$gid]['innings'])&&($score==0)) {$score="X";}





		  $innText[$gid][$tid]=$innText[$gid][$tid]."<td class='ic' width=15>$score</td>";



		  $prevGid=$gid;

	  }

  }



  ##### Get Home Runs #####

  $query="SELECT player_id,team_id,game_id,hr FROM players_game_batting WHERE hr>0 AND game_id IN ($gList)";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $tid=$row['team_id'];

		  $gid=$row['game_id'];

		  $hr=$row['hr'];



		  $players[$pid]['fnd']=1;



		  $games[$gid]['hrs'][$tid][$pid]=$hr;

	  }

  }



  ##### Get Player Names #####

  if (isset($players))

  {

	  foreach ($players as $pid => $val) {$pList.=",$pid";}

	  $pList=trim($pList,",");

	

	  $query="SELECT player_id,first_name,last_name FROM players WHERE player_id IN ($pList)";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $pid=$row['player_id'];

			  $fi=$row['first_name'];

			  $fi=$fi[0];

			  $name=$fi.". ".$row['last_name'];

			  $players[$pid]['name']=$name;

		  }

	  }

  }



  ##Display Games

  echo "<table cellpadding=0 cellspacing=0 border=0 style='clear:left;'>\n";

  if ($recordText!="") {echo "<tr><td colspan=2><div id='first' class='textbox'>$recordText</div></td></tr>\n";}

  echo "<tr><td valign='top'>\n";

  echo "<div id='first' class='textbox'>\n";

  echo " <table cellpadding=0 cellspacing=0 border=0>\n";

  echo "  <tr class='title'><td style='padding:2px;'>Notable Games</td></tr>\n";

  echo "  <tr>\n";

  echo "   <td>\n";



  asort($gamedate);



  foreach($gamedate as $gid=>$gdate)

  {

    	  $hid=$games[$gid]['hid'];

	  $hname=$teams[$hid]['name'];

    	  $aid=$games[$gid]['aid'];

	  $aname=$teams[$aid]['name'];

	  $inns=$games[$gid]['innings'];



	  # Inning-by-Inning

	  $inntxt=$innText[$gid]['inn'];

	  $ainn=$innText[$gid][0];

	  $hinn=$innText[$gid][1];



	  for ($i=0;$i<(12-$inns);$i++)

   	  {

		  $inntxt.="<td class='hc' width=15>&#160;</td>";

	 	  $ainn.="<td class='ic' width=15>&#160;</td>";

		  $hinn.="<td class='ic' width=15>&#160;</td>";

   	  }

	  $inntxt.="<td class='hc' width=15>R</td><td class='hc' width=15>H</td><td class='hc' width=15>E</td>";

    	  $ainn.="<td class='icgb' width=15>".$games[$gid]['runs0']."</td><td class='ic' width=15>".$games[$gid]['hits0']."</td><td class='ic' width=15>".$games[$gid]['errors0']."</td>";

	  $hinn.="<td class='icgb' width=15>".$games[$gid]['runs1']."</td><td class='ic' width=15>".$games[$gid]['hits1']."</td><td class='ic' width=15>".$games[$gid]['errors1']."</td>";



	  # Pitchers

	  $wp=$games[$gid]['winning_pitcher'];

	  $lp=$games[$gid]['losing_pitcher'];

	  $sp=$games[$gid]['save_pitcher'];



	  $ptxt="W: <a href='./player.php?player_id=$wp'>".$players[$wp]['name']."</a> L: <a href='./player.php?player_id=$lp'>".$players[$lp]['name']."</a>";

    	  if ($spid!=0) {$ptxt.=" S: <a href='./player.php?player_id=$sp'>".$players[$sp]['name']."</a>";}



	  # HR Text

	  unset($hrtxt);

	  if (isset($games[$gid]['hrs'][$aid]))

	  {

		  $hrtxt[$aid]=$tabbr[$aid].": ";

		  $hrCnt=0;

		  foreach ($games[$gid]['hrs'][$aid] as $pid => $hr)

		  {

			  if ($hrCnt>0) {$hrtxt[$aid].=", ";}

			  $hrtxt[$aid].="<a href='./player.php?player_id=$pid'>".$players[$pid]['name']."</a>";

			  if ($hr>1) {$hrtxt[$aid].=" ($hr)";}

			  $hrCnt++;

		  }

	  }

	  if (isset($games[$gid]['hrs'][$hid]))

	  {

		  $hrtxt[$hid]=$tabbr[$hid].": ";

		  $hrCnt=0;

		  foreach ($games[$gid]['hrs'][$hid] as $pid => $hr)

		  {

			  if ($hrCnt>0) {$hrtxt[$hid].=", ";}

			  $hrtxt[$hid].="<a href='./player.php?player_id=$pid'>".$players[$pid]['name']."</a>";

			  if ($hr>1) {$hrtxt[$hid].=" ($hr)";}

			  $hrCnt++;

		  }

	  }

    	  if ((isset($games[$gid]['hrs'][$aid]))||(isset($games[$gid]['hrs'][$hid])))

	  {

		  $hrNote="HR - ".$hrtxt[$aid]." ".$hrtxt[$hid];

	  }

	  else {$hrNote="";}



	  # Game Note

    	  if (isset($games[$gid]['note']))

	  {

	 	  $gnote=$games[$gid]['note'];

		  $gnote=trim($gnote,", ");

	 	  $gnote="<br />Notes: ".$gnote;

	  }

   	  else {$gnote="";}

	    

	  unset($tmpdate);

	  $tmpdate=new DateTime($gdate);

    	  echo " <table cellspacing=0 cellpadding=0 style='border:0px;width:430px;margin:10px;'>\n";

	  echo "  <tr>\n";

    	  echo "   <td class='hl'>\n";

	  echo "    ".$tmpdate->format("M j, Y");

    	  echo ": <a href='$lgpath/box_scores/game_box_$gid.html'>Box Score</a>";

	  echo " | <a href='$lgpath/game_logs/log_$gid.html'>Game Log</a>";

	  echo " | <a href='./matchups.php?team_id1=$hid&team_id2=$aid'>Season Series</a>";

	  echo "   </td>\n";

    	  echo "  </tr>\n";

	  echo "  <tr>\n";

    	  echo "   <td>\n";

	  echo "    <table cellpadding=0 cellspacing=0 style='border:1px black solid;width:430px;margin-top:2px;margin-left:0px;'>\n";

    	  echo "     <tr>\n";

	  echo "      <td style='padding:1px;width:44px;border-right:1px solid #999999;'>\n";

    	  echo "       <img src='$lgpath/images/team_logos/".str_replace(".png","_40.png",$teams[$aid]['logo'])."' width=40 height=40><br>\n";

	  echo "       <img src='$lgpath/images/team_logos/".str_replace(".png","_40.png",$teams[$hid]['logo'])."' width=40 height=40>\n";

    	  echo "      </td>\n";

	  echo "      <td valign='top' style='padding:0px;margin:0px'>\n";

    	  echo "       <table cellspacing=0 cellpadding=1 style='width:100%;margin:0px;border:0px'>\n";

	  echo "        <tr>$inntxt</tr>\n";

    	  echo "        <tr><td class='gl'><a href='$lgpath/teams/team_$aid.html'>".$teamcity[$aid]."</a></td>";

	  echo "$ainn</tr>\n";

    	  echo "        <tr><td class='gl'><a href='$lgpath/teams/team_$hid.html'>".$teamcity[$hid]."</a></td>";

	  echo "$hinn</tr>\n";

    	  echo "        <tr><td colspan=16 class='gl' style='padding:6px 4px 4px 4px;'>".$ptxt."<br />".$hrNote.$gnote."</td></tr>\n";

	  echo "       </table>\n";

    	  echo "      </td>\n";

	  echo "     </tr>\n";

    	  echo "    </table>\n";

	  echo "   </td>\n";

    	  echo "  </tr>\n";

	  echo " </table>\n";

  }



  echo "   </td>\n";

  echo "  </tr>\n";						 

  echo " </table>\n";

  echo "</div>\n";



  echo "</td><td width='10px'>&nbsp;</td><td valign='top'>\n";



  ##### Get Notable Players #####

  echo "<div class='textbox' style='clear:right;'>\n";

  echo " <table cellpadding=0 cellspacing=0 border=0 width=460>\n";

  echo "  <tr class='title'><td style='padding:2px;'>Top Players</td></tr>\n";

  echo "  <tr>\n";

  echo "   <td>\n";



  echo " <div class='tablebox'>\n";

  echo "  <table cellpadding=0 cellspacing=0 border=0>\n";

  echo "   <tr class='title'><td style='padding:2px;'>Batters</td></tr>\n";

  echo "   <tr><td>\n";

  $query="SELECT p.player_id,first_name,last_name,t.* FROM players as p,(SELECT player_id,team_id,sum(h) as h,sum(hr) as hr,sum(rbi) as rbi,sum(r) as r,sum(sb) as sb,(sum(h)/sum(ab)) as avg,(sum(h)+sum(bb)+sum(hp))/(sum(ab)+sum(bb)+sum(hp)+sum(sf)) as obp,(sum(h)+sum(d)+2*sum(t)+3*sum(hr))/sum(ab) as slg,(0.47*(sum(h)-sum(d)-sum(t)-sum(hr)) + .78*sum(d) + 1.09*sum(t) + 1.4*sum(hr) + .33*(sum(bb)-sum(hp)) + .3*sum(sb) - .52*sum(cs) - .26*(sum(ab)-sum(h)-sum(gdp)) - .72*sum(gdp)) as lw FROM players_game_batting WHERE game_id IN (SELECT game_id FROM games WHERE league_id=".$lgid." AND played=1 AND DATEDIFF('".$lgdate."',games.date)<".$simLen.") GROUP BY player_id ORDER BY lw DESC LIMIT 20) as t WHERE p.player_id=t.player_id ORDER BY last_name,first_name";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  echo "   <table class='sortable' width=440>\n";

  echo "    <thead><tr class='headline'>\n";

  echo "     <td class='hsc2_l'>Player</td>";

  echo "<td class='hsc2'>AVG</td>";

  echo "<td class='hsc2'>R</td>";

  echo "<td class='hsc2'>H</td>";

  echo "<td class='hsc2'>HR</td>";

  echo "<td class='hsc2'>RBI</td>";

  echo "<td class='hsc2'>SB</td>";

  echo "<td class='hsc2'>OPS</td>";

  echo "</tr></thead>\n";

  $rownum=0;

  while ($row=mysqli_fetch_array($result))

  {

    	  $cls='s'.($rownum%2+1);

	  $tid=$row['team_id'];

    	  echo "    <tr class='$cls'>";

	  echo "<td class='".$cls."_l' sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href='./player.php?player_id=".$row['player_id']."'>".$row['first_name']." ".$row['last_name']."</a>, <a href='".$lgpath."/teams/team_".$tid.".html'>".$tabbr[$tid]."</a></td>";

    	  echo "<td>".strstr(sprintf("%.3f",$row['avg']),".")."</td>";

	  echo "<td>".$row['r']."</td>";

    	  echo "<td>".$row['h']."</td>";

	  echo "<td>".$row['hr']."</td>";

    	  echo "<td>".$row['rbi']."</td>";

	  echo "<td>".$row['sb']."</td>";

    	  $ops=sprintf("%.3f",$row['obp']+$row['slg']);

	  if ($ops<1) {$ops=strstr($ops,".");}      

    	  echo "<td>".$ops."</td>";

	  echo "</tr>\n";

    	  $rownum++;

  }

  echo "   </table>\n";

  echo "   </td></tr>\n";

  echo "  </table>\n";

  echo " </div>\n";



  echo " </td></tr><tr><td>\n";



  echo " <div class='tablebox'>\n";

  echo "  <table cellpadding=0 cellspacing=0 border=0>\n";

  echo "   <tr class='title'><td style='padding:2px;'>Pitchers</td></tr>\n";

  echo "   <tr><td>\n";

  $query="SELECT p.player_id,first_name,last_name,t.* FROM players as p,(SELECT player_id,team_id,((SUM(ip)*3+SUM(ipf))/3) as ip,sum(w) as w,sum(l) as l,sum(s) as sv,sum(k) as k,9*sum(er)/((SUM(ip)*3+SUM(ipf))/3) as era,(sum(bb)+sum(ha)+sum(hp))/((SUM(ip)*3+SUM(ipf))/3) as whip,sum(ha)/sum(ab) as oavg,(((sum(ha)+sum(bb)+sum(hp))*(0.89*(1.255*(sum(ha)-sum(hra))+4*sum(hra))+0.56*(sum(bb)+sum(hp)-sum(iw))))/(sum(bf)*((SUM(ip)*3+SUM(ipf))/3)))*9*0.75 as erc,3*((SUM(ip)*3+SUM(ipf))/3)+4*sum(w)-4*sum(l)+5*sum(s)+sum(k)-2*sum(ha)-2*sum(bb) as score FROM players_game_pitching_stats WHERE game_id IN (SELECT game_id FROM games WHERE league_id=".$lgid." AND played=1 AND DATEDIFF('".$lgdate."',games.date)<".$simLen.") GROUP BY player_id ORDER BY score DESC LIMIT 20) as t WHERE p.player_id=t.player_id ORDER BY last_name,first_name";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  echo "   <table class='sortable' width=440>\n";

  echo "    <thead><tr class='headline'>\n";

  echo "     <td class='hsc2_l'>Player</td>";

  echo "<td class='hsc2'>W</td>";

  echo "<td class='hsc2'>L</td>";

  echo "<td class='hsc2'>SV</td>";

  echo "<td class='hsc2'>IP</td>";

  echo "<td class='hsc2'>ERA</td>";

  echo "<td class='hsc2'>K</td>";

  echo "<td class='hsc2'>WHIP</td>";

  echo "<td class='hsc2'>OAVG</td>";

  echo "</tr></thead>\n";

  $rownum=0;

  while ($row=mysqli_fetch_array($result))

  {

    	  $cls='s'.($rownum%2+1);

	  $tid=$row['team_id'];

    	  echo "    <tr class='$cls'>";

	  echo "<td class='".$cls."_l' sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href='./player.php?player_id=".$row['player_id']."'>".$row['first_name']." ".$row['last_name']."</a>, <a href='".$lgpath."/teams/team_".$tid.".html'>".$tabbr[$tid]."</a></td>";

    	  echo "<td>".$row['w']."</td>";

	  echo "<td>".$row['l']."</td>";

    	  echo "<td>".$row['sv']."</td>";

	  echo "<td>".sprintf("%.1f",$row['ip'])."</td>";

    	  echo "<td>".sprintf("%.2f",$row['era'])."</td>";

	  echo "<td>".$row['k']."</td>";

    	  echo "<td>".sprintf("%.2f",$row['whip'])."</td>";

	  $oavg=sprintf("%.3f",$row['oavg']);

    	  if ($oavg<1) {$oavg=strstr($oavg,".");}

	  echo "<td>".$oavg."</td>";

    	  echo "</tr>\n";

	  $rownum++;

  }

  echo "   </table>\n";

  echo "   </td></tr>\n";

  echo "  </table>\n";

  echo " </div>\n";



  echo "   </td>\n";

  echo "  </tr>\n";						 

  echo " </table>\n";



  echo "</div>\n";


  $query="SELECT pp.*,pv.overall_value,talent_value,offensive_value,offensive_value_talent,pitching_value,pitching_value_talent FROM players_value as pv";
  $query.=" JOIN ";
  $query.="(SELECT p.position,p.role,p.first_name,p.last_name,p.player_id,p.team_id,p.organization_id FROM players as p JOIN ";
  $query.="(";
  $query.=" (SELECT pcgp.player_id,carG,simG";
  $query.=" FROM ";
  $query.="(SELECT player_id,SUM(g) as carG FROM `players_career_batting_stats` WHERE league_id=100 AND split_id=1 GROUP BY player_id) as pcgp";
  $query.=" JOIN ";
  $query.="(SELECT player_id,SUM(g) as simG FROM players_game_batting as pgb JOIN ";
  $query.="(SELECT game_id FROM games WHERE league_id=$lgid AND DATEDIFF('$lgdate',games.date)<8 AND played=1 ORDER BY games.date) as g ON pgb.game_id=g.game_id GROUP BY player_id) as psgp";
  $query.=" ON pcgp.player_id=psgp.player_id WHERE pcgp.carG<=psgp.simG)";
  $query.=" UNION ";
  $query.="(SELECT pcgp.player_id,carG,simG FROM (";
  $query.="SELECT player_id,SUM(g) as carG FROM `players_career_pitching_stats` WHERE league_id=100 AND split_id=1 GROUP BY player_id) as pcgp";
  $query.=" JOIN ";
  $query.="(SELECT player_id,SUM(g) as simG FROM players_game_pitching_stats as pgb JOIN ";
  $query.="(SELECT game_id FROM games WHERE league_id=$lgid AND DATEDIFF('$lgdate',games.date)<8 AND played=1 ORDER BY games.date) as g ON pgb.game_id=g.game_id GROUP BY player_id) as psgp";
  $query.=" ON pcgp.player_id=psgp.player_id WHERE pcgp.carG<=psgp.simG)";
  $query.=") as debutP";
  $query.=" ON p.player_id=debutP.player_id GROUP BY p.player_id) as pp";
  $query.=" ON pv.player_id=pp.player_id";
  $query.=" ORDER BY talent_value DESC,last_name,first_name LIMIT 25";

  echo " <div class='tablebox'>\n";
  echo "  <table cellpadding=0 cellspacing=0 border=0>\n";
  echo "   <tr class='title'><td style='padding:2px;'>Pitchers</td></tr>\n";
  echo "   <tr><td>\n";
  echo "   <table class='sortable' width=440>\n";
  echo "    <thead><tr class='headline'>\n";
  echo "     <td class='hsc2'>Pos</td>";
  echo "<td class='hsc2_l'>Player</td>";
  echo "</tr></thead>\n";
  $rownum=0;
  while ($row=mysqli_fetch_array($result))
  {
    $cls='s'.($rownum%2+1);
    $tid=$row['team_id'];
    $pos=$row['position'];
    if ($pos==1)
    echo "    <tr class='$cls'>";
    echo "<td sorttable_customkey=\"".(100*$row['position']+(position==1?$row['role']:0))."\">".get_pos($pos)."</td>";
    echo "<td class='".$cls."_l' sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href='./player.php?player_id=".$row['player_id']."'>".$row['first_name']." ".$row['last_name']."</a>, <a href='".$lgpath."/teams/team_".$tid.".html'>".$tabbr[$tid]."</a></td>";
    echo "<td>".$row['w']."</td>";
    echo "</tr>\n";
    
    $rownum++;
  }
  echo "   </table>\n";
  echo "   </td></tr>\n";
  echo "  </table>\n";
  echo " </div>\n";

  echo "</td>";
  echo "</tr>";
  echo "</table>\n";



  mysqli_close($db);



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";



  page_foot($_SESSION['lgpath']);





?>

